blob: a0c8ad05187d53b96136bdbe3d5ec62acd48b329 [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>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080062#if USE_RPMB_FOR_DEVINFO
63#include <rpmb.h>
64#endif
Dima Zavin214cc642009-01-26 11:16:21 -080065
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070066#if ENABLE_WBC
67#include <pm_app_smbchg.h>
68#endif
69
Neeti Desai17379b82012-06-04 18:42:53 -070070#if DEVICE_TREE
71#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070072#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070073#endif
74
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053075#if WDOG_SUPPORT
76#include <wdog.h>
77#endif
78
Shashank Mittalcd98d472011-08-02 14:29:24 -070079#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080080#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080081#include "bootimg.h"
82#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070083#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080084#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070085#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070086#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070087#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070088#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020089#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070090#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080091#include <menu_keys_detect.h>
92#include <display_menu.h>
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070093
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070094extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070095extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070096extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070097extern int get_target_boot_params(const char *cmdline, const char *part,
98 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070099
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700100void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700101void write_device_info_mmc(device_info *dev);
102void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700103static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700104
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700105/* fastboot command function pointer */
106typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
107
108struct fastboot_cmd_desc {
109 char * name;
110 fastboot_cmd_fn cb;
111};
112
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700113#define EXPAND(NAME) #NAME
114#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700115
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800116#ifdef MEMBASE
117#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
118#else
David Ng183a7422009-12-07 14:55:21 -0800119#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800120#endif
121
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700122#ifndef MEMSIZE
123#define MEMSIZE 1024*1024
124#endif
125
126#define MAX_TAGS_SIZE 1024
127
lijuang72875802015-02-10 10:35:12 +0800128#define RECOVERY_HARD_RESET_MODE 0x01
129#define FASTBOOT_HARD_RESET_MODE 0x02
130#define RTC_HARD_RESET_MODE 0x03
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700131#define DM_VERITY_ENFORCING_HARD_RESET_MODE 0x04
132#define DM_VERITY_LOGGING_HARD_RESET_MODE 0x05
133#define DM_VERITY_KEYSCLEAR_HARD_RESET_MODE 0x06
lijuang72875802015-02-10 10:35:12 +0800134
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700135#define RECOVERY_MODE 0x77665502
136#define FASTBOOT_MODE 0x77665500
137#define ALARM_BOOT 0x77665503
138#define DM_VERITY_LOGGING 0x77665508
139#define DM_VERITY_ENFORCING 0x77665509
140#define DM_VERITY_KEYSCLEAR 0x7766550A
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800141
Kun Liang2f1601a2013-08-12 16:29:54 +0800142/* make 4096 as default size to ensure EFS,EXT4's erasing */
143#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800144#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800145
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700146#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700147#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800148
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800149#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
150
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700151#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
152
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800153#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700154static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700155#else
David Ng183a7422009-12-07 14:55:21 -0800156static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700157#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800158static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300159static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800160static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800161static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800162static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700163static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300164static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200165static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800166
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800167static const char *baseband_apq = " androidboot.baseband=apq";
168static const char *baseband_msm = " androidboot.baseband=msm";
169static const char *baseband_csfb = " androidboot.baseband=csfb";
170static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700171static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800172static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700173static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800174static const char *baseband_dsda = " androidboot.baseband=dsda";
175static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800176static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800177static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800178
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700179#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700180static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700181static const char *verified_state= " androidboot.verifiedbootstate=";
182
183//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700184
185struct verified_boot_verity_mode vbvm[] =
186{
187 {false, "logging"},
188 {true, "enforcing"},
189};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700190struct verified_boot_state_name vbsn[] =
191{
192 {GREEN, "green"},
193 {ORANGE, "orange"},
194 {YELLOW,"yellow"},
195 {RED,"red" },
196};
197#endif
198
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700199static unsigned page_size = 0;
200static unsigned page_mask = 0;
201static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
202static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700203static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800204static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700205static bool devinfo_present = true;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700206
Shashank Mittalcd98d472011-08-02 14:29:24 -0700207/* Assuming unauthorized kernel image by default */
208static int auth_kernel_img = 0;
209
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700210static device_info device = {DEVICE_MAGIC, 0, 0, 0, {0}, {0},{0}, 1};
vijay kumarc65876c2015-04-24 13:29:16 +0530211static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700212
vijay kumarca2e6812015-07-08 20:28:25 +0530213static char frp_ptns[2][8] = {"config","frp"};
214
Dima Zavin42168f22009-01-30 11:52:22 -0800215struct atag_ptbl_entry
216{
217 char name[16];
218 unsigned offset;
219 unsigned size;
220 unsigned flags;
221};
222
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700223/*
224 * Partition info, required to be published
225 * for fastboot
226 */
227struct getvar_partition_info {
228 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
229 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
230 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
231 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
232 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
233};
234
235/*
236 * Right now, we are publishing the info for only
237 * three partitions
238 */
239struct getvar_partition_info part_info[] =
240{
241 { "system" , "partition-size:", "partition-type:", "", "ext4" },
242 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
243 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
244};
245
246char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700247char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800248char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700249char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530250char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700251
Greg Griscod2471ef2011-07-14 13:00:42 -0700252extern int emmc_recovery_init(void);
253
Kinson Chik0b1c8162011-08-31 16:31:57 -0700254#if NO_KEYPAD_DRIVER
255extern int fastboot_trigger(void);
256#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700257
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800258static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700259{
260 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700261#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800262 if (is_arm64)
263 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
264 else
265 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700266 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
267 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700268#endif
269}
270
Dima Zavin42168f22009-01-30 11:52:22 -0800271static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
272{
273 struct atag_ptbl_entry atag_ptn;
274
275 memcpy(atag_ptn.name, ptn->name, 16);
276 atag_ptn.name[15] = '\0';
277 atag_ptn.offset = ptn->start;
278 atag_ptn.size = ptn->length;
279 atag_ptn.flags = ptn->flags;
280 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
281 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
282}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800283
Neeti Desaie245d492012-06-01 12:52:13 -0700284unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800285{
David Ng183a7422009-12-07 14:55:21 -0800286 int cmdline_len = 0;
287 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800288 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700289 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800290 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300291 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700292 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700293 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200294 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700295#if VERIFIED_BOOT
296 uint32_t boot_state = boot_verify_get_state();
297#endif
298
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200299#ifdef MDTP_SUPPORT
300 mdtp_activated(&is_mdtp_activated);
301#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800302
Brian Swetland9c4c0752009-01-25 16:23:50 -0800303 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800304 cmdline_len = strlen(cmdline);
305 have_cmdline = 1;
306 }
307 if (target_is_emmc_boot()) {
308 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800309#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700310 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
311 ASSERT(boot_dev_buf);
312 platform_boot_dev_cmdline(boot_dev_buf);
313 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700314#endif
David Ng183a7422009-12-07 14:55:21 -0800315 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800316
317 cmdline_len += strlen(usb_sn_cmdline);
318 cmdline_len += strlen(sn_buf);
319
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700320#if VERIFIED_BOOT
321 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700322 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700323#endif
324
Pavel Nedev5614d222013-06-17 18:01:02 +0300325 if (boot_into_recovery && gpt_exists)
326 cmdline_len += strlen(secondary_gpt_enable);
327
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200328 if(is_mdtp_activated)
329 cmdline_len += strlen(mdtp_activated_flag);
330
Pavel Nedev328ac822013-04-05 15:25:11 +0300331 if (boot_into_ffbm) {
332 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700333 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800334 /* reduce kernel console messages to speed-up boot */
335 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800336 } else if (boot_reason_alarm) {
337 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800338 } else if ((target_build_variant_user() || device.charger_screen_enabled)
339 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700340 pause_at_bootup = 1;
341 cmdline_len += strlen(battchg_pause);
342 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800343
Shashank Mittalcd98d472011-08-02 14:29:24 -0700344 if(target_use_signed_kernel() && auth_kernel_img) {
345 cmdline_len += strlen(auth_kernel);
346 }
347
Joonwoo Park61112782013-10-02 19:50:39 -0700348 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
349 "system",
350 target_boot_params,
351 sizeof(target_boot_params)) == 0) {
352 have_target_boot_params = 1;
353 cmdline_len += strlen(target_boot_params);
354 }
355
Ajay Dudanid04110c2011-01-17 23:55:07 -0800356 /* Determine correct androidboot.baseband to use */
357 switch(target_baseband())
358 {
359 case BASEBAND_APQ:
360 cmdline_len += strlen(baseband_apq);
361 break;
362
363 case BASEBAND_MSM:
364 cmdline_len += strlen(baseband_msm);
365 break;
366
367 case BASEBAND_CSFB:
368 cmdline_len += strlen(baseband_csfb);
369 break;
370
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800371 case BASEBAND_SVLTE2A:
372 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800373 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700374
375 case BASEBAND_MDM:
376 cmdline_len += strlen(baseband_mdm);
377 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700378
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800379 case BASEBAND_MDM2:
380 cmdline_len += strlen(baseband_mdm2);
381 break;
382
Amol Jadi5c61a952012-05-04 17:05:35 -0700383 case BASEBAND_SGLTE:
384 cmdline_len += strlen(baseband_sglte);
385 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530386
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800387 case BASEBAND_SGLTE2:
388 cmdline_len += strlen(baseband_sglte2);
389 break;
390
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530391 case BASEBAND_DSDA:
392 cmdline_len += strlen(baseband_dsda);
393 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800394
395 case BASEBAND_DSDA2:
396 cmdline_len += strlen(baseband_dsda2);
397 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800398 }
399
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800400 if (cmdline) {
401 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530402 target_display_panel_node(display_panel_buf,
403 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800404 strlen(display_panel_buf)) {
405 cmdline_len += strlen(display_panel_buf);
406 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700407 }
408
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800409 if (target_warm_boot()) {
410 warm_boot = true;
411 cmdline_len += strlen(warmboot_cmdline);
412 }
413
David Ng183a7422009-12-07 14:55:21 -0800414 if (cmdline_len > 0) {
415 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800416 unsigned char *dst;
417
418 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
419 ASSERT(cmdline_final != NULL);
420 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700421
Amol Jadi168b7712012-03-06 16:15:00 -0800422 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800423 if (have_cmdline) {
424 src = cmdline;
425 while ((*dst++ = *src++));
426 }
427 if (target_is_emmc_boot()) {
428 src = emmc_cmdline;
429 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700430 have_cmdline = 1;
431 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800432#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700433 src = boot_dev_buf;
434 if (have_cmdline) --dst;
435 while ((*dst++ = *src++));
436#endif
David Ngf773dde2010-07-26 19:55:08 -0700437 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800438
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700439#if VERIFIED_BOOT
440 src = verified_state;
441 if(have_cmdline) --dst;
442 have_cmdline = 1;
443 while ((*dst++ = *src++));
444 src = vbsn[boot_state].name;
445 if(have_cmdline) --dst;
446 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700447
448 src = verity_mode;
449 if(have_cmdline) --dst;
450 while ((*dst++ = *src++));
451 src = vbvm[device.verity_mode].name;
452 if(have_cmdline) -- dst;
453 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700454#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800455 src = usb_sn_cmdline;
456 if (have_cmdline) --dst;
457 have_cmdline = 1;
458 while ((*dst++ = *src++));
459 src = sn_buf;
460 if (have_cmdline) --dst;
461 have_cmdline = 1;
462 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800463 if (warm_boot) {
464 if (have_cmdline) --dst;
465 src = warmboot_cmdline;
466 while ((*dst++ = *src++));
467 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800468
Pavel Nedev5614d222013-06-17 18:01:02 +0300469 if (boot_into_recovery && gpt_exists) {
470 src = secondary_gpt_enable;
471 if (have_cmdline) --dst;
472 while ((*dst++ = *src++));
473 }
474
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200475 if (is_mdtp_activated) {
476 src = mdtp_activated_flag;
477 if (have_cmdline) --dst;
478 while ((*dst++ = *src++));
479 }
480
Pavel Nedev328ac822013-04-05 15:25:11 +0300481 if (boot_into_ffbm) {
482 src = androidboot_mode;
483 if (have_cmdline) --dst;
484 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700485 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300486 if (have_cmdline) --dst;
487 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800488 src = loglevel;
489 if (have_cmdline) --dst;
490 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800491 } else if (boot_reason_alarm) {
492 src = alarmboot_cmdline;
493 if (have_cmdline) --dst;
494 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300495 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700496 src = battchg_pause;
497 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800498 while ((*dst++ = *src++));
499 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800500
Shashank Mittalcd98d472011-08-02 14:29:24 -0700501 if(target_use_signed_kernel() && auth_kernel_img) {
502 src = auth_kernel;
503 if (have_cmdline) --dst;
504 while ((*dst++ = *src++));
505 }
506
Ajay Dudanid04110c2011-01-17 23:55:07 -0800507 switch(target_baseband())
508 {
509 case BASEBAND_APQ:
510 src = baseband_apq;
511 if (have_cmdline) --dst;
512 while ((*dst++ = *src++));
513 break;
514
515 case BASEBAND_MSM:
516 src = baseband_msm;
517 if (have_cmdline) --dst;
518 while ((*dst++ = *src++));
519 break;
520
521 case BASEBAND_CSFB:
522 src = baseband_csfb;
523 if (have_cmdline) --dst;
524 while ((*dst++ = *src++));
525 break;
526
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800527 case BASEBAND_SVLTE2A:
528 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800529 if (have_cmdline) --dst;
530 while ((*dst++ = *src++));
531 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700532
533 case BASEBAND_MDM:
534 src = baseband_mdm;
535 if (have_cmdline) --dst;
536 while ((*dst++ = *src++));
537 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700538
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800539 case BASEBAND_MDM2:
540 src = baseband_mdm2;
541 if (have_cmdline) --dst;
542 while ((*dst++ = *src++));
543 break;
544
Amol Jadi5c61a952012-05-04 17:05:35 -0700545 case BASEBAND_SGLTE:
546 src = baseband_sglte;
547 if (have_cmdline) --dst;
548 while ((*dst++ = *src++));
549 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530550
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800551 case BASEBAND_SGLTE2:
552 src = baseband_sglte2;
553 if (have_cmdline) --dst;
554 while ((*dst++ = *src++));
555 break;
556
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530557 case BASEBAND_DSDA:
558 src = baseband_dsda;
559 if (have_cmdline) --dst;
560 while ((*dst++ = *src++));
561 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800562
563 case BASEBAND_DSDA2:
564 src = baseband_dsda2;
565 if (have_cmdline) --dst;
566 while ((*dst++ = *src++));
567 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800568 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700569
570 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700571 src = display_panel_buf;
572 if (have_cmdline) --dst;
573 while ((*dst++ = *src++));
574 }
Joonwoo Park61112782013-10-02 19:50:39 -0700575
576 if (have_target_boot_params) {
577 if (have_cmdline) --dst;
578 src = target_boot_params;
579 while ((*dst++ = *src++));
580 }
Neeti Desaie245d492012-06-01 12:52:13 -0700581 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700582
583
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700584 if (boot_dev_buf)
585 free(boot_dev_buf);
586
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800587 if (cmdline_final)
588 dprintf(INFO, "cmdline: %s\n", cmdline_final);
589 else
590 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700591 return cmdline_final;
592}
593
594unsigned *atag_core(unsigned *ptr)
595{
596 /* CORE */
597 *ptr++ = 2;
598 *ptr++ = 0x54410001;
599
600 return ptr;
601
602}
603
604unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
605 unsigned ramdisk_size)
606{
607 if (ramdisk_size) {
608 *ptr++ = 4;
609 *ptr++ = 0x54420005;
610 *ptr++ = (unsigned)ramdisk;
611 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800612 }
613
Neeti Desaie245d492012-06-01 12:52:13 -0700614 return ptr;
615}
616
617unsigned *atag_ptable(unsigned **ptr_addr)
618{
619 int i;
620 struct ptable *ptable;
621
622 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700623 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
624 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700625 *(*ptr_addr)++ = 0x4d534d70;
626 for (i = 0; i < ptable->count; ++i)
627 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
628 }
629
630 return (*ptr_addr);
631}
632
633unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
634{
635 int cmdline_length = 0;
636 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700637 char *dest;
638
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800639 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700640 n = (cmdline_length + 4) & (~3);
641
642 *ptr++ = (n / 4) + 2;
643 *ptr++ = 0x54410009;
644 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800645 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700646 ptr += (n / 4);
647
648 return ptr;
649}
650
651unsigned *atag_end(unsigned *ptr)
652{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800653 /* END */
654 *ptr++ = 0;
655 *ptr++ = 0;
656
Neeti Desaie245d492012-06-01 12:52:13 -0700657 return ptr;
658}
659
660void generate_atags(unsigned *ptr, const char *cmdline,
661 void *ramdisk, unsigned ramdisk_size)
662{
663
664 ptr = atag_core(ptr);
665 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
666 ptr = target_atag_mem(ptr);
667
668 /* Skip NAND partition ATAGS for eMMC boot */
669 if (!target_is_emmc_boot()){
670 ptr = atag_ptable(&ptr);
671 }
672
673 ptr = atag_cmdline(ptr, cmdline);
674 ptr = atag_end(ptr);
675}
676
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700677typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700678void boot_linux(void *kernel, unsigned *tags,
679 const char *cmdline, unsigned machtype,
680 void *ramdisk, unsigned ramdisk_size)
681{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800682 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800683#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700684 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800685#endif
686
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700687 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800688 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800689 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800690
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530691 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700692
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800693 final_cmdline = update_cmdline((const char*)cmdline);
694
Neeti Desai17379b82012-06-04 18:42:53 -0700695#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800696 dprintf(INFO, "Updating device tree: start\n");
697
Neeti Desai17379b82012-06-04 18:42:53 -0700698 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530699 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700700 if(ret)
701 {
702 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
703 ASSERT(0);
704 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800705 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700706#else
Neeti Desaie245d492012-06-01 12:52:13 -0700707 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800708 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700709#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700710
Maria Yu52254c02014-07-04 16:14:54 +0800711 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700712
713#if VERIFIED_BOOT
714 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700715 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700716 {
717 dprintf(INFO, "Failed to write protect dev info\n");
718 ASSERT(0);
719 }
720#endif
721
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700722 /* Perform target specific cleanup */
723 target_uninit();
724
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800725 /* Turn off splash screen if enabled */
726#if DISPLAY_SPLASH_SCREEN
727 target_display_shutdown();
728#endif
729
730
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800731 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530732 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800733
734 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700735
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +0530736 /* Initialise wdog to catch early kernel crashes */
737#if WDOG_SUPPORT
738 msm_wdog_init();
739#endif
Amol Jadi4421e652011-06-16 15:00:48 -0700740 /* do any platform specific cleanup before kernel entry */
741 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700742
Brian Swetland9c4c0752009-01-25 16:23:50 -0800743 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700744
Amol Jadi504f9fe2012-08-16 13:56:48 -0700745#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800746 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700747#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700748 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800749
750 if (IS_ARM64(kptr))
751 /* Jump to a 64bit kernel */
752 scm_elexec_call((paddr_t)kernel, tags_phys);
753 else
754 /* Jump to a 32bit kernel */
755 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800756}
757
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700758/* Function to check if the memory address range falls within the aboot
759 * boundaries.
760 * start: Start of the memory region
761 * size: Size of the memory region
762 */
763int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
764{
765 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800766 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700767 return -1;
768
769 /* Check for memory overlap. */
770 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
771 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700772 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700773 return 0;
774 else
775 return -1;
776}
777
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800778#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800779
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800780BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800781#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800782BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800783#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800784
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700785static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
786{
787 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800788
789#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800790#if IMAGE_VERIF_ALGO_SHA1
791 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
792#else
793 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
794#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800795#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700796
797 /* Assume device is rooted at this time. */
798 device.is_tampered = 1;
799
800 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
801
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700802#if VERIFIED_BOOT
803 if(boot_into_recovery)
804 {
805 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530806 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700807 }
808 else
809 {
810 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530811 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700812 }
813 boot_verify_print_state();
814#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700815 ret = image_verify((unsigned char *)bootimg_addr,
816 (unsigned char *)(bootimg_addr + bootimg_size),
817 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800818 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700819#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700820 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
821
822 if (ret)
823 {
824 /* Authorized kernel */
825 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700826 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700827 }
828
Amit Blay4aa292f2015-04-28 21:55:59 +0300829#ifdef MDTP_SUPPORT
830 {
831 /* Verify MDTP lock.
832 * For boot & recovery partitions, use aboot's verification result.
833 */
834 mdtp_ext_partition_verification_t ext_partition;
835 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
836 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
837 ext_partition.page_size = 0; /* Not needed since already validated */
838 ext_partition.image_addr = 0; /* Not needed since already validated */
839 ext_partition.image_size = 0; /* Not needed since already validated */
840 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
841 mdtp_fwlock_verify_lock(&ext_partition);
842 }
843#endif /* MDTP_SUPPORT */
844
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700845#if USE_PCOM_SECBOOT
846 set_tamper_flag(device.is_tampered);
847#endif
848
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700849#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700850 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700851 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700852 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800853#if FBCON_DISPLAY_MSG
854 display_menu_thread(DISPLAY_THREAD_BOOT_STATE);
855 wait_for_users_action();
856#else
857 dprintf(CRITICAL,
858 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
859 mdelay(5000);
860#endif
861
862 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700863 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800864#if FBCON_DISPLAY_MSG
865 display_menu_thread(DISPLAY_THREAD_BOOT_STATE);
866 wait_for_users_action();
867#else
868 dprintf(CRITICAL,
869 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
870 mdelay(5000);
871#endif
872 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700873 default:
lijuanga40d6302015-07-20 20:10:13 +0800874 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700875 }
876#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700877#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530878 if(device.is_tampered)
879 {
880 write_device_info_mmc(&device);
881 #ifdef TZ_TAMPER_FUSE
882 set_tamper_fuse_cmd();
883 #endif
884 #ifdef ASSERT_ON_TAMPER
885 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
886 ASSERT(0);
887 #endif
888 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700889#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700890}
891
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530892static bool check_format_bit()
893{
894 bool ret = false;
895 int index;
896 uint64_t offset;
897 struct boot_selection_info *in = NULL;
898 char *buf = NULL;
899
900 index = partition_get_index("bootselect");
901 if (index == INVALID_PTN)
902 {
903 dprintf(INFO, "Unable to locate /bootselect partition\n");
904 return ret;
905 }
906 offset = partition_get_offset(index);
907 if(!offset)
908 {
909 dprintf(INFO, "partition /bootselect doesn't exist\n");
910 return ret;
911 }
912 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
913 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530914 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530915 {
916 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
917 free(buf);
918 return ret;
919 }
920 in = (struct boot_selection_info *) buf;
921 if ((in->signature == BOOTSELECT_SIGNATURE) &&
922 (in->version == BOOTSELECT_VERSION)) {
923 if ((in->state_info & BOOTSELECT_FORMAT) &&
924 !(in->state_info & BOOTSELECT_FACTORY))
925 ret = true;
926 } else {
927 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
928 in->signature, in->version);
929 ASSERT(0);
930 }
931 free(buf);
932 return ret;
933}
934
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700935void boot_verifier_init()
936{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700937 uint32_t boot_state;
938 /* Check if device unlock */
939 if(device.is_unlocked)
940 {
941 boot_verify_send_event(DEV_UNLOCK);
942 boot_verify_print_state();
943 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
944 return;
945 }
946 else
947 {
948 boot_verify_send_event(BOOT_INIT);
949 }
950
951 /* Initialize keystore */
952 boot_state = boot_verify_keystore_init();
953 if(boot_state == YELLOW)
954 {
955 boot_verify_print_state();
956 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
957 }
958}
959
Shashank Mittal23b8f422010-04-16 19:27:21 -0700960int boot_linux_from_mmc(void)
961{
962 struct boot_img_hdr *hdr = (void*) buf;
963 struct boot_img_hdr *uhdr;
964 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700965 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700966 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700967 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700968
Shashank Mittalcd98d472011-08-02 14:29:24 -0700969 unsigned char *image_addr = 0;
970 unsigned kernel_actual;
971 unsigned ramdisk_actual;
972 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700973 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700974
Matthew Qin271927e2015-03-31 22:07:07 -0400975 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +0800976 unsigned int out_len = 0;
977 unsigned int out_avai_len = 0;
978 unsigned char *out_addr = NULL;
979 uint32_t dtb_offset = 0;
980 unsigned char *kernel_start_addr = NULL;
981 unsigned int kernel_size = 0;
982 int rc;
983
Neeti Desai465491e2012-07-31 12:53:35 -0700984#if DEVICE_TREE
985 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700986 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700987 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800988 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700989 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -0400990 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -0700991#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +0800992 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800993
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530994 if (check_format_bit())
995 boot_into_recovery = 1;
996
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700997 if (!boot_into_recovery) {
998 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
999 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1000 if (rcode <= 0) {
1001 boot_into_ffbm = false;
1002 if (rcode < 0)
1003 dprintf(CRITICAL,"failed to get ffbm cookie");
1004 } else
1005 boot_into_ffbm = true;
1006 } else
1007 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001008 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1009 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1010 dprintf(INFO, "Unified boot method!\n");
1011 hdr = uhdr;
1012 goto unified_boot;
1013 }
Greg Griscod6250552011-06-29 14:40:23 -07001014 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001015 index = partition_get_index("boot");
1016 ptn = partition_get_offset(index);
1017 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001018 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1019 return -1;
1020 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001021 }
1022 else {
1023 index = partition_get_index("recovery");
1024 ptn = partition_get_offset(index);
1025 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001026 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1027 return -1;
1028 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001029 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001030 /* Set Lun for boot & recovery partitions */
1031 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001032
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301033 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001034 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1035 return -1;
1036 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001037
1038 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001039 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001040 return -1;
1041 }
1042
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001043 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301044
1045 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1046 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1047 return -1;
1048 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001049 page_size = hdr->page_size;
1050 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001051 }
1052
Matthew Qin49e51fa2015-02-09 10:40:45 +08001053 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1054 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001055
Matthew Qin49e51fa2015-02-09 10:40:45 +08001056 image_addr = (unsigned char *)target_get_scratch_address();
1057
1058#if DEVICE_TREE
1059 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1060 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1061#else
1062 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1063#endif
1064
1065#if VERIFIED_BOOT
1066 boot_verifier_init();
1067#endif
1068
1069 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
1070 {
1071 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1072 return -1;
1073 }
1074
Matthew Qinbb7923d2015-02-09 10:56:09 +08001075 /*
1076 * Update loading flow of bootimage to support compressed/uncompressed
1077 * bootimage on both 64bit and 32bit platform.
1078 * 1. Load bootimage from emmc partition onto DDR.
1079 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1080 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1081 * platform accordingly.
1082 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1083 */
1084
Matthew Qin49e51fa2015-02-09 10:40:45 +08001085 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
1086 bs_set_timestamp(BS_KERNEL_LOAD_START);
1087
Matthew Qinbb7923d2015-02-09 10:56:09 +08001088 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001089 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1090 {
1091 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1092 return -1;
1093 }
1094
1095 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
1096 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1097
1098 /* Authenticate Kernel */
1099 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1100 (int) target_use_signed_kernel(),
1101 device.is_unlocked,
1102 device.is_tampered);
1103
1104 if(target_use_signed_kernel() && (!device.is_unlocked))
1105 {
1106 offset = imagesize_actual;
1107 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1108 {
1109 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1110 return -1;
1111 }
1112
1113 /* Read signature */
1114 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1115 {
1116 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1117 return -1;
1118 }
1119
1120 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
1121 } else {
1122 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1123 #ifdef TZ_SAVE_KERNEL_HASH
1124 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1125 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001126
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001127#if VERIFIED_BOOT
1128 if(boot_verify_get_state() == ORANGE)
1129 {
lijuanga40d6302015-07-20 20:10:13 +08001130#if FBCON_DISPLAY_MSG
1131 display_menu_thread(DISPLAY_THREAD_BOOT_STATE);
1132 wait_for_users_action();
1133#else
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001134 dprintf(CRITICAL,
lijuanga40d6302015-07-20 20:10:13 +08001135 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001136 mdelay(5000);
lijuanga40d6302015-07-20 20:10:13 +08001137#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001138 }
1139#endif
1140
Amit Blay4aa292f2015-04-28 21:55:59 +03001141#ifdef MDTP_SUPPORT
1142 {
1143 /* Verify MDTP lock.
1144 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1145 * since verification was skipped in aboot. The signature is not part of the loaded image.
1146 */
1147 mdtp_ext_partition_verification_t ext_partition;
1148 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1149 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1150 ext_partition.page_size = page_size;
1151 ext_partition.image_addr = (uint32)image_addr;
1152 ext_partition.image_size = imagesize_actual;
1153 ext_partition.sig_avail = FALSE;
1154 mdtp_fwlock_verify_lock(&ext_partition);
1155 }
1156#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001157 }
1158
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001159#if VERIFIED_BOOT
1160 // send root of trust
1161 if(!send_rot_command())
1162 ASSERT(0);
1163#endif
1164
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001165 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001166 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1167 * If not, continue booting.
1168 */
1169 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1170 {
1171 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1172 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001173 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001174 rc = decompress((unsigned char *)(image_addr + page_size),
1175 hdr->kernel_size, out_addr, out_avai_len,
1176 &dtb_offset, &out_len);
1177 if (rc)
1178 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001179 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001180 ASSERT(0);
1181 }
1182
Matthew Qin0b15b322015-05-19 05:20:54 -04001183 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001184 kptr = (struct kernel64_hdr *)out_addr;
1185 kernel_start_addr = out_addr;
1186 kernel_size = out_len;
1187 } else {
1188 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1189 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1190 kernel_size = hdr->kernel_size;
1191 }
1192
1193 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001194 * Update the kernel/ramdisk/tags address if the boot image header
1195 * has default values, these default values come from mkbootimg when
1196 * the boot image is flashed using fastboot flash:raw
1197 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001198 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001199
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001200 /* Get virtual addresses since the hdr saves physical addresses. */
1201 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1202 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1203 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001204
Matthew Qinbb7923d2015-02-09 10:56:09 +08001205 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001206 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001207 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001208 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1209 {
1210 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1211 return -1;
1212 }
1213
1214#ifndef DEVICE_TREE
1215 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1216 {
1217 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1218 return -1;
1219 }
1220#endif
1221
Matthew Qin49e51fa2015-02-09 10:40:45 +08001222 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001223 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001224 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001225
Matthew Qin49e51fa2015-02-09 10:40:45 +08001226 #if DEVICE_TREE
1227 if(hdr->dt_size) {
1228 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1229 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001230
Matthew Qin49e51fa2015-02-09 10:40:45 +08001231 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1232 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1233 return -1;
1234 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001235
Matthew Qin49e51fa2015-02-09 10:40:45 +08001236 /* Find index of device tree within device tree table */
1237 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1238 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1239 return -1;
1240 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001241
Matthew Qin271927e2015-03-31 22:07:07 -04001242 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1243 {
1244 unsigned int compressed_size = 0;
1245 out_addr += out_len;
1246 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001247 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001248 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1249 dt_entry.size, out_addr, out_avai_len,
1250 &compressed_size, &dtb_size);
1251 if (rc)
1252 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001253 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001254 ASSERT(0);
1255 }
1256
Matthew Qin0b15b322015-05-19 05:20:54 -04001257 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001258 best_match_dt_addr = out_addr;
1259 } else {
1260 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1261 dtb_size = dt_entry.size;
1262 }
1263
Matthew Qin49e51fa2015-02-09 10:40:45 +08001264 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001265 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001266 {
1267 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1268 return -1;
1269 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001270
Matthew Qin271927e2015-03-31 22:07:07 -04001271 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001272 } else {
1273 /* Validate the tags_addr */
1274 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001275 {
1276 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1277 return -1;
1278 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001279 /*
1280 * If appended dev tree is found, update the atags with
1281 * memory address to the DTB appended location on RAM.
1282 * Else update with the atags address in the kernel header
1283 */
1284 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001285 dtb = dev_tree_appended((void*)(image_addr + page_size),
1286 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001287 (void *)hdr->tags_addr);
1288 if (!dtb) {
1289 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001290 return -1;
1291 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001292 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001293 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001294
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001295 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1296 target_load_ssd_keystore();
1297
Shashank Mittal23b8f422010-04-16 19:27:21 -07001298unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001299
Dima Zavin77e41f32013-03-06 16:10:43 -08001300 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001301 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001302 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1303
1304 return 0;
1305}
1306
Dima Zavin214cc642009-01-26 11:16:21 -08001307int boot_linux_from_flash(void)
1308{
1309 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001310 struct ptentry *ptn;
1311 struct ptable *ptable;
1312 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001313
Shashank Mittalcd98d472011-08-02 14:29:24 -07001314 unsigned char *image_addr = 0;
1315 unsigned kernel_actual;
1316 unsigned ramdisk_actual;
1317 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001318 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001319
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001320#if DEVICE_TREE
1321 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001322 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001323 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001324 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001325#endif
1326
David Ng183a7422009-12-07 14:55:21 -08001327 if (target_is_emmc_boot()) {
1328 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1329 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1330 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1331 return -1;
1332 }
1333 goto continue_boot;
1334 }
1335
Dima Zavin214cc642009-01-26 11:16:21 -08001336 ptable = flash_get_ptable();
1337 if (ptable == NULL) {
1338 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1339 return -1;
1340 }
1341
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001342 if(!boot_into_recovery)
1343 {
1344 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001345
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001346 if (ptn == NULL) {
1347 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1348 return -1;
1349 }
1350 }
1351 else
1352 {
1353 ptn = ptable_find(ptable, "recovery");
1354 if (ptn == NULL) {
1355 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1356 return -1;
1357 }
Dima Zavin214cc642009-01-26 11:16:21 -08001358 }
1359
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001360 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001361 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1362 return -1;
1363 }
Dima Zavin214cc642009-01-26 11:16:21 -08001364
1365 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001366 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001367 return -1;
1368 }
1369
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001370 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001371 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 -08001372 return -1;
1373 }
1374
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001375 /*
1376 * Update the kernel/ramdisk/tags address if the boot image header
1377 * has default values, these default values come from mkbootimg when
1378 * the boot image is flashed using fastboot flash:raw
1379 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001380 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001381
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001382 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001383 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1384 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1385 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1386
1387 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1388 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1389
1390 /* Check if the addresses in the header are valid. */
1391 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1392 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1393 {
1394 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1395 return -1;
1396 }
1397
1398#ifndef DEVICE_TREE
1399 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1400 {
1401 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1402 return -1;
1403 }
1404#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001405
Shashank Mittalcd98d472011-08-02 14:29:24 -07001406 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001407 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001408 {
1409 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001410 offset = 0;
1411
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001412#if DEVICE_TREE
1413 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1414 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001415
1416 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1417 {
1418 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1419 return -1;
1420 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001421#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001422 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001423#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001424
Amol Jadib6be5c12012-11-14 13:39:51 -08001425 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001426 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001427
Shashank Mittalcd98d472011-08-02 14:29:24 -07001428 /* Read image without signature */
1429 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1430 {
1431 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1432 return -1;
1433 }
Dima Zavin214cc642009-01-26 11:16:21 -08001434
Amol Jadib6be5c12012-11-14 13:39:51 -08001435 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001436 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001437
Shashank Mittalcd98d472011-08-02 14:29:24 -07001438 offset = imagesize_actual;
1439 /* Read signature */
1440 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1441 {
1442 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001443 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001444 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001445
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301446 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001447
1448 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001449 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1450 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001451#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001452 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001453 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001454 {
1455 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1456 return -1;
1457 }
1458
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001459 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1460#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001461
1462 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001463 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001464 {
1465 write_device_info_flash(&device);
1466 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301467#if USE_PCOM_SECBOOT
1468 set_tamper_flag(device.is_tampered);
1469#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001470 }
1471 else
1472 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001473 offset = page_size;
1474
Amol Jadib6be5c12012-11-14 13:39:51 -08001475 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1476 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1477 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1478
1479 dprintf(INFO, "Loading boot image (%d): start\n",
1480 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001481 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001482
1483 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001484 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1485 return -1;
1486 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001487 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001488
Amol Jadib6be5c12012-11-14 13:39:51 -08001489 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001490 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1491 return -1;
1492 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001493 offset += ramdisk_actual;
1494
1495 dprintf(INFO, "Loading boot image (%d): done\n",
1496 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001497 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001498
1499 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001500 offset += second_actual;
1501 /* Second image loading not implemented. */
1502 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001503 }
1504
1505#if DEVICE_TREE
1506 if(hdr->dt_size != 0) {
1507
1508 /* Read the device tree table into buffer */
1509 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1510 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1511 return -1;
1512 }
1513
1514 table = (struct dt_table*) dt_buf;
1515
Deepa Dinamani19648b42013-09-05 17:05:55 -07001516 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001517 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1518 return -1;
1519 }
1520
Deepa Dinamani19648b42013-09-05 17:05:55 -07001521 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1522 if (!table)
1523 return -1;
1524
1525 /* Read the entire device tree table into buffer */
1526 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1527 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1528 return -1;
1529 }
1530
1531
Joel Kingaa335dc2013-06-03 16:11:08 -07001532 /* Find index of device tree within device tree table */
1533 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001534 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1535 return -1;
1536 }
1537
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001538 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001539 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001540 {
1541 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1542 return -1;
1543 }
1544
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001545 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001546 if(flash_read(ptn, offset + dt_entry.offset,
1547 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001548 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1549 return -1;
1550 }
1551 }
1552#endif
1553
Shashank Mittalcd98d472011-08-02 14:29:24 -07001554 }
David Ng183a7422009-12-07 14:55:21 -08001555continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001556
Dima Zavin214cc642009-01-26 11:16:21 -08001557 /* TODO: create/pass atags to kernel */
1558
Ajay Dudanie28a6072011-07-01 13:59:46 -07001559 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001560 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001561 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1562
1563 return 0;
1564}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001565
Shashank Mittal162244e2011-08-08 19:01:25 -07001566void write_device_info_mmc(device_info *dev)
1567{
Shashank Mittal162244e2011-08-08 19:01:25 -07001568 unsigned long long ptn = 0;
1569 unsigned long long size;
1570 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001571 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001572 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001573 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001574
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001575 if (devinfo_present)
1576 index = partition_get_index("devinfo");
1577 else
1578 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001579
Shashank Mittal162244e2011-08-08 19:01:25 -07001580 ptn = partition_get_offset(index);
1581 if(ptn == 0)
1582 {
1583 return;
1584 }
1585
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001586 lun = partition_get_lun(index);
1587 mmc_set_lun(lun);
1588
Shashank Mittal162244e2011-08-08 19:01:25 -07001589 size = partition_get_size(index);
1590
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001591 blocksize = mmc_get_device_blocksize();
1592
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001593 if (devinfo_present)
1594 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1595 else
1596 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1597 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001598 {
1599 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001600 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001601 }
1602}
1603
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001604void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001605{
Shashank Mittal162244e2011-08-08 19:01:25 -07001606 unsigned long long ptn = 0;
1607 unsigned long long size;
1608 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001609 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001610 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001611
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001612 if ((index = partition_get_index("devinfo")) < 0)
1613 {
1614 devinfo_present = false;
1615 index = partition_get_index("aboot");
1616 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001617
Shashank Mittal162244e2011-08-08 19:01:25 -07001618 ptn = partition_get_offset(index);
1619 if(ptn == 0)
1620 {
1621 return;
1622 }
1623
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001624 mmc_set_lun(partition_get_lun(index));
1625
Shashank Mittal162244e2011-08-08 19:01:25 -07001626 size = partition_get_size(index);
1627
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001628 blocksize = mmc_get_device_blocksize();
1629
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001630 if (devinfo_present)
1631 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1632 else
1633 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1634 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001635 {
1636 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001637 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001638 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001639}
1640
1641void write_device_info_flash(device_info *dev)
1642{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001643 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001644 struct ptentry *ptn;
1645 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001646 if(info == NULL)
1647 {
1648 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1649 ASSERT(0);
1650 }
1651 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001652 ptable = flash_get_ptable();
1653 if (ptable == NULL)
1654 {
1655 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1656 return;
1657 }
1658
1659 ptn = ptable_find(ptable, "devinfo");
1660 if (ptn == NULL)
1661 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001662 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001663 return;
1664 }
1665
1666 memcpy(info, dev, sizeof(device_info));
1667
1668 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1669 {
1670 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1671 return;
1672 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001673 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001674}
1675
vijay kumarc65876c2015-04-24 13:29:16 +05301676static int read_allow_oem_unlock(device_info *dev)
1677{
vijay kumarc65876c2015-04-24 13:29:16 +05301678 unsigned offset;
1679 int index;
1680 unsigned long long ptn;
1681 unsigned long long ptn_size;
1682 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001683 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301684
vijay kumarca2e6812015-07-08 20:28:25 +05301685 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301686 if (index == INVALID_PTN)
1687 {
vijay kumarca2e6812015-07-08 20:28:25 +05301688 index = partition_get_index(frp_ptns[1]);
1689 if (index == INVALID_PTN)
1690 {
1691 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1692 return -1;
1693 }
vijay kumarc65876c2015-04-24 13:29:16 +05301694 }
1695
1696 ptn = partition_get_offset(index);
1697 ptn_size = partition_get_size(index);
1698 offset = ptn_size - blocksize;
1699
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001700 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301701 {
1702 dprintf(CRITICAL, "Reading MMC failed\n");
1703 return -1;
1704 }
1705
1706 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1707 is_allow_unlock = buf[blocksize-1] & 0x01;
1708 return 0;
1709}
1710
1711static int write_allow_oem_unlock(bool allow_unlock)
1712{
vijay kumarc65876c2015-04-24 13:29:16 +05301713 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301714 int index;
1715 unsigned long long ptn;
1716 unsigned long long ptn_size;
1717 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001718 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301719
vijay kumarca2e6812015-07-08 20:28:25 +05301720 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301721 if (index == INVALID_PTN)
1722 {
vijay kumarca2e6812015-07-08 20:28:25 +05301723 index = partition_get_index(frp_ptns[1]);
1724 if (index == INVALID_PTN)
1725 {
1726 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1727 return -1;
1728 }
vijay kumarc65876c2015-04-24 13:29:16 +05301729 }
1730
1731 ptn = partition_get_offset(index);
1732 ptn_size = partition_get_size(index);
1733 offset = ptn_size - blocksize;
1734
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001735 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301736 {
1737 dprintf(CRITICAL, "Reading MMC failed\n");
1738 return -1;
1739 }
1740
1741 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1742 buf[blocksize-1] = allow_unlock;
1743 if (mmc_write(ptn + offset, blocksize, buf))
1744 {
1745 dprintf(CRITICAL, "Writing MMC failed\n");
1746 return -1;
1747 }
1748
1749 return 0;
1750}
1751
Shashank Mittal162244e2011-08-08 19:01:25 -07001752void read_device_info_flash(device_info *dev)
1753{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001754 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001755 struct ptentry *ptn;
1756 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001757 if(info == NULL)
1758 {
1759 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1760 ASSERT(0);
1761 }
1762 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001763 ptable = flash_get_ptable();
1764 if (ptable == NULL)
1765 {
1766 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1767 return;
1768 }
1769
1770 ptn = ptable_find(ptable, "devinfo");
1771 if (ptn == NULL)
1772 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001773 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001774 return;
1775 }
1776
1777 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1778 {
1779 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1780 return;
1781 }
1782
1783 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1784 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001785 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1786 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001787 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001788 write_device_info_flash(info);
1789 }
1790 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001791 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001792}
1793
1794void write_device_info(device_info *dev)
1795{
1796 if(target_is_emmc_boot())
1797 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001798 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1799 if(info == NULL)
1800 {
1801 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1802 ASSERT(0);
1803 }
1804 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001805 memcpy(info, dev, sizeof(struct device_info));
1806
1807#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001808 if (is_secure_boot_enable()) {
1809 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1810 ASSERT(0);
1811 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001812 else
1813 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001814#else
1815 write_device_info_mmc(info);
1816#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001817 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001818 }
1819 else
1820 {
1821 write_device_info_flash(dev);
1822 }
1823}
1824
1825void read_device_info(device_info *dev)
1826{
1827 if(target_is_emmc_boot())
1828 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001829 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1830 if(info == NULL)
1831 {
1832 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1833 ASSERT(0);
1834 }
1835 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001836
1837#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001838 if (is_secure_boot_enable()) {
1839 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1840 ASSERT(0);
1841 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001842 else
1843 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001844#else
1845 read_device_info_mmc(info);
1846#endif
1847
1848 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1849 {
1850 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001851 if (is_secure_boot_enable())
1852 info->is_unlocked = 0;
1853 else
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001854 info->is_unlocked = 1;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001855 info->is_tampered = 0;
1856 info->charger_screen_enabled = 0;
Sridhar Parasuram287e5062015-08-24 16:17:22 -07001857 info->verity_mode = 1; //enforcing by default
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001858 write_device_info(info);
1859 }
1860 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001861 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001862 }
1863 else
1864 {
1865 read_device_info_flash(dev);
1866 }
1867}
1868
1869void reset_device_info()
1870{
1871 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001872 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001873 write_device_info(&device);
1874}
1875
1876void set_device_root()
1877{
1878 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001879 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001880 write_device_info(&device);
1881}
1882
Amol Jadicb524072012-08-09 16:40:18 -07001883#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001884int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
Amol Jadicb524072012-08-09 16:40:18 -07001885{
1886 uint32 dt_image_offset = 0;
1887 uint32_t n;
1888 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001889 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001890 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001891 unsigned int compressed_size = 0;
1892 unsigned int dtb_size = 0;
1893 unsigned int out_avai_len = 0;
1894 unsigned char *out_addr = NULL;
1895 unsigned char *best_match_dt_addr = NULL;
1896 int rc;
Amol Jadicb524072012-08-09 16:40:18 -07001897
1898 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1899
Amol Jadicb524072012-08-09 16:40:18 -07001900 if(hdr->dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001901 /* add kernel offset */
1902 dt_image_offset += page_size;
1903 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1904 dt_image_offset += n;
1905
1906 /* add ramdisk offset */
1907 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1908 dt_image_offset += n;
1909
1910 /* add second offset */
1911 if(hdr->second_size != 0) {
1912 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1913 dt_image_offset += n;
1914 }
1915
1916 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001917 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001918
Deepa Dinamani19648b42013-09-05 17:05:55 -07001919 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001920 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1921 return -1;
1922 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001923 /* Find index of device tree within device tree table */
1924 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001925 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1926 return -1;
1927 }
1928
Matthew Qin271927e2015-03-31 22:07:07 -04001929 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
1930 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
1931 {
1932 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
1933 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04001934 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001935 rc = decompress(best_match_dt_addr,
1936 dt_entry.size, out_addr, out_avai_len,
1937 &compressed_size, &dtb_size);
1938 if (rc)
1939 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001940 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001941 ASSERT(0);
1942 }
1943
Matthew Qin0b15b322015-05-19 05:20:54 -04001944 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001945 best_match_dt_addr = out_addr;
1946 } else {
1947 dtb_size = dt_entry.size;
1948 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001949 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001950 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001951 {
1952 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1953 return -1;
1954 }
1955
Amol Jadicb524072012-08-09 16:40:18 -07001956 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001957 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001958 } else
1959 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001960
1961 /* Everything looks fine. Return success. */
1962 return 0;
1963}
1964#endif
1965
Brian Swetland9c4c0752009-01-25 16:23:50 -08001966void cmd_boot(const char *arg, void *data, unsigned sz)
1967{
1968 unsigned kernel_actual;
1969 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001970 uint32_t image_actual;
1971 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001972 uint32_t sig_actual = SIGNATURE_SIZE;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001973 struct boot_img_hdr *hdr = NULL;
1974 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001975 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001976 int ret = 0;
1977 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001978 unsigned int out_len = 0;
1979 unsigned int out_avai_len = 0;
1980 unsigned char *out_addr = NULL;
1981 uint32_t dtb_offset = 0;
1982 unsigned char *kernel_start_addr = NULL;
1983 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04001984 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001985
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001986#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07001987 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001988 {
1989 fastboot_fail("unlock device to use this command");
1990 return;
1991 }
1992#endif
1993
Brian Swetland9c4c0752009-01-25 16:23:50 -08001994 if (sz < sizeof(hdr)) {
1995 fastboot_fail("invalid bootimage header");
1996 return;
1997 }
1998
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001999 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002000
2001 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002002 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002003
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002004 if(target_is_emmc_boot() && hdr->page_size) {
2005 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002006 page_mask = page_size - 1;
2007 }
2008
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002009 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2010 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002011#if DEVICE_TREE
2012 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2013#endif
2014
2015 image_actual = ADD_OF(page_size, kernel_actual);
2016 image_actual = ADD_OF(image_actual, ramdisk_actual);
2017 image_actual = ADD_OF(image_actual, dt_actual);
2018
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002019 if (target_use_signed_kernel() && (!device.is_unlocked))
2020 image_actual = ADD_OF(image_actual, sig_actual);
2021
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002022 /* sz should have atleast raw boot image */
2023 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002024 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002025 return;
2026 }
2027
2028 /* Verify the boot image
2029 * device & page_size are initialized in aboot_init
2030 */
2031 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002032 /* Pass size excluding signature size, otherwise we would try to
2033 * access signature beyond its length
2034 */
2035 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002036
Amit Blay4aa292f2015-04-28 21:55:59 +03002037#ifdef MDTP_SUPPORT
2038 else
2039 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002040 /* fastboot boot is not allowed when MDTP is activated */
2041
Amit Blay4aa292f2015-04-28 21:55:59 +03002042 mdtp_ext_partition_verification_t ext_partition;
2043 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
2044 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
2045 ext_partition.page_size = page_size;
2046 ext_partition.image_addr = (uint32_t)data;
2047 ext_partition.image_size = image_actual - sig_actual;
2048 ext_partition.sig_avail = TRUE;
2049 mdtp_fwlock_verify_lock(&ext_partition);
2050 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002051
2052 bool is_mdtp_activated = 0;
2053 mdtp_activated(&is_mdtp_activated);
2054 if(is_mdtp_activated){
2055 dprintf(CRITICAL, "fastboot boot command is not available.\n");
2056 return;
2057 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002058#endif /* MDTP_SUPPORT */
2059
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002060 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002061 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2062 * If not, continue booting.
2063 */
2064 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2065 {
2066 out_addr = (unsigned char *)target_get_scratch_address();
2067 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2068 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002069 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002070 ret = decompress((unsigned char *)(ptr + page_size),
2071 hdr->kernel_size, out_addr, out_avai_len,
2072 &dtb_offset, &out_len);
2073 if (ret)
2074 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002075 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002076 ASSERT(0);
2077 }
2078
Matthew Qin0b15b322015-05-19 05:20:54 -04002079 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002080 kptr = (struct kernel64_hdr *)out_addr;
2081 kernel_start_addr = out_addr;
2082 kernel_size = out_len;
2083 } else {
2084 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2085 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2086 kernel_size = hdr->kernel_size;
2087 }
2088
2089 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002090 * Update the kernel/ramdisk/tags address if the boot image header
2091 * has default values, these default values come from mkbootimg when
2092 * the boot image is flashed using fastboot flash:raw
2093 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002094 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002095
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002096 /* Get virtual addresses since the hdr saves physical addresses. */
2097 hdr->kernel_addr = VA(hdr->kernel_addr);
2098 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2099 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002100
Matthew Qinbb7923d2015-02-09 10:56:09 +08002101 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002102 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002103 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002104 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2105 {
2106 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002107 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002108 }
2109
Amol Jadicb524072012-08-09 16:40:18 -07002110#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002111 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002112 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002113 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002114
2115 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002116#else
2117 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2118 {
2119 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002120 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002121 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002122#endif
2123
2124 /* Load ramdisk & kernel */
2125 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002126 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002127
2128#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002129 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2130 {
2131 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2132 return;
2133 }
2134
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002135 /*
2136 * If dtb is not found look for appended DTB in the kernel.
2137 * If appended dev tree is found, update the atags with
2138 * memory address to the DTB appended location on RAM.
2139 * Else update with the atags address in the kernel header
2140 */
2141 if (!dtb_copied) {
2142 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002143 dtb = dev_tree_appended((void*)(ptr + page_size),
2144 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002145 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002146 if (!dtb) {
2147 fastboot_fail("dtb not found");
2148 return;
2149 }
Amol Jadicb524072012-08-09 16:40:18 -07002150 }
2151#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002152
2153 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002154 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002155
Dima Zavin77e41f32013-03-06 16:10:43 -08002156 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002157 (const char*) hdr->cmdline, board_machtype(),
2158 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002159}
2160
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002161void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002162{
2163 struct ptentry *ptn;
2164 struct ptable *ptable;
2165
2166 ptable = flash_get_ptable();
2167 if (ptable == NULL) {
2168 fastboot_fail("partition table doesn't exist");
2169 return;
2170 }
2171
2172 ptn = ptable_find(ptable, arg);
2173 if (ptn == NULL) {
2174 fastboot_fail("unknown partition name");
2175 return;
2176 }
2177
2178 if (flash_erase(ptn)) {
2179 fastboot_fail("failed to erase partition");
2180 return;
2181 }
2182 fastboot_okay("");
2183}
2184
Bikas Gurungd48bd242010-09-04 19:54:32 -07002185
2186void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2187{
2188 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002189 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002190 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002191 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002192
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002193#if VERIFIED_BOOT
2194 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2195 {
2196 if(!device.is_unlocked)
2197 {
2198 fastboot_fail("unlock device to erase keystore");
2199 return;
2200 }
2201 }
2202#endif
2203
Kinson Chikf1a43512011-07-14 11:28:39 -07002204 index = partition_get_index(arg);
2205 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002206 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002207
Kinson Chikf1a43512011-07-14 11:28:39 -07002208 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002209 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002210 return;
2211 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002212
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002213 lun = partition_get_lun(index);
2214 mmc_set_lun(lun);
2215
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002216 if (platform_boot_dev_isemmc())
2217 {
2218 if (mmc_erase_card(ptn, size)) {
2219 fastboot_fail("failed to erase partition\n");
2220 return;
2221 }
2222 } else {
2223 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2224 size = partition_get_size(index);
2225 if (size > DEFAULT_ERASE_SIZE)
2226 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002227
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002228 /* Simple inefficient version of erase. Just writing
2229 0 in first several blocks */
2230 if (mmc_write(ptn , size, (unsigned int *)out)) {
2231 fastboot_fail("failed to erase partition");
2232 return;
2233 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002234 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002235#if VERIFIED_BOOT
2236 if(!(strncmp(arg, "userdata", 8)))
2237 if(send_delete_keys_to_tz())
2238 ASSERT(0);
2239#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002240 fastboot_okay("");
2241}
2242
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002243void cmd_erase(const char *arg, void *data, unsigned sz)
2244{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002245#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002246 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002247 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002248 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002249 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002250 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002251 return;
2252 }
2253 }
2254#endif
2255
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002256 if(target_is_emmc_boot())
2257 cmd_erase_mmc(arg, data, sz);
2258 else
2259 cmd_erase_nand(arg, data, sz);
2260}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002261
Ajay Dudani5c761132011-04-07 20:19:04 -07002262void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002263{
2264 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002265 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002266 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002267 char *token = NULL;
2268 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002269 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002270 uint8_t lun = 0;
2271 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002272
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002273 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002274 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002275 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002276 if(token)
2277 {
2278 lun = atoi(token);
2279 mmc_set_lun(lun);
2280 lun_set = true;
2281 }
2282
Mao Jinlong226f33a2014-07-04 17:24:10 +08002283 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002284 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002285 if (!strcmp(pname, "partition"))
2286 {
2287 dprintf(INFO, "Attempt to write partition image.\n");
2288 if (write_partition(sz, (unsigned char *) data)) {
2289 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002290 return;
2291 }
2292 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002293 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002294 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002295#if VERIFIED_BOOT
2296 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2297 {
2298 if(!device.is_unlocked)
2299 {
2300 fastboot_fail("unlock device to flash keystore");
2301 return;
2302 }
2303 if(!boot_verify_validate_keystore((unsigned char *)data))
2304 {
2305 fastboot_fail("image is not a keystore file");
2306 return;
2307 }
2308 }
2309#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002310 index = partition_get_index(pname);
2311 ptn = partition_get_offset(index);
2312 if(ptn == 0) {
2313 fastboot_fail("partition table doesn't exist");
2314 return;
2315 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002316
Mao Jinlong226f33a2014-07-04 17:24:10 +08002317 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2318 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2319 fastboot_fail("image is not a boot image");
2320 return;
2321 }
2322 }
2323
2324 if(!lun_set)
2325 {
2326 lun = partition_get_lun(index);
2327 mmc_set_lun(lun);
2328 }
2329
2330 size = partition_get_size(index);
2331 if (ROUND_TO_PAGE(sz,511) > size) {
2332 fastboot_fail("size too large");
2333 return;
2334 }
2335 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2336 fastboot_fail("flash write failure");
2337 return;
2338 }
Greg Grisco6e754772011-06-23 12:19:39 -07002339 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002340 }
2341 fastboot_okay("");
2342 return;
2343}
2344
Ajay Dudanide984792015-03-02 09:57:41 -08002345void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2346{
2347 int i, images;
2348 meta_header_t *meta_header;
2349 img_header_entry_t *img_header_entry;
2350
2351 meta_header = (meta_header_t*) data;
2352 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2353
2354 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2355
2356 for (i=0; i<images; i++) {
2357
2358 if((img_header_entry[i].ptn_name == NULL) ||
2359 (img_header_entry[i].start_offset == 0) ||
2360 (img_header_entry[i].size == 0))
2361 break;
2362
2363 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2364 (void *) data + img_header_entry[i].start_offset,
2365 img_header_entry[i].size);
2366 }
2367
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002368 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2369 {
2370 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2371 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2372 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2373 }
2374 else
2375 {
2376 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2377 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2378 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2379 }
2380
2381 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002382 fastboot_okay("");
2383 return;
2384}
2385
Ajay Dudani5c761132011-04-07 20:19:04 -07002386void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2387{
2388 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002389 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002390 uint32_t *fill_buf = NULL;
2391 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002392 sparse_header_t *sparse_header;
2393 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002394 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002395 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302396 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002397 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302398 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002399 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302400 /*End of the sparse image address*/
2401 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002402
Kinson Chikf1a43512011-07-14 11:28:39 -07002403 index = partition_get_index(arg);
2404 ptn = partition_get_offset(index);
2405 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002406 fastboot_fail("partition table doesn't exist");
2407 return;
2408 }
2409
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302410 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302411
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002412 lun = partition_get_lun(index);
2413 mmc_set_lun(lun);
2414
vijay kumar800255e2015-04-24 20:26:19 +05302415 if (sz < sizeof(sparse_header_t)) {
2416 fastboot_fail("size too low");
2417 return;
2418 }
2419
Ajay Dudani5c761132011-04-07 20:19:04 -07002420 /* Read and skip over sparse image header */
2421 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302422
vijay kumar1321f342015-03-27 12:13:42 +05302423 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002424 fastboot_fail("size too large");
2425 return;
2426 }
2427
vijay kumarde4fcf62015-04-23 13:05:49 +05302428 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302429
2430 if (data_end < (uint32_t)data) {
2431 fastboot_fail("buffer overreads occured due to invalid sparse header");
2432 return;
2433 }
2434
vijay kumarde4fcf62015-04-23 13:05:49 +05302435 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002436 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302437 fastboot_fail("sparse header size mismatch");
2438 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002439 }
2440
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002441 dprintf (SPEW, "=== Sparse Image Header ===\n");
2442 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2443 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2444 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2445 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2446 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2447 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2448 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2449 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002450
2451 /* Start processing chunks */
2452 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2453 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302454 /* Make sure the total image size does not exceed the partition size */
2455 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2456 fastboot_fail("size too large");
2457 return;
2458 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002459 /* Read and skip over chunk header */
2460 chunk_header = (chunk_header_t *) data;
2461 data += sizeof(chunk_header_t);
2462
vijay kumar800255e2015-04-24 20:26:19 +05302463 if (data_end < (uint32_t)data) {
2464 fastboot_fail("buffer overreads occured due to invalid sparse header");
2465 return;
2466 }
2467
Ajay Dudani5c761132011-04-07 20:19:04 -07002468 dprintf (SPEW, "=== Chunk Header ===\n");
2469 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2470 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2471 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2472
vijay kumar800255e2015-04-24 20:26:19 +05302473 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002474 {
vijay kumar800255e2015-04-24 20:26:19 +05302475 fastboot_fail("chunk header size mismatch");
2476 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002477 }
2478
wufeng.jiang813dc352015-06-02 23:02:46 -04002479 if (!sparse_header->blk_sz ){
2480 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302481 return;
2482 }
2483
wufeng.jiang813dc352015-06-02 23:02:46 -04002484 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2485
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302486 /* Make sure that the chunk size calculated from sparse image does not
2487 * exceed partition size
2488 */
2489 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2490 {
2491 fastboot_fail("Chunk data size exceeds partition size");
2492 return;
2493 }
2494
Ajay Dudani5c761132011-04-07 20:19:04 -07002495 switch (chunk_header->chunk_type)
2496 {
2497 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002498 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002499 chunk_data_sz))
2500 {
2501 fastboot_fail("Bogus chunk size for chunk type Raw");
2502 return;
2503 }
2504
vijay kumar800255e2015-04-24 20:26:19 +05302505 if (data_end < (uint32_t)data + chunk_data_sz) {
2506 fastboot_fail("buffer overreads occured due to invalid sparse header");
2507 return;
2508 }
2509
wufeng.jiang813dc352015-06-02 23:02:46 -04002510 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2511 otherwise it will return in the line above
2512 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002513 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002514 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002515 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002516 {
2517 fastboot_fail("flash write failure");
2518 return;
2519 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302520 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2521 fastboot_fail("Bogus size for RAW chunk type");
2522 return;
2523 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002524 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002525 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002526 break;
2527
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002528 case CHUNK_TYPE_FILL:
2529 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2530 sizeof(uint32_t)))
2531 {
2532 fastboot_fail("Bogus chunk size for chunk type FILL");
2533 return;
2534 }
2535
2536 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2537 if (!fill_buf)
2538 {
2539 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2540 return;
2541 }
2542
vijay kumar800255e2015-04-24 20:26:19 +05302543 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2544 fastboot_fail("buffer overreads occured due to invalid sparse header");
2545 return;
2546 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002547 fill_val = *(uint32_t *)data;
2548 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002549
2550 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2551 {
2552 fill_buf[i] = fill_val;
2553 }
2554
wufeng.jiang813dc352015-06-02 23:02:46 -04002555 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002556 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302557 /* Make sure that the data written to partition does not exceed partition size */
2558 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2559 {
2560 fastboot_fail("Chunk data size for fill type exceeds partition size");
2561 return;
2562 }
2563
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002564 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2565 sparse_header->blk_sz,
2566 fill_buf))
2567 {
2568 fastboot_fail("flash write failure");
2569 free(fill_buf);
2570 return;
2571 }
2572
2573 total_blocks++;
2574 }
2575
2576 free(fill_buf);
2577 break;
2578
Ajay Dudani5c761132011-04-07 20:19:04 -07002579 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302580 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2581 fastboot_fail("bogus size for chunk DONT CARE type");
2582 return;
2583 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002584 total_blocks += chunk_header->chunk_sz;
2585 break;
2586
Ajay Dudani5c761132011-04-07 20:19:04 -07002587 case CHUNK_TYPE_CRC:
2588 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2589 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002590 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002591 return;
2592 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302593 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2594 fastboot_fail("bogus size for chunk CRC type");
2595 return;
2596 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002597 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302598 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2599 fastboot_fail("integer overflow occured");
2600 return;
2601 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002602 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302603 if (data_end < (uint32_t)data) {
2604 fastboot_fail("buffer overreads occured due to invalid sparse header");
2605 return;
2606 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002607 break;
2608
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002609 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002610 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002611 fastboot_fail("Unknown chunk type");
2612 return;
2613 }
2614 }
2615
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002616 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2617 total_blocks, sparse_header->total_blks);
2618
2619 if(total_blocks != sparse_header->total_blks)
2620 {
2621 fastboot_fail("sparse image write failure");
2622 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002623
2624 fastboot_okay("");
2625 return;
2626}
2627
2628void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2629{
2630 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002631 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002632
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002633#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002634 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2635 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002636 int ret=0;
2637 uint32 major_version=0;
2638 uint32 minor_version=0;
2639
2640 ret = scm_svc_version(&major_version,&minor_version);
2641 if(!ret)
2642 {
2643 if(major_version >= 2)
2644 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002645 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002646 {
2647 ret = encrypt_scm((uint32 **) &data, &sz);
2648 if (ret != 0) {
2649 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2650 return;
2651 }
2652
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002653 /* Protect only for SSD */
2654 if (!strcmp(arg, "ssd")) {
2655 ret = scm_protect_keystore((uint32 *) data, sz);
2656 if (ret != 0) {
2657 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2658 return;
2659 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002660 }
2661 }
2662 else
2663 {
2664 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2665 if(ret != 0)
2666 {
2667 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2668 return;
2669 }
2670 }
2671 }
2672 else
2673 {
2674 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2675 magic_number[1] == DECRYPT_MAGIC_1)
2676 {
2677 ret = decrypt_scm((uint32 **) &data, &sz);
2678 if (ret != 0) {
2679 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2680 return;
2681 }
2682 }
2683 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2684 magic_number[1] == ENCRYPT_MAGIC_1)
2685 {
2686 ret = encrypt_scm((uint32 **) &data, &sz);
2687 if (ret != 0) {
2688 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2689 return;
2690 }
2691 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002692 }
2693 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002694 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002695 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002696 dprintf(CRITICAL,"INVALID SVC Version\n");
2697 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002698 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002699#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002700
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002701#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002702 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002703 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002704 if(!device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002705 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002706 fastboot_fail("device is locked. Cannot flash images");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002707 return;
2708 }
2709 }
2710#endif
2711
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002712 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002713 meta_header = (meta_header_t *) data;
2714 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002715 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002716 else if (meta_header->magic == META_HEADER_MAGIC)
2717 cmd_flash_meta_img(arg, data, sz);
2718 else
2719 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002720
2721#if VERIFIED_BOOT
2722 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
2723 {
2724 // reset dm_verity mode to enforcing
2725 device.verity_mode = 1;
2726 write_device_info(&device);
2727 }
2728#endif
2729
Ajay Dudani5c761132011-04-07 20:19:04 -07002730 return;
2731}
2732
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002733void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2734{
2735 struct ptentry *sys_ptn;
2736 struct ptable *ptable;
2737
2738 ptable = flash_get_ptable();
2739 if (ptable == NULL) {
2740 fastboot_fail("partition table doesn't exist");
2741 return;
2742 }
2743
2744 sys_ptn = ptable_find(ptable, "system");
2745 if (sys_ptn == NULL) {
2746 fastboot_fail("system partition not found");
2747 return;
2748 }
2749
2750 sz = ROUND_TO_PAGE(sz, page_mask);
2751 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2752 fastboot_fail("update_ubi_vol failed");
2753 else
2754 fastboot_okay("");
2755}
2756
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002757void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002758{
2759 struct ptentry *ptn;
2760 struct ptable *ptable;
2761 unsigned extra = 0;
2762
2763 ptable = flash_get_ptable();
2764 if (ptable == NULL) {
2765 fastboot_fail("partition table doesn't exist");
2766 return;
2767 }
2768
2769 ptn = ptable_find(ptable, arg);
2770 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002771 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2772 arg);
2773 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002774 return;
2775 }
2776
2777 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2778 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2779 fastboot_fail("image is not a boot image");
2780 return;
2781 }
2782 }
2783
Amol Jadi5c61a952012-05-04 17:05:35 -07002784 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002785 || !strcmp(ptn->name, "userdata")
2786 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002787 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002788 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002789 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002790 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002791 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002792
2793 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002794 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2795 if (flash_ubi_img(ptn, data, sz)) {
2796 fastboot_fail("flash write failure");
2797 return;
2798 }
2799 } else {
2800 if (flash_write(ptn, extra, data, sz)) {
2801 fastboot_fail("flash write failure");
2802 return;
2803 }
Dima Zavin214cc642009-01-26 11:16:21 -08002804 }
2805 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2806 fastboot_okay("");
2807}
2808
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002809void cmd_flash(const char *arg, void *data, unsigned sz)
2810{
2811 if(target_is_emmc_boot())
2812 cmd_flash_mmc(arg, data, sz);
2813 else
2814 cmd_flash_nand(arg, data, sz);
2815}
2816
Dima Zavin214cc642009-01-26 11:16:21 -08002817void cmd_continue(const char *arg, void *data, unsigned sz)
2818{
2819 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002820 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002821
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002822 if (target_is_emmc_boot())
2823 {
lijuanga40d6302015-07-20 20:10:13 +08002824#if FBCON_DISPLAY_MSG
2825 keys_detect_init();
2826#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002827 boot_linux_from_mmc();
2828 }
2829 else
2830 {
2831 boot_linux_from_flash();
2832 }
Dima Zavin214cc642009-01-26 11:16:21 -08002833}
2834
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002835void cmd_reboot(const char *arg, void *data, unsigned sz)
2836{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002837 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002838 fastboot_okay("");
2839 reboot_device(0);
2840}
2841
2842void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2843{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002844 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002845 fastboot_okay("");
2846 reboot_device(FASTBOOT_MODE);
2847}
2848
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002849void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2850{
2851 dprintf(INFO, "Enabling charger screen check\n");
2852 device.charger_screen_enabled = 1;
2853 write_device_info(&device);
2854 fastboot_okay("");
2855}
2856
2857void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2858{
2859 dprintf(INFO, "Disabling charger screen check\n");
2860 device.charger_screen_enabled = 0;
2861 write_device_info(&device);
2862 fastboot_okay("");
2863}
2864
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302865void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2866{
2867 dprintf(INFO, "Selecting display panel %s\n", arg);
2868 if (arg)
2869 strlcpy(device.display_panel, arg,
2870 sizeof(device.display_panel));
2871 write_device_info(&device);
2872 fastboot_okay("");
2873}
2874
Shashank Mittal162244e2011-08-08 19:01:25 -07002875void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2876{
vijay kumarc65876c2015-04-24 13:29:16 +05302877 if(!is_allow_unlock) {
2878 fastboot_fail("oem unlock is not allowed");
2879 return;
2880 }
2881
lijuanga40d6302015-07-20 20:10:13 +08002882#if FBCON_DISPLAY_MSG
2883 if(!device.is_unlocked)
2884 display_menu_thread(DISPLAY_THREAD_UNLOCK);
2885 else
2886 fastboot_info("Device already unlocked!");
2887 fastboot_okay("");
2888#else
vijay kumarc65876c2015-04-24 13:29:16 +05302889 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
lijuanga40d6302015-07-20 20:10:13 +08002890#endif
vijay kumarc65876c2015-04-24 13:29:16 +05302891}
2892
2893void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
2894{
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002895 if(!device.is_unlocked)
Shashank Mittal162244e2011-08-08 19:01:25 -07002896 {
vijay kumarc65876c2015-04-24 13:29:16 +05302897 if(!is_allow_unlock) {
2898 fastboot_fail("oem unlock is not allowed");
2899 return;
2900 }
2901
Shashank Mittal162244e2011-08-08 19:01:25 -07002902 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002903 write_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05302904
2905 struct recovery_message msg;
2906 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2907 write_misc(0, &msg, sizeof(msg));
2908
2909 fastboot_okay("");
2910 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002911 }
2912 fastboot_okay("");
2913}
2914
2915void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2916{
Sridhar Parasuram8b792422015-07-05 11:38:13 -07002917 struct recovery_message msg;
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002918 if(device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002919 {
2920 device.is_unlocked = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002921 write_device_info(&device);
Sridhar Parasuram8b792422015-07-05 11:38:13 -07002922 // upon oem lock, reboot to recovery to wipe user data
2923 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2924 write_misc(0, &msg, sizeof(msg));
2925 fastboot_okay("");
2926 reboot_device(RECOVERY_MODE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002927 }
2928 fastboot_okay("");
2929}
2930
Shashank Mittala0032282011-08-26 14:50:11 -07002931void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2932{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302933 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002934 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002935 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002936 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2937 fastboot_info(response);
2938 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002939 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302940 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2941 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002942 fastboot_okay("");
2943}
2944
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002945void cmd_preflash(const char *arg, void *data, unsigned sz)
2946{
2947 fastboot_okay("");
2948}
2949
Mao Flynn7b379f32015-04-20 00:28:30 +08002950static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302951
Mao Flynn7b379f32015-04-20 00:28:30 +08002952int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302953{
Mao Flynn7b379f32015-04-20 00:28:30 +08002954 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302955 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08002956 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302957 return -1;
2958 return 0;
2959}
2960
Mao Flynn7b379f32015-04-20 00:28:30 +08002961int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002962{
2963 struct ptentry *ptn;
2964 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08002965 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002966 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002967
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302968 ptable = flash_get_ptable();
2969 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002970 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2971 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302972 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002973
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302974 ptn = ptable_find(ptable, "splash");
2975 if (ptn == NULL) {
2976 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002977 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302978 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002979 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302980 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002981 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302982 }
2983
Mao Flynn7b379f32015-04-20 00:28:30 +08002984 header = (struct logo_img_header *)logo_header;
2985 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302986 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002987 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302988 }
2989
2990 fb_display = fbcon_display();
2991 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002992 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
2993 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
2994
2995 /* if the logo is full-screen size, remove "fbcon_clear()" */
2996 if ((header->width != fb_display->width)
2997 || (header->height != fb_display->height))
2998 fbcon_clear();
2999
3000 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3001 (uint32_t *)base,
3002 (header->blocks * 512))) {
3003 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3004 return -1;
3005 }
3006 fbcon_extract_to_screen(header, base);
3007 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003008 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003009
3010 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3011 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3012 return -1;
3013 }
3014
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303015 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003016 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3017 (uint32_t *)base,
3018 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303019 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303020 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003021 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003022 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303023 }
3024
Mao Flynn7b379f32015-04-20 00:28:30 +08003025 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303026}
3027
Mao Flynn7b379f32015-04-20 00:28:30 +08003028int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303029{
3030 int index = INVALID_PTN;
3031 unsigned long long ptn = 0;
3032 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003033 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003034 uint32_t blocksize, realsize, readsize;
3035 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303036
3037 index = partition_get_index("splash");
3038 if (index == 0) {
3039 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003040 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303041 }
3042
3043 ptn = partition_get_offset(index);
3044 if (ptn == 0) {
3045 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003046 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303047 }
3048
Mao Flynn1893a7f2015-06-03 12:03:36 +08003049 mmc_set_lun(partition_get_lun(index));
3050
3051 blocksize = mmc_get_device_blocksize();
3052 if (blocksize == 0) {
3053 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3054 return -1;
3055 }
3056
3057 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003058 if (!fb_display)
3059 {
3060 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3061 return -1;
3062 }
3063
Mao Flynn1893a7f2015-06-03 12:03:36 +08003064 base = (uint8_t *) fb_display->base;
3065
3066 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303067 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003068 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303069 }
3070
Mao Flynn1893a7f2015-06-03 12:03:36 +08003071 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003072 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303073 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003074 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303075 }
3076
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303077 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003078 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3079 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003080
Mao Flynn1893a7f2015-06-03 12:03:36 +08003081 realsize = header->blocks * 512;
3082 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3083
3084 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003085 if ((header->width != fb_display->width)
3086 || (header->height != fb_display->height))
3087 fbcon_clear();
3088
Mao Flynn1893a7f2015-06-03 12:03:36 +08003089 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003090 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3091 return -1;
3092 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003093
Mao Flynn1893a7f2015-06-03 12:03:36 +08003094 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3095 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303096
Mao Flynn1893a7f2015-06-03 12:03:36 +08003097 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3098 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3099 return -1;
3100 }
3101
3102 realsize = header->width * header->height * fb_display->bpp / 8;
3103 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3104
3105 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3106 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3107 fbcon_clear();
3108 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3109 return -1;
3110 }
3111 } else {
3112 if (mmc_read(ptn + blocksize ,
3113 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3114 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3115 return -1;
3116 }
3117 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3118 }
3119 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303120 }
3121
Mao Flynn7b379f32015-04-20 00:28:30 +08003122 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303123}
3124
Mao Flynn7b379f32015-04-20 00:28:30 +08003125int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303126{
3127 if (target_is_emmc_boot()) {
3128 return splash_screen_mmc();
3129 } else {
3130 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003131 }
3132}
3133
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003134/* Get the size from partiton name */
3135static void get_partition_size(const char *arg, char *response)
3136{
3137 uint64_t ptn = 0;
3138 uint64_t size;
3139 int index = INVALID_PTN;
3140
3141 index = partition_get_index(arg);
3142
3143 if (index == INVALID_PTN)
3144 {
3145 dprintf(CRITICAL, "Invalid partition index\n");
3146 return;
3147 }
3148
3149 ptn = partition_get_offset(index);
3150
3151 if(!ptn)
3152 {
3153 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3154 return;
3155 }
3156
3157 size = partition_get_size(index);
3158
3159 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3160 return;
3161}
3162
3163/*
3164 * Publish the partition type & size info
3165 * fastboot getvar will publish the required information.
3166 * fastboot getvar partition_size:<partition_name>: partition size in hex
3167 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3168 */
3169static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3170{
3171 uint8_t i;
3172
3173 for (i = 0; i < num_parts; i++) {
3174 get_partition_size(info[i].part_name, info[i].size_response);
3175
3176 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3177 {
3178 dprintf(CRITICAL, "partition size name truncated\n");
3179 return;
3180 }
3181 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3182 {
3183 dprintf(CRITICAL, "partition type name truncated\n");
3184 return;
3185 }
3186
3187 /* publish partition size & type info */
3188 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3189 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3190 }
3191}
3192
Amol Jadi5edf3552013-07-23 14:15:34 -07003193/* register commands and variables for fastboot */
3194void aboot_fastboot_register_commands(void)
3195{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003196 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07003197
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003198 struct fastboot_cmd_desc cmd_list[] = {
3199 /* By default the enabled list is empty. */
3200 {"", NULL},
3201 /* move commands enclosed within the below ifndef to here
3202 * if they need to be enabled in user build.
3203 */
3204#ifndef DISABLE_FASTBOOT_CMDS
3205 /* Register the following commands only for non-user builds */
3206 {"flash:", cmd_flash},
3207 {"erase:", cmd_erase},
3208 {"boot", cmd_boot},
3209 {"continue", cmd_continue},
3210 {"reboot", cmd_reboot},
3211 {"reboot-bootloader", cmd_reboot_bootloader},
3212 {"oem unlock", cmd_oem_unlock},
vijay kumarc65876c2015-04-24 13:29:16 +05303213 {"oem unlock-go", cmd_oem_unlock_go},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003214 {"oem lock", cmd_oem_lock},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003215 {"oem device-info", cmd_oem_devinfo},
3216 {"preflash", cmd_preflash},
3217 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3218 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08003219 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003220#endif
3221 };
3222
3223 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3224 for (i = 1; i < fastboot_cmds_count; i++)
3225 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3226
Amol Jadi5edf3552013-07-23 14:15:34 -07003227 /* publish variables and their values */
3228 fastboot_publish("product", TARGET(BOARD));
3229 fastboot_publish("kernel", "lk");
3230 fastboot_publish("serialno", sn_buf);
3231
3232 /*
3233 * partition info is supported only for emmc partitions
3234 * Calling this for NAND prints some error messages which
3235 * is harmless but misleading. Avoid calling this for NAND
3236 * devices.
3237 */
3238 if (target_is_emmc_boot())
3239 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3240
3241 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003242 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3243 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003244 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003245 /* Is the charger screen check enabled */
3246 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3247 device.charger_screen_enabled);
3248 fastboot_publish("charger-screen-enabled",
3249 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303250 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3251 device.display_panel);
3252 fastboot_publish("display-panel",
3253 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003254 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3255 fastboot_publish("version-baseband", (const char *) device.radio_version);
Amol Jadi5edf3552013-07-23 14:15:34 -07003256}
3257
Brian Swetland9c4c0752009-01-25 16:23:50 -08003258void aboot_init(const struct app_descriptor *app)
3259{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003260 unsigned reboot_mode = 0;
lijuang72875802015-02-10 10:35:12 +08003261 unsigned hard_reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03003262 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003263
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003264 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003265 if (target_is_emmc_boot())
3266 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003267 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003268 page_mask = page_size - 1;
3269 }
3270 else
3271 {
3272 page_size = flash_page_size();
3273 page_mask = page_size - 1;
3274 }
3275
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003276 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3277
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003278 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303279 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003280
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003281 /* Display splash screen if enabled */
3282#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003283#if NO_ALARM_DISPLAY
3284 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003285#endif
lijuang99c02d82015-02-13 19:04:34 +08003286 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003287#if ENABLE_WBC
3288 if (!pm_appsbl_display_init_done())
3289 target_display_init(device.display_panel);
3290 else
3291 display_image_on_screen();
3292#else
lijuang99c02d82015-02-13 19:04:34 +08003293 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003294#endif
lijuang99c02d82015-02-13 19:04:34 +08003295 dprintf(SPEW, "Display Init: Done\n");
3296#if NO_ALARM_DISPLAY
3297 }
3298#endif
3299#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003300
Greg Griscod6250552011-06-29 14:40:23 -07003301 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003302 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003303
Dhaval Patel223ec952013-07-18 14:49:44 -07003304 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3305
Matthew Qindefd5562014-07-11 18:02:40 +08003306 /*
3307 * Check power off reason if user force reset,
3308 * if yes phone will do normal boot.
3309 */
3310 if (is_user_force_reset())
3311 goto normal_boot;
3312
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003313 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003314 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003315 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003316 dprintf(ALWAYS,"dload mode key sequence detected\n");
3317 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003318 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003319 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003320 }
3321 else
3322 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303323 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003324 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003325 }
3326 boot_into_fastboot = true;
3327 }
3328 if (!boot_into_fastboot)
3329 {
3330 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3331 boot_into_recovery = 1;
3332 if (!boot_into_recovery &&
3333 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003334 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003335 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003336 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003337 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003338 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003339 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003340
Ajay Dudani77421292010-10-27 19:34:06 -07003341 reboot_mode = check_reboot_mode();
lijuang72875802015-02-10 10:35:12 +08003342 hard_reboot_mode = check_hard_reboot_mode();
3343 if (reboot_mode == RECOVERY_MODE ||
3344 hard_reboot_mode == RECOVERY_HARD_RESET_MODE) {
Ajay Dudani77421292010-10-27 19:34:06 -07003345 boot_into_recovery = 1;
lijuang72875802015-02-10 10:35:12 +08003346 } else if(reboot_mode == FASTBOOT_MODE ||
3347 hard_reboot_mode == FASTBOOT_HARD_RESET_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003348 boot_into_fastboot = true;
lijuang72875802015-02-10 10:35:12 +08003349 } else if(reboot_mode == ALARM_BOOT ||
3350 hard_reboot_mode == RTC_HARD_RESET_MODE) {
Matthew Qind886f3c2014-01-17 16:52:01 +08003351 boot_reason_alarm = true;
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003352 } else if(reboot_mode == DM_VERITY_ENFORCING ||
3353 hard_reboot_mode == DM_VERITY_ENFORCING_HARD_RESET_MODE) {
3354 device.verity_mode = 1;
3355 write_device_info(&device);
3356 } else if(reboot_mode == DM_VERITY_LOGGING ||
3357 hard_reboot_mode == DM_VERITY_LOGGING_HARD_RESET_MODE) {
3358 device.verity_mode = 0;
3359 write_device_info(&device);
3360 } else if(reboot_mode == DM_VERITY_KEYSCLEAR ||
3361 hard_reboot_mode == DM_VERITY_KEYSCLEAR_HARD_RESET_MODE) {
3362 if(send_delete_keys_to_tz())
3363 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003364 }
3365
Matthew Qindefd5562014-07-11 18:02:40 +08003366normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003367 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003368 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003369 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003370 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003371 if(emmc_recovery_init())
3372 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3373 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003374 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003375 if((device.is_unlocked) || (device.is_tampered))
3376 {
3377 #ifdef TZ_TAMPER_FUSE
3378 set_tamper_fuse_cmd();
3379 #endif
3380 #if USE_PCOM_SECBOOT
3381 set_tamper_flag(device.is_tampered);
3382 #endif
3383 }
Shashank Mittala0032282011-08-26 14:50:11 -07003384 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003385
Pavel Nedev5d91d412013-04-29 11:34:24 +03003386 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003387 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003388 else
3389 {
3390 recovery_init();
3391 #if USE_PCOM_SECBOOT
3392 if((device.is_unlocked) || (device.is_tampered))
3393 set_tamper_flag(device.is_tampered);
3394 #endif
3395 boot_linux_from_flash();
3396 }
3397 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3398 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003399 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003400
Amol Jadi5edf3552013-07-23 14:15:34 -07003401 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003402
Amol Jadi5edf3552013-07-23 14:15:34 -07003403 /* register aboot specific fastboot commands */
3404 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003405
Amol Jadi5edf3552013-07-23 14:15:34 -07003406 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003407 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003408
3409 /* initialize and start fastboot */
3410 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08003411}
3412
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003413uint32_t get_page_size()
3414{
3415 return page_size;
3416}
3417
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003418/*
3419 * Calculated and save hash (SHA256) for non-signed boot image.
3420 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003421 * @param image_addr - Boot image address
3422 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003423 *
3424 * @return int - 0 on success, negative value on failure.
3425 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003426static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003427{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003428 unsigned int digest[8];
3429#if IMAGE_VERIF_ALGO_SHA1
3430 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3431#else
3432 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3433#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003434
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003435 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003436 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003437
3438 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003439 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003440
3441 return 0;
3442}
3443
Brian Swetland9c4c0752009-01-25 16:23:50 -08003444APP_START(aboot)
3445 .init = aboot_init,
3446APP_END