blob: 7f2d1c70824b6f513c7848168b45d67aae70c8a3 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
lijuang9a7d3b92015-11-30 14:41:24 +08005 * Copyright (c) 2009-2016, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080060#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053061#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080062#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080063#if USE_RPMB_FOR_DEVINFO
64#include <rpmb.h>
65#endif
Dima Zavin214cc642009-01-26 11:16:21 -080066
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070067#if ENABLE_WBC
68#include <pm_app_smbchg.h>
69#endif
70
Neeti Desai17379b82012-06-04 18:42:53 -070071#if DEVICE_TREE
72#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070073#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070074#endif
75
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053076#if WDOG_SUPPORT
77#include <wdog.h>
78#endif
79
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070080#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070081#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080082#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080083#include "bootimg.h"
84#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070085#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080086#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070087#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070088#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070089#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020091#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070092#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080093#include <menu_keys_detect.h>
94#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070095#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070096
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070097extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070098extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070099extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700100extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530101 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700102
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700103void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700104void write_device_info_mmc(device_info *dev);
105void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700106static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700107static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700108
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700109/* fastboot command function pointer */
110typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
111
112struct fastboot_cmd_desc {
113 char * name;
114 fastboot_cmd_fn cb;
115};
116
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700117#define EXPAND(NAME) #NAME
118#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700119
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800120#ifdef MEMBASE
121#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
122#else
David Ng183a7422009-12-07 14:55:21 -0800123#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800124#endif
125
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700126#ifndef MEMSIZE
127#define MEMSIZE 1024*1024
128#endif
129
130#define MAX_TAGS_SIZE 1024
131
Kun Liang2f1601a2013-08-12 16:29:54 +0800132/* make 4096 as default size to ensure EFS,EXT4's erasing */
133#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700134#define MAX_PANEL_BUF_SIZE 196
Kun Liang2f1601a2013-08-12 16:29:54 +0800135
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700136#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700137#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800138
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800139#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
140
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700141#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
142
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800143#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700144static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700145#else
David Ng183a7422009-12-07 14:55:21 -0800146static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700147#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800148static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300149static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800150static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800151static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800152static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700153static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300154static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200155static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800156
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800157static const char *baseband_apq = " androidboot.baseband=apq";
158static const char *baseband_msm = " androidboot.baseband=msm";
159static const char *baseband_csfb = " androidboot.baseband=csfb";
160static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700161static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800162static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700163static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800164static const char *baseband_dsda = " androidboot.baseband=dsda";
165static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800166static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800167static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800168
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700169#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530170#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700171static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700172static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530173static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700174//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700175
176struct verified_boot_verity_mode vbvm[] =
177{
178 {false, "logging"},
179 {true, "enforcing"},
180};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700181struct verified_boot_state_name vbsn[] =
182{
183 {GREEN, "green"},
184 {ORANGE, "orange"},
185 {YELLOW,"yellow"},
186 {RED,"red" },
187};
188#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530189#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700190
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700191static unsigned page_size = 0;
192static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530193static unsigned mmc_blocksize = 0;
194static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700195static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
196static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530197static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800198static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700199static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700200bool boot_into_fastboot = false;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700201
Shashank Mittalcd98d472011-08-02 14:29:24 -0700202/* Assuming unauthorized kernel image by default */
203static int auth_kernel_img = 0;
Parth Dixit6f5822f2015-10-18 01:20:53 +0530204#if VBOOT_MOTA
205static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
206#else
lijuang511a2b52015-08-14 20:50:51 +0800207static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
Parth Dixit6f5822f2015-10-18 01:20:53 +0530208#endif
vijay kumarc65876c2015-04-24 13:29:16 +0530209static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700210
vijay kumarca2e6812015-07-08 20:28:25 +0530211static char frp_ptns[2][8] = {"config","frp"};
212
lijuang511a2b52015-08-14 20:50:51 +0800213static const char *critical_flash_allowed_ptn[] = {
214 "aboot",
215 "rpm",
216 "tz",
217 "sbl",
218 "sdi",
219 "sbl1",
220 "xbl",
221 "hyp",
222 "pmic",
223 "bootloader",
224 "devinfo",
225 "partition"};
226
Dima Zavin42168f22009-01-30 11:52:22 -0800227struct atag_ptbl_entry
228{
229 char name[16];
230 unsigned offset;
231 unsigned size;
232 unsigned flags;
233};
234
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700235/*
236 * Partition info, required to be published
237 * for fastboot
238 */
239struct getvar_partition_info {
240 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
241 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
242 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
243 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
244 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
245};
246
247/*
248 * Right now, we are publishing the info for only
249 * three partitions
250 */
251struct getvar_partition_info part_info[] =
252{
253 { "system" , "partition-size:", "partition-type:", "", "ext4" },
254 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
255 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
256};
257
258char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700259char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800260char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700261char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530262char panel_display_mode[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800263
264#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800265char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800266char battery_soc_ok [MAX_RSP_SIZE];
267#endif
268
lijuangf16461c2015-08-03 17:09:34 +0800269char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700270
Greg Griscod2471ef2011-07-14 13:00:42 -0700271extern int emmc_recovery_init(void);
272
Kinson Chik0b1c8162011-08-31 16:31:57 -0700273#if NO_KEYPAD_DRIVER
274extern int fastboot_trigger(void);
275#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700276
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800277static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700278{
279 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700280#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800281 if (is_arm64)
282 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
283 else
284 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700285 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
286 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700287#endif
288}
289
Dima Zavin42168f22009-01-30 11:52:22 -0800290static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
291{
292 struct atag_ptbl_entry atag_ptn;
293
294 memcpy(atag_ptn.name, ptn->name, 16);
295 atag_ptn.name[15] = '\0';
296 atag_ptn.offset = ptn->start;
297 atag_ptn.size = ptn->length;
298 atag_ptn.flags = ptn->flags;
299 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
300 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
301}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800302
lijuang102dfa92015-10-09 18:31:03 +0800303#if CHECK_BAT_VOLTAGE
304void update_battery_status(void)
305{
306 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
307 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
308}
309#endif
310
Neeti Desaie245d492012-06-01 12:52:13 -0700311unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800312{
David Ng183a7422009-12-07 14:55:21 -0800313 int cmdline_len = 0;
314 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800315 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700316 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800317 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300318 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700319 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700320 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200321 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700322#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530323#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700324 uint32_t boot_state = boot_verify_get_state();
325#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530326#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700327
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200328#ifdef MDTP_SUPPORT
329 mdtp_activated(&is_mdtp_activated);
330#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800331
Brian Swetland9c4c0752009-01-25 16:23:50 -0800332 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800333 cmdline_len = strlen(cmdline);
334 have_cmdline = 1;
335 }
336 if (target_is_emmc_boot()) {
337 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800338#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700339 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
340 ASSERT(boot_dev_buf);
341 platform_boot_dev_cmdline(boot_dev_buf);
342 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700343#endif
David Ng183a7422009-12-07 14:55:21 -0800344 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800345
346 cmdline_len += strlen(usb_sn_cmdline);
347 cmdline_len += strlen(sn_buf);
348
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700349#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530350#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700351 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700352 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
353 {
354 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
355 ASSERT(0);
356 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700357 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Parth Dixita5715a02015-10-29 12:25:10 +0530358 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700359#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530360#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700361
Pavel Nedev5614d222013-06-17 18:01:02 +0300362 if (boot_into_recovery && gpt_exists)
363 cmdline_len += strlen(secondary_gpt_enable);
364
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200365 if(is_mdtp_activated)
366 cmdline_len += strlen(mdtp_activated_flag);
367
Pavel Nedev328ac822013-04-05 15:25:11 +0300368 if (boot_into_ffbm) {
369 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700370 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800371 /* reduce kernel console messages to speed-up boot */
372 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800373 } else if (boot_reason_alarm) {
374 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800375 } else if ((target_build_variant_user() || device.charger_screen_enabled)
376 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700377 pause_at_bootup = 1;
378 cmdline_len += strlen(battchg_pause);
379 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800380
Shashank Mittalcd98d472011-08-02 14:29:24 -0700381 if(target_use_signed_kernel() && auth_kernel_img) {
382 cmdline_len += strlen(auth_kernel);
383 }
384
Joonwoo Park61112782013-10-02 19:50:39 -0700385 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
386 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530387 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700388 have_target_boot_params = 1;
389 cmdline_len += strlen(target_boot_params);
390 }
391
Ajay Dudanid04110c2011-01-17 23:55:07 -0800392 /* Determine correct androidboot.baseband to use */
393 switch(target_baseband())
394 {
395 case BASEBAND_APQ:
396 cmdline_len += strlen(baseband_apq);
397 break;
398
399 case BASEBAND_MSM:
400 cmdline_len += strlen(baseband_msm);
401 break;
402
403 case BASEBAND_CSFB:
404 cmdline_len += strlen(baseband_csfb);
405 break;
406
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800407 case BASEBAND_SVLTE2A:
408 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800409 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700410
411 case BASEBAND_MDM:
412 cmdline_len += strlen(baseband_mdm);
413 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700414
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800415 case BASEBAND_MDM2:
416 cmdline_len += strlen(baseband_mdm2);
417 break;
418
Amol Jadi5c61a952012-05-04 17:05:35 -0700419 case BASEBAND_SGLTE:
420 cmdline_len += strlen(baseband_sglte);
421 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530422
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800423 case BASEBAND_SGLTE2:
424 cmdline_len += strlen(baseband_sglte2);
425 break;
426
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530427 case BASEBAND_DSDA:
428 cmdline_len += strlen(baseband_dsda);
429 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800430
431 case BASEBAND_DSDA2:
432 cmdline_len += strlen(baseband_dsda2);
433 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800434 }
435
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800436 if (cmdline) {
437 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530438 target_display_panel_node(display_panel_buf,
439 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800440 strlen(display_panel_buf)) {
441 cmdline_len += strlen(display_panel_buf);
442 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700443 }
444
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800445 if (target_warm_boot()) {
446 warm_boot = true;
447 cmdline_len += strlen(warmboot_cmdline);
448 }
449
David Ng183a7422009-12-07 14:55:21 -0800450 if (cmdline_len > 0) {
451 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800452 unsigned char *dst;
453
454 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
455 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530456 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800457 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700458
Amol Jadi168b7712012-03-06 16:15:00 -0800459 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800460 if (have_cmdline) {
461 src = cmdline;
462 while ((*dst++ = *src++));
463 }
464 if (target_is_emmc_boot()) {
465 src = emmc_cmdline;
466 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700467 have_cmdline = 1;
468 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800469#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700470 src = boot_dev_buf;
471 if (have_cmdline) --dst;
472 while ((*dst++ = *src++));
473#endif
David Ngf773dde2010-07-26 19:55:08 -0700474 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800475
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700476#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530477#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700478 src = verified_state;
479 if(have_cmdline) --dst;
480 have_cmdline = 1;
481 while ((*dst++ = *src++));
482 src = vbsn[boot_state].name;
483 if(have_cmdline) --dst;
484 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700485
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700486 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
487 {
488 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
489 ASSERT(0);
490 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700491 src = verity_mode;
492 if(have_cmdline) --dst;
493 while ((*dst++ = *src++));
494 src = vbvm[device.verity_mode].name;
495 if(have_cmdline) -- dst;
496 while ((*dst++ = *src++));
Parth Dixita5715a02015-10-29 12:25:10 +0530497 src = keymaster_v1;
498 if(have_cmdline) --dst;
499 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700500#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530501#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800502 src = usb_sn_cmdline;
503 if (have_cmdline) --dst;
504 have_cmdline = 1;
505 while ((*dst++ = *src++));
506 src = sn_buf;
507 if (have_cmdline) --dst;
508 have_cmdline = 1;
509 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800510 if (warm_boot) {
511 if (have_cmdline) --dst;
512 src = warmboot_cmdline;
513 while ((*dst++ = *src++));
514 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800515
Pavel Nedev5614d222013-06-17 18:01:02 +0300516 if (boot_into_recovery && gpt_exists) {
517 src = secondary_gpt_enable;
518 if (have_cmdline) --dst;
519 while ((*dst++ = *src++));
520 }
521
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200522 if (is_mdtp_activated) {
523 src = mdtp_activated_flag;
524 if (have_cmdline) --dst;
525 while ((*dst++ = *src++));
526 }
527
Pavel Nedev328ac822013-04-05 15:25:11 +0300528 if (boot_into_ffbm) {
529 src = androidboot_mode;
530 if (have_cmdline) --dst;
531 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700532 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300533 if (have_cmdline) --dst;
534 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800535 src = loglevel;
536 if (have_cmdline) --dst;
537 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800538 } else if (boot_reason_alarm) {
539 src = alarmboot_cmdline;
540 if (have_cmdline) --dst;
541 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300542 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700543 src = battchg_pause;
544 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800545 while ((*dst++ = *src++));
546 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800547
Shashank Mittalcd98d472011-08-02 14:29:24 -0700548 if(target_use_signed_kernel() && auth_kernel_img) {
549 src = auth_kernel;
550 if (have_cmdline) --dst;
551 while ((*dst++ = *src++));
552 }
553
Ajay Dudanid04110c2011-01-17 23:55:07 -0800554 switch(target_baseband())
555 {
556 case BASEBAND_APQ:
557 src = baseband_apq;
558 if (have_cmdline) --dst;
559 while ((*dst++ = *src++));
560 break;
561
562 case BASEBAND_MSM:
563 src = baseband_msm;
564 if (have_cmdline) --dst;
565 while ((*dst++ = *src++));
566 break;
567
568 case BASEBAND_CSFB:
569 src = baseband_csfb;
570 if (have_cmdline) --dst;
571 while ((*dst++ = *src++));
572 break;
573
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800574 case BASEBAND_SVLTE2A:
575 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800576 if (have_cmdline) --dst;
577 while ((*dst++ = *src++));
578 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700579
580 case BASEBAND_MDM:
581 src = baseband_mdm;
582 if (have_cmdline) --dst;
583 while ((*dst++ = *src++));
584 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700585
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800586 case BASEBAND_MDM2:
587 src = baseband_mdm2;
588 if (have_cmdline) --dst;
589 while ((*dst++ = *src++));
590 break;
591
Amol Jadi5c61a952012-05-04 17:05:35 -0700592 case BASEBAND_SGLTE:
593 src = baseband_sglte;
594 if (have_cmdline) --dst;
595 while ((*dst++ = *src++));
596 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530597
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800598 case BASEBAND_SGLTE2:
599 src = baseband_sglte2;
600 if (have_cmdline) --dst;
601 while ((*dst++ = *src++));
602 break;
603
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530604 case BASEBAND_DSDA:
605 src = baseband_dsda;
606 if (have_cmdline) --dst;
607 while ((*dst++ = *src++));
608 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800609
610 case BASEBAND_DSDA2:
611 src = baseband_dsda2;
612 if (have_cmdline) --dst;
613 while ((*dst++ = *src++));
614 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800615 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700616
617 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700618 src = display_panel_buf;
619 if (have_cmdline) --dst;
620 while ((*dst++ = *src++));
621 }
Joonwoo Park61112782013-10-02 19:50:39 -0700622
623 if (have_target_boot_params) {
624 if (have_cmdline) --dst;
625 src = target_boot_params;
626 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530627 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700628 }
Neeti Desaie245d492012-06-01 12:52:13 -0700629 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700630
631
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700632 if (boot_dev_buf)
633 free(boot_dev_buf);
634
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800635 if (cmdline_final)
636 dprintf(INFO, "cmdline: %s\n", cmdline_final);
637 else
638 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700639 return cmdline_final;
640}
641
642unsigned *atag_core(unsigned *ptr)
643{
644 /* CORE */
645 *ptr++ = 2;
646 *ptr++ = 0x54410001;
647
648 return ptr;
649
650}
651
652unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
653 unsigned ramdisk_size)
654{
655 if (ramdisk_size) {
656 *ptr++ = 4;
657 *ptr++ = 0x54420005;
658 *ptr++ = (unsigned)ramdisk;
659 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800660 }
661
Neeti Desaie245d492012-06-01 12:52:13 -0700662 return ptr;
663}
664
665unsigned *atag_ptable(unsigned **ptr_addr)
666{
667 int i;
668 struct ptable *ptable;
669
670 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700671 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
672 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700673 *(*ptr_addr)++ = 0x4d534d70;
674 for (i = 0; i < ptable->count; ++i)
675 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
676 }
677
678 return (*ptr_addr);
679}
680
681unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
682{
683 int cmdline_length = 0;
684 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700685 char *dest;
686
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800687 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700688 n = (cmdline_length + 4) & (~3);
689
690 *ptr++ = (n / 4) + 2;
691 *ptr++ = 0x54410009;
692 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800693 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700694 ptr += (n / 4);
695
696 return ptr;
697}
698
699unsigned *atag_end(unsigned *ptr)
700{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800701 /* END */
702 *ptr++ = 0;
703 *ptr++ = 0;
704
Neeti Desaie245d492012-06-01 12:52:13 -0700705 return ptr;
706}
707
708void generate_atags(unsigned *ptr, const char *cmdline,
709 void *ramdisk, unsigned ramdisk_size)
710{
711
712 ptr = atag_core(ptr);
713 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
714 ptr = target_atag_mem(ptr);
715
716 /* Skip NAND partition ATAGS for eMMC boot */
717 if (!target_is_emmc_boot()){
718 ptr = atag_ptable(&ptr);
719 }
720
721 ptr = atag_cmdline(ptr, cmdline);
722 ptr = atag_end(ptr);
723}
724
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700725typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700726void boot_linux(void *kernel, unsigned *tags,
727 const char *cmdline, unsigned machtype,
728 void *ramdisk, unsigned ramdisk_size)
729{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800730 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800731#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700732 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800733#endif
734
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700735 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800736 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530737 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800738
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530739 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700740
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800741 final_cmdline = update_cmdline((const char*)cmdline);
742
Neeti Desai17379b82012-06-04 18:42:53 -0700743#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800744 dprintf(INFO, "Updating device tree: start\n");
745
Neeti Desai17379b82012-06-04 18:42:53 -0700746 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530747 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700748 if(ret)
749 {
750 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
751 ASSERT(0);
752 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800753 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700754#else
Neeti Desaie245d492012-06-01 12:52:13 -0700755 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800756 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700757#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700758
Maria Yu52254c02014-07-04 16:14:54 +0800759 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700760
761#if VERIFIED_BOOT
lijuangbdd9bb42016-03-01 18:22:17 +0800762#if !VBOOT_MOTA
763 if (device.verity_mode == 0) {
764#if FBCON_DISPLAY_MSG
765 display_bootverify_menu(DISPLAY_MENU_LOGGING);
766 wait_for_users_action();
767#else
768 dprintf(CRITICAL,
769 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
770 mdelay(5000);
771#endif
772 }
773
774#endif
775#endif
776
777#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700778 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700779 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700780 {
781 dprintf(INFO, "Failed to write protect dev info\n");
782 ASSERT(0);
783 }
784#endif
785
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800786 /* Turn off splash screen if enabled */
787#if DISPLAY_SPLASH_SCREEN
788 target_display_shutdown();
789#endif
790
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700791 /* Perform target specific cleanup */
792 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800793
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800794 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530795 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800796
797 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700798
Amol Jadi4421e652011-06-16 15:00:48 -0700799 /* do any platform specific cleanup before kernel entry */
800 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700801
Brian Swetland9c4c0752009-01-25 16:23:50 -0800802 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700803
Amol Jadi504f9fe2012-08-16 13:56:48 -0700804#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800805 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700806#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700807 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800808
809 if (IS_ARM64(kptr))
810 /* Jump to a 64bit kernel */
811 scm_elexec_call((paddr_t)kernel, tags_phys);
812 else
813 /* Jump to a 32bit kernel */
814 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800815}
816
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700817/* Function to check if the memory address range falls within the aboot
818 * boundaries.
819 * start: Start of the memory region
820 * size: Size of the memory region
821 */
822int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
823{
824 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800825 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700826 return -1;
827
828 /* Check for memory overlap. */
829 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
830 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700831 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700832 return 0;
833 else
834 return -1;
835}
836
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800837#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800838
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800839BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800840#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800841BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800842#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800843
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700844static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
845{
846 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800847
848#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800849#if IMAGE_VERIF_ALGO_SHA1
850 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
851#else
852 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
853#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800854#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700855
856 /* Assume device is rooted at this time. */
857 device.is_tampered = 1;
858
859 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
860
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700861#if VERIFIED_BOOT
862 if(boot_into_recovery)
863 {
864 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530865 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700866 }
867 else
868 {
869 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530870 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700871 }
872 boot_verify_print_state();
873#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700874 ret = image_verify((unsigned char *)bootimg_addr,
875 (unsigned char *)(bootimg_addr + bootimg_size),
876 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800877 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700878#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700879 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
880
881 if (ret)
882 {
883 /* Authorized kernel */
884 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700885 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700886 }
887
Amit Blay4aa292f2015-04-28 21:55:59 +0300888#ifdef MDTP_SUPPORT
889 {
890 /* Verify MDTP lock.
891 * For boot & recovery partitions, use aboot's verification result.
892 */
893 mdtp_ext_partition_verification_t ext_partition;
894 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
895 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
896 ext_partition.page_size = 0; /* Not needed since already validated */
897 ext_partition.image_addr = 0; /* Not needed since already validated */
898 ext_partition.image_size = 0; /* Not needed since already validated */
899 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
900 mdtp_fwlock_verify_lock(&ext_partition);
901 }
902#endif /* MDTP_SUPPORT */
903
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700904#if USE_PCOM_SECBOOT
905 set_tamper_flag(device.is_tampered);
906#endif
907
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700908#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700909 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700910 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700911 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800912#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800913 display_bootverify_menu(DISPLAY_MENU_RED);
lijuanga40d6302015-07-20 20:10:13 +0800914 wait_for_users_action();
915#else
916 dprintf(CRITICAL,
917 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
918 mdelay(5000);
919#endif
920
921 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700922 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800923#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800924 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800925 wait_for_users_action();
926#else
927 dprintf(CRITICAL,
928 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
929 mdelay(5000);
930#endif
931 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700932 default:
lijuanga40d6302015-07-20 20:10:13 +0800933 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700934 }
935#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700936#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530937 if(device.is_tampered)
938 {
939 write_device_info_mmc(&device);
940 #ifdef TZ_TAMPER_FUSE
941 set_tamper_fuse_cmd();
942 #endif
943 #ifdef ASSERT_ON_TAMPER
944 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
945 ASSERT(0);
946 #endif
947 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700948#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700949}
950
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530951static bool check_format_bit()
952{
953 bool ret = false;
954 int index;
955 uint64_t offset;
956 struct boot_selection_info *in = NULL;
957 char *buf = NULL;
958
959 index = partition_get_index("bootselect");
960 if (index == INVALID_PTN)
961 {
962 dprintf(INFO, "Unable to locate /bootselect partition\n");
963 return ret;
964 }
965 offset = partition_get_offset(index);
966 if(!offset)
967 {
968 dprintf(INFO, "partition /bootselect doesn't exist\n");
969 return ret;
970 }
971 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
972 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530973 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530974 {
975 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
976 free(buf);
977 return ret;
978 }
979 in = (struct boot_selection_info *) buf;
980 if ((in->signature == BOOTSELECT_SIGNATURE) &&
981 (in->version == BOOTSELECT_VERSION)) {
982 if ((in->state_info & BOOTSELECT_FORMAT) &&
983 !(in->state_info & BOOTSELECT_FACTORY))
984 ret = true;
985 } else {
986 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
987 in->signature, in->version);
988 ASSERT(0);
989 }
990 free(buf);
991 return ret;
992}
993
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700994void boot_verifier_init()
995{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700996 uint32_t boot_state;
997 /* Check if device unlock */
998 if(device.is_unlocked)
999 {
1000 boot_verify_send_event(DEV_UNLOCK);
1001 boot_verify_print_state();
1002 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1003 return;
1004 }
1005 else
1006 {
1007 boot_verify_send_event(BOOT_INIT);
1008 }
1009
1010 /* Initialize keystore */
1011 boot_state = boot_verify_keystore_init();
1012 if(boot_state == YELLOW)
1013 {
1014 boot_verify_print_state();
1015 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1016 }
1017}
1018
Shashank Mittal23b8f422010-04-16 19:27:21 -07001019int boot_linux_from_mmc(void)
1020{
1021 struct boot_img_hdr *hdr = (void*) buf;
1022 struct boot_img_hdr *uhdr;
1023 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001024 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001025 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001026 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001027
Shashank Mittalcd98d472011-08-02 14:29:24 -07001028 unsigned char *image_addr = 0;
1029 unsigned kernel_actual;
1030 unsigned ramdisk_actual;
1031 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001032 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001033
Matthew Qin271927e2015-03-31 22:07:07 -04001034 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001035 unsigned int out_len = 0;
1036 unsigned int out_avai_len = 0;
1037 unsigned char *out_addr = NULL;
1038 uint32_t dtb_offset = 0;
1039 unsigned char *kernel_start_addr = NULL;
1040 unsigned int kernel_size = 0;
1041 int rc;
1042
Neeti Desai465491e2012-07-31 12:53:35 -07001043#if DEVICE_TREE
1044 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001045 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001046 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001047 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001048 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001049 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001050#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001051 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001052
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301053 if (check_format_bit())
1054 boot_into_recovery = 1;
1055
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001056 if (!boot_into_recovery) {
1057 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1058 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1059 if (rcode <= 0) {
1060 boot_into_ffbm = false;
1061 if (rcode < 0)
1062 dprintf(CRITICAL,"failed to get ffbm cookie");
1063 } else
1064 boot_into_ffbm = true;
1065 } else
1066 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001067 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1068 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1069 dprintf(INFO, "Unified boot method!\n");
1070 hdr = uhdr;
1071 goto unified_boot;
1072 }
Greg Griscod6250552011-06-29 14:40:23 -07001073 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001074 index = partition_get_index("boot");
1075 ptn = partition_get_offset(index);
1076 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001077 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1078 return -1;
1079 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001080 }
1081 else {
1082 index = partition_get_index("recovery");
1083 ptn = partition_get_offset(index);
1084 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001085 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1086 return -1;
1087 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001088 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001089 /* Set Lun for boot & recovery partitions */
1090 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001091
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301092 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001093 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1094 return -1;
1095 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001096
1097 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001098 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001099 return -1;
1100 }
1101
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001102 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301103
1104 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1105 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1106 return -1;
1107 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001108 page_size = hdr->page_size;
1109 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001110 }
1111
vijay kumar287bb542015-09-29 13:01:52 +05301112 /* ensure commandline is terminated */
1113 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1114
Matthew Qin49e51fa2015-02-09 10:40:45 +08001115 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1116 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001117
Matthew Qin49e51fa2015-02-09 10:40:45 +08001118 image_addr = (unsigned char *)target_get_scratch_address();
1119
1120#if DEVICE_TREE
1121 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1122 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1123#else
1124 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1125#endif
1126
1127#if VERIFIED_BOOT
1128 boot_verifier_init();
1129#endif
1130
1131 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
1132 {
1133 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1134 return -1;
1135 }
1136
Matthew Qinbb7923d2015-02-09 10:56:09 +08001137 /*
1138 * Update loading flow of bootimage to support compressed/uncompressed
1139 * bootimage on both 64bit and 32bit platform.
1140 * 1. Load bootimage from emmc partition onto DDR.
1141 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1142 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1143 * platform accordingly.
1144 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1145 */
1146
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001147 dprintf(INFO, "Loading (%s) image (%d): start\n",
1148 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001149 bs_set_timestamp(BS_KERNEL_LOAD_START);
1150
Matthew Qinbb7923d2015-02-09 10:56:09 +08001151 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001152 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1153 {
1154 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1155 return -1;
1156 }
1157
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001158 dprintf(INFO, "Loading (%s) image (%d): done\n",
1159 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1160
Matthew Qin49e51fa2015-02-09 10:40:45 +08001161 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1162
1163 /* Authenticate Kernel */
1164 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1165 (int) target_use_signed_kernel(),
1166 device.is_unlocked,
1167 device.is_tampered);
1168
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001169 /* Change the condition a little bit to include the test framework support.
1170 * We would never reach this point if device is in fastboot mode, even if we did
1171 * that means we are in test mode, so execute kernel authentication part for the
1172 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301173 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001174 {
1175 offset = imagesize_actual;
1176 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1177 {
1178 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1179 return -1;
1180 }
1181
1182 /* Read signature */
1183 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1184 {
1185 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1186 return -1;
1187 }
1188
1189 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001190 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301191 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001192 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001193 } else {
1194 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1195 #ifdef TZ_SAVE_KERNEL_HASH
1196 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1197 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001198
1199#ifdef MDTP_SUPPORT
1200 {
1201 /* Verify MDTP lock.
1202 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1203 * since verification was skipped in aboot. The signature is not part of the loaded image.
1204 */
1205 mdtp_ext_partition_verification_t ext_partition;
1206 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1207 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1208 ext_partition.page_size = page_size;
1209 ext_partition.image_addr = (uint32)image_addr;
1210 ext_partition.image_size = imagesize_actual;
1211 ext_partition.sig_avail = FALSE;
1212 mdtp_fwlock_verify_lock(&ext_partition);
1213 }
1214#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001215 }
1216
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001217#if VERIFIED_BOOT
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001218 if(boot_verify_get_state() == ORANGE)
1219 {
1220#if FBCON_DISPLAY_MSG
1221 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1222 wait_for_users_action();
1223#else
1224 dprintf(CRITICAL,
1225 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1226 mdelay(5000);
1227#endif
1228 }
1229#endif
1230
1231#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05301232#if !VBOOT_MOTA
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001233 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001234 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001235 ASSERT(0);
1236#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05301237#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001238 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001239 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1240 * If not, continue booting.
1241 */
1242 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1243 {
1244 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1245 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001246 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001247 rc = decompress((unsigned char *)(image_addr + page_size),
1248 hdr->kernel_size, out_addr, out_avai_len,
1249 &dtb_offset, &out_len);
1250 if (rc)
1251 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001252 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001253 ASSERT(0);
1254 }
1255
Matthew Qin0b15b322015-05-19 05:20:54 -04001256 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001257 kptr = (struct kernel64_hdr *)out_addr;
1258 kernel_start_addr = out_addr;
1259 kernel_size = out_len;
1260 } else {
1261 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1262 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1263 kernel_size = hdr->kernel_size;
1264 }
1265
1266 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001267 * Update the kernel/ramdisk/tags address if the boot image header
1268 * has default values, these default values come from mkbootimg when
1269 * the boot image is flashed using fastboot flash:raw
1270 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001271 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001272
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001273 /* Get virtual addresses since the hdr saves physical addresses. */
1274 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1275 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1276 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001277
Matthew Qinbb7923d2015-02-09 10:56:09 +08001278 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001279 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001280 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001281 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1282 {
1283 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1284 return -1;
1285 }
1286
1287#ifndef DEVICE_TREE
1288 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1289 {
1290 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1291 return -1;
1292 }
1293#endif
1294
Matthew Qin49e51fa2015-02-09 10:40:45 +08001295 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001296 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001297 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001298
Matthew Qin49e51fa2015-02-09 10:40:45 +08001299 #if DEVICE_TREE
1300 if(hdr->dt_size) {
1301 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1302 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001303
Matthew Qin49e51fa2015-02-09 10:40:45 +08001304 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1305 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1306 return -1;
1307 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001308
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301309 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1310 goes beyound hdr->dt_size*/
1311 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1312 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1313 return -1;
1314 }
1315
Matthew Qin49e51fa2015-02-09 10:40:45 +08001316 /* Find index of device tree within device tree table */
1317 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1318 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1319 return -1;
1320 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001321
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301322 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1323 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1324 return -1;
1325 }
1326
1327 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1328 if ((dt_entry.offset + dt_entry.size) > hdr->dt_size) {
1329 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1330 return -1;
1331 }
1332
Matthew Qin271927e2015-03-31 22:07:07 -04001333 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1334 {
1335 unsigned int compressed_size = 0;
1336 out_addr += out_len;
1337 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001338 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001339 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1340 dt_entry.size, out_addr, out_avai_len,
1341 &compressed_size, &dtb_size);
1342 if (rc)
1343 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001344 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001345 ASSERT(0);
1346 }
1347
Matthew Qin0b15b322015-05-19 05:20:54 -04001348 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001349 best_match_dt_addr = out_addr;
1350 } else {
1351 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1352 dtb_size = dt_entry.size;
1353 }
1354
Matthew Qin49e51fa2015-02-09 10:40:45 +08001355 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001356 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001357 {
1358 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1359 return -1;
1360 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001361
Matthew Qin271927e2015-03-31 22:07:07 -04001362 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001363 } else {
1364 /* Validate the tags_addr */
1365 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001366 {
1367 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1368 return -1;
1369 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001370 /*
1371 * If appended dev tree is found, update the atags with
1372 * memory address to the DTB appended location on RAM.
1373 * Else update with the atags address in the kernel header
1374 */
1375 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001376 dtb = dev_tree_appended((void*)(image_addr + page_size),
1377 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001378 (void *)hdr->tags_addr);
1379 if (!dtb) {
1380 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001381 return -1;
1382 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001383 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001384 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001385
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001386 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1387 target_load_ssd_keystore();
1388
Shashank Mittal23b8f422010-04-16 19:27:21 -07001389unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001390
Dima Zavin77e41f32013-03-06 16:10:43 -08001391 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001392 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001393 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1394
1395 return 0;
1396}
1397
Dima Zavin214cc642009-01-26 11:16:21 -08001398int boot_linux_from_flash(void)
1399{
1400 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001401 struct ptentry *ptn;
1402 struct ptable *ptable;
1403 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001404
Shashank Mittalcd98d472011-08-02 14:29:24 -07001405 unsigned char *image_addr = 0;
1406 unsigned kernel_actual;
1407 unsigned ramdisk_actual;
1408 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301409 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001410
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001411#if DEVICE_TREE
1412 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001413 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301414 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001415 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001416 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301417 unsigned int dtb_size = 0;
1418 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001419#endif
1420
David Ng183a7422009-12-07 14:55:21 -08001421 if (target_is_emmc_boot()) {
1422 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1423 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1424 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1425 return -1;
1426 }
1427 goto continue_boot;
1428 }
1429
Dima Zavin214cc642009-01-26 11:16:21 -08001430 ptable = flash_get_ptable();
1431 if (ptable == NULL) {
1432 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1433 return -1;
1434 }
1435
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001436 if(!boot_into_recovery)
1437 {
1438 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001439
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001440 if (ptn == NULL) {
1441 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1442 return -1;
1443 }
1444 }
1445 else
1446 {
1447 ptn = ptable_find(ptable, "recovery");
1448 if (ptn == NULL) {
1449 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1450 return -1;
1451 }
Dima Zavin214cc642009-01-26 11:16:21 -08001452 }
1453
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001454 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001455 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1456 return -1;
1457 }
Dima Zavin214cc642009-01-26 11:16:21 -08001458
1459 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001460 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001461 return -1;
1462 }
1463
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001464 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001465 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 -08001466 return -1;
1467 }
1468
vijay kumar287bb542015-09-29 13:01:52 +05301469 /* ensure commandline is terminated */
1470 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1471
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001472 /*
1473 * Update the kernel/ramdisk/tags address if the boot image header
1474 * has default values, these default values come from mkbootimg when
1475 * the boot image is flashed using fastboot flash:raw
1476 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001477 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001478
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001479 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001480 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1481 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1482 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1483
1484 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1485 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1486
1487 /* Check if the addresses in the header are valid. */
1488 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1489 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
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001502
Shashank Mittalcd98d472011-08-02 14:29:24 -07001503 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001504 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001505 {
1506 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001507 offset = 0;
1508
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001509#if DEVICE_TREE
1510 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
vijay kumar966a9822015-12-09 18:36:09 +05301511
1512 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1513 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1514 return -1;
1515 }
1516
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001517 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001518
1519 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1520 {
1521 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1522 return -1;
1523 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001524#else
vijay kumar966a9822015-12-09 18:36:09 +05301525 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) {
1526 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1527 return -1;
1528 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001529 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001530#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001531
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001532 dprintf(INFO, "Loading (%s) image (%d): start\n",
1533 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001534 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001535
Shashank Mittalcd98d472011-08-02 14:29:24 -07001536 /* Read image without signature */
1537 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1538 {
1539 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1540 return -1;
1541 }
Dima Zavin214cc642009-01-26 11:16:21 -08001542
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001543 dprintf(INFO, "Loading (%s) image (%d): done\n",
1544 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001545 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001546
Shashank Mittalcd98d472011-08-02 14:29:24 -07001547 offset = imagesize_actual;
1548 /* Read signature */
1549 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1550 {
1551 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001552 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001553 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001554
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301555 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001556
1557 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001558 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1559 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001560#if DEVICE_TREE
vijay kumar8f53e362015-11-24 13:38:11 +05301561 if(hdr->dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001562
vijay kumar8f53e362015-11-24 13:38:11 +05301563 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1564
1565 table = (struct dt_table*) dt_table_offset;
1566
1567 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1568 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1569 return -1;
1570 }
1571
1572 /* Find index of device tree within device tree table */
1573 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1574 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1575 return -1;
1576 }
1577
1578 /* Validate and Read device device tree in the "tags_add */
1579 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){
1580 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1581 return -1;
1582 }
1583
1584 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1585 dtb_size = dt_entry.size;
1586 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1587 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001588#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001589
1590 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001591 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001592 {
1593 write_device_info_flash(&device);
1594 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301595#if USE_PCOM_SECBOOT
1596 set_tamper_flag(device.is_tampered);
1597#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001598 }
1599 else
1600 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001601 offset = page_size;
1602
Amol Jadib6be5c12012-11-14 13:39:51 -08001603 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1604 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1605 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1606
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001607 dprintf(INFO, "Loading (%s) image (%d): start\n",
1608 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1609
Amol Jadi492d5a52013-03-15 16:12:34 -07001610 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001611
1612 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001613 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1614 return -1;
1615 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001616 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001617
Amol Jadib6be5c12012-11-14 13:39:51 -08001618 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001619 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1620 return -1;
1621 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001622 offset += ramdisk_actual;
1623
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001624 dprintf(INFO, "Loading (%s) image (%d): done\n",
1625 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1626
Amol Jadi492d5a52013-03-15 16:12:34 -07001627 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001628
1629 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001630 offset += second_actual;
1631 /* Second image loading not implemented. */
1632 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001633 }
1634
1635#if DEVICE_TREE
1636 if(hdr->dt_size != 0) {
1637
1638 /* Read the device tree table into buffer */
1639 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1640 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1641 return -1;
1642 }
1643
1644 table = (struct dt_table*) dt_buf;
1645
Deepa Dinamani19648b42013-09-05 17:05:55 -07001646 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001647 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1648 return -1;
1649 }
1650
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301651 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1652 goes beyound hdr->dt_size*/
1653 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1654 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1655 return -1;
1656 }
1657
Deepa Dinamani19648b42013-09-05 17:05:55 -07001658 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1659 if (!table)
1660 return -1;
1661
1662 /* Read the entire device tree table into buffer */
1663 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1664 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1665 return -1;
1666 }
1667
1668
Joel Kingaa335dc2013-06-03 16:11:08 -07001669 /* Find index of device tree within device tree table */
1670 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001671 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1672 return -1;
1673 }
1674
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001675 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001676 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001677 {
1678 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1679 return -1;
1680 }
1681
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001682 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001683 if(flash_read(ptn, offset + dt_entry.offset,
1684 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001685 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1686 return -1;
1687 }
1688 }
1689#endif
1690
Shashank Mittalcd98d472011-08-02 14:29:24 -07001691 }
David Ng183a7422009-12-07 14:55:21 -08001692continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001693
Dima Zavin214cc642009-01-26 11:16:21 -08001694 /* TODO: create/pass atags to kernel */
1695
Ajay Dudanie28a6072011-07-01 13:59:46 -07001696 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001697 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001698 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1699
1700 return 0;
1701}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001702
Shashank Mittal162244e2011-08-08 19:01:25 -07001703void write_device_info_mmc(device_info *dev)
1704{
Shashank Mittal162244e2011-08-08 19:01:25 -07001705 unsigned long long ptn = 0;
1706 unsigned long long size;
1707 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001708 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001709 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001710 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001711
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001712 if (devinfo_present)
1713 index = partition_get_index("devinfo");
1714 else
1715 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001716
Shashank Mittal162244e2011-08-08 19:01:25 -07001717 ptn = partition_get_offset(index);
1718 if(ptn == 0)
1719 {
1720 return;
1721 }
1722
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001723 lun = partition_get_lun(index);
1724 mmc_set_lun(lun);
1725
Shashank Mittal162244e2011-08-08 19:01:25 -07001726 size = partition_get_size(index);
1727
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001728 blocksize = mmc_get_device_blocksize();
1729
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001730 if (devinfo_present)
1731 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1732 else
1733 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1734 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001735 {
1736 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001737 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001738 }
1739}
1740
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001741void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001742{
Shashank Mittal162244e2011-08-08 19:01:25 -07001743 unsigned long long ptn = 0;
1744 unsigned long long size;
1745 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001746 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001747 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001748
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001749 if ((index = partition_get_index("devinfo")) < 0)
1750 {
1751 devinfo_present = false;
1752 index = partition_get_index("aboot");
1753 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001754
Shashank Mittal162244e2011-08-08 19:01:25 -07001755 ptn = partition_get_offset(index);
1756 if(ptn == 0)
1757 {
1758 return;
1759 }
1760
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001761 mmc_set_lun(partition_get_lun(index));
1762
Shashank Mittal162244e2011-08-08 19:01:25 -07001763 size = partition_get_size(index);
1764
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001765 blocksize = mmc_get_device_blocksize();
1766
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001767 if (devinfo_present)
1768 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1769 else
1770 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1771 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001772 {
1773 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001774 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001775 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001776}
1777
1778void write_device_info_flash(device_info *dev)
1779{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001780 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001781 struct ptentry *ptn;
1782 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001783 if(info == NULL)
1784 {
1785 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1786 ASSERT(0);
1787 }
1788 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001789 ptable = flash_get_ptable();
1790 if (ptable == NULL)
1791 {
1792 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1793 return;
1794 }
1795
1796 ptn = ptable_find(ptable, "devinfo");
1797 if (ptn == NULL)
1798 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001799 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001800 return;
1801 }
1802
1803 memcpy(info, dev, sizeof(device_info));
1804
1805 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1806 {
1807 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1808 return;
1809 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001810 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001811}
1812
vijay kumarc65876c2015-04-24 13:29:16 +05301813static int read_allow_oem_unlock(device_info *dev)
1814{
vijay kumarc65876c2015-04-24 13:29:16 +05301815 unsigned offset;
1816 int index;
1817 unsigned long long ptn;
1818 unsigned long long ptn_size;
1819 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001820 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301821
vijay kumarca2e6812015-07-08 20:28:25 +05301822 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301823 if (index == INVALID_PTN)
1824 {
vijay kumarca2e6812015-07-08 20:28:25 +05301825 index = partition_get_index(frp_ptns[1]);
1826 if (index == INVALID_PTN)
1827 {
1828 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1829 return -1;
1830 }
vijay kumarc65876c2015-04-24 13:29:16 +05301831 }
1832
1833 ptn = partition_get_offset(index);
1834 ptn_size = partition_get_size(index);
1835 offset = ptn_size - blocksize;
1836
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001837 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301838 {
1839 dprintf(CRITICAL, "Reading MMC failed\n");
1840 return -1;
1841 }
1842
1843 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1844 is_allow_unlock = buf[blocksize-1] & 0x01;
1845 return 0;
1846}
1847
1848static int write_allow_oem_unlock(bool allow_unlock)
1849{
vijay kumarc65876c2015-04-24 13:29:16 +05301850 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301851 int index;
1852 unsigned long long ptn;
1853 unsigned long long ptn_size;
1854 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001855 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301856
vijay kumarca2e6812015-07-08 20:28:25 +05301857 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301858 if (index == INVALID_PTN)
1859 {
vijay kumarca2e6812015-07-08 20:28:25 +05301860 index = partition_get_index(frp_ptns[1]);
1861 if (index == INVALID_PTN)
1862 {
1863 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1864 return -1;
1865 }
vijay kumarc65876c2015-04-24 13:29:16 +05301866 }
1867
1868 ptn = partition_get_offset(index);
1869 ptn_size = partition_get_size(index);
1870 offset = ptn_size - blocksize;
1871
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001872 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301873 {
1874 dprintf(CRITICAL, "Reading MMC failed\n");
1875 return -1;
1876 }
1877
1878 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1879 buf[blocksize-1] = allow_unlock;
1880 if (mmc_write(ptn + offset, blocksize, buf))
1881 {
1882 dprintf(CRITICAL, "Writing MMC failed\n");
1883 return -1;
1884 }
1885
1886 return 0;
1887}
1888
Shashank Mittal162244e2011-08-08 19:01:25 -07001889void read_device_info_flash(device_info *dev)
1890{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001891 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001892 struct ptentry *ptn;
1893 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001894 if(info == NULL)
1895 {
1896 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1897 ASSERT(0);
1898 }
1899 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001900 ptable = flash_get_ptable();
1901 if (ptable == NULL)
1902 {
1903 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1904 return;
1905 }
1906
1907 ptn = ptable_find(ptable, "devinfo");
1908 if (ptn == NULL)
1909 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001910 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001911 return;
1912 }
1913
1914 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1915 {
1916 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1917 return;
1918 }
1919
1920 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1921 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001922 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1923 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001924 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001925 write_device_info_flash(info);
1926 }
1927 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001928 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001929}
1930
1931void write_device_info(device_info *dev)
1932{
1933 if(target_is_emmc_boot())
1934 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001935 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1936 if(info == NULL)
1937 {
1938 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1939 ASSERT(0);
1940 }
1941 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001942 memcpy(info, dev, sizeof(struct device_info));
1943
1944#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001945 if (is_secure_boot_enable()) {
1946 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1947 ASSERT(0);
1948 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001949 else
1950 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001951#else
1952 write_device_info_mmc(info);
1953#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001954 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001955 }
1956 else
1957 {
1958 write_device_info_flash(dev);
1959 }
1960}
1961
1962void read_device_info(device_info *dev)
1963{
1964 if(target_is_emmc_boot())
1965 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001966 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1967 if(info == NULL)
1968 {
1969 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1970 ASSERT(0);
1971 }
1972 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001973
1974#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001975 if (is_secure_boot_enable()) {
1976 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1977 ASSERT(0);
1978 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001979 else
1980 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001981#else
1982 read_device_info_mmc(info);
1983#endif
1984
1985 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1986 {
1987 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08001988 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001989 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301990#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08001991 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301992#endif
lijuang511a2b52015-08-14 20:50:51 +08001993 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001994 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05301995#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08001996 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05301997#endif
lijuang511a2b52015-08-14 20:50:51 +08001998 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001999 info->is_tampered = 0;
2000 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302001#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07002002 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302003#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002004 write_device_info(info);
2005 }
2006 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002007 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002008 }
2009 else
2010 {
2011 read_device_info_flash(dev);
2012 }
2013}
2014
2015void reset_device_info()
2016{
2017 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002018 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002019 write_device_info(&device);
2020}
2021
2022void set_device_root()
2023{
2024 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002025 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002026 write_device_info(&device);
2027}
2028
lijuang4ece1e72015-08-14 21:02:36 +08002029/* set device unlock value
2030 * Must check FRP before call this function
2031 * Need to wipe data when unlock status changed
2032 * type 0: oem unlock
2033 * type 1: unlock critical
2034 * status 0: unlock as false
2035 * status 1: lock as true
2036 */
2037void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002038{
lijuang4ece1e72015-08-14 21:02:36 +08002039 if (type == UNLOCK)
2040 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302041#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002042 else if (type == UNLOCK_CRITICAL)
2043 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302044#endif
lijuang4ece1e72015-08-14 21:02:36 +08002045 write_device_info(&device);
2046}
2047
2048static void set_device_unlock(int type, bool status)
2049{
2050 int is_unlocked = -1;
2051 char response[MAX_RSP_SIZE];
2052
2053 /* check device unlock status if it is as expected */
2054 if (type == UNLOCK)
2055 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05302056#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002057 else if (type == UNLOCK_CRITICAL)
2058 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05302059#endif
lijuang4ece1e72015-08-14 21:02:36 +08002060 if (is_unlocked == status) {
2061 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2062 fastboot_info(response);
2063 fastboot_okay("");
2064 return;
2065 }
2066
2067 /* status is true, it means to unlock device */
2068 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002069 if(!is_allow_unlock) {
2070 fastboot_fail("oem unlock is not allowed");
2071 return;
2072 }
2073
lijuang4ece1e72015-08-14 21:02:36 +08002074#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002075 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002076 fastboot_okay("");
2077 return;
2078#else
2079 if (type == UNLOCK) {
2080 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2081 return;
2082 }
2083#endif
lijuang21f12f52015-08-22 16:22:19 +08002084 }
lijuang4ece1e72015-08-14 21:02:36 +08002085
2086 set_device_unlock_value(type, status);
2087
2088 /* wipe data */
2089 struct recovery_message msg;
2090
2091 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2092 write_misc(0, &msg, sizeof(msg));
2093
2094 fastboot_okay("");
2095 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002096}
2097
lijuang511a2b52015-08-14 20:50:51 +08002098static bool critical_flash_allowed(const char * entry)
2099{
2100 uint32_t i = 0;
2101 if (entry == NULL)
2102 return false;
2103
2104 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2105 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2106 return true;
2107 }
2108 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002109}
2110
2111#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002112int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2113{
2114 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002115 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002116 struct dt_table *table;
2117 struct dt_entry dt_entry;
2118 uint32_t dt_hdr_size;
2119 unsigned int compressed_size = 0;
2120 unsigned int dtb_size = 0;
2121 unsigned int out_avai_len = 0;
2122 unsigned char *out_addr = NULL;
2123 unsigned char *best_match_dt_addr = NULL;
2124 int rc;
2125
2126 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2127
2128 if(hdr->dt_size != 0) {
2129 /* add kernel offset */
2130 dt_image_offset += page_size;
2131 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002132 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002133
Deepa Dinamani19648b42013-09-05 17:05:55 -07002134 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002135 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2136 dt_image_offset += n;
2137
Joel Kingaa335dc2013-06-03 16:11:08 -07002138 /* add second offset */
2139 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002140 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2141 dt_image_offset += n;
2142 }
2143
Matthew Qin271927e2015-03-31 22:07:07 -04002144 /* offset now point to start of dt.img */
2145 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2146
2147 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2148 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002149 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002150 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302151
2152 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2153 goes beyound hdr->dt_size*/
2154 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
2155 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2156 return -1;
2157 }
2158
Amol Jadicb524072012-08-09 16:40:18 -07002159 /* Find index of device tree within device tree table */
2160 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2161 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2162 return -1;
2163 }
2164
Matthew Qin271927e2015-03-31 22:07:07 -04002165 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2166 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2167 {
2168 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2169 out_avai_len = target_get_max_flash_size() - scratch_offset;
2170 dprintf(INFO, "decompressing dtb: start\n");
2171 rc = decompress(best_match_dt_addr,
2172 dt_entry.size, out_addr, out_avai_len,
2173 &compressed_size, &dtb_size);
2174 if (rc)
2175 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002176 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002177 ASSERT(0);
2178 }
2179
Matthew Qin0b15b322015-05-19 05:20:54 -04002180 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002181 best_match_dt_addr = out_addr;
2182 } else {
2183 dtb_size = dt_entry.size;
2184 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002185 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002186 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002187 {
2188 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
2189 return -1;
2190 }
2191
Amol Jadicb524072012-08-09 16:40:18 -07002192 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002193 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002194 } else
2195 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002196
2197 /* Everything looks fine. Return success. */
2198 return 0;
2199}
2200#endif
2201
Brian Swetland9c4c0752009-01-25 16:23:50 -08002202void cmd_boot(const char *arg, void *data, unsigned sz)
2203{
Amit Blay8a510302015-08-17 09:20:01 +03002204#ifdef MDTP_SUPPORT
2205 static bool is_mdtp_activated = 0;
2206#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002207 unsigned kernel_actual;
2208 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002209 uint32_t image_actual;
2210 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302211 uint32_t sig_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002212 struct boot_img_hdr *hdr = NULL;
2213 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002214 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002215 int ret = 0;
2216 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002217 unsigned int out_len = 0;
2218 unsigned int out_avai_len = 0;
2219 unsigned char *out_addr = NULL;
2220 uint32_t dtb_offset = 0;
2221 unsigned char *kernel_start_addr = NULL;
2222 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002223 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002224
lijuang2008ff22016-03-07 17:56:27 +08002225#if FBCON_DISPLAY_MSG
2226 /* Exit keys' detection thread firstly */
2227 exit_menu_keys_detection();
2228#endif
2229
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002230#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002231 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002232 {
2233 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002234 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002235 }
2236#endif
2237
Brian Swetland9c4c0752009-01-25 16:23:50 -08002238 if (sz < sizeof(hdr)) {
2239 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002240 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002241 }
2242
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002243 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002244
2245 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002246 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002247
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002248 if(target_is_emmc_boot() && hdr->page_size) {
2249 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002250 page_mask = page_size - 1;
2251 }
2252
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002253 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2254 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002255#if DEVICE_TREE
2256 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2257#endif
2258
2259 image_actual = ADD_OF(page_size, kernel_actual);
2260 image_actual = ADD_OF(image_actual, ramdisk_actual);
2261 image_actual = ADD_OF(image_actual, dt_actual);
2262
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302263 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2264 /* Calculate the signature length from boot image */
2265 sig_actual = read_der_message_length(
2266 (unsigned char*)(data + image_actual),sz);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002267 image_actual = ADD_OF(image_actual, sig_actual);
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302268 }
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002269
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002270 /* sz should have atleast raw boot image */
2271 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002272 fastboot_fail("bootimage: incomplete or not signed");
lijuang2008ff22016-03-07 17:56:27 +08002273 goto boot_failed;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002274 }
2275
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002276 // Initialize boot state before trying to verify boot.img
2277#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002278 boot_verifier_init();
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002279 /* Handle overflow if the input image size is greater than
2280 * boot image buffer can hold
2281 */
2282 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2283 {
2284 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002285 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002286 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002287#endif
2288
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002289 /* Verify the boot image
2290 * device & page_size are initialized in aboot_init
2291 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002292 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002293 /* Pass size excluding signature size, otherwise we would try to
2294 * access signature beyond its length
2295 */
2296 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002297 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002298#ifdef MDTP_SUPPORT
2299 else
2300 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002301 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002302 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002303
2304 if (!is_mdtp_activated) {
2305 ext_partition.partition = MDTP_PARTITION_NONE;
2306 mdtp_fwlock_verify_lock(&ext_partition);
2307 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002308 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002309
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002310 mdtp_activated(&is_mdtp_activated);
2311 if(is_mdtp_activated){
2312 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002313 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002314 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002315#endif /* MDTP_SUPPORT */
2316
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002317#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302318#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002319 // send root of trust
2320 if(!send_rot_command((uint32_t)device.is_unlocked))
2321 ASSERT(0);
2322#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302323#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002324 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002325 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2326 * If not, continue booting.
2327 */
2328 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2329 {
2330 out_addr = (unsigned char *)target_get_scratch_address();
2331 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2332 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002333 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002334 ret = decompress((unsigned char *)(ptr + page_size),
2335 hdr->kernel_size, out_addr, out_avai_len,
2336 &dtb_offset, &out_len);
2337 if (ret)
2338 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002339 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002340 ASSERT(0);
2341 }
2342
Matthew Qin0b15b322015-05-19 05:20:54 -04002343 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002344 kptr = (struct kernel64_hdr *)out_addr;
2345 kernel_start_addr = out_addr;
2346 kernel_size = out_len;
2347 } else {
2348 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2349 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2350 kernel_size = hdr->kernel_size;
2351 }
2352
2353 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002354 * Update the kernel/ramdisk/tags address if the boot image header
2355 * has default values, these default values come from mkbootimg when
2356 * the boot image is flashed using fastboot flash:raw
2357 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002358 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002359
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002360 /* Get virtual addresses since the hdr saves physical addresses. */
2361 hdr->kernel_addr = VA(hdr->kernel_addr);
2362 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2363 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002364
Matthew Qinbb7923d2015-02-09 10:56:09 +08002365 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002366 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002367 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002368 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2369 {
2370 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002371 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002372 }
2373
Amol Jadicb524072012-08-09 16:40:18 -07002374#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002375 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002376 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002377 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002378
2379 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002380#else
2381 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2382 {
2383 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002384 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002385 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002386#endif
2387
2388 /* Load ramdisk & kernel */
2389 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002390 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002391
2392#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002393 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2394 {
2395 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002396 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002397 }
2398
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002399 /*
2400 * If dtb is not found look for appended DTB in the kernel.
2401 * If appended dev tree is found, update the atags with
2402 * memory address to the DTB appended location on RAM.
2403 * Else update with the atags address in the kernel header
2404 */
2405 if (!dtb_copied) {
2406 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002407 dtb = dev_tree_appended((void*)(ptr + page_size),
2408 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002409 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002410 if (!dtb) {
2411 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002412 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002413 }
Amol Jadicb524072012-08-09 16:40:18 -07002414 }
2415#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002416
2417 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002418 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002419
Dima Zavin77e41f32013-03-06 16:10:43 -08002420 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002421 (const char*) hdr->cmdline, board_machtype(),
2422 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002423
2424 /* fastboot already stop, it's no need to show fastboot menu */
2425 return;
2426boot_failed:
2427#if FBCON_DISPLAY_MSG
2428 /* revert to fastboot menu if boot failed */
2429 display_fastboot_menu();
2430#endif
2431 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002432}
2433
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002434void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002435{
2436 struct ptentry *ptn;
2437 struct ptable *ptable;
2438
2439 ptable = flash_get_ptable();
2440 if (ptable == NULL) {
2441 fastboot_fail("partition table doesn't exist");
2442 return;
2443 }
2444
2445 ptn = ptable_find(ptable, arg);
2446 if (ptn == NULL) {
2447 fastboot_fail("unknown partition name");
2448 return;
2449 }
2450
2451 if (flash_erase(ptn)) {
2452 fastboot_fail("failed to erase partition");
2453 return;
2454 }
2455 fastboot_okay("");
2456}
2457
Bikas Gurungd48bd242010-09-04 19:54:32 -07002458
2459void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2460{
2461 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002462 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002463 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002464 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002465
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002466#if VERIFIED_BOOT
2467 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2468 {
2469 if(!device.is_unlocked)
2470 {
2471 fastboot_fail("unlock device to erase keystore");
2472 return;
2473 }
2474 }
2475#endif
2476
Kinson Chikf1a43512011-07-14 11:28:39 -07002477 index = partition_get_index(arg);
2478 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002479 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002480
Kinson Chikf1a43512011-07-14 11:28:39 -07002481 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002482 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002483 return;
2484 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002485
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002486 lun = partition_get_lun(index);
2487 mmc_set_lun(lun);
2488
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002489 if (platform_boot_dev_isemmc())
2490 {
2491 if (mmc_erase_card(ptn, size)) {
2492 fastboot_fail("failed to erase partition\n");
2493 return;
2494 }
2495 } else {
2496 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2497 size = partition_get_size(index);
2498 if (size > DEFAULT_ERASE_SIZE)
2499 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002500
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002501 /* Simple inefficient version of erase. Just writing
2502 0 in first several blocks */
2503 if (mmc_write(ptn , size, (unsigned int *)out)) {
2504 fastboot_fail("failed to erase partition");
2505 return;
2506 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002507 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002508#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302509#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002510 if(!(strncmp(arg, "userdata", 8)))
2511 if(send_delete_keys_to_tz())
2512 ASSERT(0);
2513#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302514#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002515 fastboot_okay("");
2516}
2517
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002518void cmd_erase(const char *arg, void *data, unsigned sz)
2519{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002520#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002521 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002522 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002523 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002524 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002525 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002526 return;
2527 }
2528 }
2529#endif
2530
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002531 if(target_is_emmc_boot())
2532 cmd_erase_mmc(arg, data, sz);
2533 else
2534 cmd_erase_nand(arg, data, sz);
2535}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002536
Channagoud Kadabiad259832015-05-29 11:14:17 -07002537static uint32_t aboot_get_secret_key()
2538{
2539 /* 0 is invalid secret key, update this implementation to return
2540 * device specific unique secret key
2541 */
2542 return 0;
2543}
2544
Ajay Dudani5c761132011-04-07 20:19:04 -07002545void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002546{
2547 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002548 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002549 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002550 char *token = NULL;
2551 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002552 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002553 uint8_t lun = 0;
2554 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002555
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002556 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002557 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002558 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002559 if(token)
2560 {
2561 lun = atoi(token);
2562 mmc_set_lun(lun);
2563 lun_set = true;
2564 }
2565
Mao Jinlong226f33a2014-07-04 17:24:10 +08002566 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002567 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002568 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2569 {
2570 if (!aboot_frp_unlock(pname, data, sz))
2571 {
2572 fastboot_info("FRP unlock successful");
2573 fastboot_okay("");
2574 }
2575 else
2576 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2577
2578 return;
2579 }
2580
Mao Jinlong226f33a2014-07-04 17:24:10 +08002581 if (!strcmp(pname, "partition"))
2582 {
2583 dprintf(INFO, "Attempt to write partition image.\n");
2584 if (write_partition(sz, (unsigned char *) data)) {
2585 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002586 return;
2587 }
2588 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002589 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002590 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002591#if VERIFIED_BOOT
2592 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2593 {
2594 if(!device.is_unlocked)
2595 {
2596 fastboot_fail("unlock device to flash keystore");
2597 return;
2598 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302599 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002600 {
2601 fastboot_fail("image is not a keystore file");
2602 return;
2603 }
2604 }
2605#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002606 index = partition_get_index(pname);
2607 ptn = partition_get_offset(index);
2608 if(ptn == 0) {
2609 fastboot_fail("partition table doesn't exist");
2610 return;
2611 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002612
Mao Jinlong226f33a2014-07-04 17:24:10 +08002613 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2614 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2615 fastboot_fail("image is not a boot image");
2616 return;
2617 }
2618 }
2619
2620 if(!lun_set)
2621 {
2622 lun = partition_get_lun(index);
2623 mmc_set_lun(lun);
2624 }
2625
2626 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05302627 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002628 fastboot_fail("size too large");
2629 return;
2630 }
2631 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2632 fastboot_fail("flash write failure");
2633 return;
2634 }
Greg Grisco6e754772011-06-23 12:19:39 -07002635 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002636 }
2637 fastboot_okay("");
2638 return;
2639}
2640
Ajay Dudanide984792015-03-02 09:57:41 -08002641void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2642{
2643 int i, images;
2644 meta_header_t *meta_header;
2645 img_header_entry_t *img_header_entry;
2646
2647 meta_header = (meta_header_t*) data;
2648 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2649
2650 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2651
2652 for (i=0; i<images; i++) {
2653
2654 if((img_header_entry[i].ptn_name == NULL) ||
2655 (img_header_entry[i].start_offset == 0) ||
2656 (img_header_entry[i].size == 0))
2657 break;
2658
2659 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2660 (void *) data + img_header_entry[i].start_offset,
2661 img_header_entry[i].size);
2662 }
2663
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002664 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2665 {
2666 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2667 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2668 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2669 }
2670 else
2671 {
2672 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2673 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2674 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2675 }
2676
2677 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002678 fastboot_okay("");
2679 return;
2680}
2681
Ajay Dudani5c761132011-04-07 20:19:04 -07002682void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2683{
2684 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002685 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002686 uint32_t *fill_buf = NULL;
2687 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002688 sparse_header_t *sparse_header;
2689 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002690 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002691 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302692 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002693 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302694 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002695 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302696 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05302697 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002698
Kinson Chikf1a43512011-07-14 11:28:39 -07002699 index = partition_get_index(arg);
2700 ptn = partition_get_offset(index);
2701 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002702 fastboot_fail("partition table doesn't exist");
2703 return;
2704 }
2705
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302706 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302707
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002708 lun = partition_get_lun(index);
2709 mmc_set_lun(lun);
2710
vijay kumar800255e2015-04-24 20:26:19 +05302711 if (sz < sizeof(sparse_header_t)) {
2712 fastboot_fail("size too low");
2713 return;
2714 }
2715
Ajay Dudani5c761132011-04-07 20:19:04 -07002716 /* Read and skip over sparse image header */
2717 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302718
vijay kumar1321f342015-03-27 12:13:42 +05302719 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002720 fastboot_fail("size too large");
2721 return;
2722 }
2723
vijay kumarde4fcf62015-04-23 13:05:49 +05302724 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302725
Parth Dixit64b1a482016-03-07 16:31:26 +05302726 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302727 fastboot_fail("buffer overreads occured due to invalid sparse header");
2728 return;
2729 }
2730
vijay kumarde4fcf62015-04-23 13:05:49 +05302731 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002732 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302733 fastboot_fail("sparse header size mismatch");
2734 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002735 }
2736
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002737 dprintf (SPEW, "=== Sparse Image Header ===\n");
2738 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2739 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2740 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2741 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2742 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2743 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2744 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2745 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002746
2747 /* Start processing chunks */
2748 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2749 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302750 /* Make sure the total image size does not exceed the partition size */
2751 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2752 fastboot_fail("size too large");
2753 return;
2754 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002755 /* Read and skip over chunk header */
2756 chunk_header = (chunk_header_t *) data;
2757 data += sizeof(chunk_header_t);
2758
Parth Dixit64b1a482016-03-07 16:31:26 +05302759 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302760 fastboot_fail("buffer overreads occured due to invalid sparse header");
2761 return;
2762 }
2763
Ajay Dudani5c761132011-04-07 20:19:04 -07002764 dprintf (SPEW, "=== Chunk Header ===\n");
2765 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2766 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2767 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2768
vijay kumar800255e2015-04-24 20:26:19 +05302769 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002770 {
vijay kumar800255e2015-04-24 20:26:19 +05302771 fastboot_fail("chunk header size mismatch");
2772 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002773 }
2774
wufeng.jiang813dc352015-06-02 23:02:46 -04002775 if (!sparse_header->blk_sz ){
2776 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302777 return;
2778 }
2779
wufeng.jiang813dc352015-06-02 23:02:46 -04002780 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2781
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302782 /* Make sure that the chunk size calculated from sparse image does not
2783 * exceed partition size
2784 */
2785 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2786 {
2787 fastboot_fail("Chunk data size exceeds partition size");
2788 return;
2789 }
2790
Ajay Dudani5c761132011-04-07 20:19:04 -07002791 switch (chunk_header->chunk_type)
2792 {
2793 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002794 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002795 chunk_data_sz))
2796 {
2797 fastboot_fail("Bogus chunk size for chunk type Raw");
2798 return;
2799 }
2800
Parth Dixit64b1a482016-03-07 16:31:26 +05302801 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05302802 fastboot_fail("buffer overreads occured due to invalid sparse header");
2803 return;
2804 }
2805
wufeng.jiang813dc352015-06-02 23:02:46 -04002806 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2807 otherwise it will return in the line above
2808 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002809 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002810 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002811 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002812 {
2813 fastboot_fail("flash write failure");
2814 return;
2815 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302816 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2817 fastboot_fail("Bogus size for RAW chunk type");
2818 return;
2819 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002820 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002821 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002822 break;
2823
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002824 case CHUNK_TYPE_FILL:
2825 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2826 sizeof(uint32_t)))
2827 {
2828 fastboot_fail("Bogus chunk size for chunk type FILL");
2829 return;
2830 }
2831
2832 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2833 if (!fill_buf)
2834 {
2835 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2836 return;
2837 }
2838
Parth Dixit64b1a482016-03-07 16:31:26 +05302839 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05302840 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05302841 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05302842 return;
2843 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002844 fill_val = *(uint32_t *)data;
2845 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002846
2847 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2848 {
2849 fill_buf[i] = fill_val;
2850 }
2851
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05302852 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
2853 {
2854 fastboot_fail("bogus size for chunk FILL type");
2855 free(fill_buf);
2856 return;
2857 }
2858
wufeng.jiang813dc352015-06-02 23:02:46 -04002859 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002860 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302861 /* Make sure that the data written to partition does not exceed partition size */
2862 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2863 {
2864 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05302865 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302866 return;
2867 }
2868
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002869 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2870 sparse_header->blk_sz,
2871 fill_buf))
2872 {
2873 fastboot_fail("flash write failure");
2874 free(fill_buf);
2875 return;
2876 }
2877
2878 total_blocks++;
2879 }
2880
2881 free(fill_buf);
2882 break;
2883
Ajay Dudani5c761132011-04-07 20:19:04 -07002884 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302885 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2886 fastboot_fail("bogus size for chunk DONT CARE type");
2887 return;
2888 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002889 total_blocks += chunk_header->chunk_sz;
2890 break;
2891
Ajay Dudani5c761132011-04-07 20:19:04 -07002892 case CHUNK_TYPE_CRC:
2893 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2894 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002895 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002896 return;
2897 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302898 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2899 fastboot_fail("bogus size for chunk CRC type");
2900 return;
2901 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002902 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05302903 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05302904 fastboot_fail("integer overflow occured");
2905 return;
2906 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002907 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05302908 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302909 fastboot_fail("buffer overreads occured due to invalid sparse header");
2910 return;
2911 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002912 break;
2913
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002914 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002915 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002916 fastboot_fail("Unknown chunk type");
2917 return;
2918 }
2919 }
2920
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002921 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2922 total_blocks, sparse_header->total_blks);
2923
2924 if(total_blocks != sparse_header->total_blks)
2925 {
2926 fastboot_fail("sparse image write failure");
2927 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002928
2929 fastboot_okay("");
2930 return;
2931}
2932
2933void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2934{
2935 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002936 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002937
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002938#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002939 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2940 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002941 int ret=0;
2942 uint32 major_version=0;
2943 uint32 minor_version=0;
2944
2945 ret = scm_svc_version(&major_version,&minor_version);
2946 if(!ret)
2947 {
2948 if(major_version >= 2)
2949 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002950 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002951 {
2952 ret = encrypt_scm((uint32 **) &data, &sz);
2953 if (ret != 0) {
2954 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2955 return;
2956 }
2957
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002958 /* Protect only for SSD */
2959 if (!strcmp(arg, "ssd")) {
2960 ret = scm_protect_keystore((uint32 *) data, sz);
2961 if (ret != 0) {
2962 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2963 return;
2964 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002965 }
2966 }
2967 else
2968 {
2969 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2970 if(ret != 0)
2971 {
2972 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2973 return;
2974 }
2975 }
2976 }
2977 else
2978 {
2979 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2980 magic_number[1] == DECRYPT_MAGIC_1)
2981 {
2982 ret = decrypt_scm((uint32 **) &data, &sz);
2983 if (ret != 0) {
2984 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2985 return;
2986 }
2987 }
2988 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2989 magic_number[1] == ENCRYPT_MAGIC_1)
2990 {
2991 ret = encrypt_scm((uint32 **) &data, &sz);
2992 if (ret != 0) {
2993 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2994 return;
2995 }
2996 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002997 }
2998 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002999 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003000 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003001 dprintf(CRITICAL,"INVALID SVC Version\n");
3002 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003003 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003004#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003005
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003006#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003007 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003008 {
lijuang511a2b52015-08-14 20:50:51 +08003009 /* if device is locked:
3010 * common partition will not allow to be flashed
3011 * critical partition will allow to flash image.
3012 */
3013 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3014 fastboot_fail("Partition flashing is not allowed");
3015 return;
3016 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303017#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003018 /* if device critical is locked:
3019 * common partition will allow to be flashed
3020 * critical partition will not allow to flash image.
3021 */
3022 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
3023 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003024 return;
3025 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303026#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003027 }
3028#endif
3029
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003030 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003031 meta_header = (meta_header_t *) data;
3032 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003033 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003034 else if (meta_header->magic == META_HEADER_MAGIC)
3035 cmd_flash_meta_img(arg, data, sz);
3036 else
3037 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003038
3039#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303040#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003041 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
3042 {
3043 // reset dm_verity mode to enforcing
3044 device.verity_mode = 1;
3045 write_device_info(&device);
3046 }
3047#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303048#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003049
Ajay Dudani5c761132011-04-07 20:19:04 -07003050 return;
3051}
3052
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003053void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3054{
3055 struct ptentry *sys_ptn;
3056 struct ptable *ptable;
3057
3058 ptable = flash_get_ptable();
3059 if (ptable == NULL) {
3060 fastboot_fail("partition table doesn't exist");
3061 return;
3062 }
3063
3064 sys_ptn = ptable_find(ptable, "system");
3065 if (sys_ptn == NULL) {
3066 fastboot_fail("system partition not found");
3067 return;
3068 }
3069
3070 sz = ROUND_TO_PAGE(sz, page_mask);
3071 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3072 fastboot_fail("update_ubi_vol failed");
3073 else
3074 fastboot_okay("");
3075}
3076
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003077void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003078{
3079 struct ptentry *ptn;
3080 struct ptable *ptable;
3081 unsigned extra = 0;
3082
3083 ptable = flash_get_ptable();
3084 if (ptable == NULL) {
3085 fastboot_fail("partition table doesn't exist");
3086 return;
3087 }
3088
3089 ptn = ptable_find(ptable, arg);
3090 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003091 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3092 arg);
3093 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003094 return;
3095 }
3096
3097 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
3098 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3099 fastboot_fail("image is not a boot image");
3100 return;
3101 }
3102 }
3103
Amol Jadi5c61a952012-05-04 17:05:35 -07003104 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003105 || !strcmp(ptn->name, "userdata")
3106 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003107 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003108 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003109 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003110 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003111 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003112
3113 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003114 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3115 if (flash_ubi_img(ptn, data, sz)) {
3116 fastboot_fail("flash write failure");
3117 return;
3118 }
3119 } else {
3120 if (flash_write(ptn, extra, data, sz)) {
3121 fastboot_fail("flash write failure");
3122 return;
3123 }
Dima Zavin214cc642009-01-26 11:16:21 -08003124 }
3125 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3126 fastboot_okay("");
3127}
3128
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003129void cmd_flash(const char *arg, void *data, unsigned sz)
3130{
3131 if(target_is_emmc_boot())
3132 cmd_flash_mmc(arg, data, sz);
3133 else
3134 cmd_flash_nand(arg, data, sz);
3135}
3136
Dima Zavin214cc642009-01-26 11:16:21 -08003137void cmd_continue(const char *arg, void *data, unsigned sz)
3138{
3139 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003140 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003141
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003142 if (target_is_emmc_boot())
3143 {
lijuanga40d6302015-07-20 20:10:13 +08003144#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003145 /* Exit keys' detection thread firstly */
3146 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003147#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003148 boot_linux_from_mmc();
3149 }
3150 else
3151 {
3152 boot_linux_from_flash();
3153 }
Dima Zavin214cc642009-01-26 11:16:21 -08003154}
3155
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003156void cmd_reboot(const char *arg, void *data, unsigned sz)
3157{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003158 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003159 fastboot_okay("");
3160 reboot_device(0);
3161}
3162
3163void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3164{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003165 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003166 fastboot_okay("");
3167 reboot_device(FASTBOOT_MODE);
3168}
3169
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003170void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3171{
3172 dprintf(INFO, "Enabling charger screen check\n");
3173 device.charger_screen_enabled = 1;
3174 write_device_info(&device);
3175 fastboot_okay("");
3176}
3177
3178void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3179{
3180 dprintf(INFO, "Disabling charger screen check\n");
3181 device.charger_screen_enabled = 0;
3182 write_device_info(&device);
3183 fastboot_okay("");
3184}
3185
lijuanga25d8bb2015-09-16 13:11:52 +08003186void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3187{
3188 char *p = NULL;
3189 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303190 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003191
3192 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303193 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003194 if (p) {
3195 if (!strncmp(p, "0", 1)) {
3196 device.charger_screen_enabled = 0;
3197 } else if (!strncmp(p, "1", 1)) {
3198 device.charger_screen_enabled = 1;
3199 }
3200 }
3201 }
3202
3203 /* update charger_screen_enabled value for getvar
3204 * command
3205 */
3206 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3207 device.charger_screen_enabled);
3208
3209 write_device_info(&device);
3210 fastboot_okay("");
3211}
3212
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303213void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3214{
3215 dprintf(INFO, "Selecting display panel %s\n", arg);
3216 if (arg)
3217 strlcpy(device.display_panel, arg,
3218 sizeof(device.display_panel));
3219 write_device_info(&device);
3220 fastboot_okay("");
3221}
3222
Shashank Mittal162244e2011-08-08 19:01:25 -07003223void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3224{
lijuang4ece1e72015-08-14 21:02:36 +08003225 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303226}
3227
3228void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3229{
lijuang4ece1e72015-08-14 21:02:36 +08003230 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303231 if(!is_allow_unlock) {
3232 fastboot_fail("oem unlock is not allowed");
3233 return;
3234 }
3235
lijuang4ece1e72015-08-14 21:02:36 +08003236 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303237
lijuang4ece1e72015-08-14 21:02:36 +08003238 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303239 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003240
vijay kumarc65876c2015-04-24 13:29:16 +05303241 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3242 write_misc(0, &msg, sizeof(msg));
3243
3244 fastboot_okay("");
3245 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003246 }
3247 fastboot_okay("");
3248}
3249
Channagoud Kadabiad259832015-05-29 11:14:17 -07003250static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3251{
3252 int ret = 1;
3253 uint32_t secret_key;
3254 char seckey_buffer[MAX_RSP_SIZE];
3255
3256 secret_key = aboot_get_secret_key();
3257 if (secret_key)
3258 {
3259 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3260 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3261 {
3262 is_allow_unlock = true;
3263 write_allow_oem_unlock(is_allow_unlock);
3264 ret = 0;
3265 }
3266 }
3267 return ret;
3268}
3269
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003270void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3271{
lijuang4ece1e72015-08-14 21:02:36 +08003272 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003273}
3274
Shashank Mittala0032282011-08-26 14:50:11 -07003275void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3276{
lijuang511a2b52015-08-14 20:50:51 +08003277 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003278 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003279 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003280 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3281 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303282#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003283 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3284 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303285#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003286 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003287 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303288 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3289 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003290 fastboot_okay("");
3291}
3292
lijuang511a2b52015-08-14 20:50:51 +08003293void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3294{
3295 char response[MAX_RSP_SIZE];
3296 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3297 fastboot_info(response);
3298 fastboot_okay("");
3299}
3300
3301void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3302{
lijuang4ece1e72015-08-14 21:02:36 +08003303 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003304}
3305
3306void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3307{
lijuang4ece1e72015-08-14 21:02:36 +08003308 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003309}
3310
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003311void cmd_preflash(const char *arg, void *data, unsigned sz)
3312{
3313 fastboot_okay("");
3314}
3315
Mao Flynn7b379f32015-04-20 00:28:30 +08003316static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303317
Mao Flynn7b379f32015-04-20 00:28:30 +08003318int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303319{
Mao Flynn7b379f32015-04-20 00:28:30 +08003320 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303321 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003322 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303323 return -1;
3324 return 0;
3325}
3326
Mao Flynn7b379f32015-04-20 00:28:30 +08003327int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003328{
3329 struct ptentry *ptn;
3330 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003331 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003332 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003333
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303334 ptable = flash_get_ptable();
3335 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003336 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3337 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303338 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003339
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303340 ptn = ptable_find(ptable, "splash");
3341 if (ptn == NULL) {
3342 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003343 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303344 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003345 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303346 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003347 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303348 }
3349
Mao Flynn7b379f32015-04-20 00:28:30 +08003350 header = (struct logo_img_header *)logo_header;
3351 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303352 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003353 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303354 }
3355
3356 fb_display = fbcon_display();
3357 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003358 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3359 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3360
3361 /* if the logo is full-screen size, remove "fbcon_clear()" */
3362 if ((header->width != fb_display->width)
3363 || (header->height != fb_display->height))
3364 fbcon_clear();
3365
3366 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3367 (uint32_t *)base,
3368 (header->blocks * 512))) {
3369 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3370 return -1;
3371 }
3372 fbcon_extract_to_screen(header, base);
3373 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003374 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003375
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003376 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3377 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003378 return -1;
3379 }
3380
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303381 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003382 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3383 (uint32_t *)base,
3384 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303385 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303386 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003387 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003388 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303389 }
3390
Mao Flynn7b379f32015-04-20 00:28:30 +08003391 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303392}
3393
Mao Flynn7b379f32015-04-20 00:28:30 +08003394int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303395{
3396 int index = INVALID_PTN;
3397 unsigned long long ptn = 0;
3398 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003399 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003400 uint32_t blocksize, realsize, readsize;
3401 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303402
3403 index = partition_get_index("splash");
3404 if (index == 0) {
3405 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003406 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303407 }
3408
3409 ptn = partition_get_offset(index);
3410 if (ptn == 0) {
3411 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003412 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303413 }
3414
Mao Flynn1893a7f2015-06-03 12:03:36 +08003415 mmc_set_lun(partition_get_lun(index));
3416
3417 blocksize = mmc_get_device_blocksize();
3418 if (blocksize == 0) {
3419 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3420 return -1;
3421 }
3422
3423 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003424 if (!fb_display)
3425 {
3426 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3427 return -1;
3428 }
3429
Mao Flynn1893a7f2015-06-03 12:03:36 +08003430 base = (uint8_t *) fb_display->base;
3431
3432 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303433 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003434 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303435 }
3436
Mao Flynn1893a7f2015-06-03 12:03:36 +08003437 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003438 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303439 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003440 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303441 }
3442
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303443 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003444 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3445 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003446
Mao Flynn1893a7f2015-06-03 12:03:36 +08003447 realsize = header->blocks * 512;
3448 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3449
3450 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003451 if ((header->width != fb_display->width)
3452 || (header->height != fb_display->height))
3453 fbcon_clear();
3454
Mao Flynn1893a7f2015-06-03 12:03:36 +08003455 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003456 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3457 return -1;
3458 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003459
Mao Flynn1893a7f2015-06-03 12:03:36 +08003460 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3461 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303462
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003463 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3464 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003465 return -1;
3466 }
3467
3468 realsize = header->width * header->height * fb_display->bpp / 8;
3469 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3470
3471 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3472 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3473 fbcon_clear();
3474 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3475 return -1;
3476 }
3477 } else {
3478 if (mmc_read(ptn + blocksize ,
3479 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3480 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3481 return -1;
3482 }
3483 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3484 }
3485 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303486 }
3487
Mao Flynn7b379f32015-04-20 00:28:30 +08003488 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303489}
3490
Mao Flynn7b379f32015-04-20 00:28:30 +08003491int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303492{
3493 if (target_is_emmc_boot()) {
3494 return splash_screen_mmc();
3495 } else {
3496 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003497 }
3498}
3499
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003500/* Get the size from partiton name */
3501static void get_partition_size(const char *arg, char *response)
3502{
3503 uint64_t ptn = 0;
3504 uint64_t size;
3505 int index = INVALID_PTN;
3506
3507 index = partition_get_index(arg);
3508
3509 if (index == INVALID_PTN)
3510 {
3511 dprintf(CRITICAL, "Invalid partition index\n");
3512 return;
3513 }
3514
3515 ptn = partition_get_offset(index);
3516
3517 if(!ptn)
3518 {
3519 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3520 return;
3521 }
3522
3523 size = partition_get_size(index);
3524
3525 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3526 return;
3527}
3528
3529/*
3530 * Publish the partition type & size info
3531 * fastboot getvar will publish the required information.
3532 * fastboot getvar partition_size:<partition_name>: partition size in hex
3533 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3534 */
3535static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3536{
3537 uint8_t i;
3538
3539 for (i = 0; i < num_parts; i++) {
3540 get_partition_size(info[i].part_name, info[i].size_response);
3541
3542 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3543 {
3544 dprintf(CRITICAL, "partition size name truncated\n");
3545 return;
3546 }
3547 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3548 {
3549 dprintf(CRITICAL, "partition type name truncated\n");
3550 return;
3551 }
3552
3553 /* publish partition size & type info */
3554 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3555 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3556 }
3557}
3558
lijuang4ece1e72015-08-14 21:02:36 +08003559void get_product_name(unsigned char *buf)
3560{
3561 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3562 return;
3563}
3564
3565void get_bootloader_version(unsigned char *buf)
3566{
3567 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3568 return;
3569}
3570
3571void get_baseband_version(unsigned char *buf)
3572{
3573 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3574 return;
3575}
3576
3577bool is_device_locked()
3578{
3579 return device.is_unlocked ? false:true;
3580}
3581
Amol Jadi5edf3552013-07-23 14:15:34 -07003582/* register commands and variables for fastboot */
3583void aboot_fastboot_register_commands(void)
3584{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003585 int i;
lijuangf16461c2015-08-03 17:09:34 +08003586 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003587
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003588 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003589 /* By default the enabled list is empty. */
3590 {"", NULL},
3591 /* move commands enclosed within the below ifndef to here
3592 * if they need to be enabled in user build.
3593 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003594#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003595 /* Register the following commands only for non-user builds */
3596 {"flash:", cmd_flash},
3597 {"erase:", cmd_erase},
3598 {"boot", cmd_boot},
3599 {"continue", cmd_continue},
3600 {"reboot", cmd_reboot},
3601 {"reboot-bootloader", cmd_reboot_bootloader},
3602 {"oem unlock", cmd_oem_unlock},
3603 {"oem unlock-go", cmd_oem_unlock_go},
3604 {"oem lock", cmd_oem_lock},
3605 {"flashing unlock", cmd_oem_unlock},
3606 {"flashing lock", cmd_oem_lock},
3607 {"flashing lock_critical", cmd_flashing_lock_critical},
3608 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3609 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3610 {"oem device-info", cmd_oem_devinfo},
3611 {"preflash", cmd_preflash},
3612 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3613 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003614 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003615 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003616#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003617 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003618#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003619#endif
lijuang511a2b52015-08-14 20:50:51 +08003620 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003621
3622 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3623 for (i = 1; i < fastboot_cmds_count; i++)
3624 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3625
Amol Jadi5edf3552013-07-23 14:15:34 -07003626 /* publish variables and their values */
3627 fastboot_publish("product", TARGET(BOARD));
3628 fastboot_publish("kernel", "lk");
3629 fastboot_publish("serialno", sn_buf);
3630
3631 /*
3632 * partition info is supported only for emmc partitions
3633 * Calling this for NAND prints some error messages which
3634 * is harmless but misleading. Avoid calling this for NAND
3635 * devices.
3636 */
3637 if (target_is_emmc_boot())
3638 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3639
3640 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003641 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3642 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003643 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003644 /* Is the charger screen check enabled */
3645 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3646 device.charger_screen_enabled);
3647 fastboot_publish("charger-screen-enabled",
3648 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003649 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303650 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3651 device.display_panel);
3652 fastboot_publish("display-panel",
3653 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003654 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3655 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003656 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3657 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3658 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3659 target_is_emmc_boot()? "eMMC":"UFS");
3660 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003661#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003662 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003663 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003664 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003665#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003666}
3667
Brian Swetland9c4c0752009-01-25 16:23:50 -08003668void aboot_init(const struct app_descriptor *app)
3669{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003670 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003671
lijuang39831732016-01-08 17:49:02 +08003672 /* Initialise wdog to catch early lk crashes */
3673#if WDOG_SUPPORT
3674 msm_wdog_init();
3675#endif
3676
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003677 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003678 if (target_is_emmc_boot())
3679 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003680 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003681 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303682 mmc_blocksize = mmc_get_device_blocksize();
3683 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003684 }
3685 else
3686 {
3687 page_size = flash_page_size();
3688 page_mask = page_size - 1;
3689 }
3690
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003691 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3692
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003693 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303694 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003695
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003696 /* Display splash screen if enabled */
3697#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003698#if NO_ALARM_DISPLAY
3699 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003700#endif
lijuang99c02d82015-02-13 19:04:34 +08003701 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003702#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003703 /* Wait if the display shutdown is in progress */
3704 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003705 if (!pm_appsbl_display_init_done())
3706 target_display_init(device.display_panel);
3707 else
3708 display_image_on_screen();
3709#else
lijuang99c02d82015-02-13 19:04:34 +08003710 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003711#endif
lijuang99c02d82015-02-13 19:04:34 +08003712 dprintf(SPEW, "Display Init: Done\n");
3713#if NO_ALARM_DISPLAY
3714 }
3715#endif
3716#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003717
Greg Griscod6250552011-06-29 14:40:23 -07003718 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003719 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003720
Dhaval Patel223ec952013-07-18 14:49:44 -07003721 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3722
Matthew Qindefd5562014-07-11 18:02:40 +08003723 /*
3724 * Check power off reason if user force reset,
3725 * if yes phone will do normal boot.
3726 */
3727 if (is_user_force_reset())
3728 goto normal_boot;
3729
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003730 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003731 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003732 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003733 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08003734 reboot_device(EMERGENCY_DLOAD);
3735 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
3736
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003737 boot_into_fastboot = true;
3738 }
3739 if (!boot_into_fastboot)
3740 {
3741 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3742 boot_into_recovery = 1;
3743 if (!boot_into_recovery &&
3744 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003745 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003746 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003747 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003748 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003749 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003750 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003751
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003752#if USE_PON_REBOOT_REG
3753 reboot_mode = check_hard_reboot_mode();
3754#else
Ajay Dudani77421292010-10-27 19:34:06 -07003755 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003756#endif
3757 if (reboot_mode == RECOVERY_MODE)
3758 {
Ajay Dudani77421292010-10-27 19:34:06 -07003759 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003760 }
3761 else if(reboot_mode == FASTBOOT_MODE)
3762 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003763 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003764 }
3765 else if(reboot_mode == ALARM_BOOT)
3766 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003767 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003768 }
Parth Dixit207573a2015-10-27 17:26:21 +05303769#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303770#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003771 else if (reboot_mode == DM_VERITY_ENFORCING)
3772 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003773 device.verity_mode = 1;
3774 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003775 }
3776 else if (reboot_mode == DM_VERITY_LOGGING)
3777 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003778 device.verity_mode = 0;
3779 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003780 }
3781 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3782 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003783 if(send_delete_keys_to_tz())
3784 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003785 }
Parth Dixit207573a2015-10-27 17:26:21 +05303786#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303787#endif
Ajay Dudani77421292010-10-27 19:34:06 -07003788
Matthew Qindefd5562014-07-11 18:02:40 +08003789normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003790 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003791 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003792 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003793 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003794 if(emmc_recovery_init())
3795 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3796 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003797 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003798 if((device.is_unlocked) || (device.is_tampered))
3799 {
3800 #ifdef TZ_TAMPER_FUSE
3801 set_tamper_fuse_cmd();
3802 #endif
3803 #if USE_PCOM_SECBOOT
3804 set_tamper_flag(device.is_tampered);
3805 #endif
3806 }
Shashank Mittala0032282011-08-26 14:50:11 -07003807 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003808
Pavel Nedev5d91d412013-04-29 11:34:24 +03003809 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003810 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003811 else
3812 {
3813 recovery_init();
3814 #if USE_PCOM_SECBOOT
3815 if((device.is_unlocked) || (device.is_tampered))
3816 set_tamper_flag(device.is_tampered);
3817 #endif
3818 boot_linux_from_flash();
3819 }
3820 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3821 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003822 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003823
Amol Jadi5edf3552013-07-23 14:15:34 -07003824 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003825
Amol Jadi5edf3552013-07-23 14:15:34 -07003826 /* register aboot specific fastboot commands */
3827 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003828
Amol Jadi5edf3552013-07-23 14:15:34 -07003829 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003830 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003831
3832 /* initialize and start fastboot */
3833 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003834#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08003835 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08003836#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003837}
3838
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003839uint32_t get_page_size()
3840{
3841 return page_size;
3842}
3843
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003844/*
3845 * Calculated and save hash (SHA256) for non-signed boot image.
3846 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003847 * @param image_addr - Boot image address
3848 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003849 *
3850 * @return int - 0 on success, negative value on failure.
3851 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003852static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003853{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003854 unsigned int digest[8];
3855#if IMAGE_VERIF_ALGO_SHA1
3856 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3857#else
3858 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3859#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003860
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003861 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003862 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003863
3864 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003865 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003866
3867 return 0;
3868}
3869
Brian Swetland9c4c0752009-01-25 16:23:50 -08003870APP_START(aboot)
3871 .init = aboot_init,
3872APP_END