blob: beb8181113a9ad5a59952048f19590b057d7547d [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08005 * Copyright (c) 2009-2015, 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
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +0530776 /* Initialise wdog to catch early kernel crashes */
777#if WDOG_SUPPORT
778 msm_wdog_init();
779#endif
Amol Jadi4421e652011-06-16 15:00:48 -0700780 /* do any platform specific cleanup before kernel entry */
781 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700782
Brian Swetland9c4c0752009-01-25 16:23:50 -0800783 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700784
Amol Jadi504f9fe2012-08-16 13:56:48 -0700785#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800786 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700787#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700788 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800789
790 if (IS_ARM64(kptr))
791 /* Jump to a 64bit kernel */
792 scm_elexec_call((paddr_t)kernel, tags_phys);
793 else
794 /* Jump to a 32bit kernel */
795 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800796}
797
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700798/* Function to check if the memory address range falls within the aboot
799 * boundaries.
800 * start: Start of the memory region
801 * size: Size of the memory region
802 */
803int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
804{
805 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800806 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700807 return -1;
808
809 /* Check for memory overlap. */
810 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
811 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700812 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700813 return 0;
814 else
815 return -1;
816}
817
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800818#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800819
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800820BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800821#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800822BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800823#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800824
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700825static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
826{
827 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800828
829#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800830#if IMAGE_VERIF_ALGO_SHA1
831 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
832#else
833 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
834#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800835#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700836
837 /* Assume device is rooted at this time. */
838 device.is_tampered = 1;
839
840 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
841
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700842#if VERIFIED_BOOT
843 if(boot_into_recovery)
844 {
845 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530846 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700847 }
848 else
849 {
850 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530851 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700852 }
853 boot_verify_print_state();
854#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700855 ret = image_verify((unsigned char *)bootimg_addr,
856 (unsigned char *)(bootimg_addr + bootimg_size),
857 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800858 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700859#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700860 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
861
862 if (ret)
863 {
864 /* Authorized kernel */
865 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700866 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700867 }
868
Amit Blay4aa292f2015-04-28 21:55:59 +0300869#ifdef MDTP_SUPPORT
870 {
871 /* Verify MDTP lock.
872 * For boot & recovery partitions, use aboot's verification result.
873 */
874 mdtp_ext_partition_verification_t ext_partition;
875 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
876 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
877 ext_partition.page_size = 0; /* Not needed since already validated */
878 ext_partition.image_addr = 0; /* Not needed since already validated */
879 ext_partition.image_size = 0; /* Not needed since already validated */
880 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
881 mdtp_fwlock_verify_lock(&ext_partition);
882 }
883#endif /* MDTP_SUPPORT */
884
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700885#if USE_PCOM_SECBOOT
886 set_tamper_flag(device.is_tampered);
887#endif
888
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700889#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700890 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700891 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700892 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800893#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +0800894 display_bootverify_menu_thread(DISPLAY_MENU_RED);
lijuanga40d6302015-07-20 20:10:13 +0800895 wait_for_users_action();
896#else
897 dprintf(CRITICAL,
898 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
899 mdelay(5000);
900#endif
901
902 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700903 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800904#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +0800905 display_bootverify_menu_thread(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800906 wait_for_users_action();
907#else
908 dprintf(CRITICAL,
909 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
910 mdelay(5000);
911#endif
912 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700913 default:
lijuanga40d6302015-07-20 20:10:13 +0800914 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700915 }
916#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700917#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530918 if(device.is_tampered)
919 {
920 write_device_info_mmc(&device);
921 #ifdef TZ_TAMPER_FUSE
922 set_tamper_fuse_cmd();
923 #endif
924 #ifdef ASSERT_ON_TAMPER
925 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
926 ASSERT(0);
927 #endif
928 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700929#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700930}
931
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530932static bool check_format_bit()
933{
934 bool ret = false;
935 int index;
936 uint64_t offset;
937 struct boot_selection_info *in = NULL;
938 char *buf = NULL;
939
940 index = partition_get_index("bootselect");
941 if (index == INVALID_PTN)
942 {
943 dprintf(INFO, "Unable to locate /bootselect partition\n");
944 return ret;
945 }
946 offset = partition_get_offset(index);
947 if(!offset)
948 {
949 dprintf(INFO, "partition /bootselect doesn't exist\n");
950 return ret;
951 }
952 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
953 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530954 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530955 {
956 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
957 free(buf);
958 return ret;
959 }
960 in = (struct boot_selection_info *) buf;
961 if ((in->signature == BOOTSELECT_SIGNATURE) &&
962 (in->version == BOOTSELECT_VERSION)) {
963 if ((in->state_info & BOOTSELECT_FORMAT) &&
964 !(in->state_info & BOOTSELECT_FACTORY))
965 ret = true;
966 } else {
967 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
968 in->signature, in->version);
969 ASSERT(0);
970 }
971 free(buf);
972 return ret;
973}
974
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700975void boot_verifier_init()
976{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700977 uint32_t boot_state;
978 /* Check if device unlock */
979 if(device.is_unlocked)
980 {
981 boot_verify_send_event(DEV_UNLOCK);
982 boot_verify_print_state();
983 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
984 return;
985 }
986 else
987 {
988 boot_verify_send_event(BOOT_INIT);
989 }
990
991 /* Initialize keystore */
992 boot_state = boot_verify_keystore_init();
993 if(boot_state == YELLOW)
994 {
995 boot_verify_print_state();
996 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
997 }
998}
999
Shashank Mittal23b8f422010-04-16 19:27:21 -07001000int boot_linux_from_mmc(void)
1001{
1002 struct boot_img_hdr *hdr = (void*) buf;
1003 struct boot_img_hdr *uhdr;
1004 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001005 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001006 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001007 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001008
Shashank Mittalcd98d472011-08-02 14:29:24 -07001009 unsigned char *image_addr = 0;
1010 unsigned kernel_actual;
1011 unsigned ramdisk_actual;
1012 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001013 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001014
Matthew Qin271927e2015-03-31 22:07:07 -04001015 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001016 unsigned int out_len = 0;
1017 unsigned int out_avai_len = 0;
1018 unsigned char *out_addr = NULL;
1019 uint32_t dtb_offset = 0;
1020 unsigned char *kernel_start_addr = NULL;
1021 unsigned int kernel_size = 0;
1022 int rc;
1023
Neeti Desai465491e2012-07-31 12:53:35 -07001024#if DEVICE_TREE
1025 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001026 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001027 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001028 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001029 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001030 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001031#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001032 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001033
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301034 if (check_format_bit())
1035 boot_into_recovery = 1;
1036
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001037 if (!boot_into_recovery) {
1038 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1039 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1040 if (rcode <= 0) {
1041 boot_into_ffbm = false;
1042 if (rcode < 0)
1043 dprintf(CRITICAL,"failed to get ffbm cookie");
1044 } else
1045 boot_into_ffbm = true;
1046 } else
1047 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001048 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1049 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1050 dprintf(INFO, "Unified boot method!\n");
1051 hdr = uhdr;
1052 goto unified_boot;
1053 }
Greg Griscod6250552011-06-29 14:40:23 -07001054 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001055 index = partition_get_index("boot");
1056 ptn = partition_get_offset(index);
1057 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001058 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1059 return -1;
1060 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001061 }
1062 else {
1063 index = partition_get_index("recovery");
1064 ptn = partition_get_offset(index);
1065 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001066 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1067 return -1;
1068 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001069 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001070 /* Set Lun for boot & recovery partitions */
1071 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001072
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301073 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001074 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1075 return -1;
1076 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001077
1078 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001079 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001080 return -1;
1081 }
1082
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001083 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301084
1085 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1086 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1087 return -1;
1088 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001089 page_size = hdr->page_size;
1090 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001091 }
1092
vijay kumar287bb542015-09-29 13:01:52 +05301093 /* ensure commandline is terminated */
1094 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1095
Matthew Qin49e51fa2015-02-09 10:40:45 +08001096 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1097 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001098
Matthew Qin49e51fa2015-02-09 10:40:45 +08001099 image_addr = (unsigned char *)target_get_scratch_address();
1100
1101#if DEVICE_TREE
1102 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1103 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1104#else
1105 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1106#endif
1107
1108#if VERIFIED_BOOT
1109 boot_verifier_init();
1110#endif
1111
1112 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
1113 {
1114 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1115 return -1;
1116 }
1117
Matthew Qinbb7923d2015-02-09 10:56:09 +08001118 /*
1119 * Update loading flow of bootimage to support compressed/uncompressed
1120 * bootimage on both 64bit and 32bit platform.
1121 * 1. Load bootimage from emmc partition onto DDR.
1122 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1123 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1124 * platform accordingly.
1125 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1126 */
1127
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001128 dprintf(INFO, "Loading (%s) image (%d): start\n",
1129 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001130 bs_set_timestamp(BS_KERNEL_LOAD_START);
1131
Matthew Qinbb7923d2015-02-09 10:56:09 +08001132 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001133 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1134 {
1135 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1136 return -1;
1137 }
1138
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001139 dprintf(INFO, "Loading (%s) image (%d): done\n",
1140 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1141
Matthew Qin49e51fa2015-02-09 10:40:45 +08001142 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1143
1144 /* Authenticate Kernel */
1145 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1146 (int) target_use_signed_kernel(),
1147 device.is_unlocked,
1148 device.is_tampered);
1149
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001150 /* Change the condition a little bit to include the test framework support.
1151 * We would never reach this point if device is in fastboot mode, even if we did
1152 * that means we are in test mode, so execute kernel authentication part for the
1153 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301154 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001155 {
1156 offset = imagesize_actual;
1157 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1158 {
1159 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1160 return -1;
1161 }
1162
1163 /* Read signature */
1164 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1165 {
1166 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1167 return -1;
1168 }
1169
1170 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001171 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301172 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001173 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001174 } else {
1175 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1176 #ifdef TZ_SAVE_KERNEL_HASH
1177 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1178 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001179
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001180#if VERIFIED_BOOT
1181 if(boot_verify_get_state() == ORANGE)
1182 {
lijuanga40d6302015-07-20 20:10:13 +08001183#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +08001184 display_bootverify_menu_thread(DISPLAY_MENU_ORANGE);
lijuanga40d6302015-07-20 20:10:13 +08001185 wait_for_users_action();
1186#else
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001187 dprintf(CRITICAL,
lijuanga40d6302015-07-20 20:10:13 +08001188 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001189 mdelay(5000);
lijuanga40d6302015-07-20 20:10:13 +08001190#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001191 }
1192#endif
1193
Amit Blay4aa292f2015-04-28 21:55:59 +03001194#ifdef MDTP_SUPPORT
1195 {
1196 /* Verify MDTP lock.
1197 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1198 * since verification was skipped in aboot. The signature is not part of the loaded image.
1199 */
1200 mdtp_ext_partition_verification_t ext_partition;
1201 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1202 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1203 ext_partition.page_size = page_size;
1204 ext_partition.image_addr = (uint32)image_addr;
1205 ext_partition.image_size = imagesize_actual;
1206 ext_partition.sig_avail = FALSE;
1207 mdtp_fwlock_verify_lock(&ext_partition);
1208 }
1209#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001210 }
1211
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001212#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05301213#if !VBOOT_MOTA
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001214 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001215 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001216 ASSERT(0);
1217#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05301218#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001219 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001220 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1221 * If not, continue booting.
1222 */
1223 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1224 {
1225 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1226 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001227 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001228 rc = decompress((unsigned char *)(image_addr + page_size),
1229 hdr->kernel_size, out_addr, out_avai_len,
1230 &dtb_offset, &out_len);
1231 if (rc)
1232 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001233 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001234 ASSERT(0);
1235 }
1236
Matthew Qin0b15b322015-05-19 05:20:54 -04001237 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001238 kptr = (struct kernel64_hdr *)out_addr;
1239 kernel_start_addr = out_addr;
1240 kernel_size = out_len;
1241 } else {
1242 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1243 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1244 kernel_size = hdr->kernel_size;
1245 }
1246
1247 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001248 * Update the kernel/ramdisk/tags address if the boot image header
1249 * has default values, these default values come from mkbootimg when
1250 * the boot image is flashed using fastboot flash:raw
1251 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001252 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001253
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001254 /* Get virtual addresses since the hdr saves physical addresses. */
1255 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1256 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1257 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001258
Matthew Qinbb7923d2015-02-09 10:56:09 +08001259 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001260 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001261 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001262 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1263 {
1264 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1265 return -1;
1266 }
1267
1268#ifndef DEVICE_TREE
1269 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1270 {
1271 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1272 return -1;
1273 }
1274#endif
1275
Matthew Qin49e51fa2015-02-09 10:40:45 +08001276 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001277 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001278 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001279
Matthew Qin49e51fa2015-02-09 10:40:45 +08001280 #if DEVICE_TREE
1281 if(hdr->dt_size) {
1282 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1283 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001284
Matthew Qin49e51fa2015-02-09 10:40:45 +08001285 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1286 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1287 return -1;
1288 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001289
Matthew Qin49e51fa2015-02-09 10:40:45 +08001290 /* Find index of device tree within device tree table */
1291 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1292 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1293 return -1;
1294 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001295
Matthew Qin271927e2015-03-31 22:07:07 -04001296 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1297 {
1298 unsigned int compressed_size = 0;
1299 out_addr += out_len;
1300 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001301 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001302 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1303 dt_entry.size, out_addr, out_avai_len,
1304 &compressed_size, &dtb_size);
1305 if (rc)
1306 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001307 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001308 ASSERT(0);
1309 }
1310
Matthew Qin0b15b322015-05-19 05:20:54 -04001311 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001312 best_match_dt_addr = out_addr;
1313 } else {
1314 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1315 dtb_size = dt_entry.size;
1316 }
1317
Matthew Qin49e51fa2015-02-09 10:40:45 +08001318 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001319 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001320 {
1321 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1322 return -1;
1323 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001324
Matthew Qin271927e2015-03-31 22:07:07 -04001325 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001326 } else {
1327 /* Validate the tags_addr */
1328 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001329 {
1330 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1331 return -1;
1332 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001333 /*
1334 * If appended dev tree is found, update the atags with
1335 * memory address to the DTB appended location on RAM.
1336 * Else update with the atags address in the kernel header
1337 */
1338 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001339 dtb = dev_tree_appended((void*)(image_addr + page_size),
1340 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001341 (void *)hdr->tags_addr);
1342 if (!dtb) {
1343 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001344 return -1;
1345 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001346 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001347 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001348
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001349 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1350 target_load_ssd_keystore();
1351
Shashank Mittal23b8f422010-04-16 19:27:21 -07001352unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001353
Dima Zavin77e41f32013-03-06 16:10:43 -08001354 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001355 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001356 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1357
1358 return 0;
1359}
1360
Dima Zavin214cc642009-01-26 11:16:21 -08001361int boot_linux_from_flash(void)
1362{
1363 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001364 struct ptentry *ptn;
1365 struct ptable *ptable;
1366 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001367
Shashank Mittalcd98d472011-08-02 14:29:24 -07001368 unsigned char *image_addr = 0;
1369 unsigned kernel_actual;
1370 unsigned ramdisk_actual;
1371 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001372 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001373
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001374#if DEVICE_TREE
1375 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001376 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001377 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001378 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001379#endif
1380
David Ng183a7422009-12-07 14:55:21 -08001381 if (target_is_emmc_boot()) {
1382 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1383 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1384 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1385 return -1;
1386 }
1387 goto continue_boot;
1388 }
1389
Dima Zavin214cc642009-01-26 11:16:21 -08001390 ptable = flash_get_ptable();
1391 if (ptable == NULL) {
1392 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1393 return -1;
1394 }
1395
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001396 if(!boot_into_recovery)
1397 {
1398 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001399
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001400 if (ptn == NULL) {
1401 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1402 return -1;
1403 }
1404 }
1405 else
1406 {
1407 ptn = ptable_find(ptable, "recovery");
1408 if (ptn == NULL) {
1409 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1410 return -1;
1411 }
Dima Zavin214cc642009-01-26 11:16:21 -08001412 }
1413
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001414 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001415 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1416 return -1;
1417 }
Dima Zavin214cc642009-01-26 11:16:21 -08001418
1419 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001420 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001421 return -1;
1422 }
1423
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001424 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001425 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 -08001426 return -1;
1427 }
1428
vijay kumar287bb542015-09-29 13:01:52 +05301429 /* ensure commandline is terminated */
1430 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1431
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001432 /*
1433 * Update the kernel/ramdisk/tags address if the boot image header
1434 * has default values, these default values come from mkbootimg when
1435 * the boot image is flashed using fastboot flash:raw
1436 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001437 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001438
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001439 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001440 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1441 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1442 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1443
1444 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1445 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1446
1447 /* Check if the addresses in the header are valid. */
1448 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1449 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1450 {
1451 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1452 return -1;
1453 }
1454
1455#ifndef DEVICE_TREE
1456 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1457 {
1458 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1459 return -1;
1460 }
1461#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001462
Shashank Mittalcd98d472011-08-02 14:29:24 -07001463 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001464 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001465 {
1466 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001467 offset = 0;
1468
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001469#if DEVICE_TREE
1470 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1471 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001472
1473 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1474 {
1475 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1476 return -1;
1477 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001478#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001479 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001480#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001481
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001482 dprintf(INFO, "Loading (%s) image (%d): start\n",
1483 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001484 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001485
Shashank Mittalcd98d472011-08-02 14:29:24 -07001486 /* Read image without signature */
1487 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1488 {
1489 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1490 return -1;
1491 }
Dima Zavin214cc642009-01-26 11:16:21 -08001492
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001493 dprintf(INFO, "Loading (%s) image (%d): done\n",
1494 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001495 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001496
Shashank Mittalcd98d472011-08-02 14:29:24 -07001497 offset = imagesize_actual;
1498 /* Read signature */
1499 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1500 {
1501 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001502 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001503 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001504
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301505 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001506
1507 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001508 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1509 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001510#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001511 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001512 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001513 {
1514 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1515 return -1;
1516 }
1517
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001518 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1519#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001520
1521 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001522 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001523 {
1524 write_device_info_flash(&device);
1525 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301526#if USE_PCOM_SECBOOT
1527 set_tamper_flag(device.is_tampered);
1528#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001529 }
1530 else
1531 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001532 offset = page_size;
1533
Amol Jadib6be5c12012-11-14 13:39:51 -08001534 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1535 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1536 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1537
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001538 dprintf(INFO, "Loading (%s) image (%d): start\n",
1539 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1540
Amol Jadi492d5a52013-03-15 16:12:34 -07001541 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001542
1543 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001544 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1545 return -1;
1546 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001547 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001548
Amol Jadib6be5c12012-11-14 13:39:51 -08001549 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001550 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1551 return -1;
1552 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001553 offset += ramdisk_actual;
1554
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001555 dprintf(INFO, "Loading (%s) image (%d): done\n",
1556 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1557
Amol Jadi492d5a52013-03-15 16:12:34 -07001558 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001559
1560 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001561 offset += second_actual;
1562 /* Second image loading not implemented. */
1563 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001564 }
1565
1566#if DEVICE_TREE
1567 if(hdr->dt_size != 0) {
1568
1569 /* Read the device tree table into buffer */
1570 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1571 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1572 return -1;
1573 }
1574
1575 table = (struct dt_table*) dt_buf;
1576
Deepa Dinamani19648b42013-09-05 17:05:55 -07001577 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001578 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1579 return -1;
1580 }
1581
Deepa Dinamani19648b42013-09-05 17:05:55 -07001582 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1583 if (!table)
1584 return -1;
1585
1586 /* Read the entire device tree table into buffer */
1587 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1588 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1589 return -1;
1590 }
1591
1592
Joel Kingaa335dc2013-06-03 16:11:08 -07001593 /* Find index of device tree within device tree table */
1594 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001595 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1596 return -1;
1597 }
1598
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001599 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001600 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001601 {
1602 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1603 return -1;
1604 }
1605
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001606 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001607 if(flash_read(ptn, offset + dt_entry.offset,
1608 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001609 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1610 return -1;
1611 }
1612 }
1613#endif
1614
Shashank Mittalcd98d472011-08-02 14:29:24 -07001615 }
David Ng183a7422009-12-07 14:55:21 -08001616continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001617
Dima Zavin214cc642009-01-26 11:16:21 -08001618 /* TODO: create/pass atags to kernel */
1619
Ajay Dudanie28a6072011-07-01 13:59:46 -07001620 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001621 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001622 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1623
1624 return 0;
1625}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001626
Shashank Mittal162244e2011-08-08 19:01:25 -07001627void write_device_info_mmc(device_info *dev)
1628{
Shashank Mittal162244e2011-08-08 19:01:25 -07001629 unsigned long long ptn = 0;
1630 unsigned long long size;
1631 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001632 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001633 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001634 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001635
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001636 if (devinfo_present)
1637 index = partition_get_index("devinfo");
1638 else
1639 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001640
Shashank Mittal162244e2011-08-08 19:01:25 -07001641 ptn = partition_get_offset(index);
1642 if(ptn == 0)
1643 {
1644 return;
1645 }
1646
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001647 lun = partition_get_lun(index);
1648 mmc_set_lun(lun);
1649
Shashank Mittal162244e2011-08-08 19:01:25 -07001650 size = partition_get_size(index);
1651
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001652 blocksize = mmc_get_device_blocksize();
1653
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001654 if (devinfo_present)
1655 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1656 else
1657 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1658 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001659 {
1660 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001661 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001662 }
1663}
1664
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001665void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001666{
Shashank Mittal162244e2011-08-08 19:01:25 -07001667 unsigned long long ptn = 0;
1668 unsigned long long size;
1669 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001670 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001671 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001672
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001673 if ((index = partition_get_index("devinfo")) < 0)
1674 {
1675 devinfo_present = false;
1676 index = partition_get_index("aboot");
1677 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001678
Shashank Mittal162244e2011-08-08 19:01:25 -07001679 ptn = partition_get_offset(index);
1680 if(ptn == 0)
1681 {
1682 return;
1683 }
1684
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001685 mmc_set_lun(partition_get_lun(index));
1686
Shashank Mittal162244e2011-08-08 19:01:25 -07001687 size = partition_get_size(index);
1688
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001689 blocksize = mmc_get_device_blocksize();
1690
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001691 if (devinfo_present)
1692 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1693 else
1694 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1695 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001696 {
1697 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001698 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001699 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001700}
1701
1702void write_device_info_flash(device_info *dev)
1703{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001704 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001705 struct ptentry *ptn;
1706 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001707 if(info == NULL)
1708 {
1709 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1710 ASSERT(0);
1711 }
1712 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001713 ptable = flash_get_ptable();
1714 if (ptable == NULL)
1715 {
1716 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1717 return;
1718 }
1719
1720 ptn = ptable_find(ptable, "devinfo");
1721 if (ptn == NULL)
1722 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001723 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001724 return;
1725 }
1726
1727 memcpy(info, dev, sizeof(device_info));
1728
1729 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1730 {
1731 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1732 return;
1733 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001734 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001735}
1736
vijay kumarc65876c2015-04-24 13:29:16 +05301737static int read_allow_oem_unlock(device_info *dev)
1738{
vijay kumarc65876c2015-04-24 13:29:16 +05301739 unsigned offset;
1740 int index;
1741 unsigned long long ptn;
1742 unsigned long long ptn_size;
1743 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001744 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301745
vijay kumarca2e6812015-07-08 20:28:25 +05301746 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301747 if (index == INVALID_PTN)
1748 {
vijay kumarca2e6812015-07-08 20:28:25 +05301749 index = partition_get_index(frp_ptns[1]);
1750 if (index == INVALID_PTN)
1751 {
1752 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1753 return -1;
1754 }
vijay kumarc65876c2015-04-24 13:29:16 +05301755 }
1756
1757 ptn = partition_get_offset(index);
1758 ptn_size = partition_get_size(index);
1759 offset = ptn_size - blocksize;
1760
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001761 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301762 {
1763 dprintf(CRITICAL, "Reading MMC failed\n");
1764 return -1;
1765 }
1766
1767 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1768 is_allow_unlock = buf[blocksize-1] & 0x01;
1769 return 0;
1770}
1771
1772static int write_allow_oem_unlock(bool allow_unlock)
1773{
vijay kumarc65876c2015-04-24 13:29:16 +05301774 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301775 int index;
1776 unsigned long long ptn;
1777 unsigned long long ptn_size;
1778 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001779 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301780
vijay kumarca2e6812015-07-08 20:28:25 +05301781 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301782 if (index == INVALID_PTN)
1783 {
vijay kumarca2e6812015-07-08 20:28:25 +05301784 index = partition_get_index(frp_ptns[1]);
1785 if (index == INVALID_PTN)
1786 {
1787 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1788 return -1;
1789 }
vijay kumarc65876c2015-04-24 13:29:16 +05301790 }
1791
1792 ptn = partition_get_offset(index);
1793 ptn_size = partition_get_size(index);
1794 offset = ptn_size - blocksize;
1795
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001796 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301797 {
1798 dprintf(CRITICAL, "Reading MMC failed\n");
1799 return -1;
1800 }
1801
1802 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1803 buf[blocksize-1] = allow_unlock;
1804 if (mmc_write(ptn + offset, blocksize, buf))
1805 {
1806 dprintf(CRITICAL, "Writing MMC failed\n");
1807 return -1;
1808 }
1809
1810 return 0;
1811}
1812
Shashank Mittal162244e2011-08-08 19:01:25 -07001813void read_device_info_flash(device_info *dev)
1814{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001815 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001816 struct ptentry *ptn;
1817 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001818 if(info == NULL)
1819 {
1820 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1821 ASSERT(0);
1822 }
1823 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001824 ptable = flash_get_ptable();
1825 if (ptable == NULL)
1826 {
1827 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1828 return;
1829 }
1830
1831 ptn = ptable_find(ptable, "devinfo");
1832 if (ptn == NULL)
1833 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001834 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001835 return;
1836 }
1837
1838 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1839 {
1840 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1841 return;
1842 }
1843
1844 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1845 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001846 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1847 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001848 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001849 write_device_info_flash(info);
1850 }
1851 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001852 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001853}
1854
1855void write_device_info(device_info *dev)
1856{
1857 if(target_is_emmc_boot())
1858 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001859 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1860 if(info == NULL)
1861 {
1862 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1863 ASSERT(0);
1864 }
1865 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001866 memcpy(info, dev, sizeof(struct device_info));
1867
1868#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001869 if (is_secure_boot_enable()) {
1870 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1871 ASSERT(0);
1872 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001873 else
1874 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001875#else
1876 write_device_info_mmc(info);
1877#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001878 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001879 }
1880 else
1881 {
1882 write_device_info_flash(dev);
1883 }
1884}
1885
1886void read_device_info(device_info *dev)
1887{
1888 if(target_is_emmc_boot())
1889 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001890 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1891 if(info == NULL)
1892 {
1893 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1894 ASSERT(0);
1895 }
1896 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001897
1898#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001899 if (is_secure_boot_enable()) {
1900 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1901 ASSERT(0);
1902 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001903 else
1904 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001905#else
1906 read_device_info_mmc(info);
1907#endif
1908
1909 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1910 {
1911 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08001912 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001913 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301914#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08001915 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301916#endif
lijuang511a2b52015-08-14 20:50:51 +08001917 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001918 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05301919#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08001920 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05301921#endif
lijuang511a2b52015-08-14 20:50:51 +08001922 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001923 info->is_tampered = 0;
1924 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301925#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07001926 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05301927#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001928 write_device_info(info);
1929 }
1930 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001931 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001932 }
1933 else
1934 {
1935 read_device_info_flash(dev);
1936 }
1937}
1938
1939void reset_device_info()
1940{
1941 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001942 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001943 write_device_info(&device);
1944}
1945
1946void set_device_root()
1947{
1948 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001949 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001950 write_device_info(&device);
1951}
1952
lijuang4ece1e72015-08-14 21:02:36 +08001953/* set device unlock value
1954 * Must check FRP before call this function
1955 * Need to wipe data when unlock status changed
1956 * type 0: oem unlock
1957 * type 1: unlock critical
1958 * status 0: unlock as false
1959 * status 1: lock as true
1960 */
1961void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08001962{
lijuang4ece1e72015-08-14 21:02:36 +08001963 if (type == UNLOCK)
1964 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05301965#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08001966 else if (type == UNLOCK_CRITICAL)
1967 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05301968#endif
lijuang4ece1e72015-08-14 21:02:36 +08001969 write_device_info(&device);
1970}
1971
1972static void set_device_unlock(int type, bool status)
1973{
1974 int is_unlocked = -1;
1975 char response[MAX_RSP_SIZE];
1976
1977 /* check device unlock status if it is as expected */
1978 if (type == UNLOCK)
1979 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05301980#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08001981 else if (type == UNLOCK_CRITICAL)
1982 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05301983#endif
lijuang4ece1e72015-08-14 21:02:36 +08001984 if (is_unlocked == status) {
1985 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
1986 fastboot_info(response);
1987 fastboot_okay("");
1988 return;
1989 }
1990
1991 /* status is true, it means to unlock device */
1992 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08001993 if(!is_allow_unlock) {
1994 fastboot_fail("oem unlock is not allowed");
1995 return;
1996 }
1997
lijuang4ece1e72015-08-14 21:02:36 +08001998#if FBCON_DISPLAY_MSG
1999 display_unlock_menu_thread(type);
2000 fastboot_okay("");
2001 return;
2002#else
2003 if (type == UNLOCK) {
2004 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2005 return;
2006 }
2007#endif
lijuang21f12f52015-08-22 16:22:19 +08002008 }
lijuang4ece1e72015-08-14 21:02:36 +08002009
2010 set_device_unlock_value(type, status);
2011
2012 /* wipe data */
2013 struct recovery_message msg;
2014
2015 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2016 write_misc(0, &msg, sizeof(msg));
2017
2018 fastboot_okay("");
2019 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002020}
2021
lijuang511a2b52015-08-14 20:50:51 +08002022static bool critical_flash_allowed(const char * entry)
2023{
2024 uint32_t i = 0;
2025 if (entry == NULL)
2026 return false;
2027
2028 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2029 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2030 return true;
2031 }
2032 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002033}
2034
2035#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002036int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2037{
2038 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002039 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002040 struct dt_table *table;
2041 struct dt_entry dt_entry;
2042 uint32_t dt_hdr_size;
2043 unsigned int compressed_size = 0;
2044 unsigned int dtb_size = 0;
2045 unsigned int out_avai_len = 0;
2046 unsigned char *out_addr = NULL;
2047 unsigned char *best_match_dt_addr = NULL;
2048 int rc;
2049
2050 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2051
2052 if(hdr->dt_size != 0) {
2053 /* add kernel offset */
2054 dt_image_offset += page_size;
2055 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002056 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002057
Deepa Dinamani19648b42013-09-05 17:05:55 -07002058 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002059 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2060 dt_image_offset += n;
2061
Joel Kingaa335dc2013-06-03 16:11:08 -07002062 /* add second offset */
2063 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002064 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2065 dt_image_offset += n;
2066 }
2067
Matthew Qin271927e2015-03-31 22:07:07 -04002068 /* offset now point to start of dt.img */
2069 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2070
2071 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2072 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002073 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002074 }
2075 /* Find index of device tree within device tree table */
2076 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2077 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2078 return -1;
Matthew Qin0b15b322015-05-19 05:20:54 -04002079 }
Matthew Qin271927e2015-03-31 22:07:07 -04002080
2081 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2082 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
Matthew Qin0b15b322015-05-19 05:20:54 -04002083 {
Matthew Qin271927e2015-03-31 22:07:07 -04002084 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2085 out_avai_len = target_get_max_flash_size() - scratch_offset;
2086 dprintf(INFO, "decompressing dtb: start\n");
2087 rc = decompress(best_match_dt_addr,
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002088 dt_entry.size, out_addr, out_avai_len,
Matthew Qin271927e2015-03-31 22:07:07 -04002089 &compressed_size, &dtb_size);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002090 if (rc)
2091 {
2092 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
2093 ASSERT(0);
2094 }
Amol Jadicb524072012-08-09 16:40:18 -07002095
Matthew Qin271927e2015-03-31 22:07:07 -04002096 dprintf(INFO, "decompressing dtb: done\n");
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002097 best_match_dt_addr = out_addr;
2098 } else {
Amol Jadicb524072012-08-09 16:40:18 -07002099 dtb_size = dt_entry.size;
2100 }
2101 /* Validate and Read device device tree in the "tags_add */
2102 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
2103 {
2104 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002105 return -1;
2106 }
Amit Blay8a510302015-08-17 09:20:01 +03002107
2108 /* Read device device tree in the "tags_add */
2109 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002110 } else
2111 return -1;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002112
2113 /* Everything looks fine. Return success. */
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002114 return 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002115}
2116#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002117
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002118void cmd_boot(const char *arg, void *data, unsigned sz)
2119{
Matthew Qinbb7923d2015-02-09 10:56:09 +08002120#ifdef MDTP_SUPPORT
2121 static bool is_mdtp_activated = 0;
2122#endif /* MDTP_SUPPORT */
2123 unsigned kernel_actual;
2124 unsigned ramdisk_actual;
2125 uint32_t image_actual;
Matthew Qin271927e2015-03-31 22:07:07 -04002126 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002127 uint32_t sig_actual = SIGNATURE_SIZE;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002128 struct boot_img_hdr *hdr = NULL;
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002129 struct kernel64_hdr *kptr = NULL;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002130 char *ptr = ((char*) data);
2131 int ret = 0;
2132 uint8_t dtb_copied = 0;
2133 unsigned int out_len = 0;
2134 unsigned int out_avai_len = 0;
2135 unsigned char *out_addr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002136 uint32_t dtb_offset = 0;
2137 unsigned char *kernel_start_addr = NULL;
2138 unsigned int kernel_size = 0;
2139 unsigned int scratch_offset = 0;
2140
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002141#if VERIFIED_BOOT
Brian Swetland9c4c0752009-01-25 16:23:50 -08002142 if(target_build_variant_user() && !device.is_unlocked)
2143 {
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002144 fastboot_fail("unlock device to use this command");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002145 return;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002146 }
2147#endif
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002148
2149 if (sz < sizeof(hdr)) {
2150 fastboot_fail("invalid bootimage header");
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002151 return;
2152 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002153
2154 hdr = (struct boot_img_hdr *)data;
2155
2156 /* ensure commandline is terminated */
2157 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2158
2159 if(target_is_emmc_boot() && hdr->page_size) {
2160 page_size = hdr->page_size;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002161 page_mask = page_size - 1;
2162 }
2163
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002164 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2165 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002166#if DEVICE_TREE
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002167 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2168#endif
2169
2170 image_actual = ADD_OF(page_size, kernel_actual);
2171 image_actual = ADD_OF(image_actual, ramdisk_actual);
2172 image_actual = ADD_OF(image_actual, dt_actual);
2173
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002174 if (target_use_signed_kernel() && (!device.is_unlocked))
2175 image_actual = ADD_OF(image_actual, sig_actual);
2176
2177 /* sz should have atleast raw boot image */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002178 if (image_actual > sz) {
Amit Blay4aa292f2015-04-28 21:55:59 +03002179 fastboot_fail("bootimage: incomplete or not signed");
2180 return;
2181 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002182
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002183 // Initialize boot state before trying to verify boot.img
2184#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002185 boot_verifier_init();
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002186 /* Handle overflow if the input image size is greater than
2187 * boot image buffer can hold
2188 */
2189 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2190 {
2191 fastboot_fail("booimage: size is greater than boot image buffer can hold");
2192 return;
2193 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002194#endif
2195
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002196 /* Verify the boot image
2197 * device & page_size are initialized in aboot_init
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002198 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002199 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002200 /* Pass size excluding signature size, otherwise we would try to
2201 * access signature beyond its length
2202 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002203 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002204 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002205#ifdef MDTP_SUPPORT
2206 else
2207 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002208 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002209 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002210
2211 if (!is_mdtp_activated) {
2212 ext_partition.partition = MDTP_PARTITION_NONE;
2213 mdtp_fwlock_verify_lock(&ext_partition);
2214 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002215 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002216
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002217 mdtp_activated(&is_mdtp_activated);
2218 if(is_mdtp_activated){
2219 dprintf(CRITICAL, "fastboot boot command is not available.\n");
2220 return;
2221 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002222#endif /* MDTP_SUPPORT */
2223
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002224#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302225#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002226 // send root of trust
2227 if(!send_rot_command((uint32_t)device.is_unlocked))
2228 ASSERT(0);
2229#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302230#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002231 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002232 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2233 * If not, continue booting.
2234 */
2235 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2236 {
2237 out_addr = (unsigned char *)target_get_scratch_address();
2238 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2239 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002240 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002241 ret = decompress((unsigned char *)(ptr + page_size),
2242 hdr->kernel_size, out_addr, out_avai_len,
2243 &dtb_offset, &out_len);
2244 if (ret)
2245 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002246 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002247 ASSERT(0);
2248 }
2249
Matthew Qin0b15b322015-05-19 05:20:54 -04002250 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002251 kptr = (struct kernel64_hdr *)out_addr;
2252 kernel_start_addr = out_addr;
2253 kernel_size = out_len;
2254 } else {
2255 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2256 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2257 kernel_size = hdr->kernel_size;
2258 }
2259
2260 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002261 * Update the kernel/ramdisk/tags address if the boot image header
2262 * has default values, these default values come from mkbootimg when
2263 * the boot image is flashed using fastboot flash:raw
2264 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002265 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002266
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002267 /* Get virtual addresses since the hdr saves physical addresses. */
2268 hdr->kernel_addr = VA(hdr->kernel_addr);
2269 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2270 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002271
Matthew Qinbb7923d2015-02-09 10:56:09 +08002272 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002273 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002274 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002275 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2276 {
2277 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002278 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002279 }
2280
Amol Jadicb524072012-08-09 16:40:18 -07002281#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002282 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002283 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002284 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002285
2286 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002287#else
2288 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2289 {
2290 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002291 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002292 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002293#endif
2294
2295 /* Load ramdisk & kernel */
2296 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002297 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002298
2299#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002300 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2301 {
2302 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2303 return;
2304 }
2305
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002306 /*
2307 * If dtb is not found look for appended DTB in the kernel.
2308 * If appended dev tree is found, update the atags with
2309 * memory address to the DTB appended location on RAM.
2310 * Else update with the atags address in the kernel header
2311 */
2312 if (!dtb_copied) {
2313 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002314 dtb = dev_tree_appended((void*)(ptr + page_size),
2315 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002316 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002317 if (!dtb) {
2318 fastboot_fail("dtb not found");
2319 return;
2320 }
Amol Jadicb524072012-08-09 16:40:18 -07002321 }
2322#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002323
2324 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002325 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002326
Dima Zavin77e41f32013-03-06 16:10:43 -08002327 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002328 (const char*) hdr->cmdline, board_machtype(),
2329 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002330}
2331
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002332void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002333{
2334 struct ptentry *ptn;
2335 struct ptable *ptable;
2336
2337 ptable = flash_get_ptable();
2338 if (ptable == NULL) {
2339 fastboot_fail("partition table doesn't exist");
2340 return;
2341 }
2342
2343 ptn = ptable_find(ptable, arg);
2344 if (ptn == NULL) {
2345 fastboot_fail("unknown partition name");
2346 return;
2347 }
2348
2349 if (flash_erase(ptn)) {
2350 fastboot_fail("failed to erase partition");
2351 return;
2352 }
2353 fastboot_okay("");
2354}
2355
Bikas Gurungd48bd242010-09-04 19:54:32 -07002356
2357void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2358{
2359 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002360 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002361 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002362 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002363
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002364#if VERIFIED_BOOT
2365 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2366 {
2367 if(!device.is_unlocked)
2368 {
2369 fastboot_fail("unlock device to erase keystore");
2370 return;
2371 }
2372 }
2373#endif
2374
Kinson Chikf1a43512011-07-14 11:28:39 -07002375 index = partition_get_index(arg);
2376 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002377 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002378
Kinson Chikf1a43512011-07-14 11:28:39 -07002379 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002380 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002381 return;
2382 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002383
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002384 lun = partition_get_lun(index);
2385 mmc_set_lun(lun);
2386
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002387 if (platform_boot_dev_isemmc())
2388 {
2389 if (mmc_erase_card(ptn, size)) {
2390 fastboot_fail("failed to erase partition\n");
2391 return;
2392 }
2393 } else {
2394 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2395 size = partition_get_size(index);
2396 if (size > DEFAULT_ERASE_SIZE)
2397 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002398
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002399 /* Simple inefficient version of erase. Just writing
2400 0 in first several blocks */
2401 if (mmc_write(ptn , size, (unsigned int *)out)) {
2402 fastboot_fail("failed to erase partition");
2403 return;
2404 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002405 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002406#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302407#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002408 if(!(strncmp(arg, "userdata", 8)))
2409 if(send_delete_keys_to_tz())
2410 ASSERT(0);
2411#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302412#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002413 fastboot_okay("");
2414}
2415
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002416void cmd_erase(const char *arg, void *data, unsigned sz)
2417{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002418#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002419 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002420 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002421 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002422 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002423 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002424 return;
2425 }
2426 }
2427#endif
2428
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002429 if(target_is_emmc_boot())
2430 cmd_erase_mmc(arg, data, sz);
2431 else
2432 cmd_erase_nand(arg, data, sz);
2433}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002434
Channagoud Kadabiad259832015-05-29 11:14:17 -07002435static uint32_t aboot_get_secret_key()
2436{
2437 /* 0 is invalid secret key, update this implementation to return
2438 * device specific unique secret key
2439 */
2440 return 0;
2441}
2442
Ajay Dudani5c761132011-04-07 20:19:04 -07002443void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002444{
2445 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002446 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002447 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002448 char *token = NULL;
2449 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002450 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002451 uint8_t lun = 0;
2452 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002453
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002454 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002455 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002456 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002457 if(token)
2458 {
2459 lun = atoi(token);
2460 mmc_set_lun(lun);
2461 lun_set = true;
2462 }
2463
Mao Jinlong226f33a2014-07-04 17:24:10 +08002464 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002465 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002466 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2467 {
2468 if (!aboot_frp_unlock(pname, data, sz))
2469 {
2470 fastboot_info("FRP unlock successful");
2471 fastboot_okay("");
2472 }
2473 else
2474 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2475
2476 return;
2477 }
2478
Mao Jinlong226f33a2014-07-04 17:24:10 +08002479 if (!strcmp(pname, "partition"))
2480 {
2481 dprintf(INFO, "Attempt to write partition image.\n");
2482 if (write_partition(sz, (unsigned char *) data)) {
2483 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002484 return;
2485 }
2486 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002487 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002488 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002489#if VERIFIED_BOOT
2490 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2491 {
2492 if(!device.is_unlocked)
2493 {
2494 fastboot_fail("unlock device to flash keystore");
2495 return;
2496 }
2497 if(!boot_verify_validate_keystore((unsigned char *)data))
2498 {
2499 fastboot_fail("image is not a keystore file");
2500 return;
2501 }
2502 }
2503#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002504 index = partition_get_index(pname);
2505 ptn = partition_get_offset(index);
2506 if(ptn == 0) {
2507 fastboot_fail("partition table doesn't exist");
2508 return;
2509 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002510
Mao Jinlong226f33a2014-07-04 17:24:10 +08002511 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2512 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2513 fastboot_fail("image is not a boot image");
2514 return;
2515 }
2516 }
2517
2518 if(!lun_set)
2519 {
2520 lun = partition_get_lun(index);
2521 mmc_set_lun(lun);
2522 }
2523
2524 size = partition_get_size(index);
2525 if (ROUND_TO_PAGE(sz,511) > size) {
2526 fastboot_fail("size too large");
2527 return;
2528 }
2529 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2530 fastboot_fail("flash write failure");
2531 return;
2532 }
Greg Grisco6e754772011-06-23 12:19:39 -07002533 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002534 }
2535 fastboot_okay("");
2536 return;
2537}
2538
Ajay Dudanide984792015-03-02 09:57:41 -08002539void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2540{
2541 int i, images;
2542 meta_header_t *meta_header;
2543 img_header_entry_t *img_header_entry;
2544
2545 meta_header = (meta_header_t*) data;
2546 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2547
2548 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2549
2550 for (i=0; i<images; i++) {
2551
2552 if((img_header_entry[i].ptn_name == NULL) ||
2553 (img_header_entry[i].start_offset == 0) ||
2554 (img_header_entry[i].size == 0))
2555 break;
2556
2557 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2558 (void *) data + img_header_entry[i].start_offset,
2559 img_header_entry[i].size);
2560 }
2561
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002562 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2563 {
2564 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2565 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2566 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2567 }
2568 else
2569 {
2570 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2571 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2572 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2573 }
2574
2575 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002576 fastboot_okay("");
2577 return;
2578}
2579
Ajay Dudani5c761132011-04-07 20:19:04 -07002580void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2581{
2582 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002583 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002584 uint32_t *fill_buf = NULL;
2585 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002586 sparse_header_t *sparse_header;
2587 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002588 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002589 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302590 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002591 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302592 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002593 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302594 /*End of the sparse image address*/
2595 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002596
Kinson Chikf1a43512011-07-14 11:28:39 -07002597 index = partition_get_index(arg);
2598 ptn = partition_get_offset(index);
2599 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002600 fastboot_fail("partition table doesn't exist");
2601 return;
2602 }
2603
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302604 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302605
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002606 lun = partition_get_lun(index);
2607 mmc_set_lun(lun);
2608
vijay kumar800255e2015-04-24 20:26:19 +05302609 if (sz < sizeof(sparse_header_t)) {
2610 fastboot_fail("size too low");
2611 return;
2612 }
2613
Ajay Dudani5c761132011-04-07 20:19:04 -07002614 /* Read and skip over sparse image header */
2615 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302616
vijay kumar1321f342015-03-27 12:13:42 +05302617 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002618 fastboot_fail("size too large");
2619 return;
2620 }
2621
vijay kumarde4fcf62015-04-23 13:05:49 +05302622 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302623
2624 if (data_end < (uint32_t)data) {
2625 fastboot_fail("buffer overreads occured due to invalid sparse header");
2626 return;
2627 }
2628
vijay kumarde4fcf62015-04-23 13:05:49 +05302629 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002630 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302631 fastboot_fail("sparse header size mismatch");
2632 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002633 }
2634
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002635 dprintf (SPEW, "=== Sparse Image Header ===\n");
2636 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2637 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2638 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2639 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2640 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2641 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2642 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2643 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002644
2645 /* Start processing chunks */
2646 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2647 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302648 /* Make sure the total image size does not exceed the partition size */
2649 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2650 fastboot_fail("size too large");
2651 return;
2652 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002653 /* Read and skip over chunk header */
2654 chunk_header = (chunk_header_t *) data;
2655 data += sizeof(chunk_header_t);
2656
vijay kumar800255e2015-04-24 20:26:19 +05302657 if (data_end < (uint32_t)data) {
2658 fastboot_fail("buffer overreads occured due to invalid sparse header");
2659 return;
2660 }
2661
Ajay Dudani5c761132011-04-07 20:19:04 -07002662 dprintf (SPEW, "=== Chunk Header ===\n");
2663 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2664 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2665 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2666
vijay kumar800255e2015-04-24 20:26:19 +05302667 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002668 {
vijay kumar800255e2015-04-24 20:26:19 +05302669 fastboot_fail("chunk header size mismatch");
2670 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002671 }
2672
wufeng.jiang813dc352015-06-02 23:02:46 -04002673 if (!sparse_header->blk_sz ){
2674 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302675 return;
2676 }
2677
wufeng.jiang813dc352015-06-02 23:02:46 -04002678 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2679
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302680 /* Make sure that the chunk size calculated from sparse image does not
2681 * exceed partition size
2682 */
2683 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2684 {
2685 fastboot_fail("Chunk data size exceeds partition size");
2686 return;
2687 }
2688
Ajay Dudani5c761132011-04-07 20:19:04 -07002689 switch (chunk_header->chunk_type)
2690 {
2691 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002692 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002693 chunk_data_sz))
2694 {
2695 fastboot_fail("Bogus chunk size for chunk type Raw");
2696 return;
2697 }
2698
vijay kumar800255e2015-04-24 20:26:19 +05302699 if (data_end < (uint32_t)data + chunk_data_sz) {
2700 fastboot_fail("buffer overreads occured due to invalid sparse header");
2701 return;
2702 }
2703
wufeng.jiang813dc352015-06-02 23:02:46 -04002704 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2705 otherwise it will return in the line above
2706 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002707 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002708 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002709 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002710 {
2711 fastboot_fail("flash write failure");
2712 return;
2713 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302714 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2715 fastboot_fail("Bogus size for RAW chunk type");
2716 return;
2717 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002718 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002719 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002720 break;
2721
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002722 case CHUNK_TYPE_FILL:
2723 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2724 sizeof(uint32_t)))
2725 {
2726 fastboot_fail("Bogus chunk size for chunk type FILL");
2727 return;
2728 }
2729
2730 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2731 if (!fill_buf)
2732 {
2733 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2734 return;
2735 }
2736
vijay kumar800255e2015-04-24 20:26:19 +05302737 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2738 fastboot_fail("buffer overreads occured due to invalid sparse header");
2739 return;
2740 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002741 fill_val = *(uint32_t *)data;
2742 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002743
2744 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2745 {
2746 fill_buf[i] = fill_val;
2747 }
2748
wufeng.jiang813dc352015-06-02 23:02:46 -04002749 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002750 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302751 /* Make sure that the data written to partition does not exceed partition size */
2752 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2753 {
2754 fastboot_fail("Chunk data size for fill type exceeds partition size");
2755 return;
2756 }
2757
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002758 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2759 sparse_header->blk_sz,
2760 fill_buf))
2761 {
2762 fastboot_fail("flash write failure");
2763 free(fill_buf);
2764 return;
2765 }
2766
2767 total_blocks++;
2768 }
2769
2770 free(fill_buf);
2771 break;
2772
Ajay Dudani5c761132011-04-07 20:19:04 -07002773 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302774 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2775 fastboot_fail("bogus size for chunk DONT CARE type");
2776 return;
2777 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002778 total_blocks += chunk_header->chunk_sz;
2779 break;
2780
Ajay Dudani5c761132011-04-07 20:19:04 -07002781 case CHUNK_TYPE_CRC:
2782 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2783 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002784 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002785 return;
2786 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302787 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2788 fastboot_fail("bogus size for chunk CRC type");
2789 return;
2790 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002791 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302792 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2793 fastboot_fail("integer overflow occured");
2794 return;
2795 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002796 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302797 if (data_end < (uint32_t)data) {
2798 fastboot_fail("buffer overreads occured due to invalid sparse header");
2799 return;
2800 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002801 break;
2802
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002803 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002804 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002805 fastboot_fail("Unknown chunk type");
2806 return;
2807 }
2808 }
2809
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002810 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2811 total_blocks, sparse_header->total_blks);
2812
2813 if(total_blocks != sparse_header->total_blks)
2814 {
2815 fastboot_fail("sparse image write failure");
2816 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002817
2818 fastboot_okay("");
2819 return;
2820}
2821
2822void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2823{
2824 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002825 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002826
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002827#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002828 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2829 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002830 int ret=0;
2831 uint32 major_version=0;
2832 uint32 minor_version=0;
2833
2834 ret = scm_svc_version(&major_version,&minor_version);
2835 if(!ret)
2836 {
2837 if(major_version >= 2)
2838 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002839 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002840 {
2841 ret = encrypt_scm((uint32 **) &data, &sz);
2842 if (ret != 0) {
2843 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2844 return;
2845 }
2846
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002847 /* Protect only for SSD */
2848 if (!strcmp(arg, "ssd")) {
2849 ret = scm_protect_keystore((uint32 *) data, sz);
2850 if (ret != 0) {
2851 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2852 return;
2853 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002854 }
2855 }
2856 else
2857 {
2858 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2859 if(ret != 0)
2860 {
2861 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2862 return;
2863 }
2864 }
2865 }
2866 else
2867 {
2868 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2869 magic_number[1] == DECRYPT_MAGIC_1)
2870 {
2871 ret = decrypt_scm((uint32 **) &data, &sz);
2872 if (ret != 0) {
2873 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2874 return;
2875 }
2876 }
2877 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2878 magic_number[1] == ENCRYPT_MAGIC_1)
2879 {
2880 ret = encrypt_scm((uint32 **) &data, &sz);
2881 if (ret != 0) {
2882 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2883 return;
2884 }
2885 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002886 }
2887 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002888 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002889 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002890 dprintf(CRITICAL,"INVALID SVC Version\n");
2891 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002892 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002893#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002894
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002895#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002896 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002897 {
lijuang511a2b52015-08-14 20:50:51 +08002898 /* if device is locked:
2899 * common partition will not allow to be flashed
2900 * critical partition will allow to flash image.
2901 */
2902 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
2903 fastboot_fail("Partition flashing is not allowed");
2904 return;
2905 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302906#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002907 /* if device critical is locked:
2908 * common partition will allow to be flashed
2909 * critical partition will not allow to flash image.
2910 */
2911 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
2912 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002913 return;
2914 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302915#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002916 }
2917#endif
2918
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002919 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002920 meta_header = (meta_header_t *) data;
2921 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002922 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002923 else if (meta_header->magic == META_HEADER_MAGIC)
2924 cmd_flash_meta_img(arg, data, sz);
2925 else
2926 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002927
2928#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05302929#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002930 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
2931 {
2932 // reset dm_verity mode to enforcing
2933 device.verity_mode = 1;
2934 write_device_info(&device);
2935 }
2936#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05302937#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002938
Ajay Dudani5c761132011-04-07 20:19:04 -07002939 return;
2940}
2941
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002942void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2943{
2944 struct ptentry *sys_ptn;
2945 struct ptable *ptable;
2946
2947 ptable = flash_get_ptable();
2948 if (ptable == NULL) {
2949 fastboot_fail("partition table doesn't exist");
2950 return;
2951 }
2952
2953 sys_ptn = ptable_find(ptable, "system");
2954 if (sys_ptn == NULL) {
2955 fastboot_fail("system partition not found");
2956 return;
2957 }
2958
2959 sz = ROUND_TO_PAGE(sz, page_mask);
2960 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2961 fastboot_fail("update_ubi_vol failed");
2962 else
2963 fastboot_okay("");
2964}
2965
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002966void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002967{
2968 struct ptentry *ptn;
2969 struct ptable *ptable;
2970 unsigned extra = 0;
2971
2972 ptable = flash_get_ptable();
2973 if (ptable == NULL) {
2974 fastboot_fail("partition table doesn't exist");
2975 return;
2976 }
2977
2978 ptn = ptable_find(ptable, arg);
2979 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002980 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2981 arg);
2982 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002983 return;
2984 }
2985
2986 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2987 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2988 fastboot_fail("image is not a boot image");
2989 return;
2990 }
2991 }
2992
Amol Jadi5c61a952012-05-04 17:05:35 -07002993 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002994 || !strcmp(ptn->name, "userdata")
2995 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002996 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002997 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002998 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002999 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003000 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003001
3002 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003003 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3004 if (flash_ubi_img(ptn, data, sz)) {
3005 fastboot_fail("flash write failure");
3006 return;
3007 }
3008 } else {
3009 if (flash_write(ptn, extra, data, sz)) {
3010 fastboot_fail("flash write failure");
3011 return;
3012 }
Dima Zavin214cc642009-01-26 11:16:21 -08003013 }
3014 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3015 fastboot_okay("");
3016}
3017
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003018void cmd_flash(const char *arg, void *data, unsigned sz)
3019{
3020 if(target_is_emmc_boot())
3021 cmd_flash_mmc(arg, data, sz);
3022 else
3023 cmd_flash_nand(arg, data, sz);
3024}
3025
Dima Zavin214cc642009-01-26 11:16:21 -08003026void cmd_continue(const char *arg, void *data, unsigned sz)
3027{
3028 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003029 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003030
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003031 if (target_is_emmc_boot())
3032 {
lijuanga40d6302015-07-20 20:10:13 +08003033#if FBCON_DISPLAY_MSG
3034 keys_detect_init();
3035#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003036 boot_linux_from_mmc();
3037 }
3038 else
3039 {
3040 boot_linux_from_flash();
3041 }
Dima Zavin214cc642009-01-26 11:16:21 -08003042}
3043
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003044void cmd_reboot(const char *arg, void *data, unsigned sz)
3045{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003046 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003047 fastboot_okay("");
3048 reboot_device(0);
3049}
3050
3051void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3052{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003053 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003054 fastboot_okay("");
3055 reboot_device(FASTBOOT_MODE);
3056}
3057
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003058void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3059{
3060 dprintf(INFO, "Enabling charger screen check\n");
3061 device.charger_screen_enabled = 1;
3062 write_device_info(&device);
3063 fastboot_okay("");
3064}
3065
3066void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3067{
3068 dprintf(INFO, "Disabling charger screen check\n");
3069 device.charger_screen_enabled = 0;
3070 write_device_info(&device);
3071 fastboot_okay("");
3072}
3073
lijuanga25d8bb2015-09-16 13:11:52 +08003074void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3075{
3076 char *p = NULL;
3077 const char *delim = " \t\n\r";
3078
3079 if (arg) {
3080 p = strtok((char *)arg, delim);
3081 if (p) {
3082 if (!strncmp(p, "0", 1)) {
3083 device.charger_screen_enabled = 0;
3084 } else if (!strncmp(p, "1", 1)) {
3085 device.charger_screen_enabled = 1;
3086 }
3087 }
3088 }
3089
3090 /* update charger_screen_enabled value for getvar
3091 * command
3092 */
3093 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3094 device.charger_screen_enabled);
3095
3096 write_device_info(&device);
3097 fastboot_okay("");
3098}
3099
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303100void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3101{
3102 dprintf(INFO, "Selecting display panel %s\n", arg);
3103 if (arg)
3104 strlcpy(device.display_panel, arg,
3105 sizeof(device.display_panel));
3106 write_device_info(&device);
3107 fastboot_okay("");
3108}
3109
Shashank Mittal162244e2011-08-08 19:01:25 -07003110void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3111{
lijuang4ece1e72015-08-14 21:02:36 +08003112 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303113}
3114
3115void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3116{
lijuang4ece1e72015-08-14 21:02:36 +08003117 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303118 if(!is_allow_unlock) {
3119 fastboot_fail("oem unlock is not allowed");
3120 return;
3121 }
3122
lijuang4ece1e72015-08-14 21:02:36 +08003123 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303124
lijuang4ece1e72015-08-14 21:02:36 +08003125 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303126 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003127
vijay kumarc65876c2015-04-24 13:29:16 +05303128 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3129 write_misc(0, &msg, sizeof(msg));
3130
3131 fastboot_okay("");
3132 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003133 }
3134 fastboot_okay("");
3135}
3136
Channagoud Kadabiad259832015-05-29 11:14:17 -07003137static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3138{
3139 int ret = 1;
3140 uint32_t secret_key;
3141 char seckey_buffer[MAX_RSP_SIZE];
3142
3143 secret_key = aboot_get_secret_key();
3144 if (secret_key)
3145 {
3146 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3147 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3148 {
3149 is_allow_unlock = true;
3150 write_allow_oem_unlock(is_allow_unlock);
3151 ret = 0;
3152 }
3153 }
3154 return ret;
3155}
3156
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003157void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3158{
lijuang4ece1e72015-08-14 21:02:36 +08003159 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003160}
3161
Shashank Mittala0032282011-08-26 14:50:11 -07003162void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3163{
lijuang511a2b52015-08-14 20:50:51 +08003164 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003165 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003166 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003167 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3168 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303169#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003170 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3171 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303172#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003173 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003174 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303175 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3176 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003177 fastboot_okay("");
3178}
3179
lijuang511a2b52015-08-14 20:50:51 +08003180void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3181{
3182 char response[MAX_RSP_SIZE];
3183 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3184 fastboot_info(response);
3185 fastboot_okay("");
3186}
3187
3188void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3189{
lijuang4ece1e72015-08-14 21:02:36 +08003190 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003191}
3192
3193void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3194{
lijuang4ece1e72015-08-14 21:02:36 +08003195 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003196}
3197
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003198void cmd_preflash(const char *arg, void *data, unsigned sz)
3199{
3200 fastboot_okay("");
3201}
3202
Mao Flynn7b379f32015-04-20 00:28:30 +08003203static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303204
Mao Flynn7b379f32015-04-20 00:28:30 +08003205int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303206{
Mao Flynn7b379f32015-04-20 00:28:30 +08003207 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303208 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003209 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303210 return -1;
3211 return 0;
3212}
3213
Mao Flynn7b379f32015-04-20 00:28:30 +08003214int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003215{
3216 struct ptentry *ptn;
3217 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003218 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003219 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003220
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303221 ptable = flash_get_ptable();
3222 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003223 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3224 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303225 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003226
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303227 ptn = ptable_find(ptable, "splash");
3228 if (ptn == NULL) {
3229 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003230 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303231 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003232 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303233 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003234 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303235 }
3236
Mao Flynn7b379f32015-04-20 00:28:30 +08003237 header = (struct logo_img_header *)logo_header;
3238 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303239 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003240 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303241 }
3242
3243 fb_display = fbcon_display();
3244 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003245 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3246 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3247
3248 /* if the logo is full-screen size, remove "fbcon_clear()" */
3249 if ((header->width != fb_display->width)
3250 || (header->height != fb_display->height))
3251 fbcon_clear();
3252
3253 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3254 (uint32_t *)base,
3255 (header->blocks * 512))) {
3256 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3257 return -1;
3258 }
3259 fbcon_extract_to_screen(header, base);
3260 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003261 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003262
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003263 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3264 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003265 return -1;
3266 }
3267
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303268 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003269 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3270 (uint32_t *)base,
3271 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303272 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303273 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003274 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003275 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303276 }
3277
Mao Flynn7b379f32015-04-20 00:28:30 +08003278 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303279}
3280
Mao Flynn7b379f32015-04-20 00:28:30 +08003281int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303282{
3283 int index = INVALID_PTN;
3284 unsigned long long ptn = 0;
3285 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003286 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003287 uint32_t blocksize, realsize, readsize;
3288 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303289
3290 index = partition_get_index("splash");
3291 if (index == 0) {
3292 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003293 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303294 }
3295
3296 ptn = partition_get_offset(index);
3297 if (ptn == 0) {
3298 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003299 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303300 }
3301
Mao Flynn1893a7f2015-06-03 12:03:36 +08003302 mmc_set_lun(partition_get_lun(index));
3303
3304 blocksize = mmc_get_device_blocksize();
3305 if (blocksize == 0) {
3306 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3307 return -1;
3308 }
3309
3310 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003311 if (!fb_display)
3312 {
3313 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3314 return -1;
3315 }
3316
Mao Flynn1893a7f2015-06-03 12:03:36 +08003317 base = (uint8_t *) fb_display->base;
3318
3319 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303320 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003321 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303322 }
3323
Mao Flynn1893a7f2015-06-03 12:03:36 +08003324 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003325 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303326 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003327 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303328 }
3329
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303330 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003331 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3332 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003333
Mao Flynn1893a7f2015-06-03 12:03:36 +08003334 realsize = header->blocks * 512;
3335 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3336
3337 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003338 if ((header->width != fb_display->width)
3339 || (header->height != fb_display->height))
3340 fbcon_clear();
3341
Mao Flynn1893a7f2015-06-03 12:03:36 +08003342 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003343 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3344 return -1;
3345 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003346
Mao Flynn1893a7f2015-06-03 12:03:36 +08003347 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3348 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303349
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003350 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3351 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003352 return -1;
3353 }
3354
3355 realsize = header->width * header->height * fb_display->bpp / 8;
3356 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3357
3358 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3359 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3360 fbcon_clear();
3361 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3362 return -1;
3363 }
3364 } else {
3365 if (mmc_read(ptn + blocksize ,
3366 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3367 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3368 return -1;
3369 }
3370 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3371 }
3372 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303373 }
3374
Mao Flynn7b379f32015-04-20 00:28:30 +08003375 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303376}
3377
Mao Flynn7b379f32015-04-20 00:28:30 +08003378int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303379{
3380 if (target_is_emmc_boot()) {
3381 return splash_screen_mmc();
3382 } else {
3383 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003384 }
3385}
3386
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003387/* Get the size from partiton name */
3388static void get_partition_size(const char *arg, char *response)
3389{
3390 uint64_t ptn = 0;
3391 uint64_t size;
3392 int index = INVALID_PTN;
3393
3394 index = partition_get_index(arg);
3395
3396 if (index == INVALID_PTN)
3397 {
3398 dprintf(CRITICAL, "Invalid partition index\n");
3399 return;
3400 }
3401
3402 ptn = partition_get_offset(index);
3403
3404 if(!ptn)
3405 {
3406 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3407 return;
3408 }
3409
3410 size = partition_get_size(index);
3411
3412 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3413 return;
3414}
3415
3416/*
3417 * Publish the partition type & size info
3418 * fastboot getvar will publish the required information.
3419 * fastboot getvar partition_size:<partition_name>: partition size in hex
3420 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3421 */
3422static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3423{
3424 uint8_t i;
3425
3426 for (i = 0; i < num_parts; i++) {
3427 get_partition_size(info[i].part_name, info[i].size_response);
3428
3429 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3430 {
3431 dprintf(CRITICAL, "partition size name truncated\n");
3432 return;
3433 }
3434 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3435 {
3436 dprintf(CRITICAL, "partition type name truncated\n");
3437 return;
3438 }
3439
3440 /* publish partition size & type info */
3441 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3442 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3443 }
3444}
3445
lijuang4ece1e72015-08-14 21:02:36 +08003446void get_product_name(unsigned char *buf)
3447{
3448 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3449 return;
3450}
3451
3452void get_bootloader_version(unsigned char *buf)
3453{
3454 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3455 return;
3456}
3457
3458void get_baseband_version(unsigned char *buf)
3459{
3460 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3461 return;
3462}
3463
3464bool is_device_locked()
3465{
3466 return device.is_unlocked ? false:true;
3467}
3468
Amol Jadi5edf3552013-07-23 14:15:34 -07003469/* register commands and variables for fastboot */
3470void aboot_fastboot_register_commands(void)
3471{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003472 int i;
lijuangf16461c2015-08-03 17:09:34 +08003473 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003474
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003475 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003476 /* By default the enabled list is empty. */
3477 {"", NULL},
3478 /* move commands enclosed within the below ifndef to here
3479 * if they need to be enabled in user build.
3480 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003481#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003482 /* Register the following commands only for non-user builds */
3483 {"flash:", cmd_flash},
3484 {"erase:", cmd_erase},
3485 {"boot", cmd_boot},
3486 {"continue", cmd_continue},
3487 {"reboot", cmd_reboot},
3488 {"reboot-bootloader", cmd_reboot_bootloader},
3489 {"oem unlock", cmd_oem_unlock},
3490 {"oem unlock-go", cmd_oem_unlock_go},
3491 {"oem lock", cmd_oem_lock},
3492 {"flashing unlock", cmd_oem_unlock},
3493 {"flashing lock", cmd_oem_lock},
3494 {"flashing lock_critical", cmd_flashing_lock_critical},
3495 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3496 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3497 {"oem device-info", cmd_oem_devinfo},
3498 {"preflash", cmd_preflash},
3499 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3500 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003501 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003502 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003503#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003504 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003505#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003506#endif
lijuang511a2b52015-08-14 20:50:51 +08003507 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003508
3509 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3510 for (i = 1; i < fastboot_cmds_count; i++)
3511 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3512
Amol Jadi5edf3552013-07-23 14:15:34 -07003513 /* publish variables and their values */
3514 fastboot_publish("product", TARGET(BOARD));
3515 fastboot_publish("kernel", "lk");
3516 fastboot_publish("serialno", sn_buf);
3517
3518 /*
3519 * partition info is supported only for emmc partitions
3520 * Calling this for NAND prints some error messages which
3521 * is harmless but misleading. Avoid calling this for NAND
3522 * devices.
3523 */
3524 if (target_is_emmc_boot())
3525 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3526
3527 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003528 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3529 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003530 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003531 /* Is the charger screen check enabled */
3532 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3533 device.charger_screen_enabled);
3534 fastboot_publish("charger-screen-enabled",
3535 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003536 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303537 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3538 device.display_panel);
3539 fastboot_publish("display-panel",
3540 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003541 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3542 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003543 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3544 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3545 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3546 target_is_emmc_boot()? "eMMC":"UFS");
3547 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003548#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003549 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003550 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003551 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003552#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003553}
3554
Brian Swetland9c4c0752009-01-25 16:23:50 -08003555void aboot_init(const struct app_descriptor *app)
3556{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003557 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003558
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003559 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003560 if (target_is_emmc_boot())
3561 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003562 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003563 page_mask = page_size - 1;
3564 }
3565 else
3566 {
3567 page_size = flash_page_size();
3568 page_mask = page_size - 1;
3569 }
3570
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003571 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3572
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003573 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303574 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003575
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003576 /* Display splash screen if enabled */
3577#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003578#if NO_ALARM_DISPLAY
3579 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003580#endif
lijuang99c02d82015-02-13 19:04:34 +08003581 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003582#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003583 /* Wait if the display shutdown is in progress */
3584 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003585 if (!pm_appsbl_display_init_done())
3586 target_display_init(device.display_panel);
3587 else
3588 display_image_on_screen();
3589#else
lijuang99c02d82015-02-13 19:04:34 +08003590 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003591#endif
lijuang99c02d82015-02-13 19:04:34 +08003592 dprintf(SPEW, "Display Init: Done\n");
3593#if NO_ALARM_DISPLAY
3594 }
3595#endif
3596#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003597
Greg Griscod6250552011-06-29 14:40:23 -07003598 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003599 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003600
Dhaval Patel223ec952013-07-18 14:49:44 -07003601 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3602
Matthew Qindefd5562014-07-11 18:02:40 +08003603 /*
3604 * Check power off reason if user force reset,
3605 * if yes phone will do normal boot.
3606 */
3607 if (is_user_force_reset())
3608 goto normal_boot;
3609
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003610 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003611 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003612 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003613 dprintf(ALWAYS,"dload mode key sequence detected\n");
3614 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003615 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003616 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003617 }
3618 else
3619 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303620 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003621 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003622 }
3623 boot_into_fastboot = true;
3624 }
3625 if (!boot_into_fastboot)
3626 {
3627 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3628 boot_into_recovery = 1;
3629 if (!boot_into_recovery &&
3630 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003631 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003632 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003633 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003634 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003635 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003636 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003637
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003638#if USE_PON_REBOOT_REG
3639 reboot_mode = check_hard_reboot_mode();
3640#else
Ajay Dudani77421292010-10-27 19:34:06 -07003641 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003642#endif
3643 if (reboot_mode == RECOVERY_MODE)
3644 {
Ajay Dudani77421292010-10-27 19:34:06 -07003645 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003646 }
3647 else if(reboot_mode == FASTBOOT_MODE)
3648 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003649 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003650 }
3651 else if(reboot_mode == ALARM_BOOT)
3652 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003653 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003654 }
Parth Dixit207573a2015-10-27 17:26:21 +05303655#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303656#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003657 else if (reboot_mode == DM_VERITY_ENFORCING)
3658 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003659 device.verity_mode = 1;
3660 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003661 }
3662 else if (reboot_mode == DM_VERITY_LOGGING)
3663 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003664 device.verity_mode = 0;
3665 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003666 }
3667 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3668 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003669 if(send_delete_keys_to_tz())
3670 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003671 }
Parth Dixit207573a2015-10-27 17:26:21 +05303672#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303673#endif
Ajay Dudani77421292010-10-27 19:34:06 -07003674
Matthew Qindefd5562014-07-11 18:02:40 +08003675normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003676 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003677 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003678 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003679 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003680 if(emmc_recovery_init())
3681 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3682 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003683 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003684 if((device.is_unlocked) || (device.is_tampered))
3685 {
3686 #ifdef TZ_TAMPER_FUSE
3687 set_tamper_fuse_cmd();
3688 #endif
3689 #if USE_PCOM_SECBOOT
3690 set_tamper_flag(device.is_tampered);
3691 #endif
3692 }
Shashank Mittala0032282011-08-26 14:50:11 -07003693 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003694
Pavel Nedev5d91d412013-04-29 11:34:24 +03003695 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003696 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003697 else
3698 {
3699 recovery_init();
3700 #if USE_PCOM_SECBOOT
3701 if((device.is_unlocked) || (device.is_tampered))
3702 set_tamper_flag(device.is_tampered);
3703 #endif
3704 boot_linux_from_flash();
3705 }
3706 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3707 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003708 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003709
Amol Jadi5edf3552013-07-23 14:15:34 -07003710 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003711
Amol Jadi5edf3552013-07-23 14:15:34 -07003712 /* register aboot specific fastboot commands */
3713 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003714
Amol Jadi5edf3552013-07-23 14:15:34 -07003715 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003716 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003717
3718 /* initialize and start fastboot */
3719 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003720#if FBCON_DISPLAY_MSG
3721 display_fastboot_menu_thread();
3722#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003723}
3724
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003725uint32_t get_page_size()
3726{
3727 return page_size;
3728}
3729
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003730/*
3731 * Calculated and save hash (SHA256) for non-signed boot image.
3732 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003733 * @param image_addr - Boot image address
3734 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003735 *
3736 * @return int - 0 on success, negative value on failure.
3737 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003738static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003739{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003740 unsigned int digest[8];
3741#if IMAGE_VERIF_ALGO_SHA1
3742 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3743#else
3744 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3745#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003746
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003747 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003748 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003749
3750 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003751 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003752
3753 return 0;
3754}
3755
Brian Swetland9c4c0752009-01-25 16:23:50 -08003756APP_START(aboot)
3757 .init = aboot_init,
3758APP_END