blob: 9ac8d36a219becb0a067dcafdc553062efe906cc [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
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070079#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070080#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080081#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080082#include "bootimg.h"
83#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070084#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080085#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070086#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070087#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070088#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070089#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020090#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070091#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080092#include <menu_keys_detect.h>
93#include <display_menu.h>
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070094
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070095extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070096extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070097extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070098extern int get_target_boot_params(const char *cmdline, const char *part,
99 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700100
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700101void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700102void write_device_info_mmc(device_info *dev);
103void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700104static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700105
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700106/* fastboot command function pointer */
107typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
108
109struct fastboot_cmd_desc {
110 char * name;
111 fastboot_cmd_fn cb;
112};
113
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700114#define EXPAND(NAME) #NAME
115#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700116
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800117#ifdef MEMBASE
118#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
119#else
David Ng183a7422009-12-07 14:55:21 -0800120#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800121#endif
122
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700123#ifndef MEMSIZE
124#define MEMSIZE 1024*1024
125#endif
126
127#define MAX_TAGS_SIZE 1024
128
Kun Liang2f1601a2013-08-12 16:29:54 +0800129/* make 4096 as default size to ensure EFS,EXT4's erasing */
130#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700131#define MAX_PANEL_BUF_SIZE 196
Kun Liang2f1601a2013-08-12 16:29:54 +0800132
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700133#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700134#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800135
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800136#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
137
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700138#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
139
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800140#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700141static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700142#else
David Ng183a7422009-12-07 14:55:21 -0800143static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700144#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800145static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300146static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800147static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800148static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800149static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700150static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300151static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200152static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800153
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800154static const char *baseband_apq = " androidboot.baseband=apq";
155static const char *baseband_msm = " androidboot.baseband=msm";
156static const char *baseband_csfb = " androidboot.baseband=csfb";
157static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700158static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800159static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700160static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800161static const char *baseband_dsda = " androidboot.baseband=dsda";
162static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800163static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800164static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800165
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700166#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700167static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700168static const char *verified_state= " androidboot.verifiedbootstate=";
169
170//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700171
172struct verified_boot_verity_mode vbvm[] =
173{
174 {false, "logging"},
175 {true, "enforcing"},
176};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700177struct verified_boot_state_name vbsn[] =
178{
179 {GREEN, "green"},
180 {ORANGE, "orange"},
181 {YELLOW,"yellow"},
182 {RED,"red" },
183};
184#endif
185
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700186static unsigned page_size = 0;
187static unsigned page_mask = 0;
188static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
189static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700190static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800191static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700192static bool devinfo_present = true;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700193
Shashank Mittalcd98d472011-08-02 14:29:24 -0700194/* Assuming unauthorized kernel image by default */
195static int auth_kernel_img = 0;
196
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700197static device_info device = {DEVICE_MAGIC, 0, 0, 0, {0}, {0},{0}, 1};
vijay kumarc65876c2015-04-24 13:29:16 +0530198static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700199
vijay kumarca2e6812015-07-08 20:28:25 +0530200static char frp_ptns[2][8] = {"config","frp"};
201
Dima Zavin42168f22009-01-30 11:52:22 -0800202struct atag_ptbl_entry
203{
204 char name[16];
205 unsigned offset;
206 unsigned size;
207 unsigned flags;
208};
209
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700210/*
211 * Partition info, required to be published
212 * for fastboot
213 */
214struct getvar_partition_info {
215 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
216 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
217 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
218 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
219 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
220};
221
222/*
223 * Right now, we are publishing the info for only
224 * three partitions
225 */
226struct getvar_partition_info part_info[] =
227{
228 { "system" , "partition-size:", "partition-type:", "", "ext4" },
229 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
230 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
231};
232
233char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700234char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800235char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700236char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530237char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700238
Greg Griscod2471ef2011-07-14 13:00:42 -0700239extern int emmc_recovery_init(void);
240
Kinson Chik0b1c8162011-08-31 16:31:57 -0700241#if NO_KEYPAD_DRIVER
242extern int fastboot_trigger(void);
243#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700244
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800245static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700246{
247 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700248#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800249 if (is_arm64)
250 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
251 else
252 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700253 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
254 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700255#endif
256}
257
Dima Zavin42168f22009-01-30 11:52:22 -0800258static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
259{
260 struct atag_ptbl_entry atag_ptn;
261
262 memcpy(atag_ptn.name, ptn->name, 16);
263 atag_ptn.name[15] = '\0';
264 atag_ptn.offset = ptn->start;
265 atag_ptn.size = ptn->length;
266 atag_ptn.flags = ptn->flags;
267 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
268 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
269}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800270
Neeti Desaie245d492012-06-01 12:52:13 -0700271unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800272{
David Ng183a7422009-12-07 14:55:21 -0800273 int cmdline_len = 0;
274 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800275 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700276 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800277 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300278 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700279 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700280 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200281 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700282#if VERIFIED_BOOT
283 uint32_t boot_state = boot_verify_get_state();
284#endif
285
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200286#ifdef MDTP_SUPPORT
287 mdtp_activated(&is_mdtp_activated);
288#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800289
Brian Swetland9c4c0752009-01-25 16:23:50 -0800290 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800291 cmdline_len = strlen(cmdline);
292 have_cmdline = 1;
293 }
294 if (target_is_emmc_boot()) {
295 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800296#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700297 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
298 ASSERT(boot_dev_buf);
299 platform_boot_dev_cmdline(boot_dev_buf);
300 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700301#endif
David Ng183a7422009-12-07 14:55:21 -0800302 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800303
304 cmdline_len += strlen(usb_sn_cmdline);
305 cmdline_len += strlen(sn_buf);
306
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700307#if VERIFIED_BOOT
308 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700309 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700310#endif
311
Pavel Nedev5614d222013-06-17 18:01:02 +0300312 if (boot_into_recovery && gpt_exists)
313 cmdline_len += strlen(secondary_gpt_enable);
314
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200315 if(is_mdtp_activated)
316 cmdline_len += strlen(mdtp_activated_flag);
317
Pavel Nedev328ac822013-04-05 15:25:11 +0300318 if (boot_into_ffbm) {
319 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700320 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800321 /* reduce kernel console messages to speed-up boot */
322 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800323 } else if (boot_reason_alarm) {
324 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800325 } else if ((target_build_variant_user() || device.charger_screen_enabled)
326 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700327 pause_at_bootup = 1;
328 cmdline_len += strlen(battchg_pause);
329 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800330
Shashank Mittalcd98d472011-08-02 14:29:24 -0700331 if(target_use_signed_kernel() && auth_kernel_img) {
332 cmdline_len += strlen(auth_kernel);
333 }
334
Joonwoo Park61112782013-10-02 19:50:39 -0700335 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
336 "system",
337 target_boot_params,
338 sizeof(target_boot_params)) == 0) {
339 have_target_boot_params = 1;
340 cmdline_len += strlen(target_boot_params);
341 }
342
Ajay Dudanid04110c2011-01-17 23:55:07 -0800343 /* Determine correct androidboot.baseband to use */
344 switch(target_baseband())
345 {
346 case BASEBAND_APQ:
347 cmdline_len += strlen(baseband_apq);
348 break;
349
350 case BASEBAND_MSM:
351 cmdline_len += strlen(baseband_msm);
352 break;
353
354 case BASEBAND_CSFB:
355 cmdline_len += strlen(baseband_csfb);
356 break;
357
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800358 case BASEBAND_SVLTE2A:
359 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800360 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700361
362 case BASEBAND_MDM:
363 cmdline_len += strlen(baseband_mdm);
364 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700365
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800366 case BASEBAND_MDM2:
367 cmdline_len += strlen(baseband_mdm2);
368 break;
369
Amol Jadi5c61a952012-05-04 17:05:35 -0700370 case BASEBAND_SGLTE:
371 cmdline_len += strlen(baseband_sglte);
372 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530373
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800374 case BASEBAND_SGLTE2:
375 cmdline_len += strlen(baseband_sglte2);
376 break;
377
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530378 case BASEBAND_DSDA:
379 cmdline_len += strlen(baseband_dsda);
380 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800381
382 case BASEBAND_DSDA2:
383 cmdline_len += strlen(baseband_dsda2);
384 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800385 }
386
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800387 if (cmdline) {
388 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530389 target_display_panel_node(display_panel_buf,
390 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800391 strlen(display_panel_buf)) {
392 cmdline_len += strlen(display_panel_buf);
393 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700394 }
395
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800396 if (target_warm_boot()) {
397 warm_boot = true;
398 cmdline_len += strlen(warmboot_cmdline);
399 }
400
David Ng183a7422009-12-07 14:55:21 -0800401 if (cmdline_len > 0) {
402 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800403 unsigned char *dst;
404
405 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
406 ASSERT(cmdline_final != NULL);
407 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700408
Amol Jadi168b7712012-03-06 16:15:00 -0800409 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800410 if (have_cmdline) {
411 src = cmdline;
412 while ((*dst++ = *src++));
413 }
414 if (target_is_emmc_boot()) {
415 src = emmc_cmdline;
416 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700417 have_cmdline = 1;
418 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800419#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700420 src = boot_dev_buf;
421 if (have_cmdline) --dst;
422 while ((*dst++ = *src++));
423#endif
David Ngf773dde2010-07-26 19:55:08 -0700424 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800425
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700426#if VERIFIED_BOOT
427 src = verified_state;
428 if(have_cmdline) --dst;
429 have_cmdline = 1;
430 while ((*dst++ = *src++));
431 src = vbsn[boot_state].name;
432 if(have_cmdline) --dst;
433 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700434
435 src = verity_mode;
436 if(have_cmdline) --dst;
437 while ((*dst++ = *src++));
438 src = vbvm[device.verity_mode].name;
439 if(have_cmdline) -- dst;
440 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700441#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800442 src = usb_sn_cmdline;
443 if (have_cmdline) --dst;
444 have_cmdline = 1;
445 while ((*dst++ = *src++));
446 src = sn_buf;
447 if (have_cmdline) --dst;
448 have_cmdline = 1;
449 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800450 if (warm_boot) {
451 if (have_cmdline) --dst;
452 src = warmboot_cmdline;
453 while ((*dst++ = *src++));
454 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800455
Pavel Nedev5614d222013-06-17 18:01:02 +0300456 if (boot_into_recovery && gpt_exists) {
457 src = secondary_gpt_enable;
458 if (have_cmdline) --dst;
459 while ((*dst++ = *src++));
460 }
461
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200462 if (is_mdtp_activated) {
463 src = mdtp_activated_flag;
464 if (have_cmdline) --dst;
465 while ((*dst++ = *src++));
466 }
467
Pavel Nedev328ac822013-04-05 15:25:11 +0300468 if (boot_into_ffbm) {
469 src = androidboot_mode;
470 if (have_cmdline) --dst;
471 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700472 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300473 if (have_cmdline) --dst;
474 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800475 src = loglevel;
476 if (have_cmdline) --dst;
477 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800478 } else if (boot_reason_alarm) {
479 src = alarmboot_cmdline;
480 if (have_cmdline) --dst;
481 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300482 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700483 src = battchg_pause;
484 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800485 while ((*dst++ = *src++));
486 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800487
Shashank Mittalcd98d472011-08-02 14:29:24 -0700488 if(target_use_signed_kernel() && auth_kernel_img) {
489 src = auth_kernel;
490 if (have_cmdline) --dst;
491 while ((*dst++ = *src++));
492 }
493
Ajay Dudanid04110c2011-01-17 23:55:07 -0800494 switch(target_baseband())
495 {
496 case BASEBAND_APQ:
497 src = baseband_apq;
498 if (have_cmdline) --dst;
499 while ((*dst++ = *src++));
500 break;
501
502 case BASEBAND_MSM:
503 src = baseband_msm;
504 if (have_cmdline) --dst;
505 while ((*dst++ = *src++));
506 break;
507
508 case BASEBAND_CSFB:
509 src = baseband_csfb;
510 if (have_cmdline) --dst;
511 while ((*dst++ = *src++));
512 break;
513
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800514 case BASEBAND_SVLTE2A:
515 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800516 if (have_cmdline) --dst;
517 while ((*dst++ = *src++));
518 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700519
520 case BASEBAND_MDM:
521 src = baseband_mdm;
522 if (have_cmdline) --dst;
523 while ((*dst++ = *src++));
524 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700525
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800526 case BASEBAND_MDM2:
527 src = baseband_mdm2;
528 if (have_cmdline) --dst;
529 while ((*dst++ = *src++));
530 break;
531
Amol Jadi5c61a952012-05-04 17:05:35 -0700532 case BASEBAND_SGLTE:
533 src = baseband_sglte;
534 if (have_cmdline) --dst;
535 while ((*dst++ = *src++));
536 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530537
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800538 case BASEBAND_SGLTE2:
539 src = baseband_sglte2;
540 if (have_cmdline) --dst;
541 while ((*dst++ = *src++));
542 break;
543
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530544 case BASEBAND_DSDA:
545 src = baseband_dsda;
546 if (have_cmdline) --dst;
547 while ((*dst++ = *src++));
548 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800549
550 case BASEBAND_DSDA2:
551 src = baseband_dsda2;
552 if (have_cmdline) --dst;
553 while ((*dst++ = *src++));
554 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800555 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700556
557 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700558 src = display_panel_buf;
559 if (have_cmdline) --dst;
560 while ((*dst++ = *src++));
561 }
Joonwoo Park61112782013-10-02 19:50:39 -0700562
563 if (have_target_boot_params) {
564 if (have_cmdline) --dst;
565 src = target_boot_params;
566 while ((*dst++ = *src++));
567 }
Neeti Desaie245d492012-06-01 12:52:13 -0700568 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700569
570
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700571 if (boot_dev_buf)
572 free(boot_dev_buf);
573
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800574 if (cmdline_final)
575 dprintf(INFO, "cmdline: %s\n", cmdline_final);
576 else
577 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700578 return cmdline_final;
579}
580
581unsigned *atag_core(unsigned *ptr)
582{
583 /* CORE */
584 *ptr++ = 2;
585 *ptr++ = 0x54410001;
586
587 return ptr;
588
589}
590
591unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
592 unsigned ramdisk_size)
593{
594 if (ramdisk_size) {
595 *ptr++ = 4;
596 *ptr++ = 0x54420005;
597 *ptr++ = (unsigned)ramdisk;
598 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800599 }
600
Neeti Desaie245d492012-06-01 12:52:13 -0700601 return ptr;
602}
603
604unsigned *atag_ptable(unsigned **ptr_addr)
605{
606 int i;
607 struct ptable *ptable;
608
609 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700610 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
611 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700612 *(*ptr_addr)++ = 0x4d534d70;
613 for (i = 0; i < ptable->count; ++i)
614 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
615 }
616
617 return (*ptr_addr);
618}
619
620unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
621{
622 int cmdline_length = 0;
623 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700624 char *dest;
625
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800626 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700627 n = (cmdline_length + 4) & (~3);
628
629 *ptr++ = (n / 4) + 2;
630 *ptr++ = 0x54410009;
631 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800632 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700633 ptr += (n / 4);
634
635 return ptr;
636}
637
638unsigned *atag_end(unsigned *ptr)
639{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800640 /* END */
641 *ptr++ = 0;
642 *ptr++ = 0;
643
Neeti Desaie245d492012-06-01 12:52:13 -0700644 return ptr;
645}
646
647void generate_atags(unsigned *ptr, const char *cmdline,
648 void *ramdisk, unsigned ramdisk_size)
649{
650
651 ptr = atag_core(ptr);
652 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
653 ptr = target_atag_mem(ptr);
654
655 /* Skip NAND partition ATAGS for eMMC boot */
656 if (!target_is_emmc_boot()){
657 ptr = atag_ptable(&ptr);
658 }
659
660 ptr = atag_cmdline(ptr, cmdline);
661 ptr = atag_end(ptr);
662}
663
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700664typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700665void boot_linux(void *kernel, unsigned *tags,
666 const char *cmdline, unsigned machtype,
667 void *ramdisk, unsigned ramdisk_size)
668{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800669 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800670#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700671 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800672#endif
673
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700674 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800675 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800676 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800677
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530678 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700679
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800680 final_cmdline = update_cmdline((const char*)cmdline);
681
Neeti Desai17379b82012-06-04 18:42:53 -0700682#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800683 dprintf(INFO, "Updating device tree: start\n");
684
Neeti Desai17379b82012-06-04 18:42:53 -0700685 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530686 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700687 if(ret)
688 {
689 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
690 ASSERT(0);
691 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800692 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700693#else
Neeti Desaie245d492012-06-01 12:52:13 -0700694 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800695 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700696#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700697
Maria Yu52254c02014-07-04 16:14:54 +0800698 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700699
700#if VERIFIED_BOOT
701 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700702 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700703 {
704 dprintf(INFO, "Failed to write protect dev info\n");
705 ASSERT(0);
706 }
707#endif
708
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700709 /* Perform target specific cleanup */
710 target_uninit();
711
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800712 /* Turn off splash screen if enabled */
713#if DISPLAY_SPLASH_SCREEN
714 target_display_shutdown();
715#endif
716
717
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800718 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530719 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800720
721 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700722
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +0530723 /* Initialise wdog to catch early kernel crashes */
724#if WDOG_SUPPORT
725 msm_wdog_init();
726#endif
Amol Jadi4421e652011-06-16 15:00:48 -0700727 /* do any platform specific cleanup before kernel entry */
728 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700729
Brian Swetland9c4c0752009-01-25 16:23:50 -0800730 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700731
Amol Jadi504f9fe2012-08-16 13:56:48 -0700732#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800733 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700734#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700735 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800736
737 if (IS_ARM64(kptr))
738 /* Jump to a 64bit kernel */
739 scm_elexec_call((paddr_t)kernel, tags_phys);
740 else
741 /* Jump to a 32bit kernel */
742 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800743}
744
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700745/* Function to check if the memory address range falls within the aboot
746 * boundaries.
747 * start: Start of the memory region
748 * size: Size of the memory region
749 */
750int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
751{
752 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800753 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700754 return -1;
755
756 /* Check for memory overlap. */
757 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
758 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700759 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700760 return 0;
761 else
762 return -1;
763}
764
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800765#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800766
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800767BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800768#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800769BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800770#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800771
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700772static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
773{
774 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800775
776#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800777#if IMAGE_VERIF_ALGO_SHA1
778 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
779#else
780 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
781#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800782#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700783
784 /* Assume device is rooted at this time. */
785 device.is_tampered = 1;
786
787 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
788
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700789#if VERIFIED_BOOT
790 if(boot_into_recovery)
791 {
792 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530793 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700794 }
795 else
796 {
797 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530798 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700799 }
800 boot_verify_print_state();
801#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700802 ret = image_verify((unsigned char *)bootimg_addr,
803 (unsigned char *)(bootimg_addr + bootimg_size),
804 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800805 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700806#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700807 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
808
809 if (ret)
810 {
811 /* Authorized kernel */
812 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700813 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700814 }
815
Amit Blay4aa292f2015-04-28 21:55:59 +0300816#ifdef MDTP_SUPPORT
817 {
818 /* Verify MDTP lock.
819 * For boot & recovery partitions, use aboot's verification result.
820 */
821 mdtp_ext_partition_verification_t ext_partition;
822 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
823 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
824 ext_partition.page_size = 0; /* Not needed since already validated */
825 ext_partition.image_addr = 0; /* Not needed since already validated */
826 ext_partition.image_size = 0; /* Not needed since already validated */
827 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
828 mdtp_fwlock_verify_lock(&ext_partition);
829 }
830#endif /* MDTP_SUPPORT */
831
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700832#if USE_PCOM_SECBOOT
833 set_tamper_flag(device.is_tampered);
834#endif
835
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700836#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700837 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700838 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700839 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800840#if FBCON_DISPLAY_MSG
841 display_menu_thread(DISPLAY_THREAD_BOOT_STATE);
842 wait_for_users_action();
843#else
844 dprintf(CRITICAL,
845 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
846 mdelay(5000);
847#endif
848
849 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700850 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800851#if FBCON_DISPLAY_MSG
852 display_menu_thread(DISPLAY_THREAD_BOOT_STATE);
853 wait_for_users_action();
854#else
855 dprintf(CRITICAL,
856 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
857 mdelay(5000);
858#endif
859 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700860 default:
lijuanga40d6302015-07-20 20:10:13 +0800861 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700862 }
863#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700864#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530865 if(device.is_tampered)
866 {
867 write_device_info_mmc(&device);
868 #ifdef TZ_TAMPER_FUSE
869 set_tamper_fuse_cmd();
870 #endif
871 #ifdef ASSERT_ON_TAMPER
872 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
873 ASSERT(0);
874 #endif
875 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700876#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700877}
878
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530879static bool check_format_bit()
880{
881 bool ret = false;
882 int index;
883 uint64_t offset;
884 struct boot_selection_info *in = NULL;
885 char *buf = NULL;
886
887 index = partition_get_index("bootselect");
888 if (index == INVALID_PTN)
889 {
890 dprintf(INFO, "Unable to locate /bootselect partition\n");
891 return ret;
892 }
893 offset = partition_get_offset(index);
894 if(!offset)
895 {
896 dprintf(INFO, "partition /bootselect doesn't exist\n");
897 return ret;
898 }
899 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
900 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530901 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530902 {
903 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
904 free(buf);
905 return ret;
906 }
907 in = (struct boot_selection_info *) buf;
908 if ((in->signature == BOOTSELECT_SIGNATURE) &&
909 (in->version == BOOTSELECT_VERSION)) {
910 if ((in->state_info & BOOTSELECT_FORMAT) &&
911 !(in->state_info & BOOTSELECT_FACTORY))
912 ret = true;
913 } else {
914 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
915 in->signature, in->version);
916 ASSERT(0);
917 }
918 free(buf);
919 return ret;
920}
921
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700922void boot_verifier_init()
923{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700924 uint32_t boot_state;
925 /* Check if device unlock */
926 if(device.is_unlocked)
927 {
928 boot_verify_send_event(DEV_UNLOCK);
929 boot_verify_print_state();
930 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
931 return;
932 }
933 else
934 {
935 boot_verify_send_event(BOOT_INIT);
936 }
937
938 /* Initialize keystore */
939 boot_state = boot_verify_keystore_init();
940 if(boot_state == YELLOW)
941 {
942 boot_verify_print_state();
943 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
944 }
945}
946
Shashank Mittal23b8f422010-04-16 19:27:21 -0700947int boot_linux_from_mmc(void)
948{
949 struct boot_img_hdr *hdr = (void*) buf;
950 struct boot_img_hdr *uhdr;
951 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700952 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700953 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700954 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700955
Shashank Mittalcd98d472011-08-02 14:29:24 -0700956 unsigned char *image_addr = 0;
957 unsigned kernel_actual;
958 unsigned ramdisk_actual;
959 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700960 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700961
Matthew Qin271927e2015-03-31 22:07:07 -0400962 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +0800963 unsigned int out_len = 0;
964 unsigned int out_avai_len = 0;
965 unsigned char *out_addr = NULL;
966 uint32_t dtb_offset = 0;
967 unsigned char *kernel_start_addr = NULL;
968 unsigned int kernel_size = 0;
969 int rc;
970
Neeti Desai465491e2012-07-31 12:53:35 -0700971#if DEVICE_TREE
972 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700973 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700974 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800975 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700976 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -0400977 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -0700978#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +0800979 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800980
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530981 if (check_format_bit())
982 boot_into_recovery = 1;
983
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700984 if (!boot_into_recovery) {
985 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
986 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
987 if (rcode <= 0) {
988 boot_into_ffbm = false;
989 if (rcode < 0)
990 dprintf(CRITICAL,"failed to get ffbm cookie");
991 } else
992 boot_into_ffbm = true;
993 } else
994 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700995 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
996 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
997 dprintf(INFO, "Unified boot method!\n");
998 hdr = uhdr;
999 goto unified_boot;
1000 }
Greg Griscod6250552011-06-29 14:40:23 -07001001 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001002 index = partition_get_index("boot");
1003 ptn = partition_get_offset(index);
1004 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001005 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1006 return -1;
1007 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001008 }
1009 else {
1010 index = partition_get_index("recovery");
1011 ptn = partition_get_offset(index);
1012 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001013 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1014 return -1;
1015 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001016 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001017 /* Set Lun for boot & recovery partitions */
1018 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001019
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301020 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001021 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1022 return -1;
1023 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001024
1025 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001026 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001027 return -1;
1028 }
1029
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001030 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301031
1032 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1033 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1034 return -1;
1035 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001036 page_size = hdr->page_size;
1037 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001038 }
1039
Matthew Qin49e51fa2015-02-09 10:40:45 +08001040 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1041 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001042
Matthew Qin49e51fa2015-02-09 10:40:45 +08001043 image_addr = (unsigned char *)target_get_scratch_address();
1044
1045#if DEVICE_TREE
1046 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1047 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1048#else
1049 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1050#endif
1051
1052#if VERIFIED_BOOT
1053 boot_verifier_init();
1054#endif
1055
1056 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
1057 {
1058 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1059 return -1;
1060 }
1061
Matthew Qinbb7923d2015-02-09 10:56:09 +08001062 /*
1063 * Update loading flow of bootimage to support compressed/uncompressed
1064 * bootimage on both 64bit and 32bit platform.
1065 * 1. Load bootimage from emmc partition onto DDR.
1066 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1067 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1068 * platform accordingly.
1069 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1070 */
1071
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001072 dprintf(INFO, "Loading (%s) image (%d): start\n",
1073 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001074 bs_set_timestamp(BS_KERNEL_LOAD_START);
1075
Matthew Qinbb7923d2015-02-09 10:56:09 +08001076 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001077 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1078 {
1079 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1080 return -1;
1081 }
1082
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001083 dprintf(INFO, "Loading (%s) image (%d): done\n",
1084 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1085
Matthew Qin49e51fa2015-02-09 10:40:45 +08001086 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1087
1088 /* Authenticate Kernel */
1089 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1090 (int) target_use_signed_kernel(),
1091 device.is_unlocked,
1092 device.is_tampered);
1093
1094 if(target_use_signed_kernel() && (!device.is_unlocked))
1095 {
1096 offset = imagesize_actual;
1097 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1098 {
1099 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1100 return -1;
1101 }
1102
1103 /* Read signature */
1104 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1105 {
1106 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1107 return -1;
1108 }
1109
1110 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
1111 } else {
1112 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1113 #ifdef TZ_SAVE_KERNEL_HASH
1114 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1115 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001116
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001117#if VERIFIED_BOOT
1118 if(boot_verify_get_state() == ORANGE)
1119 {
lijuanga40d6302015-07-20 20:10:13 +08001120#if FBCON_DISPLAY_MSG
1121 display_menu_thread(DISPLAY_THREAD_BOOT_STATE);
1122 wait_for_users_action();
1123#else
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001124 dprintf(CRITICAL,
lijuanga40d6302015-07-20 20:10:13 +08001125 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001126 mdelay(5000);
lijuanga40d6302015-07-20 20:10:13 +08001127#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001128 }
1129#endif
1130
Amit Blay4aa292f2015-04-28 21:55:59 +03001131#ifdef MDTP_SUPPORT
1132 {
1133 /* Verify MDTP lock.
1134 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1135 * since verification was skipped in aboot. The signature is not part of the loaded image.
1136 */
1137 mdtp_ext_partition_verification_t ext_partition;
1138 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1139 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1140 ext_partition.page_size = page_size;
1141 ext_partition.image_addr = (uint32)image_addr;
1142 ext_partition.image_size = imagesize_actual;
1143 ext_partition.sig_avail = FALSE;
1144 mdtp_fwlock_verify_lock(&ext_partition);
1145 }
1146#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001147 }
1148
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001149#if VERIFIED_BOOT
1150 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001151 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001152 ASSERT(0);
1153#endif
1154
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001155 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001156 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1157 * If not, continue booting.
1158 */
1159 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1160 {
1161 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1162 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001163 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001164 rc = decompress((unsigned char *)(image_addr + page_size),
1165 hdr->kernel_size, out_addr, out_avai_len,
1166 &dtb_offset, &out_len);
1167 if (rc)
1168 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001169 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001170 ASSERT(0);
1171 }
1172
Matthew Qin0b15b322015-05-19 05:20:54 -04001173 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001174 kptr = (struct kernel64_hdr *)out_addr;
1175 kernel_start_addr = out_addr;
1176 kernel_size = out_len;
1177 } else {
1178 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1179 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1180 kernel_size = hdr->kernel_size;
1181 }
1182
1183 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001184 * Update the kernel/ramdisk/tags address if the boot image header
1185 * has default values, these default values come from mkbootimg when
1186 * the boot image is flashed using fastboot flash:raw
1187 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001188 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001189
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001190 /* Get virtual addresses since the hdr saves physical addresses. */
1191 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1192 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1193 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001194
Matthew Qinbb7923d2015-02-09 10:56:09 +08001195 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001196 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001197 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001198 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1199 {
1200 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1201 return -1;
1202 }
1203
1204#ifndef DEVICE_TREE
1205 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1206 {
1207 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1208 return -1;
1209 }
1210#endif
1211
Matthew Qin49e51fa2015-02-09 10:40:45 +08001212 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001213 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001214 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001215
Matthew Qin49e51fa2015-02-09 10:40:45 +08001216 #if DEVICE_TREE
1217 if(hdr->dt_size) {
1218 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1219 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001220
Matthew Qin49e51fa2015-02-09 10:40:45 +08001221 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1222 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1223 return -1;
1224 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001225
Matthew Qin49e51fa2015-02-09 10:40:45 +08001226 /* Find index of device tree within device tree table */
1227 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1228 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1229 return -1;
1230 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001231
Matthew Qin271927e2015-03-31 22:07:07 -04001232 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1233 {
1234 unsigned int compressed_size = 0;
1235 out_addr += out_len;
1236 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001237 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001238 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1239 dt_entry.size, out_addr, out_avai_len,
1240 &compressed_size, &dtb_size);
1241 if (rc)
1242 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001243 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001244 ASSERT(0);
1245 }
1246
Matthew Qin0b15b322015-05-19 05:20:54 -04001247 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001248 best_match_dt_addr = out_addr;
1249 } else {
1250 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1251 dtb_size = dt_entry.size;
1252 }
1253
Matthew Qin49e51fa2015-02-09 10:40:45 +08001254 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001255 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001256 {
1257 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1258 return -1;
1259 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001260
Matthew Qin271927e2015-03-31 22:07:07 -04001261 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001262 } else {
1263 /* Validate the tags_addr */
1264 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001265 {
1266 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1267 return -1;
1268 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001269 /*
1270 * If appended dev tree is found, update the atags with
1271 * memory address to the DTB appended location on RAM.
1272 * Else update with the atags address in the kernel header
1273 */
1274 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001275 dtb = dev_tree_appended((void*)(image_addr + page_size),
1276 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001277 (void *)hdr->tags_addr);
1278 if (!dtb) {
1279 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001280 return -1;
1281 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001282 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001283 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001284
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001285 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1286 target_load_ssd_keystore();
1287
Shashank Mittal23b8f422010-04-16 19:27:21 -07001288unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001289
Dima Zavin77e41f32013-03-06 16:10:43 -08001290 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001291 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001292 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1293
1294 return 0;
1295}
1296
Dima Zavin214cc642009-01-26 11:16:21 -08001297int boot_linux_from_flash(void)
1298{
1299 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001300 struct ptentry *ptn;
1301 struct ptable *ptable;
1302 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001303
Shashank Mittalcd98d472011-08-02 14:29:24 -07001304 unsigned char *image_addr = 0;
1305 unsigned kernel_actual;
1306 unsigned ramdisk_actual;
1307 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001308 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001309
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001310#if DEVICE_TREE
1311 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001312 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001313 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001314 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001315#endif
1316
David Ng183a7422009-12-07 14:55:21 -08001317 if (target_is_emmc_boot()) {
1318 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1319 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1320 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1321 return -1;
1322 }
1323 goto continue_boot;
1324 }
1325
Dima Zavin214cc642009-01-26 11:16:21 -08001326 ptable = flash_get_ptable();
1327 if (ptable == NULL) {
1328 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1329 return -1;
1330 }
1331
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001332 if(!boot_into_recovery)
1333 {
1334 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001335
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001336 if (ptn == NULL) {
1337 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1338 return -1;
1339 }
1340 }
1341 else
1342 {
1343 ptn = ptable_find(ptable, "recovery");
1344 if (ptn == NULL) {
1345 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1346 return -1;
1347 }
Dima Zavin214cc642009-01-26 11:16:21 -08001348 }
1349
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001350 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001351 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1352 return -1;
1353 }
Dima Zavin214cc642009-01-26 11:16:21 -08001354
1355 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001356 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001357 return -1;
1358 }
1359
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001360 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001361 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 -08001362 return -1;
1363 }
1364
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001365 /*
1366 * Update the kernel/ramdisk/tags address if the boot image header
1367 * has default values, these default values come from mkbootimg when
1368 * the boot image is flashed using fastboot flash:raw
1369 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001370 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001371
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001372 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001373 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1374 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1375 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1376
1377 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1378 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1379
1380 /* Check if the addresses in the header are valid. */
1381 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1382 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1383 {
1384 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1385 return -1;
1386 }
1387
1388#ifndef DEVICE_TREE
1389 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1390 {
1391 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1392 return -1;
1393 }
1394#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001395
Shashank Mittalcd98d472011-08-02 14:29:24 -07001396 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001397 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001398 {
1399 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001400 offset = 0;
1401
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001402#if DEVICE_TREE
1403 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1404 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001405
1406 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1407 {
1408 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1409 return -1;
1410 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001411#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001412 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001413#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001414
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001415 dprintf(INFO, "Loading (%s) image (%d): start\n",
1416 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001417 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001418
Shashank Mittalcd98d472011-08-02 14:29:24 -07001419 /* Read image without signature */
1420 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1421 {
1422 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1423 return -1;
1424 }
Dima Zavin214cc642009-01-26 11:16:21 -08001425
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001426 dprintf(INFO, "Loading (%s) image (%d): done\n",
1427 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001428 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001429
Shashank Mittalcd98d472011-08-02 14:29:24 -07001430 offset = imagesize_actual;
1431 /* Read signature */
1432 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1433 {
1434 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001435 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001436 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001437
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301438 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001439
1440 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001441 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1442 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001443#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001444 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001445 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001446 {
1447 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1448 return -1;
1449 }
1450
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001451 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1452#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001453
1454 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001455 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001456 {
1457 write_device_info_flash(&device);
1458 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301459#if USE_PCOM_SECBOOT
1460 set_tamper_flag(device.is_tampered);
1461#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001462 }
1463 else
1464 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001465 offset = page_size;
1466
Amol Jadib6be5c12012-11-14 13:39:51 -08001467 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1468 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1469 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1470
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001471 dprintf(INFO, "Loading (%s) image (%d): start\n",
1472 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1473
Amol Jadi492d5a52013-03-15 16:12:34 -07001474 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001475
1476 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001477 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1478 return -1;
1479 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001480 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001481
Amol Jadib6be5c12012-11-14 13:39:51 -08001482 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001483 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1484 return -1;
1485 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001486 offset += ramdisk_actual;
1487
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001488 dprintf(INFO, "Loading (%s) image (%d): done\n",
1489 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1490
Amol Jadi492d5a52013-03-15 16:12:34 -07001491 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001492
1493 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001494 offset += second_actual;
1495 /* Second image loading not implemented. */
1496 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001497 }
1498
1499#if DEVICE_TREE
1500 if(hdr->dt_size != 0) {
1501
1502 /* Read the device tree table into buffer */
1503 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1504 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1505 return -1;
1506 }
1507
1508 table = (struct dt_table*) dt_buf;
1509
Deepa Dinamani19648b42013-09-05 17:05:55 -07001510 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001511 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1512 return -1;
1513 }
1514
Deepa Dinamani19648b42013-09-05 17:05:55 -07001515 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1516 if (!table)
1517 return -1;
1518
1519 /* Read the entire device tree table into buffer */
1520 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1521 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1522 return -1;
1523 }
1524
1525
Joel Kingaa335dc2013-06-03 16:11:08 -07001526 /* Find index of device tree within device tree table */
1527 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001528 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1529 return -1;
1530 }
1531
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001532 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001533 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001534 {
1535 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1536 return -1;
1537 }
1538
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001539 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001540 if(flash_read(ptn, offset + dt_entry.offset,
1541 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001542 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1543 return -1;
1544 }
1545 }
1546#endif
1547
Shashank Mittalcd98d472011-08-02 14:29:24 -07001548 }
David Ng183a7422009-12-07 14:55:21 -08001549continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001550
Dima Zavin214cc642009-01-26 11:16:21 -08001551 /* TODO: create/pass atags to kernel */
1552
Ajay Dudanie28a6072011-07-01 13:59:46 -07001553 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001554 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001555 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1556
1557 return 0;
1558}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001559
Shashank Mittal162244e2011-08-08 19:01:25 -07001560void write_device_info_mmc(device_info *dev)
1561{
Shashank Mittal162244e2011-08-08 19:01:25 -07001562 unsigned long long ptn = 0;
1563 unsigned long long size;
1564 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001565 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001566 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001567 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001568
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001569 if (devinfo_present)
1570 index = partition_get_index("devinfo");
1571 else
1572 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001573
Shashank Mittal162244e2011-08-08 19:01:25 -07001574 ptn = partition_get_offset(index);
1575 if(ptn == 0)
1576 {
1577 return;
1578 }
1579
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001580 lun = partition_get_lun(index);
1581 mmc_set_lun(lun);
1582
Shashank Mittal162244e2011-08-08 19:01:25 -07001583 size = partition_get_size(index);
1584
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001585 blocksize = mmc_get_device_blocksize();
1586
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001587 if (devinfo_present)
1588 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1589 else
1590 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1591 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001592 {
1593 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001594 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001595 }
1596}
1597
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001598void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001599{
Shashank Mittal162244e2011-08-08 19:01:25 -07001600 unsigned long long ptn = 0;
1601 unsigned long long size;
1602 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001603 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001604 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001605
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001606 if ((index = partition_get_index("devinfo")) < 0)
1607 {
1608 devinfo_present = false;
1609 index = partition_get_index("aboot");
1610 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001611
Shashank Mittal162244e2011-08-08 19:01:25 -07001612 ptn = partition_get_offset(index);
1613 if(ptn == 0)
1614 {
1615 return;
1616 }
1617
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001618 mmc_set_lun(partition_get_lun(index));
1619
Shashank Mittal162244e2011-08-08 19:01:25 -07001620 size = partition_get_size(index);
1621
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001622 blocksize = mmc_get_device_blocksize();
1623
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001624 if (devinfo_present)
1625 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1626 else
1627 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1628 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001629 {
1630 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001631 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001632 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001633}
1634
1635void write_device_info_flash(device_info *dev)
1636{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001637 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001638 struct ptentry *ptn;
1639 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001640 if(info == NULL)
1641 {
1642 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1643 ASSERT(0);
1644 }
1645 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001646 ptable = flash_get_ptable();
1647 if (ptable == NULL)
1648 {
1649 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1650 return;
1651 }
1652
1653 ptn = ptable_find(ptable, "devinfo");
1654 if (ptn == NULL)
1655 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001656 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001657 return;
1658 }
1659
1660 memcpy(info, dev, sizeof(device_info));
1661
1662 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1663 {
1664 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1665 return;
1666 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001667 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001668}
1669
vijay kumarc65876c2015-04-24 13:29:16 +05301670static int read_allow_oem_unlock(device_info *dev)
1671{
vijay kumarc65876c2015-04-24 13:29:16 +05301672 unsigned offset;
1673 int index;
1674 unsigned long long ptn;
1675 unsigned long long ptn_size;
1676 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001677 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301678
vijay kumarca2e6812015-07-08 20:28:25 +05301679 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301680 if (index == INVALID_PTN)
1681 {
vijay kumarca2e6812015-07-08 20:28:25 +05301682 index = partition_get_index(frp_ptns[1]);
1683 if (index == INVALID_PTN)
1684 {
1685 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1686 return -1;
1687 }
vijay kumarc65876c2015-04-24 13:29:16 +05301688 }
1689
1690 ptn = partition_get_offset(index);
1691 ptn_size = partition_get_size(index);
1692 offset = ptn_size - blocksize;
1693
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001694 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301695 {
1696 dprintf(CRITICAL, "Reading MMC failed\n");
1697 return -1;
1698 }
1699
1700 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1701 is_allow_unlock = buf[blocksize-1] & 0x01;
1702 return 0;
1703}
1704
1705static int write_allow_oem_unlock(bool allow_unlock)
1706{
vijay kumarc65876c2015-04-24 13:29:16 +05301707 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301708 int index;
1709 unsigned long long ptn;
1710 unsigned long long ptn_size;
1711 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001712 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301713
vijay kumarca2e6812015-07-08 20:28:25 +05301714 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301715 if (index == INVALID_PTN)
1716 {
vijay kumarca2e6812015-07-08 20:28:25 +05301717 index = partition_get_index(frp_ptns[1]);
1718 if (index == INVALID_PTN)
1719 {
1720 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1721 return -1;
1722 }
vijay kumarc65876c2015-04-24 13:29:16 +05301723 }
1724
1725 ptn = partition_get_offset(index);
1726 ptn_size = partition_get_size(index);
1727 offset = ptn_size - blocksize;
1728
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001729 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301730 {
1731 dprintf(CRITICAL, "Reading MMC failed\n");
1732 return -1;
1733 }
1734
1735 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1736 buf[blocksize-1] = allow_unlock;
1737 if (mmc_write(ptn + offset, blocksize, buf))
1738 {
1739 dprintf(CRITICAL, "Writing MMC failed\n");
1740 return -1;
1741 }
1742
1743 return 0;
1744}
1745
Shashank Mittal162244e2011-08-08 19:01:25 -07001746void read_device_info_flash(device_info *dev)
1747{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001748 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001749 struct ptentry *ptn;
1750 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001751 if(info == NULL)
1752 {
1753 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1754 ASSERT(0);
1755 }
1756 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001757 ptable = flash_get_ptable();
1758 if (ptable == NULL)
1759 {
1760 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1761 return;
1762 }
1763
1764 ptn = ptable_find(ptable, "devinfo");
1765 if (ptn == NULL)
1766 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001767 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001768 return;
1769 }
1770
1771 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1772 {
1773 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1774 return;
1775 }
1776
1777 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1778 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001779 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1780 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001781 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001782 write_device_info_flash(info);
1783 }
1784 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001785 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001786}
1787
1788void write_device_info(device_info *dev)
1789{
1790 if(target_is_emmc_boot())
1791 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001792 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1793 if(info == NULL)
1794 {
1795 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1796 ASSERT(0);
1797 }
1798 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001799 memcpy(info, dev, sizeof(struct device_info));
1800
1801#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001802 if (is_secure_boot_enable()) {
1803 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1804 ASSERT(0);
1805 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001806 else
1807 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001808#else
1809 write_device_info_mmc(info);
1810#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001811 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001812 }
1813 else
1814 {
1815 write_device_info_flash(dev);
1816 }
1817}
1818
1819void read_device_info(device_info *dev)
1820{
1821 if(target_is_emmc_boot())
1822 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001823 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1824 if(info == NULL)
1825 {
1826 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1827 ASSERT(0);
1828 }
1829 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001830
1831#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001832 if (is_secure_boot_enable()) {
1833 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1834 ASSERT(0);
1835 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001836 else
1837 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001838#else
1839 read_device_info_mmc(info);
1840#endif
1841
1842 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1843 {
1844 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001845 if (is_secure_boot_enable())
1846 info->is_unlocked = 0;
1847 else
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001848 info->is_unlocked = 1;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001849 info->is_tampered = 0;
1850 info->charger_screen_enabled = 0;
Sridhar Parasuram287e5062015-08-24 16:17:22 -07001851 info->verity_mode = 1; //enforcing by default
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001852 write_device_info(info);
1853 }
1854 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001855 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001856 }
1857 else
1858 {
1859 read_device_info_flash(dev);
1860 }
1861}
1862
1863void reset_device_info()
1864{
1865 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001866 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001867 write_device_info(&device);
1868}
1869
1870void set_device_root()
1871{
1872 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001873 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001874 write_device_info(&device);
1875}
1876
lijuang21f12f52015-08-22 16:22:19 +08001877void set_oem_unlock()
1878{
1879 if(!device.is_unlocked) {
1880 if(!is_allow_unlock) {
1881 fastboot_fail("oem unlock is not allowed");
1882 return;
1883 }
1884
1885 device.is_unlocked = 1;
1886 write_device_info(&device);
1887 }
1888}
1889
Amol Jadicb524072012-08-09 16:40:18 -07001890#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001891int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
Amol Jadicb524072012-08-09 16:40:18 -07001892{
1893 uint32 dt_image_offset = 0;
1894 uint32_t n;
1895 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001896 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001897 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001898 unsigned int compressed_size = 0;
1899 unsigned int dtb_size = 0;
1900 unsigned int out_avai_len = 0;
1901 unsigned char *out_addr = NULL;
1902 unsigned char *best_match_dt_addr = NULL;
1903 int rc;
Amol Jadicb524072012-08-09 16:40:18 -07001904
1905 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1906
Amol Jadicb524072012-08-09 16:40:18 -07001907 if(hdr->dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001908 /* add kernel offset */
1909 dt_image_offset += page_size;
1910 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1911 dt_image_offset += n;
1912
1913 /* add ramdisk offset */
1914 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1915 dt_image_offset += n;
1916
1917 /* add second offset */
1918 if(hdr->second_size != 0) {
1919 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1920 dt_image_offset += n;
1921 }
1922
1923 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001924 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001925
Deepa Dinamani19648b42013-09-05 17:05:55 -07001926 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001927 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1928 return -1;
1929 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001930 /* Find index of device tree within device tree table */
1931 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001932 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1933 return -1;
1934 }
1935
Matthew Qin271927e2015-03-31 22:07:07 -04001936 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
1937 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
1938 {
1939 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
1940 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04001941 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001942 rc = decompress(best_match_dt_addr,
1943 dt_entry.size, out_addr, out_avai_len,
1944 &compressed_size, &dtb_size);
1945 if (rc)
1946 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001947 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001948 ASSERT(0);
1949 }
1950
Matthew Qin0b15b322015-05-19 05:20:54 -04001951 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001952 best_match_dt_addr = out_addr;
1953 } else {
1954 dtb_size = dt_entry.size;
1955 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001956 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001957 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001958 {
1959 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1960 return -1;
1961 }
1962
Amol Jadicb524072012-08-09 16:40:18 -07001963 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001964 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001965 } else
1966 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001967
1968 /* Everything looks fine. Return success. */
1969 return 0;
1970}
1971#endif
1972
Brian Swetland9c4c0752009-01-25 16:23:50 -08001973void cmd_boot(const char *arg, void *data, unsigned sz)
1974{
Amit Blay8a510302015-08-17 09:20:01 +03001975#ifdef MDTP_SUPPORT
1976 static bool is_mdtp_activated = 0;
1977#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08001978 unsigned kernel_actual;
1979 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001980 uint32_t image_actual;
1981 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001982 uint32_t sig_actual = SIGNATURE_SIZE;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001983 struct boot_img_hdr *hdr = NULL;
1984 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001985 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001986 int ret = 0;
1987 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001988 unsigned int out_len = 0;
1989 unsigned int out_avai_len = 0;
1990 unsigned char *out_addr = NULL;
1991 uint32_t dtb_offset = 0;
1992 unsigned char *kernel_start_addr = NULL;
1993 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04001994 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001995
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001996#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07001997 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001998 {
1999 fastboot_fail("unlock device to use this command");
2000 return;
2001 }
2002#endif
2003
Brian Swetland9c4c0752009-01-25 16:23:50 -08002004 if (sz < sizeof(hdr)) {
2005 fastboot_fail("invalid bootimage header");
2006 return;
2007 }
2008
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002009 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002010
2011 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002012 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002013
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002014 if(target_is_emmc_boot() && hdr->page_size) {
2015 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002016 page_mask = page_size - 1;
2017 }
2018
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002019 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2020 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002021#if DEVICE_TREE
2022 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2023#endif
2024
2025 image_actual = ADD_OF(page_size, kernel_actual);
2026 image_actual = ADD_OF(image_actual, ramdisk_actual);
2027 image_actual = ADD_OF(image_actual, dt_actual);
2028
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002029 if (target_use_signed_kernel() && (!device.is_unlocked))
2030 image_actual = ADD_OF(image_actual, sig_actual);
2031
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002032 /* sz should have atleast raw boot image */
2033 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002034 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002035 return;
2036 }
2037
2038 /* Verify the boot image
2039 * device & page_size are initialized in aboot_init
2040 */
2041 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002042 /* Pass size excluding signature size, otherwise we would try to
2043 * access signature beyond its length
2044 */
2045 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002046
Amit Blay4aa292f2015-04-28 21:55:59 +03002047#ifdef MDTP_SUPPORT
2048 else
2049 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002050 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002051 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002052
2053 if (!is_mdtp_activated) {
2054 ext_partition.partition = MDTP_PARTITION_NONE;
2055 mdtp_fwlock_verify_lock(&ext_partition);
2056 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002057 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002058
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002059 mdtp_activated(&is_mdtp_activated);
2060 if(is_mdtp_activated){
2061 dprintf(CRITICAL, "fastboot boot command is not available.\n");
2062 return;
2063 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002064#endif /* MDTP_SUPPORT */
2065
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002066 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002067 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2068 * If not, continue booting.
2069 */
2070 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2071 {
2072 out_addr = (unsigned char *)target_get_scratch_address();
2073 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2074 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002075 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002076 ret = decompress((unsigned char *)(ptr + page_size),
2077 hdr->kernel_size, out_addr, out_avai_len,
2078 &dtb_offset, &out_len);
2079 if (ret)
2080 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002081 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002082 ASSERT(0);
2083 }
2084
Matthew Qin0b15b322015-05-19 05:20:54 -04002085 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002086 kptr = (struct kernel64_hdr *)out_addr;
2087 kernel_start_addr = out_addr;
2088 kernel_size = out_len;
2089 } else {
2090 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2091 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2092 kernel_size = hdr->kernel_size;
2093 }
2094
2095 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002096 * Update the kernel/ramdisk/tags address if the boot image header
2097 * has default values, these default values come from mkbootimg when
2098 * the boot image is flashed using fastboot flash:raw
2099 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002100 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002101
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002102 /* Get virtual addresses since the hdr saves physical addresses. */
2103 hdr->kernel_addr = VA(hdr->kernel_addr);
2104 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2105 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002106
Matthew Qinbb7923d2015-02-09 10:56:09 +08002107 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002108 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002109 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002110 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2111 {
2112 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002113 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002114 }
2115
Amol Jadicb524072012-08-09 16:40:18 -07002116#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002117 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002118 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002119 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002120
2121 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002122#else
2123 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2124 {
2125 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002126 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002127 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002128#endif
2129
2130 /* Load ramdisk & kernel */
2131 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002132 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002133
2134#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002135 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2136 {
2137 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2138 return;
2139 }
2140
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002141 /*
2142 * If dtb is not found look for appended DTB in the kernel.
2143 * If appended dev tree is found, update the atags with
2144 * memory address to the DTB appended location on RAM.
2145 * Else update with the atags address in the kernel header
2146 */
2147 if (!dtb_copied) {
2148 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002149 dtb = dev_tree_appended((void*)(ptr + page_size),
2150 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002151 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002152 if (!dtb) {
2153 fastboot_fail("dtb not found");
2154 return;
2155 }
Amol Jadicb524072012-08-09 16:40:18 -07002156 }
2157#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002158
2159 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002160 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002161
Dima Zavin77e41f32013-03-06 16:10:43 -08002162 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002163 (const char*) hdr->cmdline, board_machtype(),
2164 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002165}
2166
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002167void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002168{
2169 struct ptentry *ptn;
2170 struct ptable *ptable;
2171
2172 ptable = flash_get_ptable();
2173 if (ptable == NULL) {
2174 fastboot_fail("partition table doesn't exist");
2175 return;
2176 }
2177
2178 ptn = ptable_find(ptable, arg);
2179 if (ptn == NULL) {
2180 fastboot_fail("unknown partition name");
2181 return;
2182 }
2183
2184 if (flash_erase(ptn)) {
2185 fastboot_fail("failed to erase partition");
2186 return;
2187 }
2188 fastboot_okay("");
2189}
2190
Bikas Gurungd48bd242010-09-04 19:54:32 -07002191
2192void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2193{
2194 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002195 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002196 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002197 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002198
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002199#if VERIFIED_BOOT
2200 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2201 {
2202 if(!device.is_unlocked)
2203 {
2204 fastboot_fail("unlock device to erase keystore");
2205 return;
2206 }
2207 }
2208#endif
2209
Kinson Chikf1a43512011-07-14 11:28:39 -07002210 index = partition_get_index(arg);
2211 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002212 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002213
Kinson Chikf1a43512011-07-14 11:28:39 -07002214 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002215 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002216 return;
2217 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002218
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002219 lun = partition_get_lun(index);
2220 mmc_set_lun(lun);
2221
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002222 if (platform_boot_dev_isemmc())
2223 {
2224 if (mmc_erase_card(ptn, size)) {
2225 fastboot_fail("failed to erase partition\n");
2226 return;
2227 }
2228 } else {
2229 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2230 size = partition_get_size(index);
2231 if (size > DEFAULT_ERASE_SIZE)
2232 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002233
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002234 /* Simple inefficient version of erase. Just writing
2235 0 in first several blocks */
2236 if (mmc_write(ptn , size, (unsigned int *)out)) {
2237 fastboot_fail("failed to erase partition");
2238 return;
2239 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002240 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002241#if VERIFIED_BOOT
2242 if(!(strncmp(arg, "userdata", 8)))
2243 if(send_delete_keys_to_tz())
2244 ASSERT(0);
2245#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002246 fastboot_okay("");
2247}
2248
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002249void cmd_erase(const char *arg, void *data, unsigned sz)
2250{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002251#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002252 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002253 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002254 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002255 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002256 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002257 return;
2258 }
2259 }
2260#endif
2261
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002262 if(target_is_emmc_boot())
2263 cmd_erase_mmc(arg, data, sz);
2264 else
2265 cmd_erase_nand(arg, data, sz);
2266}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002267
Ajay Dudani5c761132011-04-07 20:19:04 -07002268void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002269{
2270 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002271 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002272 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002273 char *token = NULL;
2274 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002275 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002276 uint8_t lun = 0;
2277 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002278
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002279 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002280 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002281 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002282 if(token)
2283 {
2284 lun = atoi(token);
2285 mmc_set_lun(lun);
2286 lun_set = true;
2287 }
2288
Mao Jinlong226f33a2014-07-04 17:24:10 +08002289 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002290 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002291 if (!strcmp(pname, "partition"))
2292 {
2293 dprintf(INFO, "Attempt to write partition image.\n");
2294 if (write_partition(sz, (unsigned char *) data)) {
2295 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002296 return;
2297 }
2298 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002299 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002300 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002301#if VERIFIED_BOOT
2302 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2303 {
2304 if(!device.is_unlocked)
2305 {
2306 fastboot_fail("unlock device to flash keystore");
2307 return;
2308 }
2309 if(!boot_verify_validate_keystore((unsigned char *)data))
2310 {
2311 fastboot_fail("image is not a keystore file");
2312 return;
2313 }
2314 }
2315#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002316 index = partition_get_index(pname);
2317 ptn = partition_get_offset(index);
2318 if(ptn == 0) {
2319 fastboot_fail("partition table doesn't exist");
2320 return;
2321 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002322
Mao Jinlong226f33a2014-07-04 17:24:10 +08002323 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2324 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2325 fastboot_fail("image is not a boot image");
2326 return;
2327 }
2328 }
2329
2330 if(!lun_set)
2331 {
2332 lun = partition_get_lun(index);
2333 mmc_set_lun(lun);
2334 }
2335
2336 size = partition_get_size(index);
2337 if (ROUND_TO_PAGE(sz,511) > size) {
2338 fastboot_fail("size too large");
2339 return;
2340 }
2341 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2342 fastboot_fail("flash write failure");
2343 return;
2344 }
Greg Grisco6e754772011-06-23 12:19:39 -07002345 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002346 }
2347 fastboot_okay("");
2348 return;
2349}
2350
Ajay Dudanide984792015-03-02 09:57:41 -08002351void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2352{
2353 int i, images;
2354 meta_header_t *meta_header;
2355 img_header_entry_t *img_header_entry;
2356
2357 meta_header = (meta_header_t*) data;
2358 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2359
2360 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2361
2362 for (i=0; i<images; i++) {
2363
2364 if((img_header_entry[i].ptn_name == NULL) ||
2365 (img_header_entry[i].start_offset == 0) ||
2366 (img_header_entry[i].size == 0))
2367 break;
2368
2369 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2370 (void *) data + img_header_entry[i].start_offset,
2371 img_header_entry[i].size);
2372 }
2373
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002374 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2375 {
2376 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2377 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2378 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2379 }
2380 else
2381 {
2382 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2383 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2384 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2385 }
2386
2387 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002388 fastboot_okay("");
2389 return;
2390}
2391
Ajay Dudani5c761132011-04-07 20:19:04 -07002392void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2393{
2394 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002395 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002396 uint32_t *fill_buf = NULL;
2397 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002398 sparse_header_t *sparse_header;
2399 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002400 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002401 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302402 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002403 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302404 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002405 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302406 /*End of the sparse image address*/
2407 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002408
Kinson Chikf1a43512011-07-14 11:28:39 -07002409 index = partition_get_index(arg);
2410 ptn = partition_get_offset(index);
2411 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002412 fastboot_fail("partition table doesn't exist");
2413 return;
2414 }
2415
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302416 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302417
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002418 lun = partition_get_lun(index);
2419 mmc_set_lun(lun);
2420
vijay kumar800255e2015-04-24 20:26:19 +05302421 if (sz < sizeof(sparse_header_t)) {
2422 fastboot_fail("size too low");
2423 return;
2424 }
2425
Ajay Dudani5c761132011-04-07 20:19:04 -07002426 /* Read and skip over sparse image header */
2427 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302428
vijay kumar1321f342015-03-27 12:13:42 +05302429 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002430 fastboot_fail("size too large");
2431 return;
2432 }
2433
vijay kumarde4fcf62015-04-23 13:05:49 +05302434 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302435
2436 if (data_end < (uint32_t)data) {
2437 fastboot_fail("buffer overreads occured due to invalid sparse header");
2438 return;
2439 }
2440
vijay kumarde4fcf62015-04-23 13:05:49 +05302441 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002442 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302443 fastboot_fail("sparse header size mismatch");
2444 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002445 }
2446
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002447 dprintf (SPEW, "=== Sparse Image Header ===\n");
2448 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2449 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2450 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2451 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2452 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2453 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2454 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2455 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002456
2457 /* Start processing chunks */
2458 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2459 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302460 /* Make sure the total image size does not exceed the partition size */
2461 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2462 fastboot_fail("size too large");
2463 return;
2464 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002465 /* Read and skip over chunk header */
2466 chunk_header = (chunk_header_t *) data;
2467 data += sizeof(chunk_header_t);
2468
vijay kumar800255e2015-04-24 20:26:19 +05302469 if (data_end < (uint32_t)data) {
2470 fastboot_fail("buffer overreads occured due to invalid sparse header");
2471 return;
2472 }
2473
Ajay Dudani5c761132011-04-07 20:19:04 -07002474 dprintf (SPEW, "=== Chunk Header ===\n");
2475 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2476 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2477 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2478
vijay kumar800255e2015-04-24 20:26:19 +05302479 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002480 {
vijay kumar800255e2015-04-24 20:26:19 +05302481 fastboot_fail("chunk header size mismatch");
2482 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002483 }
2484
wufeng.jiang813dc352015-06-02 23:02:46 -04002485 if (!sparse_header->blk_sz ){
2486 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302487 return;
2488 }
2489
wufeng.jiang813dc352015-06-02 23:02:46 -04002490 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2491
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302492 /* Make sure that the chunk size calculated from sparse image does not
2493 * exceed partition size
2494 */
2495 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2496 {
2497 fastboot_fail("Chunk data size exceeds partition size");
2498 return;
2499 }
2500
Ajay Dudani5c761132011-04-07 20:19:04 -07002501 switch (chunk_header->chunk_type)
2502 {
2503 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002504 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002505 chunk_data_sz))
2506 {
2507 fastboot_fail("Bogus chunk size for chunk type Raw");
2508 return;
2509 }
2510
vijay kumar800255e2015-04-24 20:26:19 +05302511 if (data_end < (uint32_t)data + chunk_data_sz) {
2512 fastboot_fail("buffer overreads occured due to invalid sparse header");
2513 return;
2514 }
2515
wufeng.jiang813dc352015-06-02 23:02:46 -04002516 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2517 otherwise it will return in the line above
2518 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002519 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002520 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002521 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002522 {
2523 fastboot_fail("flash write failure");
2524 return;
2525 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302526 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2527 fastboot_fail("Bogus size for RAW chunk type");
2528 return;
2529 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002530 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002531 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002532 break;
2533
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002534 case CHUNK_TYPE_FILL:
2535 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2536 sizeof(uint32_t)))
2537 {
2538 fastboot_fail("Bogus chunk size for chunk type FILL");
2539 return;
2540 }
2541
2542 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2543 if (!fill_buf)
2544 {
2545 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2546 return;
2547 }
2548
vijay kumar800255e2015-04-24 20:26:19 +05302549 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2550 fastboot_fail("buffer overreads occured due to invalid sparse header");
2551 return;
2552 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002553 fill_val = *(uint32_t *)data;
2554 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002555
2556 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2557 {
2558 fill_buf[i] = fill_val;
2559 }
2560
wufeng.jiang813dc352015-06-02 23:02:46 -04002561 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002562 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302563 /* Make sure that the data written to partition does not exceed partition size */
2564 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2565 {
2566 fastboot_fail("Chunk data size for fill type exceeds partition size");
2567 return;
2568 }
2569
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002570 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2571 sparse_header->blk_sz,
2572 fill_buf))
2573 {
2574 fastboot_fail("flash write failure");
2575 free(fill_buf);
2576 return;
2577 }
2578
2579 total_blocks++;
2580 }
2581
2582 free(fill_buf);
2583 break;
2584
Ajay Dudani5c761132011-04-07 20:19:04 -07002585 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302586 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2587 fastboot_fail("bogus size for chunk DONT CARE type");
2588 return;
2589 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002590 total_blocks += chunk_header->chunk_sz;
2591 break;
2592
Ajay Dudani5c761132011-04-07 20:19:04 -07002593 case CHUNK_TYPE_CRC:
2594 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2595 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002596 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002597 return;
2598 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302599 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2600 fastboot_fail("bogus size for chunk CRC type");
2601 return;
2602 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002603 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302604 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2605 fastboot_fail("integer overflow occured");
2606 return;
2607 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002608 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302609 if (data_end < (uint32_t)data) {
2610 fastboot_fail("buffer overreads occured due to invalid sparse header");
2611 return;
2612 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002613 break;
2614
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002615 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002616 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002617 fastboot_fail("Unknown chunk type");
2618 return;
2619 }
2620 }
2621
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002622 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2623 total_blocks, sparse_header->total_blks);
2624
2625 if(total_blocks != sparse_header->total_blks)
2626 {
2627 fastboot_fail("sparse image write failure");
2628 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002629
2630 fastboot_okay("");
2631 return;
2632}
2633
2634void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2635{
2636 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002637 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002638
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002639#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002640 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2641 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002642 int ret=0;
2643 uint32 major_version=0;
2644 uint32 minor_version=0;
2645
2646 ret = scm_svc_version(&major_version,&minor_version);
2647 if(!ret)
2648 {
2649 if(major_version >= 2)
2650 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002651 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002652 {
2653 ret = encrypt_scm((uint32 **) &data, &sz);
2654 if (ret != 0) {
2655 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2656 return;
2657 }
2658
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002659 /* Protect only for SSD */
2660 if (!strcmp(arg, "ssd")) {
2661 ret = scm_protect_keystore((uint32 *) data, sz);
2662 if (ret != 0) {
2663 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2664 return;
2665 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002666 }
2667 }
2668 else
2669 {
2670 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2671 if(ret != 0)
2672 {
2673 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2674 return;
2675 }
2676 }
2677 }
2678 else
2679 {
2680 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2681 magic_number[1] == DECRYPT_MAGIC_1)
2682 {
2683 ret = decrypt_scm((uint32 **) &data, &sz);
2684 if (ret != 0) {
2685 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2686 return;
2687 }
2688 }
2689 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2690 magic_number[1] == ENCRYPT_MAGIC_1)
2691 {
2692 ret = encrypt_scm((uint32 **) &data, &sz);
2693 if (ret != 0) {
2694 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2695 return;
2696 }
2697 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002698 }
2699 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002700 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002701 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002702 dprintf(CRITICAL,"INVALID SVC Version\n");
2703 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002704 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002705#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002706
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002707#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002708 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002709 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002710 if(!device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002711 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002712 fastboot_fail("device is locked. Cannot flash images");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002713 return;
2714 }
2715 }
2716#endif
2717
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002718 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002719 meta_header = (meta_header_t *) data;
2720 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002721 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002722 else if (meta_header->magic == META_HEADER_MAGIC)
2723 cmd_flash_meta_img(arg, data, sz);
2724 else
2725 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002726
2727#if VERIFIED_BOOT
2728 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
2729 {
2730 // reset dm_verity mode to enforcing
2731 device.verity_mode = 1;
2732 write_device_info(&device);
2733 }
2734#endif
2735
Ajay Dudani5c761132011-04-07 20:19:04 -07002736 return;
2737}
2738
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002739void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2740{
2741 struct ptentry *sys_ptn;
2742 struct ptable *ptable;
2743
2744 ptable = flash_get_ptable();
2745 if (ptable == NULL) {
2746 fastboot_fail("partition table doesn't exist");
2747 return;
2748 }
2749
2750 sys_ptn = ptable_find(ptable, "system");
2751 if (sys_ptn == NULL) {
2752 fastboot_fail("system partition not found");
2753 return;
2754 }
2755
2756 sz = ROUND_TO_PAGE(sz, page_mask);
2757 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2758 fastboot_fail("update_ubi_vol failed");
2759 else
2760 fastboot_okay("");
2761}
2762
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002763void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002764{
2765 struct ptentry *ptn;
2766 struct ptable *ptable;
2767 unsigned extra = 0;
2768
2769 ptable = flash_get_ptable();
2770 if (ptable == NULL) {
2771 fastboot_fail("partition table doesn't exist");
2772 return;
2773 }
2774
2775 ptn = ptable_find(ptable, arg);
2776 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002777 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2778 arg);
2779 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002780 return;
2781 }
2782
2783 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2784 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2785 fastboot_fail("image is not a boot image");
2786 return;
2787 }
2788 }
2789
Amol Jadi5c61a952012-05-04 17:05:35 -07002790 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002791 || !strcmp(ptn->name, "userdata")
2792 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002793 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002794 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002795 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002796 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002797 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002798
2799 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002800 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2801 if (flash_ubi_img(ptn, data, sz)) {
2802 fastboot_fail("flash write failure");
2803 return;
2804 }
2805 } else {
2806 if (flash_write(ptn, extra, data, sz)) {
2807 fastboot_fail("flash write failure");
2808 return;
2809 }
Dima Zavin214cc642009-01-26 11:16:21 -08002810 }
2811 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2812 fastboot_okay("");
2813}
2814
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002815void cmd_flash(const char *arg, void *data, unsigned sz)
2816{
2817 if(target_is_emmc_boot())
2818 cmd_flash_mmc(arg, data, sz);
2819 else
2820 cmd_flash_nand(arg, data, sz);
2821}
2822
Dima Zavin214cc642009-01-26 11:16:21 -08002823void cmd_continue(const char *arg, void *data, unsigned sz)
2824{
2825 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002826 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002827
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002828 if (target_is_emmc_boot())
2829 {
lijuanga40d6302015-07-20 20:10:13 +08002830#if FBCON_DISPLAY_MSG
2831 keys_detect_init();
2832#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002833 boot_linux_from_mmc();
2834 }
2835 else
2836 {
2837 boot_linux_from_flash();
2838 }
Dima Zavin214cc642009-01-26 11:16:21 -08002839}
2840
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002841void cmd_reboot(const char *arg, void *data, unsigned sz)
2842{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002843 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002844 fastboot_okay("");
2845 reboot_device(0);
2846}
2847
2848void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2849{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002850 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002851 fastboot_okay("");
2852 reboot_device(FASTBOOT_MODE);
2853}
2854
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002855void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2856{
2857 dprintf(INFO, "Enabling charger screen check\n");
2858 device.charger_screen_enabled = 1;
2859 write_device_info(&device);
2860 fastboot_okay("");
2861}
2862
2863void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2864{
2865 dprintf(INFO, "Disabling charger screen check\n");
2866 device.charger_screen_enabled = 0;
2867 write_device_info(&device);
2868 fastboot_okay("");
2869}
2870
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302871void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2872{
2873 dprintf(INFO, "Selecting display panel %s\n", arg);
2874 if (arg)
2875 strlcpy(device.display_panel, arg,
2876 sizeof(device.display_panel));
2877 write_device_info(&device);
2878 fastboot_okay("");
2879}
2880
Shashank Mittal162244e2011-08-08 19:01:25 -07002881void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2882{
vijay kumarc65876c2015-04-24 13:29:16 +05302883 if(!is_allow_unlock) {
2884 fastboot_fail("oem unlock is not allowed");
2885 return;
2886 }
2887
lijuanga40d6302015-07-20 20:10:13 +08002888#if FBCON_DISPLAY_MSG
2889 if(!device.is_unlocked)
2890 display_menu_thread(DISPLAY_THREAD_UNLOCK);
2891 else
2892 fastboot_info("Device already unlocked!");
2893 fastboot_okay("");
2894#else
vijay kumarc65876c2015-04-24 13:29:16 +05302895 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
lijuanga40d6302015-07-20 20:10:13 +08002896#endif
vijay kumarc65876c2015-04-24 13:29:16 +05302897}
2898
2899void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
2900{
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002901 if(!device.is_unlocked)
Shashank Mittal162244e2011-08-08 19:01:25 -07002902 {
vijay kumarc65876c2015-04-24 13:29:16 +05302903 if(!is_allow_unlock) {
2904 fastboot_fail("oem unlock is not allowed");
2905 return;
2906 }
2907
Shashank Mittal162244e2011-08-08 19:01:25 -07002908 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002909 write_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05302910
2911 struct recovery_message msg;
2912 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2913 write_misc(0, &msg, sizeof(msg));
2914
2915 fastboot_okay("");
2916 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002917 }
2918 fastboot_okay("");
2919}
2920
2921void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2922{
Sridhar Parasuram8b792422015-07-05 11:38:13 -07002923 struct recovery_message msg;
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002924 if(device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002925 {
2926 device.is_unlocked = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002927 write_device_info(&device);
Sridhar Parasuram8b792422015-07-05 11:38:13 -07002928 // upon oem lock, reboot to recovery to wipe user data
2929 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2930 write_misc(0, &msg, sizeof(msg));
2931 fastboot_okay("");
2932 reboot_device(RECOVERY_MODE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002933 }
2934 fastboot_okay("");
2935}
2936
Shashank Mittala0032282011-08-26 14:50:11 -07002937void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2938{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302939 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002940 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002941 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002942 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2943 fastboot_info(response);
2944 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002945 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302946 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2947 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002948 fastboot_okay("");
2949}
2950
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002951void cmd_preflash(const char *arg, void *data, unsigned sz)
2952{
2953 fastboot_okay("");
2954}
2955
Mao Flynn7b379f32015-04-20 00:28:30 +08002956static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302957
Mao Flynn7b379f32015-04-20 00:28:30 +08002958int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302959{
Mao Flynn7b379f32015-04-20 00:28:30 +08002960 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302961 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08002962 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302963 return -1;
2964 return 0;
2965}
2966
Mao Flynn7b379f32015-04-20 00:28:30 +08002967int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002968{
2969 struct ptentry *ptn;
2970 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08002971 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002972 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002973
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302974 ptable = flash_get_ptable();
2975 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002976 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2977 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302978 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002979
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302980 ptn = ptable_find(ptable, "splash");
2981 if (ptn == NULL) {
2982 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002983 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302984 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002985 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302986 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002987 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302988 }
2989
Mao Flynn7b379f32015-04-20 00:28:30 +08002990 header = (struct logo_img_header *)logo_header;
2991 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302992 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002993 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302994 }
2995
2996 fb_display = fbcon_display();
2997 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002998 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
2999 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3000
3001 /* if the logo is full-screen size, remove "fbcon_clear()" */
3002 if ((header->width != fb_display->width)
3003 || (header->height != fb_display->height))
3004 fbcon_clear();
3005
3006 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3007 (uint32_t *)base,
3008 (header->blocks * 512))) {
3009 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3010 return -1;
3011 }
3012 fbcon_extract_to_screen(header, base);
3013 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003014 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003015
3016 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3017 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3018 return -1;
3019 }
3020
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303021 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003022 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3023 (uint32_t *)base,
3024 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303025 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303026 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003027 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003028 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303029 }
3030
Mao Flynn7b379f32015-04-20 00:28:30 +08003031 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303032}
3033
Mao Flynn7b379f32015-04-20 00:28:30 +08003034int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303035{
3036 int index = INVALID_PTN;
3037 unsigned long long ptn = 0;
3038 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003039 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003040 uint32_t blocksize, realsize, readsize;
3041 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303042
3043 index = partition_get_index("splash");
3044 if (index == 0) {
3045 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003046 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303047 }
3048
3049 ptn = partition_get_offset(index);
3050 if (ptn == 0) {
3051 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003052 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303053 }
3054
Mao Flynn1893a7f2015-06-03 12:03:36 +08003055 mmc_set_lun(partition_get_lun(index));
3056
3057 blocksize = mmc_get_device_blocksize();
3058 if (blocksize == 0) {
3059 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3060 return -1;
3061 }
3062
3063 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003064 if (!fb_display)
3065 {
3066 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3067 return -1;
3068 }
3069
Mao Flynn1893a7f2015-06-03 12:03:36 +08003070 base = (uint8_t *) fb_display->base;
3071
3072 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303073 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003074 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303075 }
3076
Mao Flynn1893a7f2015-06-03 12:03:36 +08003077 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003078 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303079 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003080 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303081 }
3082
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303083 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003084 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3085 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003086
Mao Flynn1893a7f2015-06-03 12:03:36 +08003087 realsize = header->blocks * 512;
3088 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3089
3090 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003091 if ((header->width != fb_display->width)
3092 || (header->height != fb_display->height))
3093 fbcon_clear();
3094
Mao Flynn1893a7f2015-06-03 12:03:36 +08003095 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003096 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3097 return -1;
3098 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003099
Mao Flynn1893a7f2015-06-03 12:03:36 +08003100 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3101 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303102
Mao Flynn1893a7f2015-06-03 12:03:36 +08003103 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3104 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3105 return -1;
3106 }
3107
3108 realsize = header->width * header->height * fb_display->bpp / 8;
3109 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3110
3111 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3112 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3113 fbcon_clear();
3114 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3115 return -1;
3116 }
3117 } else {
3118 if (mmc_read(ptn + blocksize ,
3119 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3120 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3121 return -1;
3122 }
3123 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3124 }
3125 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303126 }
3127
Mao Flynn7b379f32015-04-20 00:28:30 +08003128 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303129}
3130
Mao Flynn7b379f32015-04-20 00:28:30 +08003131int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303132{
3133 if (target_is_emmc_boot()) {
3134 return splash_screen_mmc();
3135 } else {
3136 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003137 }
3138}
3139
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003140/* Get the size from partiton name */
3141static void get_partition_size(const char *arg, char *response)
3142{
3143 uint64_t ptn = 0;
3144 uint64_t size;
3145 int index = INVALID_PTN;
3146
3147 index = partition_get_index(arg);
3148
3149 if (index == INVALID_PTN)
3150 {
3151 dprintf(CRITICAL, "Invalid partition index\n");
3152 return;
3153 }
3154
3155 ptn = partition_get_offset(index);
3156
3157 if(!ptn)
3158 {
3159 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3160 return;
3161 }
3162
3163 size = partition_get_size(index);
3164
3165 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3166 return;
3167}
3168
3169/*
3170 * Publish the partition type & size info
3171 * fastboot getvar will publish the required information.
3172 * fastboot getvar partition_size:<partition_name>: partition size in hex
3173 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3174 */
3175static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3176{
3177 uint8_t i;
3178
3179 for (i = 0; i < num_parts; i++) {
3180 get_partition_size(info[i].part_name, info[i].size_response);
3181
3182 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3183 {
3184 dprintf(CRITICAL, "partition size name truncated\n");
3185 return;
3186 }
3187 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3188 {
3189 dprintf(CRITICAL, "partition type name truncated\n");
3190 return;
3191 }
3192
3193 /* publish partition size & type info */
3194 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3195 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3196 }
3197}
3198
Amol Jadi5edf3552013-07-23 14:15:34 -07003199/* register commands and variables for fastboot */
3200void aboot_fastboot_register_commands(void)
3201{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003202 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07003203
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003204 struct fastboot_cmd_desc cmd_list[] = {
3205 /* By default the enabled list is empty. */
3206 {"", NULL},
3207 /* move commands enclosed within the below ifndef to here
3208 * if they need to be enabled in user build.
3209 */
3210#ifndef DISABLE_FASTBOOT_CMDS
3211 /* Register the following commands only for non-user builds */
3212 {"flash:", cmd_flash},
3213 {"erase:", cmd_erase},
3214 {"boot", cmd_boot},
3215 {"continue", cmd_continue},
3216 {"reboot", cmd_reboot},
3217 {"reboot-bootloader", cmd_reboot_bootloader},
3218 {"oem unlock", cmd_oem_unlock},
vijay kumarc65876c2015-04-24 13:29:16 +05303219 {"oem unlock-go", cmd_oem_unlock_go},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003220 {"oem lock", cmd_oem_lock},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003221 {"oem device-info", cmd_oem_devinfo},
3222 {"preflash", cmd_preflash},
3223 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3224 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08003225 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003226#endif
3227 };
3228
3229 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3230 for (i = 1; i < fastboot_cmds_count; i++)
3231 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3232
Amol Jadi5edf3552013-07-23 14:15:34 -07003233 /* publish variables and their values */
3234 fastboot_publish("product", TARGET(BOARD));
3235 fastboot_publish("kernel", "lk");
3236 fastboot_publish("serialno", sn_buf);
3237
3238 /*
3239 * partition info is supported only for emmc partitions
3240 * Calling this for NAND prints some error messages which
3241 * is harmless but misleading. Avoid calling this for NAND
3242 * devices.
3243 */
3244 if (target_is_emmc_boot())
3245 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3246
3247 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003248 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3249 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003250 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003251 /* Is the charger screen check enabled */
3252 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3253 device.charger_screen_enabled);
3254 fastboot_publish("charger-screen-enabled",
3255 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303256 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3257 device.display_panel);
3258 fastboot_publish("display-panel",
3259 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003260 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3261 fastboot_publish("version-baseband", (const char *) device.radio_version);
Amol Jadi5edf3552013-07-23 14:15:34 -07003262}
3263
Brian Swetland9c4c0752009-01-25 16:23:50 -08003264void aboot_init(const struct app_descriptor *app)
3265{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003266 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03003267 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003268
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003269 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003270 if (target_is_emmc_boot())
3271 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003272 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003273 page_mask = page_size - 1;
3274 }
3275 else
3276 {
3277 page_size = flash_page_size();
3278 page_mask = page_size - 1;
3279 }
3280
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003281 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3282
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003283 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303284 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003285
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003286 /* Display splash screen if enabled */
3287#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003288#if NO_ALARM_DISPLAY
3289 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003290#endif
lijuang99c02d82015-02-13 19:04:34 +08003291 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003292#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003293 /* Wait if the display shutdown is in progress */
3294 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003295 if (!pm_appsbl_display_init_done())
3296 target_display_init(device.display_panel);
3297 else
3298 display_image_on_screen();
3299#else
lijuang99c02d82015-02-13 19:04:34 +08003300 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003301#endif
lijuang99c02d82015-02-13 19:04:34 +08003302 dprintf(SPEW, "Display Init: Done\n");
3303#if NO_ALARM_DISPLAY
3304 }
3305#endif
3306#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003307
Greg Griscod6250552011-06-29 14:40:23 -07003308 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003309 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003310
Dhaval Patel223ec952013-07-18 14:49:44 -07003311 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3312
Matthew Qindefd5562014-07-11 18:02:40 +08003313 /*
3314 * Check power off reason if user force reset,
3315 * if yes phone will do normal boot.
3316 */
3317 if (is_user_force_reset())
3318 goto normal_boot;
3319
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003320 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003321 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003322 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003323 dprintf(ALWAYS,"dload mode key sequence detected\n");
3324 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003325 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003326 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003327 }
3328 else
3329 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303330 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003331 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003332 }
3333 boot_into_fastboot = true;
3334 }
3335 if (!boot_into_fastboot)
3336 {
3337 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3338 boot_into_recovery = 1;
3339 if (!boot_into_recovery &&
3340 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003341 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003342 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003343 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003344 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003345 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003346 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003347
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003348#if USE_PON_REBOOT_REG
3349 reboot_mode = check_hard_reboot_mode();
3350#else
Ajay Dudani77421292010-10-27 19:34:06 -07003351 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003352#endif
3353 if (reboot_mode == RECOVERY_MODE)
3354 {
Ajay Dudani77421292010-10-27 19:34:06 -07003355 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003356 }
3357 else if(reboot_mode == FASTBOOT_MODE)
3358 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003359 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003360 }
3361 else if(reboot_mode == ALARM_BOOT)
3362 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003363 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003364 }
3365 else if (reboot_mode == DM_VERITY_ENFORCING)
3366 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003367 device.verity_mode = 1;
3368 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003369 }
3370 else if (reboot_mode == DM_VERITY_LOGGING)
3371 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003372 device.verity_mode = 0;
3373 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003374 }
3375 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3376 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003377 if(send_delete_keys_to_tz())
3378 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003379 }
3380
Matthew Qindefd5562014-07-11 18:02:40 +08003381normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003382 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003383 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003384 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003385 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003386 if(emmc_recovery_init())
3387 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3388 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003389 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003390 if((device.is_unlocked) || (device.is_tampered))
3391 {
3392 #ifdef TZ_TAMPER_FUSE
3393 set_tamper_fuse_cmd();
3394 #endif
3395 #if USE_PCOM_SECBOOT
3396 set_tamper_flag(device.is_tampered);
3397 #endif
3398 }
Shashank Mittala0032282011-08-26 14:50:11 -07003399 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003400
Pavel Nedev5d91d412013-04-29 11:34:24 +03003401 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003402 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003403 else
3404 {
3405 recovery_init();
3406 #if USE_PCOM_SECBOOT
3407 if((device.is_unlocked) || (device.is_tampered))
3408 set_tamper_flag(device.is_tampered);
3409 #endif
3410 boot_linux_from_flash();
3411 }
3412 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3413 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003414 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003415
Amol Jadi5edf3552013-07-23 14:15:34 -07003416 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003417
Amol Jadi5edf3552013-07-23 14:15:34 -07003418 /* register aboot specific fastboot commands */
3419 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003420
Amol Jadi5edf3552013-07-23 14:15:34 -07003421 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003422 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003423
3424 /* initialize and start fastboot */
3425 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08003426}
3427
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003428uint32_t get_page_size()
3429{
3430 return page_size;
3431}
3432
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003433/*
3434 * Calculated and save hash (SHA256) for non-signed boot image.
3435 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003436 * @param image_addr - Boot image address
3437 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003438 *
3439 * @return int - 0 on success, negative value on failure.
3440 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003441static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003442{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003443 unsigned int digest[8];
3444#if IMAGE_VERIF_ALGO_SHA1
3445 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3446#else
3447 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3448#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003449
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003450 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003451 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003452
3453 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003454 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003455
3456 return 0;
3457}
3458
Brian Swetland9c4c0752009-01-25 16:23:50 -08003459APP_START(aboot)
3460 .init = aboot_init,
3461APP_END