blob: e84e2e5b30b3b3fe79cd41c6cbf2459a513e5722 [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 Singh Parmara7865a82015-04-16 21:27:52 -0700120#define DISPLAY_PANEL_HDMI "hdmi"
121
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800122#ifdef MEMBASE
123#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
124#else
David Ng183a7422009-12-07 14:55:21 -0800125#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800126#endif
127
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700128#ifndef MEMSIZE
129#define MEMSIZE 1024*1024
130#endif
131
132#define MAX_TAGS_SIZE 1024
133
Kun Liang2f1601a2013-08-12 16:29:54 +0800134/* make 4096 as default size to ensure EFS,EXT4's erasing */
135#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700136#define MAX_PANEL_BUF_SIZE 196
Kun Liang2f1601a2013-08-12 16:29:54 +0800137
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700138#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700139#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800140
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800141#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
142
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700143#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
144
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800145#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700146static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700147#else
David Ng183a7422009-12-07 14:55:21 -0800148static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700149#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800150static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300151static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800152static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800153static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800154static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700155static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300156static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200157static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800158
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800159static const char *baseband_apq = " androidboot.baseband=apq";
160static const char *baseband_msm = " androidboot.baseband=msm";
161static const char *baseband_csfb = " androidboot.baseband=csfb";
162static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700163static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800164static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700165static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800166static const char *baseband_dsda = " androidboot.baseband=dsda";
167static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800168static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800169static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800170
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700171#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530172#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700173static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700174static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530175static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700176//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700177
178struct verified_boot_verity_mode vbvm[] =
179{
180 {false, "logging"},
181 {true, "enforcing"},
182};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700183struct verified_boot_state_name vbsn[] =
184{
185 {GREEN, "green"},
186 {ORANGE, "orange"},
187 {YELLOW,"yellow"},
188 {RED,"red" },
189};
190#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530191#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700192
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700193static unsigned page_size = 0;
194static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530195static unsigned mmc_blocksize = 0;
196static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700197static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
198static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530199static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800200static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700201static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700202bool boot_into_fastboot = false;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700203
Shashank Mittalcd98d472011-08-02 14:29:24 -0700204/* Assuming unauthorized kernel image by default */
205static int auth_kernel_img = 0;
Parth Dixit6f5822f2015-10-18 01:20:53 +0530206#if VBOOT_MOTA
207static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
208#else
lijuang511a2b52015-08-14 20:50:51 +0800209static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
Parth Dixit6f5822f2015-10-18 01:20:53 +0530210#endif
vijay kumarc65876c2015-04-24 13:29:16 +0530211static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700212
vijay kumarca2e6812015-07-08 20:28:25 +0530213static char frp_ptns[2][8] = {"config","frp"};
214
lijuang511a2b52015-08-14 20:50:51 +0800215static const char *critical_flash_allowed_ptn[] = {
216 "aboot",
217 "rpm",
218 "tz",
219 "sbl",
220 "sdi",
221 "sbl1",
222 "xbl",
223 "hyp",
224 "pmic",
225 "bootloader",
226 "devinfo",
227 "partition"};
228
Dima Zavin42168f22009-01-30 11:52:22 -0800229struct atag_ptbl_entry
230{
231 char name[16];
232 unsigned offset;
233 unsigned size;
234 unsigned flags;
235};
236
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700237/*
238 * Partition info, required to be published
239 * for fastboot
240 */
241struct getvar_partition_info {
242 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
243 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
244 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
245 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
246 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
247};
248
249/*
250 * Right now, we are publishing the info for only
251 * three partitions
252 */
253struct getvar_partition_info part_info[] =
254{
255 { "system" , "partition-size:", "partition-type:", "", "ext4" },
256 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
257 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
258};
259
260char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700261char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800262char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700263char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530264char panel_display_mode[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800265
266#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800267char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800268char battery_soc_ok [MAX_RSP_SIZE];
269#endif
270
lijuangf16461c2015-08-03 17:09:34 +0800271char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700272
Greg Griscod2471ef2011-07-14 13:00:42 -0700273extern int emmc_recovery_init(void);
274
Kinson Chik0b1c8162011-08-31 16:31:57 -0700275#if NO_KEYPAD_DRIVER
276extern int fastboot_trigger(void);
277#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700278
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800279static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700280{
281 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700282#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800283 if (is_arm64)
284 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
285 else
286 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700287 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
288 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700289#endif
290}
291
Dima Zavin42168f22009-01-30 11:52:22 -0800292static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
293{
294 struct atag_ptbl_entry atag_ptn;
295
296 memcpy(atag_ptn.name, ptn->name, 16);
297 atag_ptn.name[15] = '\0';
298 atag_ptn.offset = ptn->start;
299 atag_ptn.size = ptn->length;
300 atag_ptn.flags = ptn->flags;
301 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
302 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
303}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800304
lijuang102dfa92015-10-09 18:31:03 +0800305#if CHECK_BAT_VOLTAGE
306void update_battery_status(void)
307{
308 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
309 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
310}
311#endif
312
Neeti Desaie245d492012-06-01 12:52:13 -0700313unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800314{
David Ng183a7422009-12-07 14:55:21 -0800315 int cmdline_len = 0;
316 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800317 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700318 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800319 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300320 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700321 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700322 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200323 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700324#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530325#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700326 uint32_t boot_state = boot_verify_get_state();
327#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530328#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700329
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200330#ifdef MDTP_SUPPORT
331 mdtp_activated(&is_mdtp_activated);
332#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800333
Brian Swetland9c4c0752009-01-25 16:23:50 -0800334 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800335 cmdline_len = strlen(cmdline);
336 have_cmdline = 1;
337 }
338 if (target_is_emmc_boot()) {
339 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800340#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700341 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
342 ASSERT(boot_dev_buf);
343 platform_boot_dev_cmdline(boot_dev_buf);
344 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700345#endif
David Ng183a7422009-12-07 14:55:21 -0800346 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800347
348 cmdline_len += strlen(usb_sn_cmdline);
349 cmdline_len += strlen(sn_buf);
350
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700351#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530352#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700353 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700354 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
355 {
356 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
357 ASSERT(0);
358 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700359 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Parth Dixita5715a02015-10-29 12:25:10 +0530360 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700361#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530362#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700363
Pavel Nedev5614d222013-06-17 18:01:02 +0300364 if (boot_into_recovery && gpt_exists)
365 cmdline_len += strlen(secondary_gpt_enable);
366
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200367 if(is_mdtp_activated)
368 cmdline_len += strlen(mdtp_activated_flag);
369
Pavel Nedev328ac822013-04-05 15:25:11 +0300370 if (boot_into_ffbm) {
371 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700372 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800373 /* reduce kernel console messages to speed-up boot */
374 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800375 } else if (boot_reason_alarm) {
376 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800377 } else if ((target_build_variant_user() || device.charger_screen_enabled)
378 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700379 pause_at_bootup = 1;
380 cmdline_len += strlen(battchg_pause);
381 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800382
Shashank Mittalcd98d472011-08-02 14:29:24 -0700383 if(target_use_signed_kernel() && auth_kernel_img) {
384 cmdline_len += strlen(auth_kernel);
385 }
386
Joonwoo Park61112782013-10-02 19:50:39 -0700387 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
388 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530389 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700390 have_target_boot_params = 1;
391 cmdline_len += strlen(target_boot_params);
392 }
393
Ajay Dudanid04110c2011-01-17 23:55:07 -0800394 /* Determine correct androidboot.baseband to use */
395 switch(target_baseband())
396 {
397 case BASEBAND_APQ:
398 cmdline_len += strlen(baseband_apq);
399 break;
400
401 case BASEBAND_MSM:
402 cmdline_len += strlen(baseband_msm);
403 break;
404
405 case BASEBAND_CSFB:
406 cmdline_len += strlen(baseband_csfb);
407 break;
408
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800409 case BASEBAND_SVLTE2A:
410 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800411 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700412
413 case BASEBAND_MDM:
414 cmdline_len += strlen(baseband_mdm);
415 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700416
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800417 case BASEBAND_MDM2:
418 cmdline_len += strlen(baseband_mdm2);
419 break;
420
Amol Jadi5c61a952012-05-04 17:05:35 -0700421 case BASEBAND_SGLTE:
422 cmdline_len += strlen(baseband_sglte);
423 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530424
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800425 case BASEBAND_SGLTE2:
426 cmdline_len += strlen(baseband_sglte2);
427 break;
428
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530429 case BASEBAND_DSDA:
430 cmdline_len += strlen(baseband_dsda);
431 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800432
433 case BASEBAND_DSDA2:
434 cmdline_len += strlen(baseband_dsda2);
435 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800436 }
437
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800438 if (cmdline) {
439 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530440 target_display_panel_node(display_panel_buf,
441 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800442 strlen(display_panel_buf)) {
443 cmdline_len += strlen(display_panel_buf);
444 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700445 }
446
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800447 if (target_warm_boot()) {
448 warm_boot = true;
449 cmdline_len += strlen(warmboot_cmdline);
450 }
451
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800452#if TARGET_CMDLINE_SUPPORT
453 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
454 int target_cmd_line_len;
455 ASSERT(target_cmdline_buf);
456 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
457 cmdline_len += target_cmd_line_len;
458#endif
459
David Ng183a7422009-12-07 14:55:21 -0800460 if (cmdline_len > 0) {
461 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800462 unsigned char *dst;
463
464 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
465 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530466 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800467 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700468
Amol Jadi168b7712012-03-06 16:15:00 -0800469 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800470 if (have_cmdline) {
471 src = cmdline;
472 while ((*dst++ = *src++));
473 }
474 if (target_is_emmc_boot()) {
475 src = emmc_cmdline;
476 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700477 have_cmdline = 1;
478 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800479#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700480 src = boot_dev_buf;
481 if (have_cmdline) --dst;
482 while ((*dst++ = *src++));
483#endif
David Ngf773dde2010-07-26 19:55:08 -0700484 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800485
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700486#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530487#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700488 src = verified_state;
489 if(have_cmdline) --dst;
490 have_cmdline = 1;
491 while ((*dst++ = *src++));
492 src = vbsn[boot_state].name;
493 if(have_cmdline) --dst;
494 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700495
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700496 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
497 {
498 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
499 ASSERT(0);
500 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700501 src = verity_mode;
502 if(have_cmdline) --dst;
503 while ((*dst++ = *src++));
504 src = vbvm[device.verity_mode].name;
505 if(have_cmdline) -- dst;
506 while ((*dst++ = *src++));
Parth Dixita5715a02015-10-29 12:25:10 +0530507 src = keymaster_v1;
508 if(have_cmdline) --dst;
509 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700510#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530511#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800512 src = usb_sn_cmdline;
513 if (have_cmdline) --dst;
514 have_cmdline = 1;
515 while ((*dst++ = *src++));
516 src = sn_buf;
517 if (have_cmdline) --dst;
518 have_cmdline = 1;
519 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800520 if (warm_boot) {
521 if (have_cmdline) --dst;
522 src = warmboot_cmdline;
523 while ((*dst++ = *src++));
524 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800525
Pavel Nedev5614d222013-06-17 18:01:02 +0300526 if (boot_into_recovery && gpt_exists) {
527 src = secondary_gpt_enable;
528 if (have_cmdline) --dst;
529 while ((*dst++ = *src++));
530 }
531
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200532 if (is_mdtp_activated) {
533 src = mdtp_activated_flag;
534 if (have_cmdline) --dst;
535 while ((*dst++ = *src++));
536 }
537
Pavel Nedev328ac822013-04-05 15:25:11 +0300538 if (boot_into_ffbm) {
539 src = androidboot_mode;
540 if (have_cmdline) --dst;
541 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700542 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300543 if (have_cmdline) --dst;
544 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800545 src = loglevel;
546 if (have_cmdline) --dst;
547 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800548 } else if (boot_reason_alarm) {
549 src = alarmboot_cmdline;
550 if (have_cmdline) --dst;
551 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300552 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700553 src = battchg_pause;
554 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800555 while ((*dst++ = *src++));
556 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800557
Shashank Mittalcd98d472011-08-02 14:29:24 -0700558 if(target_use_signed_kernel() && auth_kernel_img) {
559 src = auth_kernel;
560 if (have_cmdline) --dst;
561 while ((*dst++ = *src++));
562 }
563
Ajay Dudanid04110c2011-01-17 23:55:07 -0800564 switch(target_baseband())
565 {
566 case BASEBAND_APQ:
567 src = baseband_apq;
568 if (have_cmdline) --dst;
569 while ((*dst++ = *src++));
570 break;
571
572 case BASEBAND_MSM:
573 src = baseband_msm;
574 if (have_cmdline) --dst;
575 while ((*dst++ = *src++));
576 break;
577
578 case BASEBAND_CSFB:
579 src = baseband_csfb;
580 if (have_cmdline) --dst;
581 while ((*dst++ = *src++));
582 break;
583
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800584 case BASEBAND_SVLTE2A:
585 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800586 if (have_cmdline) --dst;
587 while ((*dst++ = *src++));
588 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700589
590 case BASEBAND_MDM:
591 src = baseband_mdm;
592 if (have_cmdline) --dst;
593 while ((*dst++ = *src++));
594 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700595
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800596 case BASEBAND_MDM2:
597 src = baseband_mdm2;
598 if (have_cmdline) --dst;
599 while ((*dst++ = *src++));
600 break;
601
Amol Jadi5c61a952012-05-04 17:05:35 -0700602 case BASEBAND_SGLTE:
603 src = baseband_sglte;
604 if (have_cmdline) --dst;
605 while ((*dst++ = *src++));
606 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530607
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800608 case BASEBAND_SGLTE2:
609 src = baseband_sglte2;
610 if (have_cmdline) --dst;
611 while ((*dst++ = *src++));
612 break;
613
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530614 case BASEBAND_DSDA:
615 src = baseband_dsda;
616 if (have_cmdline) --dst;
617 while ((*dst++ = *src++));
618 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800619
620 case BASEBAND_DSDA2:
621 src = baseband_dsda2;
622 if (have_cmdline) --dst;
623 while ((*dst++ = *src++));
624 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800625 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700626
627 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700628 src = display_panel_buf;
629 if (have_cmdline) --dst;
630 while ((*dst++ = *src++));
631 }
Joonwoo Park61112782013-10-02 19:50:39 -0700632
633 if (have_target_boot_params) {
634 if (have_cmdline) --dst;
635 src = target_boot_params;
636 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530637 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700638 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800639
640#if TARGET_CMDLINE_SUPPORT
641 if (target_cmdline_buf && target_cmd_line_len)
642 {
643 if (have_cmdline) --dst;
644 src = target_cmdline_buf;
645 while((*dst++ = *src++));
646 free(target_cmdline_buf);
647 }
648#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700649 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700650
651
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700652 if (boot_dev_buf)
653 free(boot_dev_buf);
654
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800655 if (cmdline_final)
656 dprintf(INFO, "cmdline: %s\n", cmdline_final);
657 else
658 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700659 return cmdline_final;
660}
661
662unsigned *atag_core(unsigned *ptr)
663{
664 /* CORE */
665 *ptr++ = 2;
666 *ptr++ = 0x54410001;
667
668 return ptr;
669
670}
671
672unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
673 unsigned ramdisk_size)
674{
675 if (ramdisk_size) {
676 *ptr++ = 4;
677 *ptr++ = 0x54420005;
678 *ptr++ = (unsigned)ramdisk;
679 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800680 }
681
Neeti Desaie245d492012-06-01 12:52:13 -0700682 return ptr;
683}
684
685unsigned *atag_ptable(unsigned **ptr_addr)
686{
687 int i;
688 struct ptable *ptable;
689
690 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700691 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
692 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700693 *(*ptr_addr)++ = 0x4d534d70;
694 for (i = 0; i < ptable->count; ++i)
695 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
696 }
697
698 return (*ptr_addr);
699}
700
701unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
702{
703 int cmdline_length = 0;
704 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700705 char *dest;
706
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800707 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700708 n = (cmdline_length + 4) & (~3);
709
710 *ptr++ = (n / 4) + 2;
711 *ptr++ = 0x54410009;
712 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800713 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700714 ptr += (n / 4);
715
716 return ptr;
717}
718
719unsigned *atag_end(unsigned *ptr)
720{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800721 /* END */
722 *ptr++ = 0;
723 *ptr++ = 0;
724
Neeti Desaie245d492012-06-01 12:52:13 -0700725 return ptr;
726}
727
728void generate_atags(unsigned *ptr, const char *cmdline,
729 void *ramdisk, unsigned ramdisk_size)
730{
vijay kumar21a37452015-12-29 16:23:21 +0530731 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700732 ptr = atag_core(ptr);
733 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
734 ptr = target_atag_mem(ptr);
735
736 /* Skip NAND partition ATAGS for eMMC boot */
737 if (!target_is_emmc_boot()){
738 ptr = atag_ptable(&ptr);
739 }
740
vijay kumar21a37452015-12-29 16:23:21 +0530741 /*
742 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
743 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
744 */
745 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
746 ptr = atag_cmdline(ptr, cmdline);
747 ptr = atag_end(ptr);
748 }
749 else {
750 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
751 ASSERT(0);
752 }
Neeti Desaie245d492012-06-01 12:52:13 -0700753}
754
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700755typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700756void boot_linux(void *kernel, unsigned *tags,
757 const char *cmdline, unsigned machtype,
758 void *ramdisk, unsigned ramdisk_size)
759{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800760 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800761#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700762 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800763#endif
764
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700765 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800766 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530767 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800768
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530769 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700770
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800771 final_cmdline = update_cmdline((const char*)cmdline);
772
Neeti Desai17379b82012-06-04 18:42:53 -0700773#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800774 dprintf(INFO, "Updating device tree: start\n");
775
Neeti Desai17379b82012-06-04 18:42:53 -0700776 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530777 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700778 if(ret)
779 {
780 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
781 ASSERT(0);
782 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800783 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700784#else
Neeti Desaie245d492012-06-01 12:52:13 -0700785 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800786 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700787#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700788
Maria Yu52254c02014-07-04 16:14:54 +0800789 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700790
791#if VERIFIED_BOOT
lijuangbdd9bb42016-03-01 18:22:17 +0800792#if !VBOOT_MOTA
793 if (device.verity_mode == 0) {
794#if FBCON_DISPLAY_MSG
795 display_bootverify_menu(DISPLAY_MENU_LOGGING);
796 wait_for_users_action();
797#else
798 dprintf(CRITICAL,
799 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
800 mdelay(5000);
801#endif
802 }
803
804#endif
805#endif
806
807#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700808 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700809 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700810 {
811 dprintf(INFO, "Failed to write protect dev info\n");
812 ASSERT(0);
813 }
814#endif
815
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800816 /* Turn off splash screen if enabled */
817#if DISPLAY_SPLASH_SCREEN
818 target_display_shutdown();
819#endif
820
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700821 /* Perform target specific cleanup */
822 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800823
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800824 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530825 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800826
827 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700828
Amol Jadi4421e652011-06-16 15:00:48 -0700829 /* do any platform specific cleanup before kernel entry */
830 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700831
Brian Swetland9c4c0752009-01-25 16:23:50 -0800832 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700833
Amol Jadi504f9fe2012-08-16 13:56:48 -0700834#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800835 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700836#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700837 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800838
839 if (IS_ARM64(kptr))
840 /* Jump to a 64bit kernel */
841 scm_elexec_call((paddr_t)kernel, tags_phys);
842 else
843 /* Jump to a 32bit kernel */
844 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800845}
846
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700847/* Function to check if the memory address range falls within the aboot
848 * boundaries.
849 * start: Start of the memory region
850 * size: Size of the memory region
851 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +0530852int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700853{
854 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800855 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700856 return -1;
857
858 /* Check for memory overlap. */
859 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
860 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700861 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700862 return 0;
863 else
864 return -1;
865}
866
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800867#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800868
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800869BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800870#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800871BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800872#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800873
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700874static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
875{
876 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800877
878#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800879#if IMAGE_VERIF_ALGO_SHA1
880 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
881#else
882 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
883#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800884#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700885
886 /* Assume device is rooted at this time. */
887 device.is_tampered = 1;
888
889 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
890
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700891#if VERIFIED_BOOT
892 if(boot_into_recovery)
893 {
894 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530895 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700896 }
897 else
898 {
899 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530900 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700901 }
902 boot_verify_print_state();
903#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700904 ret = image_verify((unsigned char *)bootimg_addr,
905 (unsigned char *)(bootimg_addr + bootimg_size),
906 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800907 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700908#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700909 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
910
911 if (ret)
912 {
913 /* Authorized kernel */
914 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700915 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700916 }
917
Amit Blay4aa292f2015-04-28 21:55:59 +0300918#ifdef MDTP_SUPPORT
919 {
920 /* Verify MDTP lock.
921 * For boot & recovery partitions, use aboot's verification result.
922 */
923 mdtp_ext_partition_verification_t ext_partition;
924 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
925 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
926 ext_partition.page_size = 0; /* Not needed since already validated */
927 ext_partition.image_addr = 0; /* Not needed since already validated */
928 ext_partition.image_size = 0; /* Not needed since already validated */
929 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
930 mdtp_fwlock_verify_lock(&ext_partition);
931 }
932#endif /* MDTP_SUPPORT */
933
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700934#if USE_PCOM_SECBOOT
935 set_tamper_flag(device.is_tampered);
936#endif
937
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700938#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700939 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700940 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700941 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800942#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800943 display_bootverify_menu(DISPLAY_MENU_RED);
lijuanga40d6302015-07-20 20:10:13 +0800944 wait_for_users_action();
945#else
946 dprintf(CRITICAL,
947 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
948 mdelay(5000);
949#endif
950
951 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700952 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800953#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800954 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800955 wait_for_users_action();
956#else
957 dprintf(CRITICAL,
958 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
959 mdelay(5000);
960#endif
961 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700962 default:
lijuanga40d6302015-07-20 20:10:13 +0800963 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700964 }
965#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700966#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530967 if(device.is_tampered)
968 {
969 write_device_info_mmc(&device);
970 #ifdef TZ_TAMPER_FUSE
971 set_tamper_fuse_cmd();
972 #endif
973 #ifdef ASSERT_ON_TAMPER
974 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
975 ASSERT(0);
976 #endif
977 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700978#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700979}
980
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530981static bool check_format_bit()
982{
983 bool ret = false;
984 int index;
985 uint64_t offset;
986 struct boot_selection_info *in = NULL;
987 char *buf = NULL;
988
989 index = partition_get_index("bootselect");
990 if (index == INVALID_PTN)
991 {
992 dprintf(INFO, "Unable to locate /bootselect partition\n");
993 return ret;
994 }
995 offset = partition_get_offset(index);
996 if(!offset)
997 {
998 dprintf(INFO, "partition /bootselect doesn't exist\n");
999 return ret;
1000 }
1001 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
1002 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301003 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301004 {
1005 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1006 free(buf);
1007 return ret;
1008 }
1009 in = (struct boot_selection_info *) buf;
1010 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1011 (in->version == BOOTSELECT_VERSION)) {
1012 if ((in->state_info & BOOTSELECT_FORMAT) &&
1013 !(in->state_info & BOOTSELECT_FACTORY))
1014 ret = true;
1015 } else {
1016 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1017 in->signature, in->version);
1018 ASSERT(0);
1019 }
1020 free(buf);
1021 return ret;
1022}
1023
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001024void boot_verifier_init()
1025{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001026 uint32_t boot_state;
1027 /* Check if device unlock */
1028 if(device.is_unlocked)
1029 {
1030 boot_verify_send_event(DEV_UNLOCK);
1031 boot_verify_print_state();
1032 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1033 return;
1034 }
1035 else
1036 {
1037 boot_verify_send_event(BOOT_INIT);
1038 }
1039
1040 /* Initialize keystore */
1041 boot_state = boot_verify_keystore_init();
1042 if(boot_state == YELLOW)
1043 {
1044 boot_verify_print_state();
1045 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1046 }
1047}
1048
Shashank Mittal23b8f422010-04-16 19:27:21 -07001049int boot_linux_from_mmc(void)
1050{
1051 struct boot_img_hdr *hdr = (void*) buf;
1052 struct boot_img_hdr *uhdr;
1053 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001054 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001055 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001056 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001057
Shashank Mittalcd98d472011-08-02 14:29:24 -07001058 unsigned char *image_addr = 0;
1059 unsigned kernel_actual;
1060 unsigned ramdisk_actual;
1061 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001062 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001063
Matthew Qin271927e2015-03-31 22:07:07 -04001064 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001065 unsigned int out_len = 0;
1066 unsigned int out_avai_len = 0;
1067 unsigned char *out_addr = NULL;
1068 uint32_t dtb_offset = 0;
1069 unsigned char *kernel_start_addr = NULL;
1070 unsigned int kernel_size = 0;
1071 int rc;
1072
Neeti Desai465491e2012-07-31 12:53:35 -07001073#if DEVICE_TREE
1074 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001075 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001076 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001077 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001078 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001079 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001080#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001081 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001082
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301083 if (check_format_bit())
1084 boot_into_recovery = 1;
1085
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001086 if (!boot_into_recovery) {
1087 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1088 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1089 if (rcode <= 0) {
1090 boot_into_ffbm = false;
1091 if (rcode < 0)
1092 dprintf(CRITICAL,"failed to get ffbm cookie");
1093 } else
1094 boot_into_ffbm = true;
1095 } else
1096 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001097 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1098 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1099 dprintf(INFO, "Unified boot method!\n");
1100 hdr = uhdr;
1101 goto unified_boot;
1102 }
Greg Griscod6250552011-06-29 14:40:23 -07001103 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001104 index = partition_get_index("boot");
1105 ptn = partition_get_offset(index);
1106 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001107 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1108 return -1;
1109 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001110 }
1111 else {
1112 index = partition_get_index("recovery");
1113 ptn = partition_get_offset(index);
1114 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001115 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1116 return -1;
1117 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001118 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001119 /* Set Lun for boot & recovery partitions */
1120 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001121
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301122 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001123 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1124 return -1;
1125 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001126
1127 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001128 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001129 return -1;
1130 }
1131
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001132 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301133
1134 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1135 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1136 return -1;
1137 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001138 page_size = hdr->page_size;
1139 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001140 }
1141
vijay kumar287bb542015-09-29 13:01:52 +05301142 /* ensure commandline is terminated */
1143 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1144
Matthew Qin49e51fa2015-02-09 10:40:45 +08001145 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1146 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001147
Matthew Qin49e51fa2015-02-09 10:40:45 +08001148 image_addr = (unsigned char *)target_get_scratch_address();
1149
1150#if DEVICE_TREE
1151 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1152 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1153#else
1154 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1155#endif
1156
1157#if VERIFIED_BOOT
1158 boot_verifier_init();
1159#endif
1160
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301161 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001162 {
1163 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1164 return -1;
1165 }
1166
Matthew Qinbb7923d2015-02-09 10:56:09 +08001167 /*
1168 * Update loading flow of bootimage to support compressed/uncompressed
1169 * bootimage on both 64bit and 32bit platform.
1170 * 1. Load bootimage from emmc partition onto DDR.
1171 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1172 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1173 * platform accordingly.
1174 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1175 */
1176
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001177 dprintf(INFO, "Loading (%s) image (%d): start\n",
1178 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001179 bs_set_timestamp(BS_KERNEL_LOAD_START);
1180
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301181 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1182 {
1183 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1184 return -1;
1185 }
1186
Matthew Qinbb7923d2015-02-09 10:56:09 +08001187 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001188 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1189 {
1190 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1191 return -1;
1192 }
1193
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001194 dprintf(INFO, "Loading (%s) image (%d): done\n",
1195 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1196
Matthew Qin49e51fa2015-02-09 10:40:45 +08001197 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1198
1199 /* Authenticate Kernel */
1200 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1201 (int) target_use_signed_kernel(),
1202 device.is_unlocked,
1203 device.is_tampered);
1204
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001205 /* Change the condition a little bit to include the test framework support.
1206 * We would never reach this point if device is in fastboot mode, even if we did
1207 * that means we are in test mode, so execute kernel authentication part for the
1208 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301209 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001210 {
1211 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301212 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001213 {
1214 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1215 return -1;
1216 }
1217
1218 /* Read signature */
1219 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1220 {
1221 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1222 return -1;
1223 }
1224
1225 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001226 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301227 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001228 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001229 } else {
1230 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1231 #ifdef TZ_SAVE_KERNEL_HASH
1232 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1233 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001234
1235#ifdef MDTP_SUPPORT
1236 {
1237 /* Verify MDTP lock.
1238 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1239 * since verification was skipped in aboot. The signature is not part of the loaded image.
1240 */
1241 mdtp_ext_partition_verification_t ext_partition;
1242 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1243 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1244 ext_partition.page_size = page_size;
1245 ext_partition.image_addr = (uint32)image_addr;
1246 ext_partition.image_size = imagesize_actual;
1247 ext_partition.sig_avail = FALSE;
1248 mdtp_fwlock_verify_lock(&ext_partition);
1249 }
1250#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001251 }
1252
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001253#if VERIFIED_BOOT
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001254 if(boot_verify_get_state() == ORANGE)
1255 {
1256#if FBCON_DISPLAY_MSG
1257 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1258 wait_for_users_action();
1259#else
1260 dprintf(CRITICAL,
1261 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1262 mdelay(5000);
1263#endif
1264 }
1265#endif
1266
1267#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05301268#if !VBOOT_MOTA
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001269 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001270 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001271 ASSERT(0);
1272#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05301273#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001274 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001275 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1276 * If not, continue booting.
1277 */
1278 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1279 {
1280 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1281 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001282 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001283 rc = decompress((unsigned char *)(image_addr + page_size),
1284 hdr->kernel_size, out_addr, out_avai_len,
1285 &dtb_offset, &out_len);
1286 if (rc)
1287 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001288 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001289 ASSERT(0);
1290 }
1291
Matthew Qin0b15b322015-05-19 05:20:54 -04001292 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001293 kptr = (struct kernel64_hdr *)out_addr;
1294 kernel_start_addr = out_addr;
1295 kernel_size = out_len;
1296 } else {
1297 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1298 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1299 kernel_size = hdr->kernel_size;
1300 }
1301
1302 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001303 * Update the kernel/ramdisk/tags address if the boot image header
1304 * has default values, these default values come from mkbootimg when
1305 * the boot image is flashed using fastboot flash:raw
1306 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001307 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001308
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001309 /* Get virtual addresses since the hdr saves physical addresses. */
1310 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1311 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1312 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001313
Matthew Qinbb7923d2015-02-09 10:56:09 +08001314 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001315 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001316 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001317 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1318 {
1319 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1320 return -1;
1321 }
1322
1323#ifndef DEVICE_TREE
1324 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1325 {
1326 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1327 return -1;
1328 }
1329#endif
1330
Matthew Qin49e51fa2015-02-09 10:40:45 +08001331 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001332 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001333 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001334
Matthew Qin49e51fa2015-02-09 10:40:45 +08001335 #if DEVICE_TREE
1336 if(hdr->dt_size) {
1337 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1338 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001339
Matthew Qin49e51fa2015-02-09 10:40:45 +08001340 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1341 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1342 return -1;
1343 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001344
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301345 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1346 goes beyound hdr->dt_size*/
1347 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1348 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1349 return -1;
1350 }
1351
Matthew Qin49e51fa2015-02-09 10:40:45 +08001352 /* Find index of device tree within device tree table */
1353 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1354 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1355 return -1;
1356 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001357
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301358 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1359 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1360 return -1;
1361 }
1362
1363 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1364 if ((dt_entry.offset + dt_entry.size) > hdr->dt_size) {
1365 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1366 return -1;
1367 }
1368
Matthew Qin271927e2015-03-31 22:07:07 -04001369 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1370 {
1371 unsigned int compressed_size = 0;
1372 out_addr += out_len;
1373 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001374 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001375 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1376 dt_entry.size, out_addr, out_avai_len,
1377 &compressed_size, &dtb_size);
1378 if (rc)
1379 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001380 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001381 ASSERT(0);
1382 }
1383
Matthew Qin0b15b322015-05-19 05:20:54 -04001384 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001385 best_match_dt_addr = out_addr;
1386 } else {
1387 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1388 dtb_size = dt_entry.size;
1389 }
1390
Matthew Qin49e51fa2015-02-09 10:40:45 +08001391 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001392 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001393 {
1394 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1395 return -1;
1396 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001397
Matthew Qin271927e2015-03-31 22:07:07 -04001398 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001399 } else {
1400 /* Validate the tags_addr */
1401 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001402 {
1403 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1404 return -1;
1405 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001406 /*
1407 * If appended dev tree is found, update the atags with
1408 * memory address to the DTB appended location on RAM.
1409 * Else update with the atags address in the kernel header
1410 */
1411 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001412 dtb = dev_tree_appended((void*)(image_addr + page_size),
1413 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001414 (void *)hdr->tags_addr);
1415 if (!dtb) {
1416 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001417 return -1;
1418 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001419 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001420 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001421
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001422 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1423 target_load_ssd_keystore();
1424
Shashank Mittal23b8f422010-04-16 19:27:21 -07001425unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001426
Dima Zavin77e41f32013-03-06 16:10:43 -08001427 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001428 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001429 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1430
1431 return 0;
1432}
1433
Dima Zavin214cc642009-01-26 11:16:21 -08001434int boot_linux_from_flash(void)
1435{
1436 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001437 struct ptentry *ptn;
1438 struct ptable *ptable;
1439 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001440
Shashank Mittalcd98d472011-08-02 14:29:24 -07001441 unsigned char *image_addr = 0;
1442 unsigned kernel_actual;
1443 unsigned ramdisk_actual;
1444 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301445 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001446
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001447#if DEVICE_TREE
1448 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001449 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301450 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001451 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001452 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301453 unsigned int dtb_size = 0;
1454 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001455#endif
1456
David Ng183a7422009-12-07 14:55:21 -08001457 if (target_is_emmc_boot()) {
1458 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1459 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1460 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1461 return -1;
1462 }
1463 goto continue_boot;
1464 }
1465
Dima Zavin214cc642009-01-26 11:16:21 -08001466 ptable = flash_get_ptable();
1467 if (ptable == NULL) {
1468 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1469 return -1;
1470 }
1471
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001472 if(!boot_into_recovery)
1473 {
1474 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001475
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001476 if (ptn == NULL) {
1477 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1478 return -1;
1479 }
1480 }
1481 else
1482 {
1483 ptn = ptable_find(ptable, "recovery");
1484 if (ptn == NULL) {
1485 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1486 return -1;
1487 }
Dima Zavin214cc642009-01-26 11:16:21 -08001488 }
1489
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001490 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001491 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1492 return -1;
1493 }
Dima Zavin214cc642009-01-26 11:16:21 -08001494
1495 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001496 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001497 return -1;
1498 }
1499
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001500 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001501 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 -08001502 return -1;
1503 }
1504
vijay kumar287bb542015-09-29 13:01:52 +05301505 /* ensure commandline is terminated */
1506 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1507
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001508 /*
1509 * Update the kernel/ramdisk/tags address if the boot image header
1510 * has default values, these default values come from mkbootimg when
1511 * the boot image is flashed using fastboot flash:raw
1512 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001513 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001514
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001515 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001516 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1517 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1518 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1519
1520 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1521 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1522
1523 /* Check if the addresses in the header are valid. */
1524 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1525 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1526 {
1527 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1528 return -1;
1529 }
1530
1531#ifndef DEVICE_TREE
1532 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1533 {
1534 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1535 return -1;
1536 }
1537#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001538
Shashank Mittalcd98d472011-08-02 14:29:24 -07001539 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001540 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001541 {
1542 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001543 offset = 0;
1544
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001545#if DEVICE_TREE
1546 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
vijay kumar966a9822015-12-09 18:36:09 +05301547
1548 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1549 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1550 return -1;
1551 }
1552
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001553 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001554
1555 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1556 {
1557 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1558 return -1;
1559 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001560#else
vijay kumar966a9822015-12-09 18:36:09 +05301561 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) {
1562 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1563 return -1;
1564 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001565 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001566#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001567
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001568 dprintf(INFO, "Loading (%s) image (%d): start\n",
1569 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001570 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001571
Shashank Mittalcd98d472011-08-02 14:29:24 -07001572 /* Read image without signature */
1573 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1574 {
1575 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1576 return -1;
1577 }
Dima Zavin214cc642009-01-26 11:16:21 -08001578
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001579 dprintf(INFO, "Loading (%s) image (%d): done\n",
1580 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001581 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001582
Shashank Mittalcd98d472011-08-02 14:29:24 -07001583 offset = imagesize_actual;
1584 /* Read signature */
1585 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1586 {
1587 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001588 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001589 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001590
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301591 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001592
1593 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001594 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1595 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001596#if DEVICE_TREE
vijay kumar8f53e362015-11-24 13:38:11 +05301597 if(hdr->dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001598
vijay kumar8f53e362015-11-24 13:38:11 +05301599 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1600
1601 table = (struct dt_table*) dt_table_offset;
1602
1603 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1604 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1605 return -1;
1606 }
1607
1608 /* Find index of device tree within device tree table */
1609 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1610 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1611 return -1;
1612 }
1613
1614 /* Validate and Read device device tree in the "tags_add */
1615 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){
1616 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1617 return -1;
1618 }
1619
1620 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1621 dtb_size = dt_entry.size;
1622 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1623 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001624#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001625
1626 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001627 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001628 {
1629 write_device_info_flash(&device);
1630 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301631#if USE_PCOM_SECBOOT
1632 set_tamper_flag(device.is_tampered);
1633#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001634 }
1635 else
1636 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001637 offset = page_size;
1638
Amol Jadib6be5c12012-11-14 13:39:51 -08001639 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1640 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1641 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1642
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001643 dprintf(INFO, "Loading (%s) image (%d): start\n",
1644 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1645
Amol Jadi492d5a52013-03-15 16:12:34 -07001646 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001647
1648 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001649 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1650 return -1;
1651 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001652 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001653
Amol Jadib6be5c12012-11-14 13:39:51 -08001654 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001655 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1656 return -1;
1657 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001658 offset += ramdisk_actual;
1659
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001660 dprintf(INFO, "Loading (%s) image (%d): done\n",
1661 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1662
Amol Jadi492d5a52013-03-15 16:12:34 -07001663 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001664
1665 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001666 offset += second_actual;
1667 /* Second image loading not implemented. */
1668 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001669 }
1670
1671#if DEVICE_TREE
1672 if(hdr->dt_size != 0) {
1673
1674 /* Read the device tree table into buffer */
1675 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1676 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1677 return -1;
1678 }
1679
1680 table = (struct dt_table*) dt_buf;
1681
Deepa Dinamani19648b42013-09-05 17:05:55 -07001682 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001683 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1684 return -1;
1685 }
1686
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301687 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1688 goes beyound hdr->dt_size*/
1689 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1690 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1691 return -1;
1692 }
1693
Deepa Dinamani19648b42013-09-05 17:05:55 -07001694 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1695 if (!table)
1696 return -1;
1697
1698 /* Read the entire device tree table into buffer */
1699 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1700 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1701 return -1;
1702 }
1703
1704
Joel Kingaa335dc2013-06-03 16:11:08 -07001705 /* Find index of device tree within device tree table */
1706 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001707 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1708 return -1;
1709 }
1710
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001711 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001712 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001713 {
1714 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1715 return -1;
1716 }
1717
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001718 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001719 if(flash_read(ptn, offset + dt_entry.offset,
1720 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001721 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1722 return -1;
1723 }
1724 }
1725#endif
1726
Shashank Mittalcd98d472011-08-02 14:29:24 -07001727 }
David Ng183a7422009-12-07 14:55:21 -08001728continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001729
Dima Zavin214cc642009-01-26 11:16:21 -08001730 /* TODO: create/pass atags to kernel */
1731
Ajay Dudanie28a6072011-07-01 13:59:46 -07001732 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001733 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001734 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1735
1736 return 0;
1737}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001738
Shashank Mittal162244e2011-08-08 19:01:25 -07001739void write_device_info_mmc(device_info *dev)
1740{
Shashank Mittal162244e2011-08-08 19:01:25 -07001741 unsigned long long ptn = 0;
1742 unsigned long long size;
1743 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001744 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001745 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001746 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001747
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001748 if (devinfo_present)
1749 index = partition_get_index("devinfo");
1750 else
1751 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001752
Shashank Mittal162244e2011-08-08 19:01:25 -07001753 ptn = partition_get_offset(index);
1754 if(ptn == 0)
1755 {
1756 return;
1757 }
1758
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001759 lun = partition_get_lun(index);
1760 mmc_set_lun(lun);
1761
Shashank Mittal162244e2011-08-08 19:01:25 -07001762 size = partition_get_size(index);
1763
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001764 blocksize = mmc_get_device_blocksize();
1765
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001766 if (devinfo_present)
1767 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1768 else
1769 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1770 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001771 {
1772 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001773 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001774 }
1775}
1776
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001777void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001778{
Shashank Mittal162244e2011-08-08 19:01:25 -07001779 unsigned long long ptn = 0;
1780 unsigned long long size;
1781 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001782 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001783 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001784
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001785 if ((index = partition_get_index("devinfo")) < 0)
1786 {
1787 devinfo_present = false;
1788 index = partition_get_index("aboot");
1789 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001790
Shashank Mittal162244e2011-08-08 19:01:25 -07001791 ptn = partition_get_offset(index);
1792 if(ptn == 0)
1793 {
1794 return;
1795 }
1796
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001797 mmc_set_lun(partition_get_lun(index));
1798
Shashank Mittal162244e2011-08-08 19:01:25 -07001799 size = partition_get_size(index);
1800
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001801 blocksize = mmc_get_device_blocksize();
1802
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001803 if (devinfo_present)
1804 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1805 else
1806 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1807 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001808 {
1809 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001810 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001811 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001812}
1813
1814void write_device_info_flash(device_info *dev)
1815{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001816 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001817 struct ptentry *ptn;
1818 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001819 if(info == NULL)
1820 {
1821 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1822 ASSERT(0);
1823 }
1824 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001825 ptable = flash_get_ptable();
1826 if (ptable == NULL)
1827 {
1828 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1829 return;
1830 }
1831
1832 ptn = ptable_find(ptable, "devinfo");
1833 if (ptn == NULL)
1834 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001835 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001836 return;
1837 }
1838
1839 memcpy(info, dev, sizeof(device_info));
1840
1841 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1842 {
1843 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1844 return;
1845 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001846 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001847}
1848
vijay kumarc65876c2015-04-24 13:29:16 +05301849static int read_allow_oem_unlock(device_info *dev)
1850{
vijay kumarc65876c2015-04-24 13:29:16 +05301851 unsigned offset;
1852 int index;
1853 unsigned long long ptn;
1854 unsigned long long ptn_size;
1855 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001856 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301857
vijay kumarca2e6812015-07-08 20:28:25 +05301858 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301859 if (index == INVALID_PTN)
1860 {
vijay kumarca2e6812015-07-08 20:28:25 +05301861 index = partition_get_index(frp_ptns[1]);
1862 if (index == INVALID_PTN)
1863 {
1864 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1865 return -1;
1866 }
vijay kumarc65876c2015-04-24 13:29:16 +05301867 }
1868
1869 ptn = partition_get_offset(index);
1870 ptn_size = partition_get_size(index);
1871 offset = ptn_size - blocksize;
1872
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001873 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301874 {
1875 dprintf(CRITICAL, "Reading MMC failed\n");
1876 return -1;
1877 }
1878
1879 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1880 is_allow_unlock = buf[blocksize-1] & 0x01;
1881 return 0;
1882}
1883
1884static int write_allow_oem_unlock(bool allow_unlock)
1885{
vijay kumarc65876c2015-04-24 13:29:16 +05301886 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301887 int index;
1888 unsigned long long ptn;
1889 unsigned long long ptn_size;
1890 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001891 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301892
vijay kumarca2e6812015-07-08 20:28:25 +05301893 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301894 if (index == INVALID_PTN)
1895 {
vijay kumarca2e6812015-07-08 20:28:25 +05301896 index = partition_get_index(frp_ptns[1]);
1897 if (index == INVALID_PTN)
1898 {
1899 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1900 return -1;
1901 }
vijay kumarc65876c2015-04-24 13:29:16 +05301902 }
1903
1904 ptn = partition_get_offset(index);
1905 ptn_size = partition_get_size(index);
1906 offset = ptn_size - blocksize;
1907
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001908 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301909 {
1910 dprintf(CRITICAL, "Reading MMC failed\n");
1911 return -1;
1912 }
1913
1914 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1915 buf[blocksize-1] = allow_unlock;
1916 if (mmc_write(ptn + offset, blocksize, buf))
1917 {
1918 dprintf(CRITICAL, "Writing MMC failed\n");
1919 return -1;
1920 }
1921
1922 return 0;
1923}
1924
Shashank Mittal162244e2011-08-08 19:01:25 -07001925void read_device_info_flash(device_info *dev)
1926{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001927 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001928 struct ptentry *ptn;
1929 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001930 if(info == NULL)
1931 {
1932 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1933 ASSERT(0);
1934 }
1935 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001936 ptable = flash_get_ptable();
1937 if (ptable == NULL)
1938 {
1939 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1940 return;
1941 }
1942
1943 ptn = ptable_find(ptable, "devinfo");
1944 if (ptn == NULL)
1945 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001946 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001947 return;
1948 }
1949
1950 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1951 {
1952 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1953 return;
1954 }
1955
1956 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1957 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001958 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1959 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001960 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001961 write_device_info_flash(info);
1962 }
1963 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001964 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001965}
1966
1967void write_device_info(device_info *dev)
1968{
1969 if(target_is_emmc_boot())
1970 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001971 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1972 if(info == NULL)
1973 {
1974 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1975 ASSERT(0);
1976 }
1977 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001978 memcpy(info, dev, sizeof(struct device_info));
1979
1980#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001981 if (is_secure_boot_enable()) {
1982 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1983 ASSERT(0);
1984 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001985 else
1986 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001987#else
1988 write_device_info_mmc(info);
1989#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001990 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001991 }
1992 else
1993 {
1994 write_device_info_flash(dev);
1995 }
1996}
1997
1998void read_device_info(device_info *dev)
1999{
2000 if(target_is_emmc_boot())
2001 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002002 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2003 if(info == NULL)
2004 {
2005 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2006 ASSERT(0);
2007 }
2008 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002009
2010#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002011 if (is_secure_boot_enable()) {
2012 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2013 ASSERT(0);
2014 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002015 else
2016 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002017#else
2018 read_device_info_mmc(info);
2019#endif
2020
2021 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2022 {
2023 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002024 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002025 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302026#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002027 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302028#endif
lijuang511a2b52015-08-14 20:50:51 +08002029 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002030 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302031#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002032 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302033#endif
lijuang511a2b52015-08-14 20:50:51 +08002034 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002035 info->is_tampered = 0;
2036 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302037#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07002038 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302039#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002040 write_device_info(info);
2041 }
2042 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002043 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002044 }
2045 else
2046 {
2047 read_device_info_flash(dev);
2048 }
2049}
2050
2051void reset_device_info()
2052{
2053 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002054 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002055 write_device_info(&device);
2056}
2057
2058void set_device_root()
2059{
2060 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002061 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002062 write_device_info(&device);
2063}
2064
lijuang4ece1e72015-08-14 21:02:36 +08002065/* set device unlock value
2066 * Must check FRP before call this function
2067 * Need to wipe data when unlock status changed
2068 * type 0: oem unlock
2069 * type 1: unlock critical
2070 * status 0: unlock as false
2071 * status 1: lock as true
2072 */
2073void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002074{
lijuang4ece1e72015-08-14 21:02:36 +08002075 if (type == UNLOCK)
2076 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302077#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002078 else if (type == UNLOCK_CRITICAL)
2079 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302080#endif
lijuang4ece1e72015-08-14 21:02:36 +08002081 write_device_info(&device);
2082}
2083
2084static void set_device_unlock(int type, bool status)
2085{
2086 int is_unlocked = -1;
2087 char response[MAX_RSP_SIZE];
2088
2089 /* check device unlock status if it is as expected */
2090 if (type == UNLOCK)
2091 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05302092#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002093 else if (type == UNLOCK_CRITICAL)
2094 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05302095#endif
lijuang4ece1e72015-08-14 21:02:36 +08002096 if (is_unlocked == status) {
2097 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2098 fastboot_info(response);
2099 fastboot_okay("");
2100 return;
2101 }
2102
2103 /* status is true, it means to unlock device */
2104 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002105 if(!is_allow_unlock) {
2106 fastboot_fail("oem unlock is not allowed");
2107 return;
2108 }
2109
lijuang4ece1e72015-08-14 21:02:36 +08002110#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002111 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002112 fastboot_okay("");
2113 return;
2114#else
2115 if (type == UNLOCK) {
2116 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2117 return;
2118 }
2119#endif
lijuang21f12f52015-08-22 16:22:19 +08002120 }
lijuang4ece1e72015-08-14 21:02:36 +08002121
2122 set_device_unlock_value(type, status);
2123
2124 /* wipe data */
2125 struct recovery_message msg;
2126
2127 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2128 write_misc(0, &msg, sizeof(msg));
2129
2130 fastboot_okay("");
2131 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002132}
2133
lijuang511a2b52015-08-14 20:50:51 +08002134static bool critical_flash_allowed(const char * entry)
2135{
2136 uint32_t i = 0;
2137 if (entry == NULL)
2138 return false;
2139
2140 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2141 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2142 return true;
2143 }
2144 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002145}
2146
2147#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002148int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2149{
2150 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002151 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002152 struct dt_table *table;
2153 struct dt_entry dt_entry;
2154 uint32_t dt_hdr_size;
2155 unsigned int compressed_size = 0;
2156 unsigned int dtb_size = 0;
2157 unsigned int out_avai_len = 0;
2158 unsigned char *out_addr = NULL;
2159 unsigned char *best_match_dt_addr = NULL;
2160 int rc;
2161
2162 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2163
2164 if(hdr->dt_size != 0) {
2165 /* add kernel offset */
2166 dt_image_offset += page_size;
2167 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002168 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002169
Deepa Dinamani19648b42013-09-05 17:05:55 -07002170 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002171 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2172 dt_image_offset += n;
2173
Joel Kingaa335dc2013-06-03 16:11:08 -07002174 /* add second offset */
2175 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002176 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2177 dt_image_offset += n;
2178 }
2179
Matthew Qin271927e2015-03-31 22:07:07 -04002180 /* offset now point to start of dt.img */
2181 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2182
2183 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2184 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002185 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002186 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302187
2188 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2189 goes beyound hdr->dt_size*/
2190 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
2191 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2192 return -1;
2193 }
2194
Amol Jadicb524072012-08-09 16:40:18 -07002195 /* Find index of device tree within device tree table */
2196 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2197 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2198 return -1;
2199 }
2200
Matthew Qin271927e2015-03-31 22:07:07 -04002201 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2202 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2203 {
2204 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2205 out_avai_len = target_get_max_flash_size() - scratch_offset;
2206 dprintf(INFO, "decompressing dtb: start\n");
2207 rc = decompress(best_match_dt_addr,
2208 dt_entry.size, out_addr, out_avai_len,
2209 &compressed_size, &dtb_size);
2210 if (rc)
2211 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002212 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002213 ASSERT(0);
2214 }
2215
Matthew Qin0b15b322015-05-19 05:20:54 -04002216 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002217 best_match_dt_addr = out_addr;
2218 } else {
2219 dtb_size = dt_entry.size;
2220 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002221 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002222 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002223 {
2224 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
2225 return -1;
2226 }
2227
Amol Jadicb524072012-08-09 16:40:18 -07002228 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002229 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002230 } else
2231 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002232
2233 /* Everything looks fine. Return success. */
2234 return 0;
2235}
2236#endif
2237
Brian Swetland9c4c0752009-01-25 16:23:50 -08002238void cmd_boot(const char *arg, void *data, unsigned sz)
2239{
Amit Blay8a510302015-08-17 09:20:01 +03002240#ifdef MDTP_SUPPORT
2241 static bool is_mdtp_activated = 0;
2242#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002243 unsigned kernel_actual;
2244 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002245 uint32_t image_actual;
2246 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302247 uint32_t sig_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002248 struct boot_img_hdr *hdr = NULL;
2249 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002250 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002251 int ret = 0;
2252 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002253 unsigned int out_len = 0;
2254 unsigned int out_avai_len = 0;
2255 unsigned char *out_addr = NULL;
2256 uint32_t dtb_offset = 0;
2257 unsigned char *kernel_start_addr = NULL;
2258 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002259 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002260
lijuang2008ff22016-03-07 17:56:27 +08002261#if FBCON_DISPLAY_MSG
2262 /* Exit keys' detection thread firstly */
2263 exit_menu_keys_detection();
2264#endif
2265
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002266#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002267 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002268 {
2269 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002270 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002271 }
2272#endif
2273
Brian Swetland9c4c0752009-01-25 16:23:50 -08002274 if (sz < sizeof(hdr)) {
2275 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002276 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002277 }
2278
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002279 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002280
2281 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002282 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002283
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002284 if(target_is_emmc_boot() && hdr->page_size) {
2285 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002286 page_mask = page_size - 1;
2287 }
2288
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002289 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2290 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002291#if DEVICE_TREE
2292 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2293#endif
2294
2295 image_actual = ADD_OF(page_size, kernel_actual);
2296 image_actual = ADD_OF(image_actual, ramdisk_actual);
2297 image_actual = ADD_OF(image_actual, dt_actual);
2298
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302299 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2300 /* Calculate the signature length from boot image */
2301 sig_actual = read_der_message_length(
2302 (unsigned char*)(data + image_actual),sz);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002303 image_actual = ADD_OF(image_actual, sig_actual);
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302304 }
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002305
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002306 /* sz should have atleast raw boot image */
2307 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002308 fastboot_fail("bootimage: incomplete or not signed");
lijuang2008ff22016-03-07 17:56:27 +08002309 goto boot_failed;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002310 }
2311
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002312 // Initialize boot state before trying to verify boot.img
2313#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002314 boot_verifier_init();
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002315 /* Handle overflow if the input image size is greater than
2316 * boot image buffer can hold
2317 */
2318 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2319 {
2320 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002321 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002322 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002323#endif
2324
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002325 /* Verify the boot image
2326 * device & page_size are initialized in aboot_init
2327 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002328 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002329 /* Pass size excluding signature size, otherwise we would try to
2330 * access signature beyond its length
2331 */
2332 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002333 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002334#ifdef MDTP_SUPPORT
2335 else
2336 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002337 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002338 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002339
2340 if (!is_mdtp_activated) {
2341 ext_partition.partition = MDTP_PARTITION_NONE;
2342 mdtp_fwlock_verify_lock(&ext_partition);
2343 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002344 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002345
Amir Kotzer7c768c02016-04-13 09:08:05 +03002346 /* If mdtp state cannot be validate, block fastboot boot*/
2347 if(mdtp_activated(&is_mdtp_activated)){
2348 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2349 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2350 goto boot_failed;
2351 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002352 if(is_mdtp_activated){
2353 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002354 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002355 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002356#endif /* MDTP_SUPPORT */
2357
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002358#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302359#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002360 // send root of trust
2361 if(!send_rot_command((uint32_t)device.is_unlocked))
2362 ASSERT(0);
2363#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302364#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002365 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002366 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2367 * If not, continue booting.
2368 */
2369 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2370 {
2371 out_addr = (unsigned char *)target_get_scratch_address();
2372 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2373 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002374 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002375 ret = decompress((unsigned char *)(ptr + page_size),
2376 hdr->kernel_size, out_addr, out_avai_len,
2377 &dtb_offset, &out_len);
2378 if (ret)
2379 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002380 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002381 ASSERT(0);
2382 }
2383
Matthew Qin0b15b322015-05-19 05:20:54 -04002384 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002385 kptr = (struct kernel64_hdr *)out_addr;
2386 kernel_start_addr = out_addr;
2387 kernel_size = out_len;
2388 } else {
2389 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2390 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2391 kernel_size = hdr->kernel_size;
2392 }
2393
2394 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002395 * Update the kernel/ramdisk/tags address if the boot image header
2396 * has default values, these default values come from mkbootimg when
2397 * the boot image is flashed using fastboot flash:raw
2398 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002399 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002400
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002401 /* Get virtual addresses since the hdr saves physical addresses. */
2402 hdr->kernel_addr = VA(hdr->kernel_addr);
2403 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2404 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002405
Matthew Qinbb7923d2015-02-09 10:56:09 +08002406 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002407 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002408 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002409 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2410 {
2411 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002412 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002413 }
2414
Amol Jadicb524072012-08-09 16:40:18 -07002415#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002416 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002417 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002418 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002419
2420 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002421#else
2422 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2423 {
2424 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002425 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002426 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002427#endif
2428
2429 /* Load ramdisk & kernel */
2430 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002431 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002432
2433#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002434 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2435 {
2436 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002437 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002438 }
2439
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002440 /*
2441 * If dtb is not found look for appended DTB in the kernel.
2442 * If appended dev tree is found, update the atags with
2443 * memory address to the DTB appended location on RAM.
2444 * Else update with the atags address in the kernel header
2445 */
2446 if (!dtb_copied) {
2447 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002448 dtb = dev_tree_appended((void*)(ptr + page_size),
2449 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002450 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002451 if (!dtb) {
2452 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002453 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002454 }
Amol Jadicb524072012-08-09 16:40:18 -07002455 }
2456#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002457
2458 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002459 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002460
Dima Zavin77e41f32013-03-06 16:10:43 -08002461 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002462 (const char*) hdr->cmdline, board_machtype(),
2463 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002464
2465 /* fastboot already stop, it's no need to show fastboot menu */
2466 return;
2467boot_failed:
2468#if FBCON_DISPLAY_MSG
2469 /* revert to fastboot menu if boot failed */
2470 display_fastboot_menu();
2471#endif
2472 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002473}
2474
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002475void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002476{
2477 struct ptentry *ptn;
2478 struct ptable *ptable;
2479
2480 ptable = flash_get_ptable();
2481 if (ptable == NULL) {
2482 fastboot_fail("partition table doesn't exist");
2483 return;
2484 }
2485
2486 ptn = ptable_find(ptable, arg);
2487 if (ptn == NULL) {
2488 fastboot_fail("unknown partition name");
2489 return;
2490 }
2491
2492 if (flash_erase(ptn)) {
2493 fastboot_fail("failed to erase partition");
2494 return;
2495 }
2496 fastboot_okay("");
2497}
2498
Bikas Gurungd48bd242010-09-04 19:54:32 -07002499
2500void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2501{
2502 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002503 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002504 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002505 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002506
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002507#if VERIFIED_BOOT
2508 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2509 {
2510 if(!device.is_unlocked)
2511 {
2512 fastboot_fail("unlock device to erase keystore");
2513 return;
2514 }
2515 }
2516#endif
2517
Kinson Chikf1a43512011-07-14 11:28:39 -07002518 index = partition_get_index(arg);
2519 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002520 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002521
Kinson Chikf1a43512011-07-14 11:28:39 -07002522 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002523 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002524 return;
2525 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002526
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002527 lun = partition_get_lun(index);
2528 mmc_set_lun(lun);
2529
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002530 if (platform_boot_dev_isemmc())
2531 {
2532 if (mmc_erase_card(ptn, size)) {
2533 fastboot_fail("failed to erase partition\n");
2534 return;
2535 }
2536 } else {
2537 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2538 size = partition_get_size(index);
2539 if (size > DEFAULT_ERASE_SIZE)
2540 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002541
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002542 /* Simple inefficient version of erase. Just writing
2543 0 in first several blocks */
2544 if (mmc_write(ptn , size, (unsigned int *)out)) {
2545 fastboot_fail("failed to erase partition");
2546 return;
2547 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002548 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002549#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302550#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002551 if(!(strncmp(arg, "userdata", 8)))
2552 if(send_delete_keys_to_tz())
2553 ASSERT(0);
2554#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302555#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002556 fastboot_okay("");
2557}
2558
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002559void cmd_erase(const char *arg, void *data, unsigned sz)
2560{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002561#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002562 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002563 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002564 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002565 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002566 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002567 return;
2568 }
2569 }
2570#endif
2571
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002572 if(target_is_emmc_boot())
2573 cmd_erase_mmc(arg, data, sz);
2574 else
2575 cmd_erase_nand(arg, data, sz);
2576}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002577
Channagoud Kadabiad259832015-05-29 11:14:17 -07002578static uint32_t aboot_get_secret_key()
2579{
2580 /* 0 is invalid secret key, update this implementation to return
2581 * device specific unique secret key
2582 */
2583 return 0;
2584}
2585
Ajay Dudani5c761132011-04-07 20:19:04 -07002586void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002587{
2588 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002589 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002590 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002591 char *token = NULL;
2592 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002593 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002594 uint8_t lun = 0;
2595 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002596
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002597 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002598 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002599 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002600 if(token)
2601 {
2602 lun = atoi(token);
2603 mmc_set_lun(lun);
2604 lun_set = true;
2605 }
2606
Mao Jinlong226f33a2014-07-04 17:24:10 +08002607 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002608 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002609 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2610 {
2611 if (!aboot_frp_unlock(pname, data, sz))
2612 {
2613 fastboot_info("FRP unlock successful");
2614 fastboot_okay("");
2615 }
2616 else
2617 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2618
2619 return;
2620 }
2621
Mao Jinlong226f33a2014-07-04 17:24:10 +08002622 if (!strcmp(pname, "partition"))
2623 {
2624 dprintf(INFO, "Attempt to write partition image.\n");
2625 if (write_partition(sz, (unsigned char *) data)) {
2626 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002627 return;
2628 }
2629 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002630 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002631 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002632#if VERIFIED_BOOT
2633 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2634 {
2635 if(!device.is_unlocked)
2636 {
2637 fastboot_fail("unlock device to flash keystore");
2638 return;
2639 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302640 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002641 {
2642 fastboot_fail("image is not a keystore file");
2643 return;
2644 }
2645 }
2646#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002647 index = partition_get_index(pname);
2648 ptn = partition_get_offset(index);
2649 if(ptn == 0) {
2650 fastboot_fail("partition table doesn't exist");
2651 return;
2652 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002653
Mao Jinlong226f33a2014-07-04 17:24:10 +08002654 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2655 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2656 fastboot_fail("image is not a boot image");
2657 return;
2658 }
2659 }
2660
2661 if(!lun_set)
2662 {
2663 lun = partition_get_lun(index);
2664 mmc_set_lun(lun);
2665 }
2666
2667 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05302668 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002669 fastboot_fail("size too large");
2670 return;
2671 }
2672 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2673 fastboot_fail("flash write failure");
2674 return;
2675 }
Greg Grisco6e754772011-06-23 12:19:39 -07002676 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002677 }
2678 fastboot_okay("");
2679 return;
2680}
2681
Ajay Dudanide984792015-03-02 09:57:41 -08002682void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2683{
2684 int i, images;
2685 meta_header_t *meta_header;
2686 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302687 /*End of the image address*/
2688 uintptr_t data_end;
2689
2690 if( (UINT_MAX - sz) > (uintptr_t)data )
2691 data_end = (uintptr_t)data + sz;
2692 else
2693 {
2694 fastboot_fail("Cannot flash: image header corrupt");
2695 return;
2696 }
2697
2698 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
2699 {
2700 fastboot_fail("Cannot flash: image header corrupt");
2701 return;
2702 }
Ajay Dudanide984792015-03-02 09:57:41 -08002703
lijuang8ee21882016-04-19 16:57:11 +08002704 /* If device is locked:
2705 * Forbid to flash image to avoid the device to bypass the image
2706 * which with "any" name other than bootloader. Because it maybe
2707 * a meta package of all partitions.
2708 */
2709#if VERIFIED_BOOT
2710 if (target_build_variant_user()) {
2711 if (!device.is_unlocked) {
2712 fastboot_fail("Device is locked, meta image flashing is not allowed");
2713 return;
2714 }
2715#if !VBOOT_MOTA
2716 if(!device.is_unlock_critical) {
2717 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
2718 return;
2719 }
2720#endif
2721 }
2722#endif
2723
Ajay Dudanide984792015-03-02 09:57:41 -08002724 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302725 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
2726 {
2727 fastboot_fail("Cannot flash: image header corrupt");
2728 return;
2729 }
Ajay Dudanide984792015-03-02 09:57:41 -08002730 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2731
2732 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2733
2734 for (i=0; i<images; i++) {
2735
2736 if((img_header_entry[i].ptn_name == NULL) ||
2737 (img_header_entry[i].start_offset == 0) ||
2738 (img_header_entry[i].size == 0))
2739 break;
2740
Parth Dixit3e2d3032016-03-04 17:11:52 +05302741 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
2742 + img_header_entry[i].size) )
2743 {
2744 fastboot_fail("Cannot flash: image size mismatch");
2745 break;
2746 }
2747
Ajay Dudanide984792015-03-02 09:57:41 -08002748 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2749 (void *) data + img_header_entry[i].start_offset,
2750 img_header_entry[i].size);
2751 }
2752
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002753 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2754 {
2755 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2756 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2757 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2758 }
2759 else
2760 {
2761 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2762 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2763 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2764 }
2765
2766 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002767 fastboot_okay("");
2768 return;
2769}
2770
Ajay Dudani5c761132011-04-07 20:19:04 -07002771void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2772{
2773 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002774 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002775 uint32_t *fill_buf = NULL;
2776 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002777 sparse_header_t *sparse_header;
2778 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002779 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002780 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302781 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002782 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302783 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002784 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302785 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05302786 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002787
Kinson Chikf1a43512011-07-14 11:28:39 -07002788 index = partition_get_index(arg);
2789 ptn = partition_get_offset(index);
2790 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002791 fastboot_fail("partition table doesn't exist");
2792 return;
2793 }
2794
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302795 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302796
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002797 lun = partition_get_lun(index);
2798 mmc_set_lun(lun);
2799
vijay kumar800255e2015-04-24 20:26:19 +05302800 if (sz < sizeof(sparse_header_t)) {
2801 fastboot_fail("size too low");
2802 return;
2803 }
2804
Ajay Dudani5c761132011-04-07 20:19:04 -07002805 /* Read and skip over sparse image header */
2806 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302807
vijay kumar1321f342015-03-27 12:13:42 +05302808 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002809 fastboot_fail("size too large");
2810 return;
2811 }
2812
vijay kumarde4fcf62015-04-23 13:05:49 +05302813 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302814
Parth Dixit64b1a482016-03-07 16:31:26 +05302815 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302816 fastboot_fail("buffer overreads occured due to invalid sparse header");
2817 return;
2818 }
2819
vijay kumarde4fcf62015-04-23 13:05:49 +05302820 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002821 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302822 fastboot_fail("sparse header size mismatch");
2823 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002824 }
2825
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002826 dprintf (SPEW, "=== Sparse Image Header ===\n");
2827 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2828 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2829 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2830 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2831 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2832 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2833 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2834 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002835
2836 /* Start processing chunks */
2837 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2838 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302839 /* Make sure the total image size does not exceed the partition size */
2840 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2841 fastboot_fail("size too large");
2842 return;
2843 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002844 /* Read and skip over chunk header */
2845 chunk_header = (chunk_header_t *) data;
2846 data += sizeof(chunk_header_t);
2847
Parth Dixit64b1a482016-03-07 16:31:26 +05302848 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302849 fastboot_fail("buffer overreads occured due to invalid sparse header");
2850 return;
2851 }
2852
Ajay Dudani5c761132011-04-07 20:19:04 -07002853 dprintf (SPEW, "=== Chunk Header ===\n");
2854 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2855 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2856 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2857
vijay kumar800255e2015-04-24 20:26:19 +05302858 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002859 {
vijay kumar800255e2015-04-24 20:26:19 +05302860 fastboot_fail("chunk header size mismatch");
2861 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002862 }
2863
wufeng.jiang813dc352015-06-02 23:02:46 -04002864 if (!sparse_header->blk_sz ){
2865 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302866 return;
2867 }
2868
wufeng.jiang813dc352015-06-02 23:02:46 -04002869 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2870
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302871 /* Make sure that the chunk size calculated from sparse image does not
2872 * exceed partition size
2873 */
2874 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2875 {
2876 fastboot_fail("Chunk data size exceeds partition size");
2877 return;
2878 }
2879
Ajay Dudani5c761132011-04-07 20:19:04 -07002880 switch (chunk_header->chunk_type)
2881 {
2882 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002883 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002884 chunk_data_sz))
2885 {
2886 fastboot_fail("Bogus chunk size for chunk type Raw");
2887 return;
2888 }
2889
Parth Dixit64b1a482016-03-07 16:31:26 +05302890 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05302891 fastboot_fail("buffer overreads occured due to invalid sparse header");
2892 return;
2893 }
2894
wufeng.jiang813dc352015-06-02 23:02:46 -04002895 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2896 otherwise it will return in the line above
2897 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002898 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002899 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002900 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002901 {
2902 fastboot_fail("flash write failure");
2903 return;
2904 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302905 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2906 fastboot_fail("Bogus size for RAW chunk type");
2907 return;
2908 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002909 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002910 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002911 break;
2912
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002913 case CHUNK_TYPE_FILL:
2914 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2915 sizeof(uint32_t)))
2916 {
2917 fastboot_fail("Bogus chunk size for chunk type FILL");
2918 return;
2919 }
2920
2921 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2922 if (!fill_buf)
2923 {
2924 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2925 return;
2926 }
2927
Parth Dixit64b1a482016-03-07 16:31:26 +05302928 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05302929 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05302930 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05302931 return;
2932 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002933 fill_val = *(uint32_t *)data;
2934 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002935
2936 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2937 {
2938 fill_buf[i] = fill_val;
2939 }
2940
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05302941 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
2942 {
2943 fastboot_fail("bogus size for chunk FILL type");
2944 free(fill_buf);
2945 return;
2946 }
2947
wufeng.jiang813dc352015-06-02 23:02:46 -04002948 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002949 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302950 /* Make sure that the data written to partition does not exceed partition size */
2951 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2952 {
2953 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05302954 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302955 return;
2956 }
2957
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002958 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2959 sparse_header->blk_sz,
2960 fill_buf))
2961 {
2962 fastboot_fail("flash write failure");
2963 free(fill_buf);
2964 return;
2965 }
2966
2967 total_blocks++;
2968 }
2969
2970 free(fill_buf);
2971 break;
2972
Ajay Dudani5c761132011-04-07 20:19:04 -07002973 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302974 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2975 fastboot_fail("bogus size for chunk DONT CARE type");
2976 return;
2977 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002978 total_blocks += chunk_header->chunk_sz;
2979 break;
2980
Ajay Dudani5c761132011-04-07 20:19:04 -07002981 case CHUNK_TYPE_CRC:
2982 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2983 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002984 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002985 return;
2986 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302987 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2988 fastboot_fail("bogus size for chunk CRC type");
2989 return;
2990 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002991 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05302992 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05302993 fastboot_fail("integer overflow occured");
2994 return;
2995 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002996 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05302997 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302998 fastboot_fail("buffer overreads occured due to invalid sparse header");
2999 return;
3000 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003001 break;
3002
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003003 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003004 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003005 fastboot_fail("Unknown chunk type");
3006 return;
3007 }
3008 }
3009
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003010 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3011 total_blocks, sparse_header->total_blks);
3012
3013 if(total_blocks != sparse_header->total_blks)
3014 {
3015 fastboot_fail("sparse image write failure");
3016 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003017
3018 fastboot_okay("");
3019 return;
3020}
3021
3022void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3023{
3024 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003025 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003026
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003027#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003028 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3029 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003030 int ret=0;
3031 uint32 major_version=0;
3032 uint32 minor_version=0;
3033
3034 ret = scm_svc_version(&major_version,&minor_version);
3035 if(!ret)
3036 {
3037 if(major_version >= 2)
3038 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003039 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003040 {
3041 ret = encrypt_scm((uint32 **) &data, &sz);
3042 if (ret != 0) {
3043 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3044 return;
3045 }
3046
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003047 /* Protect only for SSD */
3048 if (!strcmp(arg, "ssd")) {
3049 ret = scm_protect_keystore((uint32 *) data, sz);
3050 if (ret != 0) {
3051 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3052 return;
3053 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003054 }
3055 }
3056 else
3057 {
3058 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3059 if(ret != 0)
3060 {
3061 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3062 return;
3063 }
3064 }
3065 }
3066 else
3067 {
3068 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3069 magic_number[1] == DECRYPT_MAGIC_1)
3070 {
3071 ret = decrypt_scm((uint32 **) &data, &sz);
3072 if (ret != 0) {
3073 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3074 return;
3075 }
3076 }
3077 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3078 magic_number[1] == ENCRYPT_MAGIC_1)
3079 {
3080 ret = encrypt_scm((uint32 **) &data, &sz);
3081 if (ret != 0) {
3082 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3083 return;
3084 }
3085 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003086 }
3087 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003088 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003089 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003090 dprintf(CRITICAL,"INVALID SVC Version\n");
3091 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003092 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003093#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003094
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003095#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003096 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003097 {
lijuang511a2b52015-08-14 20:50:51 +08003098 /* if device is locked:
3099 * common partition will not allow to be flashed
3100 * critical partition will allow to flash image.
3101 */
3102 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3103 fastboot_fail("Partition flashing is not allowed");
3104 return;
3105 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303106#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003107 /* if device critical is locked:
3108 * common partition will allow to be flashed
3109 * critical partition will not allow to flash image.
3110 */
3111 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
3112 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003113 return;
3114 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303115#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003116 }
3117#endif
3118
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003119 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003120 meta_header = (meta_header_t *) data;
3121 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003122 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003123 else if (meta_header->magic == META_HEADER_MAGIC)
3124 cmd_flash_meta_img(arg, data, sz);
3125 else
3126 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003127
3128#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303129#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003130 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
3131 {
3132 // reset dm_verity mode to enforcing
3133 device.verity_mode = 1;
3134 write_device_info(&device);
3135 }
3136#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303137#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003138
Ajay Dudani5c761132011-04-07 20:19:04 -07003139 return;
3140}
3141
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003142void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3143{
3144 struct ptentry *sys_ptn;
3145 struct ptable *ptable;
3146
3147 ptable = flash_get_ptable();
3148 if (ptable == NULL) {
3149 fastboot_fail("partition table doesn't exist");
3150 return;
3151 }
3152
3153 sys_ptn = ptable_find(ptable, "system");
3154 if (sys_ptn == NULL) {
3155 fastboot_fail("system partition not found");
3156 return;
3157 }
3158
3159 sz = ROUND_TO_PAGE(sz, page_mask);
3160 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3161 fastboot_fail("update_ubi_vol failed");
3162 else
3163 fastboot_okay("");
3164}
3165
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003166void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003167{
3168 struct ptentry *ptn;
3169 struct ptable *ptable;
3170 unsigned extra = 0;
3171
3172 ptable = flash_get_ptable();
3173 if (ptable == NULL) {
3174 fastboot_fail("partition table doesn't exist");
3175 return;
3176 }
3177
3178 ptn = ptable_find(ptable, arg);
3179 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003180 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3181 arg);
3182 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003183 return;
3184 }
3185
3186 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
3187 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3188 fastboot_fail("image is not a boot image");
3189 return;
3190 }
3191 }
3192
Amol Jadi5c61a952012-05-04 17:05:35 -07003193 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003194 || !strcmp(ptn->name, "userdata")
3195 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003196 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003197 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003198 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003199 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003200 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003201
3202 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003203 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3204 if (flash_ubi_img(ptn, data, sz)) {
3205 fastboot_fail("flash write failure");
3206 return;
3207 }
3208 } else {
3209 if (flash_write(ptn, extra, data, sz)) {
3210 fastboot_fail("flash write failure");
3211 return;
3212 }
Dima Zavin214cc642009-01-26 11:16:21 -08003213 }
3214 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3215 fastboot_okay("");
3216}
3217
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003218void cmd_flash(const char *arg, void *data, unsigned sz)
3219{
3220 if(target_is_emmc_boot())
3221 cmd_flash_mmc(arg, data, sz);
3222 else
3223 cmd_flash_nand(arg, data, sz);
3224}
3225
Dima Zavin214cc642009-01-26 11:16:21 -08003226void cmd_continue(const char *arg, void *data, unsigned sz)
3227{
3228 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003229 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003230
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003231 if (target_is_emmc_boot())
3232 {
lijuanga40d6302015-07-20 20:10:13 +08003233#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003234 /* Exit keys' detection thread firstly */
3235 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003236#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003237 boot_linux_from_mmc();
3238 }
3239 else
3240 {
3241 boot_linux_from_flash();
3242 }
Dima Zavin214cc642009-01-26 11:16:21 -08003243}
3244
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003245void cmd_reboot(const char *arg, void *data, unsigned sz)
3246{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003247 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003248 fastboot_okay("");
3249 reboot_device(0);
3250}
3251
3252void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3253{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003254 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003255 fastboot_okay("");
3256 reboot_device(FASTBOOT_MODE);
3257}
3258
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003259void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3260{
3261 dprintf(INFO, "Enabling charger screen check\n");
3262 device.charger_screen_enabled = 1;
3263 write_device_info(&device);
3264 fastboot_okay("");
3265}
3266
3267void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3268{
3269 dprintf(INFO, "Disabling charger screen check\n");
3270 device.charger_screen_enabled = 0;
3271 write_device_info(&device);
3272 fastboot_okay("");
3273}
3274
lijuanga25d8bb2015-09-16 13:11:52 +08003275void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3276{
3277 char *p = NULL;
3278 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303279 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003280
3281 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303282 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003283 if (p) {
3284 if (!strncmp(p, "0", 1)) {
3285 device.charger_screen_enabled = 0;
3286 } else if (!strncmp(p, "1", 1)) {
3287 device.charger_screen_enabled = 1;
3288 }
3289 }
3290 }
3291
3292 /* update charger_screen_enabled value for getvar
3293 * command
3294 */
3295 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3296 device.charger_screen_enabled);
3297
3298 write_device_info(&device);
3299 fastboot_okay("");
3300}
3301
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303302void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3303{
3304 dprintf(INFO, "Selecting display panel %s\n", arg);
3305 if (arg)
3306 strlcpy(device.display_panel, arg,
3307 sizeof(device.display_panel));
3308 write_device_info(&device);
3309 fastboot_okay("");
3310}
3311
Shashank Mittal162244e2011-08-08 19:01:25 -07003312void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3313{
lijuang4ece1e72015-08-14 21:02:36 +08003314 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303315}
3316
3317void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3318{
lijuang4ece1e72015-08-14 21:02:36 +08003319 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303320 if(!is_allow_unlock) {
3321 fastboot_fail("oem unlock is not allowed");
3322 return;
3323 }
3324
lijuang4ece1e72015-08-14 21:02:36 +08003325 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303326
lijuang4ece1e72015-08-14 21:02:36 +08003327 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303328 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003329
vijay kumarc65876c2015-04-24 13:29:16 +05303330 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3331 write_misc(0, &msg, sizeof(msg));
3332
3333 fastboot_okay("");
3334 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003335 }
3336 fastboot_okay("");
3337}
3338
Channagoud Kadabiad259832015-05-29 11:14:17 -07003339static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3340{
3341 int ret = 1;
3342 uint32_t secret_key;
3343 char seckey_buffer[MAX_RSP_SIZE];
3344
3345 secret_key = aboot_get_secret_key();
3346 if (secret_key)
3347 {
3348 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3349 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3350 {
3351 is_allow_unlock = true;
3352 write_allow_oem_unlock(is_allow_unlock);
3353 ret = 0;
3354 }
3355 }
3356 return ret;
3357}
3358
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003359void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3360{
lijuang4ece1e72015-08-14 21:02:36 +08003361 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003362}
3363
Shashank Mittala0032282011-08-26 14:50:11 -07003364void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3365{
lijuang511a2b52015-08-14 20:50:51 +08003366 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003367 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003368 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003369 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3370 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303371#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003372 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3373 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303374#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003375 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003376 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303377 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3378 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003379 fastboot_okay("");
3380}
3381
lijuang511a2b52015-08-14 20:50:51 +08003382void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3383{
3384 char response[MAX_RSP_SIZE];
3385 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3386 fastboot_info(response);
3387 fastboot_okay("");
3388}
3389
3390void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3391{
lijuang4ece1e72015-08-14 21:02:36 +08003392 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003393}
3394
3395void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3396{
lijuang4ece1e72015-08-14 21:02:36 +08003397 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003398}
3399
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003400void cmd_preflash(const char *arg, void *data, unsigned sz)
3401{
3402 fastboot_okay("");
3403}
3404
Mao Flynn7b379f32015-04-20 00:28:30 +08003405static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303406
Mao Flynn7b379f32015-04-20 00:28:30 +08003407int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303408{
Mao Flynn7b379f32015-04-20 00:28:30 +08003409 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303410 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003411 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303412 return -1;
3413 return 0;
3414}
3415
Mao Flynn7b379f32015-04-20 00:28:30 +08003416int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003417{
3418 struct ptentry *ptn;
3419 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003420 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003421 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003422
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303423 ptable = flash_get_ptable();
3424 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003425 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3426 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303427 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003428
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303429 ptn = ptable_find(ptable, "splash");
3430 if (ptn == NULL) {
3431 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003432 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303433 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003434 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303435 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003436 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303437 }
3438
Mao Flynn7b379f32015-04-20 00:28:30 +08003439 header = (struct logo_img_header *)logo_header;
3440 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303441 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003442 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303443 }
3444
3445 fb_display = fbcon_display();
3446 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003447 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3448 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3449
3450 /* if the logo is full-screen size, remove "fbcon_clear()" */
3451 if ((header->width != fb_display->width)
3452 || (header->height != fb_display->height))
3453 fbcon_clear();
3454
3455 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3456 (uint32_t *)base,
3457 (header->blocks * 512))) {
3458 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3459 return -1;
3460 }
3461 fbcon_extract_to_screen(header, base);
3462 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003463 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003464
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003465 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3466 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003467 return -1;
3468 }
3469
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303470 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003471 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3472 (uint32_t *)base,
3473 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303474 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303475 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003476 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003477 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303478 }
3479
Mao Flynn7b379f32015-04-20 00:28:30 +08003480 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303481}
3482
Mao Flynn7b379f32015-04-20 00:28:30 +08003483int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303484{
3485 int index = INVALID_PTN;
3486 unsigned long long ptn = 0;
3487 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003488 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003489 uint32_t blocksize, realsize, readsize;
3490 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303491
3492 index = partition_get_index("splash");
3493 if (index == 0) {
3494 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003495 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303496 }
3497
3498 ptn = partition_get_offset(index);
3499 if (ptn == 0) {
3500 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003501 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303502 }
3503
Mao Flynn1893a7f2015-06-03 12:03:36 +08003504 mmc_set_lun(partition_get_lun(index));
3505
3506 blocksize = mmc_get_device_blocksize();
3507 if (blocksize == 0) {
3508 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3509 return -1;
3510 }
3511
3512 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003513 if (!fb_display)
3514 {
3515 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3516 return -1;
3517 }
3518
Mao Flynn1893a7f2015-06-03 12:03:36 +08003519 base = (uint8_t *) fb_display->base;
3520
3521 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303522 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003523 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303524 }
3525
Mao Flynn1893a7f2015-06-03 12:03:36 +08003526 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003527 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303528 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003529 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303530 }
3531
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303532 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003533 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3534 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003535
Mao Flynn1893a7f2015-06-03 12:03:36 +08003536 realsize = header->blocks * 512;
3537 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3538
3539 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003540 if ((header->width != fb_display->width)
3541 || (header->height != fb_display->height))
3542 fbcon_clear();
3543
Mao Flynn1893a7f2015-06-03 12:03:36 +08003544 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003545 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3546 return -1;
3547 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003548
Mao Flynn1893a7f2015-06-03 12:03:36 +08003549 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3550 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303551
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003552 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3553 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003554 return -1;
3555 }
3556
3557 realsize = header->width * header->height * fb_display->bpp / 8;
3558 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3559
3560 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3561 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3562 fbcon_clear();
3563 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3564 return -1;
3565 }
3566 } else {
3567 if (mmc_read(ptn + blocksize ,
3568 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3569 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3570 return -1;
3571 }
3572 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3573 }
3574 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303575 }
3576
Mao Flynn7b379f32015-04-20 00:28:30 +08003577 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303578}
3579
Mao Flynn7b379f32015-04-20 00:28:30 +08003580int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303581{
3582 if (target_is_emmc_boot()) {
3583 return splash_screen_mmc();
3584 } else {
3585 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003586 }
3587}
3588
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003589/* Get the size from partiton name */
3590static void get_partition_size(const char *arg, char *response)
3591{
3592 uint64_t ptn = 0;
3593 uint64_t size;
3594 int index = INVALID_PTN;
3595
3596 index = partition_get_index(arg);
3597
3598 if (index == INVALID_PTN)
3599 {
3600 dprintf(CRITICAL, "Invalid partition index\n");
3601 return;
3602 }
3603
3604 ptn = partition_get_offset(index);
3605
3606 if(!ptn)
3607 {
3608 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3609 return;
3610 }
3611
3612 size = partition_get_size(index);
3613
3614 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3615 return;
3616}
3617
3618/*
3619 * Publish the partition type & size info
3620 * fastboot getvar will publish the required information.
3621 * fastboot getvar partition_size:<partition_name>: partition size in hex
3622 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3623 */
3624static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3625{
3626 uint8_t i;
3627
3628 for (i = 0; i < num_parts; i++) {
3629 get_partition_size(info[i].part_name, info[i].size_response);
3630
3631 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3632 {
3633 dprintf(CRITICAL, "partition size name truncated\n");
3634 return;
3635 }
3636 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3637 {
3638 dprintf(CRITICAL, "partition type name truncated\n");
3639 return;
3640 }
3641
3642 /* publish partition size & type info */
3643 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3644 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3645 }
3646}
3647
lijuang4ece1e72015-08-14 21:02:36 +08003648void get_product_name(unsigned char *buf)
3649{
3650 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3651 return;
3652}
3653
3654void get_bootloader_version(unsigned char *buf)
3655{
3656 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3657 return;
3658}
3659
3660void get_baseband_version(unsigned char *buf)
3661{
3662 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3663 return;
3664}
3665
3666bool is_device_locked()
3667{
3668 return device.is_unlocked ? false:true;
3669}
3670
Amol Jadi5edf3552013-07-23 14:15:34 -07003671/* register commands and variables for fastboot */
3672void aboot_fastboot_register_commands(void)
3673{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003674 int i;
lijuangf16461c2015-08-03 17:09:34 +08003675 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003676
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003677 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003678 /* By default the enabled list is empty. */
3679 {"", NULL},
3680 /* move commands enclosed within the below ifndef to here
3681 * if they need to be enabled in user build.
3682 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003683#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003684 /* Register the following commands only for non-user builds */
3685 {"flash:", cmd_flash},
3686 {"erase:", cmd_erase},
3687 {"boot", cmd_boot},
3688 {"continue", cmd_continue},
3689 {"reboot", cmd_reboot},
3690 {"reboot-bootloader", cmd_reboot_bootloader},
3691 {"oem unlock", cmd_oem_unlock},
3692 {"oem unlock-go", cmd_oem_unlock_go},
3693 {"oem lock", cmd_oem_lock},
3694 {"flashing unlock", cmd_oem_unlock},
3695 {"flashing lock", cmd_oem_lock},
3696 {"flashing lock_critical", cmd_flashing_lock_critical},
3697 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3698 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3699 {"oem device-info", cmd_oem_devinfo},
3700 {"preflash", cmd_preflash},
3701 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3702 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003703 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003704 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003705#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003706 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003707#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003708#endif
lijuang511a2b52015-08-14 20:50:51 +08003709 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003710
3711 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3712 for (i = 1; i < fastboot_cmds_count; i++)
3713 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3714
Amol Jadi5edf3552013-07-23 14:15:34 -07003715 /* publish variables and their values */
3716 fastboot_publish("product", TARGET(BOARD));
3717 fastboot_publish("kernel", "lk");
3718 fastboot_publish("serialno", sn_buf);
3719
3720 /*
3721 * partition info is supported only for emmc partitions
3722 * Calling this for NAND prints some error messages which
3723 * is harmless but misleading. Avoid calling this for NAND
3724 * devices.
3725 */
3726 if (target_is_emmc_boot())
3727 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3728
3729 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003730 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3731 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003732 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003733 /* Is the charger screen check enabled */
3734 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3735 device.charger_screen_enabled);
3736 fastboot_publish("charger-screen-enabled",
3737 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003738 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303739 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3740 device.display_panel);
3741 fastboot_publish("display-panel",
3742 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003743 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3744 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003745 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3746 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3747 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3748 target_is_emmc_boot()? "eMMC":"UFS");
3749 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003750#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003751 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003752 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003753 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003754#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003755}
3756
Brian Swetland9c4c0752009-01-25 16:23:50 -08003757void aboot_init(const struct app_descriptor *app)
3758{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003759 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003760
lijuang39831732016-01-08 17:49:02 +08003761 /* Initialise wdog to catch early lk crashes */
3762#if WDOG_SUPPORT
3763 msm_wdog_init();
3764#endif
3765
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003766 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003767 if (target_is_emmc_boot())
3768 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003769 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003770 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303771 mmc_blocksize = mmc_get_device_blocksize();
3772 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003773 }
3774 else
3775 {
3776 page_size = flash_page_size();
3777 page_mask = page_size - 1;
3778 }
3779
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003780 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3781
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003782 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303783 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003784
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003785 /* Display splash screen if enabled */
3786#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003787#if NO_ALARM_DISPLAY
3788 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003789#endif
lijuang99c02d82015-02-13 19:04:34 +08003790 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07003791#if DISPLAY_HDMI_PRIMARY
3792 if (!strlen(device.display_panel))
3793 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
3794 sizeof(device.display_panel));
3795#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003796#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003797 /* Wait if the display shutdown is in progress */
3798 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003799 if (!pm_appsbl_display_init_done())
3800 target_display_init(device.display_panel);
3801 else
3802 display_image_on_screen();
3803#else
lijuang99c02d82015-02-13 19:04:34 +08003804 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003805#endif
lijuang99c02d82015-02-13 19:04:34 +08003806 dprintf(SPEW, "Display Init: Done\n");
3807#if NO_ALARM_DISPLAY
3808 }
3809#endif
3810#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003811
Greg Griscod6250552011-06-29 14:40:23 -07003812 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003813 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003814
Dhaval Patel223ec952013-07-18 14:49:44 -07003815 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3816
Matthew Qindefd5562014-07-11 18:02:40 +08003817 /*
3818 * Check power off reason if user force reset,
3819 * if yes phone will do normal boot.
3820 */
3821 if (is_user_force_reset())
3822 goto normal_boot;
3823
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003824 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003825 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003826 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003827 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08003828 reboot_device(EMERGENCY_DLOAD);
3829 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
3830
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003831 boot_into_fastboot = true;
3832 }
3833 if (!boot_into_fastboot)
3834 {
3835 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3836 boot_into_recovery = 1;
3837 if (!boot_into_recovery &&
3838 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003839 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003840 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003841 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003842 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003843 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003844 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003845
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003846#if USE_PON_REBOOT_REG
3847 reboot_mode = check_hard_reboot_mode();
3848#else
Ajay Dudani77421292010-10-27 19:34:06 -07003849 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003850#endif
3851 if (reboot_mode == RECOVERY_MODE)
3852 {
Ajay Dudani77421292010-10-27 19:34:06 -07003853 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003854 }
3855 else if(reboot_mode == FASTBOOT_MODE)
3856 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003857 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003858 }
3859 else if(reboot_mode == ALARM_BOOT)
3860 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003861 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003862 }
Parth Dixit207573a2015-10-27 17:26:21 +05303863#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303864#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003865 else if (reboot_mode == DM_VERITY_ENFORCING)
3866 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003867 device.verity_mode = 1;
3868 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003869 }
3870 else if (reboot_mode == DM_VERITY_LOGGING)
3871 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003872 device.verity_mode = 0;
3873 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003874 }
3875 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3876 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003877 if(send_delete_keys_to_tz())
3878 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003879 }
Parth Dixit207573a2015-10-27 17:26:21 +05303880#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303881#endif
Ajay Dudani77421292010-10-27 19:34:06 -07003882
Matthew Qindefd5562014-07-11 18:02:40 +08003883normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003884 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003885 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003886 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003887 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003888 if(emmc_recovery_init())
3889 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3890 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003891 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003892 if((device.is_unlocked) || (device.is_tampered))
3893 {
3894 #ifdef TZ_TAMPER_FUSE
3895 set_tamper_fuse_cmd();
3896 #endif
3897 #if USE_PCOM_SECBOOT
3898 set_tamper_flag(device.is_tampered);
3899 #endif
3900 }
Shashank Mittala0032282011-08-26 14:50:11 -07003901 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003902
Pavel Nedev5d91d412013-04-29 11:34:24 +03003903 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003904 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003905 else
3906 {
3907 recovery_init();
3908 #if USE_PCOM_SECBOOT
3909 if((device.is_unlocked) || (device.is_tampered))
3910 set_tamper_flag(device.is_tampered);
3911 #endif
3912 boot_linux_from_flash();
3913 }
3914 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3915 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003916 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003917
Amol Jadi5edf3552013-07-23 14:15:34 -07003918 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003919
Amol Jadi5edf3552013-07-23 14:15:34 -07003920 /* register aboot specific fastboot commands */
3921 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003922
Amol Jadi5edf3552013-07-23 14:15:34 -07003923 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003924 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003925
3926 /* initialize and start fastboot */
3927 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003928#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08003929 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08003930#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003931}
3932
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003933uint32_t get_page_size()
3934{
3935 return page_size;
3936}
3937
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003938/*
3939 * Calculated and save hash (SHA256) for non-signed boot image.
3940 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003941 * @param image_addr - Boot image address
3942 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003943 *
3944 * @return int - 0 on success, negative value on failure.
3945 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003946static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003947{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003948 unsigned int digest[8];
3949#if IMAGE_VERIF_ALGO_SHA1
3950 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3951#else
3952 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3953#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003954
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003955 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003956 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003957
3958 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003959 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003960
3961 return 0;
3962}
3963
Brian Swetland9c4c0752009-01-25 16:23:50 -08003964APP_START(aboot)
3965 .init = aboot_init,
3966APP_END