blob: d049acb2a93aece2e1c7300b852de7b92170f1a4 [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
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800131#define MAX_PANEL_BUF_SIZE 128
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
Matthew Qin49e51fa2015-02-09 10:40:45 +08001072 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
1073 bs_set_timestamp(BS_KERNEL_LOAD_START);
1074
Matthew Qinbb7923d2015-02-09 10:56:09 +08001075 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001076 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1077 {
1078 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1079 return -1;
1080 }
1081
1082 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
1083 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1084
1085 /* Authenticate Kernel */
1086 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1087 (int) target_use_signed_kernel(),
1088 device.is_unlocked,
1089 device.is_tampered);
1090
1091 if(target_use_signed_kernel() && (!device.is_unlocked))
1092 {
1093 offset = imagesize_actual;
1094 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1095 {
1096 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1097 return -1;
1098 }
1099
1100 /* Read signature */
1101 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1102 {
1103 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1104 return -1;
1105 }
1106
1107 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
1108 } else {
1109 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1110 #ifdef TZ_SAVE_KERNEL_HASH
1111 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1112 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001113
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001114#if VERIFIED_BOOT
1115 if(boot_verify_get_state() == ORANGE)
1116 {
lijuanga40d6302015-07-20 20:10:13 +08001117#if FBCON_DISPLAY_MSG
1118 display_menu_thread(DISPLAY_THREAD_BOOT_STATE);
1119 wait_for_users_action();
1120#else
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001121 dprintf(CRITICAL,
lijuanga40d6302015-07-20 20:10:13 +08001122 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001123 mdelay(5000);
lijuanga40d6302015-07-20 20:10:13 +08001124#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001125 }
1126#endif
1127
Amit Blay4aa292f2015-04-28 21:55:59 +03001128#ifdef MDTP_SUPPORT
1129 {
1130 /* Verify MDTP lock.
1131 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1132 * since verification was skipped in aboot. The signature is not part of the loaded image.
1133 */
1134 mdtp_ext_partition_verification_t ext_partition;
1135 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1136 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1137 ext_partition.page_size = page_size;
1138 ext_partition.image_addr = (uint32)image_addr;
1139 ext_partition.image_size = imagesize_actual;
1140 ext_partition.sig_avail = FALSE;
1141 mdtp_fwlock_verify_lock(&ext_partition);
1142 }
1143#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001144 }
1145
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001146#if VERIFIED_BOOT
1147 // send root of trust
1148 if(!send_rot_command())
1149 ASSERT(0);
1150#endif
1151
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001152 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001153 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1154 * If not, continue booting.
1155 */
1156 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1157 {
1158 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1159 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001160 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001161 rc = decompress((unsigned char *)(image_addr + page_size),
1162 hdr->kernel_size, out_addr, out_avai_len,
1163 &dtb_offset, &out_len);
1164 if (rc)
1165 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001166 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001167 ASSERT(0);
1168 }
1169
Matthew Qin0b15b322015-05-19 05:20:54 -04001170 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001171 kptr = (struct kernel64_hdr *)out_addr;
1172 kernel_start_addr = out_addr;
1173 kernel_size = out_len;
1174 } else {
1175 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1176 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1177 kernel_size = hdr->kernel_size;
1178 }
1179
1180 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001181 * Update the kernel/ramdisk/tags address if the boot image header
1182 * has default values, these default values come from mkbootimg when
1183 * the boot image is flashed using fastboot flash:raw
1184 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001185 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001186
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001187 /* Get virtual addresses since the hdr saves physical addresses. */
1188 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1189 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1190 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001191
Matthew Qinbb7923d2015-02-09 10:56:09 +08001192 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001193 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001194 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001195 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1196 {
1197 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1198 return -1;
1199 }
1200
1201#ifndef DEVICE_TREE
1202 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1203 {
1204 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1205 return -1;
1206 }
1207#endif
1208
Matthew Qin49e51fa2015-02-09 10:40:45 +08001209 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001210 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001211 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001212
Matthew Qin49e51fa2015-02-09 10:40:45 +08001213 #if DEVICE_TREE
1214 if(hdr->dt_size) {
1215 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1216 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001217
Matthew Qin49e51fa2015-02-09 10:40:45 +08001218 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1219 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1220 return -1;
1221 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001222
Matthew Qin49e51fa2015-02-09 10:40:45 +08001223 /* Find index of device tree within device tree table */
1224 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1225 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1226 return -1;
1227 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001228
Matthew Qin271927e2015-03-31 22:07:07 -04001229 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1230 {
1231 unsigned int compressed_size = 0;
1232 out_addr += out_len;
1233 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001234 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001235 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1236 dt_entry.size, out_addr, out_avai_len,
1237 &compressed_size, &dtb_size);
1238 if (rc)
1239 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001240 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001241 ASSERT(0);
1242 }
1243
Matthew Qin0b15b322015-05-19 05:20:54 -04001244 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001245 best_match_dt_addr = out_addr;
1246 } else {
1247 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1248 dtb_size = dt_entry.size;
1249 }
1250
Matthew Qin49e51fa2015-02-09 10:40:45 +08001251 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001252 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001253 {
1254 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1255 return -1;
1256 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001257
Matthew Qin271927e2015-03-31 22:07:07 -04001258 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001259 } else {
1260 /* Validate the tags_addr */
1261 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001262 {
1263 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1264 return -1;
1265 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001266 /*
1267 * If appended dev tree is found, update the atags with
1268 * memory address to the DTB appended location on RAM.
1269 * Else update with the atags address in the kernel header
1270 */
1271 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001272 dtb = dev_tree_appended((void*)(image_addr + page_size),
1273 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001274 (void *)hdr->tags_addr);
1275 if (!dtb) {
1276 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001277 return -1;
1278 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001279 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001280 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001281
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001282 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1283 target_load_ssd_keystore();
1284
Shashank Mittal23b8f422010-04-16 19:27:21 -07001285unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001286
Dima Zavin77e41f32013-03-06 16:10:43 -08001287 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001288 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001289 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1290
1291 return 0;
1292}
1293
Dima Zavin214cc642009-01-26 11:16:21 -08001294int boot_linux_from_flash(void)
1295{
1296 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001297 struct ptentry *ptn;
1298 struct ptable *ptable;
1299 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001300
Shashank Mittalcd98d472011-08-02 14:29:24 -07001301 unsigned char *image_addr = 0;
1302 unsigned kernel_actual;
1303 unsigned ramdisk_actual;
1304 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001305 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001306
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001307#if DEVICE_TREE
1308 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001309 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001310 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001311 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001312#endif
1313
David Ng183a7422009-12-07 14:55:21 -08001314 if (target_is_emmc_boot()) {
1315 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1316 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1317 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1318 return -1;
1319 }
1320 goto continue_boot;
1321 }
1322
Dima Zavin214cc642009-01-26 11:16:21 -08001323 ptable = flash_get_ptable();
1324 if (ptable == NULL) {
1325 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1326 return -1;
1327 }
1328
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001329 if(!boot_into_recovery)
1330 {
1331 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001332
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001333 if (ptn == NULL) {
1334 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1335 return -1;
1336 }
1337 }
1338 else
1339 {
1340 ptn = ptable_find(ptable, "recovery");
1341 if (ptn == NULL) {
1342 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1343 return -1;
1344 }
Dima Zavin214cc642009-01-26 11:16:21 -08001345 }
1346
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001347 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001348 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1349 return -1;
1350 }
Dima Zavin214cc642009-01-26 11:16:21 -08001351
1352 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001353 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001354 return -1;
1355 }
1356
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001357 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001358 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 -08001359 return -1;
1360 }
1361
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001362 /*
1363 * Update the kernel/ramdisk/tags address if the boot image header
1364 * has default values, these default values come from mkbootimg when
1365 * the boot image is flashed using fastboot flash:raw
1366 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001367 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001368
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001369 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001370 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1371 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1372 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1373
1374 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1375 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1376
1377 /* Check if the addresses in the header are valid. */
1378 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1379 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1380 {
1381 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1382 return -1;
1383 }
1384
1385#ifndef DEVICE_TREE
1386 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1387 {
1388 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1389 return -1;
1390 }
1391#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001392
Shashank Mittalcd98d472011-08-02 14:29:24 -07001393 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001394 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001395 {
1396 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001397 offset = 0;
1398
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001399#if DEVICE_TREE
1400 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1401 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001402
1403 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1404 {
1405 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1406 return -1;
1407 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001408#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001409 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001410#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001411
Amol Jadib6be5c12012-11-14 13:39:51 -08001412 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001413 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001414
Shashank Mittalcd98d472011-08-02 14:29:24 -07001415 /* Read image without signature */
1416 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1417 {
1418 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1419 return -1;
1420 }
Dima Zavin214cc642009-01-26 11:16:21 -08001421
Amol Jadib6be5c12012-11-14 13:39:51 -08001422 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001423 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001424
Shashank Mittalcd98d472011-08-02 14:29:24 -07001425 offset = imagesize_actual;
1426 /* Read signature */
1427 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1428 {
1429 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001430 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001431 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001432
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301433 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001434
1435 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001436 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1437 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001438#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001439 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001440 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001441 {
1442 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1443 return -1;
1444 }
1445
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001446 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1447#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001448
1449 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001450 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001451 {
1452 write_device_info_flash(&device);
1453 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301454#if USE_PCOM_SECBOOT
1455 set_tamper_flag(device.is_tampered);
1456#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001457 }
1458 else
1459 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001460 offset = page_size;
1461
Amol Jadib6be5c12012-11-14 13:39:51 -08001462 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1463 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1464 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1465
1466 dprintf(INFO, "Loading boot image (%d): start\n",
1467 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001468 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001469
1470 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001471 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1472 return -1;
1473 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001474 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001475
Amol Jadib6be5c12012-11-14 13:39:51 -08001476 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001477 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1478 return -1;
1479 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001480 offset += ramdisk_actual;
1481
1482 dprintf(INFO, "Loading boot image (%d): done\n",
1483 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001484 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001485
1486 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001487 offset += second_actual;
1488 /* Second image loading not implemented. */
1489 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001490 }
1491
1492#if DEVICE_TREE
1493 if(hdr->dt_size != 0) {
1494
1495 /* Read the device tree table into buffer */
1496 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1497 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1498 return -1;
1499 }
1500
1501 table = (struct dt_table*) dt_buf;
1502
Deepa Dinamani19648b42013-09-05 17:05:55 -07001503 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001504 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1505 return -1;
1506 }
1507
Deepa Dinamani19648b42013-09-05 17:05:55 -07001508 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1509 if (!table)
1510 return -1;
1511
1512 /* Read the entire device tree table into buffer */
1513 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1514 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1515 return -1;
1516 }
1517
1518
Joel Kingaa335dc2013-06-03 16:11:08 -07001519 /* Find index of device tree within device tree table */
1520 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001521 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1522 return -1;
1523 }
1524
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001525 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001526 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001527 {
1528 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1529 return -1;
1530 }
1531
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001532 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001533 if(flash_read(ptn, offset + dt_entry.offset,
1534 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001535 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1536 return -1;
1537 }
1538 }
1539#endif
1540
Shashank Mittalcd98d472011-08-02 14:29:24 -07001541 }
David Ng183a7422009-12-07 14:55:21 -08001542continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001543
Dima Zavin214cc642009-01-26 11:16:21 -08001544 /* TODO: create/pass atags to kernel */
1545
Ajay Dudanie28a6072011-07-01 13:59:46 -07001546 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001547 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001548 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1549
1550 return 0;
1551}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001552
Shashank Mittal162244e2011-08-08 19:01:25 -07001553void write_device_info_mmc(device_info *dev)
1554{
Shashank Mittal162244e2011-08-08 19:01:25 -07001555 unsigned long long ptn = 0;
1556 unsigned long long size;
1557 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001558 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001559 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001560 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001561
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001562 if (devinfo_present)
1563 index = partition_get_index("devinfo");
1564 else
1565 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001566
Shashank Mittal162244e2011-08-08 19:01:25 -07001567 ptn = partition_get_offset(index);
1568 if(ptn == 0)
1569 {
1570 return;
1571 }
1572
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001573 lun = partition_get_lun(index);
1574 mmc_set_lun(lun);
1575
Shashank Mittal162244e2011-08-08 19:01:25 -07001576 size = partition_get_size(index);
1577
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001578 blocksize = mmc_get_device_blocksize();
1579
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001580 if (devinfo_present)
1581 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1582 else
1583 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1584 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001585 {
1586 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001587 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001588 }
1589}
1590
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001591void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001592{
Shashank Mittal162244e2011-08-08 19:01:25 -07001593 unsigned long long ptn = 0;
1594 unsigned long long size;
1595 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001596 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001597 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001598
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001599 if ((index = partition_get_index("devinfo")) < 0)
1600 {
1601 devinfo_present = false;
1602 index = partition_get_index("aboot");
1603 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001604
Shashank Mittal162244e2011-08-08 19:01:25 -07001605 ptn = partition_get_offset(index);
1606 if(ptn == 0)
1607 {
1608 return;
1609 }
1610
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001611 mmc_set_lun(partition_get_lun(index));
1612
Shashank Mittal162244e2011-08-08 19:01:25 -07001613 size = partition_get_size(index);
1614
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001615 blocksize = mmc_get_device_blocksize();
1616
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001617 if (devinfo_present)
1618 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1619 else
1620 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1621 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001622 {
1623 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001624 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001625 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001626}
1627
1628void write_device_info_flash(device_info *dev)
1629{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001630 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001631 struct ptentry *ptn;
1632 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001633 if(info == NULL)
1634 {
1635 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1636 ASSERT(0);
1637 }
1638 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001639 ptable = flash_get_ptable();
1640 if (ptable == NULL)
1641 {
1642 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1643 return;
1644 }
1645
1646 ptn = ptable_find(ptable, "devinfo");
1647 if (ptn == NULL)
1648 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001649 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001650 return;
1651 }
1652
1653 memcpy(info, dev, sizeof(device_info));
1654
1655 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1656 {
1657 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1658 return;
1659 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001660 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001661}
1662
vijay kumarc65876c2015-04-24 13:29:16 +05301663static int read_allow_oem_unlock(device_info *dev)
1664{
vijay kumarc65876c2015-04-24 13:29:16 +05301665 unsigned offset;
1666 int index;
1667 unsigned long long ptn;
1668 unsigned long long ptn_size;
1669 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001670 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301671
vijay kumarca2e6812015-07-08 20:28:25 +05301672 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301673 if (index == INVALID_PTN)
1674 {
vijay kumarca2e6812015-07-08 20:28:25 +05301675 index = partition_get_index(frp_ptns[1]);
1676 if (index == INVALID_PTN)
1677 {
1678 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1679 return -1;
1680 }
vijay kumarc65876c2015-04-24 13:29:16 +05301681 }
1682
1683 ptn = partition_get_offset(index);
1684 ptn_size = partition_get_size(index);
1685 offset = ptn_size - blocksize;
1686
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001687 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301688 {
1689 dprintf(CRITICAL, "Reading MMC failed\n");
1690 return -1;
1691 }
1692
1693 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1694 is_allow_unlock = buf[blocksize-1] & 0x01;
1695 return 0;
1696}
1697
1698static int write_allow_oem_unlock(bool allow_unlock)
1699{
vijay kumarc65876c2015-04-24 13:29:16 +05301700 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301701 int index;
1702 unsigned long long ptn;
1703 unsigned long long ptn_size;
1704 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001705 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301706
vijay kumarca2e6812015-07-08 20:28:25 +05301707 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301708 if (index == INVALID_PTN)
1709 {
vijay kumarca2e6812015-07-08 20:28:25 +05301710 index = partition_get_index(frp_ptns[1]);
1711 if (index == INVALID_PTN)
1712 {
1713 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1714 return -1;
1715 }
vijay kumarc65876c2015-04-24 13:29:16 +05301716 }
1717
1718 ptn = partition_get_offset(index);
1719 ptn_size = partition_get_size(index);
1720 offset = ptn_size - blocksize;
1721
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001722 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301723 {
1724 dprintf(CRITICAL, "Reading MMC failed\n");
1725 return -1;
1726 }
1727
1728 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1729 buf[blocksize-1] = allow_unlock;
1730 if (mmc_write(ptn + offset, blocksize, buf))
1731 {
1732 dprintf(CRITICAL, "Writing MMC failed\n");
1733 return -1;
1734 }
1735
1736 return 0;
1737}
1738
Shashank Mittal162244e2011-08-08 19:01:25 -07001739void read_device_info_flash(device_info *dev)
1740{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001741 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001742 struct ptentry *ptn;
1743 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001744 if(info == NULL)
1745 {
1746 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1747 ASSERT(0);
1748 }
1749 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001750 ptable = flash_get_ptable();
1751 if (ptable == NULL)
1752 {
1753 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1754 return;
1755 }
1756
1757 ptn = ptable_find(ptable, "devinfo");
1758 if (ptn == NULL)
1759 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001760 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001761 return;
1762 }
1763
1764 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1765 {
1766 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1767 return;
1768 }
1769
1770 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1771 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001772 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1773 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001774 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001775 write_device_info_flash(info);
1776 }
1777 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001778 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001779}
1780
1781void write_device_info(device_info *dev)
1782{
1783 if(target_is_emmc_boot())
1784 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001785 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1786 if(info == NULL)
1787 {
1788 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1789 ASSERT(0);
1790 }
1791 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001792 memcpy(info, dev, sizeof(struct device_info));
1793
1794#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001795 if (is_secure_boot_enable()) {
1796 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1797 ASSERT(0);
1798 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001799 else
1800 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001801#else
1802 write_device_info_mmc(info);
1803#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001804 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001805 }
1806 else
1807 {
1808 write_device_info_flash(dev);
1809 }
1810}
1811
1812void read_device_info(device_info *dev)
1813{
1814 if(target_is_emmc_boot())
1815 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001816 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1817 if(info == NULL)
1818 {
1819 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1820 ASSERT(0);
1821 }
1822 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001823
1824#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001825 if (is_secure_boot_enable()) {
1826 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1827 ASSERT(0);
1828 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001829 else
1830 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001831#else
1832 read_device_info_mmc(info);
1833#endif
1834
1835 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1836 {
1837 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001838 if (is_secure_boot_enable())
1839 info->is_unlocked = 0;
1840 else
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001841 info->is_unlocked = 1;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001842 info->is_tampered = 0;
1843 info->charger_screen_enabled = 0;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001844 write_device_info(info);
1845 }
1846 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001847 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001848 }
1849 else
1850 {
1851 read_device_info_flash(dev);
1852 }
1853}
1854
1855void reset_device_info()
1856{
1857 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001858 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001859 write_device_info(&device);
1860}
1861
1862void set_device_root()
1863{
1864 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001865 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001866 write_device_info(&device);
1867}
1868
Amol Jadicb524072012-08-09 16:40:18 -07001869#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001870int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
Amol Jadicb524072012-08-09 16:40:18 -07001871{
1872 uint32 dt_image_offset = 0;
1873 uint32_t n;
1874 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001875 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001876 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001877 unsigned int compressed_size = 0;
1878 unsigned int dtb_size = 0;
1879 unsigned int out_avai_len = 0;
1880 unsigned char *out_addr = NULL;
1881 unsigned char *best_match_dt_addr = NULL;
1882 int rc;
Amol Jadicb524072012-08-09 16:40:18 -07001883
1884 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1885
Amol Jadicb524072012-08-09 16:40:18 -07001886 if(hdr->dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001887 /* add kernel offset */
1888 dt_image_offset += page_size;
1889 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1890 dt_image_offset += n;
1891
1892 /* add ramdisk offset */
1893 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1894 dt_image_offset += n;
1895
1896 /* add second offset */
1897 if(hdr->second_size != 0) {
1898 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1899 dt_image_offset += n;
1900 }
1901
1902 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001903 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001904
Deepa Dinamani19648b42013-09-05 17:05:55 -07001905 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001906 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1907 return -1;
1908 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001909 /* Find index of device tree within device tree table */
1910 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001911 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1912 return -1;
1913 }
1914
Matthew Qin271927e2015-03-31 22:07:07 -04001915 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
1916 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
1917 {
1918 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
1919 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04001920 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001921 rc = decompress(best_match_dt_addr,
1922 dt_entry.size, out_addr, out_avai_len,
1923 &compressed_size, &dtb_size);
1924 if (rc)
1925 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001926 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001927 ASSERT(0);
1928 }
1929
Matthew Qin0b15b322015-05-19 05:20:54 -04001930 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001931 best_match_dt_addr = out_addr;
1932 } else {
1933 dtb_size = dt_entry.size;
1934 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001935 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001936 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001937 {
1938 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1939 return -1;
1940 }
1941
Amol Jadicb524072012-08-09 16:40:18 -07001942 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001943 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001944 } else
1945 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001946
1947 /* Everything looks fine. Return success. */
1948 return 0;
1949}
1950#endif
1951
Brian Swetland9c4c0752009-01-25 16:23:50 -08001952void cmd_boot(const char *arg, void *data, unsigned sz)
1953{
Amit Blay8a510302015-08-17 09:20:01 +03001954#ifdef MDTP_SUPPORT
1955 static bool is_mdtp_activated = 0;
1956#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08001957 unsigned kernel_actual;
1958 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001959 uint32_t image_actual;
1960 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001961 uint32_t sig_actual = SIGNATURE_SIZE;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001962 struct boot_img_hdr *hdr = NULL;
1963 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001964 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001965 int ret = 0;
1966 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001967 unsigned int out_len = 0;
1968 unsigned int out_avai_len = 0;
1969 unsigned char *out_addr = NULL;
1970 uint32_t dtb_offset = 0;
1971 unsigned char *kernel_start_addr = NULL;
1972 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04001973 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001974
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001975#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07001976 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001977 {
1978 fastboot_fail("unlock device to use this command");
1979 return;
1980 }
1981#endif
1982
Brian Swetland9c4c0752009-01-25 16:23:50 -08001983 if (sz < sizeof(hdr)) {
1984 fastboot_fail("invalid bootimage header");
1985 return;
1986 }
1987
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001988 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001989
1990 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001991 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001992
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001993 if(target_is_emmc_boot() && hdr->page_size) {
1994 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001995 page_mask = page_size - 1;
1996 }
1997
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001998 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1999 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002000#if DEVICE_TREE
2001 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2002#endif
2003
2004 image_actual = ADD_OF(page_size, kernel_actual);
2005 image_actual = ADD_OF(image_actual, ramdisk_actual);
2006 image_actual = ADD_OF(image_actual, dt_actual);
2007
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002008 if (target_use_signed_kernel() && (!device.is_unlocked))
2009 image_actual = ADD_OF(image_actual, sig_actual);
2010
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002011 /* sz should have atleast raw boot image */
2012 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002013 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002014 return;
2015 }
2016
2017 /* Verify the boot image
2018 * device & page_size are initialized in aboot_init
2019 */
2020 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002021 /* Pass size excluding signature size, otherwise we would try to
2022 * access signature beyond its length
2023 */
2024 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002025
Amit Blay4aa292f2015-04-28 21:55:59 +03002026#ifdef MDTP_SUPPORT
2027 else
2028 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002029 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002030 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002031
2032 if (!is_mdtp_activated) {
2033 ext_partition.partition = MDTP_PARTITION_NONE;
2034 mdtp_fwlock_verify_lock(&ext_partition);
2035 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002036 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002037
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002038 mdtp_activated(&is_mdtp_activated);
2039 if(is_mdtp_activated){
2040 dprintf(CRITICAL, "fastboot boot command is not available.\n");
2041 return;
2042 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002043#endif /* MDTP_SUPPORT */
2044
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002045 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002046 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2047 * If not, continue booting.
2048 */
2049 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2050 {
2051 out_addr = (unsigned char *)target_get_scratch_address();
2052 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2053 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002054 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002055 ret = decompress((unsigned char *)(ptr + page_size),
2056 hdr->kernel_size, out_addr, out_avai_len,
2057 &dtb_offset, &out_len);
2058 if (ret)
2059 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002060 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002061 ASSERT(0);
2062 }
2063
Matthew Qin0b15b322015-05-19 05:20:54 -04002064 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002065 kptr = (struct kernel64_hdr *)out_addr;
2066 kernel_start_addr = out_addr;
2067 kernel_size = out_len;
2068 } else {
2069 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2070 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2071 kernel_size = hdr->kernel_size;
2072 }
2073
2074 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002075 * Update the kernel/ramdisk/tags address if the boot image header
2076 * has default values, these default values come from mkbootimg when
2077 * the boot image is flashed using fastboot flash:raw
2078 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002079 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002080
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002081 /* Get virtual addresses since the hdr saves physical addresses. */
2082 hdr->kernel_addr = VA(hdr->kernel_addr);
2083 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2084 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002085
Matthew Qinbb7923d2015-02-09 10:56:09 +08002086 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002087 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002088 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002089 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2090 {
2091 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002092 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002093 }
2094
Amol Jadicb524072012-08-09 16:40:18 -07002095#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002096 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002097 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002098 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002099
2100 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002101#else
2102 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2103 {
2104 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002105 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002106 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002107#endif
2108
2109 /* Load ramdisk & kernel */
2110 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002111 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002112
2113#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002114 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2115 {
2116 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2117 return;
2118 }
2119
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002120 /*
2121 * If dtb is not found look for appended DTB in the kernel.
2122 * If appended dev tree is found, update the atags with
2123 * memory address to the DTB appended location on RAM.
2124 * Else update with the atags address in the kernel header
2125 */
2126 if (!dtb_copied) {
2127 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002128 dtb = dev_tree_appended((void*)(ptr + page_size),
2129 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002130 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002131 if (!dtb) {
2132 fastboot_fail("dtb not found");
2133 return;
2134 }
Amol Jadicb524072012-08-09 16:40:18 -07002135 }
2136#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002137
2138 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002139 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002140
Dima Zavin77e41f32013-03-06 16:10:43 -08002141 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002142 (const char*) hdr->cmdline, board_machtype(),
2143 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002144}
2145
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002146void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002147{
2148 struct ptentry *ptn;
2149 struct ptable *ptable;
2150
2151 ptable = flash_get_ptable();
2152 if (ptable == NULL) {
2153 fastboot_fail("partition table doesn't exist");
2154 return;
2155 }
2156
2157 ptn = ptable_find(ptable, arg);
2158 if (ptn == NULL) {
2159 fastboot_fail("unknown partition name");
2160 return;
2161 }
2162
2163 if (flash_erase(ptn)) {
2164 fastboot_fail("failed to erase partition");
2165 return;
2166 }
2167 fastboot_okay("");
2168}
2169
Bikas Gurungd48bd242010-09-04 19:54:32 -07002170
2171void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2172{
2173 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002174 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002175 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002176 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002177
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002178#if VERIFIED_BOOT
2179 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2180 {
2181 if(!device.is_unlocked)
2182 {
2183 fastboot_fail("unlock device to erase keystore");
2184 return;
2185 }
2186 }
2187#endif
2188
Kinson Chikf1a43512011-07-14 11:28:39 -07002189 index = partition_get_index(arg);
2190 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002191 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002192
Kinson Chikf1a43512011-07-14 11:28:39 -07002193 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002194 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002195 return;
2196 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002197
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002198 lun = partition_get_lun(index);
2199 mmc_set_lun(lun);
2200
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002201 if (platform_boot_dev_isemmc())
2202 {
2203 if (mmc_erase_card(ptn, size)) {
2204 fastboot_fail("failed to erase partition\n");
2205 return;
2206 }
2207 } else {
2208 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2209 size = partition_get_size(index);
2210 if (size > DEFAULT_ERASE_SIZE)
2211 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002212
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002213 /* Simple inefficient version of erase. Just writing
2214 0 in first several blocks */
2215 if (mmc_write(ptn , size, (unsigned int *)out)) {
2216 fastboot_fail("failed to erase partition");
2217 return;
2218 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002219 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002220#if VERIFIED_BOOT
2221 if(!(strncmp(arg, "userdata", 8)))
2222 if(send_delete_keys_to_tz())
2223 ASSERT(0);
2224#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002225 fastboot_okay("");
2226}
2227
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002228void cmd_erase(const char *arg, void *data, unsigned sz)
2229{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002230#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002231 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002232 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002233 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002234 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002235 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002236 return;
2237 }
2238 }
2239#endif
2240
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002241 if(target_is_emmc_boot())
2242 cmd_erase_mmc(arg, data, sz);
2243 else
2244 cmd_erase_nand(arg, data, sz);
2245}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002246
Ajay Dudani5c761132011-04-07 20:19:04 -07002247void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002248{
2249 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002250 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002251 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002252 char *token = NULL;
2253 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002254 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002255 uint8_t lun = 0;
2256 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002257
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002258 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002259 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002260 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002261 if(token)
2262 {
2263 lun = atoi(token);
2264 mmc_set_lun(lun);
2265 lun_set = true;
2266 }
2267
Mao Jinlong226f33a2014-07-04 17:24:10 +08002268 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002269 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002270 if (!strcmp(pname, "partition"))
2271 {
2272 dprintf(INFO, "Attempt to write partition image.\n");
2273 if (write_partition(sz, (unsigned char *) data)) {
2274 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002275 return;
2276 }
2277 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002278 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002279 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002280#if VERIFIED_BOOT
2281 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2282 {
2283 if(!device.is_unlocked)
2284 {
2285 fastboot_fail("unlock device to flash keystore");
2286 return;
2287 }
2288 if(!boot_verify_validate_keystore((unsigned char *)data))
2289 {
2290 fastboot_fail("image is not a keystore file");
2291 return;
2292 }
2293 }
2294#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002295 index = partition_get_index(pname);
2296 ptn = partition_get_offset(index);
2297 if(ptn == 0) {
2298 fastboot_fail("partition table doesn't exist");
2299 return;
2300 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002301
Mao Jinlong226f33a2014-07-04 17:24:10 +08002302 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2303 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2304 fastboot_fail("image is not a boot image");
2305 return;
2306 }
2307 }
2308
2309 if(!lun_set)
2310 {
2311 lun = partition_get_lun(index);
2312 mmc_set_lun(lun);
2313 }
2314
2315 size = partition_get_size(index);
2316 if (ROUND_TO_PAGE(sz,511) > size) {
2317 fastboot_fail("size too large");
2318 return;
2319 }
2320 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2321 fastboot_fail("flash write failure");
2322 return;
2323 }
Greg Grisco6e754772011-06-23 12:19:39 -07002324 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002325 }
2326 fastboot_okay("");
2327 return;
2328}
2329
Ajay Dudanide984792015-03-02 09:57:41 -08002330void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2331{
2332 int i, images;
2333 meta_header_t *meta_header;
2334 img_header_entry_t *img_header_entry;
2335
2336 meta_header = (meta_header_t*) data;
2337 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2338
2339 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2340
2341 for (i=0; i<images; i++) {
2342
2343 if((img_header_entry[i].ptn_name == NULL) ||
2344 (img_header_entry[i].start_offset == 0) ||
2345 (img_header_entry[i].size == 0))
2346 break;
2347
2348 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2349 (void *) data + img_header_entry[i].start_offset,
2350 img_header_entry[i].size);
2351 }
2352
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002353 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2354 {
2355 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2356 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2357 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2358 }
2359 else
2360 {
2361 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2362 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2363 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2364 }
2365
2366 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002367 fastboot_okay("");
2368 return;
2369}
2370
Ajay Dudani5c761132011-04-07 20:19:04 -07002371void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2372{
2373 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002374 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002375 uint32_t *fill_buf = NULL;
2376 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002377 sparse_header_t *sparse_header;
2378 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002379 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002380 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302381 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002382 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302383 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002384 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302385 /*End of the sparse image address*/
2386 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002387
Kinson Chikf1a43512011-07-14 11:28:39 -07002388 index = partition_get_index(arg);
2389 ptn = partition_get_offset(index);
2390 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002391 fastboot_fail("partition table doesn't exist");
2392 return;
2393 }
2394
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302395 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302396
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002397 lun = partition_get_lun(index);
2398 mmc_set_lun(lun);
2399
vijay kumar800255e2015-04-24 20:26:19 +05302400 if (sz < sizeof(sparse_header_t)) {
2401 fastboot_fail("size too low");
2402 return;
2403 }
2404
Ajay Dudani5c761132011-04-07 20:19:04 -07002405 /* Read and skip over sparse image header */
2406 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302407
vijay kumar1321f342015-03-27 12:13:42 +05302408 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002409 fastboot_fail("size too large");
2410 return;
2411 }
2412
vijay kumarde4fcf62015-04-23 13:05:49 +05302413 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302414
2415 if (data_end < (uint32_t)data) {
2416 fastboot_fail("buffer overreads occured due to invalid sparse header");
2417 return;
2418 }
2419
vijay kumarde4fcf62015-04-23 13:05:49 +05302420 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002421 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302422 fastboot_fail("sparse header size mismatch");
2423 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002424 }
2425
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002426 dprintf (SPEW, "=== Sparse Image Header ===\n");
2427 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2428 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2429 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2430 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2431 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2432 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2433 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2434 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002435
2436 /* Start processing chunks */
2437 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2438 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302439 /* Make sure the total image size does not exceed the partition size */
2440 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2441 fastboot_fail("size too large");
2442 return;
2443 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002444 /* Read and skip over chunk header */
2445 chunk_header = (chunk_header_t *) data;
2446 data += sizeof(chunk_header_t);
2447
vijay kumar800255e2015-04-24 20:26:19 +05302448 if (data_end < (uint32_t)data) {
2449 fastboot_fail("buffer overreads occured due to invalid sparse header");
2450 return;
2451 }
2452
Ajay Dudani5c761132011-04-07 20:19:04 -07002453 dprintf (SPEW, "=== Chunk Header ===\n");
2454 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2455 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2456 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2457
vijay kumar800255e2015-04-24 20:26:19 +05302458 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002459 {
vijay kumar800255e2015-04-24 20:26:19 +05302460 fastboot_fail("chunk header size mismatch");
2461 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002462 }
2463
wufeng.jiang813dc352015-06-02 23:02:46 -04002464 if (!sparse_header->blk_sz ){
2465 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302466 return;
2467 }
2468
wufeng.jiang813dc352015-06-02 23:02:46 -04002469 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2470
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302471 /* Make sure that the chunk size calculated from sparse image does not
2472 * exceed partition size
2473 */
2474 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2475 {
2476 fastboot_fail("Chunk data size exceeds partition size");
2477 return;
2478 }
2479
Ajay Dudani5c761132011-04-07 20:19:04 -07002480 switch (chunk_header->chunk_type)
2481 {
2482 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002483 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002484 chunk_data_sz))
2485 {
2486 fastboot_fail("Bogus chunk size for chunk type Raw");
2487 return;
2488 }
2489
vijay kumar800255e2015-04-24 20:26:19 +05302490 if (data_end < (uint32_t)data + chunk_data_sz) {
2491 fastboot_fail("buffer overreads occured due to invalid sparse header");
2492 return;
2493 }
2494
wufeng.jiang813dc352015-06-02 23:02:46 -04002495 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2496 otherwise it will return in the line above
2497 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002498 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002499 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002500 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002501 {
2502 fastboot_fail("flash write failure");
2503 return;
2504 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302505 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2506 fastboot_fail("Bogus size for RAW chunk type");
2507 return;
2508 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002509 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002510 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002511 break;
2512
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002513 case CHUNK_TYPE_FILL:
2514 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2515 sizeof(uint32_t)))
2516 {
2517 fastboot_fail("Bogus chunk size for chunk type FILL");
2518 return;
2519 }
2520
2521 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2522 if (!fill_buf)
2523 {
2524 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2525 return;
2526 }
2527
vijay kumar800255e2015-04-24 20:26:19 +05302528 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2529 fastboot_fail("buffer overreads occured due to invalid sparse header");
2530 return;
2531 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002532 fill_val = *(uint32_t *)data;
2533 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002534
2535 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2536 {
2537 fill_buf[i] = fill_val;
2538 }
2539
wufeng.jiang813dc352015-06-02 23:02:46 -04002540 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002541 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302542 /* Make sure that the data written to partition does not exceed partition size */
2543 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2544 {
2545 fastboot_fail("Chunk data size for fill type exceeds partition size");
2546 return;
2547 }
2548
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002549 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2550 sparse_header->blk_sz,
2551 fill_buf))
2552 {
2553 fastboot_fail("flash write failure");
2554 free(fill_buf);
2555 return;
2556 }
2557
2558 total_blocks++;
2559 }
2560
2561 free(fill_buf);
2562 break;
2563
Ajay Dudani5c761132011-04-07 20:19:04 -07002564 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302565 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2566 fastboot_fail("bogus size for chunk DONT CARE type");
2567 return;
2568 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002569 total_blocks += chunk_header->chunk_sz;
2570 break;
2571
Ajay Dudani5c761132011-04-07 20:19:04 -07002572 case CHUNK_TYPE_CRC:
2573 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2574 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002575 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002576 return;
2577 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302578 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2579 fastboot_fail("bogus size for chunk CRC type");
2580 return;
2581 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002582 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302583 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2584 fastboot_fail("integer overflow occured");
2585 return;
2586 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002587 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302588 if (data_end < (uint32_t)data) {
2589 fastboot_fail("buffer overreads occured due to invalid sparse header");
2590 return;
2591 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002592 break;
2593
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002594 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002595 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002596 fastboot_fail("Unknown chunk type");
2597 return;
2598 }
2599 }
2600
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002601 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2602 total_blocks, sparse_header->total_blks);
2603
2604 if(total_blocks != sparse_header->total_blks)
2605 {
2606 fastboot_fail("sparse image write failure");
2607 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002608
2609 fastboot_okay("");
2610 return;
2611}
2612
2613void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2614{
2615 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002616 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002617
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002618#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002619 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2620 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002621 int ret=0;
2622 uint32 major_version=0;
2623 uint32 minor_version=0;
2624
2625 ret = scm_svc_version(&major_version,&minor_version);
2626 if(!ret)
2627 {
2628 if(major_version >= 2)
2629 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002630 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002631 {
2632 ret = encrypt_scm((uint32 **) &data, &sz);
2633 if (ret != 0) {
2634 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2635 return;
2636 }
2637
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002638 /* Protect only for SSD */
2639 if (!strcmp(arg, "ssd")) {
2640 ret = scm_protect_keystore((uint32 *) data, sz);
2641 if (ret != 0) {
2642 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2643 return;
2644 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002645 }
2646 }
2647 else
2648 {
2649 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2650 if(ret != 0)
2651 {
2652 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2653 return;
2654 }
2655 }
2656 }
2657 else
2658 {
2659 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2660 magic_number[1] == DECRYPT_MAGIC_1)
2661 {
2662 ret = decrypt_scm((uint32 **) &data, &sz);
2663 if (ret != 0) {
2664 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2665 return;
2666 }
2667 }
2668 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2669 magic_number[1] == ENCRYPT_MAGIC_1)
2670 {
2671 ret = encrypt_scm((uint32 **) &data, &sz);
2672 if (ret != 0) {
2673 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2674 return;
2675 }
2676 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002677 }
2678 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002679 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002680 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002681 dprintf(CRITICAL,"INVALID SVC Version\n");
2682 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002683 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002684#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002685
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002686#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002687 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002688 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002689 if(!device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002690 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002691 fastboot_fail("device is locked. Cannot flash images");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002692 return;
2693 }
2694 }
2695#endif
2696
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002697 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002698 meta_header = (meta_header_t *) data;
2699 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002700 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002701 else if (meta_header->magic == META_HEADER_MAGIC)
2702 cmd_flash_meta_img(arg, data, sz);
2703 else
2704 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002705
2706#if VERIFIED_BOOT
2707 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
2708 {
2709 // reset dm_verity mode to enforcing
2710 device.verity_mode = 1;
2711 write_device_info(&device);
2712 }
2713#endif
2714
Ajay Dudani5c761132011-04-07 20:19:04 -07002715 return;
2716}
2717
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002718void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2719{
2720 struct ptentry *sys_ptn;
2721 struct ptable *ptable;
2722
2723 ptable = flash_get_ptable();
2724 if (ptable == NULL) {
2725 fastboot_fail("partition table doesn't exist");
2726 return;
2727 }
2728
2729 sys_ptn = ptable_find(ptable, "system");
2730 if (sys_ptn == NULL) {
2731 fastboot_fail("system partition not found");
2732 return;
2733 }
2734
2735 sz = ROUND_TO_PAGE(sz, page_mask);
2736 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2737 fastboot_fail("update_ubi_vol failed");
2738 else
2739 fastboot_okay("");
2740}
2741
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002742void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002743{
2744 struct ptentry *ptn;
2745 struct ptable *ptable;
2746 unsigned extra = 0;
2747
2748 ptable = flash_get_ptable();
2749 if (ptable == NULL) {
2750 fastboot_fail("partition table doesn't exist");
2751 return;
2752 }
2753
2754 ptn = ptable_find(ptable, arg);
2755 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002756 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2757 arg);
2758 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002759 return;
2760 }
2761
2762 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2763 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2764 fastboot_fail("image is not a boot image");
2765 return;
2766 }
2767 }
2768
Amol Jadi5c61a952012-05-04 17:05:35 -07002769 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002770 || !strcmp(ptn->name, "userdata")
2771 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002772 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002773 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002774 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002775 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002776 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002777
2778 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002779 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2780 if (flash_ubi_img(ptn, data, sz)) {
2781 fastboot_fail("flash write failure");
2782 return;
2783 }
2784 } else {
2785 if (flash_write(ptn, extra, data, sz)) {
2786 fastboot_fail("flash write failure");
2787 return;
2788 }
Dima Zavin214cc642009-01-26 11:16:21 -08002789 }
2790 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2791 fastboot_okay("");
2792}
2793
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002794void cmd_flash(const char *arg, void *data, unsigned sz)
2795{
2796 if(target_is_emmc_boot())
2797 cmd_flash_mmc(arg, data, sz);
2798 else
2799 cmd_flash_nand(arg, data, sz);
2800}
2801
Dima Zavin214cc642009-01-26 11:16:21 -08002802void cmd_continue(const char *arg, void *data, unsigned sz)
2803{
2804 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002805 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002806
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002807 if (target_is_emmc_boot())
2808 {
lijuanga40d6302015-07-20 20:10:13 +08002809#if FBCON_DISPLAY_MSG
2810 keys_detect_init();
2811#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002812 boot_linux_from_mmc();
2813 }
2814 else
2815 {
2816 boot_linux_from_flash();
2817 }
Dima Zavin214cc642009-01-26 11:16:21 -08002818}
2819
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002820void cmd_reboot(const char *arg, void *data, unsigned sz)
2821{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002822 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002823 fastboot_okay("");
2824 reboot_device(0);
2825}
2826
2827void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2828{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002829 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002830 fastboot_okay("");
2831 reboot_device(FASTBOOT_MODE);
2832}
2833
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002834void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2835{
2836 dprintf(INFO, "Enabling charger screen check\n");
2837 device.charger_screen_enabled = 1;
2838 write_device_info(&device);
2839 fastboot_okay("");
2840}
2841
2842void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2843{
2844 dprintf(INFO, "Disabling charger screen check\n");
2845 device.charger_screen_enabled = 0;
2846 write_device_info(&device);
2847 fastboot_okay("");
2848}
2849
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302850void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2851{
2852 dprintf(INFO, "Selecting display panel %s\n", arg);
2853 if (arg)
2854 strlcpy(device.display_panel, arg,
2855 sizeof(device.display_panel));
2856 write_device_info(&device);
2857 fastboot_okay("");
2858}
2859
Shashank Mittal162244e2011-08-08 19:01:25 -07002860void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2861{
vijay kumarc65876c2015-04-24 13:29:16 +05302862 if(!is_allow_unlock) {
2863 fastboot_fail("oem unlock is not allowed");
2864 return;
2865 }
2866
lijuanga40d6302015-07-20 20:10:13 +08002867#if FBCON_DISPLAY_MSG
2868 if(!device.is_unlocked)
2869 display_menu_thread(DISPLAY_THREAD_UNLOCK);
2870 else
2871 fastboot_info("Device already unlocked!");
2872 fastboot_okay("");
2873#else
vijay kumarc65876c2015-04-24 13:29:16 +05302874 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
lijuanga40d6302015-07-20 20:10:13 +08002875#endif
vijay kumarc65876c2015-04-24 13:29:16 +05302876}
2877
2878void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
2879{
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002880 if(!device.is_unlocked)
Shashank Mittal162244e2011-08-08 19:01:25 -07002881 {
vijay kumarc65876c2015-04-24 13:29:16 +05302882 if(!is_allow_unlock) {
2883 fastboot_fail("oem unlock is not allowed");
2884 return;
2885 }
2886
Shashank Mittal162244e2011-08-08 19:01:25 -07002887 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002888 write_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05302889
2890 struct recovery_message msg;
2891 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2892 write_misc(0, &msg, sizeof(msg));
2893
2894 fastboot_okay("");
2895 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002896 }
2897 fastboot_okay("");
2898}
2899
2900void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2901{
Sridhar Parasuram8b792422015-07-05 11:38:13 -07002902 struct recovery_message msg;
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002903 if(device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002904 {
2905 device.is_unlocked = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002906 write_device_info(&device);
Sridhar Parasuram8b792422015-07-05 11:38:13 -07002907 // upon oem lock, reboot to recovery to wipe user data
2908 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2909 write_misc(0, &msg, sizeof(msg));
2910 fastboot_okay("");
2911 reboot_device(RECOVERY_MODE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002912 }
2913 fastboot_okay("");
2914}
2915
Shashank Mittala0032282011-08-26 14:50:11 -07002916void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2917{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302918 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002919 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002920 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002921 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2922 fastboot_info(response);
2923 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002924 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302925 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2926 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002927 fastboot_okay("");
2928}
2929
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002930void cmd_preflash(const char *arg, void *data, unsigned sz)
2931{
2932 fastboot_okay("");
2933}
2934
Mao Flynn7b379f32015-04-20 00:28:30 +08002935static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302936
Mao Flynn7b379f32015-04-20 00:28:30 +08002937int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302938{
Mao Flynn7b379f32015-04-20 00:28:30 +08002939 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302940 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08002941 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302942 return -1;
2943 return 0;
2944}
2945
Mao Flynn7b379f32015-04-20 00:28:30 +08002946int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002947{
2948 struct ptentry *ptn;
2949 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08002950 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002951 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002952
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302953 ptable = flash_get_ptable();
2954 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002955 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2956 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302957 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002958
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302959 ptn = ptable_find(ptable, "splash");
2960 if (ptn == NULL) {
2961 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002962 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302963 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002964 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302965 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002966 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302967 }
2968
Mao Flynn7b379f32015-04-20 00:28:30 +08002969 header = (struct logo_img_header *)logo_header;
2970 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302971 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002972 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302973 }
2974
2975 fb_display = fbcon_display();
2976 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002977 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
2978 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
2979
2980 /* if the logo is full-screen size, remove "fbcon_clear()" */
2981 if ((header->width != fb_display->width)
2982 || (header->height != fb_display->height))
2983 fbcon_clear();
2984
2985 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
2986 (uint32_t *)base,
2987 (header->blocks * 512))) {
2988 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
2989 return -1;
2990 }
2991 fbcon_extract_to_screen(header, base);
2992 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002993 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002994
2995 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
2996 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
2997 return -1;
2998 }
2999
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303000 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003001 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3002 (uint32_t *)base,
3003 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303004 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303005 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003006 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003007 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303008 }
3009
Mao Flynn7b379f32015-04-20 00:28:30 +08003010 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303011}
3012
Mao Flynn7b379f32015-04-20 00:28:30 +08003013int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303014{
3015 int index = INVALID_PTN;
3016 unsigned long long ptn = 0;
3017 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003018 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003019 uint32_t blocksize, realsize, readsize;
3020 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303021
3022 index = partition_get_index("splash");
3023 if (index == 0) {
3024 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003025 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303026 }
3027
3028 ptn = partition_get_offset(index);
3029 if (ptn == 0) {
3030 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003031 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303032 }
3033
Mao Flynn1893a7f2015-06-03 12:03:36 +08003034 mmc_set_lun(partition_get_lun(index));
3035
3036 blocksize = mmc_get_device_blocksize();
3037 if (blocksize == 0) {
3038 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3039 return -1;
3040 }
3041
3042 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003043 if (!fb_display)
3044 {
3045 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3046 return -1;
3047 }
3048
Mao Flynn1893a7f2015-06-03 12:03:36 +08003049 base = (uint8_t *) fb_display->base;
3050
3051 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303052 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003053 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303054 }
3055
Mao Flynn1893a7f2015-06-03 12:03:36 +08003056 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003057 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303058 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003059 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303060 }
3061
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303062 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003063 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3064 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003065
Mao Flynn1893a7f2015-06-03 12:03:36 +08003066 realsize = header->blocks * 512;
3067 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3068
3069 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003070 if ((header->width != fb_display->width)
3071 || (header->height != fb_display->height))
3072 fbcon_clear();
3073
Mao Flynn1893a7f2015-06-03 12:03:36 +08003074 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003075 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3076 return -1;
3077 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003078
Mao Flynn1893a7f2015-06-03 12:03:36 +08003079 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3080 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303081
Mao Flynn1893a7f2015-06-03 12:03:36 +08003082 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3083 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3084 return -1;
3085 }
3086
3087 realsize = header->width * header->height * fb_display->bpp / 8;
3088 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3089
3090 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3091 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3092 fbcon_clear();
3093 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3094 return -1;
3095 }
3096 } else {
3097 if (mmc_read(ptn + blocksize ,
3098 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3099 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3100 return -1;
3101 }
3102 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3103 }
3104 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303105 }
3106
Mao Flynn7b379f32015-04-20 00:28:30 +08003107 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303108}
3109
Mao Flynn7b379f32015-04-20 00:28:30 +08003110int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303111{
3112 if (target_is_emmc_boot()) {
3113 return splash_screen_mmc();
3114 } else {
3115 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003116 }
3117}
3118
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003119/* Get the size from partiton name */
3120static void get_partition_size(const char *arg, char *response)
3121{
3122 uint64_t ptn = 0;
3123 uint64_t size;
3124 int index = INVALID_PTN;
3125
3126 index = partition_get_index(arg);
3127
3128 if (index == INVALID_PTN)
3129 {
3130 dprintf(CRITICAL, "Invalid partition index\n");
3131 return;
3132 }
3133
3134 ptn = partition_get_offset(index);
3135
3136 if(!ptn)
3137 {
3138 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3139 return;
3140 }
3141
3142 size = partition_get_size(index);
3143
3144 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3145 return;
3146}
3147
3148/*
3149 * Publish the partition type & size info
3150 * fastboot getvar will publish the required information.
3151 * fastboot getvar partition_size:<partition_name>: partition size in hex
3152 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3153 */
3154static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3155{
3156 uint8_t i;
3157
3158 for (i = 0; i < num_parts; i++) {
3159 get_partition_size(info[i].part_name, info[i].size_response);
3160
3161 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3162 {
3163 dprintf(CRITICAL, "partition size name truncated\n");
3164 return;
3165 }
3166 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3167 {
3168 dprintf(CRITICAL, "partition type name truncated\n");
3169 return;
3170 }
3171
3172 /* publish partition size & type info */
3173 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3174 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3175 }
3176}
3177
Amol Jadi5edf3552013-07-23 14:15:34 -07003178/* register commands and variables for fastboot */
3179void aboot_fastboot_register_commands(void)
3180{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003181 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07003182
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003183 struct fastboot_cmd_desc cmd_list[] = {
3184 /* By default the enabled list is empty. */
3185 {"", NULL},
3186 /* move commands enclosed within the below ifndef to here
3187 * if they need to be enabled in user build.
3188 */
3189#ifndef DISABLE_FASTBOOT_CMDS
3190 /* Register the following commands only for non-user builds */
3191 {"flash:", cmd_flash},
3192 {"erase:", cmd_erase},
3193 {"boot", cmd_boot},
3194 {"continue", cmd_continue},
3195 {"reboot", cmd_reboot},
3196 {"reboot-bootloader", cmd_reboot_bootloader},
3197 {"oem unlock", cmd_oem_unlock},
vijay kumarc65876c2015-04-24 13:29:16 +05303198 {"oem unlock-go", cmd_oem_unlock_go},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003199 {"oem lock", cmd_oem_lock},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003200 {"oem device-info", cmd_oem_devinfo},
3201 {"preflash", cmd_preflash},
3202 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3203 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08003204 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003205#endif
3206 };
3207
3208 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3209 for (i = 1; i < fastboot_cmds_count; i++)
3210 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3211
Amol Jadi5edf3552013-07-23 14:15:34 -07003212 /* publish variables and their values */
3213 fastboot_publish("product", TARGET(BOARD));
3214 fastboot_publish("kernel", "lk");
3215 fastboot_publish("serialno", sn_buf);
3216
3217 /*
3218 * partition info is supported only for emmc partitions
3219 * Calling this for NAND prints some error messages which
3220 * is harmless but misleading. Avoid calling this for NAND
3221 * devices.
3222 */
3223 if (target_is_emmc_boot())
3224 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3225
3226 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003227 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3228 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003229 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003230 /* Is the charger screen check enabled */
3231 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3232 device.charger_screen_enabled);
3233 fastboot_publish("charger-screen-enabled",
3234 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303235 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3236 device.display_panel);
3237 fastboot_publish("display-panel",
3238 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003239 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3240 fastboot_publish("version-baseband", (const char *) device.radio_version);
Amol Jadi5edf3552013-07-23 14:15:34 -07003241}
3242
Brian Swetland9c4c0752009-01-25 16:23:50 -08003243void aboot_init(const struct app_descriptor *app)
3244{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003245 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03003246 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003247
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003248 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003249 if (target_is_emmc_boot())
3250 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003251 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003252 page_mask = page_size - 1;
3253 }
3254 else
3255 {
3256 page_size = flash_page_size();
3257 page_mask = page_size - 1;
3258 }
3259
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003260 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3261
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003262 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303263 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003264
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003265 /* Display splash screen if enabled */
3266#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003267#if NO_ALARM_DISPLAY
3268 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003269#endif
lijuang99c02d82015-02-13 19:04:34 +08003270 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003271#if ENABLE_WBC
3272 if (!pm_appsbl_display_init_done())
3273 target_display_init(device.display_panel);
3274 else
3275 display_image_on_screen();
3276#else
lijuang99c02d82015-02-13 19:04:34 +08003277 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003278#endif
lijuang99c02d82015-02-13 19:04:34 +08003279 dprintf(SPEW, "Display Init: Done\n");
3280#if NO_ALARM_DISPLAY
3281 }
3282#endif
3283#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003284
Greg Griscod6250552011-06-29 14:40:23 -07003285 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003286 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003287
Dhaval Patel223ec952013-07-18 14:49:44 -07003288 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3289
Matthew Qindefd5562014-07-11 18:02:40 +08003290 /*
3291 * Check power off reason if user force reset,
3292 * if yes phone will do normal boot.
3293 */
3294 if (is_user_force_reset())
3295 goto normal_boot;
3296
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003297 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003298 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003299 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003300 dprintf(ALWAYS,"dload mode key sequence detected\n");
3301 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003302 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003303 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003304 }
3305 else
3306 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303307 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003308 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003309 }
3310 boot_into_fastboot = true;
3311 }
3312 if (!boot_into_fastboot)
3313 {
3314 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3315 boot_into_recovery = 1;
3316 if (!boot_into_recovery &&
3317 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003318 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003319 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003320 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003321 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003322 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003323 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003324
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003325#if USE_PON_REBOOT_REG
3326 reboot_mode = check_hard_reboot_mode();
3327#else
Ajay Dudani77421292010-10-27 19:34:06 -07003328 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003329#endif
3330 if (reboot_mode == RECOVERY_MODE)
3331 {
Ajay Dudani77421292010-10-27 19:34:06 -07003332 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003333 }
3334 else if(reboot_mode == FASTBOOT_MODE)
3335 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003336 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003337 }
3338 else if(reboot_mode == ALARM_BOOT)
3339 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003340 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003341 }
3342 else if (reboot_mode == DM_VERITY_ENFORCING)
3343 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003344 device.verity_mode = 1;
3345 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003346 }
3347 else if (reboot_mode == DM_VERITY_LOGGING)
3348 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003349 device.verity_mode = 0;
3350 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003351 }
3352 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3353 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003354 if(send_delete_keys_to_tz())
3355 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003356 }
3357
Matthew Qindefd5562014-07-11 18:02:40 +08003358normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003359 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003360 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003361 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003362 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003363 if(emmc_recovery_init())
3364 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3365 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003366 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003367 if((device.is_unlocked) || (device.is_tampered))
3368 {
3369 #ifdef TZ_TAMPER_FUSE
3370 set_tamper_fuse_cmd();
3371 #endif
3372 #if USE_PCOM_SECBOOT
3373 set_tamper_flag(device.is_tampered);
3374 #endif
3375 }
Shashank Mittala0032282011-08-26 14:50:11 -07003376 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003377
Pavel Nedev5d91d412013-04-29 11:34:24 +03003378 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003379 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003380 else
3381 {
3382 recovery_init();
3383 #if USE_PCOM_SECBOOT
3384 if((device.is_unlocked) || (device.is_tampered))
3385 set_tamper_flag(device.is_tampered);
3386 #endif
3387 boot_linux_from_flash();
3388 }
3389 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3390 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003391 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003392
Amol Jadi5edf3552013-07-23 14:15:34 -07003393 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003394
Amol Jadi5edf3552013-07-23 14:15:34 -07003395 /* register aboot specific fastboot commands */
3396 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003397
Amol Jadi5edf3552013-07-23 14:15:34 -07003398 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003399 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003400
3401 /* initialize and start fastboot */
3402 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08003403}
3404
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003405uint32_t get_page_size()
3406{
3407 return page_size;
3408}
3409
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003410/*
3411 * Calculated and save hash (SHA256) for non-signed boot image.
3412 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003413 * @param image_addr - Boot image address
3414 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003415 *
3416 * @return int - 0 on success, negative value on failure.
3417 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003418static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003419{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003420 unsigned int digest[8];
3421#if IMAGE_VERIF_ALGO_SHA1
3422 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3423#else
3424 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3425#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003426
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003427 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003428 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003429
3430 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003431 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003432
3433 return 0;
3434}
3435
Brian Swetland9c4c0752009-01-25 16:23:50 -08003436APP_START(aboot)
3437 .init = aboot_init,
3438APP_END