blob: ef8ff883fc730722bdffd8deb5b3e14c266b3052 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Kishor PK49831502017-04-21 17:55:43 +05305 * Copyright (c) 2009-2017, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Mayank Grover351a75e2017-05-30 20:06:08 +053051#include <ab_partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070052#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070053#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070054#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070055#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030056#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070057#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070058#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070059#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053060#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080061#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053062#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080063#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080064#if USE_RPMB_FOR_DEVINFO
65#include <rpmb.h>
66#endif
Dima Zavin214cc642009-01-26 11:16:21 -080067
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070068#if ENABLE_WBC
69#include <pm_app_smbchg.h>
70#endif
71
Neeti Desai17379b82012-06-04 18:42:53 -070072#if DEVICE_TREE
73#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070074#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070075#endif
76
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053077#if WDOG_SUPPORT
78#include <wdog.h>
79#endif
80
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070081#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070082#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080083#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080084#include "bootimg.h"
85#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070086#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080087#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070088#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070089#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070090#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070091#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020092#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070093#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080094#include <menu_keys_detect.h>
95#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070096#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070097
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070098extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070099extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700100extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700101extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530102 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700103
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700104void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700105void write_device_info_mmc(device_info *dev);
106void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700107static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700108static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Kishor PK38ed93d2017-04-25 14:19:26 +0530109static inline uint64_t validate_partition_size();
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530110bool pwr_key_is_pressed = false;
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530111static bool is_systemd_present=false;
Mayank Grover351a75e2017-05-30 20:06:08 +0530112static void publish_getvar_multislot_vars();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700113/* fastboot command function pointer */
114typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
115
116struct fastboot_cmd_desc {
117 char * name;
118 fastboot_cmd_fn cb;
119};
120
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700121#define EXPAND(NAME) #NAME
122#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700123
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700124#define DISPLAY_PANEL_HDMI "hdmi"
125
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800126#ifdef MEMBASE
127#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
128#else
David Ng183a7422009-12-07 14:55:21 -0800129#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800130#endif
131
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700132#ifndef MEMSIZE
133#define MEMSIZE 1024*1024
134#endif
135
136#define MAX_TAGS_SIZE 1024
137
Kun Liang2f1601a2013-08-12 16:29:54 +0800138/* make 4096 as default size to ensure EFS,EXT4's erasing */
139#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700140#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530141#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800142
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700143#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700144#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800145
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800146#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
147
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700148#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
149
Ameya Thakur10a33452016-06-13 14:24:26 -0700150//Size of the header that is used in case the boot image has
151//a uncompressed kernel + appended dtb
152#define PATCHED_KERNEL_HEADER_SIZE 20
153
154//String used to determine if the boot image has
155//a uncompressed kernel + appended dtb
156#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
157
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800158#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700159static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700160#else
David Ng183a7422009-12-07 14:55:21 -0800161static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700162#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800163static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300164static const char *androidboot_mode = " androidboot.mode=";
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530165
166static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target";
Matthew Qind886f3c2014-01-17 16:52:01 +0800167static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800168static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800169static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700170static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300171static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200172static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800173
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800174static const char *baseband_apq = " androidboot.baseband=apq";
175static const char *baseband_msm = " androidboot.baseband=msm";
176static const char *baseband_csfb = " androidboot.baseband=csfb";
177static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700178static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800179static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700180static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800181static const char *baseband_dsda = " androidboot.baseband=dsda";
182static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800183static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800184static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530185static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Mayank Grover351a75e2017-05-30 20:06:08 +0530186static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
187static const char *skip_ramfs = " skip_initramfs";
188static char *sys_path_cmdline = " rootwait ro init=/init root=/dev/mmcblk0p%d"; /*This will be updated*/
Ajay Dudanid04110c2011-01-17 23:55:07 -0800189
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700190#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530191#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700192static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700193static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530194static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700195//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700196
197struct verified_boot_verity_mode vbvm[] =
198{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700199#if ENABLE_VB_ATTEST
200 {false, "eio"},
201#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700202 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700203#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700204 {true, "enforcing"},
205};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700206struct verified_boot_state_name vbsn[] =
207{
208 {GREEN, "green"},
209 {ORANGE, "orange"},
210 {YELLOW,"yellow"},
211 {RED,"red" },
212};
213#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530214#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700215/*As per spec delay wait time before shutdown in Red state*/
216#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700217static unsigned page_size = 0;
218static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530219static unsigned mmc_blocksize = 0;
220static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700221static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
222static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530223static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800224static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700225static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700226bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530227static uint32_t dt_size = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700228
Shashank Mittalcd98d472011-08-02 14:29:24 -0700229/* Assuming unauthorized kernel image by default */
230static int auth_kernel_img = 0;
Parth Dixit6f5822f2015-10-18 01:20:53 +0530231#if VBOOT_MOTA
232static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
233#else
lijuang511a2b52015-08-14 20:50:51 +0800234static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
Parth Dixit6f5822f2015-10-18 01:20:53 +0530235#endif
vijay kumarc65876c2015-04-24 13:29:16 +0530236static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700237
vijay kumarca2e6812015-07-08 20:28:25 +0530238static char frp_ptns[2][8] = {"config","frp"};
239
lijuang511a2b52015-08-14 20:50:51 +0800240static const char *critical_flash_allowed_ptn[] = {
241 "aboot",
242 "rpm",
243 "tz",
244 "sbl",
245 "sdi",
246 "sbl1",
247 "xbl",
248 "hyp",
249 "pmic",
250 "bootloader",
251 "devinfo",
252 "partition"};
253
Dima Zavin42168f22009-01-30 11:52:22 -0800254struct atag_ptbl_entry
255{
256 char name[16];
257 unsigned offset;
258 unsigned size;
259 unsigned flags;
260};
261
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700262/*
263 * Partition info, required to be published
264 * for fastboot
265 */
266struct getvar_partition_info {
267 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
268 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
269 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
270 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
271 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
272};
273
274/*
275 * Right now, we are publishing the info for only
276 * three partitions
277 */
278struct getvar_partition_info part_info[] =
279{
280 { "system" , "partition-size:", "partition-type:", "", "ext4" },
281 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
282 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
283};
284
285char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700286char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800287char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700288char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530289char panel_display_mode[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800290
291#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800292char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800293char battery_soc_ok [MAX_RSP_SIZE];
294#endif
295
lijuangf16461c2015-08-03 17:09:34 +0800296char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700297
Greg Griscod2471ef2011-07-14 13:00:42 -0700298extern int emmc_recovery_init(void);
299
Kinson Chik0b1c8162011-08-31 16:31:57 -0700300#if NO_KEYPAD_DRIVER
301extern int fastboot_trigger(void);
302#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700303
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800304static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700305{
306 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700307#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800308 if (is_arm64)
309 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
310 else
311 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700312 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
313 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700314#endif
315}
316
Dima Zavin42168f22009-01-30 11:52:22 -0800317static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
318{
319 struct atag_ptbl_entry atag_ptn;
320
321 memcpy(atag_ptn.name, ptn->name, 16);
322 atag_ptn.name[15] = '\0';
323 atag_ptn.offset = ptn->start;
324 atag_ptn.size = ptn->length;
325 atag_ptn.flags = ptn->flags;
326 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
327 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
328}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800329
lijuang102dfa92015-10-09 18:31:03 +0800330#if CHECK_BAT_VOLTAGE
331void update_battery_status(void)
332{
333 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
334 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
335}
336#endif
337
Neeti Desaie245d492012-06-01 12:52:13 -0700338unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800339{
David Ng183a7422009-12-07 14:55:21 -0800340 int cmdline_len = 0;
341 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800342 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700343 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800344 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300345 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700346 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700347 char *boot_dev_buf = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +0530348 bool is_mdtp_activated = 0;
349 int current_active_slot = INVALID;
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530350
351#if USE_LE_SYSTEMD
352 is_systemd_present=true;
353#endif
354
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700355#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530356#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700357 uint32_t boot_state = boot_verify_get_state();
358#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530359#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700360
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200361#ifdef MDTP_SUPPORT
362 mdtp_activated(&is_mdtp_activated);
363#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800364
Brian Swetland9c4c0752009-01-25 16:23:50 -0800365 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800366 cmdline_len = strlen(cmdline);
367 have_cmdline = 1;
368 }
369 if (target_is_emmc_boot()) {
370 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800371#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700372 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
373 ASSERT(boot_dev_buf);
374 platform_boot_dev_cmdline(boot_dev_buf);
375 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700376#endif
David Ng183a7422009-12-07 14:55:21 -0800377 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800378
379 cmdline_len += strlen(usb_sn_cmdline);
380 cmdline_len += strlen(sn_buf);
381
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700382#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530383#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700384 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700385 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
386 {
387 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
388 ASSERT(0);
389 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700390 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Parth Dixita5715a02015-10-29 12:25:10 +0530391 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700392#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530393#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700394
Pavel Nedev5614d222013-06-17 18:01:02 +0300395 if (boot_into_recovery && gpt_exists)
396 cmdline_len += strlen(secondary_gpt_enable);
397
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200398 if(is_mdtp_activated)
399 cmdline_len += strlen(mdtp_activated_flag);
400
Pavel Nedev328ac822013-04-05 15:25:11 +0300401 if (boot_into_ffbm) {
402 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530403
404 if(is_systemd_present)
405 cmdline_len += strlen(systemd_ffbm_mode);
406
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700407 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800408 /* reduce kernel console messages to speed-up boot */
409 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800410 } else if (boot_reason_alarm) {
411 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800412 } else if ((target_build_variant_user() || device.charger_screen_enabled)
413 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700414 pause_at_bootup = 1;
415 cmdline_len += strlen(battchg_pause);
416 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800417
Shashank Mittalcd98d472011-08-02 14:29:24 -0700418 if(target_use_signed_kernel() && auth_kernel_img) {
419 cmdline_len += strlen(auth_kernel);
420 }
421
Joonwoo Park61112782013-10-02 19:50:39 -0700422 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
423 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530424 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700425 have_target_boot_params = 1;
426 cmdline_len += strlen(target_boot_params);
427 }
428
Ajay Dudanid04110c2011-01-17 23:55:07 -0800429 /* Determine correct androidboot.baseband to use */
430 switch(target_baseband())
431 {
432 case BASEBAND_APQ:
433 cmdline_len += strlen(baseband_apq);
434 break;
435
436 case BASEBAND_MSM:
437 cmdline_len += strlen(baseband_msm);
438 break;
439
440 case BASEBAND_CSFB:
441 cmdline_len += strlen(baseband_csfb);
442 break;
443
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800444 case BASEBAND_SVLTE2A:
445 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800446 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700447
448 case BASEBAND_MDM:
449 cmdline_len += strlen(baseband_mdm);
450 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700451
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800452 case BASEBAND_MDM2:
453 cmdline_len += strlen(baseband_mdm2);
454 break;
455
Amol Jadi5c61a952012-05-04 17:05:35 -0700456 case BASEBAND_SGLTE:
457 cmdline_len += strlen(baseband_sglte);
458 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530459
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800460 case BASEBAND_SGLTE2:
461 cmdline_len += strlen(baseband_sglte2);
462 break;
463
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530464 case BASEBAND_DSDA:
465 cmdline_len += strlen(baseband_dsda);
466 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800467
468 case BASEBAND_DSDA2:
469 cmdline_len += strlen(baseband_dsda2);
470 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530471 case BASEBAND_APQ_NOWGR:
472 cmdline_len += strlen(baseband_apq_nowgr);
473 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800474 }
475
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300476#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800477 if (cmdline) {
478 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530479 target_display_panel_node(display_panel_buf,
480 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800481 strlen(display_panel_buf)) {
482 cmdline_len += strlen(display_panel_buf);
483 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700484 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300485#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700486
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800487 if (target_warm_boot()) {
488 warm_boot = true;
489 cmdline_len += strlen(warmboot_cmdline);
490 }
491
Mayank Grover351a75e2017-05-30 20:06:08 +0530492 if (partition_multislot_is_supported())
493 {
494 current_active_slot = partition_find_active_slot();
495 cmdline_len += (strlen(androidboot_slot_suffix)+
496 strlen(SUFFIX_SLOT(current_active_slot)));
497
Mayank Grover2bd84bb2017-06-20 11:06:07 +0530498 snprintf(sys_path_cmdline, sizeof(*sys_path_cmdline),
499 sys_path_cmdline, (partition_get_index("system")+1));
Mayank Grover351a75e2017-05-30 20:06:08 +0530500 cmdline_len += strlen(sys_path_cmdline);
501
502 if (!boot_into_recovery)
503 cmdline_len += strlen(skip_ramfs);
504 }
505
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800506#if TARGET_CMDLINE_SUPPORT
507 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
508 int target_cmd_line_len;
509 ASSERT(target_cmdline_buf);
510 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
511 cmdline_len += target_cmd_line_len;
512#endif
513
David Ng183a7422009-12-07 14:55:21 -0800514 if (cmdline_len > 0) {
515 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800516 unsigned char *dst;
517
518 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
519 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530520 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800521 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700522
Amol Jadi168b7712012-03-06 16:15:00 -0800523 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800524 if (have_cmdline) {
525 src = cmdline;
526 while ((*dst++ = *src++));
527 }
528 if (target_is_emmc_boot()) {
529 src = emmc_cmdline;
530 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700531 have_cmdline = 1;
532 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800533#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700534 src = boot_dev_buf;
535 if (have_cmdline) --dst;
536 while ((*dst++ = *src++));
537#endif
David Ngf773dde2010-07-26 19:55:08 -0700538 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800539
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700540#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530541#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700542 src = verified_state;
543 if(have_cmdline) --dst;
544 have_cmdline = 1;
545 while ((*dst++ = *src++));
546 src = vbsn[boot_state].name;
547 if(have_cmdline) --dst;
548 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700549
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700550 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
551 {
552 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
553 ASSERT(0);
554 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700555 src = verity_mode;
556 if(have_cmdline) --dst;
557 while ((*dst++ = *src++));
558 src = vbvm[device.verity_mode].name;
559 if(have_cmdline) -- dst;
560 while ((*dst++ = *src++));
Parth Dixita5715a02015-10-29 12:25:10 +0530561 src = keymaster_v1;
562 if(have_cmdline) --dst;
563 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700564#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530565#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800566 src = usb_sn_cmdline;
567 if (have_cmdline) --dst;
568 have_cmdline = 1;
569 while ((*dst++ = *src++));
570 src = sn_buf;
571 if (have_cmdline) --dst;
572 have_cmdline = 1;
573 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800574 if (warm_boot) {
575 if (have_cmdline) --dst;
576 src = warmboot_cmdline;
577 while ((*dst++ = *src++));
578 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800579
Pavel Nedev5614d222013-06-17 18:01:02 +0300580 if (boot_into_recovery && gpt_exists) {
581 src = secondary_gpt_enable;
582 if (have_cmdline) --dst;
583 while ((*dst++ = *src++));
584 }
585
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200586 if (is_mdtp_activated) {
587 src = mdtp_activated_flag;
588 if (have_cmdline) --dst;
589 while ((*dst++ = *src++));
590 }
591
Pavel Nedev328ac822013-04-05 15:25:11 +0300592 if (boot_into_ffbm) {
593 src = androidboot_mode;
594 if (have_cmdline) --dst;
595 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700596 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300597 if (have_cmdline) --dst;
598 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530599
600 if(is_systemd_present) {
601 src = systemd_ffbm_mode;
602 if (have_cmdline) --dst;
603 while ((*dst++ = *src++));
604 }
605
Pavel Nedev898298c2013-02-27 12:36:09 -0800606 src = loglevel;
607 if (have_cmdline) --dst;
608 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800609 } else if (boot_reason_alarm) {
610 src = alarmboot_cmdline;
611 if (have_cmdline) --dst;
612 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300613 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700614 src = battchg_pause;
615 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800616 while ((*dst++ = *src++));
617 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800618
Shashank Mittalcd98d472011-08-02 14:29:24 -0700619 if(target_use_signed_kernel() && auth_kernel_img) {
620 src = auth_kernel;
621 if (have_cmdline) --dst;
622 while ((*dst++ = *src++));
623 }
624
Ajay Dudanid04110c2011-01-17 23:55:07 -0800625 switch(target_baseband())
626 {
627 case BASEBAND_APQ:
628 src = baseband_apq;
629 if (have_cmdline) --dst;
630 while ((*dst++ = *src++));
631 break;
632
633 case BASEBAND_MSM:
634 src = baseband_msm;
635 if (have_cmdline) --dst;
636 while ((*dst++ = *src++));
637 break;
638
639 case BASEBAND_CSFB:
640 src = baseband_csfb;
641 if (have_cmdline) --dst;
642 while ((*dst++ = *src++));
643 break;
644
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800645 case BASEBAND_SVLTE2A:
646 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800647 if (have_cmdline) --dst;
648 while ((*dst++ = *src++));
649 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700650
651 case BASEBAND_MDM:
652 src = baseband_mdm;
653 if (have_cmdline) --dst;
654 while ((*dst++ = *src++));
655 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700656
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800657 case BASEBAND_MDM2:
658 src = baseband_mdm2;
659 if (have_cmdline) --dst;
660 while ((*dst++ = *src++));
661 break;
662
Amol Jadi5c61a952012-05-04 17:05:35 -0700663 case BASEBAND_SGLTE:
664 src = baseband_sglte;
665 if (have_cmdline) --dst;
666 while ((*dst++ = *src++));
667 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530668
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800669 case BASEBAND_SGLTE2:
670 src = baseband_sglte2;
671 if (have_cmdline) --dst;
672 while ((*dst++ = *src++));
673 break;
674
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530675 case BASEBAND_DSDA:
676 src = baseband_dsda;
677 if (have_cmdline) --dst;
678 while ((*dst++ = *src++));
679 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800680
681 case BASEBAND_DSDA2:
682 src = baseband_dsda2;
683 if (have_cmdline) --dst;
684 while ((*dst++ = *src++));
685 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530686 case BASEBAND_APQ_NOWGR:
687 src = baseband_apq_nowgr;
688 if (have_cmdline) --dst;
689 while ((*dst++ = *src++));
690 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800691 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700692
693 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700694 src = display_panel_buf;
695 if (have_cmdline) --dst;
696 while ((*dst++ = *src++));
697 }
Joonwoo Park61112782013-10-02 19:50:39 -0700698
699 if (have_target_boot_params) {
700 if (have_cmdline) --dst;
701 src = target_boot_params;
702 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530703 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700704 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800705
Mayank Grover351a75e2017-05-30 20:06:08 +0530706 if (partition_multislot_is_supported() && have_cmdline)
707 {
708 src = androidboot_slot_suffix;
709 --dst;
710 while ((*dst++ = *src++));
711 --dst;
712 src = SUFFIX_SLOT(current_active_slot);
713 while ((*dst++ = *src++));
714
715 if (!boot_into_recovery)
716 {
717 src = skip_ramfs;
718 --dst;
719 while ((*dst++ = *src++));
720 }
721
722 src = sys_path_cmdline;
723 --dst;
724 while ((*dst++ = *src++));
725 }
726
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800727#if TARGET_CMDLINE_SUPPORT
728 if (target_cmdline_buf && target_cmd_line_len)
729 {
730 if (have_cmdline) --dst;
731 src = target_cmdline_buf;
732 while((*dst++ = *src++));
733 free(target_cmdline_buf);
734 }
735#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700736 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700737
738
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700739 if (boot_dev_buf)
740 free(boot_dev_buf);
741
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800742 if (cmdline_final)
743 dprintf(INFO, "cmdline: %s\n", cmdline_final);
744 else
745 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700746 return cmdline_final;
747}
748
749unsigned *atag_core(unsigned *ptr)
750{
751 /* CORE */
752 *ptr++ = 2;
753 *ptr++ = 0x54410001;
754
755 return ptr;
756
757}
758
759unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
760 unsigned ramdisk_size)
761{
762 if (ramdisk_size) {
763 *ptr++ = 4;
764 *ptr++ = 0x54420005;
765 *ptr++ = (unsigned)ramdisk;
766 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800767 }
768
Neeti Desaie245d492012-06-01 12:52:13 -0700769 return ptr;
770}
771
772unsigned *atag_ptable(unsigned **ptr_addr)
773{
774 int i;
775 struct ptable *ptable;
776
777 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700778 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
779 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700780 *(*ptr_addr)++ = 0x4d534d70;
781 for (i = 0; i < ptable->count; ++i)
782 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
783 }
784
785 return (*ptr_addr);
786}
787
788unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
789{
790 int cmdline_length = 0;
791 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700792 char *dest;
793
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800794 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700795 n = (cmdline_length + 4) & (~3);
796
797 *ptr++ = (n / 4) + 2;
798 *ptr++ = 0x54410009;
799 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800800 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700801 ptr += (n / 4);
802
803 return ptr;
804}
805
806unsigned *atag_end(unsigned *ptr)
807{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800808 /* END */
809 *ptr++ = 0;
810 *ptr++ = 0;
811
Neeti Desaie245d492012-06-01 12:52:13 -0700812 return ptr;
813}
814
815void generate_atags(unsigned *ptr, const char *cmdline,
816 void *ramdisk, unsigned ramdisk_size)
817{
vijay kumar21a37452015-12-29 16:23:21 +0530818 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700819 ptr = atag_core(ptr);
820 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
821 ptr = target_atag_mem(ptr);
822
823 /* Skip NAND partition ATAGS for eMMC boot */
824 if (!target_is_emmc_boot()){
825 ptr = atag_ptable(&ptr);
826 }
827
vijay kumar21a37452015-12-29 16:23:21 +0530828 /*
829 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
830 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
831 */
832 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
833 ptr = atag_cmdline(ptr, cmdline);
834 ptr = atag_end(ptr);
835 }
836 else {
837 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
838 ASSERT(0);
839 }
Neeti Desaie245d492012-06-01 12:52:13 -0700840}
841
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700842typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700843void boot_linux(void *kernel, unsigned *tags,
844 const char *cmdline, unsigned machtype,
845 void *ramdisk, unsigned ramdisk_size)
846{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800847 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800848#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700849 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800850#endif
851
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700852 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800853 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530854 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800855
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530856 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700857
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800858 final_cmdline = update_cmdline((const char*)cmdline);
859
Neeti Desai17379b82012-06-04 18:42:53 -0700860#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800861 dprintf(INFO, "Updating device tree: start\n");
862
Neeti Desai17379b82012-06-04 18:42:53 -0700863 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530864 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700865 if(ret)
866 {
867 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
868 ASSERT(0);
869 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800870 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700871#else
Neeti Desaie245d492012-06-01 12:52:13 -0700872 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800873 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700874#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700875
Maria Yu52254c02014-07-04 16:14:54 +0800876 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700877
878#if VERIFIED_BOOT
lijuangbdd9bb42016-03-01 18:22:17 +0800879#if !VBOOT_MOTA
880 if (device.verity_mode == 0) {
881#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700882#if ENABLE_VB_ATTEST
883 display_bootverify_menu(DISPLAY_MENU_EIO);
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530884 wait_for_users_action();
885 if(!pwr_key_is_pressed)
886 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700887#else
lijuangbdd9bb42016-03-01 18:22:17 +0800888 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700889#endif
lijuangbdd9bb42016-03-01 18:22:17 +0800890 wait_for_users_action();
891#else
892 dprintf(CRITICAL,
893 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
894 mdelay(5000);
895#endif
896 }
897
898#endif
899#endif
900
901#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700902 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700903 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700904 {
905 dprintf(INFO, "Failed to write protect dev info\n");
906 ASSERT(0);
907 }
908#endif
909
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800910 /* Turn off splash screen if enabled */
911#if DISPLAY_SPLASH_SCREEN
912 target_display_shutdown();
913#endif
914
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700915 /* Perform target specific cleanup */
916 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800917
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800918 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530919 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800920
921 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700922
Amol Jadi4421e652011-06-16 15:00:48 -0700923 /* do any platform specific cleanup before kernel entry */
924 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700925
Brian Swetland9c4c0752009-01-25 16:23:50 -0800926 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700927
Amol Jadi504f9fe2012-08-16 13:56:48 -0700928#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800929 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700930#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700931 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800932
933 if (IS_ARM64(kptr))
934 /* Jump to a 64bit kernel */
935 scm_elexec_call((paddr_t)kernel, tags_phys);
936 else
937 /* Jump to a 32bit kernel */
938 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800939}
940
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700941/* Function to check if the memory address range falls within the aboot
942 * boundaries.
943 * start: Start of the memory region
944 * size: Size of the memory region
945 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +0530946int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700947{
948 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800949 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700950 return -1;
951
952 /* Check for memory overlap. */
953 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
954 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700955 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700956 return 0;
957 else
958 return -1;
959}
960
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800961#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800962
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800963BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800964#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800965BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800966#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800967
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700968static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
969{
970 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800971
972#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800973#if IMAGE_VERIF_ALGO_SHA1
974 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
975#else
976 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
977#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800978#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700979
980 /* Assume device is rooted at this time. */
981 device.is_tampered = 1;
982
983 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
984
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700985#if VERIFIED_BOOT
986 if(boot_into_recovery)
987 {
988 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530989 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700990 }
991 else
992 {
993 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530994 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700995 }
996 boot_verify_print_state();
997#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700998 ret = image_verify((unsigned char *)bootimg_addr,
999 (unsigned char *)(bootimg_addr + bootimg_size),
1000 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001001 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001002#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001003 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1004
1005 if (ret)
1006 {
1007 /* Authorized kernel */
1008 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001009 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001010 }
1011
Amit Blay4aa292f2015-04-28 21:55:59 +03001012#ifdef MDTP_SUPPORT
1013 {
1014 /* Verify MDTP lock.
1015 * For boot & recovery partitions, use aboot's verification result.
1016 */
1017 mdtp_ext_partition_verification_t ext_partition;
1018 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1019 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1020 ext_partition.page_size = 0; /* Not needed since already validated */
1021 ext_partition.image_addr = 0; /* Not needed since already validated */
1022 ext_partition.image_size = 0; /* Not needed since already validated */
1023 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1024 mdtp_fwlock_verify_lock(&ext_partition);
1025 }
1026#endif /* MDTP_SUPPORT */
1027
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001028#if USE_PCOM_SECBOOT
1029 set_tamper_flag(device.is_tampered);
1030#endif
1031
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001032#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001033 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001034 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001035 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001036#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001037 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001038#if ENABLE_VB_ATTEST
1039 mdelay(DELAY_WAIT);
1040 shutdown_device();
1041#else
lijuanga40d6302015-07-20 20:10:13 +08001042 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001043#endif
lijuanga40d6302015-07-20 20:10:13 +08001044#else
1045 dprintf(CRITICAL,
1046 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1047 mdelay(5000);
1048#endif
1049
1050 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001051 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001052#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001053 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001054 wait_for_users_action();
1055#else
1056 dprintf(CRITICAL,
1057 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1058 mdelay(5000);
1059#endif
1060 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001061 default:
lijuanga40d6302015-07-20 20:10:13 +08001062 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001063 }
1064#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001065#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301066 if(device.is_tampered)
1067 {
1068 write_device_info_mmc(&device);
1069 #ifdef TZ_TAMPER_FUSE
1070 set_tamper_fuse_cmd();
1071 #endif
1072 #ifdef ASSERT_ON_TAMPER
1073 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1074 ASSERT(0);
1075 #endif
1076 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001077#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001078}
1079
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301080static bool check_format_bit()
1081{
1082 bool ret = false;
1083 int index;
1084 uint64_t offset;
1085 struct boot_selection_info *in = NULL;
1086 char *buf = NULL;
1087
1088 index = partition_get_index("bootselect");
1089 if (index == INVALID_PTN)
1090 {
1091 dprintf(INFO, "Unable to locate /bootselect partition\n");
1092 return ret;
1093 }
1094 offset = partition_get_offset(index);
1095 if(!offset)
1096 {
1097 dprintf(INFO, "partition /bootselect doesn't exist\n");
1098 return ret;
1099 }
1100 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301101 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301102 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301103 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301104 {
1105 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1106 free(buf);
1107 return ret;
1108 }
1109 in = (struct boot_selection_info *) buf;
1110 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1111 (in->version == BOOTSELECT_VERSION)) {
1112 if ((in->state_info & BOOTSELECT_FORMAT) &&
1113 !(in->state_info & BOOTSELECT_FACTORY))
1114 ret = true;
1115 } else {
1116 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1117 in->signature, in->version);
1118 ASSERT(0);
1119 }
1120 free(buf);
1121 return ret;
1122}
1123
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001124void boot_verifier_init()
1125{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001126 uint32_t boot_state;
1127 /* Check if device unlock */
1128 if(device.is_unlocked)
1129 {
1130 boot_verify_send_event(DEV_UNLOCK);
1131 boot_verify_print_state();
1132 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1133 return;
1134 }
1135 else
1136 {
1137 boot_verify_send_event(BOOT_INIT);
1138 }
1139
1140 /* Initialize keystore */
1141 boot_state = boot_verify_keystore_init();
1142 if(boot_state == YELLOW)
1143 {
1144 boot_verify_print_state();
1145 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1146 }
1147}
1148
Shashank Mittal23b8f422010-04-16 19:27:21 -07001149int boot_linux_from_mmc(void)
1150{
1151 struct boot_img_hdr *hdr = (void*) buf;
1152 struct boot_img_hdr *uhdr;
1153 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001154 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001155 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001156 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001157
Shashank Mittalcd98d472011-08-02 14:29:24 -07001158 unsigned char *image_addr = 0;
1159 unsigned kernel_actual;
1160 unsigned ramdisk_actual;
1161 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001162 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001163
Matthew Qin271927e2015-03-31 22:07:07 -04001164 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001165 unsigned int out_len = 0;
1166 unsigned int out_avai_len = 0;
1167 unsigned char *out_addr = NULL;
1168 uint32_t dtb_offset = 0;
1169 unsigned char *kernel_start_addr = NULL;
1170 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001171 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001172 int rc;
Neeti Desai465491e2012-07-31 12:53:35 -07001173#if DEVICE_TREE
1174 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001175 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001176 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001177 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001178 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001179 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001180#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001181 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301182 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001183
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301184 if (check_format_bit())
1185 boot_into_recovery = 1;
1186
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001187 if (!boot_into_recovery) {
1188 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1189 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1190 if (rcode <= 0) {
1191 boot_into_ffbm = false;
1192 if (rcode < 0)
1193 dprintf(CRITICAL,"failed to get ffbm cookie");
1194 } else
1195 boot_into_ffbm = true;
1196 } else
1197 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001198 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1199 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1200 dprintf(INFO, "Unified boot method!\n");
1201 hdr = uhdr;
1202 goto unified_boot;
1203 }
Greg Griscod6250552011-06-29 14:40:23 -07001204 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001205 index = partition_get_index("boot");
1206 ptn = partition_get_offset(index);
1207 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001208 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1209 return -1;
1210 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001211 }
1212 else {
1213 index = partition_get_index("recovery");
1214 ptn = partition_get_offset(index);
1215 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001216 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1217 return -1;
1218 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001219 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001220 /* Set Lun for boot & recovery partitions */
1221 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001222
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301223 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001224 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1225 return -1;
1226 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001227
1228 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001229 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301230 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001231 }
1232
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001233 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301234
1235 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1236 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1237 return -1;
1238 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001239 page_size = hdr->page_size;
1240 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001241 }
1242
Matthew Qin49e51fa2015-02-09 10:40:45 +08001243 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1244 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001245
Matthew Qin49e51fa2015-02-09 10:40:45 +08001246 image_addr = (unsigned char *)target_get_scratch_address();
Kishor PK9e91b592017-05-24 12:14:55 +05301247 memcpy(image_addr, (void *)buf, page_size);
1248
1249 /* ensure commandline is terminated */
1250 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001251
1252#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301253#ifndef OSVERSION_IN_BOOTIMAGE
1254 dt_size = hdr->dt_size;
1255#endif
1256 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301257 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1258 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1259 return -1;
1260 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001261 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1262#else
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301263 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual + page_size)) {
1264 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1265 return -1;
1266 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001267 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1268#endif
1269
1270#if VERIFIED_BOOT
1271 boot_verifier_init();
1272#endif
1273
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301274 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001275 {
1276 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1277 return -1;
1278 }
1279
Matthew Qinbb7923d2015-02-09 10:56:09 +08001280 /*
1281 * Update loading flow of bootimage to support compressed/uncompressed
1282 * bootimage on both 64bit and 32bit platform.
1283 * 1. Load bootimage from emmc partition onto DDR.
1284 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1285 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1286 * platform accordingly.
1287 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1288 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301289 if (partition_multislot_is_supported())
1290 {
1291 current_active_slot = partition_find_active_slot();
1292 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1293 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1294 }
1295 else
1296 {
1297 dprintf(INFO, "Loading (%s) image (%d): start\n",
1298 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1299 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001300 bs_set_timestamp(BS_KERNEL_LOAD_START);
1301
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301302 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1303 {
1304 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1305 return -1;
1306 }
Kishor PK9e91b592017-05-24 12:14:55 +05301307 offset = page_size;
1308 /* Read image without signature and header*/
1309 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001310 {
1311 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1312 return -1;
1313 }
1314
Mayank Grover351a75e2017-05-30 20:06:08 +05301315 if (partition_multislot_is_supported())
1316 {
1317 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1318 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1319 }
1320 else
1321 {
1322 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001323 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1324
Mayank Grover351a75e2017-05-30 20:06:08 +05301325 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001326 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1327
1328 /* Authenticate Kernel */
1329 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1330 (int) target_use_signed_kernel(),
1331 device.is_unlocked,
1332 device.is_tampered);
1333
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001334 /* Change the condition a little bit to include the test framework support.
1335 * We would never reach this point if device is in fastboot mode, even if we did
1336 * that means we are in test mode, so execute kernel authentication part for the
1337 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301338 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001339 {
1340 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301341 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001342 {
1343 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1344 return -1;
1345 }
1346
1347 /* Read signature */
1348 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1349 {
1350 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1351 return -1;
1352 }
1353
1354 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001355 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301356 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001357 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001358 } else {
1359 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1360 #ifdef TZ_SAVE_KERNEL_HASH
1361 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1362 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001363
1364#ifdef MDTP_SUPPORT
1365 {
1366 /* Verify MDTP lock.
1367 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1368 * since verification was skipped in aboot. The signature is not part of the loaded image.
1369 */
1370 mdtp_ext_partition_verification_t ext_partition;
1371 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1372 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1373 ext_partition.page_size = page_size;
1374 ext_partition.image_addr = (uint32)image_addr;
1375 ext_partition.image_size = imagesize_actual;
1376 ext_partition.sig_avail = FALSE;
1377 mdtp_fwlock_verify_lock(&ext_partition);
1378 }
1379#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001380 }
1381
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001382#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001383 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001384 {
1385#if FBCON_DISPLAY_MSG
1386 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1387 wait_for_users_action();
1388#else
1389 dprintf(CRITICAL,
1390 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1391 mdelay(5000);
1392#endif
1393 }
1394#endif
1395
1396#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05301397#if !VBOOT_MOTA
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001398 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001399 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001400 ASSERT(0);
1401#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05301402#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001403 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001404 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1405 * If not, continue booting.
1406 */
1407 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1408 {
1409 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1410 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001411 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001412 rc = decompress((unsigned char *)(image_addr + page_size),
1413 hdr->kernel_size, out_addr, out_avai_len,
1414 &dtb_offset, &out_len);
1415 if (rc)
1416 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001417 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001418 ASSERT(0);
1419 }
1420
Matthew Qin0b15b322015-05-19 05:20:54 -04001421 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001422 kptr = (struct kernel64_hdr *)out_addr;
1423 kernel_start_addr = out_addr;
1424 kernel_size = out_len;
1425 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001426 dprintf(INFO, "Uncpmpressed kernel in use\n");
1427 if (!strncmp((char*)(image_addr + page_size),
1428 PATCHED_KERNEL_MAGIC,
1429 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1430 dprintf(INFO, "Patched kernel detected\n");
1431 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1432 PATCHED_KERNEL_HEADER_SIZE);
1433 //The size of the kernel is stored at start of kernel image + 16
1434 //The dtb would start just after the kernel
1435 dtb_offset = *((uint32_t*)((unsigned char*)
1436 (image_addr + page_size +
1437 sizeof(PATCHED_KERNEL_MAGIC) -
1438 1)));
1439 //The actual kernel starts after the 20 byte header.
1440 kernel_start_addr = (unsigned char*)(image_addr +
1441 page_size + PATCHED_KERNEL_HEADER_SIZE);
1442 kernel_size = hdr->kernel_size;
1443 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1444 } else {
1445 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1446 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1447 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1448 kernel_size = hdr->kernel_size;
1449 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001450 }
1451
1452 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001453 * Update the kernel/ramdisk/tags address if the boot image header
1454 * has default values, these default values come from mkbootimg when
1455 * the boot image is flashed using fastboot flash:raw
1456 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001457 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001458
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001459 /* Get virtual addresses since the hdr saves physical addresses. */
1460 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1461 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1462 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001463
Matthew Qinbb7923d2015-02-09 10:56:09 +08001464 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001465 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001466 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001467 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1468 {
1469 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1470 return -1;
1471 }
1472
1473#ifndef DEVICE_TREE
1474 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1475 {
1476 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1477 return -1;
1478 }
1479#endif
1480
Matthew Qin49e51fa2015-02-09 10:40:45 +08001481 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001482 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001483 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001484
Matthew Qin49e51fa2015-02-09 10:40:45 +08001485 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301486 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001487 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1488 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001489
Matthew Qin49e51fa2015-02-09 10:40:45 +08001490 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1491 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1492 return -1;
1493 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001494
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301495 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1496 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301497 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301498 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1499 return -1;
1500 }
1501
Matthew Qin49e51fa2015-02-09 10:40:45 +08001502 /* Find index of device tree within device tree table */
1503 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1504 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1505 return -1;
1506 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001507
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301508 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1509 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1510 return -1;
1511 }
1512
1513 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301514 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301515 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1516 return -1;
1517 }
1518
Matthew Qin271927e2015-03-31 22:07:07 -04001519 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1520 {
1521 unsigned int compressed_size = 0;
1522 out_addr += out_len;
1523 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001524 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001525 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1526 dt_entry.size, out_addr, out_avai_len,
1527 &compressed_size, &dtb_size);
1528 if (rc)
1529 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001530 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001531 ASSERT(0);
1532 }
1533
Matthew Qin0b15b322015-05-19 05:20:54 -04001534 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001535 best_match_dt_addr = out_addr;
1536 } else {
1537 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1538 dtb_size = dt_entry.size;
1539 }
1540
Matthew Qin49e51fa2015-02-09 10:40:45 +08001541 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001542 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001543 {
1544 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1545 return -1;
1546 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001547
Matthew Qin271927e2015-03-31 22:07:07 -04001548 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001549 } else {
1550 /* Validate the tags_addr */
1551 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001552 {
1553 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1554 return -1;
1555 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001556 /*
1557 * If appended dev tree is found, update the atags with
1558 * memory address to the DTB appended location on RAM.
1559 * Else update with the atags address in the kernel header
1560 */
1561 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001562 dtb = dev_tree_appended(
1563 (void*)(image_addr + page_size +
1564 patched_kernel_hdr_size),
1565 hdr->kernel_size, dtb_offset,
1566 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001567 if (!dtb) {
1568 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001569 return -1;
1570 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001571 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001572 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001573
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001574 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1575 target_load_ssd_keystore();
1576
Shashank Mittal23b8f422010-04-16 19:27:21 -07001577unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001578
Dima Zavin77e41f32013-03-06 16:10:43 -08001579 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001580 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001581 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1582
1583 return 0;
1584}
1585
Dima Zavin214cc642009-01-26 11:16:21 -08001586int boot_linux_from_flash(void)
1587{
1588 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001589 struct ptentry *ptn;
1590 struct ptable *ptable;
1591 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001592
Shashank Mittalcd98d472011-08-02 14:29:24 -07001593 unsigned char *image_addr = 0;
1594 unsigned kernel_actual;
1595 unsigned ramdisk_actual;
1596 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301597 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001598
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001599#if DEVICE_TREE
1600 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001601 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301602 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001603 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001604 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301605 unsigned int dtb_size = 0;
1606 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001607#endif
1608
David Ng183a7422009-12-07 14:55:21 -08001609 if (target_is_emmc_boot()) {
1610 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1611 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1612 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1613 return -1;
1614 }
1615 goto continue_boot;
1616 }
1617
Dima Zavin214cc642009-01-26 11:16:21 -08001618 ptable = flash_get_ptable();
1619 if (ptable == NULL) {
1620 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1621 return -1;
1622 }
1623
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001624 if(!boot_into_recovery)
1625 {
1626 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001627
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001628 if (ptn == NULL) {
1629 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1630 return -1;
1631 }
1632 }
1633 else
1634 {
1635 ptn = ptable_find(ptable, "recovery");
1636 if (ptn == NULL) {
1637 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1638 return -1;
1639 }
Dima Zavin214cc642009-01-26 11:16:21 -08001640 }
1641
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001642 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001643 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1644 return -1;
1645 }
Dima Zavin214cc642009-01-26 11:16:21 -08001646
1647 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001648 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001649 return -1;
1650 }
1651
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001652 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001653 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 -08001654 return -1;
1655 }
1656
Kishor PK9e91b592017-05-24 12:14:55 +05301657 image_addr = (unsigned char *)target_get_scratch_address();
1658 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05301659
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001660 /*
1661 * Update the kernel/ramdisk/tags address if the boot image header
1662 * has default values, these default values come from mkbootimg when
1663 * the boot image is flashed using fastboot flash:raw
1664 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001665 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001666
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001667 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001668 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1669 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1670 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1671
1672 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1673 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1674
Kishor PK9e91b592017-05-24 12:14:55 +05301675 /* ensure commandline is terminated */
1676 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1677
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001678 /* Check if the addresses in the header are valid. */
1679 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1680 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1681 {
1682 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1683 return -1;
1684 }
1685
1686#ifndef DEVICE_TREE
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301687 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1688 {
1689 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1690 return -1;
1691 }
1692#else
1693
1694#ifndef OSVERSION_IN_BOOTIMAGE
1695 dt_size = hdr->dt_size;
1696#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001697#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001698
Shashank Mittalcd98d472011-08-02 14:29:24 -07001699 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001700 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001701 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001702
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001703#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301704 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
vijay kumar966a9822015-12-09 18:36:09 +05301705 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1706 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1707 return -1;
1708 }
1709
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001710 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001711
Parth Dixit4097b622016-03-15 14:42:27 +05301712 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001713 {
1714 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1715 return -1;
1716 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001717#else
vijay kumar966a9822015-12-09 18:36:09 +05301718 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) {
1719 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1720 return -1;
1721 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001722 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001723#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001724
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001725 dprintf(INFO, "Loading (%s) image (%d): start\n",
1726 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001727 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001728
Vijay Kumar Pendotib3458362016-07-05 13:45:54 +05301729 if (UINT_MAX - page_size < imagesize_actual)
1730 {
1731 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
1732 return -1;
1733 }
1734
1735 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1736 if (target_get_max_flash_size() < (imagesize_actual + page_size))
1737 {
1738 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
1739 return -1;
1740 }
Kishor PK9e91b592017-05-24 12:14:55 +05301741 offset = page_size;
1742 /* Read image without signature and header*/
1743 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001744 {
1745 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1746 return -1;
1747 }
Dima Zavin214cc642009-01-26 11:16:21 -08001748
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001749 dprintf(INFO, "Loading (%s) image (%d): done\n",
1750 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001751 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001752
Shashank Mittalcd98d472011-08-02 14:29:24 -07001753 offset = imagesize_actual;
1754 /* Read signature */
1755 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1756 {
1757 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001758 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001759 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001760
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301761 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001762
1763 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001764 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1765 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001766#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301767 if(dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001768
vijay kumar8f53e362015-11-24 13:38:11 +05301769 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1770
1771 table = (struct dt_table*) dt_table_offset;
1772
1773 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1774 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1775 return -1;
1776 }
1777
1778 /* Find index of device tree within device tree table */
1779 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1780 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1781 return -1;
1782 }
1783
1784 /* Validate and Read device device tree in the "tags_add */
1785 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){
1786 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1787 return -1;
1788 }
1789
Mayank Groverebf85ae2017-01-25 13:35:37 +05301790 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1791 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1792 return -1;
1793 }
1794
1795 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1796 if ((dt_entry.offset + dt_entry.size) > dt_size) {
1797 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1798 return -1;
1799 }
1800
vijay kumar8f53e362015-11-24 13:38:11 +05301801 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1802 dtb_size = dt_entry.size;
1803 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1804 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001805#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001806
1807 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001808 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001809 {
1810 write_device_info_flash(&device);
1811 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301812#if USE_PCOM_SECBOOT
1813 set_tamper_flag(device.is_tampered);
1814#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001815 }
1816 else
1817 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001818 offset = page_size;
1819
Amol Jadib6be5c12012-11-14 13:39:51 -08001820 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1821 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1822 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1823
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001824 dprintf(INFO, "Loading (%s) image (%d): start\n",
1825 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1826
Amol Jadi492d5a52013-03-15 16:12:34 -07001827 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001828
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301829 if (UINT_MAX - offset < kernel_actual)
1830 {
1831 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1832 return -1;
1833 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001834 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001835 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1836 return -1;
1837 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001838 offset += kernel_actual;
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301839 if (UINT_MAX - offset < ramdisk_actual)
1840 {
1841 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1842 return -1;
1843 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001844 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001845 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1846 return -1;
1847 }
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301848
Amol Jadib6be5c12012-11-14 13:39:51 -08001849 offset += ramdisk_actual;
1850
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001851 dprintf(INFO, "Loading (%s) image (%d): done\n",
1852 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1853
Amol Jadi492d5a52013-03-15 16:12:34 -07001854 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001855
1856 if(hdr->second_size != 0) {
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301857 if (UINT_MAX - offset < second_actual)
1858 {
1859 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1860 return -1;
1861 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001862 offset += second_actual;
1863 /* Second image loading not implemented. */
1864 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001865 }
1866
1867#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301868 if(dt_size != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001869
1870 /* Read the device tree table into buffer */
1871 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1872 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1873 return -1;
1874 }
1875
1876 table = (struct dt_table*) dt_buf;
1877
Deepa Dinamani19648b42013-09-05 17:05:55 -07001878 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001879 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1880 return -1;
1881 }
1882
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301883 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1884 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301885 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301886 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1887 return -1;
1888 }
1889
Deepa Dinamani19648b42013-09-05 17:05:55 -07001890 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1891 if (!table)
1892 return -1;
1893
1894 /* Read the entire device tree table into buffer */
1895 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1896 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1897 return -1;
1898 }
1899
1900
Joel Kingaa335dc2013-06-03 16:11:08 -07001901 /* Find index of device tree within device tree table */
1902 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001903 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1904 return -1;
1905 }
1906
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001907 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001908 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001909 {
1910 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1911 return -1;
1912 }
1913
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001914 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001915 if(flash_read(ptn, offset + dt_entry.offset,
1916 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001917 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1918 return -1;
1919 }
1920 }
1921#endif
1922
Shashank Mittalcd98d472011-08-02 14:29:24 -07001923 }
David Ng183a7422009-12-07 14:55:21 -08001924continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001925
Dima Zavin214cc642009-01-26 11:16:21 -08001926 /* TODO: create/pass atags to kernel */
1927
Ajay Dudanie28a6072011-07-01 13:59:46 -07001928 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001929 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001930 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1931
1932 return 0;
1933}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001934
Shashank Mittal162244e2011-08-08 19:01:25 -07001935void write_device_info_mmc(device_info *dev)
1936{
Shashank Mittal162244e2011-08-08 19:01:25 -07001937 unsigned long long ptn = 0;
1938 unsigned long long size;
1939 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001940 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001941 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001942 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001943
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001944 if (devinfo_present)
1945 index = partition_get_index("devinfo");
1946 else
1947 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001948
Shashank Mittal162244e2011-08-08 19:01:25 -07001949 ptn = partition_get_offset(index);
1950 if(ptn == 0)
1951 {
1952 return;
1953 }
1954
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001955 lun = partition_get_lun(index);
1956 mmc_set_lun(lun);
1957
Shashank Mittal162244e2011-08-08 19:01:25 -07001958 size = partition_get_size(index);
1959
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001960 blocksize = mmc_get_device_blocksize();
1961
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001962 if (devinfo_present)
1963 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1964 else
1965 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1966 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001967 {
1968 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001969 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001970 }
1971}
1972
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001973void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001974{
Shashank Mittal162244e2011-08-08 19:01:25 -07001975 unsigned long long ptn = 0;
1976 unsigned long long size;
1977 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001978 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001979 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001980
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001981 if ((index = partition_get_index("devinfo")) < 0)
1982 {
1983 devinfo_present = false;
1984 index = partition_get_index("aboot");
1985 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001986
Shashank Mittal162244e2011-08-08 19:01:25 -07001987 ptn = partition_get_offset(index);
1988 if(ptn == 0)
1989 {
1990 return;
1991 }
1992
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001993 mmc_set_lun(partition_get_lun(index));
1994
Shashank Mittal162244e2011-08-08 19:01:25 -07001995 size = partition_get_size(index);
1996
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001997 blocksize = mmc_get_device_blocksize();
1998
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001999 if (devinfo_present)
2000 ret = mmc_read(ptn, (void *)info_buf, blocksize);
2001 else
2002 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
2003 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002004 {
2005 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002006 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002007 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002008}
2009
2010void write_device_info_flash(device_info *dev)
2011{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002012 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002013 struct ptentry *ptn;
2014 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002015 if(info == NULL)
2016 {
2017 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2018 ASSERT(0);
2019 }
2020 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002021 ptable = flash_get_ptable();
2022 if (ptable == NULL)
2023 {
2024 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2025 return;
2026 }
2027
2028 ptn = ptable_find(ptable, "devinfo");
2029 if (ptn == NULL)
2030 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002031 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002032 return;
2033 }
2034
2035 memcpy(info, dev, sizeof(device_info));
2036
2037 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2038 {
2039 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2040 return;
2041 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002042 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002043}
2044
vijay kumarc65876c2015-04-24 13:29:16 +05302045static int read_allow_oem_unlock(device_info *dev)
2046{
vijay kumarc65876c2015-04-24 13:29:16 +05302047 unsigned offset;
2048 int index;
2049 unsigned long long ptn;
2050 unsigned long long ptn_size;
2051 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002052 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302053
vijay kumarca2e6812015-07-08 20:28:25 +05302054 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302055 if (index == INVALID_PTN)
2056 {
vijay kumarca2e6812015-07-08 20:28:25 +05302057 index = partition_get_index(frp_ptns[1]);
2058 if (index == INVALID_PTN)
2059 {
2060 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2061 return -1;
2062 }
vijay kumarc65876c2015-04-24 13:29:16 +05302063 }
2064
2065 ptn = partition_get_offset(index);
2066 ptn_size = partition_get_size(index);
2067 offset = ptn_size - blocksize;
2068
Mayank Grover48860402016-11-29 12:34:53 +05302069 /* Set Lun for partition */
2070 mmc_set_lun(partition_get_lun(index));
2071
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002072 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302073 {
2074 dprintf(CRITICAL, "Reading MMC failed\n");
2075 return -1;
2076 }
2077
2078 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2079 is_allow_unlock = buf[blocksize-1] & 0x01;
2080 return 0;
2081}
2082
2083static int write_allow_oem_unlock(bool allow_unlock)
2084{
vijay kumarc65876c2015-04-24 13:29:16 +05302085 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302086 int index;
2087 unsigned long long ptn;
2088 unsigned long long ptn_size;
2089 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002090 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302091
vijay kumarca2e6812015-07-08 20:28:25 +05302092 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302093 if (index == INVALID_PTN)
2094 {
vijay kumarca2e6812015-07-08 20:28:25 +05302095 index = partition_get_index(frp_ptns[1]);
2096 if (index == INVALID_PTN)
2097 {
2098 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2099 return -1;
2100 }
vijay kumarc65876c2015-04-24 13:29:16 +05302101 }
2102
2103 ptn = partition_get_offset(index);
2104 ptn_size = partition_get_size(index);
2105 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302106 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302107
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002108 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302109 {
2110 dprintf(CRITICAL, "Reading MMC failed\n");
2111 return -1;
2112 }
2113
2114 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2115 buf[blocksize-1] = allow_unlock;
2116 if (mmc_write(ptn + offset, blocksize, buf))
2117 {
2118 dprintf(CRITICAL, "Writing MMC failed\n");
2119 return -1;
2120 }
2121
2122 return 0;
2123}
2124
Shashank Mittal162244e2011-08-08 19:01:25 -07002125void read_device_info_flash(device_info *dev)
2126{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002127 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002128 struct ptentry *ptn;
2129 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002130 if(info == NULL)
2131 {
2132 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2133 ASSERT(0);
2134 }
2135 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002136 ptable = flash_get_ptable();
2137 if (ptable == NULL)
2138 {
2139 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2140 return;
2141 }
2142
2143 ptn = ptable_find(ptable, "devinfo");
2144 if (ptn == NULL)
2145 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002146 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002147 return;
2148 }
2149
2150 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2151 {
2152 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2153 return;
2154 }
2155
2156 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2157 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002158 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2159 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002160 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002161 write_device_info_flash(info);
2162 }
2163 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002164 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002165}
2166
2167void write_device_info(device_info *dev)
2168{
2169 if(target_is_emmc_boot())
2170 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002171 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2172 if(info == NULL)
2173 {
2174 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2175 ASSERT(0);
2176 }
2177 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002178 memcpy(info, dev, sizeof(struct device_info));
2179
2180#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002181 if (is_secure_boot_enable()) {
2182 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2183 ASSERT(0);
2184 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002185 else
2186 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002187#else
2188 write_device_info_mmc(info);
2189#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002190 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002191 }
2192 else
2193 {
2194 write_device_info_flash(dev);
2195 }
2196}
2197
2198void read_device_info(device_info *dev)
2199{
2200 if(target_is_emmc_boot())
2201 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002202 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2203 if(info == NULL)
2204 {
2205 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2206 ASSERT(0);
2207 }
2208 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002209
2210#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002211 if (is_secure_boot_enable()) {
2212 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2213 ASSERT(0);
2214 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002215 else
2216 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002217#else
2218 read_device_info_mmc(info);
2219#endif
2220
2221 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2222 {
2223 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002224 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002225 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302226#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002227 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302228#endif
lijuang511a2b52015-08-14 20:50:51 +08002229 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002230 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302231#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002232 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302233#endif
lijuang511a2b52015-08-14 20:50:51 +08002234 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002235 info->is_tampered = 0;
2236 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302237#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07002238 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302239#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002240 write_device_info(info);
2241 }
2242 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002243 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002244 }
2245 else
2246 {
2247 read_device_info_flash(dev);
2248 }
2249}
2250
2251void reset_device_info()
2252{
2253 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002254 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002255 write_device_info(&device);
2256}
2257
2258void set_device_root()
2259{
2260 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002261 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002262 write_device_info(&device);
2263}
2264
lijuang4ece1e72015-08-14 21:02:36 +08002265/* set device unlock value
2266 * Must check FRP before call this function
2267 * Need to wipe data when unlock status changed
2268 * type 0: oem unlock
2269 * type 1: unlock critical
2270 * status 0: unlock as false
2271 * status 1: lock as true
2272 */
2273void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002274{
lijuang4ece1e72015-08-14 21:02:36 +08002275 if (type == UNLOCK)
2276 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302277#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002278 else if (type == UNLOCK_CRITICAL)
2279 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302280#endif
lijuang4ece1e72015-08-14 21:02:36 +08002281 write_device_info(&device);
2282}
2283
2284static void set_device_unlock(int type, bool status)
2285{
2286 int is_unlocked = -1;
2287 char response[MAX_RSP_SIZE];
2288
2289 /* check device unlock status if it is as expected */
2290 if (type == UNLOCK)
2291 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05302292#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002293 else if (type == UNLOCK_CRITICAL)
2294 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05302295#endif
lijuang4ece1e72015-08-14 21:02:36 +08002296 if (is_unlocked == status) {
2297 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2298 fastboot_info(response);
2299 fastboot_okay("");
2300 return;
2301 }
2302
2303 /* status is true, it means to unlock device */
2304 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002305 if(!is_allow_unlock) {
2306 fastboot_fail("oem unlock is not allowed");
2307 return;
2308 }
2309
lijuang4ece1e72015-08-14 21:02:36 +08002310#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002311 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002312 fastboot_okay("");
2313 return;
2314#else
2315 if (type == UNLOCK) {
2316 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2317 return;
2318 }
2319#endif
lijuang21f12f52015-08-22 16:22:19 +08002320 }
lijuang4ece1e72015-08-14 21:02:36 +08002321
2322 set_device_unlock_value(type, status);
2323
2324 /* wipe data */
2325 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302326 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002327 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2328 write_misc(0, &msg, sizeof(msg));
2329
2330 fastboot_okay("");
2331 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002332}
2333
lijuang511a2b52015-08-14 20:50:51 +08002334static bool critical_flash_allowed(const char * entry)
2335{
2336 uint32_t i = 0;
2337 if (entry == NULL)
2338 return false;
2339
2340 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2341 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2342 return true;
2343 }
2344 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002345}
2346
2347#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002348int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2349{
2350 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002351 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002352 struct dt_table *table;
2353 struct dt_entry dt_entry;
2354 uint32_t dt_hdr_size;
2355 unsigned int compressed_size = 0;
2356 unsigned int dtb_size = 0;
2357 unsigned int out_avai_len = 0;
2358 unsigned char *out_addr = NULL;
2359 unsigned char *best_match_dt_addr = NULL;
2360 int rc;
2361
2362 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2363
Parth Dixit4097b622016-03-15 14:42:27 +05302364#ifndef OSVERSION_IN_BOOTIMAGE
2365 dt_size = hdr->dt_size;
2366#endif
2367
2368 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002369 /* add kernel offset */
2370 dt_image_offset += page_size;
2371 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2372 dt_image_offset += n;
2373
2374 /* add ramdisk offset */
2375 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2376 dt_image_offset += n;
2377
2378 /* add second offset */
2379 if(hdr->second_size != 0) {
2380 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2381 dt_image_offset += n;
2382 }
2383
2384 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002385 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002386
Deepa Dinamani19648b42013-09-05 17:05:55 -07002387 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002388 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2389 return -1;
2390 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302391
2392 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2393 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302394 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302395 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2396 return -1;
2397 }
2398
Joel Kingaa335dc2013-06-03 16:11:08 -07002399 /* Find index of device tree within device tree table */
2400 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002401 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2402 return -1;
2403 }
2404
Matthew Qin271927e2015-03-31 22:07:07 -04002405 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2406 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2407 {
2408 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2409 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002410 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002411 rc = decompress(best_match_dt_addr,
2412 dt_entry.size, out_addr, out_avai_len,
2413 &compressed_size, &dtb_size);
2414 if (rc)
2415 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002416 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002417 ASSERT(0);
2418 }
2419
Matthew Qin0b15b322015-05-19 05:20:54 -04002420 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002421 best_match_dt_addr = out_addr;
2422 } else {
2423 dtb_size = dt_entry.size;
2424 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002425 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002426 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002427 {
2428 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
2429 return -1;
2430 }
2431
Amol Jadicb524072012-08-09 16:40:18 -07002432 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002433 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002434 } else
2435 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002436
2437 /* Everything looks fine. Return success. */
2438 return 0;
2439}
2440#endif
2441
Brian Swetland9c4c0752009-01-25 16:23:50 -08002442void cmd_boot(const char *arg, void *data, unsigned sz)
2443{
Amit Blay8a510302015-08-17 09:20:01 +03002444#ifdef MDTP_SUPPORT
2445 static bool is_mdtp_activated = 0;
2446#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002447 unsigned kernel_actual;
2448 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002449 uint32_t image_actual;
2450 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302451 uint32_t sig_actual = 0;
Kishor PK5134b332017-05-09 17:50:08 +05302452 uint32_t sig_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002453 struct boot_img_hdr *hdr = NULL;
2454 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002455 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002456 int ret = 0;
2457 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002458 unsigned int out_len = 0;
2459 unsigned int out_avai_len = 0;
2460 unsigned char *out_addr = NULL;
2461 uint32_t dtb_offset = 0;
2462 unsigned char *kernel_start_addr = NULL;
2463 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002464 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002465
lijuang2008ff22016-03-07 17:56:27 +08002466#if FBCON_DISPLAY_MSG
2467 /* Exit keys' detection thread firstly */
2468 exit_menu_keys_detection();
2469#endif
2470
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002471#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002472 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002473 {
2474 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002475 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002476 }
2477#endif
2478
Brian Swetland9c4c0752009-01-25 16:23:50 -08002479 if (sz < sizeof(hdr)) {
2480 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002481 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002482 }
2483
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002484 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002485
2486 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002487 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002488
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002489 if(target_is_emmc_boot() && hdr->page_size) {
2490 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002491 page_mask = page_size - 1;
2492 }
2493
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002494 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2495 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002496#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302497#ifndef OSVERSION_IN_BOOTIMAGE
2498 dt_size = hdr->dt_size;
2499#endif
2500 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002501#endif
2502
2503 image_actual = ADD_OF(page_size, kernel_actual);
2504 image_actual = ADD_OF(image_actual, ramdisk_actual);
2505 image_actual = ADD_OF(image_actual, dt_actual);
2506
Kishor PK5134b332017-05-09 17:50:08 +05302507 /* Checking to prevent oob access in read_der_message_length */
2508 if (image_actual > sz) {
2509 fastboot_fail("bootimage header fields are invalid");
2510 goto boot_failed;
2511 }
2512 sig_size = sz - image_actual;
2513
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302514 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2515 /* Calculate the signature length from boot image */
2516 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05302517 (unsigned char*)(data + image_actual), sig_size);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002518 image_actual = ADD_OF(image_actual, sig_actual);
2519
Kishor PK5134b332017-05-09 17:50:08 +05302520 if (image_actual > sz) {
2521 fastboot_fail("bootimage header fields are invalid");
2522 goto boot_failed;
2523 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002524 }
2525
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002526 // Initialize boot state before trying to verify boot.img
2527#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002528 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302529#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002530 /* Handle overflow if the input image size is greater than
2531 * boot image buffer can hold
2532 */
2533 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2534 {
2535 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002536 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002537 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002538
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002539 /* Verify the boot image
2540 * device & page_size are initialized in aboot_init
2541 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002542 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002543 /* Pass size excluding signature size, otherwise we would try to
2544 * access signature beyond its length
2545 */
2546 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002547 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002548#ifdef MDTP_SUPPORT
2549 else
2550 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002551 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002552 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002553
2554 if (!is_mdtp_activated) {
2555 ext_partition.partition = MDTP_PARTITION_NONE;
2556 mdtp_fwlock_verify_lock(&ext_partition);
2557 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002558 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002559
Amir Kotzer7c768c02016-04-13 09:08:05 +03002560 /* If mdtp state cannot be validate, block fastboot boot*/
2561 if(mdtp_activated(&is_mdtp_activated)){
2562 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2563 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2564 goto boot_failed;
2565 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002566 if(is_mdtp_activated){
2567 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002568 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002569 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002570#endif /* MDTP_SUPPORT */
2571
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002572#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302573#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002574 // send root of trust
2575 if(!send_rot_command((uint32_t)device.is_unlocked))
2576 ASSERT(0);
2577#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302578#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002579 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002580 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2581 * If not, continue booting.
2582 */
2583 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2584 {
2585 out_addr = (unsigned char *)target_get_scratch_address();
2586 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2587 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002588 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002589 ret = decompress((unsigned char *)(ptr + page_size),
2590 hdr->kernel_size, out_addr, out_avai_len,
2591 &dtb_offset, &out_len);
2592 if (ret)
2593 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002594 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002595 ASSERT(0);
2596 }
2597
Matthew Qin0b15b322015-05-19 05:20:54 -04002598 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002599 kptr = (struct kernel64_hdr *)out_addr;
2600 kernel_start_addr = out_addr;
2601 kernel_size = out_len;
2602 } else {
2603 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2604 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2605 kernel_size = hdr->kernel_size;
2606 }
2607
2608 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002609 * Update the kernel/ramdisk/tags address if the boot image header
2610 * has default values, these default values come from mkbootimg when
2611 * the boot image is flashed using fastboot flash:raw
2612 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002613 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002614
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002615 /* Get virtual addresses since the hdr saves physical addresses. */
2616 hdr->kernel_addr = VA(hdr->kernel_addr);
2617 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2618 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002619
Matthew Qinbb7923d2015-02-09 10:56:09 +08002620 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002621 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002622 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002623 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2624 {
2625 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002626 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002627 }
2628
Amol Jadicb524072012-08-09 16:40:18 -07002629#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002630 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002631 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002632 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002633
2634 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002635#else
2636 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2637 {
2638 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002639 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002640 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002641#endif
2642
2643 /* Load ramdisk & kernel */
2644 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002645 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002646
2647#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002648 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2649 {
2650 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002651 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002652 }
2653
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002654 /*
2655 * If dtb is not found look for appended DTB in the kernel.
2656 * If appended dev tree is found, update the atags with
2657 * memory address to the DTB appended location on RAM.
2658 * Else update with the atags address in the kernel header
2659 */
2660 if (!dtb_copied) {
2661 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002662 dtb = dev_tree_appended((void*)(ptr + page_size),
2663 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002664 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002665 if (!dtb) {
2666 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002667 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002668 }
Amol Jadicb524072012-08-09 16:40:18 -07002669 }
2670#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002671
2672 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002673 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002674
Dima Zavin77e41f32013-03-06 16:10:43 -08002675 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002676 (const char*) hdr->cmdline, board_machtype(),
2677 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002678
2679 /* fastboot already stop, it's no need to show fastboot menu */
2680 return;
2681boot_failed:
2682#if FBCON_DISPLAY_MSG
2683 /* revert to fastboot menu if boot failed */
2684 display_fastboot_menu();
2685#endif
2686 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002687}
2688
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002689void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002690{
2691 struct ptentry *ptn;
2692 struct ptable *ptable;
2693
2694 ptable = flash_get_ptable();
2695 if (ptable == NULL) {
2696 fastboot_fail("partition table doesn't exist");
2697 return;
2698 }
2699
2700 ptn = ptable_find(ptable, arg);
2701 if (ptn == NULL) {
2702 fastboot_fail("unknown partition name");
2703 return;
2704 }
2705
2706 if (flash_erase(ptn)) {
2707 fastboot_fail("failed to erase partition");
2708 return;
2709 }
2710 fastboot_okay("");
2711}
2712
Bikas Gurungd48bd242010-09-04 19:54:32 -07002713
2714void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2715{
2716 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002717 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002718 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002719 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302720 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002721
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002722#if VERIFIED_BOOT
2723 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2724 {
2725 if(!device.is_unlocked)
2726 {
2727 fastboot_fail("unlock device to erase keystore");
2728 return;
2729 }
2730 }
2731#endif
2732
Kinson Chikf1a43512011-07-14 11:28:39 -07002733 index = partition_get_index(arg);
2734 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002735 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002736
Kinson Chikf1a43512011-07-14 11:28:39 -07002737 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002738 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002739 return;
2740 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002741
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002742 lun = partition_get_lun(index);
2743 mmc_set_lun(lun);
2744
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002745 if (platform_boot_dev_isemmc())
2746 {
2747 if (mmc_erase_card(ptn, size)) {
2748 fastboot_fail("failed to erase partition\n");
2749 return;
2750 }
2751 } else {
2752 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2753 size = partition_get_size(index);
2754 if (size > DEFAULT_ERASE_SIZE)
2755 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002756
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002757 /* Simple inefficient version of erase. Just writing
2758 0 in first several blocks */
2759 if (mmc_write(ptn , size, (unsigned int *)out)) {
2760 fastboot_fail("failed to erase partition");
2761 return;
2762 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302763 /*Erase FDE metadata at the userdata footer*/
2764 if(!(strncmp(arg, "userdata", 8)))
2765 {
2766 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2767 memset((void *)footer, 0, FOOTER_SIZE);
2768
2769 size = partition_get_size(index);
2770
2771 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2772 fastboot_fail("failed to erase userdata footer");
2773 free(footer);
2774 return;
2775 }
2776 free(footer);
2777 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002778 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002779#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302780#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002781 if(!(strncmp(arg, "userdata", 8)))
2782 if(send_delete_keys_to_tz())
2783 ASSERT(0);
2784#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302785#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002786 fastboot_okay("");
2787}
2788
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002789void cmd_erase(const char *arg, void *data, unsigned sz)
2790{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002791#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002792 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002793 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002794 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002795 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002796 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002797 return;
2798 }
2799 }
2800#endif
2801
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002802 if(target_is_emmc_boot())
2803 cmd_erase_mmc(arg, data, sz);
2804 else
2805 cmd_erase_nand(arg, data, sz);
2806}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002807
Ajay Dudani5c761132011-04-07 20:19:04 -07002808void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002809{
2810 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002811 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002812 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002813 char *token = NULL;
2814 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002815 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002816 uint8_t lun = 0;
2817 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05302818 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002819
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002820 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002821 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002822 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002823 if(token)
2824 {
2825 lun = atoi(token);
2826 mmc_set_lun(lun);
2827 lun_set = true;
2828 }
2829
Mao Jinlong226f33a2014-07-04 17:24:10 +08002830 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002831 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002832 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2833 {
2834 if (!aboot_frp_unlock(pname, data, sz))
2835 {
2836 fastboot_info("FRP unlock successful");
2837 fastboot_okay("");
2838 }
2839 else
2840 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2841
2842 return;
2843 }
2844
Mao Jinlong226f33a2014-07-04 17:24:10 +08002845 if (!strcmp(pname, "partition"))
2846 {
2847 dprintf(INFO, "Attempt to write partition image.\n");
2848 if (write_partition(sz, (unsigned char *) data)) {
2849 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002850 return;
2851 }
Mayank Grover351a75e2017-05-30 20:06:08 +05302852
2853 /* Rescan partition table to ensure we have multislot support*/
2854 if (partition_scan_for_multislot())
2855 {
2856 current_active_slot = partition_find_active_slot();
2857 dprintf(INFO, "Multislot supported: Slot %s active",
2858 (SUFFIX_SLOT(current_active_slot)));
2859 }
2860 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07002861 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002862 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002863 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002864#if VERIFIED_BOOT
2865 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2866 {
2867 if(!device.is_unlocked)
2868 {
2869 fastboot_fail("unlock device to flash keystore");
2870 return;
2871 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302872 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002873 {
2874 fastboot_fail("image is not a keystore file");
2875 return;
2876 }
2877 }
2878#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002879 index = partition_get_index(pname);
2880 ptn = partition_get_offset(index);
2881 if(ptn == 0) {
2882 fastboot_fail("partition table doesn't exist");
2883 return;
2884 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002885
Mayank Grover351a75e2017-05-30 20:06:08 +05302886 if (!strncmp(pname, "boot", strlen("boot"))
2887 || !strcmp(pname, "recovery"))
2888 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002889 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2890 fastboot_fail("image is not a boot image");
2891 return;
2892 }
Mayank Grover351a75e2017-05-30 20:06:08 +05302893
2894 /* Reset multislot_partition attributes in case of flashing boot */
2895 if (partition_multislot_is_supported())
2896 {
2897 partition_reset_attributes(index);
2898 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002899 }
2900
2901 if(!lun_set)
2902 {
2903 lun = partition_get_lun(index);
2904 mmc_set_lun(lun);
2905 }
2906
2907 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05302908 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002909 fastboot_fail("size too large");
2910 return;
2911 }
2912 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2913 fastboot_fail("flash write failure");
2914 return;
2915 }
Greg Grisco6e754772011-06-23 12:19:39 -07002916 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002917 }
2918 fastboot_okay("");
2919 return;
2920}
2921
Ajay Dudanide984792015-03-02 09:57:41 -08002922void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2923{
2924 int i, images;
2925 meta_header_t *meta_header;
2926 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302927 /*End of the image address*/
2928 uintptr_t data_end;
2929
2930 if( (UINT_MAX - sz) > (uintptr_t)data )
2931 data_end = (uintptr_t)data + sz;
2932 else
2933 {
2934 fastboot_fail("Cannot flash: image header corrupt");
2935 return;
2936 }
2937
2938 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
2939 {
2940 fastboot_fail("Cannot flash: image header corrupt");
2941 return;
2942 }
Ajay Dudanide984792015-03-02 09:57:41 -08002943
lijuang8ee21882016-04-19 16:57:11 +08002944 /* If device is locked:
2945 * Forbid to flash image to avoid the device to bypass the image
2946 * which with "any" name other than bootloader. Because it maybe
2947 * a meta package of all partitions.
2948 */
2949#if VERIFIED_BOOT
2950 if (target_build_variant_user()) {
2951 if (!device.is_unlocked) {
2952 fastboot_fail("Device is locked, meta image flashing is not allowed");
2953 return;
2954 }
2955#if !VBOOT_MOTA
2956 if(!device.is_unlock_critical) {
2957 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
2958 return;
2959 }
2960#endif
2961 }
2962#endif
2963
Ajay Dudanide984792015-03-02 09:57:41 -08002964 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302965 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
2966 {
2967 fastboot_fail("Cannot flash: image header corrupt");
2968 return;
2969 }
Ajay Dudanide984792015-03-02 09:57:41 -08002970 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2971
2972 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2973
2974 for (i=0; i<images; i++) {
2975
2976 if((img_header_entry[i].ptn_name == NULL) ||
2977 (img_header_entry[i].start_offset == 0) ||
2978 (img_header_entry[i].size == 0))
2979 break;
Kishor PK49831502017-04-21 17:55:43 +05302980 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
2981 fastboot_fail("Integer overflow detected in start_offset of img");
2982 break;
2983 }
2984 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
2985 fastboot_fail("Integer overflow detected in size of img");
2986 break;
2987 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05302988 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
2989 + img_header_entry[i].size) )
2990 {
2991 fastboot_fail("Cannot flash: image size mismatch");
2992 break;
2993 }
2994
Ajay Dudanide984792015-03-02 09:57:41 -08002995 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2996 (void *) data + img_header_entry[i].start_offset,
2997 img_header_entry[i].size);
2998 }
2999
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003000 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3001 {
3002 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3003 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3004 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3005 }
3006 else
3007 {
3008 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3009 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3010 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3011 }
3012
3013 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003014 fastboot_okay("");
3015 return;
3016}
3017
Ajay Dudani5c761132011-04-07 20:19:04 -07003018void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3019{
3020 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003021 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003022 uint32_t *fill_buf = NULL;
3023 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07003024 sparse_header_t *sparse_header;
3025 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003026 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003027 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303028 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003029 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303030 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003031 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303032 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303033 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003034
Kinson Chikf1a43512011-07-14 11:28:39 -07003035 index = partition_get_index(arg);
3036 ptn = partition_get_offset(index);
3037 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003038 fastboot_fail("partition table doesn't exist");
3039 return;
3040 }
3041
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303042 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303043
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003044 lun = partition_get_lun(index);
3045 mmc_set_lun(lun);
3046
vijay kumar800255e2015-04-24 20:26:19 +05303047 if (sz < sizeof(sparse_header_t)) {
3048 fastboot_fail("size too low");
3049 return;
3050 }
3051
Ajay Dudani5c761132011-04-07 20:19:04 -07003052 /* Read and skip over sparse image header */
3053 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303054
vijay kumar1321f342015-03-27 12:13:42 +05303055 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003056 fastboot_fail("size too large");
3057 return;
3058 }
3059
vijay kumarde4fcf62015-04-23 13:05:49 +05303060 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303061
Parth Dixit64b1a482016-03-07 16:31:26 +05303062 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303063 fastboot_fail("buffer overreads occured due to invalid sparse header");
3064 return;
3065 }
3066
vijay kumarde4fcf62015-04-23 13:05:49 +05303067 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003068 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303069 fastboot_fail("sparse header size mismatch");
3070 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003071 }
3072
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003073 dprintf (SPEW, "=== Sparse Image Header ===\n");
3074 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3075 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3076 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3077 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3078 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3079 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3080 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3081 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003082
3083 /* Start processing chunks */
3084 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3085 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303086 /* Make sure the total image size does not exceed the partition size */
3087 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3088 fastboot_fail("size too large");
3089 return;
3090 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003091 /* Read and skip over chunk header */
3092 chunk_header = (chunk_header_t *) data;
3093 data += sizeof(chunk_header_t);
3094
Parth Dixit64b1a482016-03-07 16:31:26 +05303095 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303096 fastboot_fail("buffer overreads occured due to invalid sparse header");
3097 return;
3098 }
3099
Ajay Dudani5c761132011-04-07 20:19:04 -07003100 dprintf (SPEW, "=== Chunk Header ===\n");
3101 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3102 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3103 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3104
vijay kumar800255e2015-04-24 20:26:19 +05303105 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003106 {
vijay kumar800255e2015-04-24 20:26:19 +05303107 fastboot_fail("chunk header size mismatch");
3108 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003109 }
3110
wufeng.jiang813dc352015-06-02 23:02:46 -04003111 if (!sparse_header->blk_sz ){
3112 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303113 return;
3114 }
3115
wufeng.jiang813dc352015-06-02 23:02:46 -04003116 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3117
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303118 /* Make sure that the chunk size calculated from sparse image does not
3119 * exceed partition size
3120 */
3121 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3122 {
3123 fastboot_fail("Chunk data size exceeds partition size");
3124 return;
3125 }
3126
Ajay Dudani5c761132011-04-07 20:19:04 -07003127 switch (chunk_header->chunk_type)
3128 {
3129 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003130 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003131 chunk_data_sz))
3132 {
3133 fastboot_fail("Bogus chunk size for chunk type Raw");
3134 return;
3135 }
3136
Parth Dixit64b1a482016-03-07 16:31:26 +05303137 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303138 fastboot_fail("buffer overreads occured due to invalid sparse header");
3139 return;
3140 }
3141
wufeng.jiang813dc352015-06-02 23:02:46 -04003142 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3143 otherwise it will return in the line above
3144 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003145 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003146 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003147 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003148 {
3149 fastboot_fail("flash write failure");
3150 return;
3151 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303152 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3153 fastboot_fail("Bogus size for RAW chunk type");
3154 return;
3155 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003156 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003157 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003158 break;
3159
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003160 case CHUNK_TYPE_FILL:
3161 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3162 sizeof(uint32_t)))
3163 {
3164 fastboot_fail("Bogus chunk size for chunk type FILL");
3165 return;
3166 }
3167
3168 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
3169 if (!fill_buf)
3170 {
3171 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3172 return;
3173 }
3174
Parth Dixit64b1a482016-03-07 16:31:26 +05303175 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303176 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303177 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303178 return;
3179 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003180 fill_val = *(uint32_t *)data;
3181 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003182
3183 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3184 {
3185 fill_buf[i] = fill_val;
3186 }
3187
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303188 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3189 {
3190 fastboot_fail("bogus size for chunk FILL type");
3191 free(fill_buf);
3192 return;
3193 }
3194
wufeng.jiang813dc352015-06-02 23:02:46 -04003195 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003196 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303197 /* Make sure that the data written to partition does not exceed partition size */
3198 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3199 {
3200 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303201 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303202 return;
3203 }
3204
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003205 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3206 sparse_header->blk_sz,
3207 fill_buf))
3208 {
3209 fastboot_fail("flash write failure");
3210 free(fill_buf);
3211 return;
3212 }
3213
3214 total_blocks++;
3215 }
3216
3217 free(fill_buf);
3218 break;
3219
Ajay Dudani5c761132011-04-07 20:19:04 -07003220 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303221 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3222 fastboot_fail("bogus size for chunk DONT CARE type");
3223 return;
3224 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003225 total_blocks += chunk_header->chunk_sz;
3226 break;
3227
Ajay Dudani5c761132011-04-07 20:19:04 -07003228 case CHUNK_TYPE_CRC:
3229 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3230 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003231 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003232 return;
3233 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303234 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3235 fastboot_fail("bogus size for chunk CRC type");
3236 return;
3237 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003238 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303239 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303240 fastboot_fail("integer overflow occured");
3241 return;
3242 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003243 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303244 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303245 fastboot_fail("buffer overreads occured due to invalid sparse header");
3246 return;
3247 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003248 break;
3249
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003250 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003251 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003252 fastboot_fail("Unknown chunk type");
3253 return;
3254 }
3255 }
3256
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003257 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3258 total_blocks, sparse_header->total_blks);
3259
3260 if(total_blocks != sparse_header->total_blks)
3261 {
3262 fastboot_fail("sparse image write failure");
3263 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003264
3265 fastboot_okay("");
3266 return;
3267}
3268
3269void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3270{
3271 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003272 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003273
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003274#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003275 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3276 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003277 int ret=0;
3278 uint32 major_version=0;
3279 uint32 minor_version=0;
3280
3281 ret = scm_svc_version(&major_version,&minor_version);
3282 if(!ret)
3283 {
3284 if(major_version >= 2)
3285 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003286 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003287 {
3288 ret = encrypt_scm((uint32 **) &data, &sz);
3289 if (ret != 0) {
3290 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3291 return;
3292 }
3293
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003294 /* Protect only for SSD */
3295 if (!strcmp(arg, "ssd")) {
3296 ret = scm_protect_keystore((uint32 *) data, sz);
3297 if (ret != 0) {
3298 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3299 return;
3300 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003301 }
3302 }
3303 else
3304 {
3305 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3306 if(ret != 0)
3307 {
3308 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3309 return;
3310 }
3311 }
3312 }
3313 else
3314 {
3315 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3316 magic_number[1] == DECRYPT_MAGIC_1)
3317 {
3318 ret = decrypt_scm((uint32 **) &data, &sz);
3319 if (ret != 0) {
3320 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3321 return;
3322 }
3323 }
3324 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3325 magic_number[1] == ENCRYPT_MAGIC_1)
3326 {
3327 ret = encrypt_scm((uint32 **) &data, &sz);
3328 if (ret != 0) {
3329 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3330 return;
3331 }
3332 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003333 }
3334 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003335 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003336 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003337 dprintf(CRITICAL,"INVALID SVC Version\n");
3338 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003339 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003340#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003341
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003342#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003343 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003344 {
lijuang511a2b52015-08-14 20:50:51 +08003345 /* if device is locked:
3346 * common partition will not allow to be flashed
3347 * critical partition will allow to flash image.
3348 */
3349 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3350 fastboot_fail("Partition flashing is not allowed");
3351 return;
3352 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303353#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003354 /* if device critical is locked:
3355 * common partition will allow to be flashed
3356 * critical partition will not allow to flash image.
3357 */
3358 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
3359 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003360 return;
3361 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303362#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003363 }
3364#endif
3365
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003366 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003367 meta_header = (meta_header_t *) data;
3368 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003369 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003370 else if (meta_header->magic == META_HEADER_MAGIC)
3371 cmd_flash_meta_img(arg, data, sz);
3372 else
3373 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003374
3375#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303376#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003377 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
3378 {
3379 // reset dm_verity mode to enforcing
3380 device.verity_mode = 1;
3381 write_device_info(&device);
3382 }
3383#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303384#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003385
Ajay Dudani5c761132011-04-07 20:19:04 -07003386 return;
3387}
3388
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003389void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3390{
3391 struct ptentry *sys_ptn;
3392 struct ptable *ptable;
3393
3394 ptable = flash_get_ptable();
3395 if (ptable == NULL) {
3396 fastboot_fail("partition table doesn't exist");
3397 return;
3398 }
3399
3400 sys_ptn = ptable_find(ptable, "system");
3401 if (sys_ptn == NULL) {
3402 fastboot_fail("system partition not found");
3403 return;
3404 }
3405
3406 sz = ROUND_TO_PAGE(sz, page_mask);
3407 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3408 fastboot_fail("update_ubi_vol failed");
3409 else
3410 fastboot_okay("");
3411}
3412
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003413void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003414{
3415 struct ptentry *ptn;
3416 struct ptable *ptable;
3417 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303418 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303419 unsigned bytes_to_round_page = 0;
3420 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003421
Kishor PK38ed93d2017-04-25 14:19:26 +05303422 if((uintptr_t)data > (UINT_MAX - sz)) {
3423 fastboot_fail("Cannot flash: image header corrupt");
3424 return;
3425 }
3426
Dima Zavin214cc642009-01-26 11:16:21 -08003427 ptable = flash_get_ptable();
3428 if (ptable == NULL) {
3429 fastboot_fail("partition table doesn't exist");
3430 return;
3431 }
3432
3433 ptn = ptable_find(ptable, arg);
3434 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003435 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3436 arg);
3437 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003438 return;
3439 }
3440
3441 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05303442 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
3443 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
3444 } else {
3445 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08003446 return;
3447 }
3448 }
3449
Amol Jadi5c61a952012-05-04 17:05:35 -07003450 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003451 || !strcmp(ptn->name, "userdata")
3452 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003453 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003454 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003455 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05303456 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303457 rounded_size = ROUNDUP(sz, page_size);
3458 bytes_to_round_page = rounded_size - sz;
3459 if (bytes_to_round_page) {
3460 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
3461 fastboot_fail("Integer overflow detected");
3462 return;
3463 }
3464 if (((uintptr_t)data + sz + bytes_to_round_page) >
3465 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
3466 fastboot_fail("Buffer size is not aligned to page_size");
3467 return;
3468 }
3469 else {
3470 memset(data + sz, 0, bytes_to_round_page);
3471 sz = rounded_size;
3472 }
Kishor PK38ed93d2017-04-25 14:19:26 +05303473 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303474 }
3475
Kishor PK38ed93d2017-04-25 14:19:26 +05303476 /*Checking partition_size for the possible integer overflow */
3477 partition_size = validate_partition_size(ptn);
3478
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303479 if (sz > partition_size) {
3480 fastboot_fail("Image size too large");
3481 return;
3482 }
3483
Dima Zavin214cc642009-01-26 11:16:21 -08003484 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Kishor PK38ed93d2017-04-25 14:19:26 +05303485 if ((sz > UBI_MAGIC_SIZE) && (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003486 if (flash_ubi_img(ptn, data, sz)) {
3487 fastboot_fail("flash write failure");
3488 return;
3489 }
3490 } else {
3491 if (flash_write(ptn, extra, data, sz)) {
3492 fastboot_fail("flash write failure");
3493 return;
3494 }
Dima Zavin214cc642009-01-26 11:16:21 -08003495 }
3496 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3497 fastboot_okay("");
3498}
3499
Kishor PK38ed93d2017-04-25 14:19:26 +05303500
3501static inline uint64_t validate_partition_size(struct ptentry *ptn)
3502{
3503 if (ptn->length && flash_num_pages_per_blk() && page_size) {
3504 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
3505 return ptn->length * flash_num_pages_per_blk() * page_size;
3506 }
3507 }
3508 return 0;
3509}
3510
3511
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003512void cmd_flash(const char *arg, void *data, unsigned sz)
3513{
3514 if(target_is_emmc_boot())
3515 cmd_flash_mmc(arg, data, sz);
3516 else
3517 cmd_flash_nand(arg, data, sz);
3518}
3519
Dima Zavin214cc642009-01-26 11:16:21 -08003520void cmd_continue(const char *arg, void *data, unsigned sz)
3521{
3522 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003523 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003524
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003525 if (target_is_emmc_boot())
3526 {
lijuanga40d6302015-07-20 20:10:13 +08003527#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003528 /* Exit keys' detection thread firstly */
3529 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003530#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003531 boot_linux_from_mmc();
3532 }
3533 else
3534 {
3535 boot_linux_from_flash();
3536 }
Dima Zavin214cc642009-01-26 11:16:21 -08003537}
3538
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003539void cmd_reboot(const char *arg, void *data, unsigned sz)
3540{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003541 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003542 fastboot_okay("");
3543 reboot_device(0);
3544}
3545
Mayank Grover351a75e2017-05-30 20:06:08 +05303546void cmd_set_active(const char *arg, void *data, unsigned sz)
3547{
3548 char *p = NULL;
3549 unsigned i,current_active_slot;
3550 const char *current_slot_suffix;
3551
3552 if (!partition_multislot_is_supported())
3553 {
3554 fastboot_fail("Command not supported");
3555 return;
3556 }
3557
3558 if (arg)
3559 {
3560 p = (char *)arg;
3561 if (p)
3562 {
3563 current_active_slot = partition_find_active_slot();
3564
3565 /* Check if trying to make curent slot active */
3566 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
3567 if (!strncmp(p, current_slot_suffix, sizeof(current_slot_suffix)))
3568 {
3569 fastboot_okay("Slot already set active");
3570 return;
3571 }
3572 else
3573 {
3574 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
3575 {
3576 current_slot_suffix = SUFFIX_SLOT(i);
3577 if (!strncmp(p, current_slot_suffix, sizeof(current_slot_suffix)))
3578 {
3579 partition_switch_slots(current_active_slot, i);
3580 publish_getvar_multislot_vars();
3581 fastboot_okay("");
3582 return;
3583 }
3584 }
3585 }
3586 }
3587 }
3588 fastboot_fail("Invalid slot suffix.");
3589 return;
3590}
3591
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003592void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3593{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003594 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003595 fastboot_okay("");
3596 reboot_device(FASTBOOT_MODE);
3597}
3598
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003599void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3600{
3601 dprintf(INFO, "Enabling charger screen check\n");
3602 device.charger_screen_enabled = 1;
3603 write_device_info(&device);
3604 fastboot_okay("");
3605}
3606
3607void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3608{
3609 dprintf(INFO, "Disabling charger screen check\n");
3610 device.charger_screen_enabled = 0;
3611 write_device_info(&device);
3612 fastboot_okay("");
3613}
3614
lijuanga25d8bb2015-09-16 13:11:52 +08003615void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3616{
3617 char *p = NULL;
3618 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303619 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003620
3621 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303622 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003623 if (p) {
3624 if (!strncmp(p, "0", 1)) {
3625 device.charger_screen_enabled = 0;
3626 } else if (!strncmp(p, "1", 1)) {
3627 device.charger_screen_enabled = 1;
3628 }
3629 }
3630 }
3631
3632 /* update charger_screen_enabled value for getvar
3633 * command
3634 */
3635 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3636 device.charger_screen_enabled);
3637
3638 write_device_info(&device);
3639 fastboot_okay("");
3640}
3641
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303642void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3643{
3644 dprintf(INFO, "Selecting display panel %s\n", arg);
3645 if (arg)
3646 strlcpy(device.display_panel, arg,
3647 sizeof(device.display_panel));
3648 write_device_info(&device);
3649 fastboot_okay("");
3650}
3651
Shashank Mittal162244e2011-08-08 19:01:25 -07003652void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3653{
lijuang4ece1e72015-08-14 21:02:36 +08003654 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303655}
3656
3657void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3658{
lijuang4ece1e72015-08-14 21:02:36 +08003659 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303660 if(!is_allow_unlock) {
3661 fastboot_fail("oem unlock is not allowed");
3662 return;
3663 }
3664
lijuang4ece1e72015-08-14 21:02:36 +08003665 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303666
lijuang4ece1e72015-08-14 21:02:36 +08003667 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303668 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05303669 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05303670 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3671 write_misc(0, &msg, sizeof(msg));
3672
3673 fastboot_okay("");
3674 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003675 }
3676 fastboot_okay("");
3677}
3678
Channagoud Kadabiad259832015-05-29 11:14:17 -07003679static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3680{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303681 int ret=1;
3682 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003683
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303684 /*
3685 Authentication method not implemented.
3686
3687 OEM to implement, authentication system which on successful validataion,
3688 calls write_allow_oem_unlock() with is_allow_unlock.
3689 */
3690#if 0
3691 authentication_success = oem_specific_auth_mthd();
3692#endif
3693
3694 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07003695 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303696 is_allow_unlock = true;
3697 write_allow_oem_unlock(is_allow_unlock);
3698 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003699 }
3700 return ret;
3701}
3702
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003703void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3704{
lijuang4ece1e72015-08-14 21:02:36 +08003705 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003706}
3707
Shashank Mittala0032282011-08-26 14:50:11 -07003708void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3709{
lijuang511a2b52015-08-14 20:50:51 +08003710 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003711 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003712 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003713 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3714 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303715#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003716 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3717 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303718#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003719 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003720 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303721 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3722 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003723 fastboot_okay("");
3724}
3725
lijuang511a2b52015-08-14 20:50:51 +08003726void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3727{
3728 char response[MAX_RSP_SIZE];
3729 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3730 fastboot_info(response);
3731 fastboot_okay("");
3732}
3733
3734void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3735{
lijuang4ece1e72015-08-14 21:02:36 +08003736 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003737}
3738
3739void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3740{
lijuang4ece1e72015-08-14 21:02:36 +08003741 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003742}
3743
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003744void cmd_preflash(const char *arg, void *data, unsigned sz)
3745{
3746 fastboot_okay("");
3747}
3748
Mao Flynn7b379f32015-04-20 00:28:30 +08003749static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303750
Mao Flynn7b379f32015-04-20 00:28:30 +08003751int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303752{
Mao Flynn7b379f32015-04-20 00:28:30 +08003753 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303754 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003755 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303756 return -1;
3757 return 0;
3758}
3759
Mao Flynn7b379f32015-04-20 00:28:30 +08003760int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003761{
3762 struct ptentry *ptn;
3763 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003764 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003765 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003766
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303767 ptable = flash_get_ptable();
3768 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003769 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3770 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303771 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003772
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303773 ptn = ptable_find(ptable, "splash");
3774 if (ptn == NULL) {
3775 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003776 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303777 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003778 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303779 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003780 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303781 }
3782
Mao Flynn7b379f32015-04-20 00:28:30 +08003783 header = (struct logo_img_header *)logo_header;
3784 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303785 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003786 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303787 }
3788
3789 fb_display = fbcon_display();
3790 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003791 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3792 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3793
3794 /* if the logo is full-screen size, remove "fbcon_clear()" */
3795 if ((header->width != fb_display->width)
3796 || (header->height != fb_display->height))
3797 fbcon_clear();
3798
3799 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3800 (uint32_t *)base,
3801 (header->blocks * 512))) {
3802 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3803 return -1;
3804 }
3805 fbcon_extract_to_screen(header, base);
3806 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003807 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003808
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003809 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3810 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003811 return -1;
3812 }
3813
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303814 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05303815 uint32_t fb_size = ROUNDUP(fb_display->width *
3816 fb_display->height *
3817 (fb_display->bpp / 8), 4096);
3818 uint32_t splash_size = ((((header->width * header->height *
3819 fb_display->bpp/8) + 511) >> 9) << 9);
3820
3821 if (splash_size > fb_size) {
3822 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
3823 return -1;
3824 }
3825
Mao Flynn7b379f32015-04-20 00:28:30 +08003826 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3827 (uint32_t *)base,
3828 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303829 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303830 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003831 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003832 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303833 }
3834
Mao Flynn7b379f32015-04-20 00:28:30 +08003835 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303836}
3837
Mao Flynn7b379f32015-04-20 00:28:30 +08003838int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303839{
3840 int index = INVALID_PTN;
3841 unsigned long long ptn = 0;
3842 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003843 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003844 uint32_t blocksize, realsize, readsize;
3845 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303846
3847 index = partition_get_index("splash");
3848 if (index == 0) {
3849 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003850 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303851 }
3852
3853 ptn = partition_get_offset(index);
3854 if (ptn == 0) {
3855 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003856 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303857 }
3858
Mao Flynn1893a7f2015-06-03 12:03:36 +08003859 mmc_set_lun(partition_get_lun(index));
3860
3861 blocksize = mmc_get_device_blocksize();
3862 if (blocksize == 0) {
3863 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3864 return -1;
3865 }
3866
3867 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003868 if (!fb_display)
3869 {
3870 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3871 return -1;
3872 }
3873
Mao Flynn1893a7f2015-06-03 12:03:36 +08003874 base = (uint8_t *) fb_display->base;
3875
3876 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303877 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003878 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303879 }
3880
Mao Flynn1893a7f2015-06-03 12:03:36 +08003881 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003882 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303883 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003884 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303885 }
3886
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303887 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003888 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3889 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003890
Mao Flynn1893a7f2015-06-03 12:03:36 +08003891 realsize = header->blocks * 512;
3892 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3893
3894 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003895 if ((header->width != fb_display->width)
3896 || (header->height != fb_display->height))
3897 fbcon_clear();
3898
Sachin Bhayare619e9e42017-05-15 13:10:31 +05303899 uint32_t fb_size = ROUNDUP(fb_display->width *
3900 fb_display->height *
3901 (fb_display->bpp / 8), 4096);
3902
3903 if (readsize > fb_size) {
3904 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
3905 return -1;
3906 }
3907
Mao Flynn1893a7f2015-06-03 12:03:36 +08003908 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003909 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3910 return -1;
3911 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003912
Mao Flynn1893a7f2015-06-03 12:03:36 +08003913 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3914 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303915
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003916 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3917 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003918 return -1;
3919 }
3920
3921 realsize = header->width * header->height * fb_display->bpp / 8;
3922 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3923
3924 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3925 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3926 fbcon_clear();
3927 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3928 return -1;
3929 }
3930 } else {
3931 if (mmc_read(ptn + blocksize ,
3932 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3933 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3934 return -1;
3935 }
3936 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3937 }
3938 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303939 }
3940
Mao Flynn7b379f32015-04-20 00:28:30 +08003941 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303942}
3943
Mao Flynn7b379f32015-04-20 00:28:30 +08003944int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303945{
3946 if (target_is_emmc_boot()) {
3947 return splash_screen_mmc();
3948 } else {
3949 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003950 }
3951}
3952
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003953/* Get the size from partiton name */
3954static void get_partition_size(const char *arg, char *response)
3955{
3956 uint64_t ptn = 0;
3957 uint64_t size;
3958 int index = INVALID_PTN;
3959
3960 index = partition_get_index(arg);
3961
3962 if (index == INVALID_PTN)
3963 {
3964 dprintf(CRITICAL, "Invalid partition index\n");
3965 return;
3966 }
3967
3968 ptn = partition_get_offset(index);
3969
3970 if(!ptn)
3971 {
3972 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3973 return;
3974 }
3975
3976 size = partition_get_size(index);
3977
3978 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3979 return;
3980}
3981
3982/*
3983 * Publish the partition type & size info
3984 * fastboot getvar will publish the required information.
3985 * fastboot getvar partition_size:<partition_name>: partition size in hex
3986 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3987 */
3988static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3989{
3990 uint8_t i;
3991
3992 for (i = 0; i < num_parts; i++) {
3993 get_partition_size(info[i].part_name, info[i].size_response);
3994
3995 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3996 {
3997 dprintf(CRITICAL, "partition size name truncated\n");
3998 return;
3999 }
4000 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
4001 {
4002 dprintf(CRITICAL, "partition type name truncated\n");
4003 return;
4004 }
4005
4006 /* publish partition size & type info */
4007 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
4008 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
4009 }
4010}
4011
Mayank Grover351a75e2017-05-30 20:06:08 +05304012void publish_getvar_multislot_vars()
4013{
4014 int i,count;
4015 static bool published = false;
4016 static char slot_count[MAX_RSP_SIZE];
4017 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4018 static char active_slot_suffix[MAX_RSP_SIZE];
4019 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4020 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4021 const char *tmp;
4022 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304023 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304024
4025 if (!published)
4026 {
4027 /* Update slot meta info */
4028 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4029 partition_get_partition_count());
4030 for(i=0; i<count; i++)
4031 {
4032 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304033 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4034 has_slot_pname[i]);
4035 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304036 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4037 }
4038
4039 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4040 {
4041 tmp = SUFFIX_SLOT(i);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304042 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4043 "slot-unbootable:%s", tmp);
4044 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4045 "slot-active:%s", tmp);
4046 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4047 "slot-success:%s", tmp);
4048 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4049 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304050 fastboot_publish(slot_info[i].slot_is_unbootable,
4051 slot_info[i].slot_is_unbootable_rsp);
4052 fastboot_publish(slot_info[i].slot_is_active,
4053 slot_info[i].slot_is_active_rsp);
4054 fastboot_publish(slot_info[i].slot_is_succesful,
4055 slot_info[i].slot_is_succesful_rsp);
4056 fastboot_publish(slot_info[i].slot_retry_count,
4057 slot_info[i].slot_retry_count_rsp);
4058 }
4059 fastboot_publish("current-slot", active_slot_suffix);
4060 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4061 fastboot_publish("slot-count", slot_count);
4062 published = true;
4063 }
4064
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304065 active_slt = partition_find_active_slot();
4066 if (active_slt != INVALID)
4067 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s",
4068 SUFFIX_SLOT(active_slt));
4069 else
4070 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4071
Mayank Grover351a75e2017-05-30 20:06:08 +05304072 /* Update partition meta information */
4073 partition_fill_slot_meta(slot_info);
4074 return;
4075}
4076
lijuang4ece1e72015-08-14 21:02:36 +08004077void get_product_name(unsigned char *buf)
4078{
4079 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4080 return;
4081}
4082
4083void get_bootloader_version(unsigned char *buf)
4084{
4085 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4086 return;
4087}
4088
4089void get_baseband_version(unsigned char *buf)
4090{
4091 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4092 return;
4093}
4094
4095bool is_device_locked()
4096{
4097 return device.is_unlocked ? false:true;
4098}
4099
Amol Jadi5edf3552013-07-23 14:15:34 -07004100/* register commands and variables for fastboot */
4101void aboot_fastboot_register_commands(void)
4102{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004103 int i;
lijuangf16461c2015-08-03 17:09:34 +08004104 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004105
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004106 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004107 /* By default the enabled list is empty. */
4108 {"", NULL},
4109 /* move commands enclosed within the below ifndef to here
4110 * if they need to be enabled in user build.
4111 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004112#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004113 /* Register the following commands only for non-user builds */
4114 {"flash:", cmd_flash},
4115 {"erase:", cmd_erase},
4116 {"boot", cmd_boot},
4117 {"continue", cmd_continue},
4118 {"reboot", cmd_reboot},
4119 {"reboot-bootloader", cmd_reboot_bootloader},
4120 {"oem unlock", cmd_oem_unlock},
4121 {"oem unlock-go", cmd_oem_unlock_go},
4122 {"oem lock", cmd_oem_lock},
4123 {"flashing unlock", cmd_oem_unlock},
4124 {"flashing lock", cmd_oem_lock},
4125 {"flashing lock_critical", cmd_flashing_lock_critical},
4126 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4127 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4128 {"oem device-info", cmd_oem_devinfo},
4129 {"preflash", cmd_preflash},
4130 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4131 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004132 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004133 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover351a75e2017-05-30 20:06:08 +05304134 {"oem set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004135#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004136 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004137#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004138#endif
lijuang511a2b52015-08-14 20:50:51 +08004139 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004140
4141 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4142 for (i = 1; i < fastboot_cmds_count; i++)
4143 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4144
Amol Jadi5edf3552013-07-23 14:15:34 -07004145 /* publish variables and their values */
4146 fastboot_publish("product", TARGET(BOARD));
4147 fastboot_publish("kernel", "lk");
4148 fastboot_publish("serialno", sn_buf);
4149
4150 /*
4151 * partition info is supported only for emmc partitions
4152 * Calling this for NAND prints some error messages which
4153 * is harmless but misleading. Avoid calling this for NAND
4154 * devices.
4155 */
4156 if (target_is_emmc_boot())
4157 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
4158
Mayank Grover351a75e2017-05-30 20:06:08 +05304159 if (partition_multislot_is_supported())
4160 publish_getvar_multislot_vars();
4161
Amol Jadi5edf3552013-07-23 14:15:34 -07004162 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004163 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4164 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004165 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004166 /* Is the charger screen check enabled */
4167 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4168 device.charger_screen_enabled);
4169 fastboot_publish("charger-screen-enabled",
4170 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004171 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304172 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4173 device.display_panel);
4174 fastboot_publish("display-panel",
4175 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004176 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4177 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08004178 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
4179 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4180 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4181 target_is_emmc_boot()? "eMMC":"UFS");
4182 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004183#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004184 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004185 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004186 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004187#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004188}
4189
Brian Swetland9c4c0752009-01-25 16:23:50 -08004190void aboot_init(const struct app_descriptor *app)
4191{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004192 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304193 int boot_err_type = 0;
4194 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004195
lijuang39831732016-01-08 17:49:02 +08004196 /* Initialise wdog to catch early lk crashes */
4197#if WDOG_SUPPORT
4198 msm_wdog_init();
4199#endif
4200
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004201 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004202 if (target_is_emmc_boot())
4203 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004204 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004205 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304206 mmc_blocksize = mmc_get_device_blocksize();
4207 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004208 }
4209 else
4210 {
4211 page_size = flash_page_size();
4212 page_mask = page_size - 1;
4213 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07004214 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
4215
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004216 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05304217 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07004218
Mayank Grover351a75e2017-05-30 20:06:08 +05304219 /* Detect multi-slot support */
4220 if (partition_multislot_is_supported())
4221 {
4222 boot_slot = partition_find_active_slot();
4223 if (boot_slot == INVALID)
4224 {
4225 boot_into_fastboot = true;
4226 dprintf(INFO, "Active Slot: (INVALID)\n");
4227 }
4228 else
4229 {
4230 /* Setting the state of system to boot active slot */
4231 partition_mark_active_slot(boot_slot);
4232 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
4233 }
4234 }
4235
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004236 /* Display splash screen if enabled */
4237#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08004238#if NO_ALARM_DISPLAY
4239 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004240#endif
lijuang99c02d82015-02-13 19:04:34 +08004241 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07004242#if DISPLAY_HDMI_PRIMARY
4243 if (!strlen(device.display_panel))
4244 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
4245 sizeof(device.display_panel));
4246#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004247#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07004248 /* Wait if the display shutdown is in progress */
4249 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004250 if (!pm_appsbl_display_init_done())
4251 target_display_init(device.display_panel);
4252 else
4253 display_image_on_screen();
4254#else
lijuang99c02d82015-02-13 19:04:34 +08004255 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004256#endif
lijuang99c02d82015-02-13 19:04:34 +08004257 dprintf(SPEW, "Display Init: Done\n");
4258#if NO_ALARM_DISPLAY
4259 }
4260#endif
4261#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004262
Greg Griscod6250552011-06-29 14:40:23 -07004263 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004264 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08004265
Dhaval Patel223ec952013-07-18 14:49:44 -07004266 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
4267
Matthew Qindefd5562014-07-11 18:02:40 +08004268 /*
4269 * Check power off reason if user force reset,
4270 * if yes phone will do normal boot.
4271 */
4272 if (is_user_force_reset())
4273 goto normal_boot;
4274
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004275 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004276 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004277 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03004278 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08004279 reboot_device(EMERGENCY_DLOAD);
4280 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
4281
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004282 boot_into_fastboot = true;
4283 }
4284 if (!boot_into_fastboot)
4285 {
4286 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
4287 boot_into_recovery = 1;
4288 if (!boot_into_recovery &&
4289 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004290 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004291 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004292 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004293 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004294 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004295 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004296
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004297#if USE_PON_REBOOT_REG
4298 reboot_mode = check_hard_reboot_mode();
4299#else
Ajay Dudani77421292010-10-27 19:34:06 -07004300 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004301#endif
4302 if (reboot_mode == RECOVERY_MODE)
4303 {
Ajay Dudani77421292010-10-27 19:34:06 -07004304 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004305 }
4306 else if(reboot_mode == FASTBOOT_MODE)
4307 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004308 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004309 }
4310 else if(reboot_mode == ALARM_BOOT)
4311 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004312 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004313 }
Parth Dixit207573a2015-10-27 17:26:21 +05304314#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05304315#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004316 else if (reboot_mode == DM_VERITY_ENFORCING)
4317 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004318 device.verity_mode = 1;
4319 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004320 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004321#if ENABLE_VB_ATTEST
4322 else if (reboot_mode == DM_VERITY_EIO)
4323#else
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004324 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004325#endif
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004326 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004327 device.verity_mode = 0;
4328 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004329 }
4330 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4331 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004332 if(send_delete_keys_to_tz())
4333 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07004334 }
Parth Dixit207573a2015-10-27 17:26:21 +05304335#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05304336#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004337
Matthew Qindefd5562014-07-11 18:02:40 +08004338normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004339 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004340 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004341 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004342 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004343 if(emmc_recovery_init())
4344 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4345 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004346 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004347 if((device.is_unlocked) || (device.is_tampered))
4348 {
4349 #ifdef TZ_TAMPER_FUSE
4350 set_tamper_fuse_cmd();
4351 #endif
4352 #if USE_PCOM_SECBOOT
4353 set_tamper_flag(device.is_tampered);
4354 #endif
4355 }
Shashank Mittala0032282011-08-26 14:50:11 -07004356 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004357
Mayank Grover351a75e2017-05-30 20:06:08 +05304358retry_boot:
4359 /* Trying to boot active partition */
4360 if (partition_multislot_is_supported())
4361 {
4362 boot_slot = partition_find_boot_slot();
4363 partition_mark_active_slot(boot_slot);
4364 if (boot_slot == INVALID)
4365 goto fastboot;
4366 }
4367
4368 boot_err_type = boot_linux_from_mmc();
4369 switch (boot_err_type)
4370 {
4371 case ERR_INVALID_PAGE_SIZE:
4372 case ERR_DT_PARSE:
4373 case ERR_ABOOT_ADDR_OVERLAP:
4374 if(partition_multislot_is_supported())
4375 goto retry_boot;
4376 else
4377 break;
4378 case ERR_INVALID_BOOT_MAGIC:
4379 default:
4380 break;
4381 /* going to fastboot menu */
4382 }
Shashank Mittala0032282011-08-26 14:50:11 -07004383 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004384 else
4385 {
4386 recovery_init();
4387 #if USE_PCOM_SECBOOT
4388 if((device.is_unlocked) || (device.is_tampered))
4389 set_tamper_flag(device.is_tampered);
4390 #endif
4391 boot_linux_from_flash();
4392 }
4393 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4394 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004395 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004396
Mayank Grover351a75e2017-05-30 20:06:08 +05304397fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07004398 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004399
Amol Jadi5edf3552013-07-23 14:15:34 -07004400 /* register aboot specific fastboot commands */
4401 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004402
Amol Jadi5edf3552013-07-23 14:15:34 -07004403 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004404 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004405
4406 /* initialize and start fastboot */
4407 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004408#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004409 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004410#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004411}
4412
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004413uint32_t get_page_size()
4414{
4415 return page_size;
4416}
4417
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004418/*
4419 * Calculated and save hash (SHA256) for non-signed boot image.
4420 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004421 * @param image_addr - Boot image address
4422 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004423 *
4424 * @return int - 0 on success, negative value on failure.
4425 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004426static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004427{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004428 unsigned int digest[8];
4429#if IMAGE_VERIF_ALGO_SHA1
4430 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4431#else
4432 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4433#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004434
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004435 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004436 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004437
4438 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004439 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004440
4441 return 0;
4442}
4443
Mayank Grover351a75e2017-05-30 20:06:08 +05304444
Brian Swetland9c4c0752009-01-25 16:23:50 -08004445APP_START(aboot)
4446 .init = aboot_init,
4447APP_END