blob: 9d9837c3b95f94e562d3883c593d44b86eb77506 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
lijuang9a7d3b92015-11-30 14:41:24 +08005 * Copyright (c) 2009-2016, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080060#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053061#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080062#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080063#if USE_RPMB_FOR_DEVINFO
64#include <rpmb.h>
65#endif
Dima Zavin214cc642009-01-26 11:16:21 -080066
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070067#if ENABLE_WBC
68#include <pm_app_smbchg.h>
69#endif
70
Neeti Desai17379b82012-06-04 18:42:53 -070071#if DEVICE_TREE
72#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070073#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070074#endif
75
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053076#if WDOG_SUPPORT
77#include <wdog.h>
78#endif
79
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070080#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070081#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080082#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080083#include "bootimg.h"
84#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070085#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080086#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070087#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070088#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070089#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020091#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070092#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080093#include <menu_keys_detect.h>
94#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070095#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070096
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070097extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070098extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070099extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700100extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530101 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700102
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700103void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700104void write_device_info_mmc(device_info *dev);
105void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700106static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700107static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700108
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700109/* fastboot command function pointer */
110typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
111
112struct fastboot_cmd_desc {
113 char * name;
114 fastboot_cmd_fn cb;
115};
116
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700117#define EXPAND(NAME) #NAME
118#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700119
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800120#ifdef MEMBASE
121#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
122#else
David Ng183a7422009-12-07 14:55:21 -0800123#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800124#endif
125
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700126#ifndef MEMSIZE
127#define MEMSIZE 1024*1024
128#endif
129
130#define MAX_TAGS_SIZE 1024
131
Kun Liang2f1601a2013-08-12 16:29:54 +0800132/* make 4096 as default size to ensure EFS,EXT4's erasing */
133#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700134#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530135#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800136
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700137#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700138#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800139
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800140#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
141
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700142#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
143
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800144#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700145static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700146#else
David Ng183a7422009-12-07 14:55:21 -0800147static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700148#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800149static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300150static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800151static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800152static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800153static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700154static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300155static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200156static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800157
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800158static const char *baseband_apq = " androidboot.baseband=apq";
159static const char *baseband_msm = " androidboot.baseband=msm";
160static const char *baseband_csfb = " androidboot.baseband=csfb";
161static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700162static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800163static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700164static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800165static const char *baseband_dsda = " androidboot.baseband=dsda";
166static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800167static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800168static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530169static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
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;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530436 case BASEBAND_APQ_NOWGR:
437 cmdline_len += strlen(baseband_apq_nowgr);
438 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800439 }
440
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800441 if (cmdline) {
442 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530443 target_display_panel_node(display_panel_buf,
444 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800445 strlen(display_panel_buf)) {
446 cmdline_len += strlen(display_panel_buf);
447 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700448 }
449
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800450 if (target_warm_boot()) {
451 warm_boot = true;
452 cmdline_len += strlen(warmboot_cmdline);
453 }
454
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800455#if TARGET_CMDLINE_SUPPORT
456 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
457 int target_cmd_line_len;
458 ASSERT(target_cmdline_buf);
459 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
460 cmdline_len += target_cmd_line_len;
461#endif
462
David Ng183a7422009-12-07 14:55:21 -0800463 if (cmdline_len > 0) {
464 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800465 unsigned char *dst;
466
467 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
468 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530469 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800470 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700471
Amol Jadi168b7712012-03-06 16:15:00 -0800472 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800473 if (have_cmdline) {
474 src = cmdline;
475 while ((*dst++ = *src++));
476 }
477 if (target_is_emmc_boot()) {
478 src = emmc_cmdline;
479 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700480 have_cmdline = 1;
481 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800482#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700483 src = boot_dev_buf;
484 if (have_cmdline) --dst;
485 while ((*dst++ = *src++));
486#endif
David Ngf773dde2010-07-26 19:55:08 -0700487 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800488
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700489#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530490#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700491 src = verified_state;
492 if(have_cmdline) --dst;
493 have_cmdline = 1;
494 while ((*dst++ = *src++));
495 src = vbsn[boot_state].name;
496 if(have_cmdline) --dst;
497 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700498
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700499 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
500 {
501 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
502 ASSERT(0);
503 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700504 src = verity_mode;
505 if(have_cmdline) --dst;
506 while ((*dst++ = *src++));
507 src = vbvm[device.verity_mode].name;
508 if(have_cmdline) -- dst;
509 while ((*dst++ = *src++));
Parth Dixita5715a02015-10-29 12:25:10 +0530510 src = keymaster_v1;
511 if(have_cmdline) --dst;
512 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700513#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530514#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800515 src = usb_sn_cmdline;
516 if (have_cmdline) --dst;
517 have_cmdline = 1;
518 while ((*dst++ = *src++));
519 src = sn_buf;
520 if (have_cmdline) --dst;
521 have_cmdline = 1;
522 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800523 if (warm_boot) {
524 if (have_cmdline) --dst;
525 src = warmboot_cmdline;
526 while ((*dst++ = *src++));
527 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800528
Pavel Nedev5614d222013-06-17 18:01:02 +0300529 if (boot_into_recovery && gpt_exists) {
530 src = secondary_gpt_enable;
531 if (have_cmdline) --dst;
532 while ((*dst++ = *src++));
533 }
534
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200535 if (is_mdtp_activated) {
536 src = mdtp_activated_flag;
537 if (have_cmdline) --dst;
538 while ((*dst++ = *src++));
539 }
540
Pavel Nedev328ac822013-04-05 15:25:11 +0300541 if (boot_into_ffbm) {
542 src = androidboot_mode;
543 if (have_cmdline) --dst;
544 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700545 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300546 if (have_cmdline) --dst;
547 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800548 src = loglevel;
549 if (have_cmdline) --dst;
550 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800551 } else if (boot_reason_alarm) {
552 src = alarmboot_cmdline;
553 if (have_cmdline) --dst;
554 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300555 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700556 src = battchg_pause;
557 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800558 while ((*dst++ = *src++));
559 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800560
Shashank Mittalcd98d472011-08-02 14:29:24 -0700561 if(target_use_signed_kernel() && auth_kernel_img) {
562 src = auth_kernel;
563 if (have_cmdline) --dst;
564 while ((*dst++ = *src++));
565 }
566
Ajay Dudanid04110c2011-01-17 23:55:07 -0800567 switch(target_baseband())
568 {
569 case BASEBAND_APQ:
570 src = baseband_apq;
571 if (have_cmdline) --dst;
572 while ((*dst++ = *src++));
573 break;
574
575 case BASEBAND_MSM:
576 src = baseband_msm;
577 if (have_cmdline) --dst;
578 while ((*dst++ = *src++));
579 break;
580
581 case BASEBAND_CSFB:
582 src = baseband_csfb;
583 if (have_cmdline) --dst;
584 while ((*dst++ = *src++));
585 break;
586
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800587 case BASEBAND_SVLTE2A:
588 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800589 if (have_cmdline) --dst;
590 while ((*dst++ = *src++));
591 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700592
593 case BASEBAND_MDM:
594 src = baseband_mdm;
595 if (have_cmdline) --dst;
596 while ((*dst++ = *src++));
597 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700598
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800599 case BASEBAND_MDM2:
600 src = baseband_mdm2;
601 if (have_cmdline) --dst;
602 while ((*dst++ = *src++));
603 break;
604
Amol Jadi5c61a952012-05-04 17:05:35 -0700605 case BASEBAND_SGLTE:
606 src = baseband_sglte;
607 if (have_cmdline) --dst;
608 while ((*dst++ = *src++));
609 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530610
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800611 case BASEBAND_SGLTE2:
612 src = baseband_sglte2;
613 if (have_cmdline) --dst;
614 while ((*dst++ = *src++));
615 break;
616
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530617 case BASEBAND_DSDA:
618 src = baseband_dsda;
619 if (have_cmdline) --dst;
620 while ((*dst++ = *src++));
621 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800622
623 case BASEBAND_DSDA2:
624 src = baseband_dsda2;
625 if (have_cmdline) --dst;
626 while ((*dst++ = *src++));
627 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530628 case BASEBAND_APQ_NOWGR:
629 src = baseband_apq_nowgr;
630 if (have_cmdline) --dst;
631 while ((*dst++ = *src++));
632 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800633 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700634
635 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700636 src = display_panel_buf;
637 if (have_cmdline) --dst;
638 while ((*dst++ = *src++));
639 }
Joonwoo Park61112782013-10-02 19:50:39 -0700640
641 if (have_target_boot_params) {
642 if (have_cmdline) --dst;
643 src = target_boot_params;
644 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530645 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700646 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800647
648#if TARGET_CMDLINE_SUPPORT
649 if (target_cmdline_buf && target_cmd_line_len)
650 {
651 if (have_cmdline) --dst;
652 src = target_cmdline_buf;
653 while((*dst++ = *src++));
654 free(target_cmdline_buf);
655 }
656#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700657 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700658
659
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700660 if (boot_dev_buf)
661 free(boot_dev_buf);
662
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800663 if (cmdline_final)
664 dprintf(INFO, "cmdline: %s\n", cmdline_final);
665 else
666 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700667 return cmdline_final;
668}
669
670unsigned *atag_core(unsigned *ptr)
671{
672 /* CORE */
673 *ptr++ = 2;
674 *ptr++ = 0x54410001;
675
676 return ptr;
677
678}
679
680unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
681 unsigned ramdisk_size)
682{
683 if (ramdisk_size) {
684 *ptr++ = 4;
685 *ptr++ = 0x54420005;
686 *ptr++ = (unsigned)ramdisk;
687 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800688 }
689
Neeti Desaie245d492012-06-01 12:52:13 -0700690 return ptr;
691}
692
693unsigned *atag_ptable(unsigned **ptr_addr)
694{
695 int i;
696 struct ptable *ptable;
697
698 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700699 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
700 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700701 *(*ptr_addr)++ = 0x4d534d70;
702 for (i = 0; i < ptable->count; ++i)
703 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
704 }
705
706 return (*ptr_addr);
707}
708
709unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
710{
711 int cmdline_length = 0;
712 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700713 char *dest;
714
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800715 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700716 n = (cmdline_length + 4) & (~3);
717
718 *ptr++ = (n / 4) + 2;
719 *ptr++ = 0x54410009;
720 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800721 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700722 ptr += (n / 4);
723
724 return ptr;
725}
726
727unsigned *atag_end(unsigned *ptr)
728{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800729 /* END */
730 *ptr++ = 0;
731 *ptr++ = 0;
732
Neeti Desaie245d492012-06-01 12:52:13 -0700733 return ptr;
734}
735
736void generate_atags(unsigned *ptr, const char *cmdline,
737 void *ramdisk, unsigned ramdisk_size)
738{
vijay kumar21a37452015-12-29 16:23:21 +0530739 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700740 ptr = atag_core(ptr);
741 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
742 ptr = target_atag_mem(ptr);
743
744 /* Skip NAND partition ATAGS for eMMC boot */
745 if (!target_is_emmc_boot()){
746 ptr = atag_ptable(&ptr);
747 }
748
vijay kumar21a37452015-12-29 16:23:21 +0530749 /*
750 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
751 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
752 */
753 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
754 ptr = atag_cmdline(ptr, cmdline);
755 ptr = atag_end(ptr);
756 }
757 else {
758 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
759 ASSERT(0);
760 }
Neeti Desaie245d492012-06-01 12:52:13 -0700761}
762
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700763typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700764void boot_linux(void *kernel, unsigned *tags,
765 const char *cmdline, unsigned machtype,
766 void *ramdisk, unsigned ramdisk_size)
767{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800768 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800769#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700770 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800771#endif
772
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700773 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800774 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530775 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800776
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530777 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700778
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800779 final_cmdline = update_cmdline((const char*)cmdline);
780
Neeti Desai17379b82012-06-04 18:42:53 -0700781#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800782 dprintf(INFO, "Updating device tree: start\n");
783
Neeti Desai17379b82012-06-04 18:42:53 -0700784 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530785 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700786 if(ret)
787 {
788 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
789 ASSERT(0);
790 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800791 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700792#else
Neeti Desaie245d492012-06-01 12:52:13 -0700793 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800794 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700795#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700796
Maria Yu52254c02014-07-04 16:14:54 +0800797 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700798
799#if VERIFIED_BOOT
lijuangbdd9bb42016-03-01 18:22:17 +0800800#if !VBOOT_MOTA
801 if (device.verity_mode == 0) {
802#if FBCON_DISPLAY_MSG
803 display_bootverify_menu(DISPLAY_MENU_LOGGING);
804 wait_for_users_action();
805#else
806 dprintf(CRITICAL,
807 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
808 mdelay(5000);
809#endif
810 }
811
812#endif
813#endif
814
815#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700816 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700817 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700818 {
819 dprintf(INFO, "Failed to write protect dev info\n");
820 ASSERT(0);
821 }
822#endif
823
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800824 /* Turn off splash screen if enabled */
825#if DISPLAY_SPLASH_SCREEN
826 target_display_shutdown();
827#endif
828
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700829 /* Perform target specific cleanup */
830 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800831
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800832 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530833 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800834
835 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700836
Amol Jadi4421e652011-06-16 15:00:48 -0700837 /* do any platform specific cleanup before kernel entry */
838 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700839
Brian Swetland9c4c0752009-01-25 16:23:50 -0800840 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700841
Amol Jadi504f9fe2012-08-16 13:56:48 -0700842#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800843 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700844#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700845 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800846
847 if (IS_ARM64(kptr))
848 /* Jump to a 64bit kernel */
849 scm_elexec_call((paddr_t)kernel, tags_phys);
850 else
851 /* Jump to a 32bit kernel */
852 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800853}
854
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700855/* Function to check if the memory address range falls within the aboot
856 * boundaries.
857 * start: Start of the memory region
858 * size: Size of the memory region
859 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +0530860int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700861{
862 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800863 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700864 return -1;
865
866 /* Check for memory overlap. */
867 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
868 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700869 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700870 return 0;
871 else
872 return -1;
873}
874
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800875#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800876
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800877BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800878#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800879BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800880#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800881
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700882static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
883{
884 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800885
886#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800887#if IMAGE_VERIF_ALGO_SHA1
888 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
889#else
890 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
891#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800892#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700893
894 /* Assume device is rooted at this time. */
895 device.is_tampered = 1;
896
897 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
898
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700899#if VERIFIED_BOOT
900 if(boot_into_recovery)
901 {
902 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530903 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700904 }
905 else
906 {
907 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530908 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700909 }
910 boot_verify_print_state();
911#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700912 ret = image_verify((unsigned char *)bootimg_addr,
913 (unsigned char *)(bootimg_addr + bootimg_size),
914 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800915 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700916#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700917 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
918
919 if (ret)
920 {
921 /* Authorized kernel */
922 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700923 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700924 }
925
Amit Blay4aa292f2015-04-28 21:55:59 +0300926#ifdef MDTP_SUPPORT
927 {
928 /* Verify MDTP lock.
929 * For boot & recovery partitions, use aboot's verification result.
930 */
931 mdtp_ext_partition_verification_t ext_partition;
932 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
933 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
934 ext_partition.page_size = 0; /* Not needed since already validated */
935 ext_partition.image_addr = 0; /* Not needed since already validated */
936 ext_partition.image_size = 0; /* Not needed since already validated */
937 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
938 mdtp_fwlock_verify_lock(&ext_partition);
939 }
940#endif /* MDTP_SUPPORT */
941
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700942#if USE_PCOM_SECBOOT
943 set_tamper_flag(device.is_tampered);
944#endif
945
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700946#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700947 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700948 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700949 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800950#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800951 display_bootverify_menu(DISPLAY_MENU_RED);
lijuanga40d6302015-07-20 20:10:13 +0800952 wait_for_users_action();
953#else
954 dprintf(CRITICAL,
955 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
956 mdelay(5000);
957#endif
958
959 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700960 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800961#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800962 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800963 wait_for_users_action();
964#else
965 dprintf(CRITICAL,
966 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
967 mdelay(5000);
968#endif
969 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700970 default:
lijuanga40d6302015-07-20 20:10:13 +0800971 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700972 }
973#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700974#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530975 if(device.is_tampered)
976 {
977 write_device_info_mmc(&device);
978 #ifdef TZ_TAMPER_FUSE
979 set_tamper_fuse_cmd();
980 #endif
981 #ifdef ASSERT_ON_TAMPER
982 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
983 ASSERT(0);
984 #endif
985 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700986#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700987}
988
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530989static bool check_format_bit()
990{
991 bool ret = false;
992 int index;
993 uint64_t offset;
994 struct boot_selection_info *in = NULL;
995 char *buf = NULL;
996
997 index = partition_get_index("bootselect");
998 if (index == INVALID_PTN)
999 {
1000 dprintf(INFO, "Unable to locate /bootselect partition\n");
1001 return ret;
1002 }
1003 offset = partition_get_offset(index);
1004 if(!offset)
1005 {
1006 dprintf(INFO, "partition /bootselect doesn't exist\n");
1007 return ret;
1008 }
1009 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
1010 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301011 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301012 {
1013 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1014 free(buf);
1015 return ret;
1016 }
1017 in = (struct boot_selection_info *) buf;
1018 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1019 (in->version == BOOTSELECT_VERSION)) {
1020 if ((in->state_info & BOOTSELECT_FORMAT) &&
1021 !(in->state_info & BOOTSELECT_FACTORY))
1022 ret = true;
1023 } else {
1024 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1025 in->signature, in->version);
1026 ASSERT(0);
1027 }
1028 free(buf);
1029 return ret;
1030}
1031
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001032void boot_verifier_init()
1033{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001034 uint32_t boot_state;
1035 /* Check if device unlock */
1036 if(device.is_unlocked)
1037 {
1038 boot_verify_send_event(DEV_UNLOCK);
1039 boot_verify_print_state();
1040 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1041 return;
1042 }
1043 else
1044 {
1045 boot_verify_send_event(BOOT_INIT);
1046 }
1047
1048 /* Initialize keystore */
1049 boot_state = boot_verify_keystore_init();
1050 if(boot_state == YELLOW)
1051 {
1052 boot_verify_print_state();
1053 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1054 }
1055}
1056
Shashank Mittal23b8f422010-04-16 19:27:21 -07001057int boot_linux_from_mmc(void)
1058{
1059 struct boot_img_hdr *hdr = (void*) buf;
1060 struct boot_img_hdr *uhdr;
1061 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001062 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001063 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001064 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001065
Shashank Mittalcd98d472011-08-02 14:29:24 -07001066 unsigned char *image_addr = 0;
1067 unsigned kernel_actual;
1068 unsigned ramdisk_actual;
1069 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001070 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001071
Matthew Qin271927e2015-03-31 22:07:07 -04001072 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001073 unsigned int out_len = 0;
1074 unsigned int out_avai_len = 0;
1075 unsigned char *out_addr = NULL;
1076 uint32_t dtb_offset = 0;
1077 unsigned char *kernel_start_addr = NULL;
1078 unsigned int kernel_size = 0;
1079 int rc;
1080
Neeti Desai465491e2012-07-31 12:53:35 -07001081#if DEVICE_TREE
1082 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001083 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001084 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001085 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001086 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001087 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001088#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001089 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001090
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301091 if (check_format_bit())
1092 boot_into_recovery = 1;
1093
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001094 if (!boot_into_recovery) {
1095 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1096 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1097 if (rcode <= 0) {
1098 boot_into_ffbm = false;
1099 if (rcode < 0)
1100 dprintf(CRITICAL,"failed to get ffbm cookie");
1101 } else
1102 boot_into_ffbm = true;
1103 } else
1104 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001105 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1106 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1107 dprintf(INFO, "Unified boot method!\n");
1108 hdr = uhdr;
1109 goto unified_boot;
1110 }
Greg Griscod6250552011-06-29 14:40:23 -07001111 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001112 index = partition_get_index("boot");
1113 ptn = partition_get_offset(index);
1114 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001115 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1116 return -1;
1117 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001118 }
1119 else {
1120 index = partition_get_index("recovery");
1121 ptn = partition_get_offset(index);
1122 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001123 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1124 return -1;
1125 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001126 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001127 /* Set Lun for boot & recovery partitions */
1128 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001129
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301130 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001131 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1132 return -1;
1133 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001134
1135 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001136 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001137 return -1;
1138 }
1139
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001140 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301141
1142 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1143 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1144 return -1;
1145 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001146 page_size = hdr->page_size;
1147 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001148 }
1149
vijay kumar287bb542015-09-29 13:01:52 +05301150 /* ensure commandline is terminated */
1151 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1152
Matthew Qin49e51fa2015-02-09 10:40:45 +08001153 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1154 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001155
Matthew Qin49e51fa2015-02-09 10:40:45 +08001156 image_addr = (unsigned char *)target_get_scratch_address();
1157
1158#if DEVICE_TREE
1159 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1160 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1161#else
1162 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1163#endif
1164
1165#if VERIFIED_BOOT
1166 boot_verifier_init();
1167#endif
1168
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301169 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001170 {
1171 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1172 return -1;
1173 }
1174
Matthew Qinbb7923d2015-02-09 10:56:09 +08001175 /*
1176 * Update loading flow of bootimage to support compressed/uncompressed
1177 * bootimage on both 64bit and 32bit platform.
1178 * 1. Load bootimage from emmc partition onto DDR.
1179 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1180 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1181 * platform accordingly.
1182 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1183 */
1184
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001185 dprintf(INFO, "Loading (%s) image (%d): start\n",
1186 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001187 bs_set_timestamp(BS_KERNEL_LOAD_START);
1188
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301189 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1190 {
1191 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1192 return -1;
1193 }
1194
Matthew Qinbb7923d2015-02-09 10:56:09 +08001195 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001196 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1197 {
1198 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1199 return -1;
1200 }
1201
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001202 dprintf(INFO, "Loading (%s) image (%d): done\n",
1203 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1204
Matthew Qin49e51fa2015-02-09 10:40:45 +08001205 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1206
1207 /* Authenticate Kernel */
1208 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1209 (int) target_use_signed_kernel(),
1210 device.is_unlocked,
1211 device.is_tampered);
1212
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001213 /* Change the condition a little bit to include the test framework support.
1214 * We would never reach this point if device is in fastboot mode, even if we did
1215 * that means we are in test mode, so execute kernel authentication part for the
1216 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301217 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001218 {
1219 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301220 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001221 {
1222 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1223 return -1;
1224 }
1225
1226 /* Read signature */
1227 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1228 {
1229 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1230 return -1;
1231 }
1232
1233 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001234 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301235 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001236 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001237 } else {
1238 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1239 #ifdef TZ_SAVE_KERNEL_HASH
1240 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1241 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001242
1243#ifdef MDTP_SUPPORT
1244 {
1245 /* Verify MDTP lock.
1246 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1247 * since verification was skipped in aboot. The signature is not part of the loaded image.
1248 */
1249 mdtp_ext_partition_verification_t ext_partition;
1250 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1251 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1252 ext_partition.page_size = page_size;
1253 ext_partition.image_addr = (uint32)image_addr;
1254 ext_partition.image_size = imagesize_actual;
1255 ext_partition.sig_avail = FALSE;
1256 mdtp_fwlock_verify_lock(&ext_partition);
1257 }
1258#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001259 }
1260
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001261#if VERIFIED_BOOT
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001262 if(boot_verify_get_state() == ORANGE)
1263 {
1264#if FBCON_DISPLAY_MSG
1265 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1266 wait_for_users_action();
1267#else
1268 dprintf(CRITICAL,
1269 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1270 mdelay(5000);
1271#endif
1272 }
1273#endif
1274
1275#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05301276#if !VBOOT_MOTA
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001277 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001278 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001279 ASSERT(0);
1280#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05301281#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001282 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001283 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1284 * If not, continue booting.
1285 */
1286 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1287 {
1288 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1289 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001290 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001291 rc = decompress((unsigned char *)(image_addr + page_size),
1292 hdr->kernel_size, out_addr, out_avai_len,
1293 &dtb_offset, &out_len);
1294 if (rc)
1295 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001296 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001297 ASSERT(0);
1298 }
1299
Matthew Qin0b15b322015-05-19 05:20:54 -04001300 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001301 kptr = (struct kernel64_hdr *)out_addr;
1302 kernel_start_addr = out_addr;
1303 kernel_size = out_len;
1304 } else {
1305 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1306 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1307 kernel_size = hdr->kernel_size;
1308 }
1309
1310 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001311 * Update the kernel/ramdisk/tags address if the boot image header
1312 * has default values, these default values come from mkbootimg when
1313 * the boot image is flashed using fastboot flash:raw
1314 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001315 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001316
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001317 /* Get virtual addresses since the hdr saves physical addresses. */
1318 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1319 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1320 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001321
Matthew Qinbb7923d2015-02-09 10:56:09 +08001322 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001323 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001324 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001325 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1326 {
1327 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1328 return -1;
1329 }
1330
1331#ifndef DEVICE_TREE
1332 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1333 {
1334 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1335 return -1;
1336 }
1337#endif
1338
Matthew Qin49e51fa2015-02-09 10:40:45 +08001339 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001340 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001341 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001342
Matthew Qin49e51fa2015-02-09 10:40:45 +08001343 #if DEVICE_TREE
1344 if(hdr->dt_size) {
1345 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1346 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001347
Matthew Qin49e51fa2015-02-09 10:40:45 +08001348 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1349 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1350 return -1;
1351 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001352
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301353 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1354 goes beyound hdr->dt_size*/
1355 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1356 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1357 return -1;
1358 }
1359
Matthew Qin49e51fa2015-02-09 10:40:45 +08001360 /* Find index of device tree within device tree table */
1361 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1362 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1363 return -1;
1364 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001365
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301366 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1367 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1368 return -1;
1369 }
1370
1371 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1372 if ((dt_entry.offset + dt_entry.size) > hdr->dt_size) {
1373 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1374 return -1;
1375 }
1376
Matthew Qin271927e2015-03-31 22:07:07 -04001377 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1378 {
1379 unsigned int compressed_size = 0;
1380 out_addr += out_len;
1381 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001382 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001383 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1384 dt_entry.size, out_addr, out_avai_len,
1385 &compressed_size, &dtb_size);
1386 if (rc)
1387 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001388 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001389 ASSERT(0);
1390 }
1391
Matthew Qin0b15b322015-05-19 05:20:54 -04001392 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001393 best_match_dt_addr = out_addr;
1394 } else {
1395 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1396 dtb_size = dt_entry.size;
1397 }
1398
Matthew Qin49e51fa2015-02-09 10:40:45 +08001399 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001400 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001401 {
1402 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1403 return -1;
1404 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001405
Matthew Qin271927e2015-03-31 22:07:07 -04001406 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001407 } else {
1408 /* Validate the tags_addr */
1409 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001410 {
1411 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1412 return -1;
1413 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001414 /*
1415 * If appended dev tree is found, update the atags with
1416 * memory address to the DTB appended location on RAM.
1417 * Else update with the atags address in the kernel header
1418 */
1419 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001420 dtb = dev_tree_appended((void*)(image_addr + page_size),
1421 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001422 (void *)hdr->tags_addr);
1423 if (!dtb) {
1424 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001425 return -1;
1426 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001427 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001428 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001429
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001430 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1431 target_load_ssd_keystore();
1432
Shashank Mittal23b8f422010-04-16 19:27:21 -07001433unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001434
Dima Zavin77e41f32013-03-06 16:10:43 -08001435 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001436 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001437 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1438
1439 return 0;
1440}
1441
Dima Zavin214cc642009-01-26 11:16:21 -08001442int boot_linux_from_flash(void)
1443{
1444 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001445 struct ptentry *ptn;
1446 struct ptable *ptable;
1447 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001448
Shashank Mittalcd98d472011-08-02 14:29:24 -07001449 unsigned char *image_addr = 0;
1450 unsigned kernel_actual;
1451 unsigned ramdisk_actual;
1452 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301453 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001454
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001455#if DEVICE_TREE
1456 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001457 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301458 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001459 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001460 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301461 unsigned int dtb_size = 0;
1462 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001463#endif
1464
David Ng183a7422009-12-07 14:55:21 -08001465 if (target_is_emmc_boot()) {
1466 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1467 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1468 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1469 return -1;
1470 }
1471 goto continue_boot;
1472 }
1473
Dima Zavin214cc642009-01-26 11:16:21 -08001474 ptable = flash_get_ptable();
1475 if (ptable == NULL) {
1476 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1477 return -1;
1478 }
1479
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001480 if(!boot_into_recovery)
1481 {
1482 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001483
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001484 if (ptn == NULL) {
1485 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1486 return -1;
1487 }
1488 }
1489 else
1490 {
1491 ptn = ptable_find(ptable, "recovery");
1492 if (ptn == NULL) {
1493 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1494 return -1;
1495 }
Dima Zavin214cc642009-01-26 11:16:21 -08001496 }
1497
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001498 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001499 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1500 return -1;
1501 }
Dima Zavin214cc642009-01-26 11:16:21 -08001502
1503 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001504 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001505 return -1;
1506 }
1507
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001508 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001509 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 -08001510 return -1;
1511 }
1512
vijay kumar287bb542015-09-29 13:01:52 +05301513 /* ensure commandline is terminated */
1514 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1515
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001516 /*
1517 * Update the kernel/ramdisk/tags address if the boot image header
1518 * has default values, these default values come from mkbootimg when
1519 * the boot image is flashed using fastboot flash:raw
1520 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001521 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001522
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001523 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001524 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1525 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1526 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1527
1528 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1529 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1530
1531 /* Check if the addresses in the header are valid. */
1532 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1533 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1534 {
1535 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1536 return -1;
1537 }
1538
1539#ifndef DEVICE_TREE
1540 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1541 {
1542 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1543 return -1;
1544 }
1545#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001546
Shashank Mittalcd98d472011-08-02 14:29:24 -07001547 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001548 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001549 {
1550 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001551 offset = 0;
1552
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001553#if DEVICE_TREE
1554 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
vijay kumar966a9822015-12-09 18:36:09 +05301555
1556 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1557 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1558 return -1;
1559 }
1560
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001561 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001562
1563 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1564 {
1565 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1566 return -1;
1567 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001568#else
vijay kumar966a9822015-12-09 18:36:09 +05301569 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) {
1570 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1571 return -1;
1572 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001573 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001574#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001575
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001576 dprintf(INFO, "Loading (%s) image (%d): start\n",
1577 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001578 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001579
Shashank Mittalcd98d472011-08-02 14:29:24 -07001580 /* Read image without signature */
1581 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1582 {
1583 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1584 return -1;
1585 }
Dima Zavin214cc642009-01-26 11:16:21 -08001586
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001587 dprintf(INFO, "Loading (%s) image (%d): done\n",
1588 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001589 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001590
Shashank Mittalcd98d472011-08-02 14:29:24 -07001591 offset = imagesize_actual;
1592 /* Read signature */
1593 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1594 {
1595 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001596 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001597 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001598
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301599 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001600
1601 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001602 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1603 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001604#if DEVICE_TREE
vijay kumar8f53e362015-11-24 13:38:11 +05301605 if(hdr->dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001606
vijay kumar8f53e362015-11-24 13:38:11 +05301607 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1608
1609 table = (struct dt_table*) dt_table_offset;
1610
1611 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1612 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1613 return -1;
1614 }
1615
1616 /* Find index of device tree within device tree table */
1617 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1618 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1619 return -1;
1620 }
1621
1622 /* Validate and Read device device tree in the "tags_add */
1623 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){
1624 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1625 return -1;
1626 }
1627
1628 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1629 dtb_size = dt_entry.size;
1630 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1631 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001632#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001633
1634 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001635 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001636 {
1637 write_device_info_flash(&device);
1638 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301639#if USE_PCOM_SECBOOT
1640 set_tamper_flag(device.is_tampered);
1641#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001642 }
1643 else
1644 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001645 offset = page_size;
1646
Amol Jadib6be5c12012-11-14 13:39:51 -08001647 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1648 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1649 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1650
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001651 dprintf(INFO, "Loading (%s) image (%d): start\n",
1652 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1653
Amol Jadi492d5a52013-03-15 16:12:34 -07001654 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001655
1656 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001657 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1658 return -1;
1659 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001660 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001661
Amol Jadib6be5c12012-11-14 13:39:51 -08001662 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001663 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1664 return -1;
1665 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001666 offset += ramdisk_actual;
1667
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001668 dprintf(INFO, "Loading (%s) image (%d): done\n",
1669 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1670
Amol Jadi492d5a52013-03-15 16:12:34 -07001671 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001672
1673 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001674 offset += second_actual;
1675 /* Second image loading not implemented. */
1676 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001677 }
1678
1679#if DEVICE_TREE
1680 if(hdr->dt_size != 0) {
1681
1682 /* Read the device tree table into buffer */
1683 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1684 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1685 return -1;
1686 }
1687
1688 table = (struct dt_table*) dt_buf;
1689
Deepa Dinamani19648b42013-09-05 17:05:55 -07001690 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001691 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1692 return -1;
1693 }
1694
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301695 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1696 goes beyound hdr->dt_size*/
1697 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1698 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1699 return -1;
1700 }
1701
Deepa Dinamani19648b42013-09-05 17:05:55 -07001702 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1703 if (!table)
1704 return -1;
1705
1706 /* Read the entire device tree table into buffer */
1707 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1708 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1709 return -1;
1710 }
1711
1712
Joel Kingaa335dc2013-06-03 16:11:08 -07001713 /* Find index of device tree within device tree table */
1714 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001715 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1716 return -1;
1717 }
1718
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001719 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001720 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001721 {
1722 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1723 return -1;
1724 }
1725
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001726 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001727 if(flash_read(ptn, offset + dt_entry.offset,
1728 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001729 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1730 return -1;
1731 }
1732 }
1733#endif
1734
Shashank Mittalcd98d472011-08-02 14:29:24 -07001735 }
David Ng183a7422009-12-07 14:55:21 -08001736continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001737
Dima Zavin214cc642009-01-26 11:16:21 -08001738 /* TODO: create/pass atags to kernel */
1739
Ajay Dudanie28a6072011-07-01 13:59:46 -07001740 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001741 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001742 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1743
1744 return 0;
1745}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001746
Shashank Mittal162244e2011-08-08 19:01:25 -07001747void write_device_info_mmc(device_info *dev)
1748{
Shashank Mittal162244e2011-08-08 19:01:25 -07001749 unsigned long long ptn = 0;
1750 unsigned long long size;
1751 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001752 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001753 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001754 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001755
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001756 if (devinfo_present)
1757 index = partition_get_index("devinfo");
1758 else
1759 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001760
Shashank Mittal162244e2011-08-08 19:01:25 -07001761 ptn = partition_get_offset(index);
1762 if(ptn == 0)
1763 {
1764 return;
1765 }
1766
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001767 lun = partition_get_lun(index);
1768 mmc_set_lun(lun);
1769
Shashank Mittal162244e2011-08-08 19:01:25 -07001770 size = partition_get_size(index);
1771
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001772 blocksize = mmc_get_device_blocksize();
1773
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001774 if (devinfo_present)
1775 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1776 else
1777 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1778 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001779 {
1780 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001781 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001782 }
1783}
1784
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001785void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001786{
Shashank Mittal162244e2011-08-08 19:01:25 -07001787 unsigned long long ptn = 0;
1788 unsigned long long size;
1789 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001790 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001791 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001792
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001793 if ((index = partition_get_index("devinfo")) < 0)
1794 {
1795 devinfo_present = false;
1796 index = partition_get_index("aboot");
1797 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001798
Shashank Mittal162244e2011-08-08 19:01:25 -07001799 ptn = partition_get_offset(index);
1800 if(ptn == 0)
1801 {
1802 return;
1803 }
1804
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001805 mmc_set_lun(partition_get_lun(index));
1806
Shashank Mittal162244e2011-08-08 19:01:25 -07001807 size = partition_get_size(index);
1808
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001809 blocksize = mmc_get_device_blocksize();
1810
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001811 if (devinfo_present)
1812 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1813 else
1814 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1815 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001816 {
1817 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001818 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001819 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001820}
1821
1822void write_device_info_flash(device_info *dev)
1823{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001824 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001825 struct ptentry *ptn;
1826 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001827 if(info == NULL)
1828 {
1829 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1830 ASSERT(0);
1831 }
1832 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001833 ptable = flash_get_ptable();
1834 if (ptable == NULL)
1835 {
1836 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1837 return;
1838 }
1839
1840 ptn = ptable_find(ptable, "devinfo");
1841 if (ptn == NULL)
1842 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001843 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001844 return;
1845 }
1846
1847 memcpy(info, dev, sizeof(device_info));
1848
1849 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1850 {
1851 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1852 return;
1853 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001854 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001855}
1856
vijay kumarc65876c2015-04-24 13:29:16 +05301857static int read_allow_oem_unlock(device_info *dev)
1858{
vijay kumarc65876c2015-04-24 13:29:16 +05301859 unsigned offset;
1860 int index;
1861 unsigned long long ptn;
1862 unsigned long long ptn_size;
1863 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001864 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301865
vijay kumarca2e6812015-07-08 20:28:25 +05301866 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301867 if (index == INVALID_PTN)
1868 {
vijay kumarca2e6812015-07-08 20:28:25 +05301869 index = partition_get_index(frp_ptns[1]);
1870 if (index == INVALID_PTN)
1871 {
1872 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1873 return -1;
1874 }
vijay kumarc65876c2015-04-24 13:29:16 +05301875 }
1876
1877 ptn = partition_get_offset(index);
1878 ptn_size = partition_get_size(index);
1879 offset = ptn_size - blocksize;
1880
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001881 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301882 {
1883 dprintf(CRITICAL, "Reading MMC failed\n");
1884 return -1;
1885 }
1886
1887 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1888 is_allow_unlock = buf[blocksize-1] & 0x01;
1889 return 0;
1890}
1891
1892static int write_allow_oem_unlock(bool allow_unlock)
1893{
vijay kumarc65876c2015-04-24 13:29:16 +05301894 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301895 int index;
1896 unsigned long long ptn;
1897 unsigned long long ptn_size;
1898 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001899 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301900
vijay kumarca2e6812015-07-08 20:28:25 +05301901 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301902 if (index == INVALID_PTN)
1903 {
vijay kumarca2e6812015-07-08 20:28:25 +05301904 index = partition_get_index(frp_ptns[1]);
1905 if (index == INVALID_PTN)
1906 {
1907 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1908 return -1;
1909 }
vijay kumarc65876c2015-04-24 13:29:16 +05301910 }
1911
1912 ptn = partition_get_offset(index);
1913 ptn_size = partition_get_size(index);
1914 offset = ptn_size - blocksize;
1915
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001916 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301917 {
1918 dprintf(CRITICAL, "Reading MMC failed\n");
1919 return -1;
1920 }
1921
1922 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1923 buf[blocksize-1] = allow_unlock;
1924 if (mmc_write(ptn + offset, blocksize, buf))
1925 {
1926 dprintf(CRITICAL, "Writing MMC failed\n");
1927 return -1;
1928 }
1929
1930 return 0;
1931}
1932
Shashank Mittal162244e2011-08-08 19:01:25 -07001933void read_device_info_flash(device_info *dev)
1934{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001935 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001936 struct ptentry *ptn;
1937 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001938 if(info == NULL)
1939 {
1940 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1941 ASSERT(0);
1942 }
1943 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001944 ptable = flash_get_ptable();
1945 if (ptable == NULL)
1946 {
1947 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1948 return;
1949 }
1950
1951 ptn = ptable_find(ptable, "devinfo");
1952 if (ptn == NULL)
1953 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001954 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001955 return;
1956 }
1957
1958 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1959 {
1960 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1961 return;
1962 }
1963
1964 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1965 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001966 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1967 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001968 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001969 write_device_info_flash(info);
1970 }
1971 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001972 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001973}
1974
1975void write_device_info(device_info *dev)
1976{
1977 if(target_is_emmc_boot())
1978 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001979 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1980 if(info == NULL)
1981 {
1982 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1983 ASSERT(0);
1984 }
1985 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001986 memcpy(info, dev, sizeof(struct device_info));
1987
1988#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001989 if (is_secure_boot_enable()) {
1990 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1991 ASSERT(0);
1992 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001993 else
1994 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001995#else
1996 write_device_info_mmc(info);
1997#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001998 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001999 }
2000 else
2001 {
2002 write_device_info_flash(dev);
2003 }
2004}
2005
2006void read_device_info(device_info *dev)
2007{
2008 if(target_is_emmc_boot())
2009 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002010 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2011 if(info == NULL)
2012 {
2013 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2014 ASSERT(0);
2015 }
2016 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002017
2018#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002019 if (is_secure_boot_enable()) {
2020 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2021 ASSERT(0);
2022 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002023 else
2024 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002025#else
2026 read_device_info_mmc(info);
2027#endif
2028
2029 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2030 {
2031 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002032 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002033 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302034#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002035 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302036#endif
lijuang511a2b52015-08-14 20:50:51 +08002037 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002038 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302039#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002040 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302041#endif
lijuang511a2b52015-08-14 20:50:51 +08002042 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002043 info->is_tampered = 0;
2044 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302045#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07002046 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302047#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002048 write_device_info(info);
2049 }
2050 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002051 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002052 }
2053 else
2054 {
2055 read_device_info_flash(dev);
2056 }
2057}
2058
2059void reset_device_info()
2060{
2061 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002062 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002063 write_device_info(&device);
2064}
2065
2066void set_device_root()
2067{
2068 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002069 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002070 write_device_info(&device);
2071}
2072
lijuang4ece1e72015-08-14 21:02:36 +08002073/* set device unlock value
2074 * Must check FRP before call this function
2075 * Need to wipe data when unlock status changed
2076 * type 0: oem unlock
2077 * type 1: unlock critical
2078 * status 0: unlock as false
2079 * status 1: lock as true
2080 */
2081void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002082{
lijuang4ece1e72015-08-14 21:02:36 +08002083 if (type == UNLOCK)
2084 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302085#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002086 else if (type == UNLOCK_CRITICAL)
2087 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302088#endif
lijuang4ece1e72015-08-14 21:02:36 +08002089 write_device_info(&device);
2090}
2091
2092static void set_device_unlock(int type, bool status)
2093{
2094 int is_unlocked = -1;
2095 char response[MAX_RSP_SIZE];
2096
2097 /* check device unlock status if it is as expected */
2098 if (type == UNLOCK)
2099 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05302100#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002101 else if (type == UNLOCK_CRITICAL)
2102 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05302103#endif
lijuang4ece1e72015-08-14 21:02:36 +08002104 if (is_unlocked == status) {
2105 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2106 fastboot_info(response);
2107 fastboot_okay("");
2108 return;
2109 }
2110
2111 /* status is true, it means to unlock device */
2112 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002113 if(!is_allow_unlock) {
2114 fastboot_fail("oem unlock is not allowed");
2115 return;
2116 }
2117
lijuang4ece1e72015-08-14 21:02:36 +08002118#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002119 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002120 fastboot_okay("");
2121 return;
2122#else
2123 if (type == UNLOCK) {
2124 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2125 return;
2126 }
2127#endif
lijuang21f12f52015-08-22 16:22:19 +08002128 }
lijuang4ece1e72015-08-14 21:02:36 +08002129
2130 set_device_unlock_value(type, status);
2131
2132 /* wipe data */
2133 struct recovery_message msg;
2134
2135 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2136 write_misc(0, &msg, sizeof(msg));
2137
2138 fastboot_okay("");
2139 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002140}
2141
lijuang511a2b52015-08-14 20:50:51 +08002142static bool critical_flash_allowed(const char * entry)
2143{
2144 uint32_t i = 0;
2145 if (entry == NULL)
2146 return false;
2147
2148 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2149 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2150 return true;
2151 }
2152 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002153}
2154
2155#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002156int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2157{
2158 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002159 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002160 struct dt_table *table;
2161 struct dt_entry dt_entry;
2162 uint32_t dt_hdr_size;
2163 unsigned int compressed_size = 0;
2164 unsigned int dtb_size = 0;
2165 unsigned int out_avai_len = 0;
2166 unsigned char *out_addr = NULL;
2167 unsigned char *best_match_dt_addr = NULL;
2168 int rc;
2169
2170 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2171
2172 if(hdr->dt_size != 0) {
2173 /* add kernel offset */
2174 dt_image_offset += page_size;
2175 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002176 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002177
Deepa Dinamani19648b42013-09-05 17:05:55 -07002178 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002179 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2180 dt_image_offset += n;
2181
Joel Kingaa335dc2013-06-03 16:11:08 -07002182 /* add second offset */
2183 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002184 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2185 dt_image_offset += n;
2186 }
2187
Matthew Qin271927e2015-03-31 22:07:07 -04002188 /* offset now point to start of dt.img */
2189 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2190
2191 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2192 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002193 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002194 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302195
2196 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2197 goes beyound hdr->dt_size*/
2198 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
2199 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2200 return -1;
2201 }
2202
Amol Jadicb524072012-08-09 16:40:18 -07002203 /* Find index of device tree within device tree table */
2204 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2205 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2206 return -1;
2207 }
2208
Matthew Qin271927e2015-03-31 22:07:07 -04002209 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2210 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2211 {
2212 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2213 out_avai_len = target_get_max_flash_size() - scratch_offset;
2214 dprintf(INFO, "decompressing dtb: start\n");
2215 rc = decompress(best_match_dt_addr,
2216 dt_entry.size, out_addr, out_avai_len,
2217 &compressed_size, &dtb_size);
2218 if (rc)
2219 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002220 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002221 ASSERT(0);
2222 }
2223
Matthew Qin0b15b322015-05-19 05:20:54 -04002224 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002225 best_match_dt_addr = out_addr;
2226 } else {
2227 dtb_size = dt_entry.size;
2228 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002229 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002230 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002231 {
2232 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
2233 return -1;
2234 }
2235
Amol Jadicb524072012-08-09 16:40:18 -07002236 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002237 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002238 } else
2239 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002240
2241 /* Everything looks fine. Return success. */
2242 return 0;
2243}
2244#endif
2245
Brian Swetland9c4c0752009-01-25 16:23:50 -08002246void cmd_boot(const char *arg, void *data, unsigned sz)
2247{
Amit Blay8a510302015-08-17 09:20:01 +03002248#ifdef MDTP_SUPPORT
2249 static bool is_mdtp_activated = 0;
2250#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002251 unsigned kernel_actual;
2252 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002253 uint32_t image_actual;
2254 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302255 uint32_t sig_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002256 struct boot_img_hdr *hdr = NULL;
2257 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002258 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002259 int ret = 0;
2260 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002261 unsigned int out_len = 0;
2262 unsigned int out_avai_len = 0;
2263 unsigned char *out_addr = NULL;
2264 uint32_t dtb_offset = 0;
2265 unsigned char *kernel_start_addr = NULL;
2266 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002267 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002268
lijuang2008ff22016-03-07 17:56:27 +08002269#if FBCON_DISPLAY_MSG
2270 /* Exit keys' detection thread firstly */
2271 exit_menu_keys_detection();
2272#endif
2273
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002274#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002275 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002276 {
2277 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002278 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002279 }
2280#endif
2281
Brian Swetland9c4c0752009-01-25 16:23:50 -08002282 if (sz < sizeof(hdr)) {
2283 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002284 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002285 }
2286
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002287 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002288
2289 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002290 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002291
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002292 if(target_is_emmc_boot() && hdr->page_size) {
2293 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002294 page_mask = page_size - 1;
2295 }
2296
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002297 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2298 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002299#if DEVICE_TREE
2300 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2301#endif
2302
2303 image_actual = ADD_OF(page_size, kernel_actual);
2304 image_actual = ADD_OF(image_actual, ramdisk_actual);
2305 image_actual = ADD_OF(image_actual, dt_actual);
2306
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302307 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2308 /* Calculate the signature length from boot image */
2309 sig_actual = read_der_message_length(
2310 (unsigned char*)(data + image_actual),sz);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002311 image_actual = ADD_OF(image_actual, sig_actual);
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302312 }
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002313
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002314 /* sz should have atleast raw boot image */
2315 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002316 fastboot_fail("bootimage: incomplete or not signed");
lijuang2008ff22016-03-07 17:56:27 +08002317 goto boot_failed;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002318 }
2319
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002320 // Initialize boot state before trying to verify boot.img
2321#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002322 boot_verifier_init();
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002323 /* Handle overflow if the input image size is greater than
2324 * boot image buffer can hold
2325 */
2326 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2327 {
2328 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002329 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002330 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002331#endif
2332
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002333 /* Verify the boot image
2334 * device & page_size are initialized in aboot_init
2335 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002336 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002337 /* Pass size excluding signature size, otherwise we would try to
2338 * access signature beyond its length
2339 */
2340 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002341 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002342#ifdef MDTP_SUPPORT
2343 else
2344 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002345 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002346 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002347
2348 if (!is_mdtp_activated) {
2349 ext_partition.partition = MDTP_PARTITION_NONE;
2350 mdtp_fwlock_verify_lock(&ext_partition);
2351 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002352 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002353
Amir Kotzer7c768c02016-04-13 09:08:05 +03002354 /* If mdtp state cannot be validate, block fastboot boot*/
2355 if(mdtp_activated(&is_mdtp_activated)){
2356 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2357 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2358 goto boot_failed;
2359 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002360 if(is_mdtp_activated){
2361 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002362 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002363 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002364#endif /* MDTP_SUPPORT */
2365
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002366#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302367#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002368 // send root of trust
2369 if(!send_rot_command((uint32_t)device.is_unlocked))
2370 ASSERT(0);
2371#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302372#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002373 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002374 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2375 * If not, continue booting.
2376 */
2377 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2378 {
2379 out_addr = (unsigned char *)target_get_scratch_address();
2380 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2381 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002382 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002383 ret = decompress((unsigned char *)(ptr + page_size),
2384 hdr->kernel_size, out_addr, out_avai_len,
2385 &dtb_offset, &out_len);
2386 if (ret)
2387 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002388 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002389 ASSERT(0);
2390 }
2391
Matthew Qin0b15b322015-05-19 05:20:54 -04002392 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002393 kptr = (struct kernel64_hdr *)out_addr;
2394 kernel_start_addr = out_addr;
2395 kernel_size = out_len;
2396 } else {
2397 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2398 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2399 kernel_size = hdr->kernel_size;
2400 }
2401
2402 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002403 * Update the kernel/ramdisk/tags address if the boot image header
2404 * has default values, these default values come from mkbootimg when
2405 * the boot image is flashed using fastboot flash:raw
2406 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002407 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002408
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002409 /* Get virtual addresses since the hdr saves physical addresses. */
2410 hdr->kernel_addr = VA(hdr->kernel_addr);
2411 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2412 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002413
Matthew Qinbb7923d2015-02-09 10:56:09 +08002414 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002415 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002416 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002417 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2418 {
2419 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002420 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002421 }
2422
Amol Jadicb524072012-08-09 16:40:18 -07002423#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002424 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002425 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002426 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002427
2428 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002429#else
2430 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2431 {
2432 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002433 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002434 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002435#endif
2436
2437 /* Load ramdisk & kernel */
2438 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002439 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002440
2441#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002442 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2443 {
2444 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002445 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002446 }
2447
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002448 /*
2449 * If dtb is not found look for appended DTB in the kernel.
2450 * If appended dev tree is found, update the atags with
2451 * memory address to the DTB appended location on RAM.
2452 * Else update with the atags address in the kernel header
2453 */
2454 if (!dtb_copied) {
2455 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002456 dtb = dev_tree_appended((void*)(ptr + page_size),
2457 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002458 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002459 if (!dtb) {
2460 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002461 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002462 }
Amol Jadicb524072012-08-09 16:40:18 -07002463 }
2464#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002465
2466 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002467 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002468
Dima Zavin77e41f32013-03-06 16:10:43 -08002469 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002470 (const char*) hdr->cmdline, board_machtype(),
2471 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002472
2473 /* fastboot already stop, it's no need to show fastboot menu */
2474 return;
2475boot_failed:
2476#if FBCON_DISPLAY_MSG
2477 /* revert to fastboot menu if boot failed */
2478 display_fastboot_menu();
2479#endif
2480 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002481}
2482
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002483void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002484{
2485 struct ptentry *ptn;
2486 struct ptable *ptable;
2487
2488 ptable = flash_get_ptable();
2489 if (ptable == NULL) {
2490 fastboot_fail("partition table doesn't exist");
2491 return;
2492 }
2493
2494 ptn = ptable_find(ptable, arg);
2495 if (ptn == NULL) {
2496 fastboot_fail("unknown partition name");
2497 return;
2498 }
2499
2500 if (flash_erase(ptn)) {
2501 fastboot_fail("failed to erase partition");
2502 return;
2503 }
2504 fastboot_okay("");
2505}
2506
Bikas Gurungd48bd242010-09-04 19:54:32 -07002507
2508void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2509{
2510 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002511 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002512 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002513 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302514 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002515
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002516#if VERIFIED_BOOT
2517 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2518 {
2519 if(!device.is_unlocked)
2520 {
2521 fastboot_fail("unlock device to erase keystore");
2522 return;
2523 }
2524 }
2525#endif
2526
Kinson Chikf1a43512011-07-14 11:28:39 -07002527 index = partition_get_index(arg);
2528 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002529 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002530
Kinson Chikf1a43512011-07-14 11:28:39 -07002531 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002532 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002533 return;
2534 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002535
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002536 lun = partition_get_lun(index);
2537 mmc_set_lun(lun);
2538
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002539 if (platform_boot_dev_isemmc())
2540 {
2541 if (mmc_erase_card(ptn, size)) {
2542 fastboot_fail("failed to erase partition\n");
2543 return;
2544 }
2545 } else {
2546 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2547 size = partition_get_size(index);
2548 if (size > DEFAULT_ERASE_SIZE)
2549 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002550
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002551 /* Simple inefficient version of erase. Just writing
2552 0 in first several blocks */
2553 if (mmc_write(ptn , size, (unsigned int *)out)) {
2554 fastboot_fail("failed to erase partition");
2555 return;
2556 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302557 /*Erase FDE metadata at the userdata footer*/
2558 if(!(strncmp(arg, "userdata", 8)))
2559 {
2560 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2561 memset((void *)footer, 0, FOOTER_SIZE);
2562
2563 size = partition_get_size(index);
2564
2565 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2566 fastboot_fail("failed to erase userdata footer");
2567 free(footer);
2568 return;
2569 }
2570 free(footer);
2571 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002572 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002573#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302574#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002575 if(!(strncmp(arg, "userdata", 8)))
2576 if(send_delete_keys_to_tz())
2577 ASSERT(0);
2578#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302579#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002580 fastboot_okay("");
2581}
2582
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002583void cmd_erase(const char *arg, void *data, unsigned sz)
2584{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002585#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002586 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002587 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002588 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002589 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002590 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002591 return;
2592 }
2593 }
2594#endif
2595
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002596 if(target_is_emmc_boot())
2597 cmd_erase_mmc(arg, data, sz);
2598 else
2599 cmd_erase_nand(arg, data, sz);
2600}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002601
Channagoud Kadabiad259832015-05-29 11:14:17 -07002602static uint32_t aboot_get_secret_key()
2603{
2604 /* 0 is invalid secret key, update this implementation to return
2605 * device specific unique secret key
2606 */
2607 return 0;
2608}
2609
Ajay Dudani5c761132011-04-07 20:19:04 -07002610void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002611{
2612 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002613 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002614 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002615 char *token = NULL;
2616 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002617 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002618 uint8_t lun = 0;
2619 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002620
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002621 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002622 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002623 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002624 if(token)
2625 {
2626 lun = atoi(token);
2627 mmc_set_lun(lun);
2628 lun_set = true;
2629 }
2630
Mao Jinlong226f33a2014-07-04 17:24:10 +08002631 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002632 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002633 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2634 {
2635 if (!aboot_frp_unlock(pname, data, sz))
2636 {
2637 fastboot_info("FRP unlock successful");
2638 fastboot_okay("");
2639 }
2640 else
2641 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2642
2643 return;
2644 }
2645
Mao Jinlong226f33a2014-07-04 17:24:10 +08002646 if (!strcmp(pname, "partition"))
2647 {
2648 dprintf(INFO, "Attempt to write partition image.\n");
2649 if (write_partition(sz, (unsigned char *) data)) {
2650 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002651 return;
2652 }
2653 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002654 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002655 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002656#if VERIFIED_BOOT
2657 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2658 {
2659 if(!device.is_unlocked)
2660 {
2661 fastboot_fail("unlock device to flash keystore");
2662 return;
2663 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302664 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002665 {
2666 fastboot_fail("image is not a keystore file");
2667 return;
2668 }
2669 }
2670#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002671 index = partition_get_index(pname);
2672 ptn = partition_get_offset(index);
2673 if(ptn == 0) {
2674 fastboot_fail("partition table doesn't exist");
2675 return;
2676 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002677
Mao Jinlong226f33a2014-07-04 17:24:10 +08002678 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2679 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2680 fastboot_fail("image is not a boot image");
2681 return;
2682 }
2683 }
2684
2685 if(!lun_set)
2686 {
2687 lun = partition_get_lun(index);
2688 mmc_set_lun(lun);
2689 }
2690
2691 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05302692 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002693 fastboot_fail("size too large");
2694 return;
2695 }
2696 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2697 fastboot_fail("flash write failure");
2698 return;
2699 }
Greg Grisco6e754772011-06-23 12:19:39 -07002700 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002701 }
2702 fastboot_okay("");
2703 return;
2704}
2705
Ajay Dudanide984792015-03-02 09:57:41 -08002706void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2707{
2708 int i, images;
2709 meta_header_t *meta_header;
2710 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302711 /*End of the image address*/
2712 uintptr_t data_end;
2713
2714 if( (UINT_MAX - sz) > (uintptr_t)data )
2715 data_end = (uintptr_t)data + sz;
2716 else
2717 {
2718 fastboot_fail("Cannot flash: image header corrupt");
2719 return;
2720 }
2721
2722 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
2723 {
2724 fastboot_fail("Cannot flash: image header corrupt");
2725 return;
2726 }
Ajay Dudanide984792015-03-02 09:57:41 -08002727
lijuang8ee21882016-04-19 16:57:11 +08002728 /* If device is locked:
2729 * Forbid to flash image to avoid the device to bypass the image
2730 * which with "any" name other than bootloader. Because it maybe
2731 * a meta package of all partitions.
2732 */
2733#if VERIFIED_BOOT
2734 if (target_build_variant_user()) {
2735 if (!device.is_unlocked) {
2736 fastboot_fail("Device is locked, meta image flashing is not allowed");
2737 return;
2738 }
2739#if !VBOOT_MOTA
2740 if(!device.is_unlock_critical) {
2741 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
2742 return;
2743 }
2744#endif
2745 }
2746#endif
2747
Ajay Dudanide984792015-03-02 09:57:41 -08002748 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302749 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
2750 {
2751 fastboot_fail("Cannot flash: image header corrupt");
2752 return;
2753 }
Ajay Dudanide984792015-03-02 09:57:41 -08002754 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2755
2756 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2757
2758 for (i=0; i<images; i++) {
2759
2760 if((img_header_entry[i].ptn_name == NULL) ||
2761 (img_header_entry[i].start_offset == 0) ||
2762 (img_header_entry[i].size == 0))
2763 break;
2764
Parth Dixit3e2d3032016-03-04 17:11:52 +05302765 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
2766 + img_header_entry[i].size) )
2767 {
2768 fastboot_fail("Cannot flash: image size mismatch");
2769 break;
2770 }
2771
Ajay Dudanide984792015-03-02 09:57:41 -08002772 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2773 (void *) data + img_header_entry[i].start_offset,
2774 img_header_entry[i].size);
2775 }
2776
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002777 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2778 {
2779 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2780 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2781 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2782 }
2783 else
2784 {
2785 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2786 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2787 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2788 }
2789
2790 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002791 fastboot_okay("");
2792 return;
2793}
2794
Ajay Dudani5c761132011-04-07 20:19:04 -07002795void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2796{
2797 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002798 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002799 uint32_t *fill_buf = NULL;
2800 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002801 sparse_header_t *sparse_header;
2802 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002803 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002804 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302805 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002806 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302807 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002808 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302809 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05302810 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002811
Kinson Chikf1a43512011-07-14 11:28:39 -07002812 index = partition_get_index(arg);
2813 ptn = partition_get_offset(index);
2814 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002815 fastboot_fail("partition table doesn't exist");
2816 return;
2817 }
2818
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302819 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302820
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002821 lun = partition_get_lun(index);
2822 mmc_set_lun(lun);
2823
vijay kumar800255e2015-04-24 20:26:19 +05302824 if (sz < sizeof(sparse_header_t)) {
2825 fastboot_fail("size too low");
2826 return;
2827 }
2828
Ajay Dudani5c761132011-04-07 20:19:04 -07002829 /* Read and skip over sparse image header */
2830 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302831
vijay kumar1321f342015-03-27 12:13:42 +05302832 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002833 fastboot_fail("size too large");
2834 return;
2835 }
2836
vijay kumarde4fcf62015-04-23 13:05:49 +05302837 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302838
Parth Dixit64b1a482016-03-07 16:31:26 +05302839 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302840 fastboot_fail("buffer overreads occured due to invalid sparse header");
2841 return;
2842 }
2843
vijay kumarde4fcf62015-04-23 13:05:49 +05302844 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002845 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302846 fastboot_fail("sparse header size mismatch");
2847 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002848 }
2849
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002850 dprintf (SPEW, "=== Sparse Image Header ===\n");
2851 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2852 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2853 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2854 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2855 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2856 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2857 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2858 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002859
2860 /* Start processing chunks */
2861 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2862 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302863 /* Make sure the total image size does not exceed the partition size */
2864 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2865 fastboot_fail("size too large");
2866 return;
2867 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002868 /* Read and skip over chunk header */
2869 chunk_header = (chunk_header_t *) data;
2870 data += sizeof(chunk_header_t);
2871
Parth Dixit64b1a482016-03-07 16:31:26 +05302872 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302873 fastboot_fail("buffer overreads occured due to invalid sparse header");
2874 return;
2875 }
2876
Ajay Dudani5c761132011-04-07 20:19:04 -07002877 dprintf (SPEW, "=== Chunk Header ===\n");
2878 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2879 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2880 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2881
vijay kumar800255e2015-04-24 20:26:19 +05302882 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002883 {
vijay kumar800255e2015-04-24 20:26:19 +05302884 fastboot_fail("chunk header size mismatch");
2885 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002886 }
2887
wufeng.jiang813dc352015-06-02 23:02:46 -04002888 if (!sparse_header->blk_sz ){
2889 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302890 return;
2891 }
2892
wufeng.jiang813dc352015-06-02 23:02:46 -04002893 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2894
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302895 /* Make sure that the chunk size calculated from sparse image does not
2896 * exceed partition size
2897 */
2898 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2899 {
2900 fastboot_fail("Chunk data size exceeds partition size");
2901 return;
2902 }
2903
Ajay Dudani5c761132011-04-07 20:19:04 -07002904 switch (chunk_header->chunk_type)
2905 {
2906 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002907 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002908 chunk_data_sz))
2909 {
2910 fastboot_fail("Bogus chunk size for chunk type Raw");
2911 return;
2912 }
2913
Parth Dixit64b1a482016-03-07 16:31:26 +05302914 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05302915 fastboot_fail("buffer overreads occured due to invalid sparse header");
2916 return;
2917 }
2918
wufeng.jiang813dc352015-06-02 23:02:46 -04002919 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2920 otherwise it will return in the line above
2921 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002922 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002923 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002924 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002925 {
2926 fastboot_fail("flash write failure");
2927 return;
2928 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302929 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2930 fastboot_fail("Bogus size for RAW chunk type");
2931 return;
2932 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002933 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002934 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002935 break;
2936
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002937 case CHUNK_TYPE_FILL:
2938 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2939 sizeof(uint32_t)))
2940 {
2941 fastboot_fail("Bogus chunk size for chunk type FILL");
2942 return;
2943 }
2944
2945 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2946 if (!fill_buf)
2947 {
2948 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2949 return;
2950 }
2951
Parth Dixit64b1a482016-03-07 16:31:26 +05302952 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05302953 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05302954 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05302955 return;
2956 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002957 fill_val = *(uint32_t *)data;
2958 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002959
2960 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2961 {
2962 fill_buf[i] = fill_val;
2963 }
2964
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05302965 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
2966 {
2967 fastboot_fail("bogus size for chunk FILL type");
2968 free(fill_buf);
2969 return;
2970 }
2971
wufeng.jiang813dc352015-06-02 23:02:46 -04002972 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002973 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302974 /* Make sure that the data written to partition does not exceed partition size */
2975 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2976 {
2977 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05302978 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302979 return;
2980 }
2981
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002982 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2983 sparse_header->blk_sz,
2984 fill_buf))
2985 {
2986 fastboot_fail("flash write failure");
2987 free(fill_buf);
2988 return;
2989 }
2990
2991 total_blocks++;
2992 }
2993
2994 free(fill_buf);
2995 break;
2996
Ajay Dudani5c761132011-04-07 20:19:04 -07002997 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302998 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2999 fastboot_fail("bogus size for chunk DONT CARE type");
3000 return;
3001 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003002 total_blocks += chunk_header->chunk_sz;
3003 break;
3004
Ajay Dudani5c761132011-04-07 20:19:04 -07003005 case CHUNK_TYPE_CRC:
3006 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3007 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003008 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003009 return;
3010 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303011 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3012 fastboot_fail("bogus size for chunk CRC type");
3013 return;
3014 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003015 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303016 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303017 fastboot_fail("integer overflow occured");
3018 return;
3019 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003020 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303021 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303022 fastboot_fail("buffer overreads occured due to invalid sparse header");
3023 return;
3024 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003025 break;
3026
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003027 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003028 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003029 fastboot_fail("Unknown chunk type");
3030 return;
3031 }
3032 }
3033
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003034 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3035 total_blocks, sparse_header->total_blks);
3036
3037 if(total_blocks != sparse_header->total_blks)
3038 {
3039 fastboot_fail("sparse image write failure");
3040 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003041
3042 fastboot_okay("");
3043 return;
3044}
3045
3046void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3047{
3048 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003049 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003050
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003051#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003052 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3053 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003054 int ret=0;
3055 uint32 major_version=0;
3056 uint32 minor_version=0;
3057
3058 ret = scm_svc_version(&major_version,&minor_version);
3059 if(!ret)
3060 {
3061 if(major_version >= 2)
3062 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003063 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003064 {
3065 ret = encrypt_scm((uint32 **) &data, &sz);
3066 if (ret != 0) {
3067 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3068 return;
3069 }
3070
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003071 /* Protect only for SSD */
3072 if (!strcmp(arg, "ssd")) {
3073 ret = scm_protect_keystore((uint32 *) data, sz);
3074 if (ret != 0) {
3075 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3076 return;
3077 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003078 }
3079 }
3080 else
3081 {
3082 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3083 if(ret != 0)
3084 {
3085 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3086 return;
3087 }
3088 }
3089 }
3090 else
3091 {
3092 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3093 magic_number[1] == DECRYPT_MAGIC_1)
3094 {
3095 ret = decrypt_scm((uint32 **) &data, &sz);
3096 if (ret != 0) {
3097 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3098 return;
3099 }
3100 }
3101 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3102 magic_number[1] == ENCRYPT_MAGIC_1)
3103 {
3104 ret = encrypt_scm((uint32 **) &data, &sz);
3105 if (ret != 0) {
3106 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3107 return;
3108 }
3109 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003110 }
3111 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003112 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003113 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003114 dprintf(CRITICAL,"INVALID SVC Version\n");
3115 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003116 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003117#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003118
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003119#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003120 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003121 {
lijuang511a2b52015-08-14 20:50:51 +08003122 /* if device is locked:
3123 * common partition will not allow to be flashed
3124 * critical partition will allow to flash image.
3125 */
3126 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3127 fastboot_fail("Partition flashing is not allowed");
3128 return;
3129 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303130#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003131 /* if device critical is locked:
3132 * common partition will allow to be flashed
3133 * critical partition will not allow to flash image.
3134 */
3135 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
3136 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003137 return;
3138 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303139#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003140 }
3141#endif
3142
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003143 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003144 meta_header = (meta_header_t *) data;
3145 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003146 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003147 else if (meta_header->magic == META_HEADER_MAGIC)
3148 cmd_flash_meta_img(arg, data, sz);
3149 else
3150 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003151
3152#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303153#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003154 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
3155 {
3156 // reset dm_verity mode to enforcing
3157 device.verity_mode = 1;
3158 write_device_info(&device);
3159 }
3160#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303161#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003162
Ajay Dudani5c761132011-04-07 20:19:04 -07003163 return;
3164}
3165
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003166void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3167{
3168 struct ptentry *sys_ptn;
3169 struct ptable *ptable;
3170
3171 ptable = flash_get_ptable();
3172 if (ptable == NULL) {
3173 fastboot_fail("partition table doesn't exist");
3174 return;
3175 }
3176
3177 sys_ptn = ptable_find(ptable, "system");
3178 if (sys_ptn == NULL) {
3179 fastboot_fail("system partition not found");
3180 return;
3181 }
3182
3183 sz = ROUND_TO_PAGE(sz, page_mask);
3184 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3185 fastboot_fail("update_ubi_vol failed");
3186 else
3187 fastboot_okay("");
3188}
3189
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003190void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003191{
3192 struct ptentry *ptn;
3193 struct ptable *ptable;
3194 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303195 uint64_t partition_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003196
3197 ptable = flash_get_ptable();
3198 if (ptable == NULL) {
3199 fastboot_fail("partition table doesn't exist");
3200 return;
3201 }
3202
3203 ptn = ptable_find(ptable, arg);
3204 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003205 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3206 arg);
3207 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003208 return;
3209 }
3210
3211 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
3212 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3213 fastboot_fail("image is not a boot image");
3214 return;
3215 }
3216 }
3217
Amol Jadi5c61a952012-05-04 17:05:35 -07003218 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003219 || !strcmp(ptn->name, "userdata")
3220 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003221 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003222 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003223 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003224 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003225 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003226
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303227 partition_size = (uint64_t)ptn->length * (uint64_t)flash_num_pages_per_blk() * (uint64_t)flash_page_size();
3228 if (partition_size > UINT_MAX) {
3229 fastboot_fail("Invalid partition size");
3230 return;
3231 }
3232
3233 if (sz > partition_size) {
3234 fastboot_fail("Image size too large");
3235 return;
3236 }
3237
Dima Zavin214cc642009-01-26 11:16:21 -08003238 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003239 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3240 if (flash_ubi_img(ptn, data, sz)) {
3241 fastboot_fail("flash write failure");
3242 return;
3243 }
3244 } else {
3245 if (flash_write(ptn, extra, data, sz)) {
3246 fastboot_fail("flash write failure");
3247 return;
3248 }
Dima Zavin214cc642009-01-26 11:16:21 -08003249 }
3250 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3251 fastboot_okay("");
3252}
3253
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003254void cmd_flash(const char *arg, void *data, unsigned sz)
3255{
3256 if(target_is_emmc_boot())
3257 cmd_flash_mmc(arg, data, sz);
3258 else
3259 cmd_flash_nand(arg, data, sz);
3260}
3261
Dima Zavin214cc642009-01-26 11:16:21 -08003262void cmd_continue(const char *arg, void *data, unsigned sz)
3263{
3264 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003265 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003266
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003267 if (target_is_emmc_boot())
3268 {
lijuanga40d6302015-07-20 20:10:13 +08003269#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003270 /* Exit keys' detection thread firstly */
3271 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003272#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003273 boot_linux_from_mmc();
3274 }
3275 else
3276 {
3277 boot_linux_from_flash();
3278 }
Dima Zavin214cc642009-01-26 11:16:21 -08003279}
3280
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003281void cmd_reboot(const char *arg, void *data, unsigned sz)
3282{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003283 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003284 fastboot_okay("");
3285 reboot_device(0);
3286}
3287
3288void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3289{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003290 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003291 fastboot_okay("");
3292 reboot_device(FASTBOOT_MODE);
3293}
3294
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003295void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3296{
3297 dprintf(INFO, "Enabling charger screen check\n");
3298 device.charger_screen_enabled = 1;
3299 write_device_info(&device);
3300 fastboot_okay("");
3301}
3302
3303void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3304{
3305 dprintf(INFO, "Disabling charger screen check\n");
3306 device.charger_screen_enabled = 0;
3307 write_device_info(&device);
3308 fastboot_okay("");
3309}
3310
lijuanga25d8bb2015-09-16 13:11:52 +08003311void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3312{
3313 char *p = NULL;
3314 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303315 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003316
3317 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303318 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003319 if (p) {
3320 if (!strncmp(p, "0", 1)) {
3321 device.charger_screen_enabled = 0;
3322 } else if (!strncmp(p, "1", 1)) {
3323 device.charger_screen_enabled = 1;
3324 }
3325 }
3326 }
3327
3328 /* update charger_screen_enabled value for getvar
3329 * command
3330 */
3331 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3332 device.charger_screen_enabled);
3333
3334 write_device_info(&device);
3335 fastboot_okay("");
3336}
3337
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303338void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3339{
3340 dprintf(INFO, "Selecting display panel %s\n", arg);
3341 if (arg)
3342 strlcpy(device.display_panel, arg,
3343 sizeof(device.display_panel));
3344 write_device_info(&device);
3345 fastboot_okay("");
3346}
3347
Shashank Mittal162244e2011-08-08 19:01:25 -07003348void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3349{
lijuang4ece1e72015-08-14 21:02:36 +08003350 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303351}
3352
3353void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3354{
lijuang4ece1e72015-08-14 21:02:36 +08003355 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303356 if(!is_allow_unlock) {
3357 fastboot_fail("oem unlock is not allowed");
3358 return;
3359 }
3360
lijuang4ece1e72015-08-14 21:02:36 +08003361 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303362
lijuang4ece1e72015-08-14 21:02:36 +08003363 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303364 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003365
vijay kumarc65876c2015-04-24 13:29:16 +05303366 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3367 write_misc(0, &msg, sizeof(msg));
3368
3369 fastboot_okay("");
3370 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003371 }
3372 fastboot_okay("");
3373}
3374
Channagoud Kadabiad259832015-05-29 11:14:17 -07003375static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3376{
3377 int ret = 1;
3378 uint32_t secret_key;
3379 char seckey_buffer[MAX_RSP_SIZE];
3380
3381 secret_key = aboot_get_secret_key();
3382 if (secret_key)
3383 {
3384 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3385 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3386 {
3387 is_allow_unlock = true;
3388 write_allow_oem_unlock(is_allow_unlock);
3389 ret = 0;
3390 }
3391 }
3392 return ret;
3393}
3394
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003395void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3396{
lijuang4ece1e72015-08-14 21:02:36 +08003397 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003398}
3399
Shashank Mittala0032282011-08-26 14:50:11 -07003400void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3401{
lijuang511a2b52015-08-14 20:50:51 +08003402 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003403 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003404 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003405 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3406 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303407#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003408 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3409 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303410#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003411 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003412 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303413 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3414 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003415 fastboot_okay("");
3416}
3417
lijuang511a2b52015-08-14 20:50:51 +08003418void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3419{
3420 char response[MAX_RSP_SIZE];
3421 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3422 fastboot_info(response);
3423 fastboot_okay("");
3424}
3425
3426void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3427{
lijuang4ece1e72015-08-14 21:02:36 +08003428 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003429}
3430
3431void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3432{
lijuang4ece1e72015-08-14 21:02:36 +08003433 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003434}
3435
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003436void cmd_preflash(const char *arg, void *data, unsigned sz)
3437{
3438 fastboot_okay("");
3439}
3440
Mao Flynn7b379f32015-04-20 00:28:30 +08003441static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303442
Mao Flynn7b379f32015-04-20 00:28:30 +08003443int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303444{
Mao Flynn7b379f32015-04-20 00:28:30 +08003445 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303446 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003447 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303448 return -1;
3449 return 0;
3450}
3451
Mao Flynn7b379f32015-04-20 00:28:30 +08003452int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003453{
3454 struct ptentry *ptn;
3455 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003456 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003457 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003458
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303459 ptable = flash_get_ptable();
3460 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003461 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3462 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303463 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003464
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303465 ptn = ptable_find(ptable, "splash");
3466 if (ptn == NULL) {
3467 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003468 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303469 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003470 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303471 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003472 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303473 }
3474
Mao Flynn7b379f32015-04-20 00:28:30 +08003475 header = (struct logo_img_header *)logo_header;
3476 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303477 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003478 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303479 }
3480
3481 fb_display = fbcon_display();
3482 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003483 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3484 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3485
3486 /* if the logo is full-screen size, remove "fbcon_clear()" */
3487 if ((header->width != fb_display->width)
3488 || (header->height != fb_display->height))
3489 fbcon_clear();
3490
3491 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3492 (uint32_t *)base,
3493 (header->blocks * 512))) {
3494 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3495 return -1;
3496 }
3497 fbcon_extract_to_screen(header, base);
3498 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003499 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003500
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003501 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3502 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003503 return -1;
3504 }
3505
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303506 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003507 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3508 (uint32_t *)base,
3509 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303510 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303511 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003512 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003513 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303514 }
3515
Mao Flynn7b379f32015-04-20 00:28:30 +08003516 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303517}
3518
Mao Flynn7b379f32015-04-20 00:28:30 +08003519int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303520{
3521 int index = INVALID_PTN;
3522 unsigned long long ptn = 0;
3523 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003524 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003525 uint32_t blocksize, realsize, readsize;
3526 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303527
3528 index = partition_get_index("splash");
3529 if (index == 0) {
3530 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003531 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303532 }
3533
3534 ptn = partition_get_offset(index);
3535 if (ptn == 0) {
3536 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003537 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303538 }
3539
Mao Flynn1893a7f2015-06-03 12:03:36 +08003540 mmc_set_lun(partition_get_lun(index));
3541
3542 blocksize = mmc_get_device_blocksize();
3543 if (blocksize == 0) {
3544 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3545 return -1;
3546 }
3547
3548 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003549 if (!fb_display)
3550 {
3551 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3552 return -1;
3553 }
3554
Mao Flynn1893a7f2015-06-03 12:03:36 +08003555 base = (uint8_t *) fb_display->base;
3556
3557 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303558 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003559 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303560 }
3561
Mao Flynn1893a7f2015-06-03 12:03:36 +08003562 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003563 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303564 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003565 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303566 }
3567
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303568 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003569 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3570 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003571
Mao Flynn1893a7f2015-06-03 12:03:36 +08003572 realsize = header->blocks * 512;
3573 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3574
3575 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003576 if ((header->width != fb_display->width)
3577 || (header->height != fb_display->height))
3578 fbcon_clear();
3579
Mao Flynn1893a7f2015-06-03 12:03:36 +08003580 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003581 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3582 return -1;
3583 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003584
Mao Flynn1893a7f2015-06-03 12:03:36 +08003585 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3586 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303587
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003588 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3589 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003590 return -1;
3591 }
3592
3593 realsize = header->width * header->height * fb_display->bpp / 8;
3594 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3595
3596 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3597 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3598 fbcon_clear();
3599 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3600 return -1;
3601 }
3602 } else {
3603 if (mmc_read(ptn + blocksize ,
3604 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3605 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3606 return -1;
3607 }
3608 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3609 }
3610 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303611 }
3612
Mao Flynn7b379f32015-04-20 00:28:30 +08003613 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303614}
3615
Mao Flynn7b379f32015-04-20 00:28:30 +08003616int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303617{
3618 if (target_is_emmc_boot()) {
3619 return splash_screen_mmc();
3620 } else {
3621 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003622 }
3623}
3624
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003625/* Get the size from partiton name */
3626static void get_partition_size(const char *arg, char *response)
3627{
3628 uint64_t ptn = 0;
3629 uint64_t size;
3630 int index = INVALID_PTN;
3631
3632 index = partition_get_index(arg);
3633
3634 if (index == INVALID_PTN)
3635 {
3636 dprintf(CRITICAL, "Invalid partition index\n");
3637 return;
3638 }
3639
3640 ptn = partition_get_offset(index);
3641
3642 if(!ptn)
3643 {
3644 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3645 return;
3646 }
3647
3648 size = partition_get_size(index);
3649
3650 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3651 return;
3652}
3653
3654/*
3655 * Publish the partition type & size info
3656 * fastboot getvar will publish the required information.
3657 * fastboot getvar partition_size:<partition_name>: partition size in hex
3658 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3659 */
3660static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3661{
3662 uint8_t i;
3663
3664 for (i = 0; i < num_parts; i++) {
3665 get_partition_size(info[i].part_name, info[i].size_response);
3666
3667 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3668 {
3669 dprintf(CRITICAL, "partition size name truncated\n");
3670 return;
3671 }
3672 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3673 {
3674 dprintf(CRITICAL, "partition type name truncated\n");
3675 return;
3676 }
3677
3678 /* publish partition size & type info */
3679 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3680 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3681 }
3682}
3683
lijuang4ece1e72015-08-14 21:02:36 +08003684void get_product_name(unsigned char *buf)
3685{
3686 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3687 return;
3688}
3689
3690void get_bootloader_version(unsigned char *buf)
3691{
3692 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3693 return;
3694}
3695
3696void get_baseband_version(unsigned char *buf)
3697{
3698 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3699 return;
3700}
3701
3702bool is_device_locked()
3703{
3704 return device.is_unlocked ? false:true;
3705}
3706
Amol Jadi5edf3552013-07-23 14:15:34 -07003707/* register commands and variables for fastboot */
3708void aboot_fastboot_register_commands(void)
3709{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003710 int i;
lijuangf16461c2015-08-03 17:09:34 +08003711 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003712
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003713 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003714 /* By default the enabled list is empty. */
3715 {"", NULL},
3716 /* move commands enclosed within the below ifndef to here
3717 * if they need to be enabled in user build.
3718 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003719#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003720 /* Register the following commands only for non-user builds */
3721 {"flash:", cmd_flash},
3722 {"erase:", cmd_erase},
3723 {"boot", cmd_boot},
3724 {"continue", cmd_continue},
3725 {"reboot", cmd_reboot},
3726 {"reboot-bootloader", cmd_reboot_bootloader},
3727 {"oem unlock", cmd_oem_unlock},
3728 {"oem unlock-go", cmd_oem_unlock_go},
3729 {"oem lock", cmd_oem_lock},
3730 {"flashing unlock", cmd_oem_unlock},
3731 {"flashing lock", cmd_oem_lock},
3732 {"flashing lock_critical", cmd_flashing_lock_critical},
3733 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3734 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3735 {"oem device-info", cmd_oem_devinfo},
3736 {"preflash", cmd_preflash},
3737 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3738 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003739 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003740 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003741#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003742 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003743#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003744#endif
lijuang511a2b52015-08-14 20:50:51 +08003745 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003746
3747 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3748 for (i = 1; i < fastboot_cmds_count; i++)
3749 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3750
Amol Jadi5edf3552013-07-23 14:15:34 -07003751 /* publish variables and their values */
3752 fastboot_publish("product", TARGET(BOARD));
3753 fastboot_publish("kernel", "lk");
3754 fastboot_publish("serialno", sn_buf);
3755
3756 /*
3757 * partition info is supported only for emmc partitions
3758 * Calling this for NAND prints some error messages which
3759 * is harmless but misleading. Avoid calling this for NAND
3760 * devices.
3761 */
3762 if (target_is_emmc_boot())
3763 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3764
3765 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003766 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3767 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003768 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003769 /* Is the charger screen check enabled */
3770 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3771 device.charger_screen_enabled);
3772 fastboot_publish("charger-screen-enabled",
3773 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003774 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303775 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3776 device.display_panel);
3777 fastboot_publish("display-panel",
3778 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003779 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3780 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003781 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3782 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3783 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3784 target_is_emmc_boot()? "eMMC":"UFS");
3785 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003786#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003787 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003788 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003789 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003790#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003791}
3792
Brian Swetland9c4c0752009-01-25 16:23:50 -08003793void aboot_init(const struct app_descriptor *app)
3794{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003795 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003796
lijuang39831732016-01-08 17:49:02 +08003797 /* Initialise wdog to catch early lk crashes */
3798#if WDOG_SUPPORT
3799 msm_wdog_init();
3800#endif
3801
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003802 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003803 if (target_is_emmc_boot())
3804 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003805 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003806 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303807 mmc_blocksize = mmc_get_device_blocksize();
3808 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003809 }
3810 else
3811 {
3812 page_size = flash_page_size();
3813 page_mask = page_size - 1;
3814 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003815 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3816
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003817 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303818 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003819
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003820 /* Display splash screen if enabled */
3821#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003822#if NO_ALARM_DISPLAY
3823 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003824#endif
lijuang99c02d82015-02-13 19:04:34 +08003825 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003826#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003827 /* Wait if the display shutdown is in progress */
3828 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003829 if (!pm_appsbl_display_init_done())
3830 target_display_init(device.display_panel);
3831 else
3832 display_image_on_screen();
3833#else
lijuang99c02d82015-02-13 19:04:34 +08003834 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003835#endif
lijuang99c02d82015-02-13 19:04:34 +08003836 dprintf(SPEW, "Display Init: Done\n");
3837#if NO_ALARM_DISPLAY
3838 }
3839#endif
3840#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003841
Greg Griscod6250552011-06-29 14:40:23 -07003842 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003843 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003844
Dhaval Patel223ec952013-07-18 14:49:44 -07003845 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3846
Matthew Qindefd5562014-07-11 18:02:40 +08003847 /*
3848 * Check power off reason if user force reset,
3849 * if yes phone will do normal boot.
3850 */
3851 if (is_user_force_reset())
3852 goto normal_boot;
3853
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003854 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003855 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003856 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003857 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08003858 reboot_device(EMERGENCY_DLOAD);
3859 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
3860
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003861 boot_into_fastboot = true;
3862 }
3863 if (!boot_into_fastboot)
3864 {
3865 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3866 boot_into_recovery = 1;
3867 if (!boot_into_recovery &&
3868 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003869 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003870 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003871 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003872 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003873 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003874 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003875
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003876#if USE_PON_REBOOT_REG
3877 reboot_mode = check_hard_reboot_mode();
3878#else
Ajay Dudani77421292010-10-27 19:34:06 -07003879 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003880#endif
3881 if (reboot_mode == RECOVERY_MODE)
3882 {
Ajay Dudani77421292010-10-27 19:34:06 -07003883 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003884 }
3885 else if(reboot_mode == FASTBOOT_MODE)
3886 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003887 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003888 }
3889 else if(reboot_mode == ALARM_BOOT)
3890 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003891 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003892 }
Parth Dixit207573a2015-10-27 17:26:21 +05303893#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303894#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003895 else if (reboot_mode == DM_VERITY_ENFORCING)
3896 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003897 device.verity_mode = 1;
3898 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003899 }
3900 else if (reboot_mode == DM_VERITY_LOGGING)
3901 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003902 device.verity_mode = 0;
3903 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003904 }
3905 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3906 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003907 if(send_delete_keys_to_tz())
3908 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003909 }
Parth Dixit207573a2015-10-27 17:26:21 +05303910#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303911#endif
Ajay Dudani77421292010-10-27 19:34:06 -07003912
Matthew Qindefd5562014-07-11 18:02:40 +08003913normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003914 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003915 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003916 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003917 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003918 if(emmc_recovery_init())
3919 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3920 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003921 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003922 if((device.is_unlocked) || (device.is_tampered))
3923 {
3924 #ifdef TZ_TAMPER_FUSE
3925 set_tamper_fuse_cmd();
3926 #endif
3927 #if USE_PCOM_SECBOOT
3928 set_tamper_flag(device.is_tampered);
3929 #endif
3930 }
Shashank Mittala0032282011-08-26 14:50:11 -07003931 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003932
Pavel Nedev5d91d412013-04-29 11:34:24 +03003933 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003934 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003935 else
3936 {
3937 recovery_init();
3938 #if USE_PCOM_SECBOOT
3939 if((device.is_unlocked) || (device.is_tampered))
3940 set_tamper_flag(device.is_tampered);
3941 #endif
3942 boot_linux_from_flash();
3943 }
3944 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3945 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003946 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003947
Amol Jadi5edf3552013-07-23 14:15:34 -07003948 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003949
Amol Jadi5edf3552013-07-23 14:15:34 -07003950 /* register aboot specific fastboot commands */
3951 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003952
Amol Jadi5edf3552013-07-23 14:15:34 -07003953 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003954 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003955
3956 /* initialize and start fastboot */
3957 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003958#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08003959 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08003960#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003961}
3962
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003963uint32_t get_page_size()
3964{
3965 return page_size;
3966}
3967
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003968/*
3969 * Calculated and save hash (SHA256) for non-signed boot image.
3970 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003971 * @param image_addr - Boot image address
3972 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003973 *
3974 * @return int - 0 on success, negative value on failure.
3975 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003976static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003977{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003978 unsigned int digest[8];
3979#if IMAGE_VERIF_ALGO_SHA1
3980 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3981#else
3982 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3983#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003984
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003985 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003986 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003987
3988 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003989 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003990
3991 return 0;
3992}
3993
Brian Swetland9c4c0752009-01-25 16:23:50 -08003994APP_START(aboot)
3995 .init = aboot_init,
3996APP_END