blob: 9d139a4a64c98e9f610e905bdc67e2e545542b3d [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;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001857 write_device_info(info);
1858 }
1859 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001860 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001861 }
1862 else
1863 {
1864 read_device_info_flash(dev);
1865 }
1866}
1867
1868void reset_device_info()
1869{
1870 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001871 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001872 write_device_info(&device);
1873}
1874
1875void set_device_root()
1876{
1877 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001878 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001879 write_device_info(&device);
1880}
1881
Amol Jadicb524072012-08-09 16:40:18 -07001882#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001883int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
Amol Jadicb524072012-08-09 16:40:18 -07001884{
1885 uint32 dt_image_offset = 0;
1886 uint32_t n;
1887 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001888 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001889 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001890 unsigned int compressed_size = 0;
1891 unsigned int dtb_size = 0;
1892 unsigned int out_avai_len = 0;
1893 unsigned char *out_addr = NULL;
1894 unsigned char *best_match_dt_addr = NULL;
1895 int rc;
Amol Jadicb524072012-08-09 16:40:18 -07001896
1897 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1898
Amol Jadicb524072012-08-09 16:40:18 -07001899 if(hdr->dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001900 /* add kernel offset */
1901 dt_image_offset += page_size;
1902 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1903 dt_image_offset += n;
1904
1905 /* add ramdisk offset */
1906 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1907 dt_image_offset += n;
1908
1909 /* add second offset */
1910 if(hdr->second_size != 0) {
1911 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1912 dt_image_offset += n;
1913 }
1914
1915 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001916 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001917
Deepa Dinamani19648b42013-09-05 17:05:55 -07001918 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001919 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1920 return -1;
1921 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001922 /* Find index of device tree within device tree table */
1923 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001924 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1925 return -1;
1926 }
1927
Matthew Qin271927e2015-03-31 22:07:07 -04001928 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
1929 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
1930 {
1931 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
1932 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04001933 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001934 rc = decompress(best_match_dt_addr,
1935 dt_entry.size, out_addr, out_avai_len,
1936 &compressed_size, &dtb_size);
1937 if (rc)
1938 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001939 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001940 ASSERT(0);
1941 }
1942
Matthew Qin0b15b322015-05-19 05:20:54 -04001943 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001944 best_match_dt_addr = out_addr;
1945 } else {
1946 dtb_size = dt_entry.size;
1947 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001948 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001949 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001950 {
1951 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1952 return -1;
1953 }
1954
Amol Jadicb524072012-08-09 16:40:18 -07001955 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001956 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001957 } else
1958 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001959
1960 /* Everything looks fine. Return success. */
1961 return 0;
1962}
1963#endif
1964
Brian Swetland9c4c0752009-01-25 16:23:50 -08001965void cmd_boot(const char *arg, void *data, unsigned sz)
1966{
Amit Blay8a510302015-08-17 09:20:01 +03001967#ifdef MDTP_SUPPORT
1968 static bool is_mdtp_activated = 0;
1969#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08001970 unsigned kernel_actual;
1971 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001972 uint32_t image_actual;
1973 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001974 uint32_t sig_actual = SIGNATURE_SIZE;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001975 struct boot_img_hdr *hdr = NULL;
1976 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001977 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001978 int ret = 0;
1979 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001980 unsigned int out_len = 0;
1981 unsigned int out_avai_len = 0;
1982 unsigned char *out_addr = NULL;
1983 uint32_t dtb_offset = 0;
1984 unsigned char *kernel_start_addr = NULL;
1985 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04001986 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001987
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001988#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07001989 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001990 {
1991 fastboot_fail("unlock device to use this command");
1992 return;
1993 }
1994#endif
1995
Brian Swetland9c4c0752009-01-25 16:23:50 -08001996 if (sz < sizeof(hdr)) {
1997 fastboot_fail("invalid bootimage header");
1998 return;
1999 }
2000
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002001 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002002
2003 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002004 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002005
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002006 if(target_is_emmc_boot() && hdr->page_size) {
2007 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002008 page_mask = page_size - 1;
2009 }
2010
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002011 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2012 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002013#if DEVICE_TREE
2014 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2015#endif
2016
2017 image_actual = ADD_OF(page_size, kernel_actual);
2018 image_actual = ADD_OF(image_actual, ramdisk_actual);
2019 image_actual = ADD_OF(image_actual, dt_actual);
2020
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002021 if (target_use_signed_kernel() && (!device.is_unlocked))
2022 image_actual = ADD_OF(image_actual, sig_actual);
2023
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002024 /* sz should have atleast raw boot image */
2025 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002026 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002027 return;
2028 }
2029
2030 /* Verify the boot image
2031 * device & page_size are initialized in aboot_init
2032 */
2033 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002034 /* Pass size excluding signature size, otherwise we would try to
2035 * access signature beyond its length
2036 */
2037 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002038
Amit Blay4aa292f2015-04-28 21:55:59 +03002039#ifdef MDTP_SUPPORT
2040 else
2041 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002042 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002043 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002044
2045 if (!is_mdtp_activated) {
2046 ext_partition.partition = MDTP_PARTITION_NONE;
2047 mdtp_fwlock_verify_lock(&ext_partition);
2048 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002049 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002050
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002051 mdtp_activated(&is_mdtp_activated);
2052 if(is_mdtp_activated){
2053 dprintf(CRITICAL, "fastboot boot command is not available.\n");
2054 return;
2055 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002056#endif /* MDTP_SUPPORT */
2057
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002058 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002059 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2060 * If not, continue booting.
2061 */
2062 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2063 {
2064 out_addr = (unsigned char *)target_get_scratch_address();
2065 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2066 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002067 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002068 ret = decompress((unsigned char *)(ptr + page_size),
2069 hdr->kernel_size, out_addr, out_avai_len,
2070 &dtb_offset, &out_len);
2071 if (ret)
2072 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002073 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002074 ASSERT(0);
2075 }
2076
Matthew Qin0b15b322015-05-19 05:20:54 -04002077 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002078 kptr = (struct kernel64_hdr *)out_addr;
2079 kernel_start_addr = out_addr;
2080 kernel_size = out_len;
2081 } else {
2082 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2083 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2084 kernel_size = hdr->kernel_size;
2085 }
2086
2087 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002088 * Update the kernel/ramdisk/tags address if the boot image header
2089 * has default values, these default values come from mkbootimg when
2090 * the boot image is flashed using fastboot flash:raw
2091 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002092 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002093
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002094 /* Get virtual addresses since the hdr saves physical addresses. */
2095 hdr->kernel_addr = VA(hdr->kernel_addr);
2096 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2097 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002098
Matthew Qinbb7923d2015-02-09 10:56:09 +08002099 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002100 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002101 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002102 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2103 {
2104 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002105 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002106 }
2107
Amol Jadicb524072012-08-09 16:40:18 -07002108#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002109 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002110 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002111 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002112
2113 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002114#else
2115 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2116 {
2117 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002118 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002119 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002120#endif
2121
2122 /* Load ramdisk & kernel */
2123 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002124 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002125
2126#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002127 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2128 {
2129 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2130 return;
2131 }
2132
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002133 /*
2134 * If dtb is not found look for appended DTB in the kernel.
2135 * If appended dev tree is found, update the atags with
2136 * memory address to the DTB appended location on RAM.
2137 * Else update with the atags address in the kernel header
2138 */
2139 if (!dtb_copied) {
2140 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002141 dtb = dev_tree_appended((void*)(ptr + page_size),
2142 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002143 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002144 if (!dtb) {
2145 fastboot_fail("dtb not found");
2146 return;
2147 }
Amol Jadicb524072012-08-09 16:40:18 -07002148 }
2149#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002150
2151 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002152 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002153
Dima Zavin77e41f32013-03-06 16:10:43 -08002154 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002155 (const char*) hdr->cmdline, board_machtype(),
2156 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002157}
2158
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002159void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002160{
2161 struct ptentry *ptn;
2162 struct ptable *ptable;
2163
2164 ptable = flash_get_ptable();
2165 if (ptable == NULL) {
2166 fastboot_fail("partition table doesn't exist");
2167 return;
2168 }
2169
2170 ptn = ptable_find(ptable, arg);
2171 if (ptn == NULL) {
2172 fastboot_fail("unknown partition name");
2173 return;
2174 }
2175
2176 if (flash_erase(ptn)) {
2177 fastboot_fail("failed to erase partition");
2178 return;
2179 }
2180 fastboot_okay("");
2181}
2182
Bikas Gurungd48bd242010-09-04 19:54:32 -07002183
2184void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2185{
2186 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002187 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002188 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002189 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002190
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002191#if VERIFIED_BOOT
2192 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2193 {
2194 if(!device.is_unlocked)
2195 {
2196 fastboot_fail("unlock device to erase keystore");
2197 return;
2198 }
2199 }
2200#endif
2201
Kinson Chikf1a43512011-07-14 11:28:39 -07002202 index = partition_get_index(arg);
2203 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002204 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002205
Kinson Chikf1a43512011-07-14 11:28:39 -07002206 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002207 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002208 return;
2209 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002210
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002211 lun = partition_get_lun(index);
2212 mmc_set_lun(lun);
2213
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002214 if (platform_boot_dev_isemmc())
2215 {
2216 if (mmc_erase_card(ptn, size)) {
2217 fastboot_fail("failed to erase partition\n");
2218 return;
2219 }
2220 } else {
2221 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2222 size = partition_get_size(index);
2223 if (size > DEFAULT_ERASE_SIZE)
2224 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002225
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002226 /* Simple inefficient version of erase. Just writing
2227 0 in first several blocks */
2228 if (mmc_write(ptn , size, (unsigned int *)out)) {
2229 fastboot_fail("failed to erase partition");
2230 return;
2231 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002232 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002233#if VERIFIED_BOOT
2234 if(!(strncmp(arg, "userdata", 8)))
2235 if(send_delete_keys_to_tz())
2236 ASSERT(0);
2237#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002238 fastboot_okay("");
2239}
2240
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002241void cmd_erase(const char *arg, void *data, unsigned sz)
2242{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002243#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002244 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002245 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002246 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002247 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002248 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002249 return;
2250 }
2251 }
2252#endif
2253
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002254 if(target_is_emmc_boot())
2255 cmd_erase_mmc(arg, data, sz);
2256 else
2257 cmd_erase_nand(arg, data, sz);
2258}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002259
Ajay Dudani5c761132011-04-07 20:19:04 -07002260void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002261{
2262 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002263 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002264 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002265 char *token = NULL;
2266 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002267 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002268 uint8_t lun = 0;
2269 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002270
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002271 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002272 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002273 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002274 if(token)
2275 {
2276 lun = atoi(token);
2277 mmc_set_lun(lun);
2278 lun_set = true;
2279 }
2280
Mao Jinlong226f33a2014-07-04 17:24:10 +08002281 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002282 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002283 if (!strcmp(pname, "partition"))
2284 {
2285 dprintf(INFO, "Attempt to write partition image.\n");
2286 if (write_partition(sz, (unsigned char *) data)) {
2287 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002288 return;
2289 }
2290 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002291 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002292 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002293#if VERIFIED_BOOT
2294 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2295 {
2296 if(!device.is_unlocked)
2297 {
2298 fastboot_fail("unlock device to flash keystore");
2299 return;
2300 }
2301 if(!boot_verify_validate_keystore((unsigned char *)data))
2302 {
2303 fastboot_fail("image is not a keystore file");
2304 return;
2305 }
2306 }
2307#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002308 index = partition_get_index(pname);
2309 ptn = partition_get_offset(index);
2310 if(ptn == 0) {
2311 fastboot_fail("partition table doesn't exist");
2312 return;
2313 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002314
Mao Jinlong226f33a2014-07-04 17:24:10 +08002315 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2316 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2317 fastboot_fail("image is not a boot image");
2318 return;
2319 }
2320 }
2321
2322 if(!lun_set)
2323 {
2324 lun = partition_get_lun(index);
2325 mmc_set_lun(lun);
2326 }
2327
2328 size = partition_get_size(index);
2329 if (ROUND_TO_PAGE(sz,511) > size) {
2330 fastboot_fail("size too large");
2331 return;
2332 }
2333 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2334 fastboot_fail("flash write failure");
2335 return;
2336 }
Greg Grisco6e754772011-06-23 12:19:39 -07002337 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002338 }
2339 fastboot_okay("");
2340 return;
2341}
2342
Ajay Dudanide984792015-03-02 09:57:41 -08002343void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2344{
2345 int i, images;
2346 meta_header_t *meta_header;
2347 img_header_entry_t *img_header_entry;
2348
2349 meta_header = (meta_header_t*) data;
2350 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2351
2352 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2353
2354 for (i=0; i<images; i++) {
2355
2356 if((img_header_entry[i].ptn_name == NULL) ||
2357 (img_header_entry[i].start_offset == 0) ||
2358 (img_header_entry[i].size == 0))
2359 break;
2360
2361 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2362 (void *) data + img_header_entry[i].start_offset,
2363 img_header_entry[i].size);
2364 }
2365
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002366 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2367 {
2368 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2369 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2370 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2371 }
2372 else
2373 {
2374 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2375 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2376 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2377 }
2378
2379 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002380 fastboot_okay("");
2381 return;
2382}
2383
Ajay Dudani5c761132011-04-07 20:19:04 -07002384void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2385{
2386 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002387 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002388 uint32_t *fill_buf = NULL;
2389 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002390 sparse_header_t *sparse_header;
2391 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002392 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002393 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302394 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002395 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302396 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002397 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302398 /*End of the sparse image address*/
2399 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002400
Kinson Chikf1a43512011-07-14 11:28:39 -07002401 index = partition_get_index(arg);
2402 ptn = partition_get_offset(index);
2403 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002404 fastboot_fail("partition table doesn't exist");
2405 return;
2406 }
2407
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302408 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302409
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002410 lun = partition_get_lun(index);
2411 mmc_set_lun(lun);
2412
vijay kumar800255e2015-04-24 20:26:19 +05302413 if (sz < sizeof(sparse_header_t)) {
2414 fastboot_fail("size too low");
2415 return;
2416 }
2417
Ajay Dudani5c761132011-04-07 20:19:04 -07002418 /* Read and skip over sparse image header */
2419 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302420
vijay kumar1321f342015-03-27 12:13:42 +05302421 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002422 fastboot_fail("size too large");
2423 return;
2424 }
2425
vijay kumarde4fcf62015-04-23 13:05:49 +05302426 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302427
2428 if (data_end < (uint32_t)data) {
2429 fastboot_fail("buffer overreads occured due to invalid sparse header");
2430 return;
2431 }
2432
vijay kumarde4fcf62015-04-23 13:05:49 +05302433 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002434 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302435 fastboot_fail("sparse header size mismatch");
2436 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002437 }
2438
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002439 dprintf (SPEW, "=== Sparse Image Header ===\n");
2440 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2441 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2442 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2443 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2444 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2445 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2446 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2447 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002448
2449 /* Start processing chunks */
2450 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2451 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302452 /* Make sure the total image size does not exceed the partition size */
2453 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2454 fastboot_fail("size too large");
2455 return;
2456 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002457 /* Read and skip over chunk header */
2458 chunk_header = (chunk_header_t *) data;
2459 data += sizeof(chunk_header_t);
2460
vijay kumar800255e2015-04-24 20:26:19 +05302461 if (data_end < (uint32_t)data) {
2462 fastboot_fail("buffer overreads occured due to invalid sparse header");
2463 return;
2464 }
2465
Ajay Dudani5c761132011-04-07 20:19:04 -07002466 dprintf (SPEW, "=== Chunk Header ===\n");
2467 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2468 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2469 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2470
vijay kumar800255e2015-04-24 20:26:19 +05302471 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002472 {
vijay kumar800255e2015-04-24 20:26:19 +05302473 fastboot_fail("chunk header size mismatch");
2474 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002475 }
2476
wufeng.jiang813dc352015-06-02 23:02:46 -04002477 if (!sparse_header->blk_sz ){
2478 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302479 return;
2480 }
2481
wufeng.jiang813dc352015-06-02 23:02:46 -04002482 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2483
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302484 /* Make sure that the chunk size calculated from sparse image does not
2485 * exceed partition size
2486 */
2487 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2488 {
2489 fastboot_fail("Chunk data size exceeds partition size");
2490 return;
2491 }
2492
Ajay Dudani5c761132011-04-07 20:19:04 -07002493 switch (chunk_header->chunk_type)
2494 {
2495 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002496 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002497 chunk_data_sz))
2498 {
2499 fastboot_fail("Bogus chunk size for chunk type Raw");
2500 return;
2501 }
2502
vijay kumar800255e2015-04-24 20:26:19 +05302503 if (data_end < (uint32_t)data + chunk_data_sz) {
2504 fastboot_fail("buffer overreads occured due to invalid sparse header");
2505 return;
2506 }
2507
wufeng.jiang813dc352015-06-02 23:02:46 -04002508 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2509 otherwise it will return in the line above
2510 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002511 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002512 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002513 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002514 {
2515 fastboot_fail("flash write failure");
2516 return;
2517 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302518 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2519 fastboot_fail("Bogus size for RAW chunk type");
2520 return;
2521 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002522 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002523 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002524 break;
2525
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002526 case CHUNK_TYPE_FILL:
2527 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2528 sizeof(uint32_t)))
2529 {
2530 fastboot_fail("Bogus chunk size for chunk type FILL");
2531 return;
2532 }
2533
2534 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2535 if (!fill_buf)
2536 {
2537 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2538 return;
2539 }
2540
vijay kumar800255e2015-04-24 20:26:19 +05302541 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2542 fastboot_fail("buffer overreads occured due to invalid sparse header");
2543 return;
2544 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002545 fill_val = *(uint32_t *)data;
2546 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002547
2548 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2549 {
2550 fill_buf[i] = fill_val;
2551 }
2552
wufeng.jiang813dc352015-06-02 23:02:46 -04002553 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002554 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302555 /* Make sure that the data written to partition does not exceed partition size */
2556 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2557 {
2558 fastboot_fail("Chunk data size for fill type exceeds partition size");
2559 return;
2560 }
2561
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002562 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2563 sparse_header->blk_sz,
2564 fill_buf))
2565 {
2566 fastboot_fail("flash write failure");
2567 free(fill_buf);
2568 return;
2569 }
2570
2571 total_blocks++;
2572 }
2573
2574 free(fill_buf);
2575 break;
2576
Ajay Dudani5c761132011-04-07 20:19:04 -07002577 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302578 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2579 fastboot_fail("bogus size for chunk DONT CARE type");
2580 return;
2581 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002582 total_blocks += chunk_header->chunk_sz;
2583 break;
2584
Ajay Dudani5c761132011-04-07 20:19:04 -07002585 case CHUNK_TYPE_CRC:
2586 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2587 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002588 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002589 return;
2590 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302591 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2592 fastboot_fail("bogus size for chunk CRC type");
2593 return;
2594 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002595 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302596 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2597 fastboot_fail("integer overflow occured");
2598 return;
2599 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002600 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302601 if (data_end < (uint32_t)data) {
2602 fastboot_fail("buffer overreads occured due to invalid sparse header");
2603 return;
2604 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002605 break;
2606
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002607 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002608 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002609 fastboot_fail("Unknown chunk type");
2610 return;
2611 }
2612 }
2613
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002614 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2615 total_blocks, sparse_header->total_blks);
2616
2617 if(total_blocks != sparse_header->total_blks)
2618 {
2619 fastboot_fail("sparse image write failure");
2620 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002621
2622 fastboot_okay("");
2623 return;
2624}
2625
2626void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2627{
2628 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002629 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002630
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002631#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002632 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2633 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002634 int ret=0;
2635 uint32 major_version=0;
2636 uint32 minor_version=0;
2637
2638 ret = scm_svc_version(&major_version,&minor_version);
2639 if(!ret)
2640 {
2641 if(major_version >= 2)
2642 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002643 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002644 {
2645 ret = encrypt_scm((uint32 **) &data, &sz);
2646 if (ret != 0) {
2647 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2648 return;
2649 }
2650
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002651 /* Protect only for SSD */
2652 if (!strcmp(arg, "ssd")) {
2653 ret = scm_protect_keystore((uint32 *) data, sz);
2654 if (ret != 0) {
2655 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2656 return;
2657 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002658 }
2659 }
2660 else
2661 {
2662 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2663 if(ret != 0)
2664 {
2665 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2666 return;
2667 }
2668 }
2669 }
2670 else
2671 {
2672 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2673 magic_number[1] == DECRYPT_MAGIC_1)
2674 {
2675 ret = decrypt_scm((uint32 **) &data, &sz);
2676 if (ret != 0) {
2677 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2678 return;
2679 }
2680 }
2681 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2682 magic_number[1] == ENCRYPT_MAGIC_1)
2683 {
2684 ret = encrypt_scm((uint32 **) &data, &sz);
2685 if (ret != 0) {
2686 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2687 return;
2688 }
2689 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002690 }
2691 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002692 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002693 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002694 dprintf(CRITICAL,"INVALID SVC Version\n");
2695 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002696 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002697#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002698
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002699#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002700 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002701 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002702 if(!device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002703 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002704 fastboot_fail("device is locked. Cannot flash images");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002705 return;
2706 }
2707 }
2708#endif
2709
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002710 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002711 meta_header = (meta_header_t *) data;
2712 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002713 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002714 else if (meta_header->magic == META_HEADER_MAGIC)
2715 cmd_flash_meta_img(arg, data, sz);
2716 else
2717 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002718
2719#if VERIFIED_BOOT
2720 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
2721 {
2722 // reset dm_verity mode to enforcing
2723 device.verity_mode = 1;
2724 write_device_info(&device);
2725 }
2726#endif
2727
Ajay Dudani5c761132011-04-07 20:19:04 -07002728 return;
2729}
2730
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002731void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2732{
2733 struct ptentry *sys_ptn;
2734 struct ptable *ptable;
2735
2736 ptable = flash_get_ptable();
2737 if (ptable == NULL) {
2738 fastboot_fail("partition table doesn't exist");
2739 return;
2740 }
2741
2742 sys_ptn = ptable_find(ptable, "system");
2743 if (sys_ptn == NULL) {
2744 fastboot_fail("system partition not found");
2745 return;
2746 }
2747
2748 sz = ROUND_TO_PAGE(sz, page_mask);
2749 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2750 fastboot_fail("update_ubi_vol failed");
2751 else
2752 fastboot_okay("");
2753}
2754
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002755void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002756{
2757 struct ptentry *ptn;
2758 struct ptable *ptable;
2759 unsigned extra = 0;
2760
2761 ptable = flash_get_ptable();
2762 if (ptable == NULL) {
2763 fastboot_fail("partition table doesn't exist");
2764 return;
2765 }
2766
2767 ptn = ptable_find(ptable, arg);
2768 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002769 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2770 arg);
2771 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002772 return;
2773 }
2774
2775 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2776 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2777 fastboot_fail("image is not a boot image");
2778 return;
2779 }
2780 }
2781
Amol Jadi5c61a952012-05-04 17:05:35 -07002782 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002783 || !strcmp(ptn->name, "userdata")
2784 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002785 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002786 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002787 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002788 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002789 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002790
2791 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002792 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2793 if (flash_ubi_img(ptn, data, sz)) {
2794 fastboot_fail("flash write failure");
2795 return;
2796 }
2797 } else {
2798 if (flash_write(ptn, extra, data, sz)) {
2799 fastboot_fail("flash write failure");
2800 return;
2801 }
Dima Zavin214cc642009-01-26 11:16:21 -08002802 }
2803 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2804 fastboot_okay("");
2805}
2806
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002807void cmd_flash(const char *arg, void *data, unsigned sz)
2808{
2809 if(target_is_emmc_boot())
2810 cmd_flash_mmc(arg, data, sz);
2811 else
2812 cmd_flash_nand(arg, data, sz);
2813}
2814
Dima Zavin214cc642009-01-26 11:16:21 -08002815void cmd_continue(const char *arg, void *data, unsigned sz)
2816{
2817 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002818 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002819
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002820 if (target_is_emmc_boot())
2821 {
lijuanga40d6302015-07-20 20:10:13 +08002822#if FBCON_DISPLAY_MSG
2823 keys_detect_init();
2824#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002825 boot_linux_from_mmc();
2826 }
2827 else
2828 {
2829 boot_linux_from_flash();
2830 }
Dima Zavin214cc642009-01-26 11:16:21 -08002831}
2832
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002833void cmd_reboot(const char *arg, void *data, unsigned sz)
2834{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002835 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002836 fastboot_okay("");
2837 reboot_device(0);
2838}
2839
2840void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2841{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002842 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002843 fastboot_okay("");
2844 reboot_device(FASTBOOT_MODE);
2845}
2846
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002847void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2848{
2849 dprintf(INFO, "Enabling charger screen check\n");
2850 device.charger_screen_enabled = 1;
2851 write_device_info(&device);
2852 fastboot_okay("");
2853}
2854
2855void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2856{
2857 dprintf(INFO, "Disabling charger screen check\n");
2858 device.charger_screen_enabled = 0;
2859 write_device_info(&device);
2860 fastboot_okay("");
2861}
2862
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302863void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2864{
2865 dprintf(INFO, "Selecting display panel %s\n", arg);
2866 if (arg)
2867 strlcpy(device.display_panel, arg,
2868 sizeof(device.display_panel));
2869 write_device_info(&device);
2870 fastboot_okay("");
2871}
2872
Shashank Mittal162244e2011-08-08 19:01:25 -07002873void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2874{
vijay kumarc65876c2015-04-24 13:29:16 +05302875 if(!is_allow_unlock) {
2876 fastboot_fail("oem unlock is not allowed");
2877 return;
2878 }
2879
lijuanga40d6302015-07-20 20:10:13 +08002880#if FBCON_DISPLAY_MSG
2881 if(!device.is_unlocked)
2882 display_menu_thread(DISPLAY_THREAD_UNLOCK);
2883 else
2884 fastboot_info("Device already unlocked!");
2885 fastboot_okay("");
2886#else
vijay kumarc65876c2015-04-24 13:29:16 +05302887 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
lijuanga40d6302015-07-20 20:10:13 +08002888#endif
vijay kumarc65876c2015-04-24 13:29:16 +05302889}
2890
2891void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
2892{
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002893 if(!device.is_unlocked)
Shashank Mittal162244e2011-08-08 19:01:25 -07002894 {
vijay kumarc65876c2015-04-24 13:29:16 +05302895 if(!is_allow_unlock) {
2896 fastboot_fail("oem unlock is not allowed");
2897 return;
2898 }
2899
Shashank Mittal162244e2011-08-08 19:01:25 -07002900 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002901 write_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05302902
2903 struct recovery_message msg;
2904 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2905 write_misc(0, &msg, sizeof(msg));
2906
2907 fastboot_okay("");
2908 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002909 }
2910 fastboot_okay("");
2911}
2912
2913void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2914{
Sridhar Parasuram8b792422015-07-05 11:38:13 -07002915 struct recovery_message msg;
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002916 if(device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002917 {
2918 device.is_unlocked = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002919 write_device_info(&device);
Sridhar Parasuram8b792422015-07-05 11:38:13 -07002920 // upon oem lock, reboot to recovery to wipe user data
2921 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2922 write_misc(0, &msg, sizeof(msg));
2923 fastboot_okay("");
2924 reboot_device(RECOVERY_MODE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002925 }
2926 fastboot_okay("");
2927}
2928
Shashank Mittala0032282011-08-26 14:50:11 -07002929void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2930{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302931 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002932 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002933 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002934 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2935 fastboot_info(response);
2936 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002937 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302938 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2939 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002940 fastboot_okay("");
2941}
2942
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002943void cmd_preflash(const char *arg, void *data, unsigned sz)
2944{
2945 fastboot_okay("");
2946}
2947
Mao Flynn7b379f32015-04-20 00:28:30 +08002948static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302949
Mao Flynn7b379f32015-04-20 00:28:30 +08002950int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302951{
Mao Flynn7b379f32015-04-20 00:28:30 +08002952 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302953 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08002954 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302955 return -1;
2956 return 0;
2957}
2958
Mao Flynn7b379f32015-04-20 00:28:30 +08002959int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002960{
2961 struct ptentry *ptn;
2962 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08002963 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002964 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002965
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302966 ptable = flash_get_ptable();
2967 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002968 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2969 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302970 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002971
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302972 ptn = ptable_find(ptable, "splash");
2973 if (ptn == NULL) {
2974 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002975 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302976 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002977 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302978 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002979 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302980 }
2981
Mao Flynn7b379f32015-04-20 00:28:30 +08002982 header = (struct logo_img_header *)logo_header;
2983 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302984 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002985 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302986 }
2987
2988 fb_display = fbcon_display();
2989 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002990 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
2991 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
2992
2993 /* if the logo is full-screen size, remove "fbcon_clear()" */
2994 if ((header->width != fb_display->width)
2995 || (header->height != fb_display->height))
2996 fbcon_clear();
2997
2998 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
2999 (uint32_t *)base,
3000 (header->blocks * 512))) {
3001 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3002 return -1;
3003 }
3004 fbcon_extract_to_screen(header, base);
3005 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003006 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003007
3008 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3009 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3010 return -1;
3011 }
3012
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303013 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003014 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3015 (uint32_t *)base,
3016 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303017 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303018 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003019 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003020 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303021 }
3022
Mao Flynn7b379f32015-04-20 00:28:30 +08003023 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303024}
3025
Mao Flynn7b379f32015-04-20 00:28:30 +08003026int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303027{
3028 int index = INVALID_PTN;
3029 unsigned long long ptn = 0;
3030 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003031 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003032 uint32_t blocksize, realsize, readsize;
3033 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303034
3035 index = partition_get_index("splash");
3036 if (index == 0) {
3037 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003038 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303039 }
3040
3041 ptn = partition_get_offset(index);
3042 if (ptn == 0) {
3043 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003044 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303045 }
3046
Mao Flynn1893a7f2015-06-03 12:03:36 +08003047 mmc_set_lun(partition_get_lun(index));
3048
3049 blocksize = mmc_get_device_blocksize();
3050 if (blocksize == 0) {
3051 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3052 return -1;
3053 }
3054
3055 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003056 if (!fb_display)
3057 {
3058 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3059 return -1;
3060 }
3061
Mao Flynn1893a7f2015-06-03 12:03:36 +08003062 base = (uint8_t *) fb_display->base;
3063
3064 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303065 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003066 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303067 }
3068
Mao Flynn1893a7f2015-06-03 12:03:36 +08003069 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003070 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303071 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003072 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303073 }
3074
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303075 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003076 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3077 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003078
Mao Flynn1893a7f2015-06-03 12:03:36 +08003079 realsize = header->blocks * 512;
3080 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3081
3082 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003083 if ((header->width != fb_display->width)
3084 || (header->height != fb_display->height))
3085 fbcon_clear();
3086
Mao Flynn1893a7f2015-06-03 12:03:36 +08003087 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003088 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3089 return -1;
3090 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003091
Mao Flynn1893a7f2015-06-03 12:03:36 +08003092 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3093 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303094
Mao Flynn1893a7f2015-06-03 12:03:36 +08003095 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3096 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3097 return -1;
3098 }
3099
3100 realsize = header->width * header->height * fb_display->bpp / 8;
3101 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3102
3103 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3104 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3105 fbcon_clear();
3106 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3107 return -1;
3108 }
3109 } else {
3110 if (mmc_read(ptn + blocksize ,
3111 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3112 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3113 return -1;
3114 }
3115 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3116 }
3117 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303118 }
3119
Mao Flynn7b379f32015-04-20 00:28:30 +08003120 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303121}
3122
Mao Flynn7b379f32015-04-20 00:28:30 +08003123int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303124{
3125 if (target_is_emmc_boot()) {
3126 return splash_screen_mmc();
3127 } else {
3128 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003129 }
3130}
3131
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003132/* Get the size from partiton name */
3133static void get_partition_size(const char *arg, char *response)
3134{
3135 uint64_t ptn = 0;
3136 uint64_t size;
3137 int index = INVALID_PTN;
3138
3139 index = partition_get_index(arg);
3140
3141 if (index == INVALID_PTN)
3142 {
3143 dprintf(CRITICAL, "Invalid partition index\n");
3144 return;
3145 }
3146
3147 ptn = partition_get_offset(index);
3148
3149 if(!ptn)
3150 {
3151 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3152 return;
3153 }
3154
3155 size = partition_get_size(index);
3156
3157 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3158 return;
3159}
3160
3161/*
3162 * Publish the partition type & size info
3163 * fastboot getvar will publish the required information.
3164 * fastboot getvar partition_size:<partition_name>: partition size in hex
3165 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3166 */
3167static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3168{
3169 uint8_t i;
3170
3171 for (i = 0; i < num_parts; i++) {
3172 get_partition_size(info[i].part_name, info[i].size_response);
3173
3174 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3175 {
3176 dprintf(CRITICAL, "partition size name truncated\n");
3177 return;
3178 }
3179 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3180 {
3181 dprintf(CRITICAL, "partition type name truncated\n");
3182 return;
3183 }
3184
3185 /* publish partition size & type info */
3186 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3187 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3188 }
3189}
3190
Amol Jadi5edf3552013-07-23 14:15:34 -07003191/* register commands and variables for fastboot */
3192void aboot_fastboot_register_commands(void)
3193{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003194 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07003195
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003196 struct fastboot_cmd_desc cmd_list[] = {
3197 /* By default the enabled list is empty. */
3198 {"", NULL},
3199 /* move commands enclosed within the below ifndef to here
3200 * if they need to be enabled in user build.
3201 */
3202#ifndef DISABLE_FASTBOOT_CMDS
3203 /* Register the following commands only for non-user builds */
3204 {"flash:", cmd_flash},
3205 {"erase:", cmd_erase},
3206 {"boot", cmd_boot},
3207 {"continue", cmd_continue},
3208 {"reboot", cmd_reboot},
3209 {"reboot-bootloader", cmd_reboot_bootloader},
3210 {"oem unlock", cmd_oem_unlock},
vijay kumarc65876c2015-04-24 13:29:16 +05303211 {"oem unlock-go", cmd_oem_unlock_go},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003212 {"oem lock", cmd_oem_lock},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003213 {"oem device-info", cmd_oem_devinfo},
3214 {"preflash", cmd_preflash},
3215 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3216 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08003217 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003218#endif
3219 };
3220
3221 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3222 for (i = 1; i < fastboot_cmds_count; i++)
3223 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3224
Amol Jadi5edf3552013-07-23 14:15:34 -07003225 /* publish variables and their values */
3226 fastboot_publish("product", TARGET(BOARD));
3227 fastboot_publish("kernel", "lk");
3228 fastboot_publish("serialno", sn_buf);
3229
3230 /*
3231 * partition info is supported only for emmc partitions
3232 * Calling this for NAND prints some error messages which
3233 * is harmless but misleading. Avoid calling this for NAND
3234 * devices.
3235 */
3236 if (target_is_emmc_boot())
3237 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3238
3239 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003240 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3241 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003242 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003243 /* Is the charger screen check enabled */
3244 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3245 device.charger_screen_enabled);
3246 fastboot_publish("charger-screen-enabled",
3247 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303248 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3249 device.display_panel);
3250 fastboot_publish("display-panel",
3251 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003252 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3253 fastboot_publish("version-baseband", (const char *) device.radio_version);
Amol Jadi5edf3552013-07-23 14:15:34 -07003254}
3255
Brian Swetland9c4c0752009-01-25 16:23:50 -08003256void aboot_init(const struct app_descriptor *app)
3257{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003258 unsigned reboot_mode = 0;
lijuang72875802015-02-10 10:35:12 +08003259 unsigned hard_reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03003260 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003261
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003262 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003263 if (target_is_emmc_boot())
3264 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003265 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003266 page_mask = page_size - 1;
3267 }
3268 else
3269 {
3270 page_size = flash_page_size();
3271 page_mask = page_size - 1;
3272 }
3273
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003274 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3275
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003276 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303277 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003278
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003279 /* Display splash screen if enabled */
3280#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003281#if NO_ALARM_DISPLAY
3282 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003283#endif
lijuang99c02d82015-02-13 19:04:34 +08003284 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003285#if ENABLE_WBC
3286 if (!pm_appsbl_display_init_done())
3287 target_display_init(device.display_panel);
3288 else
3289 display_image_on_screen();
3290#else
lijuang99c02d82015-02-13 19:04:34 +08003291 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003292#endif
lijuang99c02d82015-02-13 19:04:34 +08003293 dprintf(SPEW, "Display Init: Done\n");
3294#if NO_ALARM_DISPLAY
3295 }
3296#endif
3297#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003298
Greg Griscod6250552011-06-29 14:40:23 -07003299 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003300 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003301
Dhaval Patel223ec952013-07-18 14:49:44 -07003302 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3303
Matthew Qindefd5562014-07-11 18:02:40 +08003304 /*
3305 * Check power off reason if user force reset,
3306 * if yes phone will do normal boot.
3307 */
3308 if (is_user_force_reset())
3309 goto normal_boot;
3310
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003311 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003312 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003313 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003314 dprintf(ALWAYS,"dload mode key sequence detected\n");
3315 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003316 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003317 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003318 }
3319 else
3320 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303321 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003322 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003323 }
3324 boot_into_fastboot = true;
3325 }
3326 if (!boot_into_fastboot)
3327 {
3328 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3329 boot_into_recovery = 1;
3330 if (!boot_into_recovery &&
3331 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003332 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003333 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003334 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003335 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003336 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003337 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003338
Ajay Dudani77421292010-10-27 19:34:06 -07003339 reboot_mode = check_reboot_mode();
lijuang72875802015-02-10 10:35:12 +08003340 hard_reboot_mode = check_hard_reboot_mode();
3341 if (reboot_mode == RECOVERY_MODE ||
3342 hard_reboot_mode == RECOVERY_HARD_RESET_MODE) {
Ajay Dudani77421292010-10-27 19:34:06 -07003343 boot_into_recovery = 1;
lijuang72875802015-02-10 10:35:12 +08003344 } else if(reboot_mode == FASTBOOT_MODE ||
3345 hard_reboot_mode == FASTBOOT_HARD_RESET_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003346 boot_into_fastboot = true;
lijuang72875802015-02-10 10:35:12 +08003347 } else if(reboot_mode == ALARM_BOOT ||
3348 hard_reboot_mode == RTC_HARD_RESET_MODE) {
Matthew Qind886f3c2014-01-17 16:52:01 +08003349 boot_reason_alarm = true;
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003350 } else if(reboot_mode == DM_VERITY_ENFORCING ||
3351 hard_reboot_mode == DM_VERITY_ENFORCING_HARD_RESET_MODE) {
3352 device.verity_mode = 1;
3353 write_device_info(&device);
3354 } else if(reboot_mode == DM_VERITY_LOGGING ||
3355 hard_reboot_mode == DM_VERITY_LOGGING_HARD_RESET_MODE) {
3356 device.verity_mode = 0;
3357 write_device_info(&device);
3358 } else if(reboot_mode == DM_VERITY_KEYSCLEAR ||
3359 hard_reboot_mode == DM_VERITY_KEYSCLEAR_HARD_RESET_MODE) {
3360 if(send_delete_keys_to_tz())
3361 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003362 }
3363
Matthew Qindefd5562014-07-11 18:02:40 +08003364normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003365 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003366 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003367 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003368 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003369 if(emmc_recovery_init())
3370 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3371 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003372 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003373 if((device.is_unlocked) || (device.is_tampered))
3374 {
3375 #ifdef TZ_TAMPER_FUSE
3376 set_tamper_fuse_cmd();
3377 #endif
3378 #if USE_PCOM_SECBOOT
3379 set_tamper_flag(device.is_tampered);
3380 #endif
3381 }
Shashank Mittala0032282011-08-26 14:50:11 -07003382 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003383
Pavel Nedev5d91d412013-04-29 11:34:24 +03003384 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003385 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003386 else
3387 {
3388 recovery_init();
3389 #if USE_PCOM_SECBOOT
3390 if((device.is_unlocked) || (device.is_tampered))
3391 set_tamper_flag(device.is_tampered);
3392 #endif
3393 boot_linux_from_flash();
3394 }
3395 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3396 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003397 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003398
Amol Jadi5edf3552013-07-23 14:15:34 -07003399 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003400
Amol Jadi5edf3552013-07-23 14:15:34 -07003401 /* register aboot specific fastboot commands */
3402 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003403
Amol Jadi5edf3552013-07-23 14:15:34 -07003404 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003405 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003406
3407 /* initialize and start fastboot */
3408 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08003409}
3410
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003411uint32_t get_page_size()
3412{
3413 return page_size;
3414}
3415
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003416/*
3417 * Calculated and save hash (SHA256) for non-signed boot image.
3418 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003419 * @param image_addr - Boot image address
3420 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003421 *
3422 * @return int - 0 on success, negative value on failure.
3423 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003424static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003425{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003426 unsigned int digest[8];
3427#if IMAGE_VERIF_ALGO_SHA1
3428 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3429#else
3430 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3431#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003432
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003433 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003434 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003435
3436 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003437 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003438
3439 return 0;
3440}
3441
Brian Swetland9c4c0752009-01-25 16:23:50 -08003442APP_START(aboot)
3443 .init = aboot_init,
3444APP_END