blob: 2b7cc102c5f2950bb7e98c53e84446df162604bb [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);
vijay kumar966a9822015-12-09 18:36:09 +05301471
1472 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1473 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1474 return -1;
1475 }
1476
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001477 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001478
1479 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1480 {
1481 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1482 return -1;
1483 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001484#else
vijay kumar966a9822015-12-09 18:36:09 +05301485 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) {
1486 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1487 return -1;
1488 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001489 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001490#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001491
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001492 dprintf(INFO, "Loading (%s) image (%d): start\n",
1493 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001494 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001495
Shashank Mittalcd98d472011-08-02 14:29:24 -07001496 /* Read image without signature */
1497 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1498 {
1499 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1500 return -1;
1501 }
Dima Zavin214cc642009-01-26 11:16:21 -08001502
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001503 dprintf(INFO, "Loading (%s) image (%d): done\n",
1504 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001505 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001506
Shashank Mittalcd98d472011-08-02 14:29:24 -07001507 offset = imagesize_actual;
1508 /* Read signature */
1509 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1510 {
1511 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001512 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001513 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001514
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301515 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001516
1517 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001518 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1519 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001520#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001521 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001522 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001523 {
1524 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1525 return -1;
1526 }
1527
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001528 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1529#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001530
1531 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001532 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001533 {
1534 write_device_info_flash(&device);
1535 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301536#if USE_PCOM_SECBOOT
1537 set_tamper_flag(device.is_tampered);
1538#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001539 }
1540 else
1541 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001542 offset = page_size;
1543
Amol Jadib6be5c12012-11-14 13:39:51 -08001544 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1545 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1546 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1547
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001548 dprintf(INFO, "Loading (%s) image (%d): start\n",
1549 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1550
Amol Jadi492d5a52013-03-15 16:12:34 -07001551 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001552
1553 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001554 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1555 return -1;
1556 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001557 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001558
Amol Jadib6be5c12012-11-14 13:39:51 -08001559 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001560 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1561 return -1;
1562 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001563 offset += ramdisk_actual;
1564
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001565 dprintf(INFO, "Loading (%s) image (%d): done\n",
1566 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1567
Amol Jadi492d5a52013-03-15 16:12:34 -07001568 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001569
1570 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001571 offset += second_actual;
1572 /* Second image loading not implemented. */
1573 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001574 }
1575
1576#if DEVICE_TREE
1577 if(hdr->dt_size != 0) {
1578
1579 /* Read the device tree table into buffer */
1580 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1581 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1582 return -1;
1583 }
1584
1585 table = (struct dt_table*) dt_buf;
1586
Deepa Dinamani19648b42013-09-05 17:05:55 -07001587 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001588 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1589 return -1;
1590 }
1591
Deepa Dinamani19648b42013-09-05 17:05:55 -07001592 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1593 if (!table)
1594 return -1;
1595
1596 /* Read the entire device tree table into buffer */
1597 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1598 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1599 return -1;
1600 }
1601
1602
Joel Kingaa335dc2013-06-03 16:11:08 -07001603 /* Find index of device tree within device tree table */
1604 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001605 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1606 return -1;
1607 }
1608
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001609 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001610 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001611 {
1612 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1613 return -1;
1614 }
1615
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001616 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001617 if(flash_read(ptn, offset + dt_entry.offset,
1618 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001619 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1620 return -1;
1621 }
1622 }
1623#endif
1624
Shashank Mittalcd98d472011-08-02 14:29:24 -07001625 }
David Ng183a7422009-12-07 14:55:21 -08001626continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001627
Dima Zavin214cc642009-01-26 11:16:21 -08001628 /* TODO: create/pass atags to kernel */
1629
Ajay Dudanie28a6072011-07-01 13:59:46 -07001630 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001631 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001632 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1633
1634 return 0;
1635}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001636
Shashank Mittal162244e2011-08-08 19:01:25 -07001637void write_device_info_mmc(device_info *dev)
1638{
Shashank Mittal162244e2011-08-08 19:01:25 -07001639 unsigned long long ptn = 0;
1640 unsigned long long size;
1641 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001642 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001643 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001644 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001645
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001646 if (devinfo_present)
1647 index = partition_get_index("devinfo");
1648 else
1649 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001650
Shashank Mittal162244e2011-08-08 19:01:25 -07001651 ptn = partition_get_offset(index);
1652 if(ptn == 0)
1653 {
1654 return;
1655 }
1656
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001657 lun = partition_get_lun(index);
1658 mmc_set_lun(lun);
1659
Shashank Mittal162244e2011-08-08 19:01:25 -07001660 size = partition_get_size(index);
1661
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001662 blocksize = mmc_get_device_blocksize();
1663
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001664 if (devinfo_present)
1665 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1666 else
1667 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1668 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001669 {
1670 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001671 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001672 }
1673}
1674
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001675void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001676{
Shashank Mittal162244e2011-08-08 19:01:25 -07001677 unsigned long long ptn = 0;
1678 unsigned long long size;
1679 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001680 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001681 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001682
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001683 if ((index = partition_get_index("devinfo")) < 0)
1684 {
1685 devinfo_present = false;
1686 index = partition_get_index("aboot");
1687 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001688
Shashank Mittal162244e2011-08-08 19:01:25 -07001689 ptn = partition_get_offset(index);
1690 if(ptn == 0)
1691 {
1692 return;
1693 }
1694
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001695 mmc_set_lun(partition_get_lun(index));
1696
Shashank Mittal162244e2011-08-08 19:01:25 -07001697 size = partition_get_size(index);
1698
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001699 blocksize = mmc_get_device_blocksize();
1700
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001701 if (devinfo_present)
1702 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1703 else
1704 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1705 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001706 {
1707 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001708 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001709 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001710}
1711
1712void write_device_info_flash(device_info *dev)
1713{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001714 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001715 struct ptentry *ptn;
1716 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001717 if(info == NULL)
1718 {
1719 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1720 ASSERT(0);
1721 }
1722 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001723 ptable = flash_get_ptable();
1724 if (ptable == NULL)
1725 {
1726 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1727 return;
1728 }
1729
1730 ptn = ptable_find(ptable, "devinfo");
1731 if (ptn == NULL)
1732 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001733 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001734 return;
1735 }
1736
1737 memcpy(info, dev, sizeof(device_info));
1738
1739 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1740 {
1741 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1742 return;
1743 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001744 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001745}
1746
vijay kumarc65876c2015-04-24 13:29:16 +05301747static int read_allow_oem_unlock(device_info *dev)
1748{
vijay kumarc65876c2015-04-24 13:29:16 +05301749 unsigned offset;
1750 int index;
1751 unsigned long long ptn;
1752 unsigned long long ptn_size;
1753 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001754 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301755
vijay kumarca2e6812015-07-08 20:28:25 +05301756 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301757 if (index == INVALID_PTN)
1758 {
vijay kumarca2e6812015-07-08 20:28:25 +05301759 index = partition_get_index(frp_ptns[1]);
1760 if (index == INVALID_PTN)
1761 {
1762 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1763 return -1;
1764 }
vijay kumarc65876c2015-04-24 13:29:16 +05301765 }
1766
1767 ptn = partition_get_offset(index);
1768 ptn_size = partition_get_size(index);
1769 offset = ptn_size - blocksize;
1770
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001771 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301772 {
1773 dprintf(CRITICAL, "Reading MMC failed\n");
1774 return -1;
1775 }
1776
1777 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1778 is_allow_unlock = buf[blocksize-1] & 0x01;
1779 return 0;
1780}
1781
1782static int write_allow_oem_unlock(bool allow_unlock)
1783{
vijay kumarc65876c2015-04-24 13:29:16 +05301784 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301785 int index;
1786 unsigned long long ptn;
1787 unsigned long long ptn_size;
1788 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001789 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301790
vijay kumarca2e6812015-07-08 20:28:25 +05301791 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301792 if (index == INVALID_PTN)
1793 {
vijay kumarca2e6812015-07-08 20:28:25 +05301794 index = partition_get_index(frp_ptns[1]);
1795 if (index == INVALID_PTN)
1796 {
1797 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1798 return -1;
1799 }
vijay kumarc65876c2015-04-24 13:29:16 +05301800 }
1801
1802 ptn = partition_get_offset(index);
1803 ptn_size = partition_get_size(index);
1804 offset = ptn_size - blocksize;
1805
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001806 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301807 {
1808 dprintf(CRITICAL, "Reading MMC failed\n");
1809 return -1;
1810 }
1811
1812 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1813 buf[blocksize-1] = allow_unlock;
1814 if (mmc_write(ptn + offset, blocksize, buf))
1815 {
1816 dprintf(CRITICAL, "Writing MMC failed\n");
1817 return -1;
1818 }
1819
1820 return 0;
1821}
1822
Shashank Mittal162244e2011-08-08 19:01:25 -07001823void read_device_info_flash(device_info *dev)
1824{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001825 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001826 struct ptentry *ptn;
1827 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001828 if(info == NULL)
1829 {
1830 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1831 ASSERT(0);
1832 }
1833 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001834 ptable = flash_get_ptable();
1835 if (ptable == NULL)
1836 {
1837 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1838 return;
1839 }
1840
1841 ptn = ptable_find(ptable, "devinfo");
1842 if (ptn == NULL)
1843 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001844 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001845 return;
1846 }
1847
1848 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1849 {
1850 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1851 return;
1852 }
1853
1854 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1855 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001856 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1857 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001858 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001859 write_device_info_flash(info);
1860 }
1861 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001862 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001863}
1864
1865void write_device_info(device_info *dev)
1866{
1867 if(target_is_emmc_boot())
1868 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001869 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1870 if(info == NULL)
1871 {
1872 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1873 ASSERT(0);
1874 }
1875 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001876 memcpy(info, dev, sizeof(struct device_info));
1877
1878#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001879 if (is_secure_boot_enable()) {
1880 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1881 ASSERT(0);
1882 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001883 else
1884 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001885#else
1886 write_device_info_mmc(info);
1887#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001888 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001889 }
1890 else
1891 {
1892 write_device_info_flash(dev);
1893 }
1894}
1895
1896void read_device_info(device_info *dev)
1897{
1898 if(target_is_emmc_boot())
1899 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001900 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1901 if(info == NULL)
1902 {
1903 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1904 ASSERT(0);
1905 }
1906 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001907
1908#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001909 if (is_secure_boot_enable()) {
1910 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1911 ASSERT(0);
1912 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001913 else
1914 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001915#else
1916 read_device_info_mmc(info);
1917#endif
1918
1919 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1920 {
1921 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08001922 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001923 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301924#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08001925 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301926#endif
lijuang511a2b52015-08-14 20:50:51 +08001927 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001928 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05301929#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08001930 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05301931#endif
lijuang511a2b52015-08-14 20:50:51 +08001932 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001933 info->is_tampered = 0;
1934 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301935#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07001936 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05301937#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001938 write_device_info(info);
1939 }
1940 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001941 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001942 }
1943 else
1944 {
1945 read_device_info_flash(dev);
1946 }
1947}
1948
1949void reset_device_info()
1950{
1951 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001952 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001953 write_device_info(&device);
1954}
1955
1956void set_device_root()
1957{
1958 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001959 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001960 write_device_info(&device);
1961}
1962
lijuang4ece1e72015-08-14 21:02:36 +08001963/* set device unlock value
1964 * Must check FRP before call this function
1965 * Need to wipe data when unlock status changed
1966 * type 0: oem unlock
1967 * type 1: unlock critical
1968 * status 0: unlock as false
1969 * status 1: lock as true
1970 */
1971void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08001972{
lijuang4ece1e72015-08-14 21:02:36 +08001973 if (type == UNLOCK)
1974 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05301975#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08001976 else if (type == UNLOCK_CRITICAL)
1977 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05301978#endif
lijuang4ece1e72015-08-14 21:02:36 +08001979 write_device_info(&device);
1980}
1981
1982static void set_device_unlock(int type, bool status)
1983{
1984 int is_unlocked = -1;
1985 char response[MAX_RSP_SIZE];
1986
1987 /* check device unlock status if it is as expected */
1988 if (type == UNLOCK)
1989 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05301990#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08001991 else if (type == UNLOCK_CRITICAL)
1992 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05301993#endif
lijuang4ece1e72015-08-14 21:02:36 +08001994 if (is_unlocked == status) {
1995 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
1996 fastboot_info(response);
1997 fastboot_okay("");
1998 return;
1999 }
2000
2001 /* status is true, it means to unlock device */
2002 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002003 if(!is_allow_unlock) {
2004 fastboot_fail("oem unlock is not allowed");
2005 return;
2006 }
2007
lijuang4ece1e72015-08-14 21:02:36 +08002008#if FBCON_DISPLAY_MSG
2009 display_unlock_menu_thread(type);
2010 fastboot_okay("");
2011 return;
2012#else
2013 if (type == UNLOCK) {
2014 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2015 return;
2016 }
2017#endif
lijuang21f12f52015-08-22 16:22:19 +08002018 }
lijuang4ece1e72015-08-14 21:02:36 +08002019
2020 set_device_unlock_value(type, status);
2021
2022 /* wipe data */
2023 struct recovery_message msg;
2024
2025 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2026 write_misc(0, &msg, sizeof(msg));
2027
2028 fastboot_okay("");
2029 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002030}
2031
lijuang511a2b52015-08-14 20:50:51 +08002032static bool critical_flash_allowed(const char * entry)
2033{
2034 uint32_t i = 0;
2035 if (entry == NULL)
2036 return false;
2037
2038 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2039 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2040 return true;
2041 }
2042 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002043}
2044
2045#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002046int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2047{
2048 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002049 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002050 struct dt_table *table;
2051 struct dt_entry dt_entry;
2052 uint32_t dt_hdr_size;
2053 unsigned int compressed_size = 0;
2054 unsigned int dtb_size = 0;
2055 unsigned int out_avai_len = 0;
2056 unsigned char *out_addr = NULL;
2057 unsigned char *best_match_dt_addr = NULL;
2058 int rc;
2059
2060 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2061
2062 if(hdr->dt_size != 0) {
2063 /* add kernel offset */
2064 dt_image_offset += page_size;
2065 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002066 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002067
Deepa Dinamani19648b42013-09-05 17:05:55 -07002068 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002069 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2070 dt_image_offset += n;
2071
Joel Kingaa335dc2013-06-03 16:11:08 -07002072 /* add second offset */
2073 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002074 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2075 dt_image_offset += n;
2076 }
2077
Matthew Qin271927e2015-03-31 22:07:07 -04002078 /* offset now point to start of dt.img */
2079 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2080
2081 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2082 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002083 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002084 }
2085 /* Find index of device tree within device tree table */
2086 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2087 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2088 return -1;
Matthew Qin0b15b322015-05-19 05:20:54 -04002089 }
Matthew Qin271927e2015-03-31 22:07:07 -04002090
2091 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2092 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
Matthew Qin0b15b322015-05-19 05:20:54 -04002093 {
Matthew Qin271927e2015-03-31 22:07:07 -04002094 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2095 out_avai_len = target_get_max_flash_size() - scratch_offset;
2096 dprintf(INFO, "decompressing dtb: start\n");
2097 rc = decompress(best_match_dt_addr,
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002098 dt_entry.size, out_addr, out_avai_len,
Matthew Qin271927e2015-03-31 22:07:07 -04002099 &compressed_size, &dtb_size);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002100 if (rc)
2101 {
2102 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
2103 ASSERT(0);
2104 }
Amol Jadicb524072012-08-09 16:40:18 -07002105
Matthew Qin271927e2015-03-31 22:07:07 -04002106 dprintf(INFO, "decompressing dtb: done\n");
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002107 best_match_dt_addr = out_addr;
2108 } else {
Amol Jadicb524072012-08-09 16:40:18 -07002109 dtb_size = dt_entry.size;
2110 }
2111 /* Validate and Read device device tree in the "tags_add */
2112 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
2113 {
2114 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002115 return -1;
2116 }
Amit Blay8a510302015-08-17 09:20:01 +03002117
2118 /* Read device device tree in the "tags_add */
2119 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002120 } else
2121 return -1;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002122
2123 /* Everything looks fine. Return success. */
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002124 return 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002125}
2126#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002127
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002128void cmd_boot(const char *arg, void *data, unsigned sz)
2129{
Matthew Qinbb7923d2015-02-09 10:56:09 +08002130#ifdef MDTP_SUPPORT
2131 static bool is_mdtp_activated = 0;
2132#endif /* MDTP_SUPPORT */
2133 unsigned kernel_actual;
2134 unsigned ramdisk_actual;
2135 uint32_t image_actual;
Matthew Qin271927e2015-03-31 22:07:07 -04002136 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002137 uint32_t sig_actual = SIGNATURE_SIZE;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002138 struct boot_img_hdr *hdr = NULL;
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002139 struct kernel64_hdr *kptr = NULL;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002140 char *ptr = ((char*) data);
2141 int ret = 0;
2142 uint8_t dtb_copied = 0;
2143 unsigned int out_len = 0;
2144 unsigned int out_avai_len = 0;
2145 unsigned char *out_addr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002146 uint32_t dtb_offset = 0;
2147 unsigned char *kernel_start_addr = NULL;
2148 unsigned int kernel_size = 0;
2149 unsigned int scratch_offset = 0;
2150
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002151#if VERIFIED_BOOT
Brian Swetland9c4c0752009-01-25 16:23:50 -08002152 if(target_build_variant_user() && !device.is_unlocked)
2153 {
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002154 fastboot_fail("unlock device to use this command");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002155 return;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002156 }
2157#endif
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002158
2159 if (sz < sizeof(hdr)) {
2160 fastboot_fail("invalid bootimage header");
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002161 return;
2162 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002163
2164 hdr = (struct boot_img_hdr *)data;
2165
2166 /* ensure commandline is terminated */
2167 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2168
2169 if(target_is_emmc_boot() && hdr->page_size) {
2170 page_size = hdr->page_size;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002171 page_mask = page_size - 1;
2172 }
2173
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002174 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2175 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002176#if DEVICE_TREE
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002177 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2178#endif
2179
2180 image_actual = ADD_OF(page_size, kernel_actual);
2181 image_actual = ADD_OF(image_actual, ramdisk_actual);
2182 image_actual = ADD_OF(image_actual, dt_actual);
2183
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002184 if (target_use_signed_kernel() && (!device.is_unlocked))
2185 image_actual = ADD_OF(image_actual, sig_actual);
2186
2187 /* sz should have atleast raw boot image */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002188 if (image_actual > sz) {
Amit Blay4aa292f2015-04-28 21:55:59 +03002189 fastboot_fail("bootimage: incomplete or not signed");
2190 return;
2191 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002192
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002193 // Initialize boot state before trying to verify boot.img
2194#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002195 boot_verifier_init();
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002196 /* Handle overflow if the input image size is greater than
2197 * boot image buffer can hold
2198 */
2199 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2200 {
2201 fastboot_fail("booimage: size is greater than boot image buffer can hold");
2202 return;
2203 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002204#endif
2205
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002206 /* Verify the boot image
2207 * device & page_size are initialized in aboot_init
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002208 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002209 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002210 /* Pass size excluding signature size, otherwise we would try to
2211 * access signature beyond its length
2212 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002213 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002214 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002215#ifdef MDTP_SUPPORT
2216 else
2217 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002218 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002219 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002220
2221 if (!is_mdtp_activated) {
2222 ext_partition.partition = MDTP_PARTITION_NONE;
2223 mdtp_fwlock_verify_lock(&ext_partition);
2224 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002225 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002226
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002227 mdtp_activated(&is_mdtp_activated);
2228 if(is_mdtp_activated){
2229 dprintf(CRITICAL, "fastboot boot command is not available.\n");
2230 return;
2231 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002232#endif /* MDTP_SUPPORT */
2233
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002234#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302235#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002236 // send root of trust
2237 if(!send_rot_command((uint32_t)device.is_unlocked))
2238 ASSERT(0);
2239#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302240#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002241 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002242 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2243 * If not, continue booting.
2244 */
2245 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2246 {
2247 out_addr = (unsigned char *)target_get_scratch_address();
2248 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2249 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002250 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002251 ret = decompress((unsigned char *)(ptr + page_size),
2252 hdr->kernel_size, out_addr, out_avai_len,
2253 &dtb_offset, &out_len);
2254 if (ret)
2255 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002256 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002257 ASSERT(0);
2258 }
2259
Matthew Qin0b15b322015-05-19 05:20:54 -04002260 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002261 kptr = (struct kernel64_hdr *)out_addr;
2262 kernel_start_addr = out_addr;
2263 kernel_size = out_len;
2264 } else {
2265 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2266 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2267 kernel_size = hdr->kernel_size;
2268 }
2269
2270 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002271 * Update the kernel/ramdisk/tags address if the boot image header
2272 * has default values, these default values come from mkbootimg when
2273 * the boot image is flashed using fastboot flash:raw
2274 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002275 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002276
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002277 /* Get virtual addresses since the hdr saves physical addresses. */
2278 hdr->kernel_addr = VA(hdr->kernel_addr);
2279 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2280 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002281
Matthew Qinbb7923d2015-02-09 10:56:09 +08002282 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002283 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002284 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002285 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2286 {
2287 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002288 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002289 }
2290
Amol Jadicb524072012-08-09 16:40:18 -07002291#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002292 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002293 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002294 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002295
2296 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002297#else
2298 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2299 {
2300 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002301 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002302 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002303#endif
2304
2305 /* Load ramdisk & kernel */
2306 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002307 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002308
2309#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002310 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2311 {
2312 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2313 return;
2314 }
2315
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002316 /*
2317 * If dtb is not found look for appended DTB in the kernel.
2318 * If appended dev tree is found, update the atags with
2319 * memory address to the DTB appended location on RAM.
2320 * Else update with the atags address in the kernel header
2321 */
2322 if (!dtb_copied) {
2323 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002324 dtb = dev_tree_appended((void*)(ptr + page_size),
2325 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002326 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002327 if (!dtb) {
2328 fastboot_fail("dtb not found");
2329 return;
2330 }
Amol Jadicb524072012-08-09 16:40:18 -07002331 }
2332#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002333
2334 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002335 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002336
Dima Zavin77e41f32013-03-06 16:10:43 -08002337 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002338 (const char*) hdr->cmdline, board_machtype(),
2339 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002340}
2341
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002342void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002343{
2344 struct ptentry *ptn;
2345 struct ptable *ptable;
2346
2347 ptable = flash_get_ptable();
2348 if (ptable == NULL) {
2349 fastboot_fail("partition table doesn't exist");
2350 return;
2351 }
2352
2353 ptn = ptable_find(ptable, arg);
2354 if (ptn == NULL) {
2355 fastboot_fail("unknown partition name");
2356 return;
2357 }
2358
2359 if (flash_erase(ptn)) {
2360 fastboot_fail("failed to erase partition");
2361 return;
2362 }
2363 fastboot_okay("");
2364}
2365
Bikas Gurungd48bd242010-09-04 19:54:32 -07002366
2367void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2368{
2369 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002370 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002371 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002372 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002373
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002374#if VERIFIED_BOOT
2375 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2376 {
2377 if(!device.is_unlocked)
2378 {
2379 fastboot_fail("unlock device to erase keystore");
2380 return;
2381 }
2382 }
2383#endif
2384
Kinson Chikf1a43512011-07-14 11:28:39 -07002385 index = partition_get_index(arg);
2386 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002387 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002388
Kinson Chikf1a43512011-07-14 11:28:39 -07002389 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002390 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002391 return;
2392 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002393
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002394 lun = partition_get_lun(index);
2395 mmc_set_lun(lun);
2396
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002397 if (platform_boot_dev_isemmc())
2398 {
2399 if (mmc_erase_card(ptn, size)) {
2400 fastboot_fail("failed to erase partition\n");
2401 return;
2402 }
2403 } else {
2404 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2405 size = partition_get_size(index);
2406 if (size > DEFAULT_ERASE_SIZE)
2407 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002408
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002409 /* Simple inefficient version of erase. Just writing
2410 0 in first several blocks */
2411 if (mmc_write(ptn , size, (unsigned int *)out)) {
2412 fastboot_fail("failed to erase partition");
2413 return;
2414 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002415 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002416#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302417#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002418 if(!(strncmp(arg, "userdata", 8)))
2419 if(send_delete_keys_to_tz())
2420 ASSERT(0);
2421#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302422#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002423 fastboot_okay("");
2424}
2425
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002426void cmd_erase(const char *arg, void *data, unsigned sz)
2427{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002428#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002429 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002430 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002431 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002432 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002433 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002434 return;
2435 }
2436 }
2437#endif
2438
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002439 if(target_is_emmc_boot())
2440 cmd_erase_mmc(arg, data, sz);
2441 else
2442 cmd_erase_nand(arg, data, sz);
2443}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002444
Channagoud Kadabiad259832015-05-29 11:14:17 -07002445static uint32_t aboot_get_secret_key()
2446{
2447 /* 0 is invalid secret key, update this implementation to return
2448 * device specific unique secret key
2449 */
2450 return 0;
2451}
2452
Ajay Dudani5c761132011-04-07 20:19:04 -07002453void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002454{
2455 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002456 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002457 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002458 char *token = NULL;
2459 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002460 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002461 uint8_t lun = 0;
2462 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002463
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002464 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002465 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002466 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002467 if(token)
2468 {
2469 lun = atoi(token);
2470 mmc_set_lun(lun);
2471 lun_set = true;
2472 }
2473
Mao Jinlong226f33a2014-07-04 17:24:10 +08002474 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002475 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002476 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2477 {
2478 if (!aboot_frp_unlock(pname, data, sz))
2479 {
2480 fastboot_info("FRP unlock successful");
2481 fastboot_okay("");
2482 }
2483 else
2484 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2485
2486 return;
2487 }
2488
Mao Jinlong226f33a2014-07-04 17:24:10 +08002489 if (!strcmp(pname, "partition"))
2490 {
2491 dprintf(INFO, "Attempt to write partition image.\n");
2492 if (write_partition(sz, (unsigned char *) data)) {
2493 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002494 return;
2495 }
2496 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002497 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002498 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002499#if VERIFIED_BOOT
2500 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2501 {
2502 if(!device.is_unlocked)
2503 {
2504 fastboot_fail("unlock device to flash keystore");
2505 return;
2506 }
2507 if(!boot_verify_validate_keystore((unsigned char *)data))
2508 {
2509 fastboot_fail("image is not a keystore file");
2510 return;
2511 }
2512 }
2513#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002514 index = partition_get_index(pname);
2515 ptn = partition_get_offset(index);
2516 if(ptn == 0) {
2517 fastboot_fail("partition table doesn't exist");
2518 return;
2519 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002520
Mao Jinlong226f33a2014-07-04 17:24:10 +08002521 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2522 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2523 fastboot_fail("image is not a boot image");
2524 return;
2525 }
2526 }
2527
2528 if(!lun_set)
2529 {
2530 lun = partition_get_lun(index);
2531 mmc_set_lun(lun);
2532 }
2533
2534 size = partition_get_size(index);
2535 if (ROUND_TO_PAGE(sz,511) > size) {
2536 fastboot_fail("size too large");
2537 return;
2538 }
2539 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2540 fastboot_fail("flash write failure");
2541 return;
2542 }
Greg Grisco6e754772011-06-23 12:19:39 -07002543 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002544 }
2545 fastboot_okay("");
2546 return;
2547}
2548
Ajay Dudanide984792015-03-02 09:57:41 -08002549void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2550{
2551 int i, images;
2552 meta_header_t *meta_header;
2553 img_header_entry_t *img_header_entry;
2554
2555 meta_header = (meta_header_t*) data;
2556 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2557
2558 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2559
2560 for (i=0; i<images; i++) {
2561
2562 if((img_header_entry[i].ptn_name == NULL) ||
2563 (img_header_entry[i].start_offset == 0) ||
2564 (img_header_entry[i].size == 0))
2565 break;
2566
2567 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2568 (void *) data + img_header_entry[i].start_offset,
2569 img_header_entry[i].size);
2570 }
2571
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002572 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2573 {
2574 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2575 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2576 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2577 }
2578 else
2579 {
2580 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2581 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2582 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2583 }
2584
2585 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002586 fastboot_okay("");
2587 return;
2588}
2589
Ajay Dudani5c761132011-04-07 20:19:04 -07002590void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2591{
2592 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002593 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002594 uint32_t *fill_buf = NULL;
2595 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002596 sparse_header_t *sparse_header;
2597 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002598 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002599 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302600 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002601 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302602 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002603 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302604 /*End of the sparse image address*/
2605 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002606
Kinson Chikf1a43512011-07-14 11:28:39 -07002607 index = partition_get_index(arg);
2608 ptn = partition_get_offset(index);
2609 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002610 fastboot_fail("partition table doesn't exist");
2611 return;
2612 }
2613
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302614 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302615
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002616 lun = partition_get_lun(index);
2617 mmc_set_lun(lun);
2618
vijay kumar800255e2015-04-24 20:26:19 +05302619 if (sz < sizeof(sparse_header_t)) {
2620 fastboot_fail("size too low");
2621 return;
2622 }
2623
Ajay Dudani5c761132011-04-07 20:19:04 -07002624 /* Read and skip over sparse image header */
2625 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302626
vijay kumar1321f342015-03-27 12:13:42 +05302627 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002628 fastboot_fail("size too large");
2629 return;
2630 }
2631
vijay kumarde4fcf62015-04-23 13:05:49 +05302632 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302633
2634 if (data_end < (uint32_t)data) {
2635 fastboot_fail("buffer overreads occured due to invalid sparse header");
2636 return;
2637 }
2638
vijay kumarde4fcf62015-04-23 13:05:49 +05302639 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002640 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302641 fastboot_fail("sparse header size mismatch");
2642 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002643 }
2644
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002645 dprintf (SPEW, "=== Sparse Image Header ===\n");
2646 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2647 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2648 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2649 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2650 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2651 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2652 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2653 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002654
2655 /* Start processing chunks */
2656 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2657 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302658 /* Make sure the total image size does not exceed the partition size */
2659 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2660 fastboot_fail("size too large");
2661 return;
2662 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002663 /* Read and skip over chunk header */
2664 chunk_header = (chunk_header_t *) data;
2665 data += sizeof(chunk_header_t);
2666
vijay kumar800255e2015-04-24 20:26:19 +05302667 if (data_end < (uint32_t)data) {
2668 fastboot_fail("buffer overreads occured due to invalid sparse header");
2669 return;
2670 }
2671
Ajay Dudani5c761132011-04-07 20:19:04 -07002672 dprintf (SPEW, "=== Chunk Header ===\n");
2673 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2674 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2675 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2676
vijay kumar800255e2015-04-24 20:26:19 +05302677 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002678 {
vijay kumar800255e2015-04-24 20:26:19 +05302679 fastboot_fail("chunk header size mismatch");
2680 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002681 }
2682
wufeng.jiang813dc352015-06-02 23:02:46 -04002683 if (!sparse_header->blk_sz ){
2684 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302685 return;
2686 }
2687
wufeng.jiang813dc352015-06-02 23:02:46 -04002688 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2689
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302690 /* Make sure that the chunk size calculated from sparse image does not
2691 * exceed partition size
2692 */
2693 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2694 {
2695 fastboot_fail("Chunk data size exceeds partition size");
2696 return;
2697 }
2698
Ajay Dudani5c761132011-04-07 20:19:04 -07002699 switch (chunk_header->chunk_type)
2700 {
2701 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002702 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002703 chunk_data_sz))
2704 {
2705 fastboot_fail("Bogus chunk size for chunk type Raw");
2706 return;
2707 }
2708
vijay kumar800255e2015-04-24 20:26:19 +05302709 if (data_end < (uint32_t)data + chunk_data_sz) {
2710 fastboot_fail("buffer overreads occured due to invalid sparse header");
2711 return;
2712 }
2713
wufeng.jiang813dc352015-06-02 23:02:46 -04002714 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2715 otherwise it will return in the line above
2716 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002717 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002718 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002719 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002720 {
2721 fastboot_fail("flash write failure");
2722 return;
2723 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302724 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2725 fastboot_fail("Bogus size for RAW chunk type");
2726 return;
2727 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002728 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002729 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002730 break;
2731
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002732 case CHUNK_TYPE_FILL:
2733 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2734 sizeof(uint32_t)))
2735 {
2736 fastboot_fail("Bogus chunk size for chunk type FILL");
2737 return;
2738 }
2739
2740 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2741 if (!fill_buf)
2742 {
2743 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2744 return;
2745 }
2746
vijay kumar800255e2015-04-24 20:26:19 +05302747 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2748 fastboot_fail("buffer overreads occured due to invalid sparse header");
2749 return;
2750 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002751 fill_val = *(uint32_t *)data;
2752 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002753
2754 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2755 {
2756 fill_buf[i] = fill_val;
2757 }
2758
wufeng.jiang813dc352015-06-02 23:02:46 -04002759 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002760 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302761 /* Make sure that the data written to partition does not exceed partition size */
2762 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2763 {
2764 fastboot_fail("Chunk data size for fill type exceeds partition size");
2765 return;
2766 }
2767
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002768 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2769 sparse_header->blk_sz,
2770 fill_buf))
2771 {
2772 fastboot_fail("flash write failure");
2773 free(fill_buf);
2774 return;
2775 }
2776
2777 total_blocks++;
2778 }
2779
2780 free(fill_buf);
2781 break;
2782
Ajay Dudani5c761132011-04-07 20:19:04 -07002783 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302784 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2785 fastboot_fail("bogus size for chunk DONT CARE type");
2786 return;
2787 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002788 total_blocks += chunk_header->chunk_sz;
2789 break;
2790
Ajay Dudani5c761132011-04-07 20:19:04 -07002791 case CHUNK_TYPE_CRC:
2792 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2793 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002794 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002795 return;
2796 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302797 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2798 fastboot_fail("bogus size for chunk CRC type");
2799 return;
2800 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002801 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302802 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2803 fastboot_fail("integer overflow occured");
2804 return;
2805 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002806 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302807 if (data_end < (uint32_t)data) {
2808 fastboot_fail("buffer overreads occured due to invalid sparse header");
2809 return;
2810 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002811 break;
2812
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002813 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002814 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002815 fastboot_fail("Unknown chunk type");
2816 return;
2817 }
2818 }
2819
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002820 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2821 total_blocks, sparse_header->total_blks);
2822
2823 if(total_blocks != sparse_header->total_blks)
2824 {
2825 fastboot_fail("sparse image write failure");
2826 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002827
2828 fastboot_okay("");
2829 return;
2830}
2831
2832void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2833{
2834 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002835 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002836
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002837#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002838 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2839 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002840 int ret=0;
2841 uint32 major_version=0;
2842 uint32 minor_version=0;
2843
2844 ret = scm_svc_version(&major_version,&minor_version);
2845 if(!ret)
2846 {
2847 if(major_version >= 2)
2848 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002849 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002850 {
2851 ret = encrypt_scm((uint32 **) &data, &sz);
2852 if (ret != 0) {
2853 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2854 return;
2855 }
2856
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002857 /* Protect only for SSD */
2858 if (!strcmp(arg, "ssd")) {
2859 ret = scm_protect_keystore((uint32 *) data, sz);
2860 if (ret != 0) {
2861 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2862 return;
2863 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002864 }
2865 }
2866 else
2867 {
2868 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2869 if(ret != 0)
2870 {
2871 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2872 return;
2873 }
2874 }
2875 }
2876 else
2877 {
2878 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2879 magic_number[1] == DECRYPT_MAGIC_1)
2880 {
2881 ret = decrypt_scm((uint32 **) &data, &sz);
2882 if (ret != 0) {
2883 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2884 return;
2885 }
2886 }
2887 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2888 magic_number[1] == ENCRYPT_MAGIC_1)
2889 {
2890 ret = encrypt_scm((uint32 **) &data, &sz);
2891 if (ret != 0) {
2892 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2893 return;
2894 }
2895 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002896 }
2897 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002898 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002899 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002900 dprintf(CRITICAL,"INVALID SVC Version\n");
2901 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002902 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002903#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002904
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002905#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002906 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002907 {
lijuang511a2b52015-08-14 20:50:51 +08002908 /* if device is locked:
2909 * common partition will not allow to be flashed
2910 * critical partition will allow to flash image.
2911 */
2912 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
2913 fastboot_fail("Partition flashing is not allowed");
2914 return;
2915 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302916#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002917 /* if device critical is locked:
2918 * common partition will allow to be flashed
2919 * critical partition will not allow to flash image.
2920 */
2921 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
2922 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002923 return;
2924 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302925#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002926 }
2927#endif
2928
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002929 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002930 meta_header = (meta_header_t *) data;
2931 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002932 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002933 else if (meta_header->magic == META_HEADER_MAGIC)
2934 cmd_flash_meta_img(arg, data, sz);
2935 else
2936 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002937
2938#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05302939#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002940 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
2941 {
2942 // reset dm_verity mode to enforcing
2943 device.verity_mode = 1;
2944 write_device_info(&device);
2945 }
2946#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05302947#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002948
Ajay Dudani5c761132011-04-07 20:19:04 -07002949 return;
2950}
2951
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002952void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2953{
2954 struct ptentry *sys_ptn;
2955 struct ptable *ptable;
2956
2957 ptable = flash_get_ptable();
2958 if (ptable == NULL) {
2959 fastboot_fail("partition table doesn't exist");
2960 return;
2961 }
2962
2963 sys_ptn = ptable_find(ptable, "system");
2964 if (sys_ptn == NULL) {
2965 fastboot_fail("system partition not found");
2966 return;
2967 }
2968
2969 sz = ROUND_TO_PAGE(sz, page_mask);
2970 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2971 fastboot_fail("update_ubi_vol failed");
2972 else
2973 fastboot_okay("");
2974}
2975
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002976void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002977{
2978 struct ptentry *ptn;
2979 struct ptable *ptable;
2980 unsigned extra = 0;
2981
2982 ptable = flash_get_ptable();
2983 if (ptable == NULL) {
2984 fastboot_fail("partition table doesn't exist");
2985 return;
2986 }
2987
2988 ptn = ptable_find(ptable, arg);
2989 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002990 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2991 arg);
2992 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002993 return;
2994 }
2995
2996 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2997 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2998 fastboot_fail("image is not a boot image");
2999 return;
3000 }
3001 }
3002
Amol Jadi5c61a952012-05-04 17:05:35 -07003003 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003004 || !strcmp(ptn->name, "userdata")
3005 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003006 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003007 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003008 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003009 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003010 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003011
3012 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003013 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3014 if (flash_ubi_img(ptn, data, sz)) {
3015 fastboot_fail("flash write failure");
3016 return;
3017 }
3018 } else {
3019 if (flash_write(ptn, extra, data, sz)) {
3020 fastboot_fail("flash write failure");
3021 return;
3022 }
Dima Zavin214cc642009-01-26 11:16:21 -08003023 }
3024 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3025 fastboot_okay("");
3026}
3027
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003028void cmd_flash(const char *arg, void *data, unsigned sz)
3029{
3030 if(target_is_emmc_boot())
3031 cmd_flash_mmc(arg, data, sz);
3032 else
3033 cmd_flash_nand(arg, data, sz);
3034}
3035
Dima Zavin214cc642009-01-26 11:16:21 -08003036void cmd_continue(const char *arg, void *data, unsigned sz)
3037{
3038 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003039 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003040
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003041 if (target_is_emmc_boot())
3042 {
lijuanga40d6302015-07-20 20:10:13 +08003043#if FBCON_DISPLAY_MSG
3044 keys_detect_init();
3045#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003046 boot_linux_from_mmc();
3047 }
3048 else
3049 {
3050 boot_linux_from_flash();
3051 }
Dima Zavin214cc642009-01-26 11:16:21 -08003052}
3053
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003054void cmd_reboot(const char *arg, void *data, unsigned sz)
3055{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003056 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003057 fastboot_okay("");
3058 reboot_device(0);
3059}
3060
3061void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3062{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003063 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003064 fastboot_okay("");
3065 reboot_device(FASTBOOT_MODE);
3066}
3067
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003068void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3069{
3070 dprintf(INFO, "Enabling charger screen check\n");
3071 device.charger_screen_enabled = 1;
3072 write_device_info(&device);
3073 fastboot_okay("");
3074}
3075
3076void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3077{
3078 dprintf(INFO, "Disabling charger screen check\n");
3079 device.charger_screen_enabled = 0;
3080 write_device_info(&device);
3081 fastboot_okay("");
3082}
3083
lijuanga25d8bb2015-09-16 13:11:52 +08003084void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3085{
3086 char *p = NULL;
3087 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303088 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003089
3090 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303091 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003092 if (p) {
3093 if (!strncmp(p, "0", 1)) {
3094 device.charger_screen_enabled = 0;
3095 } else if (!strncmp(p, "1", 1)) {
3096 device.charger_screen_enabled = 1;
3097 }
3098 }
3099 }
3100
3101 /* update charger_screen_enabled value for getvar
3102 * command
3103 */
3104 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3105 device.charger_screen_enabled);
3106
3107 write_device_info(&device);
3108 fastboot_okay("");
3109}
3110
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303111void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3112{
3113 dprintf(INFO, "Selecting display panel %s\n", arg);
3114 if (arg)
3115 strlcpy(device.display_panel, arg,
3116 sizeof(device.display_panel));
3117 write_device_info(&device);
3118 fastboot_okay("");
3119}
3120
Shashank Mittal162244e2011-08-08 19:01:25 -07003121void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3122{
lijuang4ece1e72015-08-14 21:02:36 +08003123 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303124}
3125
3126void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3127{
lijuang4ece1e72015-08-14 21:02:36 +08003128 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303129 if(!is_allow_unlock) {
3130 fastboot_fail("oem unlock is not allowed");
3131 return;
3132 }
3133
lijuang4ece1e72015-08-14 21:02:36 +08003134 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303135
lijuang4ece1e72015-08-14 21:02:36 +08003136 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303137 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003138
vijay kumarc65876c2015-04-24 13:29:16 +05303139 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3140 write_misc(0, &msg, sizeof(msg));
3141
3142 fastboot_okay("");
3143 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003144 }
3145 fastboot_okay("");
3146}
3147
Channagoud Kadabiad259832015-05-29 11:14:17 -07003148static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3149{
3150 int ret = 1;
3151 uint32_t secret_key;
3152 char seckey_buffer[MAX_RSP_SIZE];
3153
3154 secret_key = aboot_get_secret_key();
3155 if (secret_key)
3156 {
3157 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3158 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3159 {
3160 is_allow_unlock = true;
3161 write_allow_oem_unlock(is_allow_unlock);
3162 ret = 0;
3163 }
3164 }
3165 return ret;
3166}
3167
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003168void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3169{
lijuang4ece1e72015-08-14 21:02:36 +08003170 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003171}
3172
Shashank Mittala0032282011-08-26 14:50:11 -07003173void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3174{
lijuang511a2b52015-08-14 20:50:51 +08003175 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003176 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003177 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003178 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3179 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303180#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003181 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3182 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303183#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003184 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003185 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303186 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3187 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003188 fastboot_okay("");
3189}
3190
lijuang511a2b52015-08-14 20:50:51 +08003191void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3192{
3193 char response[MAX_RSP_SIZE];
3194 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3195 fastboot_info(response);
3196 fastboot_okay("");
3197}
3198
3199void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3200{
lijuang4ece1e72015-08-14 21:02:36 +08003201 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003202}
3203
3204void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3205{
lijuang4ece1e72015-08-14 21:02:36 +08003206 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003207}
3208
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003209void cmd_preflash(const char *arg, void *data, unsigned sz)
3210{
3211 fastboot_okay("");
3212}
3213
Mao Flynn7b379f32015-04-20 00:28:30 +08003214static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303215
Mao Flynn7b379f32015-04-20 00:28:30 +08003216int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303217{
Mao Flynn7b379f32015-04-20 00:28:30 +08003218 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303219 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003220 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303221 return -1;
3222 return 0;
3223}
3224
Mao Flynn7b379f32015-04-20 00:28:30 +08003225int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003226{
3227 struct ptentry *ptn;
3228 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003229 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003230 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003231
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303232 ptable = flash_get_ptable();
3233 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003234 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3235 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303236 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003237
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303238 ptn = ptable_find(ptable, "splash");
3239 if (ptn == NULL) {
3240 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003241 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303242 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003243 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303244 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003245 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303246 }
3247
Mao Flynn7b379f32015-04-20 00:28:30 +08003248 header = (struct logo_img_header *)logo_header;
3249 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303250 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003251 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303252 }
3253
3254 fb_display = fbcon_display();
3255 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003256 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3257 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3258
3259 /* if the logo is full-screen size, remove "fbcon_clear()" */
3260 if ((header->width != fb_display->width)
3261 || (header->height != fb_display->height))
3262 fbcon_clear();
3263
3264 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3265 (uint32_t *)base,
3266 (header->blocks * 512))) {
3267 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3268 return -1;
3269 }
3270 fbcon_extract_to_screen(header, base);
3271 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003272 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003273
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003274 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3275 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003276 return -1;
3277 }
3278
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303279 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003280 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3281 (uint32_t *)base,
3282 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303283 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303284 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003285 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003286 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303287 }
3288
Mao Flynn7b379f32015-04-20 00:28:30 +08003289 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303290}
3291
Mao Flynn7b379f32015-04-20 00:28:30 +08003292int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303293{
3294 int index = INVALID_PTN;
3295 unsigned long long ptn = 0;
3296 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003297 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003298 uint32_t blocksize, realsize, readsize;
3299 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303300
3301 index = partition_get_index("splash");
3302 if (index == 0) {
3303 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003304 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303305 }
3306
3307 ptn = partition_get_offset(index);
3308 if (ptn == 0) {
3309 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003310 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303311 }
3312
Mao Flynn1893a7f2015-06-03 12:03:36 +08003313 mmc_set_lun(partition_get_lun(index));
3314
3315 blocksize = mmc_get_device_blocksize();
3316 if (blocksize == 0) {
3317 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3318 return -1;
3319 }
3320
3321 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003322 if (!fb_display)
3323 {
3324 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3325 return -1;
3326 }
3327
Mao Flynn1893a7f2015-06-03 12:03:36 +08003328 base = (uint8_t *) fb_display->base;
3329
3330 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303331 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003332 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303333 }
3334
Mao Flynn1893a7f2015-06-03 12:03:36 +08003335 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003336 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303337 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003338 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303339 }
3340
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303341 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003342 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3343 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003344
Mao Flynn1893a7f2015-06-03 12:03:36 +08003345 realsize = header->blocks * 512;
3346 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3347
3348 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003349 if ((header->width != fb_display->width)
3350 || (header->height != fb_display->height))
3351 fbcon_clear();
3352
Mao Flynn1893a7f2015-06-03 12:03:36 +08003353 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003354 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3355 return -1;
3356 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003357
Mao Flynn1893a7f2015-06-03 12:03:36 +08003358 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3359 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303360
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003361 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3362 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003363 return -1;
3364 }
3365
3366 realsize = header->width * header->height * fb_display->bpp / 8;
3367 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3368
3369 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3370 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3371 fbcon_clear();
3372 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3373 return -1;
3374 }
3375 } else {
3376 if (mmc_read(ptn + blocksize ,
3377 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3378 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3379 return -1;
3380 }
3381 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3382 }
3383 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303384 }
3385
Mao Flynn7b379f32015-04-20 00:28:30 +08003386 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303387}
3388
Mao Flynn7b379f32015-04-20 00:28:30 +08003389int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303390{
3391 if (target_is_emmc_boot()) {
3392 return splash_screen_mmc();
3393 } else {
3394 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003395 }
3396}
3397
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003398/* Get the size from partiton name */
3399static void get_partition_size(const char *arg, char *response)
3400{
3401 uint64_t ptn = 0;
3402 uint64_t size;
3403 int index = INVALID_PTN;
3404
3405 index = partition_get_index(arg);
3406
3407 if (index == INVALID_PTN)
3408 {
3409 dprintf(CRITICAL, "Invalid partition index\n");
3410 return;
3411 }
3412
3413 ptn = partition_get_offset(index);
3414
3415 if(!ptn)
3416 {
3417 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3418 return;
3419 }
3420
3421 size = partition_get_size(index);
3422
3423 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3424 return;
3425}
3426
3427/*
3428 * Publish the partition type & size info
3429 * fastboot getvar will publish the required information.
3430 * fastboot getvar partition_size:<partition_name>: partition size in hex
3431 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3432 */
3433static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3434{
3435 uint8_t i;
3436
3437 for (i = 0; i < num_parts; i++) {
3438 get_partition_size(info[i].part_name, info[i].size_response);
3439
3440 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3441 {
3442 dprintf(CRITICAL, "partition size name truncated\n");
3443 return;
3444 }
3445 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3446 {
3447 dprintf(CRITICAL, "partition type name truncated\n");
3448 return;
3449 }
3450
3451 /* publish partition size & type info */
3452 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3453 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3454 }
3455}
3456
lijuang4ece1e72015-08-14 21:02:36 +08003457void get_product_name(unsigned char *buf)
3458{
3459 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3460 return;
3461}
3462
3463void get_bootloader_version(unsigned char *buf)
3464{
3465 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3466 return;
3467}
3468
3469void get_baseband_version(unsigned char *buf)
3470{
3471 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3472 return;
3473}
3474
3475bool is_device_locked()
3476{
3477 return device.is_unlocked ? false:true;
3478}
3479
Amol Jadi5edf3552013-07-23 14:15:34 -07003480/* register commands and variables for fastboot */
3481void aboot_fastboot_register_commands(void)
3482{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003483 int i;
lijuangf16461c2015-08-03 17:09:34 +08003484 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003485
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003486 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003487 /* By default the enabled list is empty. */
3488 {"", NULL},
3489 /* move commands enclosed within the below ifndef to here
3490 * if they need to be enabled in user build.
3491 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003492#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003493 /* Register the following commands only for non-user builds */
3494 {"flash:", cmd_flash},
3495 {"erase:", cmd_erase},
3496 {"boot", cmd_boot},
3497 {"continue", cmd_continue},
3498 {"reboot", cmd_reboot},
3499 {"reboot-bootloader", cmd_reboot_bootloader},
3500 {"oem unlock", cmd_oem_unlock},
3501 {"oem unlock-go", cmd_oem_unlock_go},
3502 {"oem lock", cmd_oem_lock},
3503 {"flashing unlock", cmd_oem_unlock},
3504 {"flashing lock", cmd_oem_lock},
3505 {"flashing lock_critical", cmd_flashing_lock_critical},
3506 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3507 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3508 {"oem device-info", cmd_oem_devinfo},
3509 {"preflash", cmd_preflash},
3510 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3511 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003512 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003513 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003514#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003515 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003516#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003517#endif
lijuang511a2b52015-08-14 20:50:51 +08003518 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003519
3520 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3521 for (i = 1; i < fastboot_cmds_count; i++)
3522 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3523
Amol Jadi5edf3552013-07-23 14:15:34 -07003524 /* publish variables and their values */
3525 fastboot_publish("product", TARGET(BOARD));
3526 fastboot_publish("kernel", "lk");
3527 fastboot_publish("serialno", sn_buf);
3528
3529 /*
3530 * partition info is supported only for emmc partitions
3531 * Calling this for NAND prints some error messages which
3532 * is harmless but misleading. Avoid calling this for NAND
3533 * devices.
3534 */
3535 if (target_is_emmc_boot())
3536 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3537
3538 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003539 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3540 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003541 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003542 /* Is the charger screen check enabled */
3543 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3544 device.charger_screen_enabled);
3545 fastboot_publish("charger-screen-enabled",
3546 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003547 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303548 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3549 device.display_panel);
3550 fastboot_publish("display-panel",
3551 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003552 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3553 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003554 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3555 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3556 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3557 target_is_emmc_boot()? "eMMC":"UFS");
3558 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003559#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003560 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003561 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003562 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003563#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003564}
3565
Brian Swetland9c4c0752009-01-25 16:23:50 -08003566void aboot_init(const struct app_descriptor *app)
3567{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003568 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003569
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003570 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003571 if (target_is_emmc_boot())
3572 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003573 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003574 page_mask = page_size - 1;
3575 }
3576 else
3577 {
3578 page_size = flash_page_size();
3579 page_mask = page_size - 1;
3580 }
3581
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003582 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3583
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003584 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303585 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003586
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003587 /* Display splash screen if enabled */
3588#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003589#if NO_ALARM_DISPLAY
3590 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003591#endif
lijuang99c02d82015-02-13 19:04:34 +08003592 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003593#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003594 /* Wait if the display shutdown is in progress */
3595 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003596 if (!pm_appsbl_display_init_done())
3597 target_display_init(device.display_panel);
3598 else
3599 display_image_on_screen();
3600#else
lijuang99c02d82015-02-13 19:04:34 +08003601 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003602#endif
lijuang99c02d82015-02-13 19:04:34 +08003603 dprintf(SPEW, "Display Init: Done\n");
3604#if NO_ALARM_DISPLAY
3605 }
3606#endif
3607#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003608
Greg Griscod6250552011-06-29 14:40:23 -07003609 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003610 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003611
Dhaval Patel223ec952013-07-18 14:49:44 -07003612 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3613
Matthew Qindefd5562014-07-11 18:02:40 +08003614 /*
3615 * Check power off reason if user force reset,
3616 * if yes phone will do normal boot.
3617 */
3618 if (is_user_force_reset())
3619 goto normal_boot;
3620
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003621 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003622 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003623 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003624 dprintf(ALWAYS,"dload mode key sequence detected\n");
3625 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003626 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003627 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003628 }
3629 else
3630 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303631 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003632 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003633 }
3634 boot_into_fastboot = true;
3635 }
3636 if (!boot_into_fastboot)
3637 {
3638 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3639 boot_into_recovery = 1;
3640 if (!boot_into_recovery &&
3641 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003642 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003643 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003644 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003645 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003646 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003647 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003648
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003649#if USE_PON_REBOOT_REG
3650 reboot_mode = check_hard_reboot_mode();
3651#else
Ajay Dudani77421292010-10-27 19:34:06 -07003652 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003653#endif
3654 if (reboot_mode == RECOVERY_MODE)
3655 {
Ajay Dudani77421292010-10-27 19:34:06 -07003656 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003657 }
3658 else if(reboot_mode == FASTBOOT_MODE)
3659 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003660 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003661 }
3662 else if(reboot_mode == ALARM_BOOT)
3663 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003664 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003665 }
Parth Dixit207573a2015-10-27 17:26:21 +05303666#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303667#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003668 else if (reboot_mode == DM_VERITY_ENFORCING)
3669 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003670 device.verity_mode = 1;
3671 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003672 }
3673 else if (reboot_mode == DM_VERITY_LOGGING)
3674 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003675 device.verity_mode = 0;
3676 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003677 }
3678 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3679 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003680 if(send_delete_keys_to_tz())
3681 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003682 }
Parth Dixit207573a2015-10-27 17:26:21 +05303683#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303684#endif
Ajay Dudani77421292010-10-27 19:34:06 -07003685
Matthew Qindefd5562014-07-11 18:02:40 +08003686normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003687 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003688 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003689 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003690 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003691 if(emmc_recovery_init())
3692 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3693 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003694 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003695 if((device.is_unlocked) || (device.is_tampered))
3696 {
3697 #ifdef TZ_TAMPER_FUSE
3698 set_tamper_fuse_cmd();
3699 #endif
3700 #if USE_PCOM_SECBOOT
3701 set_tamper_flag(device.is_tampered);
3702 #endif
3703 }
Shashank Mittala0032282011-08-26 14:50:11 -07003704 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003705
Pavel Nedev5d91d412013-04-29 11:34:24 +03003706 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003707 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003708 else
3709 {
3710 recovery_init();
3711 #if USE_PCOM_SECBOOT
3712 if((device.is_unlocked) || (device.is_tampered))
3713 set_tamper_flag(device.is_tampered);
3714 #endif
3715 boot_linux_from_flash();
3716 }
3717 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3718 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003719 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003720
Amol Jadi5edf3552013-07-23 14:15:34 -07003721 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003722
Amol Jadi5edf3552013-07-23 14:15:34 -07003723 /* register aboot specific fastboot commands */
3724 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003725
Amol Jadi5edf3552013-07-23 14:15:34 -07003726 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003727 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003728
3729 /* initialize and start fastboot */
3730 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003731#if FBCON_DISPLAY_MSG
3732 display_fastboot_menu_thread();
3733#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003734}
3735
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003736uint32_t get_page_size()
3737{
3738 return page_size;
3739}
3740
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003741/*
3742 * Calculated and save hash (SHA256) for non-signed boot image.
3743 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003744 * @param image_addr - Boot image address
3745 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003746 *
3747 * @return int - 0 on success, negative value on failure.
3748 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003749static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003750{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003751 unsigned int digest[8];
3752#if IMAGE_VERIF_ALGO_SHA1
3753 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3754#else
3755 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3756#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003757
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003758 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003759 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003760
3761 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003762 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003763
3764 return 0;
3765}
3766
Brian Swetland9c4c0752009-01-25 16:23:50 -08003767APP_START(aboot)
3768 .init = aboot_init,
3769APP_END