blob: 1e134ba89431b2b2a4f4d86609db737d5bd87e2c [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
lijuang9a7d3b92015-11-30 14:41:24 +08005 * Copyright (c) 2009-2016, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080060#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053061#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080062#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080063#if USE_RPMB_FOR_DEVINFO
64#include <rpmb.h>
65#endif
Dima Zavin214cc642009-01-26 11:16:21 -080066
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070067#if ENABLE_WBC
68#include <pm_app_smbchg.h>
69#endif
70
Neeti Desai17379b82012-06-04 18:42:53 -070071#if DEVICE_TREE
72#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070073#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070074#endif
75
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053076#if WDOG_SUPPORT
77#include <wdog.h>
78#endif
79
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070080#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070081#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080082#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080083#include "bootimg.h"
84#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070085#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080086#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070087#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070088#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070089#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020091#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070092#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080093#include <menu_keys_detect.h>
94#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070095#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070096
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070097extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070098extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070099extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700100extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530101 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700102
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700103void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700104void write_device_info_mmc(device_info *dev);
105void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700106static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700107static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700108
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700109/* fastboot command function pointer */
110typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
111
112struct fastboot_cmd_desc {
113 char * name;
114 fastboot_cmd_fn cb;
115};
116
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700117#define EXPAND(NAME) #NAME
118#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700119
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800120#ifdef MEMBASE
121#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
122#else
David Ng183a7422009-12-07 14:55:21 -0800123#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800124#endif
125
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700126#ifndef MEMSIZE
127#define MEMSIZE 1024*1024
128#endif
129
130#define MAX_TAGS_SIZE 1024
131
Kun Liang2f1601a2013-08-12 16:29:54 +0800132/* make 4096 as default size to ensure EFS,EXT4's erasing */
133#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700134#define MAX_PANEL_BUF_SIZE 196
Kun Liang2f1601a2013-08-12 16:29:54 +0800135
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700136#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700137#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800138
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800139#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
140
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700141#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
142
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800143#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700144static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700145#else
David Ng183a7422009-12-07 14:55:21 -0800146static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700147#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800148static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300149static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800150static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800151static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800152static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700153static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300154static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200155static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800156
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800157static const char *baseband_apq = " androidboot.baseband=apq";
158static const char *baseband_msm = " androidboot.baseband=msm";
159static const char *baseband_csfb = " androidboot.baseband=csfb";
160static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700161static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800162static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700163static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800164static const char *baseband_dsda = " androidboot.baseband=dsda";
165static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800166static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800167static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800168
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700169#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530170#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700171static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700172static const char *verified_state= " androidboot.verifiedbootstate=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700173//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700174
175struct verified_boot_verity_mode vbvm[] =
176{
177 {false, "logging"},
178 {true, "enforcing"},
179};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700180struct verified_boot_state_name vbsn[] =
181{
182 {GREEN, "green"},
183 {ORANGE, "orange"},
184 {YELLOW,"yellow"},
185 {RED,"red" },
186};
187#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530188#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700189
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700190static unsigned page_size = 0;
191static unsigned page_mask = 0;
192static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
193static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530194static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800195static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700196static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700197bool boot_into_fastboot = false;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700198
Shashank Mittalcd98d472011-08-02 14:29:24 -0700199/* Assuming unauthorized kernel image by default */
200static int auth_kernel_img = 0;
Parth Dixit6f5822f2015-10-18 01:20:53 +0530201#if VBOOT_MOTA
202static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
203#else
lijuang511a2b52015-08-14 20:50:51 +0800204static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
Parth Dixit6f5822f2015-10-18 01:20:53 +0530205#endif
vijay kumarc65876c2015-04-24 13:29:16 +0530206static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700207
vijay kumarca2e6812015-07-08 20:28:25 +0530208static char frp_ptns[2][8] = {"config","frp"};
209
lijuang511a2b52015-08-14 20:50:51 +0800210static const char *critical_flash_allowed_ptn[] = {
211 "aboot",
212 "rpm",
213 "tz",
214 "sbl",
215 "sdi",
216 "sbl1",
217 "xbl",
218 "hyp",
219 "pmic",
220 "bootloader",
221 "devinfo",
222 "partition"};
223
Dima Zavin42168f22009-01-30 11:52:22 -0800224struct atag_ptbl_entry
225{
226 char name[16];
227 unsigned offset;
228 unsigned size;
229 unsigned flags;
230};
231
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700232/*
233 * Partition info, required to be published
234 * for fastboot
235 */
236struct getvar_partition_info {
237 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
238 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
239 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
240 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
241 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
242};
243
244/*
245 * Right now, we are publishing the info for only
246 * three partitions
247 */
248struct getvar_partition_info part_info[] =
249{
250 { "system" , "partition-size:", "partition-type:", "", "ext4" },
251 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
252 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
253};
254
255char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700256char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800257char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700258char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530259char panel_display_mode[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800260
261#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800262char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800263char battery_soc_ok [MAX_RSP_SIZE];
264#endif
265
lijuangf16461c2015-08-03 17:09:34 +0800266char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700267
Greg Griscod2471ef2011-07-14 13:00:42 -0700268extern int emmc_recovery_init(void);
269
Kinson Chik0b1c8162011-08-31 16:31:57 -0700270#if NO_KEYPAD_DRIVER
271extern int fastboot_trigger(void);
272#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700273
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800274static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700275{
276 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700277#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800278 if (is_arm64)
279 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
280 else
281 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700282 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
283 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700284#endif
285}
286
Dima Zavin42168f22009-01-30 11:52:22 -0800287static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
288{
289 struct atag_ptbl_entry atag_ptn;
290
291 memcpy(atag_ptn.name, ptn->name, 16);
292 atag_ptn.name[15] = '\0';
293 atag_ptn.offset = ptn->start;
294 atag_ptn.size = ptn->length;
295 atag_ptn.flags = ptn->flags;
296 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
297 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
298}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800299
lijuang102dfa92015-10-09 18:31:03 +0800300#if CHECK_BAT_VOLTAGE
301void update_battery_status(void)
302{
303 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
304 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
305}
306#endif
307
Neeti Desaie245d492012-06-01 12:52:13 -0700308unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800309{
David Ng183a7422009-12-07 14:55:21 -0800310 int cmdline_len = 0;
311 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800312 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700313 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800314 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300315 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700316 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700317 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200318 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700319#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530320#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700321 uint32_t boot_state = boot_verify_get_state();
322#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530323#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700324
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200325#ifdef MDTP_SUPPORT
326 mdtp_activated(&is_mdtp_activated);
327#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800328
Brian Swetland9c4c0752009-01-25 16:23:50 -0800329 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800330 cmdline_len = strlen(cmdline);
331 have_cmdline = 1;
332 }
333 if (target_is_emmc_boot()) {
334 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800335#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700336 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
337 ASSERT(boot_dev_buf);
338 platform_boot_dev_cmdline(boot_dev_buf);
339 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700340#endif
David Ng183a7422009-12-07 14:55:21 -0800341 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800342
343 cmdline_len += strlen(usb_sn_cmdline);
344 cmdline_len += strlen(sn_buf);
345
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700346#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530347#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700348 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700349 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
350 {
351 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
352 ASSERT(0);
353 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700354 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700355#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530356#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700357
Pavel Nedev5614d222013-06-17 18:01:02 +0300358 if (boot_into_recovery && gpt_exists)
359 cmdline_len += strlen(secondary_gpt_enable);
360
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200361 if(is_mdtp_activated)
362 cmdline_len += strlen(mdtp_activated_flag);
363
Pavel Nedev328ac822013-04-05 15:25:11 +0300364 if (boot_into_ffbm) {
365 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700366 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800367 /* reduce kernel console messages to speed-up boot */
368 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800369 } else if (boot_reason_alarm) {
370 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800371 } else if ((target_build_variant_user() || device.charger_screen_enabled)
372 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700373 pause_at_bootup = 1;
374 cmdline_len += strlen(battchg_pause);
375 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800376
Shashank Mittalcd98d472011-08-02 14:29:24 -0700377 if(target_use_signed_kernel() && auth_kernel_img) {
378 cmdline_len += strlen(auth_kernel);
379 }
380
Joonwoo Park61112782013-10-02 19:50:39 -0700381 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
382 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530383 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700384 have_target_boot_params = 1;
385 cmdline_len += strlen(target_boot_params);
386 }
387
Ajay Dudanid04110c2011-01-17 23:55:07 -0800388 /* Determine correct androidboot.baseband to use */
389 switch(target_baseband())
390 {
391 case BASEBAND_APQ:
392 cmdline_len += strlen(baseband_apq);
393 break;
394
395 case BASEBAND_MSM:
396 cmdline_len += strlen(baseband_msm);
397 break;
398
399 case BASEBAND_CSFB:
400 cmdline_len += strlen(baseband_csfb);
401 break;
402
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800403 case BASEBAND_SVLTE2A:
404 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800405 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700406
407 case BASEBAND_MDM:
408 cmdline_len += strlen(baseband_mdm);
409 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700410
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800411 case BASEBAND_MDM2:
412 cmdline_len += strlen(baseband_mdm2);
413 break;
414
Amol Jadi5c61a952012-05-04 17:05:35 -0700415 case BASEBAND_SGLTE:
416 cmdline_len += strlen(baseband_sglte);
417 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530418
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800419 case BASEBAND_SGLTE2:
420 cmdline_len += strlen(baseband_sglte2);
421 break;
422
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530423 case BASEBAND_DSDA:
424 cmdline_len += strlen(baseband_dsda);
425 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800426
427 case BASEBAND_DSDA2:
428 cmdline_len += strlen(baseband_dsda2);
429 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800430 }
431
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800432 if (cmdline) {
433 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530434 target_display_panel_node(display_panel_buf,
435 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800436 strlen(display_panel_buf)) {
437 cmdline_len += strlen(display_panel_buf);
438 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700439 }
440
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800441 if (target_warm_boot()) {
442 warm_boot = true;
443 cmdline_len += strlen(warmboot_cmdline);
444 }
445
David Ng183a7422009-12-07 14:55:21 -0800446 if (cmdline_len > 0) {
447 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800448 unsigned char *dst;
449
450 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
451 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530452 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800453 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700454
Amol Jadi168b7712012-03-06 16:15:00 -0800455 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800456 if (have_cmdline) {
457 src = cmdline;
458 while ((*dst++ = *src++));
459 }
460 if (target_is_emmc_boot()) {
461 src = emmc_cmdline;
462 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700463 have_cmdline = 1;
464 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800465#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700466 src = boot_dev_buf;
467 if (have_cmdline) --dst;
468 while ((*dst++ = *src++));
469#endif
David Ngf773dde2010-07-26 19:55:08 -0700470 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800471
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700472#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530473#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700474 src = verified_state;
475 if(have_cmdline) --dst;
476 have_cmdline = 1;
477 while ((*dst++ = *src++));
478 src = vbsn[boot_state].name;
479 if(have_cmdline) --dst;
480 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700481
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700482 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
483 {
484 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
485 ASSERT(0);
486 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700487 src = verity_mode;
488 if(have_cmdline) --dst;
489 while ((*dst++ = *src++));
490 src = vbvm[device.verity_mode].name;
491 if(have_cmdline) -- dst;
492 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700493#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530494#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800495 src = usb_sn_cmdline;
496 if (have_cmdline) --dst;
497 have_cmdline = 1;
498 while ((*dst++ = *src++));
499 src = sn_buf;
500 if (have_cmdline) --dst;
501 have_cmdline = 1;
502 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800503 if (warm_boot) {
504 if (have_cmdline) --dst;
505 src = warmboot_cmdline;
506 while ((*dst++ = *src++));
507 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800508
Pavel Nedev5614d222013-06-17 18:01:02 +0300509 if (boot_into_recovery && gpt_exists) {
510 src = secondary_gpt_enable;
511 if (have_cmdline) --dst;
512 while ((*dst++ = *src++));
513 }
514
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200515 if (is_mdtp_activated) {
516 src = mdtp_activated_flag;
517 if (have_cmdline) --dst;
518 while ((*dst++ = *src++));
519 }
520
Pavel Nedev328ac822013-04-05 15:25:11 +0300521 if (boot_into_ffbm) {
522 src = androidboot_mode;
523 if (have_cmdline) --dst;
524 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700525 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300526 if (have_cmdline) --dst;
527 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800528 src = loglevel;
529 if (have_cmdline) --dst;
530 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800531 } else if (boot_reason_alarm) {
532 src = alarmboot_cmdline;
533 if (have_cmdline) --dst;
534 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300535 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700536 src = battchg_pause;
537 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800538 while ((*dst++ = *src++));
539 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800540
Shashank Mittalcd98d472011-08-02 14:29:24 -0700541 if(target_use_signed_kernel() && auth_kernel_img) {
542 src = auth_kernel;
543 if (have_cmdline) --dst;
544 while ((*dst++ = *src++));
545 }
546
Ajay Dudanid04110c2011-01-17 23:55:07 -0800547 switch(target_baseband())
548 {
549 case BASEBAND_APQ:
550 src = baseband_apq;
551 if (have_cmdline) --dst;
552 while ((*dst++ = *src++));
553 break;
554
555 case BASEBAND_MSM:
556 src = baseband_msm;
557 if (have_cmdline) --dst;
558 while ((*dst++ = *src++));
559 break;
560
561 case BASEBAND_CSFB:
562 src = baseband_csfb;
563 if (have_cmdline) --dst;
564 while ((*dst++ = *src++));
565 break;
566
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800567 case BASEBAND_SVLTE2A:
568 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800569 if (have_cmdline) --dst;
570 while ((*dst++ = *src++));
571 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700572
573 case BASEBAND_MDM:
574 src = baseband_mdm;
575 if (have_cmdline) --dst;
576 while ((*dst++ = *src++));
577 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700578
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800579 case BASEBAND_MDM2:
580 src = baseband_mdm2;
581 if (have_cmdline) --dst;
582 while ((*dst++ = *src++));
583 break;
584
Amol Jadi5c61a952012-05-04 17:05:35 -0700585 case BASEBAND_SGLTE:
586 src = baseband_sglte;
587 if (have_cmdline) --dst;
588 while ((*dst++ = *src++));
589 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530590
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800591 case BASEBAND_SGLTE2:
592 src = baseband_sglte2;
593 if (have_cmdline) --dst;
594 while ((*dst++ = *src++));
595 break;
596
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530597 case BASEBAND_DSDA:
598 src = baseband_dsda;
599 if (have_cmdline) --dst;
600 while ((*dst++ = *src++));
601 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800602
603 case BASEBAND_DSDA2:
604 src = baseband_dsda2;
605 if (have_cmdline) --dst;
606 while ((*dst++ = *src++));
607 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800608 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700609
610 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700611 src = display_panel_buf;
612 if (have_cmdline) --dst;
613 while ((*dst++ = *src++));
614 }
Joonwoo Park61112782013-10-02 19:50:39 -0700615
616 if (have_target_boot_params) {
617 if (have_cmdline) --dst;
618 src = target_boot_params;
619 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530620 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700621 }
Neeti Desaie245d492012-06-01 12:52:13 -0700622 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700623
624
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700625 if (boot_dev_buf)
626 free(boot_dev_buf);
627
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800628 if (cmdline_final)
629 dprintf(INFO, "cmdline: %s\n", cmdline_final);
630 else
631 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700632 return cmdline_final;
633}
634
635unsigned *atag_core(unsigned *ptr)
636{
637 /* CORE */
638 *ptr++ = 2;
639 *ptr++ = 0x54410001;
640
641 return ptr;
642
643}
644
645unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
646 unsigned ramdisk_size)
647{
648 if (ramdisk_size) {
649 *ptr++ = 4;
650 *ptr++ = 0x54420005;
651 *ptr++ = (unsigned)ramdisk;
652 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800653 }
654
Neeti Desaie245d492012-06-01 12:52:13 -0700655 return ptr;
656}
657
658unsigned *atag_ptable(unsigned **ptr_addr)
659{
660 int i;
661 struct ptable *ptable;
662
663 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700664 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
665 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700666 *(*ptr_addr)++ = 0x4d534d70;
667 for (i = 0; i < ptable->count; ++i)
668 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
669 }
670
671 return (*ptr_addr);
672}
673
674unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
675{
676 int cmdline_length = 0;
677 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700678 char *dest;
679
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800680 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700681 n = (cmdline_length + 4) & (~3);
682
683 *ptr++ = (n / 4) + 2;
684 *ptr++ = 0x54410009;
685 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800686 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700687 ptr += (n / 4);
688
689 return ptr;
690}
691
692unsigned *atag_end(unsigned *ptr)
693{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800694 /* END */
695 *ptr++ = 0;
696 *ptr++ = 0;
697
Neeti Desaie245d492012-06-01 12:52:13 -0700698 return ptr;
699}
700
701void generate_atags(unsigned *ptr, const char *cmdline,
702 void *ramdisk, unsigned ramdisk_size)
703{
704
705 ptr = atag_core(ptr);
706 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
707 ptr = target_atag_mem(ptr);
708
709 /* Skip NAND partition ATAGS for eMMC boot */
710 if (!target_is_emmc_boot()){
711 ptr = atag_ptable(&ptr);
712 }
713
714 ptr = atag_cmdline(ptr, cmdline);
715 ptr = atag_end(ptr);
716}
717
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700718typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700719void boot_linux(void *kernel, unsigned *tags,
720 const char *cmdline, unsigned machtype,
721 void *ramdisk, unsigned ramdisk_size)
722{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800723 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800724#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700725 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800726#endif
727
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700728 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800729 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530730 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800731
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530732 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700733
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800734 final_cmdline = update_cmdline((const char*)cmdline);
735
Neeti Desai17379b82012-06-04 18:42:53 -0700736#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800737 dprintf(INFO, "Updating device tree: start\n");
738
Neeti Desai17379b82012-06-04 18:42:53 -0700739 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530740 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700741 if(ret)
742 {
743 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
744 ASSERT(0);
745 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800746 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700747#else
Neeti Desaie245d492012-06-01 12:52:13 -0700748 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800749 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700750#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700751
Maria Yu52254c02014-07-04 16:14:54 +0800752 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700753
754#if VERIFIED_BOOT
755 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700756 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700757 {
758 dprintf(INFO, "Failed to write protect dev info\n");
759 ASSERT(0);
760 }
761#endif
762
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800763 /* Turn off splash screen if enabled */
764#if DISPLAY_SPLASH_SCREEN
765 target_display_shutdown();
766#endif
767
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700768 /* Perform target specific cleanup */
769 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800770
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800771 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530772 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800773
774 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700775
Amol Jadi4421e652011-06-16 15:00:48 -0700776 /* do any platform specific cleanup before kernel entry */
777 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700778
Brian Swetland9c4c0752009-01-25 16:23:50 -0800779 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700780
Amol Jadi504f9fe2012-08-16 13:56:48 -0700781#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800782 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700783#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700784 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800785
786 if (IS_ARM64(kptr))
787 /* Jump to a 64bit kernel */
788 scm_elexec_call((paddr_t)kernel, tags_phys);
789 else
790 /* Jump to a 32bit kernel */
791 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800792}
793
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700794/* Function to check if the memory address range falls within the aboot
795 * boundaries.
796 * start: Start of the memory region
797 * size: Size of the memory region
798 */
799int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
800{
801 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800802 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700803 return -1;
804
805 /* Check for memory overlap. */
806 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
807 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700808 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700809 return 0;
810 else
811 return -1;
812}
813
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800814#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800815
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800816BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800817#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800818BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800819#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800820
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700821static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
822{
823 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800824
825#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800826#if IMAGE_VERIF_ALGO_SHA1
827 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
828#else
829 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
830#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800831#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700832
833 /* Assume device is rooted at this time. */
834 device.is_tampered = 1;
835
836 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
837
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700838#if VERIFIED_BOOT
839 if(boot_into_recovery)
840 {
841 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530842 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700843 }
844 else
845 {
846 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530847 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700848 }
849 boot_verify_print_state();
850#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700851 ret = image_verify((unsigned char *)bootimg_addr,
852 (unsigned char *)(bootimg_addr + bootimg_size),
853 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800854 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700855#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700856 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
857
858 if (ret)
859 {
860 /* Authorized kernel */
861 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700862 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700863 }
864
Amit Blay4aa292f2015-04-28 21:55:59 +0300865#ifdef MDTP_SUPPORT
866 {
867 /* Verify MDTP lock.
868 * For boot & recovery partitions, use aboot's verification result.
869 */
870 mdtp_ext_partition_verification_t ext_partition;
871 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
872 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
873 ext_partition.page_size = 0; /* Not needed since already validated */
874 ext_partition.image_addr = 0; /* Not needed since already validated */
875 ext_partition.image_size = 0; /* Not needed since already validated */
876 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
877 mdtp_fwlock_verify_lock(&ext_partition);
878 }
879#endif /* MDTP_SUPPORT */
880
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700881#if USE_PCOM_SECBOOT
882 set_tamper_flag(device.is_tampered);
883#endif
884
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700885#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700886 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700887 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700888 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800889#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800890 display_bootverify_menu(DISPLAY_MENU_RED);
lijuanga40d6302015-07-20 20:10:13 +0800891 wait_for_users_action();
892#else
893 dprintf(CRITICAL,
894 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
895 mdelay(5000);
896#endif
897
898 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700899 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800900#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800901 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800902 wait_for_users_action();
903#else
904 dprintf(CRITICAL,
905 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
906 mdelay(5000);
907#endif
908 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700909 default:
lijuanga40d6302015-07-20 20:10:13 +0800910 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700911 }
912#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700913#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530914 if(device.is_tampered)
915 {
916 write_device_info_mmc(&device);
917 #ifdef TZ_TAMPER_FUSE
918 set_tamper_fuse_cmd();
919 #endif
920 #ifdef ASSERT_ON_TAMPER
921 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
922 ASSERT(0);
923 #endif
924 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700925#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700926}
927
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530928static bool check_format_bit()
929{
930 bool ret = false;
931 int index;
932 uint64_t offset;
933 struct boot_selection_info *in = NULL;
934 char *buf = NULL;
935
936 index = partition_get_index("bootselect");
937 if (index == INVALID_PTN)
938 {
939 dprintf(INFO, "Unable to locate /bootselect partition\n");
940 return ret;
941 }
942 offset = partition_get_offset(index);
943 if(!offset)
944 {
945 dprintf(INFO, "partition /bootselect doesn't exist\n");
946 return ret;
947 }
948 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
949 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530950 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530951 {
952 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
953 free(buf);
954 return ret;
955 }
956 in = (struct boot_selection_info *) buf;
957 if ((in->signature == BOOTSELECT_SIGNATURE) &&
958 (in->version == BOOTSELECT_VERSION)) {
959 if ((in->state_info & BOOTSELECT_FORMAT) &&
960 !(in->state_info & BOOTSELECT_FACTORY))
961 ret = true;
962 } else {
963 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
964 in->signature, in->version);
965 ASSERT(0);
966 }
967 free(buf);
968 return ret;
969}
970
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700971void boot_verifier_init()
972{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700973 uint32_t boot_state;
974 /* Check if device unlock */
975 if(device.is_unlocked)
976 {
977 boot_verify_send_event(DEV_UNLOCK);
978 boot_verify_print_state();
979 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
980 return;
981 }
982 else
983 {
984 boot_verify_send_event(BOOT_INIT);
985 }
986
987 /* Initialize keystore */
988 boot_state = boot_verify_keystore_init();
989 if(boot_state == YELLOW)
990 {
991 boot_verify_print_state();
992 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
993 }
994}
995
Shashank Mittal23b8f422010-04-16 19:27:21 -0700996int boot_linux_from_mmc(void)
997{
998 struct boot_img_hdr *hdr = (void*) buf;
999 struct boot_img_hdr *uhdr;
1000 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001001 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001002 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001003 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001004
Shashank Mittalcd98d472011-08-02 14:29:24 -07001005 unsigned char *image_addr = 0;
1006 unsigned kernel_actual;
1007 unsigned ramdisk_actual;
1008 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001009 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001010
Matthew Qin271927e2015-03-31 22:07:07 -04001011 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001012 unsigned int out_len = 0;
1013 unsigned int out_avai_len = 0;
1014 unsigned char *out_addr = NULL;
1015 uint32_t dtb_offset = 0;
1016 unsigned char *kernel_start_addr = NULL;
1017 unsigned int kernel_size = 0;
1018 int rc;
1019
Neeti Desai465491e2012-07-31 12:53:35 -07001020#if DEVICE_TREE
1021 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001022 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001023 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001024 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001025 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001026 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001027#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001028 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001029
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301030 if (check_format_bit())
1031 boot_into_recovery = 1;
1032
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001033 if (!boot_into_recovery) {
1034 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1035 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1036 if (rcode <= 0) {
1037 boot_into_ffbm = false;
1038 if (rcode < 0)
1039 dprintf(CRITICAL,"failed to get ffbm cookie");
1040 } else
1041 boot_into_ffbm = true;
1042 } else
1043 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001044 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1045 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1046 dprintf(INFO, "Unified boot method!\n");
1047 hdr = uhdr;
1048 goto unified_boot;
1049 }
Greg Griscod6250552011-06-29 14:40:23 -07001050 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001051 index = partition_get_index("boot");
1052 ptn = partition_get_offset(index);
1053 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001054 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1055 return -1;
1056 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001057 }
1058 else {
1059 index = partition_get_index("recovery");
1060 ptn = partition_get_offset(index);
1061 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001062 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1063 return -1;
1064 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001065 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001066 /* Set Lun for boot & recovery partitions */
1067 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001068
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301069 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001070 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1071 return -1;
1072 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001073
1074 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001075 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001076 return -1;
1077 }
1078
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001079 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301080
1081 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1082 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1083 return -1;
1084 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001085 page_size = hdr->page_size;
1086 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001087 }
1088
vijay kumar287bb542015-09-29 13:01:52 +05301089 /* ensure commandline is terminated */
1090 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1091
Matthew Qin49e51fa2015-02-09 10:40:45 +08001092 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1093 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001094
Matthew Qin49e51fa2015-02-09 10:40:45 +08001095 image_addr = (unsigned char *)target_get_scratch_address();
1096
1097#if DEVICE_TREE
1098 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1099 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1100#else
1101 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1102#endif
1103
1104#if VERIFIED_BOOT
1105 boot_verifier_init();
1106#endif
1107
1108 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
1109 {
1110 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1111 return -1;
1112 }
1113
Matthew Qinbb7923d2015-02-09 10:56:09 +08001114 /*
1115 * Update loading flow of bootimage to support compressed/uncompressed
1116 * bootimage on both 64bit and 32bit platform.
1117 * 1. Load bootimage from emmc partition onto DDR.
1118 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1119 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1120 * platform accordingly.
1121 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1122 */
1123
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001124 dprintf(INFO, "Loading (%s) image (%d): start\n",
1125 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001126 bs_set_timestamp(BS_KERNEL_LOAD_START);
1127
Matthew Qinbb7923d2015-02-09 10:56:09 +08001128 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001129 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1130 {
1131 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1132 return -1;
1133 }
1134
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001135 dprintf(INFO, "Loading (%s) image (%d): done\n",
1136 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1137
Matthew Qin49e51fa2015-02-09 10:40:45 +08001138 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1139
1140 /* Authenticate Kernel */
1141 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1142 (int) target_use_signed_kernel(),
1143 device.is_unlocked,
1144 device.is_tampered);
1145
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001146 /* Change the condition a little bit to include the test framework support.
1147 * We would never reach this point if device is in fastboot mode, even if we did
1148 * that means we are in test mode, so execute kernel authentication part for the
1149 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301150 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001151 {
1152 offset = imagesize_actual;
1153 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1154 {
1155 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1156 return -1;
1157 }
1158
1159 /* Read signature */
1160 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1161 {
1162 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1163 return -1;
1164 }
1165
1166 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001167 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301168 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001169 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001170 } else {
1171 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1172 #ifdef TZ_SAVE_KERNEL_HASH
1173 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1174 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001175
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001176#if VERIFIED_BOOT
1177 if(boot_verify_get_state() == ORANGE)
1178 {
lijuanga40d6302015-07-20 20:10:13 +08001179#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001180 display_bootverify_menu(DISPLAY_MENU_ORANGE);
lijuanga40d6302015-07-20 20:10:13 +08001181 wait_for_users_action();
1182#else
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001183 dprintf(CRITICAL,
lijuanga40d6302015-07-20 20:10:13 +08001184 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001185 mdelay(5000);
lijuanga40d6302015-07-20 20:10:13 +08001186#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001187 }
1188#endif
1189
Amit Blay4aa292f2015-04-28 21:55:59 +03001190#ifdef MDTP_SUPPORT
1191 {
1192 /* Verify MDTP lock.
1193 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1194 * since verification was skipped in aboot. The signature is not part of the loaded image.
1195 */
1196 mdtp_ext_partition_verification_t ext_partition;
1197 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1198 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1199 ext_partition.page_size = page_size;
1200 ext_partition.image_addr = (uint32)image_addr;
1201 ext_partition.image_size = imagesize_actual;
1202 ext_partition.sig_avail = FALSE;
1203 mdtp_fwlock_verify_lock(&ext_partition);
1204 }
1205#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001206 }
1207
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001208#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05301209#if !VBOOT_MOTA
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001210 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001211 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001212 ASSERT(0);
1213#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05301214#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001215 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001216 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1217 * If not, continue booting.
1218 */
1219 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1220 {
1221 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1222 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001223 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001224 rc = decompress((unsigned char *)(image_addr + page_size),
1225 hdr->kernel_size, out_addr, out_avai_len,
1226 &dtb_offset, &out_len);
1227 if (rc)
1228 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001229 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001230 ASSERT(0);
1231 }
1232
Matthew Qin0b15b322015-05-19 05:20:54 -04001233 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001234 kptr = (struct kernel64_hdr *)out_addr;
1235 kernel_start_addr = out_addr;
1236 kernel_size = out_len;
1237 } else {
1238 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1239 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1240 kernel_size = hdr->kernel_size;
1241 }
1242
1243 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001244 * Update the kernel/ramdisk/tags address if the boot image header
1245 * has default values, these default values come from mkbootimg when
1246 * the boot image is flashed using fastboot flash:raw
1247 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001248 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001249
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001250 /* Get virtual addresses since the hdr saves physical addresses. */
1251 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1252 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1253 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001254
Matthew Qinbb7923d2015-02-09 10:56:09 +08001255 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001256 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001257 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001258 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1259 {
1260 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1261 return -1;
1262 }
1263
1264#ifndef DEVICE_TREE
1265 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1266 {
1267 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1268 return -1;
1269 }
1270#endif
1271
Matthew Qin49e51fa2015-02-09 10:40:45 +08001272 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001273 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001274 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001275
Matthew Qin49e51fa2015-02-09 10:40:45 +08001276 #if DEVICE_TREE
1277 if(hdr->dt_size) {
1278 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1279 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001280
Matthew Qin49e51fa2015-02-09 10:40:45 +08001281 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1282 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1283 return -1;
1284 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001285
Matthew Qin49e51fa2015-02-09 10:40:45 +08001286 /* Find index of device tree within device tree table */
1287 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1288 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1289 return -1;
1290 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001291
Matthew Qin271927e2015-03-31 22:07:07 -04001292 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1293 {
1294 unsigned int compressed_size = 0;
1295 out_addr += out_len;
1296 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001297 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001298 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1299 dt_entry.size, out_addr, out_avai_len,
1300 &compressed_size, &dtb_size);
1301 if (rc)
1302 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001303 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001304 ASSERT(0);
1305 }
1306
Matthew Qin0b15b322015-05-19 05:20:54 -04001307 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001308 best_match_dt_addr = out_addr;
1309 } else {
1310 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1311 dtb_size = dt_entry.size;
1312 }
1313
Matthew Qin49e51fa2015-02-09 10:40:45 +08001314 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001315 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001316 {
1317 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1318 return -1;
1319 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001320
Matthew Qin271927e2015-03-31 22:07:07 -04001321 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001322 } else {
1323 /* Validate the tags_addr */
1324 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001325 {
1326 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1327 return -1;
1328 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001329 /*
1330 * If appended dev tree is found, update the atags with
1331 * memory address to the DTB appended location on RAM.
1332 * Else update with the atags address in the kernel header
1333 */
1334 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001335 dtb = dev_tree_appended((void*)(image_addr + page_size),
1336 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001337 (void *)hdr->tags_addr);
1338 if (!dtb) {
1339 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001340 return -1;
1341 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001342 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001343 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001344
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001345 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1346 target_load_ssd_keystore();
1347
Shashank Mittal23b8f422010-04-16 19:27:21 -07001348unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001349
Dima Zavin77e41f32013-03-06 16:10:43 -08001350 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001351 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001352 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1353
1354 return 0;
1355}
1356
Dima Zavin214cc642009-01-26 11:16:21 -08001357int boot_linux_from_flash(void)
1358{
1359 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001360 struct ptentry *ptn;
1361 struct ptable *ptable;
1362 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001363
Shashank Mittalcd98d472011-08-02 14:29:24 -07001364 unsigned char *image_addr = 0;
1365 unsigned kernel_actual;
1366 unsigned ramdisk_actual;
1367 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001368 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001369
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001370#if DEVICE_TREE
1371 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001372 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001373 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001374 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001375#endif
1376
David Ng183a7422009-12-07 14:55:21 -08001377 if (target_is_emmc_boot()) {
1378 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1379 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1380 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1381 return -1;
1382 }
1383 goto continue_boot;
1384 }
1385
Dima Zavin214cc642009-01-26 11:16:21 -08001386 ptable = flash_get_ptable();
1387 if (ptable == NULL) {
1388 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1389 return -1;
1390 }
1391
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001392 if(!boot_into_recovery)
1393 {
1394 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001395
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001396 if (ptn == NULL) {
1397 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1398 return -1;
1399 }
1400 }
1401 else
1402 {
1403 ptn = ptable_find(ptable, "recovery");
1404 if (ptn == NULL) {
1405 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1406 return -1;
1407 }
Dima Zavin214cc642009-01-26 11:16:21 -08001408 }
1409
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001410 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001411 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1412 return -1;
1413 }
Dima Zavin214cc642009-01-26 11:16:21 -08001414
1415 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001416 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001417 return -1;
1418 }
1419
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001420 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001421 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 -08001422 return -1;
1423 }
1424
vijay kumar287bb542015-09-29 13:01:52 +05301425 /* ensure commandline is terminated */
1426 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1427
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001428 /*
1429 * Update the kernel/ramdisk/tags address if the boot image header
1430 * has default values, these default values come from mkbootimg when
1431 * the boot image is flashed using fastboot flash:raw
1432 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001433 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001434
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001435 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001436 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1437 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1438 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1439
1440 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1441 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1442
1443 /* Check if the addresses in the header are valid. */
1444 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1445 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1446 {
1447 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1448 return -1;
1449 }
1450
1451#ifndef DEVICE_TREE
1452 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1453 {
1454 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1455 return -1;
1456 }
1457#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001458
Shashank Mittalcd98d472011-08-02 14:29:24 -07001459 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001460 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001461 {
1462 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001463 offset = 0;
1464
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001465#if DEVICE_TREE
1466 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
vijay kumar966a9822015-12-09 18:36:09 +05301467
1468 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1469 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1470 return -1;
1471 }
1472
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001473 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001474
1475 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1476 {
1477 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1478 return -1;
1479 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001480#else
vijay kumar966a9822015-12-09 18:36:09 +05301481 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) {
1482 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1483 return -1;
1484 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001485 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001486#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001487
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001488 dprintf(INFO, "Loading (%s) image (%d): start\n",
1489 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001490 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001491
Shashank Mittalcd98d472011-08-02 14:29:24 -07001492 /* Read image without signature */
1493 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1494 {
1495 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1496 return -1;
1497 }
Dima Zavin214cc642009-01-26 11:16:21 -08001498
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001499 dprintf(INFO, "Loading (%s) image (%d): done\n",
1500 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001501 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001502
Shashank Mittalcd98d472011-08-02 14:29:24 -07001503 offset = imagesize_actual;
1504 /* Read signature */
1505 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1506 {
1507 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001508 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001509 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001510
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301511 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001512
1513 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001514 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1515 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001516#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001517 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001518 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001519 {
1520 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1521 return -1;
1522 }
1523
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001524 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1525#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001526
1527 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001528 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001529 {
1530 write_device_info_flash(&device);
1531 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301532#if USE_PCOM_SECBOOT
1533 set_tamper_flag(device.is_tampered);
1534#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001535 }
1536 else
1537 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001538 offset = page_size;
1539
Amol Jadib6be5c12012-11-14 13:39:51 -08001540 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1541 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1542 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1543
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001544 dprintf(INFO, "Loading (%s) image (%d): start\n",
1545 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1546
Amol Jadi492d5a52013-03-15 16:12:34 -07001547 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001548
1549 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001550 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1551 return -1;
1552 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001553 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001554
Amol Jadib6be5c12012-11-14 13:39:51 -08001555 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001556 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1557 return -1;
1558 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001559 offset += ramdisk_actual;
1560
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001561 dprintf(INFO, "Loading (%s) image (%d): done\n",
1562 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1563
Amol Jadi492d5a52013-03-15 16:12:34 -07001564 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001565
1566 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001567 offset += second_actual;
1568 /* Second image loading not implemented. */
1569 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001570 }
1571
1572#if DEVICE_TREE
1573 if(hdr->dt_size != 0) {
1574
1575 /* Read the device tree table into buffer */
1576 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1577 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1578 return -1;
1579 }
1580
1581 table = (struct dt_table*) dt_buf;
1582
Deepa Dinamani19648b42013-09-05 17:05:55 -07001583 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001584 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1585 return -1;
1586 }
1587
Deepa Dinamani19648b42013-09-05 17:05:55 -07001588 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1589 if (!table)
1590 return -1;
1591
1592 /* Read the entire device tree table into buffer */
1593 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1594 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1595 return -1;
1596 }
1597
1598
Joel Kingaa335dc2013-06-03 16:11:08 -07001599 /* Find index of device tree within device tree table */
1600 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001601 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1602 return -1;
1603 }
1604
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001605 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001606 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001607 {
1608 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1609 return -1;
1610 }
1611
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001612 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001613 if(flash_read(ptn, offset + dt_entry.offset,
1614 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001615 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1616 return -1;
1617 }
1618 }
1619#endif
1620
Shashank Mittalcd98d472011-08-02 14:29:24 -07001621 }
David Ng183a7422009-12-07 14:55:21 -08001622continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001623
Dima Zavin214cc642009-01-26 11:16:21 -08001624 /* TODO: create/pass atags to kernel */
1625
Ajay Dudanie28a6072011-07-01 13:59:46 -07001626 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001627 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001628 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1629
1630 return 0;
1631}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001632
Shashank Mittal162244e2011-08-08 19:01:25 -07001633void write_device_info_mmc(device_info *dev)
1634{
Shashank Mittal162244e2011-08-08 19:01:25 -07001635 unsigned long long ptn = 0;
1636 unsigned long long size;
1637 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001638 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001639 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001640 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001641
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001642 if (devinfo_present)
1643 index = partition_get_index("devinfo");
1644 else
1645 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001646
Shashank Mittal162244e2011-08-08 19:01:25 -07001647 ptn = partition_get_offset(index);
1648 if(ptn == 0)
1649 {
1650 return;
1651 }
1652
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001653 lun = partition_get_lun(index);
1654 mmc_set_lun(lun);
1655
Shashank Mittal162244e2011-08-08 19:01:25 -07001656 size = partition_get_size(index);
1657
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001658 blocksize = mmc_get_device_blocksize();
1659
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001660 if (devinfo_present)
1661 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1662 else
1663 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1664 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001665 {
1666 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001667 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001668 }
1669}
1670
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001671void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001672{
Shashank Mittal162244e2011-08-08 19:01:25 -07001673 unsigned long long ptn = 0;
1674 unsigned long long size;
1675 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001676 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001677 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001678
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001679 if ((index = partition_get_index("devinfo")) < 0)
1680 {
1681 devinfo_present = false;
1682 index = partition_get_index("aboot");
1683 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001684
Shashank Mittal162244e2011-08-08 19:01:25 -07001685 ptn = partition_get_offset(index);
1686 if(ptn == 0)
1687 {
1688 return;
1689 }
1690
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001691 mmc_set_lun(partition_get_lun(index));
1692
Shashank Mittal162244e2011-08-08 19:01:25 -07001693 size = partition_get_size(index);
1694
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001695 blocksize = mmc_get_device_blocksize();
1696
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001697 if (devinfo_present)
1698 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1699 else
1700 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1701 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001702 {
1703 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001704 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001705 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001706}
1707
1708void write_device_info_flash(device_info *dev)
1709{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001710 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001711 struct ptentry *ptn;
1712 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001713 if(info == NULL)
1714 {
1715 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1716 ASSERT(0);
1717 }
1718 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001719 ptable = flash_get_ptable();
1720 if (ptable == NULL)
1721 {
1722 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1723 return;
1724 }
1725
1726 ptn = ptable_find(ptable, "devinfo");
1727 if (ptn == NULL)
1728 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001729 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001730 return;
1731 }
1732
1733 memcpy(info, dev, sizeof(device_info));
1734
1735 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1736 {
1737 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1738 return;
1739 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001740 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001741}
1742
vijay kumarc65876c2015-04-24 13:29:16 +05301743static int read_allow_oem_unlock(device_info *dev)
1744{
vijay kumarc65876c2015-04-24 13:29:16 +05301745 unsigned offset;
1746 int index;
1747 unsigned long long ptn;
1748 unsigned long long ptn_size;
1749 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001750 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301751
vijay kumarca2e6812015-07-08 20:28:25 +05301752 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301753 if (index == INVALID_PTN)
1754 {
vijay kumarca2e6812015-07-08 20:28:25 +05301755 index = partition_get_index(frp_ptns[1]);
1756 if (index == INVALID_PTN)
1757 {
1758 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1759 return -1;
1760 }
vijay kumarc65876c2015-04-24 13:29:16 +05301761 }
1762
1763 ptn = partition_get_offset(index);
1764 ptn_size = partition_get_size(index);
1765 offset = ptn_size - blocksize;
1766
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001767 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301768 {
1769 dprintf(CRITICAL, "Reading MMC failed\n");
1770 return -1;
1771 }
1772
1773 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1774 is_allow_unlock = buf[blocksize-1] & 0x01;
1775 return 0;
1776}
1777
1778static int write_allow_oem_unlock(bool allow_unlock)
1779{
vijay kumarc65876c2015-04-24 13:29:16 +05301780 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301781 int index;
1782 unsigned long long ptn;
1783 unsigned long long ptn_size;
1784 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001785 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301786
vijay kumarca2e6812015-07-08 20:28:25 +05301787 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301788 if (index == INVALID_PTN)
1789 {
vijay kumarca2e6812015-07-08 20:28:25 +05301790 index = partition_get_index(frp_ptns[1]);
1791 if (index == INVALID_PTN)
1792 {
1793 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1794 return -1;
1795 }
vijay kumarc65876c2015-04-24 13:29:16 +05301796 }
1797
1798 ptn = partition_get_offset(index);
1799 ptn_size = partition_get_size(index);
1800 offset = ptn_size - blocksize;
1801
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001802 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301803 {
1804 dprintf(CRITICAL, "Reading MMC failed\n");
1805 return -1;
1806 }
1807
1808 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1809 buf[blocksize-1] = allow_unlock;
1810 if (mmc_write(ptn + offset, blocksize, buf))
1811 {
1812 dprintf(CRITICAL, "Writing MMC failed\n");
1813 return -1;
1814 }
1815
1816 return 0;
1817}
1818
Shashank Mittal162244e2011-08-08 19:01:25 -07001819void read_device_info_flash(device_info *dev)
1820{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001821 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001822 struct ptentry *ptn;
1823 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001824 if(info == NULL)
1825 {
1826 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1827 ASSERT(0);
1828 }
1829 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001830 ptable = flash_get_ptable();
1831 if (ptable == NULL)
1832 {
1833 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1834 return;
1835 }
1836
1837 ptn = ptable_find(ptable, "devinfo");
1838 if (ptn == NULL)
1839 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001840 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001841 return;
1842 }
1843
1844 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1845 {
1846 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1847 return;
1848 }
1849
1850 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1851 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001852 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1853 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001854 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001855 write_device_info_flash(info);
1856 }
1857 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001858 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001859}
1860
1861void write_device_info(device_info *dev)
1862{
1863 if(target_is_emmc_boot())
1864 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001865 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1866 if(info == NULL)
1867 {
1868 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1869 ASSERT(0);
1870 }
1871 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001872 memcpy(info, dev, sizeof(struct device_info));
1873
1874#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001875 if (is_secure_boot_enable()) {
1876 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1877 ASSERT(0);
1878 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001879 else
1880 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001881#else
1882 write_device_info_mmc(info);
1883#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001884 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001885 }
1886 else
1887 {
1888 write_device_info_flash(dev);
1889 }
1890}
1891
1892void read_device_info(device_info *dev)
1893{
1894 if(target_is_emmc_boot())
1895 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001896 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1897 if(info == NULL)
1898 {
1899 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1900 ASSERT(0);
1901 }
1902 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001903
1904#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001905 if (is_secure_boot_enable()) {
1906 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1907 ASSERT(0);
1908 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001909 else
1910 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001911#else
1912 read_device_info_mmc(info);
1913#endif
1914
1915 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1916 {
1917 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08001918 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001919 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301920#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08001921 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301922#endif
lijuang511a2b52015-08-14 20:50:51 +08001923 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001924 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05301925#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08001926 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05301927#endif
lijuang511a2b52015-08-14 20:50:51 +08001928 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001929 info->is_tampered = 0;
1930 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301931#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07001932 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05301933#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001934 write_device_info(info);
1935 }
1936 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001937 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001938 }
1939 else
1940 {
1941 read_device_info_flash(dev);
1942 }
1943}
1944
1945void reset_device_info()
1946{
1947 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001948 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001949 write_device_info(&device);
1950}
1951
1952void set_device_root()
1953{
1954 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001955 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001956 write_device_info(&device);
1957}
1958
lijuang4ece1e72015-08-14 21:02:36 +08001959/* set device unlock value
1960 * Must check FRP before call this function
1961 * Need to wipe data when unlock status changed
1962 * type 0: oem unlock
1963 * type 1: unlock critical
1964 * status 0: unlock as false
1965 * status 1: lock as true
1966 */
1967void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08001968{
lijuang4ece1e72015-08-14 21:02:36 +08001969 if (type == UNLOCK)
1970 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05301971#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08001972 else if (type == UNLOCK_CRITICAL)
1973 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05301974#endif
lijuang4ece1e72015-08-14 21:02:36 +08001975 write_device_info(&device);
1976}
1977
1978static void set_device_unlock(int type, bool status)
1979{
1980 int is_unlocked = -1;
1981 char response[MAX_RSP_SIZE];
1982
1983 /* check device unlock status if it is as expected */
1984 if (type == UNLOCK)
1985 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05301986#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08001987 else if (type == UNLOCK_CRITICAL)
1988 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05301989#endif
lijuang4ece1e72015-08-14 21:02:36 +08001990 if (is_unlocked == status) {
1991 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
1992 fastboot_info(response);
1993 fastboot_okay("");
1994 return;
1995 }
1996
1997 /* status is true, it means to unlock device */
1998 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08001999 if(!is_allow_unlock) {
2000 fastboot_fail("oem unlock is not allowed");
2001 return;
2002 }
2003
lijuang4ece1e72015-08-14 21:02:36 +08002004#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002005 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002006 fastboot_okay("");
2007 return;
2008#else
2009 if (type == UNLOCK) {
2010 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2011 return;
2012 }
2013#endif
lijuang21f12f52015-08-22 16:22:19 +08002014 }
lijuang4ece1e72015-08-14 21:02:36 +08002015
2016 set_device_unlock_value(type, status);
2017
2018 /* wipe data */
2019 struct recovery_message msg;
2020
2021 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2022 write_misc(0, &msg, sizeof(msg));
2023
2024 fastboot_okay("");
2025 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002026}
2027
lijuang511a2b52015-08-14 20:50:51 +08002028static bool critical_flash_allowed(const char * entry)
2029{
2030 uint32_t i = 0;
2031 if (entry == NULL)
2032 return false;
2033
2034 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2035 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2036 return true;
2037 }
2038 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002039}
2040
2041#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002042int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2043{
2044 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002045 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002046 struct dt_table *table;
2047 struct dt_entry dt_entry;
2048 uint32_t dt_hdr_size;
2049 unsigned int compressed_size = 0;
2050 unsigned int dtb_size = 0;
2051 unsigned int out_avai_len = 0;
2052 unsigned char *out_addr = NULL;
2053 unsigned char *best_match_dt_addr = NULL;
2054 int rc;
2055
2056 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2057
2058 if(hdr->dt_size != 0) {
2059 /* add kernel offset */
2060 dt_image_offset += page_size;
2061 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002062 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002063
Deepa Dinamani19648b42013-09-05 17:05:55 -07002064 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002065 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2066 dt_image_offset += n;
2067
Joel Kingaa335dc2013-06-03 16:11:08 -07002068 /* add second offset */
2069 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002070 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2071 dt_image_offset += n;
2072 }
2073
Matthew Qin271927e2015-03-31 22:07:07 -04002074 /* offset now point to start of dt.img */
2075 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2076
2077 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2078 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002079 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002080 }
2081 /* Find index of device tree within device tree table */
2082 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2083 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2084 return -1;
Matthew Qin0b15b322015-05-19 05:20:54 -04002085 }
Matthew Qin271927e2015-03-31 22:07:07 -04002086
2087 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2088 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
Matthew Qin0b15b322015-05-19 05:20:54 -04002089 {
Matthew Qin271927e2015-03-31 22:07:07 -04002090 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2091 out_avai_len = target_get_max_flash_size() - scratch_offset;
2092 dprintf(INFO, "decompressing dtb: start\n");
2093 rc = decompress(best_match_dt_addr,
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002094 dt_entry.size, out_addr, out_avai_len,
Matthew Qin271927e2015-03-31 22:07:07 -04002095 &compressed_size, &dtb_size);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002096 if (rc)
2097 {
2098 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
2099 ASSERT(0);
2100 }
Amol Jadicb524072012-08-09 16:40:18 -07002101
Matthew Qin271927e2015-03-31 22:07:07 -04002102 dprintf(INFO, "decompressing dtb: done\n");
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002103 best_match_dt_addr = out_addr;
2104 } else {
Amol Jadicb524072012-08-09 16:40:18 -07002105 dtb_size = dt_entry.size;
2106 }
2107 /* Validate and Read device device tree in the "tags_add */
2108 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
2109 {
2110 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002111 return -1;
2112 }
Amit Blay8a510302015-08-17 09:20:01 +03002113
2114 /* Read device device tree in the "tags_add */
2115 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002116 } else
2117 return -1;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002118
2119 /* Everything looks fine. Return success. */
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002120 return 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002121}
2122#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002123
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002124void cmd_boot(const char *arg, void *data, unsigned sz)
2125{
Matthew Qinbb7923d2015-02-09 10:56:09 +08002126#ifdef MDTP_SUPPORT
2127 static bool is_mdtp_activated = 0;
2128#endif /* MDTP_SUPPORT */
2129 unsigned kernel_actual;
2130 unsigned ramdisk_actual;
2131 uint32_t image_actual;
Matthew Qin271927e2015-03-31 22:07:07 -04002132 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002133 uint32_t sig_actual = SIGNATURE_SIZE;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002134 struct boot_img_hdr *hdr = NULL;
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002135 struct kernel64_hdr *kptr = NULL;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002136 char *ptr = ((char*) data);
2137 int ret = 0;
2138 uint8_t dtb_copied = 0;
2139 unsigned int out_len = 0;
2140 unsigned int out_avai_len = 0;
2141 unsigned char *out_addr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002142 uint32_t dtb_offset = 0;
2143 unsigned char *kernel_start_addr = NULL;
2144 unsigned int kernel_size = 0;
2145 unsigned int scratch_offset = 0;
2146
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002147#if VERIFIED_BOOT
Brian Swetland9c4c0752009-01-25 16:23:50 -08002148 if(target_build_variant_user() && !device.is_unlocked)
2149 {
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002150 fastboot_fail("unlock device to use this command");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002151 return;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002152 }
2153#endif
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002154
2155 if (sz < sizeof(hdr)) {
2156 fastboot_fail("invalid bootimage header");
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002157 return;
2158 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002159
2160 hdr = (struct boot_img_hdr *)data;
2161
2162 /* ensure commandline is terminated */
2163 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2164
2165 if(target_is_emmc_boot() && hdr->page_size) {
2166 page_size = hdr->page_size;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002167 page_mask = page_size - 1;
2168 }
2169
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002170 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2171 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002172#if DEVICE_TREE
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002173 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2174#endif
2175
2176 image_actual = ADD_OF(page_size, kernel_actual);
2177 image_actual = ADD_OF(image_actual, ramdisk_actual);
2178 image_actual = ADD_OF(image_actual, dt_actual);
2179
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002180 if (target_use_signed_kernel() && (!device.is_unlocked))
2181 image_actual = ADD_OF(image_actual, sig_actual);
2182
2183 /* sz should have atleast raw boot image */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002184 if (image_actual > sz) {
Amit Blay4aa292f2015-04-28 21:55:59 +03002185 fastboot_fail("bootimage: incomplete or not signed");
2186 return;
2187 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002188
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002189 // Initialize boot state before trying to verify boot.img
2190#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002191 boot_verifier_init();
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002192 /* Handle overflow if the input image size is greater than
2193 * boot image buffer can hold
2194 */
2195 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2196 {
2197 fastboot_fail("booimage: size is greater than boot image buffer can hold");
2198 return;
2199 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002200#endif
2201
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002202 /* Verify the boot image
2203 * device & page_size are initialized in aboot_init
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002204 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002205 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002206 /* Pass size excluding signature size, otherwise we would try to
2207 * access signature beyond its length
2208 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002209 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002210 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002211#ifdef MDTP_SUPPORT
2212 else
2213 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002214 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002215 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002216
2217 if (!is_mdtp_activated) {
2218 ext_partition.partition = MDTP_PARTITION_NONE;
2219 mdtp_fwlock_verify_lock(&ext_partition);
2220 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002221 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002222
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002223 mdtp_activated(&is_mdtp_activated);
2224 if(is_mdtp_activated){
2225 dprintf(CRITICAL, "fastboot boot command is not available.\n");
2226 return;
2227 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002228#endif /* MDTP_SUPPORT */
2229
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002230#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302231#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002232 // send root of trust
2233 if(!send_rot_command((uint32_t)device.is_unlocked))
2234 ASSERT(0);
2235#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302236#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002237 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002238 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2239 * If not, continue booting.
2240 */
2241 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2242 {
2243 out_addr = (unsigned char *)target_get_scratch_address();
2244 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2245 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002246 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002247 ret = decompress((unsigned char *)(ptr + page_size),
2248 hdr->kernel_size, out_addr, out_avai_len,
2249 &dtb_offset, &out_len);
2250 if (ret)
2251 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002252 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002253 ASSERT(0);
2254 }
2255
Matthew Qin0b15b322015-05-19 05:20:54 -04002256 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002257 kptr = (struct kernel64_hdr *)out_addr;
2258 kernel_start_addr = out_addr;
2259 kernel_size = out_len;
2260 } else {
2261 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2262 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2263 kernel_size = hdr->kernel_size;
2264 }
2265
2266 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002267 * Update the kernel/ramdisk/tags address if the boot image header
2268 * has default values, these default values come from mkbootimg when
2269 * the boot image is flashed using fastboot flash:raw
2270 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002271 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002272
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002273 /* Get virtual addresses since the hdr saves physical addresses. */
2274 hdr->kernel_addr = VA(hdr->kernel_addr);
2275 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2276 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002277
Matthew Qinbb7923d2015-02-09 10:56:09 +08002278 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002279 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002280 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002281 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2282 {
2283 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002284 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002285 }
2286
Amol Jadicb524072012-08-09 16:40:18 -07002287#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002288 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002289 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002290 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002291
2292 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002293#else
2294 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2295 {
2296 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002297 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002298 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002299#endif
2300
2301 /* Load ramdisk & kernel */
2302 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002303 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002304
2305#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002306 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2307 {
2308 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2309 return;
2310 }
2311
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002312 /*
2313 * If dtb is not found look for appended DTB in the kernel.
2314 * If appended dev tree is found, update the atags with
2315 * memory address to the DTB appended location on RAM.
2316 * Else update with the atags address in the kernel header
2317 */
2318 if (!dtb_copied) {
2319 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002320 dtb = dev_tree_appended((void*)(ptr + page_size),
2321 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002322 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002323 if (!dtb) {
2324 fastboot_fail("dtb not found");
2325 return;
2326 }
Amol Jadicb524072012-08-09 16:40:18 -07002327 }
2328#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002329
2330 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002331 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002332
Dima Zavin77e41f32013-03-06 16:10:43 -08002333 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002334 (const char*) hdr->cmdline, board_machtype(),
2335 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002336}
2337
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002338void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002339{
2340 struct ptentry *ptn;
2341 struct ptable *ptable;
2342
2343 ptable = flash_get_ptable();
2344 if (ptable == NULL) {
2345 fastboot_fail("partition table doesn't exist");
2346 return;
2347 }
2348
2349 ptn = ptable_find(ptable, arg);
2350 if (ptn == NULL) {
2351 fastboot_fail("unknown partition name");
2352 return;
2353 }
2354
2355 if (flash_erase(ptn)) {
2356 fastboot_fail("failed to erase partition");
2357 return;
2358 }
2359 fastboot_okay("");
2360}
2361
Bikas Gurungd48bd242010-09-04 19:54:32 -07002362
2363void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2364{
2365 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002366 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002367 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002368 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002369
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002370#if VERIFIED_BOOT
2371 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2372 {
2373 if(!device.is_unlocked)
2374 {
2375 fastboot_fail("unlock device to erase keystore");
2376 return;
2377 }
2378 }
2379#endif
2380
Kinson Chikf1a43512011-07-14 11:28:39 -07002381 index = partition_get_index(arg);
2382 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002383 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002384
Kinson Chikf1a43512011-07-14 11:28:39 -07002385 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002386 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002387 return;
2388 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002389
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002390 lun = partition_get_lun(index);
2391 mmc_set_lun(lun);
2392
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002393 if (platform_boot_dev_isemmc())
2394 {
2395 if (mmc_erase_card(ptn, size)) {
2396 fastboot_fail("failed to erase partition\n");
2397 return;
2398 }
2399 } else {
2400 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2401 size = partition_get_size(index);
2402 if (size > DEFAULT_ERASE_SIZE)
2403 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002404
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002405 /* Simple inefficient version of erase. Just writing
2406 0 in first several blocks */
2407 if (mmc_write(ptn , size, (unsigned int *)out)) {
2408 fastboot_fail("failed to erase partition");
2409 return;
2410 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002411 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002412#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302413#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002414 if(!(strncmp(arg, "userdata", 8)))
2415 if(send_delete_keys_to_tz())
2416 ASSERT(0);
2417#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302418#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002419 fastboot_okay("");
2420}
2421
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002422void cmd_erase(const char *arg, void *data, unsigned sz)
2423{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002424#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002425 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002426 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002427 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002428 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002429 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002430 return;
2431 }
2432 }
2433#endif
2434
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002435 if(target_is_emmc_boot())
2436 cmd_erase_mmc(arg, data, sz);
2437 else
2438 cmd_erase_nand(arg, data, sz);
2439}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002440
Channagoud Kadabiad259832015-05-29 11:14:17 -07002441static uint32_t aboot_get_secret_key()
2442{
2443 /* 0 is invalid secret key, update this implementation to return
2444 * device specific unique secret key
2445 */
2446 return 0;
2447}
2448
Ajay Dudani5c761132011-04-07 20:19:04 -07002449void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002450{
2451 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002452 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002453 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002454 char *token = NULL;
2455 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002456 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002457 uint8_t lun = 0;
2458 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002459
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002460 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002461 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002462 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002463 if(token)
2464 {
2465 lun = atoi(token);
2466 mmc_set_lun(lun);
2467 lun_set = true;
2468 }
2469
Mao Jinlong226f33a2014-07-04 17:24:10 +08002470 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002471 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002472 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2473 {
2474 if (!aboot_frp_unlock(pname, data, sz))
2475 {
2476 fastboot_info("FRP unlock successful");
2477 fastboot_okay("");
2478 }
2479 else
2480 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2481
2482 return;
2483 }
2484
Mao Jinlong226f33a2014-07-04 17:24:10 +08002485 if (!strcmp(pname, "partition"))
2486 {
2487 dprintf(INFO, "Attempt to write partition image.\n");
2488 if (write_partition(sz, (unsigned char *) data)) {
2489 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002490 return;
2491 }
2492 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002493 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002494 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002495#if VERIFIED_BOOT
2496 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2497 {
2498 if(!device.is_unlocked)
2499 {
2500 fastboot_fail("unlock device to flash keystore");
2501 return;
2502 }
2503 if(!boot_verify_validate_keystore((unsigned char *)data))
2504 {
2505 fastboot_fail("image is not a keystore file");
2506 return;
2507 }
2508 }
2509#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002510 index = partition_get_index(pname);
2511 ptn = partition_get_offset(index);
2512 if(ptn == 0) {
2513 fastboot_fail("partition table doesn't exist");
2514 return;
2515 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002516
Mao Jinlong226f33a2014-07-04 17:24:10 +08002517 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2518 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2519 fastboot_fail("image is not a boot image");
2520 return;
2521 }
2522 }
2523
2524 if(!lun_set)
2525 {
2526 lun = partition_get_lun(index);
2527 mmc_set_lun(lun);
2528 }
2529
2530 size = partition_get_size(index);
2531 if (ROUND_TO_PAGE(sz,511) > size) {
2532 fastboot_fail("size too large");
2533 return;
2534 }
2535 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2536 fastboot_fail("flash write failure");
2537 return;
2538 }
Greg Grisco6e754772011-06-23 12:19:39 -07002539 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002540 }
2541 fastboot_okay("");
2542 return;
2543}
2544
Ajay Dudanide984792015-03-02 09:57:41 -08002545void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2546{
2547 int i, images;
2548 meta_header_t *meta_header;
2549 img_header_entry_t *img_header_entry;
2550
2551 meta_header = (meta_header_t*) data;
2552 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2553
2554 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2555
2556 for (i=0; i<images; i++) {
2557
2558 if((img_header_entry[i].ptn_name == NULL) ||
2559 (img_header_entry[i].start_offset == 0) ||
2560 (img_header_entry[i].size == 0))
2561 break;
2562
2563 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2564 (void *) data + img_header_entry[i].start_offset,
2565 img_header_entry[i].size);
2566 }
2567
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002568 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2569 {
2570 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2571 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2572 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2573 }
2574 else
2575 {
2576 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2577 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2578 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2579 }
2580
2581 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002582 fastboot_okay("");
2583 return;
2584}
2585
Ajay Dudani5c761132011-04-07 20:19:04 -07002586void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2587{
2588 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002589 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002590 uint32_t *fill_buf = NULL;
2591 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002592 sparse_header_t *sparse_header;
2593 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002594 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002595 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302596 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002597 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302598 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002599 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302600 /*End of the sparse image address*/
2601 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002602
Kinson Chikf1a43512011-07-14 11:28:39 -07002603 index = partition_get_index(arg);
2604 ptn = partition_get_offset(index);
2605 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002606 fastboot_fail("partition table doesn't exist");
2607 return;
2608 }
2609
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302610 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302611
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002612 lun = partition_get_lun(index);
2613 mmc_set_lun(lun);
2614
vijay kumar800255e2015-04-24 20:26:19 +05302615 if (sz < sizeof(sparse_header_t)) {
2616 fastboot_fail("size too low");
2617 return;
2618 }
2619
Ajay Dudani5c761132011-04-07 20:19:04 -07002620 /* Read and skip over sparse image header */
2621 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302622
vijay kumar1321f342015-03-27 12:13:42 +05302623 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002624 fastboot_fail("size too large");
2625 return;
2626 }
2627
vijay kumarde4fcf62015-04-23 13:05:49 +05302628 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302629
2630 if (data_end < (uint32_t)data) {
2631 fastboot_fail("buffer overreads occured due to invalid sparse header");
2632 return;
2633 }
2634
vijay kumarde4fcf62015-04-23 13:05:49 +05302635 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002636 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302637 fastboot_fail("sparse header size mismatch");
2638 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002639 }
2640
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002641 dprintf (SPEW, "=== Sparse Image Header ===\n");
2642 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2643 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2644 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2645 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2646 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2647 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2648 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2649 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002650
2651 /* Start processing chunks */
2652 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2653 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302654 /* Make sure the total image size does not exceed the partition size */
2655 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2656 fastboot_fail("size too large");
2657 return;
2658 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002659 /* Read and skip over chunk header */
2660 chunk_header = (chunk_header_t *) data;
2661 data += sizeof(chunk_header_t);
2662
vijay kumar800255e2015-04-24 20:26:19 +05302663 if (data_end < (uint32_t)data) {
2664 fastboot_fail("buffer overreads occured due to invalid sparse header");
2665 return;
2666 }
2667
Ajay Dudani5c761132011-04-07 20:19:04 -07002668 dprintf (SPEW, "=== Chunk Header ===\n");
2669 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2670 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2671 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2672
vijay kumar800255e2015-04-24 20:26:19 +05302673 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002674 {
vijay kumar800255e2015-04-24 20:26:19 +05302675 fastboot_fail("chunk header size mismatch");
2676 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002677 }
2678
wufeng.jiang813dc352015-06-02 23:02:46 -04002679 if (!sparse_header->blk_sz ){
2680 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302681 return;
2682 }
2683
wufeng.jiang813dc352015-06-02 23:02:46 -04002684 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2685
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302686 /* Make sure that the chunk size calculated from sparse image does not
2687 * exceed partition size
2688 */
2689 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2690 {
2691 fastboot_fail("Chunk data size exceeds partition size");
2692 return;
2693 }
2694
Ajay Dudani5c761132011-04-07 20:19:04 -07002695 switch (chunk_header->chunk_type)
2696 {
2697 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002698 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002699 chunk_data_sz))
2700 {
2701 fastboot_fail("Bogus chunk size for chunk type Raw");
2702 return;
2703 }
2704
vijay kumar800255e2015-04-24 20:26:19 +05302705 if (data_end < (uint32_t)data + chunk_data_sz) {
2706 fastboot_fail("buffer overreads occured due to invalid sparse header");
2707 return;
2708 }
2709
wufeng.jiang813dc352015-06-02 23:02:46 -04002710 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2711 otherwise it will return in the line above
2712 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002713 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002714 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002715 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002716 {
2717 fastboot_fail("flash write failure");
2718 return;
2719 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302720 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2721 fastboot_fail("Bogus size for RAW chunk type");
2722 return;
2723 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002724 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002725 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002726 break;
2727
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002728 case CHUNK_TYPE_FILL:
2729 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2730 sizeof(uint32_t)))
2731 {
2732 fastboot_fail("Bogus chunk size for chunk type FILL");
2733 return;
2734 }
2735
2736 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2737 if (!fill_buf)
2738 {
2739 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2740 return;
2741 }
2742
vijay kumar800255e2015-04-24 20:26:19 +05302743 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2744 fastboot_fail("buffer overreads occured due to invalid sparse header");
2745 return;
2746 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002747 fill_val = *(uint32_t *)data;
2748 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002749
2750 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2751 {
2752 fill_buf[i] = fill_val;
2753 }
2754
wufeng.jiang813dc352015-06-02 23:02:46 -04002755 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002756 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302757 /* Make sure that the data written to partition does not exceed partition size */
2758 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2759 {
2760 fastboot_fail("Chunk data size for fill type exceeds partition size");
2761 return;
2762 }
2763
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002764 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2765 sparse_header->blk_sz,
2766 fill_buf))
2767 {
2768 fastboot_fail("flash write failure");
2769 free(fill_buf);
2770 return;
2771 }
2772
2773 total_blocks++;
2774 }
2775
2776 free(fill_buf);
2777 break;
2778
Ajay Dudani5c761132011-04-07 20:19:04 -07002779 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302780 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2781 fastboot_fail("bogus size for chunk DONT CARE type");
2782 return;
2783 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002784 total_blocks += chunk_header->chunk_sz;
2785 break;
2786
Ajay Dudani5c761132011-04-07 20:19:04 -07002787 case CHUNK_TYPE_CRC:
2788 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2789 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002790 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002791 return;
2792 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302793 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2794 fastboot_fail("bogus size for chunk CRC type");
2795 return;
2796 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002797 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302798 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2799 fastboot_fail("integer overflow occured");
2800 return;
2801 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002802 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302803 if (data_end < (uint32_t)data) {
2804 fastboot_fail("buffer overreads occured due to invalid sparse header");
2805 return;
2806 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002807 break;
2808
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002809 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002810 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002811 fastboot_fail("Unknown chunk type");
2812 return;
2813 }
2814 }
2815
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002816 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2817 total_blocks, sparse_header->total_blks);
2818
2819 if(total_blocks != sparse_header->total_blks)
2820 {
2821 fastboot_fail("sparse image write failure");
2822 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002823
2824 fastboot_okay("");
2825 return;
2826}
2827
2828void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2829{
2830 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002831 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002832
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002833#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002834 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2835 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002836 int ret=0;
2837 uint32 major_version=0;
2838 uint32 minor_version=0;
2839
2840 ret = scm_svc_version(&major_version,&minor_version);
2841 if(!ret)
2842 {
2843 if(major_version >= 2)
2844 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002845 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002846 {
2847 ret = encrypt_scm((uint32 **) &data, &sz);
2848 if (ret != 0) {
2849 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2850 return;
2851 }
2852
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002853 /* Protect only for SSD */
2854 if (!strcmp(arg, "ssd")) {
2855 ret = scm_protect_keystore((uint32 *) data, sz);
2856 if (ret != 0) {
2857 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2858 return;
2859 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002860 }
2861 }
2862 else
2863 {
2864 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2865 if(ret != 0)
2866 {
2867 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2868 return;
2869 }
2870 }
2871 }
2872 else
2873 {
2874 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2875 magic_number[1] == DECRYPT_MAGIC_1)
2876 {
2877 ret = decrypt_scm((uint32 **) &data, &sz);
2878 if (ret != 0) {
2879 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2880 return;
2881 }
2882 }
2883 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2884 magic_number[1] == ENCRYPT_MAGIC_1)
2885 {
2886 ret = encrypt_scm((uint32 **) &data, &sz);
2887 if (ret != 0) {
2888 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2889 return;
2890 }
2891 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002892 }
2893 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002894 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002895 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002896 dprintf(CRITICAL,"INVALID SVC Version\n");
2897 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002898 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002899#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002900
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002901#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002902 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002903 {
lijuang511a2b52015-08-14 20:50:51 +08002904 /* if device is locked:
2905 * common partition will not allow to be flashed
2906 * critical partition will allow to flash image.
2907 */
2908 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
2909 fastboot_fail("Partition flashing is not allowed");
2910 return;
2911 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302912#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002913 /* if device critical is locked:
2914 * common partition will allow to be flashed
2915 * critical partition will not allow to flash image.
2916 */
2917 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
2918 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002919 return;
2920 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302921#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002922 }
2923#endif
2924
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002925 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002926 meta_header = (meta_header_t *) data;
2927 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002928 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002929 else if (meta_header->magic == META_HEADER_MAGIC)
2930 cmd_flash_meta_img(arg, data, sz);
2931 else
2932 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002933
2934#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05302935#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002936 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
2937 {
2938 // reset dm_verity mode to enforcing
2939 device.verity_mode = 1;
2940 write_device_info(&device);
2941 }
2942#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05302943#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002944
Ajay Dudani5c761132011-04-07 20:19:04 -07002945 return;
2946}
2947
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002948void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2949{
2950 struct ptentry *sys_ptn;
2951 struct ptable *ptable;
2952
2953 ptable = flash_get_ptable();
2954 if (ptable == NULL) {
2955 fastboot_fail("partition table doesn't exist");
2956 return;
2957 }
2958
2959 sys_ptn = ptable_find(ptable, "system");
2960 if (sys_ptn == NULL) {
2961 fastboot_fail("system partition not found");
2962 return;
2963 }
2964
2965 sz = ROUND_TO_PAGE(sz, page_mask);
2966 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2967 fastboot_fail("update_ubi_vol failed");
2968 else
2969 fastboot_okay("");
2970}
2971
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002972void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002973{
2974 struct ptentry *ptn;
2975 struct ptable *ptable;
2976 unsigned extra = 0;
2977
2978 ptable = flash_get_ptable();
2979 if (ptable == NULL) {
2980 fastboot_fail("partition table doesn't exist");
2981 return;
2982 }
2983
2984 ptn = ptable_find(ptable, arg);
2985 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002986 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2987 arg);
2988 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002989 return;
2990 }
2991
2992 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2993 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2994 fastboot_fail("image is not a boot image");
2995 return;
2996 }
2997 }
2998
Amol Jadi5c61a952012-05-04 17:05:35 -07002999 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003000 || !strcmp(ptn->name, "userdata")
3001 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003002 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003003 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003004 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003005 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003006 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003007
3008 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003009 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3010 if (flash_ubi_img(ptn, data, sz)) {
3011 fastboot_fail("flash write failure");
3012 return;
3013 }
3014 } else {
3015 if (flash_write(ptn, extra, data, sz)) {
3016 fastboot_fail("flash write failure");
3017 return;
3018 }
Dima Zavin214cc642009-01-26 11:16:21 -08003019 }
3020 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3021 fastboot_okay("");
3022}
3023
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003024void cmd_flash(const char *arg, void *data, unsigned sz)
3025{
3026 if(target_is_emmc_boot())
3027 cmd_flash_mmc(arg, data, sz);
3028 else
3029 cmd_flash_nand(arg, data, sz);
3030}
3031
Dima Zavin214cc642009-01-26 11:16:21 -08003032void cmd_continue(const char *arg, void *data, unsigned sz)
3033{
3034 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003035 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003036
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003037 if (target_is_emmc_boot())
3038 {
lijuanga40d6302015-07-20 20:10:13 +08003039#if FBCON_DISPLAY_MSG
3040 keys_detect_init();
3041#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003042 boot_linux_from_mmc();
3043 }
3044 else
3045 {
3046 boot_linux_from_flash();
3047 }
Dima Zavin214cc642009-01-26 11:16:21 -08003048}
3049
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003050void cmd_reboot(const char *arg, void *data, unsigned sz)
3051{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003052 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003053 fastboot_okay("");
3054 reboot_device(0);
3055}
3056
3057void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3058{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003059 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003060 fastboot_okay("");
3061 reboot_device(FASTBOOT_MODE);
3062}
3063
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003064void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3065{
3066 dprintf(INFO, "Enabling charger screen check\n");
3067 device.charger_screen_enabled = 1;
3068 write_device_info(&device);
3069 fastboot_okay("");
3070}
3071
3072void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3073{
3074 dprintf(INFO, "Disabling charger screen check\n");
3075 device.charger_screen_enabled = 0;
3076 write_device_info(&device);
3077 fastboot_okay("");
3078}
3079
lijuanga25d8bb2015-09-16 13:11:52 +08003080void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3081{
3082 char *p = NULL;
3083 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303084 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003085
3086 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303087 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003088 if (p) {
3089 if (!strncmp(p, "0", 1)) {
3090 device.charger_screen_enabled = 0;
3091 } else if (!strncmp(p, "1", 1)) {
3092 device.charger_screen_enabled = 1;
3093 }
3094 }
3095 }
3096
3097 /* update charger_screen_enabled value for getvar
3098 * command
3099 */
3100 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3101 device.charger_screen_enabled);
3102
3103 write_device_info(&device);
3104 fastboot_okay("");
3105}
3106
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303107void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3108{
3109 dprintf(INFO, "Selecting display panel %s\n", arg);
3110 if (arg)
3111 strlcpy(device.display_panel, arg,
3112 sizeof(device.display_panel));
3113 write_device_info(&device);
3114 fastboot_okay("");
3115}
3116
Shashank Mittal162244e2011-08-08 19:01:25 -07003117void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3118{
lijuang4ece1e72015-08-14 21:02:36 +08003119 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303120}
3121
3122void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3123{
lijuang4ece1e72015-08-14 21:02:36 +08003124 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303125 if(!is_allow_unlock) {
3126 fastboot_fail("oem unlock is not allowed");
3127 return;
3128 }
3129
lijuang4ece1e72015-08-14 21:02:36 +08003130 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303131
lijuang4ece1e72015-08-14 21:02:36 +08003132 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303133 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003134
vijay kumarc65876c2015-04-24 13:29:16 +05303135 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3136 write_misc(0, &msg, sizeof(msg));
3137
3138 fastboot_okay("");
3139 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003140 }
3141 fastboot_okay("");
3142}
3143
Channagoud Kadabiad259832015-05-29 11:14:17 -07003144static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3145{
3146 int ret = 1;
3147 uint32_t secret_key;
3148 char seckey_buffer[MAX_RSP_SIZE];
3149
3150 secret_key = aboot_get_secret_key();
3151 if (secret_key)
3152 {
3153 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3154 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3155 {
3156 is_allow_unlock = true;
3157 write_allow_oem_unlock(is_allow_unlock);
3158 ret = 0;
3159 }
3160 }
3161 return ret;
3162}
3163
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003164void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3165{
lijuang4ece1e72015-08-14 21:02:36 +08003166 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003167}
3168
Shashank Mittala0032282011-08-26 14:50:11 -07003169void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3170{
lijuang511a2b52015-08-14 20:50:51 +08003171 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003172 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003173 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003174 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3175 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303176#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003177 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3178 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303179#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003180 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003181 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303182 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3183 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003184 fastboot_okay("");
3185}
3186
lijuang511a2b52015-08-14 20:50:51 +08003187void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3188{
3189 char response[MAX_RSP_SIZE];
3190 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3191 fastboot_info(response);
3192 fastboot_okay("");
3193}
3194
3195void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3196{
lijuang4ece1e72015-08-14 21:02:36 +08003197 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003198}
3199
3200void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3201{
lijuang4ece1e72015-08-14 21:02:36 +08003202 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003203}
3204
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003205void cmd_preflash(const char *arg, void *data, unsigned sz)
3206{
3207 fastboot_okay("");
3208}
3209
Mao Flynn7b379f32015-04-20 00:28:30 +08003210static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303211
Mao Flynn7b379f32015-04-20 00:28:30 +08003212int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303213{
Mao Flynn7b379f32015-04-20 00:28:30 +08003214 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303215 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003216 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303217 return -1;
3218 return 0;
3219}
3220
Mao Flynn7b379f32015-04-20 00:28:30 +08003221int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003222{
3223 struct ptentry *ptn;
3224 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003225 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003226 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003227
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303228 ptable = flash_get_ptable();
3229 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003230 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3231 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303232 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003233
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303234 ptn = ptable_find(ptable, "splash");
3235 if (ptn == NULL) {
3236 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003237 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303238 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003239 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303240 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003241 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303242 }
3243
Mao Flynn7b379f32015-04-20 00:28:30 +08003244 header = (struct logo_img_header *)logo_header;
3245 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303246 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003247 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303248 }
3249
3250 fb_display = fbcon_display();
3251 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003252 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3253 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3254
3255 /* if the logo is full-screen size, remove "fbcon_clear()" */
3256 if ((header->width != fb_display->width)
3257 || (header->height != fb_display->height))
3258 fbcon_clear();
3259
3260 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3261 (uint32_t *)base,
3262 (header->blocks * 512))) {
3263 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3264 return -1;
3265 }
3266 fbcon_extract_to_screen(header, base);
3267 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003268 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003269
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003270 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3271 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003272 return -1;
3273 }
3274
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303275 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003276 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3277 (uint32_t *)base,
3278 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303279 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303280 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003281 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003282 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303283 }
3284
Mao Flynn7b379f32015-04-20 00:28:30 +08003285 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303286}
3287
Mao Flynn7b379f32015-04-20 00:28:30 +08003288int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303289{
3290 int index = INVALID_PTN;
3291 unsigned long long ptn = 0;
3292 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003293 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003294 uint32_t blocksize, realsize, readsize;
3295 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303296
3297 index = partition_get_index("splash");
3298 if (index == 0) {
3299 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003300 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303301 }
3302
3303 ptn = partition_get_offset(index);
3304 if (ptn == 0) {
3305 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003306 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303307 }
3308
Mao Flynn1893a7f2015-06-03 12:03:36 +08003309 mmc_set_lun(partition_get_lun(index));
3310
3311 blocksize = mmc_get_device_blocksize();
3312 if (blocksize == 0) {
3313 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3314 return -1;
3315 }
3316
3317 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003318 if (!fb_display)
3319 {
3320 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3321 return -1;
3322 }
3323
Mao Flynn1893a7f2015-06-03 12:03:36 +08003324 base = (uint8_t *) fb_display->base;
3325
3326 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303327 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003328 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303329 }
3330
Mao Flynn1893a7f2015-06-03 12:03:36 +08003331 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003332 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303333 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003334 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303335 }
3336
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303337 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003338 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3339 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003340
Mao Flynn1893a7f2015-06-03 12:03:36 +08003341 realsize = header->blocks * 512;
3342 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3343
3344 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003345 if ((header->width != fb_display->width)
3346 || (header->height != fb_display->height))
3347 fbcon_clear();
3348
Mao Flynn1893a7f2015-06-03 12:03:36 +08003349 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003350 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3351 return -1;
3352 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003353
Mao Flynn1893a7f2015-06-03 12:03:36 +08003354 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3355 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303356
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003357 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3358 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003359 return -1;
3360 }
3361
3362 realsize = header->width * header->height * fb_display->bpp / 8;
3363 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3364
3365 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3366 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3367 fbcon_clear();
3368 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3369 return -1;
3370 }
3371 } else {
3372 if (mmc_read(ptn + blocksize ,
3373 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3374 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3375 return -1;
3376 }
3377 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3378 }
3379 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303380 }
3381
Mao Flynn7b379f32015-04-20 00:28:30 +08003382 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303383}
3384
Mao Flynn7b379f32015-04-20 00:28:30 +08003385int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303386{
3387 if (target_is_emmc_boot()) {
3388 return splash_screen_mmc();
3389 } else {
3390 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003391 }
3392}
3393
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003394/* Get the size from partiton name */
3395static void get_partition_size(const char *arg, char *response)
3396{
3397 uint64_t ptn = 0;
3398 uint64_t size;
3399 int index = INVALID_PTN;
3400
3401 index = partition_get_index(arg);
3402
3403 if (index == INVALID_PTN)
3404 {
3405 dprintf(CRITICAL, "Invalid partition index\n");
3406 return;
3407 }
3408
3409 ptn = partition_get_offset(index);
3410
3411 if(!ptn)
3412 {
3413 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3414 return;
3415 }
3416
3417 size = partition_get_size(index);
3418
3419 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3420 return;
3421}
3422
3423/*
3424 * Publish the partition type & size info
3425 * fastboot getvar will publish the required information.
3426 * fastboot getvar partition_size:<partition_name>: partition size in hex
3427 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3428 */
3429static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3430{
3431 uint8_t i;
3432
3433 for (i = 0; i < num_parts; i++) {
3434 get_partition_size(info[i].part_name, info[i].size_response);
3435
3436 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3437 {
3438 dprintf(CRITICAL, "partition size name truncated\n");
3439 return;
3440 }
3441 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3442 {
3443 dprintf(CRITICAL, "partition type name truncated\n");
3444 return;
3445 }
3446
3447 /* publish partition size & type info */
3448 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3449 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3450 }
3451}
3452
lijuang4ece1e72015-08-14 21:02:36 +08003453void get_product_name(unsigned char *buf)
3454{
3455 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3456 return;
3457}
3458
3459void get_bootloader_version(unsigned char *buf)
3460{
3461 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3462 return;
3463}
3464
3465void get_baseband_version(unsigned char *buf)
3466{
3467 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3468 return;
3469}
3470
3471bool is_device_locked()
3472{
3473 return device.is_unlocked ? false:true;
3474}
3475
Amol Jadi5edf3552013-07-23 14:15:34 -07003476/* register commands and variables for fastboot */
3477void aboot_fastboot_register_commands(void)
3478{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003479 int i;
lijuangf16461c2015-08-03 17:09:34 +08003480 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003481
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003482 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003483 /* By default the enabled list is empty. */
3484 {"", NULL},
3485 /* move commands enclosed within the below ifndef to here
3486 * if they need to be enabled in user build.
3487 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003488#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003489 /* Register the following commands only for non-user builds */
3490 {"flash:", cmd_flash},
3491 {"erase:", cmd_erase},
3492 {"boot", cmd_boot},
3493 {"continue", cmd_continue},
3494 {"reboot", cmd_reboot},
3495 {"reboot-bootloader", cmd_reboot_bootloader},
3496 {"oem unlock", cmd_oem_unlock},
3497 {"oem unlock-go", cmd_oem_unlock_go},
3498 {"oem lock", cmd_oem_lock},
3499 {"flashing unlock", cmd_oem_unlock},
3500 {"flashing lock", cmd_oem_lock},
3501 {"flashing lock_critical", cmd_flashing_lock_critical},
3502 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3503 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3504 {"oem device-info", cmd_oem_devinfo},
3505 {"preflash", cmd_preflash},
3506 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3507 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003508 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003509 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003510#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003511 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003512#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003513#endif
lijuang511a2b52015-08-14 20:50:51 +08003514 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003515
3516 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3517 for (i = 1; i < fastboot_cmds_count; i++)
3518 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3519
Amol Jadi5edf3552013-07-23 14:15:34 -07003520 /* publish variables and their values */
3521 fastboot_publish("product", TARGET(BOARD));
3522 fastboot_publish("kernel", "lk");
3523 fastboot_publish("serialno", sn_buf);
3524
3525 /*
3526 * partition info is supported only for emmc partitions
3527 * Calling this for NAND prints some error messages which
3528 * is harmless but misleading. Avoid calling this for NAND
3529 * devices.
3530 */
3531 if (target_is_emmc_boot())
3532 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3533
3534 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003535 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3536 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003537 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003538 /* Is the charger screen check enabled */
3539 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3540 device.charger_screen_enabled);
3541 fastboot_publish("charger-screen-enabled",
3542 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003543 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303544 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3545 device.display_panel);
3546 fastboot_publish("display-panel",
3547 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003548 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3549 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003550 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3551 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3552 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3553 target_is_emmc_boot()? "eMMC":"UFS");
3554 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003555#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003556 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003557 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003558 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003559#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003560}
3561
Brian Swetland9c4c0752009-01-25 16:23:50 -08003562void aboot_init(const struct app_descriptor *app)
3563{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003564 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003565
lijuang39831732016-01-08 17:49:02 +08003566 /* Initialise wdog to catch early lk crashes */
3567#if WDOG_SUPPORT
3568 msm_wdog_init();
3569#endif
3570
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003571 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003572 if (target_is_emmc_boot())
3573 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003574 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003575 page_mask = page_size - 1;
3576 }
3577 else
3578 {
3579 page_size = flash_page_size();
3580 page_mask = page_size - 1;
3581 }
3582
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003583 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3584
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003585 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303586 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003587
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003588 /* Display splash screen if enabled */
3589#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003590#if NO_ALARM_DISPLAY
3591 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003592#endif
lijuang99c02d82015-02-13 19:04:34 +08003593 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003594#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003595 /* Wait if the display shutdown is in progress */
3596 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003597 if (!pm_appsbl_display_init_done())
3598 target_display_init(device.display_panel);
3599 else
3600 display_image_on_screen();
3601#else
lijuang99c02d82015-02-13 19:04:34 +08003602 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003603#endif
lijuang99c02d82015-02-13 19:04:34 +08003604 dprintf(SPEW, "Display Init: Done\n");
3605#if NO_ALARM_DISPLAY
3606 }
3607#endif
3608#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003609
Greg Griscod6250552011-06-29 14:40:23 -07003610 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003611 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003612
Dhaval Patel223ec952013-07-18 14:49:44 -07003613 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3614
Matthew Qindefd5562014-07-11 18:02:40 +08003615 /*
3616 * Check power off reason if user force reset,
3617 * if yes phone will do normal boot.
3618 */
3619 if (is_user_force_reset())
3620 goto normal_boot;
3621
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003622 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003623 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003624 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003625 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08003626 reboot_device(EMERGENCY_DLOAD);
3627 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
3628
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003629 boot_into_fastboot = true;
3630 }
3631 if (!boot_into_fastboot)
3632 {
3633 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3634 boot_into_recovery = 1;
3635 if (!boot_into_recovery &&
3636 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003637 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003638 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003639 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003640 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003641 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003642 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003643
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003644#if USE_PON_REBOOT_REG
3645 reboot_mode = check_hard_reboot_mode();
3646#else
Ajay Dudani77421292010-10-27 19:34:06 -07003647 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003648#endif
3649 if (reboot_mode == RECOVERY_MODE)
3650 {
Ajay Dudani77421292010-10-27 19:34:06 -07003651 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003652 }
3653 else if(reboot_mode == FASTBOOT_MODE)
3654 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003655 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003656 }
3657 else if(reboot_mode == ALARM_BOOT)
3658 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003659 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003660 }
Parth Dixit207573a2015-10-27 17:26:21 +05303661#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303662#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003663 else if (reboot_mode == DM_VERITY_ENFORCING)
3664 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003665 device.verity_mode = 1;
3666 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003667 }
3668 else if (reboot_mode == DM_VERITY_LOGGING)
3669 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003670 device.verity_mode = 0;
3671 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003672 }
3673 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3674 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003675 if(send_delete_keys_to_tz())
3676 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003677 }
Parth Dixit207573a2015-10-27 17:26:21 +05303678#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303679#endif
Ajay Dudani77421292010-10-27 19:34:06 -07003680
Matthew Qindefd5562014-07-11 18:02:40 +08003681normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003682 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003683 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003684 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003685 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003686 if(emmc_recovery_init())
3687 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3688 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003689 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003690 if((device.is_unlocked) || (device.is_tampered))
3691 {
3692 #ifdef TZ_TAMPER_FUSE
3693 set_tamper_fuse_cmd();
3694 #endif
3695 #if USE_PCOM_SECBOOT
3696 set_tamper_flag(device.is_tampered);
3697 #endif
3698 }
Shashank Mittala0032282011-08-26 14:50:11 -07003699 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003700
Pavel Nedev5d91d412013-04-29 11:34:24 +03003701 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003702 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003703 else
3704 {
3705 recovery_init();
3706 #if USE_PCOM_SECBOOT
3707 if((device.is_unlocked) || (device.is_tampered))
3708 set_tamper_flag(device.is_tampered);
3709 #endif
3710 boot_linux_from_flash();
3711 }
3712 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3713 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003714 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003715
Amol Jadi5edf3552013-07-23 14:15:34 -07003716 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003717
Amol Jadi5edf3552013-07-23 14:15:34 -07003718 /* register aboot specific fastboot commands */
3719 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003720
Amol Jadi5edf3552013-07-23 14:15:34 -07003721 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003722 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003723
3724 /* initialize and start fastboot */
3725 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003726#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08003727 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08003728#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003729}
3730
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003731uint32_t get_page_size()
3732{
3733 return page_size;
3734}
3735
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003736/*
3737 * Calculated and save hash (SHA256) for non-signed boot image.
3738 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003739 * @param image_addr - Boot image address
3740 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003741 *
3742 * @return int - 0 on success, negative value on failure.
3743 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003744static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003745{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003746 unsigned int digest[8];
3747#if IMAGE_VERIF_ALGO_SHA1
3748 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3749#else
3750 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3751#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003752
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003753 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003754 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003755
3756 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003757 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003758
3759 return 0;
3760}
3761
Brian Swetland9c4c0752009-01-25 16:23:50 -08003762APP_START(aboot)
3763 .init = aboot_init,
3764APP_END