blob: 18ecf8be06789cedbeddd9c41816ea7a5da80939 [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";
Mayank Grover3804be72017-06-22 11:59:23 +0530188static const char *sys_path_cmdline = " rootwait ro init=/init";
189static const char *sys_path = " root=/dev/mmcblk0p";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800190
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700191#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530192#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700193static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700194static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530195static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700196//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700197
198struct verified_boot_verity_mode vbvm[] =
199{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700200#if ENABLE_VB_ATTEST
201 {false, "eio"},
202#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700203 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700204#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700205 {true, "enforcing"},
206};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700207struct verified_boot_state_name vbsn[] =
208{
209 {GREEN, "green"},
210 {ORANGE, "orange"},
211 {YELLOW,"yellow"},
212 {RED,"red" },
213};
214#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530215#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700216/*As per spec delay wait time before shutdown in Red state*/
217#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700218static unsigned page_size = 0;
219static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530220static unsigned mmc_blocksize = 0;
221static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700222static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
223static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530224static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800225static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700226static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700227bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530228static uint32_t dt_size = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700229
Shashank Mittalcd98d472011-08-02 14:29:24 -0700230/* Assuming unauthorized kernel image by default */
231static int auth_kernel_img = 0;
Parth Dixit6f5822f2015-10-18 01:20:53 +0530232#if VBOOT_MOTA
233static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
234#else
lijuang511a2b52015-08-14 20:50:51 +0800235static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
Parth Dixit6f5822f2015-10-18 01:20:53 +0530236#endif
vijay kumarc65876c2015-04-24 13:29:16 +0530237static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700238
vijay kumarca2e6812015-07-08 20:28:25 +0530239static char frp_ptns[2][8] = {"config","frp"};
240
lijuang511a2b52015-08-14 20:50:51 +0800241static const char *critical_flash_allowed_ptn[] = {
242 "aboot",
243 "rpm",
244 "tz",
245 "sbl",
246 "sdi",
247 "sbl1",
248 "xbl",
249 "hyp",
250 "pmic",
251 "bootloader",
252 "devinfo",
253 "partition"};
254
Dima Zavin42168f22009-01-30 11:52:22 -0800255struct atag_ptbl_entry
256{
257 char name[16];
258 unsigned offset;
259 unsigned size;
260 unsigned flags;
261};
262
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700263/*
264 * Partition info, required to be published
265 * for fastboot
266 */
267struct getvar_partition_info {
268 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
269 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
270 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
271 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
272 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
273};
274
275/*
276 * Right now, we are publishing the info for only
277 * three partitions
278 */
279struct getvar_partition_info part_info[] =
280{
281 { "system" , "partition-size:", "partition-type:", "", "ext4" },
282 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
283 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
284};
285
286char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700287char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800288char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700289char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530290char panel_display_mode[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800291
292#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800293char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800294char battery_soc_ok [MAX_RSP_SIZE];
295#endif
296
lijuangf16461c2015-08-03 17:09:34 +0800297char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700298
Greg Griscod2471ef2011-07-14 13:00:42 -0700299extern int emmc_recovery_init(void);
300
Kinson Chik0b1c8162011-08-31 16:31:57 -0700301#if NO_KEYPAD_DRIVER
302extern int fastboot_trigger(void);
303#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700304
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800305static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700306{
307 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700308#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800309 if (is_arm64)
310 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
311 else
312 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700313 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
314 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700315#endif
316}
317
Dima Zavin42168f22009-01-30 11:52:22 -0800318static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
319{
320 struct atag_ptbl_entry atag_ptn;
321
322 memcpy(atag_ptn.name, ptn->name, 16);
323 atag_ptn.name[15] = '\0';
324 atag_ptn.offset = ptn->start;
325 atag_ptn.size = ptn->length;
326 atag_ptn.flags = ptn->flags;
327 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
328 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
329}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800330
lijuang102dfa92015-10-09 18:31:03 +0800331#if CHECK_BAT_VOLTAGE
332void update_battery_status(void)
333{
334 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
335 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
336}
337#endif
338
Neeti Desaie245d492012-06-01 12:52:13 -0700339unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800340{
David Ng183a7422009-12-07 14:55:21 -0800341 int cmdline_len = 0;
342 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800343 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700344 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800345 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300346 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700347 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700348 char *boot_dev_buf = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +0530349 bool is_mdtp_activated = 0;
350 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530351 int system_ptn_index = -1;
352 unsigned int lun = 0;
353 char lun_char_base = 'a';
354 int syspath_buflen = strlen(sys_path) + sizeof(int) + 1; /*allocate buflen for largest possible string*/
355 char syspath_buf[syspath_buflen];
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530356
357#if USE_LE_SYSTEMD
358 is_systemd_present=true;
359#endif
360
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700361#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530362#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700363 uint32_t boot_state = boot_verify_get_state();
364#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530365#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700366
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200367#ifdef MDTP_SUPPORT
368 mdtp_activated(&is_mdtp_activated);
369#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800370
Brian Swetland9c4c0752009-01-25 16:23:50 -0800371 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800372 cmdline_len = strlen(cmdline);
373 have_cmdline = 1;
374 }
375 if (target_is_emmc_boot()) {
376 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800377#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700378 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
379 ASSERT(boot_dev_buf);
380 platform_boot_dev_cmdline(boot_dev_buf);
381 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700382#endif
David Ng183a7422009-12-07 14:55:21 -0800383 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800384
385 cmdline_len += strlen(usb_sn_cmdline);
386 cmdline_len += strlen(sn_buf);
387
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700388#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530389#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700390 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700391 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
392 {
393 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
394 ASSERT(0);
395 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700396 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Parth Dixita5715a02015-10-29 12:25:10 +0530397 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700398#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530399#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700400
Pavel Nedev5614d222013-06-17 18:01:02 +0300401 if (boot_into_recovery && gpt_exists)
402 cmdline_len += strlen(secondary_gpt_enable);
403
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200404 if(is_mdtp_activated)
405 cmdline_len += strlen(mdtp_activated_flag);
406
Pavel Nedev328ac822013-04-05 15:25:11 +0300407 if (boot_into_ffbm) {
408 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530409
410 if(is_systemd_present)
411 cmdline_len += strlen(systemd_ffbm_mode);
412
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700413 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800414 /* reduce kernel console messages to speed-up boot */
415 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800416 } else if (boot_reason_alarm) {
417 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800418 } else if ((target_build_variant_user() || device.charger_screen_enabled)
419 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700420 pause_at_bootup = 1;
421 cmdline_len += strlen(battchg_pause);
422 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800423
Shashank Mittalcd98d472011-08-02 14:29:24 -0700424 if(target_use_signed_kernel() && auth_kernel_img) {
425 cmdline_len += strlen(auth_kernel);
426 }
427
Joonwoo Park61112782013-10-02 19:50:39 -0700428 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
429 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530430 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700431 have_target_boot_params = 1;
432 cmdline_len += strlen(target_boot_params);
433 }
434
Ajay Dudanid04110c2011-01-17 23:55:07 -0800435 /* Determine correct androidboot.baseband to use */
436 switch(target_baseband())
437 {
438 case BASEBAND_APQ:
439 cmdline_len += strlen(baseband_apq);
440 break;
441
442 case BASEBAND_MSM:
443 cmdline_len += strlen(baseband_msm);
444 break;
445
446 case BASEBAND_CSFB:
447 cmdline_len += strlen(baseband_csfb);
448 break;
449
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800450 case BASEBAND_SVLTE2A:
451 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800452 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700453
454 case BASEBAND_MDM:
455 cmdline_len += strlen(baseband_mdm);
456 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700457
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800458 case BASEBAND_MDM2:
459 cmdline_len += strlen(baseband_mdm2);
460 break;
461
Amol Jadi5c61a952012-05-04 17:05:35 -0700462 case BASEBAND_SGLTE:
463 cmdline_len += strlen(baseband_sglte);
464 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530465
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800466 case BASEBAND_SGLTE2:
467 cmdline_len += strlen(baseband_sglte2);
468 break;
469
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530470 case BASEBAND_DSDA:
471 cmdline_len += strlen(baseband_dsda);
472 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800473
474 case BASEBAND_DSDA2:
475 cmdline_len += strlen(baseband_dsda2);
476 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530477 case BASEBAND_APQ_NOWGR:
478 cmdline_len += strlen(baseband_apq_nowgr);
479 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800480 }
481
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300482#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800483 if (cmdline) {
484 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530485 target_display_panel_node(display_panel_buf,
486 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800487 strlen(display_panel_buf)) {
488 cmdline_len += strlen(display_panel_buf);
489 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700490 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300491#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700492
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800493 if (target_warm_boot()) {
494 warm_boot = true;
495 cmdline_len += strlen(warmboot_cmdline);
496 }
497
Mayank Grover351a75e2017-05-30 20:06:08 +0530498 if (partition_multislot_is_supported())
499 {
500 current_active_slot = partition_find_active_slot();
501 cmdline_len += (strlen(androidboot_slot_suffix)+
502 strlen(SUFFIX_SLOT(current_active_slot)));
503
Mayank Grover3804be72017-06-22 11:59:23 +0530504 system_ptn_index = partition_get_index("system");
505 if (platform_boot_dev_isemmc())
506 {
507 snprintf(syspath_buf, syspath_buflen, " root=/dev/mmcblk0p%d",
508 system_ptn_index + 1);
509 }
510 else
511 {
512 lun = partition_get_lun(system_ptn_index);
513 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
514 lun_char_base + lun,
515 partition_get_index_in_lun("system", lun));
516 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530517
Mayank Grover3804be72017-06-22 11:59:23 +0530518 cmdline_len += strlen(sys_path_cmdline);
519 cmdline_len += strlen(syspath_buf);
Mayank Grover351a75e2017-05-30 20:06:08 +0530520 if (!boot_into_recovery)
521 cmdline_len += strlen(skip_ramfs);
522 }
523
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800524#if TARGET_CMDLINE_SUPPORT
525 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
526 int target_cmd_line_len;
527 ASSERT(target_cmdline_buf);
528 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
529 cmdline_len += target_cmd_line_len;
530#endif
531
David Ng183a7422009-12-07 14:55:21 -0800532 if (cmdline_len > 0) {
533 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800534 unsigned char *dst;
535
536 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
537 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530538 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800539 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700540
Amol Jadi168b7712012-03-06 16:15:00 -0800541 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800542 if (have_cmdline) {
543 src = cmdline;
544 while ((*dst++ = *src++));
545 }
546 if (target_is_emmc_boot()) {
547 src = emmc_cmdline;
548 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700549 have_cmdline = 1;
550 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800551#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700552 src = boot_dev_buf;
553 if (have_cmdline) --dst;
554 while ((*dst++ = *src++));
555#endif
David Ngf773dde2010-07-26 19:55:08 -0700556 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800557
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700558#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530559#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700560 src = verified_state;
561 if(have_cmdline) --dst;
562 have_cmdline = 1;
563 while ((*dst++ = *src++));
564 src = vbsn[boot_state].name;
565 if(have_cmdline) --dst;
566 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700567
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700568 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
569 {
570 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
571 ASSERT(0);
572 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700573 src = verity_mode;
574 if(have_cmdline) --dst;
575 while ((*dst++ = *src++));
576 src = vbvm[device.verity_mode].name;
577 if(have_cmdline) -- dst;
578 while ((*dst++ = *src++));
Parth Dixita5715a02015-10-29 12:25:10 +0530579 src = keymaster_v1;
580 if(have_cmdline) --dst;
581 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700582#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530583#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800584 src = usb_sn_cmdline;
585 if (have_cmdline) --dst;
586 have_cmdline = 1;
587 while ((*dst++ = *src++));
588 src = sn_buf;
589 if (have_cmdline) --dst;
590 have_cmdline = 1;
591 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800592 if (warm_boot) {
593 if (have_cmdline) --dst;
594 src = warmboot_cmdline;
595 while ((*dst++ = *src++));
596 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800597
Pavel Nedev5614d222013-06-17 18:01:02 +0300598 if (boot_into_recovery && gpt_exists) {
599 src = secondary_gpt_enable;
600 if (have_cmdline) --dst;
601 while ((*dst++ = *src++));
602 }
603
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200604 if (is_mdtp_activated) {
605 src = mdtp_activated_flag;
606 if (have_cmdline) --dst;
607 while ((*dst++ = *src++));
608 }
609
Pavel Nedev328ac822013-04-05 15:25:11 +0300610 if (boot_into_ffbm) {
611 src = androidboot_mode;
612 if (have_cmdline) --dst;
613 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700614 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300615 if (have_cmdline) --dst;
616 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530617
618 if(is_systemd_present) {
619 src = systemd_ffbm_mode;
620 if (have_cmdline) --dst;
621 while ((*dst++ = *src++));
622 }
623
Pavel Nedev898298c2013-02-27 12:36:09 -0800624 src = loglevel;
625 if (have_cmdline) --dst;
626 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800627 } else if (boot_reason_alarm) {
628 src = alarmboot_cmdline;
629 if (have_cmdline) --dst;
630 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300631 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700632 src = battchg_pause;
633 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800634 while ((*dst++ = *src++));
635 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800636
Shashank Mittalcd98d472011-08-02 14:29:24 -0700637 if(target_use_signed_kernel() && auth_kernel_img) {
638 src = auth_kernel;
639 if (have_cmdline) --dst;
640 while ((*dst++ = *src++));
641 }
642
Ajay Dudanid04110c2011-01-17 23:55:07 -0800643 switch(target_baseband())
644 {
645 case BASEBAND_APQ:
646 src = baseband_apq;
647 if (have_cmdline) --dst;
648 while ((*dst++ = *src++));
649 break;
650
651 case BASEBAND_MSM:
652 src = baseband_msm;
653 if (have_cmdline) --dst;
654 while ((*dst++ = *src++));
655 break;
656
657 case BASEBAND_CSFB:
658 src = baseband_csfb;
659 if (have_cmdline) --dst;
660 while ((*dst++ = *src++));
661 break;
662
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800663 case BASEBAND_SVLTE2A:
664 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800665 if (have_cmdline) --dst;
666 while ((*dst++ = *src++));
667 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700668
669 case BASEBAND_MDM:
670 src = baseband_mdm;
671 if (have_cmdline) --dst;
672 while ((*dst++ = *src++));
673 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700674
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800675 case BASEBAND_MDM2:
676 src = baseband_mdm2;
677 if (have_cmdline) --dst;
678 while ((*dst++ = *src++));
679 break;
680
Amol Jadi5c61a952012-05-04 17:05:35 -0700681 case BASEBAND_SGLTE:
682 src = baseband_sglte;
683 if (have_cmdline) --dst;
684 while ((*dst++ = *src++));
685 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530686
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800687 case BASEBAND_SGLTE2:
688 src = baseband_sglte2;
689 if (have_cmdline) --dst;
690 while ((*dst++ = *src++));
691 break;
692
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530693 case BASEBAND_DSDA:
694 src = baseband_dsda;
695 if (have_cmdline) --dst;
696 while ((*dst++ = *src++));
697 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800698
699 case BASEBAND_DSDA2:
700 src = baseband_dsda2;
701 if (have_cmdline) --dst;
702 while ((*dst++ = *src++));
703 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530704 case BASEBAND_APQ_NOWGR:
705 src = baseband_apq_nowgr;
706 if (have_cmdline) --dst;
707 while ((*dst++ = *src++));
708 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800709 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700710
711 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700712 src = display_panel_buf;
713 if (have_cmdline) --dst;
714 while ((*dst++ = *src++));
715 }
Joonwoo Park61112782013-10-02 19:50:39 -0700716
717 if (have_target_boot_params) {
718 if (have_cmdline) --dst;
719 src = target_boot_params;
720 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530721 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700722 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800723
Mayank Grover351a75e2017-05-30 20:06:08 +0530724 if (partition_multislot_is_supported() && have_cmdline)
725 {
726 src = androidboot_slot_suffix;
727 --dst;
728 while ((*dst++ = *src++));
729 --dst;
730 src = SUFFIX_SLOT(current_active_slot);
731 while ((*dst++ = *src++));
732
733 if (!boot_into_recovery)
734 {
735 src = skip_ramfs;
736 --dst;
737 while ((*dst++ = *src++));
738 }
739
740 src = sys_path_cmdline;
741 --dst;
742 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530743
744 src = syspath_buf;
745 --dst;
746 while ((*dst++ = *src++));
Mayank Grover351a75e2017-05-30 20:06:08 +0530747 }
748
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800749#if TARGET_CMDLINE_SUPPORT
750 if (target_cmdline_buf && target_cmd_line_len)
751 {
752 if (have_cmdline) --dst;
753 src = target_cmdline_buf;
754 while((*dst++ = *src++));
755 free(target_cmdline_buf);
756 }
757#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700758 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700759
760
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700761 if (boot_dev_buf)
762 free(boot_dev_buf);
763
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800764 if (cmdline_final)
765 dprintf(INFO, "cmdline: %s\n", cmdline_final);
766 else
767 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700768 return cmdline_final;
769}
770
771unsigned *atag_core(unsigned *ptr)
772{
773 /* CORE */
774 *ptr++ = 2;
775 *ptr++ = 0x54410001;
776
777 return ptr;
778
779}
780
781unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
782 unsigned ramdisk_size)
783{
784 if (ramdisk_size) {
785 *ptr++ = 4;
786 *ptr++ = 0x54420005;
787 *ptr++ = (unsigned)ramdisk;
788 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800789 }
790
Neeti Desaie245d492012-06-01 12:52:13 -0700791 return ptr;
792}
793
794unsigned *atag_ptable(unsigned **ptr_addr)
795{
796 int i;
797 struct ptable *ptable;
798
799 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700800 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
801 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700802 *(*ptr_addr)++ = 0x4d534d70;
803 for (i = 0; i < ptable->count; ++i)
804 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
805 }
806
807 return (*ptr_addr);
808}
809
810unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
811{
812 int cmdline_length = 0;
813 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700814 char *dest;
815
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800816 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700817 n = (cmdline_length + 4) & (~3);
818
819 *ptr++ = (n / 4) + 2;
820 *ptr++ = 0x54410009;
821 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800822 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700823 ptr += (n / 4);
824
825 return ptr;
826}
827
828unsigned *atag_end(unsigned *ptr)
829{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800830 /* END */
831 *ptr++ = 0;
832 *ptr++ = 0;
833
Neeti Desaie245d492012-06-01 12:52:13 -0700834 return ptr;
835}
836
837void generate_atags(unsigned *ptr, const char *cmdline,
838 void *ramdisk, unsigned ramdisk_size)
839{
vijay kumar21a37452015-12-29 16:23:21 +0530840 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700841 ptr = atag_core(ptr);
842 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
843 ptr = target_atag_mem(ptr);
844
845 /* Skip NAND partition ATAGS for eMMC boot */
846 if (!target_is_emmc_boot()){
847 ptr = atag_ptable(&ptr);
848 }
849
vijay kumar21a37452015-12-29 16:23:21 +0530850 /*
851 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
852 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
853 */
854 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
855 ptr = atag_cmdline(ptr, cmdline);
856 ptr = atag_end(ptr);
857 }
858 else {
859 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
860 ASSERT(0);
861 }
Neeti Desaie245d492012-06-01 12:52:13 -0700862}
863
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700864typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700865void boot_linux(void *kernel, unsigned *tags,
866 const char *cmdline, unsigned machtype,
867 void *ramdisk, unsigned ramdisk_size)
868{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800869 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800870#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700871 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800872#endif
873
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700874 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800875 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530876 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800877
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530878 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700879
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800880 final_cmdline = update_cmdline((const char*)cmdline);
881
Neeti Desai17379b82012-06-04 18:42:53 -0700882#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800883 dprintf(INFO, "Updating device tree: start\n");
884
Neeti Desai17379b82012-06-04 18:42:53 -0700885 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530886 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700887 if(ret)
888 {
889 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
890 ASSERT(0);
891 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800892 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700893#else
Neeti Desaie245d492012-06-01 12:52:13 -0700894 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800895 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700896#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700897
Maria Yu52254c02014-07-04 16:14:54 +0800898 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700899
900#if VERIFIED_BOOT
lijuangbdd9bb42016-03-01 18:22:17 +0800901#if !VBOOT_MOTA
902 if (device.verity_mode == 0) {
903#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700904#if ENABLE_VB_ATTEST
905 display_bootverify_menu(DISPLAY_MENU_EIO);
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530906 wait_for_users_action();
907 if(!pwr_key_is_pressed)
908 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700909#else
lijuangbdd9bb42016-03-01 18:22:17 +0800910 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700911#endif
lijuangbdd9bb42016-03-01 18:22:17 +0800912 wait_for_users_action();
913#else
914 dprintf(CRITICAL,
915 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
916 mdelay(5000);
917#endif
918 }
919
920#endif
921#endif
922
923#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700924 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700925 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700926 {
927 dprintf(INFO, "Failed to write protect dev info\n");
928 ASSERT(0);
929 }
930#endif
931
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800932 /* Turn off splash screen if enabled */
933#if DISPLAY_SPLASH_SCREEN
934 target_display_shutdown();
935#endif
936
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700937 /* Perform target specific cleanup */
938 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800939
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800940 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530941 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800942
943 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700944
Amol Jadi4421e652011-06-16 15:00:48 -0700945 /* do any platform specific cleanup before kernel entry */
946 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700947
Brian Swetland9c4c0752009-01-25 16:23:50 -0800948 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700949
Amol Jadi504f9fe2012-08-16 13:56:48 -0700950#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800951 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700952#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700953 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800954
955 if (IS_ARM64(kptr))
956 /* Jump to a 64bit kernel */
957 scm_elexec_call((paddr_t)kernel, tags_phys);
958 else
959 /* Jump to a 32bit kernel */
960 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800961}
962
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700963/* Function to check if the memory address range falls within the aboot
964 * boundaries.
965 * start: Start of the memory region
966 * size: Size of the memory region
967 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +0530968int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700969{
970 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800971 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700972 return -1;
973
974 /* Check for memory overlap. */
975 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
976 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700977 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700978 return 0;
979 else
980 return -1;
981}
982
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800983#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800984
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800985BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800986#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800987BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800988#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800989
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700990static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
991{
992 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800993
994#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800995#if IMAGE_VERIF_ALGO_SHA1
996 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
997#else
998 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
999#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001000#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001001
1002 /* Assume device is rooted at this time. */
1003 device.is_tampered = 1;
1004
1005 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1006
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001007#if VERIFIED_BOOT
1008 if(boot_into_recovery)
1009 {
1010 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +05301011 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001012 }
1013 else
1014 {
1015 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +05301016 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001017 }
1018 boot_verify_print_state();
1019#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001020 ret = image_verify((unsigned char *)bootimg_addr,
1021 (unsigned char *)(bootimg_addr + bootimg_size),
1022 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001023 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001024#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001025 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1026
1027 if (ret)
1028 {
1029 /* Authorized kernel */
1030 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001031 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001032 }
1033
Amit Blay4aa292f2015-04-28 21:55:59 +03001034#ifdef MDTP_SUPPORT
1035 {
1036 /* Verify MDTP lock.
1037 * For boot & recovery partitions, use aboot's verification result.
1038 */
1039 mdtp_ext_partition_verification_t ext_partition;
1040 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1041 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1042 ext_partition.page_size = 0; /* Not needed since already validated */
1043 ext_partition.image_addr = 0; /* Not needed since already validated */
1044 ext_partition.image_size = 0; /* Not needed since already validated */
1045 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1046 mdtp_fwlock_verify_lock(&ext_partition);
1047 }
1048#endif /* MDTP_SUPPORT */
1049
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001050#if USE_PCOM_SECBOOT
1051 set_tamper_flag(device.is_tampered);
1052#endif
1053
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001054#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001055 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001056 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001057 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001058#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001059 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001060#if ENABLE_VB_ATTEST
1061 mdelay(DELAY_WAIT);
1062 shutdown_device();
1063#else
lijuanga40d6302015-07-20 20:10:13 +08001064 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001065#endif
lijuanga40d6302015-07-20 20:10:13 +08001066#else
1067 dprintf(CRITICAL,
1068 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1069 mdelay(5000);
1070#endif
1071
1072 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001073 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001074#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001075 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001076 wait_for_users_action();
1077#else
1078 dprintf(CRITICAL,
1079 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1080 mdelay(5000);
1081#endif
1082 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001083 default:
lijuanga40d6302015-07-20 20:10:13 +08001084 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001085 }
1086#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001087#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301088 if(device.is_tampered)
1089 {
1090 write_device_info_mmc(&device);
1091 #ifdef TZ_TAMPER_FUSE
1092 set_tamper_fuse_cmd();
1093 #endif
1094 #ifdef ASSERT_ON_TAMPER
1095 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1096 ASSERT(0);
1097 #endif
1098 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001099#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001100}
1101
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301102static bool check_format_bit()
1103{
1104 bool ret = false;
1105 int index;
1106 uint64_t offset;
1107 struct boot_selection_info *in = NULL;
1108 char *buf = NULL;
1109
1110 index = partition_get_index("bootselect");
1111 if (index == INVALID_PTN)
1112 {
1113 dprintf(INFO, "Unable to locate /bootselect partition\n");
1114 return ret;
1115 }
1116 offset = partition_get_offset(index);
1117 if(!offset)
1118 {
1119 dprintf(INFO, "partition /bootselect doesn't exist\n");
1120 return ret;
1121 }
1122 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301123 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301124 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301125 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301126 {
1127 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1128 free(buf);
1129 return ret;
1130 }
1131 in = (struct boot_selection_info *) buf;
1132 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1133 (in->version == BOOTSELECT_VERSION)) {
1134 if ((in->state_info & BOOTSELECT_FORMAT) &&
1135 !(in->state_info & BOOTSELECT_FACTORY))
1136 ret = true;
1137 } else {
1138 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1139 in->signature, in->version);
1140 ASSERT(0);
1141 }
1142 free(buf);
1143 return ret;
1144}
1145
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001146void boot_verifier_init()
1147{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001148 uint32_t boot_state;
1149 /* Check if device unlock */
1150 if(device.is_unlocked)
1151 {
1152 boot_verify_send_event(DEV_UNLOCK);
1153 boot_verify_print_state();
1154 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1155 return;
1156 }
1157 else
1158 {
1159 boot_verify_send_event(BOOT_INIT);
1160 }
1161
1162 /* Initialize keystore */
1163 boot_state = boot_verify_keystore_init();
1164 if(boot_state == YELLOW)
1165 {
1166 boot_verify_print_state();
1167 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1168 }
1169}
1170
Shashank Mittal23b8f422010-04-16 19:27:21 -07001171int boot_linux_from_mmc(void)
1172{
1173 struct boot_img_hdr *hdr = (void*) buf;
1174 struct boot_img_hdr *uhdr;
1175 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001176 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001177 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001178 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001179
Shashank Mittalcd98d472011-08-02 14:29:24 -07001180 unsigned char *image_addr = 0;
1181 unsigned kernel_actual;
1182 unsigned ramdisk_actual;
1183 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001184 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001185
Matthew Qin271927e2015-03-31 22:07:07 -04001186 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001187 unsigned int out_len = 0;
1188 unsigned int out_avai_len = 0;
1189 unsigned char *out_addr = NULL;
1190 uint32_t dtb_offset = 0;
1191 unsigned char *kernel_start_addr = NULL;
1192 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001193 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001194 int rc;
Neeti Desai465491e2012-07-31 12:53:35 -07001195#if DEVICE_TREE
1196 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001197 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001198 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001199 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001200 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001201 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001202#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001203 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301204 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001205
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301206 if (check_format_bit())
1207 boot_into_recovery = 1;
1208
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001209 if (!boot_into_recovery) {
1210 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1211 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1212 if (rcode <= 0) {
1213 boot_into_ffbm = false;
1214 if (rcode < 0)
1215 dprintf(CRITICAL,"failed to get ffbm cookie");
1216 } else
1217 boot_into_ffbm = true;
1218 } else
1219 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001220 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1221 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1222 dprintf(INFO, "Unified boot method!\n");
1223 hdr = uhdr;
1224 goto unified_boot;
1225 }
Greg Griscod6250552011-06-29 14:40:23 -07001226 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001227 index = partition_get_index("boot");
1228 ptn = partition_get_offset(index);
1229 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001230 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1231 return -1;
1232 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001233 }
1234 else {
1235 index = partition_get_index("recovery");
1236 ptn = partition_get_offset(index);
1237 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001238 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1239 return -1;
1240 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001241 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001242 /* Set Lun for boot & recovery partitions */
1243 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001244
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301245 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001246 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1247 return -1;
1248 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001249
1250 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001251 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301252 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001253 }
1254
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001255 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301256
1257 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1258 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1259 return -1;
1260 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001261 page_size = hdr->page_size;
1262 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001263 }
1264
Matthew Qin49e51fa2015-02-09 10:40:45 +08001265 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1266 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001267
Matthew Qin49e51fa2015-02-09 10:40:45 +08001268 image_addr = (unsigned char *)target_get_scratch_address();
Kishor PK9e91b592017-05-24 12:14:55 +05301269 memcpy(image_addr, (void *)buf, page_size);
1270
1271 /* ensure commandline is terminated */
1272 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001273
1274#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301275#ifndef OSVERSION_IN_BOOTIMAGE
1276 dt_size = hdr->dt_size;
1277#endif
1278 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301279 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1280 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1281 return -1;
1282 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001283 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1284#else
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301285 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual + page_size)) {
1286 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1287 return -1;
1288 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001289 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1290#endif
1291
1292#if VERIFIED_BOOT
1293 boot_verifier_init();
1294#endif
1295
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301296 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001297 {
1298 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1299 return -1;
1300 }
1301
Matthew Qinbb7923d2015-02-09 10:56:09 +08001302 /*
1303 * Update loading flow of bootimage to support compressed/uncompressed
1304 * bootimage on both 64bit and 32bit platform.
1305 * 1. Load bootimage from emmc partition onto DDR.
1306 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1307 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1308 * platform accordingly.
1309 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1310 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301311 if (partition_multislot_is_supported())
1312 {
1313 current_active_slot = partition_find_active_slot();
1314 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1315 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1316 }
1317 else
1318 {
1319 dprintf(INFO, "Loading (%s) image (%d): start\n",
1320 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1321 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001322 bs_set_timestamp(BS_KERNEL_LOAD_START);
1323
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301324 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1325 {
1326 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1327 return -1;
1328 }
Kishor PK9e91b592017-05-24 12:14:55 +05301329 offset = page_size;
1330 /* Read image without signature and header*/
1331 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001332 {
1333 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1334 return -1;
1335 }
1336
Mayank Grover351a75e2017-05-30 20:06:08 +05301337 if (partition_multislot_is_supported())
1338 {
1339 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1340 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1341 }
1342 else
1343 {
1344 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001345 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1346
Mayank Grover351a75e2017-05-30 20:06:08 +05301347 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001348 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1349
1350 /* Authenticate Kernel */
1351 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1352 (int) target_use_signed_kernel(),
1353 device.is_unlocked,
1354 device.is_tampered);
1355
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001356 /* Change the condition a little bit to include the test framework support.
1357 * We would never reach this point if device is in fastboot mode, even if we did
1358 * that means we are in test mode, so execute kernel authentication part for the
1359 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301360 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001361 {
1362 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301363 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001364 {
1365 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1366 return -1;
1367 }
1368
1369 /* Read signature */
1370 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1371 {
1372 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1373 return -1;
1374 }
1375
1376 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001377 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301378 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001379 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001380 } else {
1381 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1382 #ifdef TZ_SAVE_KERNEL_HASH
1383 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1384 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001385
1386#ifdef MDTP_SUPPORT
1387 {
1388 /* Verify MDTP lock.
1389 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1390 * since verification was skipped in aboot. The signature is not part of the loaded image.
1391 */
1392 mdtp_ext_partition_verification_t ext_partition;
1393 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1394 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1395 ext_partition.page_size = page_size;
1396 ext_partition.image_addr = (uint32)image_addr;
1397 ext_partition.image_size = imagesize_actual;
1398 ext_partition.sig_avail = FALSE;
1399 mdtp_fwlock_verify_lock(&ext_partition);
1400 }
1401#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001402 }
1403
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001404#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001405 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001406 {
1407#if FBCON_DISPLAY_MSG
1408 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1409 wait_for_users_action();
1410#else
1411 dprintf(CRITICAL,
1412 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1413 mdelay(5000);
1414#endif
1415 }
1416#endif
1417
1418#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05301419#if !VBOOT_MOTA
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001420 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001421 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001422 ASSERT(0);
1423#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05301424#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001425 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001426 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1427 * If not, continue booting.
1428 */
1429 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1430 {
1431 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1432 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001433 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001434 rc = decompress((unsigned char *)(image_addr + page_size),
1435 hdr->kernel_size, out_addr, out_avai_len,
1436 &dtb_offset, &out_len);
1437 if (rc)
1438 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001439 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001440 ASSERT(0);
1441 }
1442
Matthew Qin0b15b322015-05-19 05:20:54 -04001443 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001444 kptr = (struct kernel64_hdr *)out_addr;
1445 kernel_start_addr = out_addr;
1446 kernel_size = out_len;
1447 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001448 dprintf(INFO, "Uncpmpressed kernel in use\n");
1449 if (!strncmp((char*)(image_addr + page_size),
1450 PATCHED_KERNEL_MAGIC,
1451 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1452 dprintf(INFO, "Patched kernel detected\n");
1453 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1454 PATCHED_KERNEL_HEADER_SIZE);
1455 //The size of the kernel is stored at start of kernel image + 16
1456 //The dtb would start just after the kernel
1457 dtb_offset = *((uint32_t*)((unsigned char*)
1458 (image_addr + page_size +
1459 sizeof(PATCHED_KERNEL_MAGIC) -
1460 1)));
1461 //The actual kernel starts after the 20 byte header.
1462 kernel_start_addr = (unsigned char*)(image_addr +
1463 page_size + PATCHED_KERNEL_HEADER_SIZE);
1464 kernel_size = hdr->kernel_size;
1465 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1466 } else {
1467 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1468 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1469 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1470 kernel_size = hdr->kernel_size;
1471 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001472 }
1473
1474 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001475 * Update the kernel/ramdisk/tags address if the boot image header
1476 * has default values, these default values come from mkbootimg when
1477 * the boot image is flashed using fastboot flash:raw
1478 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001479 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001480
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001481 /* Get virtual addresses since the hdr saves physical addresses. */
1482 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1483 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1484 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001485
Matthew Qinbb7923d2015-02-09 10:56:09 +08001486 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001487 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001488 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001489 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1490 {
1491 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1492 return -1;
1493 }
1494
1495#ifndef DEVICE_TREE
1496 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1497 {
1498 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1499 return -1;
1500 }
1501#endif
1502
Matthew Qin49e51fa2015-02-09 10:40:45 +08001503 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001504 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001505 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001506
Matthew Qin49e51fa2015-02-09 10:40:45 +08001507 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301508 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001509 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1510 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001511
Matthew Qin49e51fa2015-02-09 10:40:45 +08001512 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1513 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1514 return -1;
1515 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001516
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301517 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1518 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301519 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301520 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1521 return -1;
1522 }
1523
Matthew Qin49e51fa2015-02-09 10:40:45 +08001524 /* Find index of device tree within device tree table */
1525 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1526 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1527 return -1;
1528 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001529
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301530 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1531 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1532 return -1;
1533 }
1534
1535 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301536 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301537 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1538 return -1;
1539 }
1540
Matthew Qin271927e2015-03-31 22:07:07 -04001541 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1542 {
1543 unsigned int compressed_size = 0;
1544 out_addr += out_len;
1545 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001546 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001547 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1548 dt_entry.size, out_addr, out_avai_len,
1549 &compressed_size, &dtb_size);
1550 if (rc)
1551 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001552 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001553 ASSERT(0);
1554 }
1555
Matthew Qin0b15b322015-05-19 05:20:54 -04001556 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001557 best_match_dt_addr = out_addr;
1558 } else {
1559 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1560 dtb_size = dt_entry.size;
1561 }
1562
Matthew Qin49e51fa2015-02-09 10:40:45 +08001563 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001564 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001565 {
1566 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1567 return -1;
1568 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001569
Matthew Qin271927e2015-03-31 22:07:07 -04001570 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001571 } else {
1572 /* Validate the tags_addr */
1573 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001574 {
1575 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1576 return -1;
1577 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001578 /*
1579 * If appended dev tree is found, update the atags with
1580 * memory address to the DTB appended location on RAM.
1581 * Else update with the atags address in the kernel header
1582 */
1583 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001584 dtb = dev_tree_appended(
1585 (void*)(image_addr + page_size +
1586 patched_kernel_hdr_size),
1587 hdr->kernel_size, dtb_offset,
1588 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001589 if (!dtb) {
1590 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001591 return -1;
1592 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001593 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001594 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001595
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001596 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1597 target_load_ssd_keystore();
1598
Shashank Mittal23b8f422010-04-16 19:27:21 -07001599unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001600
Dima Zavin77e41f32013-03-06 16:10:43 -08001601 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001602 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001603 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1604
1605 return 0;
1606}
1607
Dima Zavin214cc642009-01-26 11:16:21 -08001608int boot_linux_from_flash(void)
1609{
1610 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001611 struct ptentry *ptn;
1612 struct ptable *ptable;
1613 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001614
Shashank Mittalcd98d472011-08-02 14:29:24 -07001615 unsigned char *image_addr = 0;
1616 unsigned kernel_actual;
1617 unsigned ramdisk_actual;
1618 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301619 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001620
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001621#if DEVICE_TREE
1622 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001623 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301624 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001625 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001626 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301627 unsigned int dtb_size = 0;
1628 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001629#endif
1630
David Ng183a7422009-12-07 14:55:21 -08001631 if (target_is_emmc_boot()) {
1632 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1633 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1634 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1635 return -1;
1636 }
1637 goto continue_boot;
1638 }
1639
Dima Zavin214cc642009-01-26 11:16:21 -08001640 ptable = flash_get_ptable();
1641 if (ptable == NULL) {
1642 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1643 return -1;
1644 }
1645
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001646 if(!boot_into_recovery)
1647 {
1648 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001649
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001650 if (ptn == NULL) {
1651 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1652 return -1;
1653 }
1654 }
1655 else
1656 {
1657 ptn = ptable_find(ptable, "recovery");
1658 if (ptn == NULL) {
1659 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1660 return -1;
1661 }
Dima Zavin214cc642009-01-26 11:16:21 -08001662 }
1663
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001664 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001665 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1666 return -1;
1667 }
Dima Zavin214cc642009-01-26 11:16:21 -08001668
1669 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001670 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001671 return -1;
1672 }
1673
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001674 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001675 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 -08001676 return -1;
1677 }
1678
Kishor PK9e91b592017-05-24 12:14:55 +05301679 image_addr = (unsigned char *)target_get_scratch_address();
1680 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05301681
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001682 /*
1683 * Update the kernel/ramdisk/tags address if the boot image header
1684 * has default values, these default values come from mkbootimg when
1685 * the boot image is flashed using fastboot flash:raw
1686 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001687 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001688
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001689 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001690 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1691 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1692 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1693
1694 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1695 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1696
Kishor PK9e91b592017-05-24 12:14:55 +05301697 /* ensure commandline is terminated */
1698 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1699
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001700 /* Check if the addresses in the header are valid. */
1701 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1702 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1703 {
1704 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1705 return -1;
1706 }
1707
1708#ifndef DEVICE_TREE
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301709 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1710 {
1711 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1712 return -1;
1713 }
1714#else
1715
1716#ifndef OSVERSION_IN_BOOTIMAGE
1717 dt_size = hdr->dt_size;
1718#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001719#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001720
Shashank Mittalcd98d472011-08-02 14:29:24 -07001721 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001722 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001723 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001724
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001725#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301726 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
vijay kumar966a9822015-12-09 18:36:09 +05301727 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1728 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1729 return -1;
1730 }
1731
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001732 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001733
Parth Dixit4097b622016-03-15 14:42:27 +05301734 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001735 {
1736 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1737 return -1;
1738 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001739#else
vijay kumar966a9822015-12-09 18:36:09 +05301740 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) {
1741 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1742 return -1;
1743 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001744 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001745#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001746
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001747 dprintf(INFO, "Loading (%s) image (%d): start\n",
1748 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001749 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001750
Vijay Kumar Pendotib3458362016-07-05 13:45:54 +05301751 if (UINT_MAX - page_size < imagesize_actual)
1752 {
1753 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
1754 return -1;
1755 }
1756
1757 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1758 if (target_get_max_flash_size() < (imagesize_actual + page_size))
1759 {
1760 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
1761 return -1;
1762 }
Kishor PK9e91b592017-05-24 12:14:55 +05301763 offset = page_size;
1764 /* Read image without signature and header*/
1765 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001766 {
1767 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1768 return -1;
1769 }
Dima Zavin214cc642009-01-26 11:16:21 -08001770
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001771 dprintf(INFO, "Loading (%s) image (%d): done\n",
1772 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001773 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001774
Shashank Mittalcd98d472011-08-02 14:29:24 -07001775 offset = imagesize_actual;
1776 /* Read signature */
1777 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1778 {
1779 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001780 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001781 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001782
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301783 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001784
1785 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001786 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1787 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001788#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301789 if(dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001790
vijay kumar8f53e362015-11-24 13:38:11 +05301791 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1792
1793 table = (struct dt_table*) dt_table_offset;
1794
1795 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1796 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1797 return -1;
1798 }
1799
1800 /* Find index of device tree within device tree table */
1801 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1802 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1803 return -1;
1804 }
1805
1806 /* Validate and Read device device tree in the "tags_add */
1807 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){
1808 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1809 return -1;
1810 }
1811
Mayank Groverebf85ae2017-01-25 13:35:37 +05301812 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1813 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1814 return -1;
1815 }
1816
1817 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1818 if ((dt_entry.offset + dt_entry.size) > dt_size) {
1819 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1820 return -1;
1821 }
1822
vijay kumar8f53e362015-11-24 13:38:11 +05301823 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1824 dtb_size = dt_entry.size;
1825 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1826 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001827#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001828
1829 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001830 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001831 {
1832 write_device_info_flash(&device);
1833 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301834#if USE_PCOM_SECBOOT
1835 set_tamper_flag(device.is_tampered);
1836#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001837 }
1838 else
1839 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001840 offset = page_size;
1841
Amol Jadib6be5c12012-11-14 13:39:51 -08001842 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1843 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1844 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1845
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001846 dprintf(INFO, "Loading (%s) image (%d): start\n",
1847 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1848
Amol Jadi492d5a52013-03-15 16:12:34 -07001849 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001850
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301851 if (UINT_MAX - offset < kernel_actual)
1852 {
1853 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1854 return -1;
1855 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001856 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001857 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1858 return -1;
1859 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001860 offset += kernel_actual;
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301861 if (UINT_MAX - offset < ramdisk_actual)
1862 {
1863 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1864 return -1;
1865 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001866 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001867 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1868 return -1;
1869 }
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301870
Amol Jadib6be5c12012-11-14 13:39:51 -08001871 offset += ramdisk_actual;
1872
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001873 dprintf(INFO, "Loading (%s) image (%d): done\n",
1874 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1875
Amol Jadi492d5a52013-03-15 16:12:34 -07001876 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001877
1878 if(hdr->second_size != 0) {
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301879 if (UINT_MAX - offset < second_actual)
1880 {
1881 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1882 return -1;
1883 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001884 offset += second_actual;
1885 /* Second image loading not implemented. */
1886 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001887 }
1888
1889#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301890 if(dt_size != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001891
1892 /* Read the device tree table into buffer */
1893 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1894 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1895 return -1;
1896 }
1897
1898 table = (struct dt_table*) dt_buf;
1899
Deepa Dinamani19648b42013-09-05 17:05:55 -07001900 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001901 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1902 return -1;
1903 }
1904
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301905 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1906 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301907 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301908 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1909 return -1;
1910 }
1911
Deepa Dinamani19648b42013-09-05 17:05:55 -07001912 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1913 if (!table)
1914 return -1;
1915
1916 /* Read the entire device tree table into buffer */
1917 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1918 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1919 return -1;
1920 }
1921
1922
Joel Kingaa335dc2013-06-03 16:11:08 -07001923 /* Find index of device tree within device tree table */
1924 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001925 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1926 return -1;
1927 }
1928
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001929 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001930 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001931 {
1932 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1933 return -1;
1934 }
1935
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001936 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001937 if(flash_read(ptn, offset + dt_entry.offset,
1938 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001939 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1940 return -1;
1941 }
1942 }
1943#endif
1944
Shashank Mittalcd98d472011-08-02 14:29:24 -07001945 }
David Ng183a7422009-12-07 14:55:21 -08001946continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001947
Dima Zavin214cc642009-01-26 11:16:21 -08001948 /* TODO: create/pass atags to kernel */
1949
Ajay Dudanie28a6072011-07-01 13:59:46 -07001950 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001951 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001952 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1953
1954 return 0;
1955}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001956
Shashank Mittal162244e2011-08-08 19:01:25 -07001957void write_device_info_mmc(device_info *dev)
1958{
Shashank Mittal162244e2011-08-08 19:01:25 -07001959 unsigned long long ptn = 0;
1960 unsigned long long size;
1961 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001962 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001963 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001964 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001965
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001966 if (devinfo_present)
1967 index = partition_get_index("devinfo");
1968 else
1969 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001970
Shashank Mittal162244e2011-08-08 19:01:25 -07001971 ptn = partition_get_offset(index);
1972 if(ptn == 0)
1973 {
1974 return;
1975 }
1976
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001977 lun = partition_get_lun(index);
1978 mmc_set_lun(lun);
1979
Shashank Mittal162244e2011-08-08 19:01:25 -07001980 size = partition_get_size(index);
1981
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001982 blocksize = mmc_get_device_blocksize();
1983
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001984 if (devinfo_present)
1985 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1986 else
1987 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1988 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001989 {
1990 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001991 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001992 }
1993}
1994
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001995void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001996{
Shashank Mittal162244e2011-08-08 19:01:25 -07001997 unsigned long long ptn = 0;
1998 unsigned long long size;
1999 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002000 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002001 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002002
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002003 if ((index = partition_get_index("devinfo")) < 0)
2004 {
2005 devinfo_present = false;
2006 index = partition_get_index("aboot");
2007 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002008
Shashank Mittal162244e2011-08-08 19:01:25 -07002009 ptn = partition_get_offset(index);
2010 if(ptn == 0)
2011 {
2012 return;
2013 }
2014
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002015 mmc_set_lun(partition_get_lun(index));
2016
Shashank Mittal162244e2011-08-08 19:01:25 -07002017 size = partition_get_size(index);
2018
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002019 blocksize = mmc_get_device_blocksize();
2020
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002021 if (devinfo_present)
2022 ret = mmc_read(ptn, (void *)info_buf, blocksize);
2023 else
2024 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
2025 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002026 {
2027 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002028 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002029 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002030}
2031
2032void write_device_info_flash(device_info *dev)
2033{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002034 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002035 struct ptentry *ptn;
2036 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002037 if(info == NULL)
2038 {
2039 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2040 ASSERT(0);
2041 }
2042 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002043 ptable = flash_get_ptable();
2044 if (ptable == NULL)
2045 {
2046 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2047 return;
2048 }
2049
2050 ptn = ptable_find(ptable, "devinfo");
2051 if (ptn == NULL)
2052 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002053 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002054 return;
2055 }
2056
2057 memcpy(info, dev, sizeof(device_info));
2058
2059 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2060 {
2061 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2062 return;
2063 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002064 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002065}
2066
vijay kumarc65876c2015-04-24 13:29:16 +05302067static int read_allow_oem_unlock(device_info *dev)
2068{
vijay kumarc65876c2015-04-24 13:29:16 +05302069 unsigned offset;
2070 int index;
2071 unsigned long long ptn;
2072 unsigned long long ptn_size;
2073 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002074 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302075
vijay kumarca2e6812015-07-08 20:28:25 +05302076 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302077 if (index == INVALID_PTN)
2078 {
vijay kumarca2e6812015-07-08 20:28:25 +05302079 index = partition_get_index(frp_ptns[1]);
2080 if (index == INVALID_PTN)
2081 {
2082 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2083 return -1;
2084 }
vijay kumarc65876c2015-04-24 13:29:16 +05302085 }
2086
2087 ptn = partition_get_offset(index);
2088 ptn_size = partition_get_size(index);
2089 offset = ptn_size - blocksize;
2090
Mayank Grover48860402016-11-29 12:34:53 +05302091 /* Set Lun for partition */
2092 mmc_set_lun(partition_get_lun(index));
2093
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002094 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302095 {
2096 dprintf(CRITICAL, "Reading MMC failed\n");
2097 return -1;
2098 }
2099
2100 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2101 is_allow_unlock = buf[blocksize-1] & 0x01;
2102 return 0;
2103}
2104
2105static int write_allow_oem_unlock(bool allow_unlock)
2106{
vijay kumarc65876c2015-04-24 13:29:16 +05302107 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302108 int index;
2109 unsigned long long ptn;
2110 unsigned long long ptn_size;
2111 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002112 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302113
vijay kumarca2e6812015-07-08 20:28:25 +05302114 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302115 if (index == INVALID_PTN)
2116 {
vijay kumarca2e6812015-07-08 20:28:25 +05302117 index = partition_get_index(frp_ptns[1]);
2118 if (index == INVALID_PTN)
2119 {
2120 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2121 return -1;
2122 }
vijay kumarc65876c2015-04-24 13:29:16 +05302123 }
2124
2125 ptn = partition_get_offset(index);
2126 ptn_size = partition_get_size(index);
2127 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302128 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302129
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002130 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302131 {
2132 dprintf(CRITICAL, "Reading MMC failed\n");
2133 return -1;
2134 }
2135
2136 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2137 buf[blocksize-1] = allow_unlock;
2138 if (mmc_write(ptn + offset, blocksize, buf))
2139 {
2140 dprintf(CRITICAL, "Writing MMC failed\n");
2141 return -1;
2142 }
2143
2144 return 0;
2145}
2146
Shashank Mittal162244e2011-08-08 19:01:25 -07002147void read_device_info_flash(device_info *dev)
2148{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002149 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002150 struct ptentry *ptn;
2151 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002152 if(info == NULL)
2153 {
2154 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2155 ASSERT(0);
2156 }
2157 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002158 ptable = flash_get_ptable();
2159 if (ptable == NULL)
2160 {
2161 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2162 return;
2163 }
2164
2165 ptn = ptable_find(ptable, "devinfo");
2166 if (ptn == NULL)
2167 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002168 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002169 return;
2170 }
2171
2172 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2173 {
2174 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2175 return;
2176 }
2177
2178 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2179 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002180 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2181 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002182 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002183 write_device_info_flash(info);
2184 }
2185 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002186 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002187}
2188
2189void write_device_info(device_info *dev)
2190{
2191 if(target_is_emmc_boot())
2192 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002193 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2194 if(info == NULL)
2195 {
2196 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2197 ASSERT(0);
2198 }
2199 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002200 memcpy(info, dev, sizeof(struct device_info));
2201
2202#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002203 if (is_secure_boot_enable()) {
2204 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2205 ASSERT(0);
2206 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002207 else
2208 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002209#else
2210 write_device_info_mmc(info);
2211#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002212 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002213 }
2214 else
2215 {
2216 write_device_info_flash(dev);
2217 }
2218}
2219
2220void read_device_info(device_info *dev)
2221{
2222 if(target_is_emmc_boot())
2223 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002224 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2225 if(info == NULL)
2226 {
2227 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2228 ASSERT(0);
2229 }
2230 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002231
2232#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002233 if (is_secure_boot_enable()) {
2234 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2235 ASSERT(0);
2236 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002237 else
2238 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002239#else
2240 read_device_info_mmc(info);
2241#endif
2242
2243 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2244 {
2245 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002246 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002247 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302248#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002249 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302250#endif
lijuang511a2b52015-08-14 20:50:51 +08002251 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002252 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302253#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002254 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302255#endif
lijuang511a2b52015-08-14 20:50:51 +08002256 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002257 info->is_tampered = 0;
2258 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302259#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07002260 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302261#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002262 write_device_info(info);
2263 }
2264 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002265 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002266 }
2267 else
2268 {
2269 read_device_info_flash(dev);
2270 }
2271}
2272
2273void reset_device_info()
2274{
2275 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002276 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002277 write_device_info(&device);
2278}
2279
2280void set_device_root()
2281{
2282 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002283 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002284 write_device_info(&device);
2285}
2286
lijuang4ece1e72015-08-14 21:02:36 +08002287/* set device unlock value
2288 * Must check FRP before call this function
2289 * Need to wipe data when unlock status changed
2290 * type 0: oem unlock
2291 * type 1: unlock critical
2292 * status 0: unlock as false
2293 * status 1: lock as true
2294 */
2295void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002296{
lijuang4ece1e72015-08-14 21:02:36 +08002297 if (type == UNLOCK)
2298 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302299#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002300 else if (type == UNLOCK_CRITICAL)
2301 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302302#endif
lijuang4ece1e72015-08-14 21:02:36 +08002303 write_device_info(&device);
2304}
2305
2306static void set_device_unlock(int type, bool status)
2307{
2308 int is_unlocked = -1;
2309 char response[MAX_RSP_SIZE];
2310
2311 /* check device unlock status if it is as expected */
2312 if (type == UNLOCK)
2313 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05302314#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002315 else if (type == UNLOCK_CRITICAL)
2316 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05302317#endif
lijuang4ece1e72015-08-14 21:02:36 +08002318 if (is_unlocked == status) {
2319 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2320 fastboot_info(response);
2321 fastboot_okay("");
2322 return;
2323 }
2324
2325 /* status is true, it means to unlock device */
2326 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002327 if(!is_allow_unlock) {
2328 fastboot_fail("oem unlock is not allowed");
2329 return;
2330 }
2331
lijuang4ece1e72015-08-14 21:02:36 +08002332#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002333 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002334 fastboot_okay("");
2335 return;
2336#else
2337 if (type == UNLOCK) {
2338 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2339 return;
2340 }
2341#endif
lijuang21f12f52015-08-22 16:22:19 +08002342 }
lijuang4ece1e72015-08-14 21:02:36 +08002343
2344 set_device_unlock_value(type, status);
2345
2346 /* wipe data */
2347 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302348 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002349 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2350 write_misc(0, &msg, sizeof(msg));
2351
2352 fastboot_okay("");
2353 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002354}
2355
lijuang511a2b52015-08-14 20:50:51 +08002356static bool critical_flash_allowed(const char * entry)
2357{
2358 uint32_t i = 0;
2359 if (entry == NULL)
2360 return false;
2361
2362 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2363 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2364 return true;
2365 }
2366 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002367}
2368
2369#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002370int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2371{
2372 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002373 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002374 struct dt_table *table;
2375 struct dt_entry dt_entry;
2376 uint32_t dt_hdr_size;
2377 unsigned int compressed_size = 0;
2378 unsigned int dtb_size = 0;
2379 unsigned int out_avai_len = 0;
2380 unsigned char *out_addr = NULL;
2381 unsigned char *best_match_dt_addr = NULL;
2382 int rc;
2383
2384 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2385
Parth Dixit4097b622016-03-15 14:42:27 +05302386#ifndef OSVERSION_IN_BOOTIMAGE
2387 dt_size = hdr->dt_size;
2388#endif
2389
2390 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002391 /* add kernel offset */
2392 dt_image_offset += page_size;
2393 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2394 dt_image_offset += n;
2395
2396 /* add ramdisk offset */
2397 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2398 dt_image_offset += n;
2399
2400 /* add second offset */
2401 if(hdr->second_size != 0) {
2402 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2403 dt_image_offset += n;
2404 }
2405
2406 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002407 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002408
Deepa Dinamani19648b42013-09-05 17:05:55 -07002409 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002410 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2411 return -1;
2412 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302413
2414 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2415 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302416 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302417 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2418 return -1;
2419 }
2420
Joel Kingaa335dc2013-06-03 16:11:08 -07002421 /* Find index of device tree within device tree table */
2422 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002423 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2424 return -1;
2425 }
2426
Matthew Qin271927e2015-03-31 22:07:07 -04002427 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2428 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2429 {
2430 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2431 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002432 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002433 rc = decompress(best_match_dt_addr,
2434 dt_entry.size, out_addr, out_avai_len,
2435 &compressed_size, &dtb_size);
2436 if (rc)
2437 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002438 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002439 ASSERT(0);
2440 }
2441
Matthew Qin0b15b322015-05-19 05:20:54 -04002442 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002443 best_match_dt_addr = out_addr;
2444 } else {
2445 dtb_size = dt_entry.size;
2446 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002447 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002448 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002449 {
2450 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
2451 return -1;
2452 }
2453
Amol Jadicb524072012-08-09 16:40:18 -07002454 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002455 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002456 } else
2457 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002458
2459 /* Everything looks fine. Return success. */
2460 return 0;
2461}
2462#endif
2463
Brian Swetland9c4c0752009-01-25 16:23:50 -08002464void cmd_boot(const char *arg, void *data, unsigned sz)
2465{
Amit Blay8a510302015-08-17 09:20:01 +03002466#ifdef MDTP_SUPPORT
2467 static bool is_mdtp_activated = 0;
2468#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002469 unsigned kernel_actual;
2470 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002471 uint32_t image_actual;
2472 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302473 uint32_t sig_actual = 0;
Kishor PK5134b332017-05-09 17:50:08 +05302474 uint32_t sig_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002475 struct boot_img_hdr *hdr = NULL;
2476 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002477 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002478 int ret = 0;
2479 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002480 unsigned int out_len = 0;
2481 unsigned int out_avai_len = 0;
2482 unsigned char *out_addr = NULL;
2483 uint32_t dtb_offset = 0;
2484 unsigned char *kernel_start_addr = NULL;
2485 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002486 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002487
lijuang2008ff22016-03-07 17:56:27 +08002488#if FBCON_DISPLAY_MSG
2489 /* Exit keys' detection thread firstly */
2490 exit_menu_keys_detection();
2491#endif
2492
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002493#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002494 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002495 {
2496 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002497 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002498 }
2499#endif
2500
Brian Swetland9c4c0752009-01-25 16:23:50 -08002501 if (sz < sizeof(hdr)) {
2502 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002503 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002504 }
2505
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002506 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002507
2508 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002509 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002510
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002511 if(target_is_emmc_boot() && hdr->page_size) {
2512 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002513 page_mask = page_size - 1;
2514 }
2515
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002516 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2517 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002518#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302519#ifndef OSVERSION_IN_BOOTIMAGE
2520 dt_size = hdr->dt_size;
2521#endif
2522 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002523#endif
2524
2525 image_actual = ADD_OF(page_size, kernel_actual);
2526 image_actual = ADD_OF(image_actual, ramdisk_actual);
2527 image_actual = ADD_OF(image_actual, dt_actual);
2528
Kishor PK5134b332017-05-09 17:50:08 +05302529 /* Checking to prevent oob access in read_der_message_length */
2530 if (image_actual > sz) {
2531 fastboot_fail("bootimage header fields are invalid");
2532 goto boot_failed;
2533 }
2534 sig_size = sz - image_actual;
2535
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302536 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2537 /* Calculate the signature length from boot image */
2538 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05302539 (unsigned char*)(data + image_actual), sig_size);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002540 image_actual = ADD_OF(image_actual, sig_actual);
2541
Kishor PK5134b332017-05-09 17:50:08 +05302542 if (image_actual > sz) {
2543 fastboot_fail("bootimage header fields are invalid");
2544 goto boot_failed;
2545 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002546 }
2547
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002548 // Initialize boot state before trying to verify boot.img
2549#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002550 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302551#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002552 /* Handle overflow if the input image size is greater than
2553 * boot image buffer can hold
2554 */
2555 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2556 {
2557 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002558 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002559 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002560
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002561 /* Verify the boot image
2562 * device & page_size are initialized in aboot_init
2563 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002564 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002565 /* Pass size excluding signature size, otherwise we would try to
2566 * access signature beyond its length
2567 */
2568 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002569 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002570#ifdef MDTP_SUPPORT
2571 else
2572 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002573 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002574 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002575
2576 if (!is_mdtp_activated) {
2577 ext_partition.partition = MDTP_PARTITION_NONE;
2578 mdtp_fwlock_verify_lock(&ext_partition);
2579 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002580 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002581
Amir Kotzer7c768c02016-04-13 09:08:05 +03002582 /* If mdtp state cannot be validate, block fastboot boot*/
2583 if(mdtp_activated(&is_mdtp_activated)){
2584 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2585 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2586 goto boot_failed;
2587 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002588 if(is_mdtp_activated){
2589 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002590 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002591 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002592#endif /* MDTP_SUPPORT */
2593
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002594#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302595#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002596 // send root of trust
2597 if(!send_rot_command((uint32_t)device.is_unlocked))
2598 ASSERT(0);
2599#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302600#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002601 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002602 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2603 * If not, continue booting.
2604 */
2605 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2606 {
2607 out_addr = (unsigned char *)target_get_scratch_address();
2608 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2609 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002610 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002611 ret = decompress((unsigned char *)(ptr + page_size),
2612 hdr->kernel_size, out_addr, out_avai_len,
2613 &dtb_offset, &out_len);
2614 if (ret)
2615 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002616 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002617 ASSERT(0);
2618 }
2619
Matthew Qin0b15b322015-05-19 05:20:54 -04002620 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002621 kptr = (struct kernel64_hdr *)out_addr;
2622 kernel_start_addr = out_addr;
2623 kernel_size = out_len;
2624 } else {
2625 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2626 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2627 kernel_size = hdr->kernel_size;
2628 }
2629
2630 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002631 * Update the kernel/ramdisk/tags address if the boot image header
2632 * has default values, these default values come from mkbootimg when
2633 * the boot image is flashed using fastboot flash:raw
2634 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002635 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002636
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002637 /* Get virtual addresses since the hdr saves physical addresses. */
2638 hdr->kernel_addr = VA(hdr->kernel_addr);
2639 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2640 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002641
Matthew Qinbb7923d2015-02-09 10:56:09 +08002642 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002643 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002644 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002645 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2646 {
2647 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002648 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002649 }
2650
Amol Jadicb524072012-08-09 16:40:18 -07002651#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002652 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002653 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002654 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002655
2656 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002657#else
2658 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2659 {
2660 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002661 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002662 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002663#endif
2664
2665 /* Load ramdisk & kernel */
2666 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002667 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002668
2669#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002670 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2671 {
2672 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002673 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002674 }
2675
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002676 /*
2677 * If dtb is not found look for appended DTB in the kernel.
2678 * If appended dev tree is found, update the atags with
2679 * memory address to the DTB appended location on RAM.
2680 * Else update with the atags address in the kernel header
2681 */
2682 if (!dtb_copied) {
2683 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002684 dtb = dev_tree_appended((void*)(ptr + page_size),
2685 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002686 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002687 if (!dtb) {
2688 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002689 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002690 }
Amol Jadicb524072012-08-09 16:40:18 -07002691 }
2692#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002693
2694 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002695 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002696
Dima Zavin77e41f32013-03-06 16:10:43 -08002697 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002698 (const char*) hdr->cmdline, board_machtype(),
2699 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002700
2701 /* fastboot already stop, it's no need to show fastboot menu */
2702 return;
2703boot_failed:
2704#if FBCON_DISPLAY_MSG
2705 /* revert to fastboot menu if boot failed */
2706 display_fastboot_menu();
2707#endif
2708 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002709}
2710
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002711void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002712{
2713 struct ptentry *ptn;
2714 struct ptable *ptable;
2715
2716 ptable = flash_get_ptable();
2717 if (ptable == NULL) {
2718 fastboot_fail("partition table doesn't exist");
2719 return;
2720 }
2721
2722 ptn = ptable_find(ptable, arg);
2723 if (ptn == NULL) {
2724 fastboot_fail("unknown partition name");
2725 return;
2726 }
2727
2728 if (flash_erase(ptn)) {
2729 fastboot_fail("failed to erase partition");
2730 return;
2731 }
2732 fastboot_okay("");
2733}
2734
Bikas Gurungd48bd242010-09-04 19:54:32 -07002735
2736void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2737{
2738 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002739 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002740 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002741 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302742 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002743
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002744#if VERIFIED_BOOT
2745 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2746 {
2747 if(!device.is_unlocked)
2748 {
2749 fastboot_fail("unlock device to erase keystore");
2750 return;
2751 }
2752 }
2753#endif
2754
Kinson Chikf1a43512011-07-14 11:28:39 -07002755 index = partition_get_index(arg);
2756 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002757 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002758
Kinson Chikf1a43512011-07-14 11:28:39 -07002759 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002760 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002761 return;
2762 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002763
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002764 lun = partition_get_lun(index);
2765 mmc_set_lun(lun);
2766
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002767 if (platform_boot_dev_isemmc())
2768 {
2769 if (mmc_erase_card(ptn, size)) {
2770 fastboot_fail("failed to erase partition\n");
2771 return;
2772 }
2773 } else {
2774 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2775 size = partition_get_size(index);
2776 if (size > DEFAULT_ERASE_SIZE)
2777 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002778
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002779 /* Simple inefficient version of erase. Just writing
2780 0 in first several blocks */
2781 if (mmc_write(ptn , size, (unsigned int *)out)) {
2782 fastboot_fail("failed to erase partition");
2783 return;
2784 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302785 /*Erase FDE metadata at the userdata footer*/
2786 if(!(strncmp(arg, "userdata", 8)))
2787 {
2788 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2789 memset((void *)footer, 0, FOOTER_SIZE);
2790
2791 size = partition_get_size(index);
2792
2793 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2794 fastboot_fail("failed to erase userdata footer");
2795 free(footer);
2796 return;
2797 }
2798 free(footer);
2799 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002800 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002801#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302802#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002803 if(!(strncmp(arg, "userdata", 8)))
2804 if(send_delete_keys_to_tz())
2805 ASSERT(0);
2806#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302807#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002808 fastboot_okay("");
2809}
2810
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002811void cmd_erase(const char *arg, void *data, unsigned sz)
2812{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002813#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002814 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002815 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002816 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002817 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002818 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002819 return;
2820 }
2821 }
2822#endif
2823
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002824 if(target_is_emmc_boot())
2825 cmd_erase_mmc(arg, data, sz);
2826 else
2827 cmd_erase_nand(arg, data, sz);
2828}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002829
Ajay Dudani5c761132011-04-07 20:19:04 -07002830void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002831{
2832 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002833 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002834 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002835 char *token = NULL;
2836 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002837 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002838 uint8_t lun = 0;
2839 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05302840 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002841
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002842 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002843 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002844 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002845 if(token)
2846 {
2847 lun = atoi(token);
2848 mmc_set_lun(lun);
2849 lun_set = true;
2850 }
2851
Mao Jinlong226f33a2014-07-04 17:24:10 +08002852 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002853 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002854 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2855 {
2856 if (!aboot_frp_unlock(pname, data, sz))
2857 {
2858 fastboot_info("FRP unlock successful");
2859 fastboot_okay("");
2860 }
2861 else
2862 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2863
2864 return;
2865 }
2866
Mao Jinlong226f33a2014-07-04 17:24:10 +08002867 if (!strcmp(pname, "partition"))
2868 {
2869 dprintf(INFO, "Attempt to write partition image.\n");
2870 if (write_partition(sz, (unsigned char *) data)) {
2871 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002872 return;
2873 }
Mayank Grover351a75e2017-05-30 20:06:08 +05302874
2875 /* Rescan partition table to ensure we have multislot support*/
2876 if (partition_scan_for_multislot())
2877 {
2878 current_active_slot = partition_find_active_slot();
2879 dprintf(INFO, "Multislot supported: Slot %s active",
2880 (SUFFIX_SLOT(current_active_slot)));
2881 }
2882 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07002883 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002884 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002885 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002886#if VERIFIED_BOOT
2887 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2888 {
2889 if(!device.is_unlocked)
2890 {
2891 fastboot_fail("unlock device to flash keystore");
2892 return;
2893 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302894 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002895 {
2896 fastboot_fail("image is not a keystore file");
2897 return;
2898 }
2899 }
2900#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002901 index = partition_get_index(pname);
2902 ptn = partition_get_offset(index);
2903 if(ptn == 0) {
2904 fastboot_fail("partition table doesn't exist");
2905 return;
2906 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002907
Mayank Grover351a75e2017-05-30 20:06:08 +05302908 if (!strncmp(pname, "boot", strlen("boot"))
2909 || !strcmp(pname, "recovery"))
2910 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002911 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2912 fastboot_fail("image is not a boot image");
2913 return;
2914 }
Mayank Grover351a75e2017-05-30 20:06:08 +05302915
2916 /* Reset multislot_partition attributes in case of flashing boot */
2917 if (partition_multislot_is_supported())
2918 {
2919 partition_reset_attributes(index);
2920 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002921 }
2922
2923 if(!lun_set)
2924 {
2925 lun = partition_get_lun(index);
2926 mmc_set_lun(lun);
2927 }
2928
2929 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05302930 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002931 fastboot_fail("size too large");
2932 return;
2933 }
2934 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2935 fastboot_fail("flash write failure");
2936 return;
2937 }
Greg Grisco6e754772011-06-23 12:19:39 -07002938 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002939 }
2940 fastboot_okay("");
2941 return;
2942}
2943
Ajay Dudanide984792015-03-02 09:57:41 -08002944void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2945{
2946 int i, images;
2947 meta_header_t *meta_header;
2948 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302949 /*End of the image address*/
2950 uintptr_t data_end;
2951
2952 if( (UINT_MAX - sz) > (uintptr_t)data )
2953 data_end = (uintptr_t)data + sz;
2954 else
2955 {
2956 fastboot_fail("Cannot flash: image header corrupt");
2957 return;
2958 }
2959
2960 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
2961 {
2962 fastboot_fail("Cannot flash: image header corrupt");
2963 return;
2964 }
Ajay Dudanide984792015-03-02 09:57:41 -08002965
lijuang8ee21882016-04-19 16:57:11 +08002966 /* If device is locked:
2967 * Forbid to flash image to avoid the device to bypass the image
2968 * which with "any" name other than bootloader. Because it maybe
2969 * a meta package of all partitions.
2970 */
2971#if VERIFIED_BOOT
2972 if (target_build_variant_user()) {
2973 if (!device.is_unlocked) {
2974 fastboot_fail("Device is locked, meta image flashing is not allowed");
2975 return;
2976 }
2977#if !VBOOT_MOTA
2978 if(!device.is_unlock_critical) {
2979 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
2980 return;
2981 }
2982#endif
2983 }
2984#endif
2985
Ajay Dudanide984792015-03-02 09:57:41 -08002986 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302987 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
2988 {
2989 fastboot_fail("Cannot flash: image header corrupt");
2990 return;
2991 }
Ajay Dudanide984792015-03-02 09:57:41 -08002992 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2993
2994 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2995
2996 for (i=0; i<images; i++) {
2997
2998 if((img_header_entry[i].ptn_name == NULL) ||
2999 (img_header_entry[i].start_offset == 0) ||
3000 (img_header_entry[i].size == 0))
3001 break;
Kishor PK49831502017-04-21 17:55:43 +05303002 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3003 fastboot_fail("Integer overflow detected in start_offset of img");
3004 break;
3005 }
3006 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3007 fastboot_fail("Integer overflow detected in size of img");
3008 break;
3009 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303010 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3011 + img_header_entry[i].size) )
3012 {
3013 fastboot_fail("Cannot flash: image size mismatch");
3014 break;
3015 }
3016
Ajay Dudanide984792015-03-02 09:57:41 -08003017 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3018 (void *) data + img_header_entry[i].start_offset,
3019 img_header_entry[i].size);
3020 }
3021
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003022 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3023 {
3024 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3025 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3026 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3027 }
3028 else
3029 {
3030 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3031 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3032 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3033 }
3034
3035 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003036 fastboot_okay("");
3037 return;
3038}
3039
Ajay Dudani5c761132011-04-07 20:19:04 -07003040void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3041{
3042 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003043 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003044 uint32_t *fill_buf = NULL;
3045 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07003046 sparse_header_t *sparse_header;
3047 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003048 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003049 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303050 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003051 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303052 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003053 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303054 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303055 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003056
Kinson Chikf1a43512011-07-14 11:28:39 -07003057 index = partition_get_index(arg);
3058 ptn = partition_get_offset(index);
3059 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003060 fastboot_fail("partition table doesn't exist");
3061 return;
3062 }
3063
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303064 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303065
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003066 lun = partition_get_lun(index);
3067 mmc_set_lun(lun);
3068
vijay kumar800255e2015-04-24 20:26:19 +05303069 if (sz < sizeof(sparse_header_t)) {
3070 fastboot_fail("size too low");
3071 return;
3072 }
3073
Ajay Dudani5c761132011-04-07 20:19:04 -07003074 /* Read and skip over sparse image header */
3075 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303076
vijay kumar1321f342015-03-27 12:13:42 +05303077 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003078 fastboot_fail("size too large");
3079 return;
3080 }
3081
vijay kumarde4fcf62015-04-23 13:05:49 +05303082 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303083
Parth Dixit64b1a482016-03-07 16:31:26 +05303084 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303085 fastboot_fail("buffer overreads occured due to invalid sparse header");
3086 return;
3087 }
3088
vijay kumarde4fcf62015-04-23 13:05:49 +05303089 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003090 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303091 fastboot_fail("sparse header size mismatch");
3092 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003093 }
3094
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003095 dprintf (SPEW, "=== Sparse Image Header ===\n");
3096 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3097 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3098 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3099 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3100 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3101 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3102 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3103 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003104
3105 /* Start processing chunks */
3106 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3107 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303108 /* Make sure the total image size does not exceed the partition size */
3109 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3110 fastboot_fail("size too large");
3111 return;
3112 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003113 /* Read and skip over chunk header */
3114 chunk_header = (chunk_header_t *) data;
3115 data += sizeof(chunk_header_t);
3116
Parth Dixit64b1a482016-03-07 16:31:26 +05303117 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303118 fastboot_fail("buffer overreads occured due to invalid sparse header");
3119 return;
3120 }
3121
Ajay Dudani5c761132011-04-07 20:19:04 -07003122 dprintf (SPEW, "=== Chunk Header ===\n");
3123 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3124 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3125 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3126
vijay kumar800255e2015-04-24 20:26:19 +05303127 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003128 {
vijay kumar800255e2015-04-24 20:26:19 +05303129 fastboot_fail("chunk header size mismatch");
3130 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003131 }
3132
wufeng.jiang813dc352015-06-02 23:02:46 -04003133 if (!sparse_header->blk_sz ){
3134 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303135 return;
3136 }
3137
wufeng.jiang813dc352015-06-02 23:02:46 -04003138 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3139
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303140 /* Make sure that the chunk size calculated from sparse image does not
3141 * exceed partition size
3142 */
3143 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3144 {
3145 fastboot_fail("Chunk data size exceeds partition size");
3146 return;
3147 }
3148
Ajay Dudani5c761132011-04-07 20:19:04 -07003149 switch (chunk_header->chunk_type)
3150 {
3151 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003152 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003153 chunk_data_sz))
3154 {
3155 fastboot_fail("Bogus chunk size for chunk type Raw");
3156 return;
3157 }
3158
Parth Dixit64b1a482016-03-07 16:31:26 +05303159 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303160 fastboot_fail("buffer overreads occured due to invalid sparse header");
3161 return;
3162 }
3163
wufeng.jiang813dc352015-06-02 23:02:46 -04003164 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3165 otherwise it will return in the line above
3166 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003167 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003168 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003169 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003170 {
3171 fastboot_fail("flash write failure");
3172 return;
3173 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303174 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3175 fastboot_fail("Bogus size for RAW chunk type");
3176 return;
3177 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003178 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003179 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003180 break;
3181
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003182 case CHUNK_TYPE_FILL:
3183 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3184 sizeof(uint32_t)))
3185 {
3186 fastboot_fail("Bogus chunk size for chunk type FILL");
3187 return;
3188 }
3189
3190 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
3191 if (!fill_buf)
3192 {
3193 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3194 return;
3195 }
3196
Parth Dixit64b1a482016-03-07 16:31:26 +05303197 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303198 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303199 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303200 return;
3201 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003202 fill_val = *(uint32_t *)data;
3203 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003204
3205 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3206 {
3207 fill_buf[i] = fill_val;
3208 }
3209
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303210 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3211 {
3212 fastboot_fail("bogus size for chunk FILL type");
3213 free(fill_buf);
3214 return;
3215 }
3216
wufeng.jiang813dc352015-06-02 23:02:46 -04003217 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003218 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303219 /* Make sure that the data written to partition does not exceed partition size */
3220 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3221 {
3222 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303223 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303224 return;
3225 }
3226
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003227 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3228 sparse_header->blk_sz,
3229 fill_buf))
3230 {
3231 fastboot_fail("flash write failure");
3232 free(fill_buf);
3233 return;
3234 }
3235
3236 total_blocks++;
3237 }
3238
3239 free(fill_buf);
3240 break;
3241
Ajay Dudani5c761132011-04-07 20:19:04 -07003242 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303243 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3244 fastboot_fail("bogus size for chunk DONT CARE type");
3245 return;
3246 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003247 total_blocks += chunk_header->chunk_sz;
3248 break;
3249
Ajay Dudani5c761132011-04-07 20:19:04 -07003250 case CHUNK_TYPE_CRC:
3251 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3252 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003253 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003254 return;
3255 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303256 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3257 fastboot_fail("bogus size for chunk CRC type");
3258 return;
3259 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003260 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303261 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303262 fastboot_fail("integer overflow occured");
3263 return;
3264 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003265 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303266 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303267 fastboot_fail("buffer overreads occured due to invalid sparse header");
3268 return;
3269 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003270 break;
3271
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003272 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003273 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003274 fastboot_fail("Unknown chunk type");
3275 return;
3276 }
3277 }
3278
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003279 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3280 total_blocks, sparse_header->total_blks);
3281
3282 if(total_blocks != sparse_header->total_blks)
3283 {
3284 fastboot_fail("sparse image write failure");
3285 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003286
3287 fastboot_okay("");
3288 return;
3289}
3290
3291void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3292{
3293 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003294 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003295
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003296#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003297 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3298 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003299 int ret=0;
3300 uint32 major_version=0;
3301 uint32 minor_version=0;
3302
3303 ret = scm_svc_version(&major_version,&minor_version);
3304 if(!ret)
3305 {
3306 if(major_version >= 2)
3307 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003308 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003309 {
3310 ret = encrypt_scm((uint32 **) &data, &sz);
3311 if (ret != 0) {
3312 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3313 return;
3314 }
3315
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003316 /* Protect only for SSD */
3317 if (!strcmp(arg, "ssd")) {
3318 ret = scm_protect_keystore((uint32 *) data, sz);
3319 if (ret != 0) {
3320 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3321 return;
3322 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003323 }
3324 }
3325 else
3326 {
3327 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3328 if(ret != 0)
3329 {
3330 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3331 return;
3332 }
3333 }
3334 }
3335 else
3336 {
3337 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3338 magic_number[1] == DECRYPT_MAGIC_1)
3339 {
3340 ret = decrypt_scm((uint32 **) &data, &sz);
3341 if (ret != 0) {
3342 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3343 return;
3344 }
3345 }
3346 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3347 magic_number[1] == ENCRYPT_MAGIC_1)
3348 {
3349 ret = encrypt_scm((uint32 **) &data, &sz);
3350 if (ret != 0) {
3351 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3352 return;
3353 }
3354 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003355 }
3356 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003357 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003358 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003359 dprintf(CRITICAL,"INVALID SVC Version\n");
3360 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003361 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003362#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003363
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003364#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003365 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003366 {
lijuang511a2b52015-08-14 20:50:51 +08003367 /* if device is locked:
3368 * common partition will not allow to be flashed
3369 * critical partition will allow to flash image.
3370 */
3371 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3372 fastboot_fail("Partition flashing is not allowed");
3373 return;
3374 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303375#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003376 /* if device critical is locked:
3377 * common partition will allow to be flashed
3378 * critical partition will not allow to flash image.
3379 */
3380 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
3381 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003382 return;
3383 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303384#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003385 }
3386#endif
3387
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003388 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003389 meta_header = (meta_header_t *) data;
3390 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003391 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003392 else if (meta_header->magic == META_HEADER_MAGIC)
3393 cmd_flash_meta_img(arg, data, sz);
3394 else
3395 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003396
3397#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303398#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003399 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
3400 {
3401 // reset dm_verity mode to enforcing
3402 device.verity_mode = 1;
3403 write_device_info(&device);
3404 }
3405#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303406#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003407
Ajay Dudani5c761132011-04-07 20:19:04 -07003408 return;
3409}
3410
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003411void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3412{
3413 struct ptentry *sys_ptn;
3414 struct ptable *ptable;
3415
3416 ptable = flash_get_ptable();
3417 if (ptable == NULL) {
3418 fastboot_fail("partition table doesn't exist");
3419 return;
3420 }
3421
3422 sys_ptn = ptable_find(ptable, "system");
3423 if (sys_ptn == NULL) {
3424 fastboot_fail("system partition not found");
3425 return;
3426 }
3427
3428 sz = ROUND_TO_PAGE(sz, page_mask);
3429 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3430 fastboot_fail("update_ubi_vol failed");
3431 else
3432 fastboot_okay("");
3433}
3434
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003435void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003436{
3437 struct ptentry *ptn;
3438 struct ptable *ptable;
3439 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303440 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303441 unsigned bytes_to_round_page = 0;
3442 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003443
Kishor PK38ed93d2017-04-25 14:19:26 +05303444 if((uintptr_t)data > (UINT_MAX - sz)) {
3445 fastboot_fail("Cannot flash: image header corrupt");
3446 return;
3447 }
3448
Dima Zavin214cc642009-01-26 11:16:21 -08003449 ptable = flash_get_ptable();
3450 if (ptable == NULL) {
3451 fastboot_fail("partition table doesn't exist");
3452 return;
3453 }
3454
3455 ptn = ptable_find(ptable, arg);
3456 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003457 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3458 arg);
3459 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003460 return;
3461 }
3462
3463 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05303464 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
3465 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
3466 } else {
3467 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08003468 return;
3469 }
3470 }
3471
Amol Jadi5c61a952012-05-04 17:05:35 -07003472 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003473 || !strcmp(ptn->name, "userdata")
3474 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003475 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003476 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003477 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05303478 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303479 rounded_size = ROUNDUP(sz, page_size);
3480 bytes_to_round_page = rounded_size - sz;
3481 if (bytes_to_round_page) {
3482 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
3483 fastboot_fail("Integer overflow detected");
3484 return;
3485 }
3486 if (((uintptr_t)data + sz + bytes_to_round_page) >
3487 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
3488 fastboot_fail("Buffer size is not aligned to page_size");
3489 return;
3490 }
3491 else {
3492 memset(data + sz, 0, bytes_to_round_page);
3493 sz = rounded_size;
3494 }
Kishor PK38ed93d2017-04-25 14:19:26 +05303495 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303496 }
3497
Kishor PK38ed93d2017-04-25 14:19:26 +05303498 /*Checking partition_size for the possible integer overflow */
3499 partition_size = validate_partition_size(ptn);
3500
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303501 if (sz > partition_size) {
3502 fastboot_fail("Image size too large");
3503 return;
3504 }
3505
Dima Zavin214cc642009-01-26 11:16:21 -08003506 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05303507 if ((sz > UBI_EC_HDR_SIZE) &&
3508 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003509 if (flash_ubi_img(ptn, data, sz)) {
3510 fastboot_fail("flash write failure");
3511 return;
3512 }
3513 } else {
3514 if (flash_write(ptn, extra, data, sz)) {
3515 fastboot_fail("flash write failure");
3516 return;
3517 }
Dima Zavin214cc642009-01-26 11:16:21 -08003518 }
3519 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3520 fastboot_okay("");
3521}
3522
Kishor PK38ed93d2017-04-25 14:19:26 +05303523
3524static inline uint64_t validate_partition_size(struct ptentry *ptn)
3525{
3526 if (ptn->length && flash_num_pages_per_blk() && page_size) {
3527 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
3528 return ptn->length * flash_num_pages_per_blk() * page_size;
3529 }
3530 }
3531 return 0;
3532}
3533
3534
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003535void cmd_flash(const char *arg, void *data, unsigned sz)
3536{
3537 if(target_is_emmc_boot())
3538 cmd_flash_mmc(arg, data, sz);
3539 else
3540 cmd_flash_nand(arg, data, sz);
3541}
3542
Dima Zavin214cc642009-01-26 11:16:21 -08003543void cmd_continue(const char *arg, void *data, unsigned sz)
3544{
3545 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003546 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003547
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003548 if (target_is_emmc_boot())
3549 {
lijuanga40d6302015-07-20 20:10:13 +08003550#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003551 /* Exit keys' detection thread firstly */
3552 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003553#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003554 boot_linux_from_mmc();
3555 }
3556 else
3557 {
3558 boot_linux_from_flash();
3559 }
Dima Zavin214cc642009-01-26 11:16:21 -08003560}
3561
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003562void cmd_reboot(const char *arg, void *data, unsigned sz)
3563{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003564 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003565 fastboot_okay("");
3566 reboot_device(0);
3567}
3568
Mayank Grover351a75e2017-05-30 20:06:08 +05303569void cmd_set_active(const char *arg, void *data, unsigned sz)
3570{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303571 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05303572 unsigned i,current_active_slot;
3573 const char *current_slot_suffix;
3574
3575 if (!partition_multislot_is_supported())
3576 {
3577 fastboot_fail("Command not supported");
3578 return;
3579 }
3580
3581 if (arg)
3582 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303583 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303584 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05303585 {
3586 current_active_slot = partition_find_active_slot();
3587
3588 /* Check if trying to make curent slot active */
3589 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303590 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3591 (char *)suffix_delimiter, &sp);
3592
Mayank Grover5eb5f692017-07-19 20:16:04 +05303593 if (current_slot_suffix &&
3594 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303595 {
3596 fastboot_okay("Slot already set active");
3597 return;
3598 }
3599 else
3600 {
3601 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
3602 {
3603 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303604 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3605 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303606 if (current_slot_suffix &&
3607 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303608 {
3609 partition_switch_slots(current_active_slot, i);
3610 publish_getvar_multislot_vars();
3611 fastboot_okay("");
3612 return;
3613 }
3614 }
3615 }
3616 }
3617 }
3618 fastboot_fail("Invalid slot suffix.");
3619 return;
3620}
3621
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003622void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3623{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003624 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003625 fastboot_okay("");
3626 reboot_device(FASTBOOT_MODE);
3627}
3628
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003629void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3630{
3631 dprintf(INFO, "Enabling charger screen check\n");
3632 device.charger_screen_enabled = 1;
3633 write_device_info(&device);
3634 fastboot_okay("");
3635}
3636
3637void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3638{
3639 dprintf(INFO, "Disabling charger screen check\n");
3640 device.charger_screen_enabled = 0;
3641 write_device_info(&device);
3642 fastboot_okay("");
3643}
3644
lijuanga25d8bb2015-09-16 13:11:52 +08003645void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3646{
3647 char *p = NULL;
3648 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303649 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003650
3651 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303652 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003653 if (p) {
3654 if (!strncmp(p, "0", 1)) {
3655 device.charger_screen_enabled = 0;
3656 } else if (!strncmp(p, "1", 1)) {
3657 device.charger_screen_enabled = 1;
3658 }
3659 }
3660 }
3661
3662 /* update charger_screen_enabled value for getvar
3663 * command
3664 */
3665 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3666 device.charger_screen_enabled);
3667
3668 write_device_info(&device);
3669 fastboot_okay("");
3670}
3671
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303672void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3673{
3674 dprintf(INFO, "Selecting display panel %s\n", arg);
3675 if (arg)
3676 strlcpy(device.display_panel, arg,
3677 sizeof(device.display_panel));
3678 write_device_info(&device);
3679 fastboot_okay("");
3680}
3681
Shashank Mittal162244e2011-08-08 19:01:25 -07003682void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3683{
lijuang4ece1e72015-08-14 21:02:36 +08003684 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303685}
3686
3687void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3688{
lijuang4ece1e72015-08-14 21:02:36 +08003689 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303690 if(!is_allow_unlock) {
3691 fastboot_fail("oem unlock is not allowed");
3692 return;
3693 }
3694
lijuang4ece1e72015-08-14 21:02:36 +08003695 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303696
lijuang4ece1e72015-08-14 21:02:36 +08003697 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303698 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05303699 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05303700 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3701 write_misc(0, &msg, sizeof(msg));
3702
3703 fastboot_okay("");
3704 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003705 }
3706 fastboot_okay("");
3707}
3708
Channagoud Kadabiad259832015-05-29 11:14:17 -07003709static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3710{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303711 int ret=1;
3712 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003713
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303714 /*
3715 Authentication method not implemented.
3716
3717 OEM to implement, authentication system which on successful validataion,
3718 calls write_allow_oem_unlock() with is_allow_unlock.
3719 */
3720#if 0
3721 authentication_success = oem_specific_auth_mthd();
3722#endif
3723
3724 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07003725 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303726 is_allow_unlock = true;
3727 write_allow_oem_unlock(is_allow_unlock);
3728 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003729 }
3730 return ret;
3731}
3732
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003733void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3734{
lijuang4ece1e72015-08-14 21:02:36 +08003735 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003736}
3737
Shashank Mittala0032282011-08-26 14:50:11 -07003738void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3739{
lijuang511a2b52015-08-14 20:50:51 +08003740 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003741 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003742 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003743 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3744 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303745#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003746 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3747 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303748#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003749 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003750 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303751 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3752 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003753 fastboot_okay("");
3754}
3755
lijuang511a2b52015-08-14 20:50:51 +08003756void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3757{
3758 char response[MAX_RSP_SIZE];
3759 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3760 fastboot_info(response);
3761 fastboot_okay("");
3762}
3763
3764void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3765{
lijuang4ece1e72015-08-14 21:02:36 +08003766 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003767}
3768
3769void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3770{
lijuang4ece1e72015-08-14 21:02:36 +08003771 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003772}
3773
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003774void cmd_preflash(const char *arg, void *data, unsigned sz)
3775{
3776 fastboot_okay("");
3777}
3778
Mao Flynn7b379f32015-04-20 00:28:30 +08003779static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303780
Mao Flynn7b379f32015-04-20 00:28:30 +08003781int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303782{
Mao Flynn7b379f32015-04-20 00:28:30 +08003783 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303784 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003785 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303786 return -1;
3787 return 0;
3788}
3789
Mao Flynn7b379f32015-04-20 00:28:30 +08003790int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003791{
3792 struct ptentry *ptn;
3793 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003794 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003795 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003796
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303797 ptable = flash_get_ptable();
3798 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003799 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3800 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303801 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003802
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303803 ptn = ptable_find(ptable, "splash");
3804 if (ptn == NULL) {
3805 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003806 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303807 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003808 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303809 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003810 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303811 }
3812
Mao Flynn7b379f32015-04-20 00:28:30 +08003813 header = (struct logo_img_header *)logo_header;
3814 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303815 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003816 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303817 }
3818
3819 fb_display = fbcon_display();
3820 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003821 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3822 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3823
3824 /* if the logo is full-screen size, remove "fbcon_clear()" */
3825 if ((header->width != fb_display->width)
3826 || (header->height != fb_display->height))
3827 fbcon_clear();
3828
3829 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3830 (uint32_t *)base,
3831 (header->blocks * 512))) {
3832 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3833 return -1;
3834 }
3835 fbcon_extract_to_screen(header, base);
3836 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003837 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003838
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003839 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3840 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003841 return -1;
3842 }
3843
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303844 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05303845 uint32_t fb_size = ROUNDUP(fb_display->width *
3846 fb_display->height *
3847 (fb_display->bpp / 8), 4096);
3848 uint32_t splash_size = ((((header->width * header->height *
3849 fb_display->bpp/8) + 511) >> 9) << 9);
3850
3851 if (splash_size > fb_size) {
3852 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
3853 return -1;
3854 }
3855
Mao Flynn7b379f32015-04-20 00:28:30 +08003856 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3857 (uint32_t *)base,
3858 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303859 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303860 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003861 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003862 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303863 }
3864
Mao Flynn7b379f32015-04-20 00:28:30 +08003865 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303866}
3867
Mao Flynn7b379f32015-04-20 00:28:30 +08003868int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303869{
3870 int index = INVALID_PTN;
3871 unsigned long long ptn = 0;
3872 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003873 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003874 uint32_t blocksize, realsize, readsize;
3875 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303876
3877 index = partition_get_index("splash");
3878 if (index == 0) {
3879 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003880 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303881 }
3882
3883 ptn = partition_get_offset(index);
3884 if (ptn == 0) {
3885 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003886 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303887 }
3888
Mao Flynn1893a7f2015-06-03 12:03:36 +08003889 mmc_set_lun(partition_get_lun(index));
3890
3891 blocksize = mmc_get_device_blocksize();
3892 if (blocksize == 0) {
3893 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3894 return -1;
3895 }
3896
3897 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003898 if (!fb_display)
3899 {
3900 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3901 return -1;
3902 }
3903
Mao Flynn1893a7f2015-06-03 12:03:36 +08003904 base = (uint8_t *) fb_display->base;
3905
3906 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303907 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003908 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303909 }
3910
Mao Flynn1893a7f2015-06-03 12:03:36 +08003911 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003912 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303913 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003914 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303915 }
3916
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303917 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003918 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3919 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003920
Mao Flynn1893a7f2015-06-03 12:03:36 +08003921 realsize = header->blocks * 512;
3922 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3923
3924 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003925 if ((header->width != fb_display->width)
3926 || (header->height != fb_display->height))
3927 fbcon_clear();
3928
Sachin Bhayare619e9e42017-05-15 13:10:31 +05303929 uint32_t fb_size = ROUNDUP(fb_display->width *
3930 fb_display->height *
3931 (fb_display->bpp / 8), 4096);
3932
3933 if (readsize > fb_size) {
3934 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
3935 return -1;
3936 }
3937
Mao Flynn1893a7f2015-06-03 12:03:36 +08003938 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003939 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3940 return -1;
3941 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003942
Mao Flynn1893a7f2015-06-03 12:03:36 +08003943 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3944 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303945
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003946 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3947 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003948 return -1;
3949 }
3950
3951 realsize = header->width * header->height * fb_display->bpp / 8;
3952 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3953
3954 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3955 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3956 fbcon_clear();
3957 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3958 return -1;
3959 }
3960 } else {
3961 if (mmc_read(ptn + blocksize ,
3962 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3963 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3964 return -1;
3965 }
3966 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3967 }
3968 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303969 }
3970
Mao Flynn7b379f32015-04-20 00:28:30 +08003971 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303972}
3973
Mao Flynn7b379f32015-04-20 00:28:30 +08003974int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303975{
3976 if (target_is_emmc_boot()) {
3977 return splash_screen_mmc();
3978 } else {
3979 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003980 }
3981}
3982
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003983/* Get the size from partiton name */
3984static void get_partition_size(const char *arg, char *response)
3985{
3986 uint64_t ptn = 0;
3987 uint64_t size;
3988 int index = INVALID_PTN;
3989
3990 index = partition_get_index(arg);
3991
3992 if (index == INVALID_PTN)
3993 {
3994 dprintf(CRITICAL, "Invalid partition index\n");
3995 return;
3996 }
3997
3998 ptn = partition_get_offset(index);
3999
4000 if(!ptn)
4001 {
4002 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
4003 return;
4004 }
4005
4006 size = partition_get_size(index);
4007
4008 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
4009 return;
4010}
4011
4012/*
4013 * Publish the partition type & size info
4014 * fastboot getvar will publish the required information.
4015 * fastboot getvar partition_size:<partition_name>: partition size in hex
4016 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
4017 */
4018static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
4019{
4020 uint8_t i;
4021
4022 for (i = 0; i < num_parts; i++) {
4023 get_partition_size(info[i].part_name, info[i].size_response);
4024
4025 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
4026 {
4027 dprintf(CRITICAL, "partition size name truncated\n");
4028 return;
4029 }
4030 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
4031 {
4032 dprintf(CRITICAL, "partition type name truncated\n");
4033 return;
4034 }
4035
4036 /* publish partition size & type info */
4037 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
4038 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
4039 }
4040}
4041
Mayank Grover351a75e2017-05-30 20:06:08 +05304042void publish_getvar_multislot_vars()
4043{
4044 int i,count;
4045 static bool published = false;
4046 static char slot_count[MAX_RSP_SIZE];
4047 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4048 static char active_slot_suffix[MAX_RSP_SIZE];
4049 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4050 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4051 const char *tmp;
4052 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304053 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304054
4055 if (!published)
4056 {
4057 /* Update slot meta info */
4058 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4059 partition_get_partition_count());
4060 for(i=0; i<count; i++)
4061 {
4062 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304063 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4064 has_slot_pname[i]);
4065 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304066 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4067 }
4068
4069 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4070 {
4071 tmp = SUFFIX_SLOT(i);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304072 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4073 "slot-unbootable:%s", tmp);
4074 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4075 "slot-active:%s", tmp);
4076 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4077 "slot-success:%s", tmp);
4078 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4079 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304080 fastboot_publish(slot_info[i].slot_is_unbootable,
4081 slot_info[i].slot_is_unbootable_rsp);
4082 fastboot_publish(slot_info[i].slot_is_active,
4083 slot_info[i].slot_is_active_rsp);
4084 fastboot_publish(slot_info[i].slot_is_succesful,
4085 slot_info[i].slot_is_succesful_rsp);
4086 fastboot_publish(slot_info[i].slot_retry_count,
4087 slot_info[i].slot_retry_count_rsp);
4088 }
4089 fastboot_publish("current-slot", active_slot_suffix);
4090 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4091 fastboot_publish("slot-count", slot_count);
4092 published = true;
4093 }
4094
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304095 active_slt = partition_find_active_slot();
4096 if (active_slt != INVALID)
4097 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s",
4098 SUFFIX_SLOT(active_slt));
4099 else
4100 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4101
Mayank Grover351a75e2017-05-30 20:06:08 +05304102 /* Update partition meta information */
4103 partition_fill_slot_meta(slot_info);
4104 return;
4105}
4106
lijuang4ece1e72015-08-14 21:02:36 +08004107void get_product_name(unsigned char *buf)
4108{
4109 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4110 return;
4111}
4112
4113void get_bootloader_version(unsigned char *buf)
4114{
4115 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4116 return;
4117}
4118
4119void get_baseband_version(unsigned char *buf)
4120{
4121 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4122 return;
4123}
4124
4125bool is_device_locked()
4126{
4127 return device.is_unlocked ? false:true;
4128}
4129
Amol Jadi5edf3552013-07-23 14:15:34 -07004130/* register commands and variables for fastboot */
4131void aboot_fastboot_register_commands(void)
4132{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004133 int i;
lijuangf16461c2015-08-03 17:09:34 +08004134 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004135
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004136 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004137 /* By default the enabled list is empty. */
4138 {"", NULL},
4139 /* move commands enclosed within the below ifndef to here
4140 * if they need to be enabled in user build.
4141 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004142#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004143 /* Register the following commands only for non-user builds */
4144 {"flash:", cmd_flash},
4145 {"erase:", cmd_erase},
4146 {"boot", cmd_boot},
4147 {"continue", cmd_continue},
4148 {"reboot", cmd_reboot},
4149 {"reboot-bootloader", cmd_reboot_bootloader},
4150 {"oem unlock", cmd_oem_unlock},
4151 {"oem unlock-go", cmd_oem_unlock_go},
4152 {"oem lock", cmd_oem_lock},
4153 {"flashing unlock", cmd_oem_unlock},
4154 {"flashing lock", cmd_oem_lock},
4155 {"flashing lock_critical", cmd_flashing_lock_critical},
4156 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4157 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4158 {"oem device-info", cmd_oem_devinfo},
4159 {"preflash", cmd_preflash},
4160 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4161 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004162 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004163 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304164 {"set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004165#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004166 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004167#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004168#endif
lijuang511a2b52015-08-14 20:50:51 +08004169 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004170
4171 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4172 for (i = 1; i < fastboot_cmds_count; i++)
4173 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4174
Amol Jadi5edf3552013-07-23 14:15:34 -07004175 /* publish variables and their values */
4176 fastboot_publish("product", TARGET(BOARD));
4177 fastboot_publish("kernel", "lk");
4178 fastboot_publish("serialno", sn_buf);
4179
4180 /*
4181 * partition info is supported only for emmc partitions
4182 * Calling this for NAND prints some error messages which
4183 * is harmless but misleading. Avoid calling this for NAND
4184 * devices.
4185 */
4186 if (target_is_emmc_boot())
4187 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
4188
Mayank Grover351a75e2017-05-30 20:06:08 +05304189 if (partition_multislot_is_supported())
4190 publish_getvar_multislot_vars();
4191
Amol Jadi5edf3552013-07-23 14:15:34 -07004192 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004193 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4194 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004195 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004196 /* Is the charger screen check enabled */
4197 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4198 device.charger_screen_enabled);
4199 fastboot_publish("charger-screen-enabled",
4200 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004201 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304202 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4203 device.display_panel);
4204 fastboot_publish("display-panel",
4205 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004206 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4207 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08004208 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
4209 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4210 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4211 target_is_emmc_boot()? "eMMC":"UFS");
4212 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004213#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004214 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004215 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004216 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004217#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004218}
4219
Brian Swetland9c4c0752009-01-25 16:23:50 -08004220void aboot_init(const struct app_descriptor *app)
4221{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004222 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304223 int boot_err_type = 0;
4224 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004225
lijuang39831732016-01-08 17:49:02 +08004226 /* Initialise wdog to catch early lk crashes */
4227#if WDOG_SUPPORT
4228 msm_wdog_init();
4229#endif
4230
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004231 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004232 if (target_is_emmc_boot())
4233 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004234 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004235 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304236 mmc_blocksize = mmc_get_device_blocksize();
4237 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004238 }
4239 else
4240 {
4241 page_size = flash_page_size();
4242 page_mask = page_size - 1;
4243 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07004244 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
4245
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004246 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05304247 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07004248
Mayank Grover351a75e2017-05-30 20:06:08 +05304249 /* Detect multi-slot support */
4250 if (partition_multislot_is_supported())
4251 {
4252 boot_slot = partition_find_active_slot();
4253 if (boot_slot == INVALID)
4254 {
4255 boot_into_fastboot = true;
4256 dprintf(INFO, "Active Slot: (INVALID)\n");
4257 }
4258 else
4259 {
4260 /* Setting the state of system to boot active slot */
4261 partition_mark_active_slot(boot_slot);
4262 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
4263 }
4264 }
4265
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004266 /* Display splash screen if enabled */
4267#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08004268#if NO_ALARM_DISPLAY
4269 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004270#endif
lijuang99c02d82015-02-13 19:04:34 +08004271 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07004272#if DISPLAY_HDMI_PRIMARY
4273 if (!strlen(device.display_panel))
4274 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
4275 sizeof(device.display_panel));
4276#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004277#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07004278 /* Wait if the display shutdown is in progress */
4279 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004280 if (!pm_appsbl_display_init_done())
4281 target_display_init(device.display_panel);
4282 else
4283 display_image_on_screen();
4284#else
lijuang99c02d82015-02-13 19:04:34 +08004285 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004286#endif
lijuang99c02d82015-02-13 19:04:34 +08004287 dprintf(SPEW, "Display Init: Done\n");
4288#if NO_ALARM_DISPLAY
4289 }
4290#endif
4291#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004292
Greg Griscod6250552011-06-29 14:40:23 -07004293 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004294 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08004295
Dhaval Patel223ec952013-07-18 14:49:44 -07004296 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
4297
Matthew Qindefd5562014-07-11 18:02:40 +08004298 /*
4299 * Check power off reason if user force reset,
4300 * if yes phone will do normal boot.
4301 */
4302 if (is_user_force_reset())
4303 goto normal_boot;
4304
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004305 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004306 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004307 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03004308 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08004309 reboot_device(EMERGENCY_DLOAD);
4310 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
4311
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004312 boot_into_fastboot = true;
4313 }
4314 if (!boot_into_fastboot)
4315 {
4316 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
4317 boot_into_recovery = 1;
4318 if (!boot_into_recovery &&
4319 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004320 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004321 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004322 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004323 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004324 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004325 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004326
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004327#if USE_PON_REBOOT_REG
4328 reboot_mode = check_hard_reboot_mode();
4329#else
Ajay Dudani77421292010-10-27 19:34:06 -07004330 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004331#endif
4332 if (reboot_mode == RECOVERY_MODE)
4333 {
Ajay Dudani77421292010-10-27 19:34:06 -07004334 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004335 }
4336 else if(reboot_mode == FASTBOOT_MODE)
4337 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004338 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004339 }
4340 else if(reboot_mode == ALARM_BOOT)
4341 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004342 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004343 }
Parth Dixit207573a2015-10-27 17:26:21 +05304344#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05304345#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004346 else if (reboot_mode == DM_VERITY_ENFORCING)
4347 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004348 device.verity_mode = 1;
4349 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004350 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004351#if ENABLE_VB_ATTEST
4352 else if (reboot_mode == DM_VERITY_EIO)
4353#else
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004354 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004355#endif
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004356 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004357 device.verity_mode = 0;
4358 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004359 }
4360 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4361 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004362 if(send_delete_keys_to_tz())
4363 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07004364 }
Parth Dixit207573a2015-10-27 17:26:21 +05304365#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05304366#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004367
Matthew Qindefd5562014-07-11 18:02:40 +08004368normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004369 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004370 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004371 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004372 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004373 if(emmc_recovery_init())
4374 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4375 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004376 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004377 if((device.is_unlocked) || (device.is_tampered))
4378 {
4379 #ifdef TZ_TAMPER_FUSE
4380 set_tamper_fuse_cmd();
4381 #endif
4382 #if USE_PCOM_SECBOOT
4383 set_tamper_flag(device.is_tampered);
4384 #endif
4385 }
Shashank Mittala0032282011-08-26 14:50:11 -07004386 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004387
Mayank Grover351a75e2017-05-30 20:06:08 +05304388retry_boot:
4389 /* Trying to boot active partition */
4390 if (partition_multislot_is_supported())
4391 {
4392 boot_slot = partition_find_boot_slot();
4393 partition_mark_active_slot(boot_slot);
4394 if (boot_slot == INVALID)
4395 goto fastboot;
4396 }
4397
4398 boot_err_type = boot_linux_from_mmc();
4399 switch (boot_err_type)
4400 {
4401 case ERR_INVALID_PAGE_SIZE:
4402 case ERR_DT_PARSE:
4403 case ERR_ABOOT_ADDR_OVERLAP:
4404 if(partition_multislot_is_supported())
4405 goto retry_boot;
4406 else
4407 break;
4408 case ERR_INVALID_BOOT_MAGIC:
4409 default:
4410 break;
4411 /* going to fastboot menu */
4412 }
Shashank Mittala0032282011-08-26 14:50:11 -07004413 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004414 else
4415 {
4416 recovery_init();
4417 #if USE_PCOM_SECBOOT
4418 if((device.is_unlocked) || (device.is_tampered))
4419 set_tamper_flag(device.is_tampered);
4420 #endif
4421 boot_linux_from_flash();
4422 }
4423 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4424 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004425 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004426
Mayank Grover351a75e2017-05-30 20:06:08 +05304427fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07004428 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004429
Amol Jadi5edf3552013-07-23 14:15:34 -07004430 /* register aboot specific fastboot commands */
4431 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004432
Amol Jadi5edf3552013-07-23 14:15:34 -07004433 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004434 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004435
4436 /* initialize and start fastboot */
4437 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004438#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004439 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004440#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004441}
4442
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004443uint32_t get_page_size()
4444{
4445 return page_size;
4446}
4447
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004448/*
4449 * Calculated and save hash (SHA256) for non-signed boot image.
4450 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004451 * @param image_addr - Boot image address
4452 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004453 *
4454 * @return int - 0 on success, negative value on failure.
4455 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004456static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004457{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004458 unsigned int digest[8];
4459#if IMAGE_VERIF_ALGO_SHA1
4460 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4461#else
4462 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4463#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004464
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004465 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004466 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004467
4468 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004469 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004470
4471 return 0;
4472}
4473
Mayank Grover351a75e2017-05-30 20:06:08 +05304474
Brian Swetland9c4c0752009-01-25 16:23:50 -08004475APP_START(aboot)
4476 .init = aboot_init,
4477APP_END