blob: 8e1d71d25d800e9d585e3c33178844afc4f15f6e [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08005 * Copyright (c) 2009-2015, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080060#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053061#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080062#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080063#if USE_RPMB_FOR_DEVINFO
64#include <rpmb.h>
65#endif
Dima Zavin214cc642009-01-26 11:16:21 -080066
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070067#if ENABLE_WBC
68#include <pm_app_smbchg.h>
69#endif
70
Neeti Desai17379b82012-06-04 18:42:53 -070071#if DEVICE_TREE
72#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070073#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070074#endif
75
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053076#if WDOG_SUPPORT
77#include <wdog.h>
78#endif
79
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070080#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070081#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080082#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080083#include "bootimg.h"
84#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070085#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080086#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070087#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070088#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070089#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020091#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070092#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080093#include <menu_keys_detect.h>
94#include <display_menu.h>
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070095
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070096extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070097extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070098extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070099extern int get_target_boot_params(const char *cmdline, const char *part,
100 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700101
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700102void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700103void write_device_info_mmc(device_info *dev);
104void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700105static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700106
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700107/* fastboot command function pointer */
108typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
109
110struct fastboot_cmd_desc {
111 char * name;
112 fastboot_cmd_fn cb;
113};
114
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700115#define EXPAND(NAME) #NAME
116#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700117
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800118#ifdef MEMBASE
119#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
120#else
David Ng183a7422009-12-07 14:55:21 -0800121#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800122#endif
123
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700124#ifndef MEMSIZE
125#define MEMSIZE 1024*1024
126#endif
127
128#define MAX_TAGS_SIZE 1024
129
Kun Liang2f1601a2013-08-12 16:29:54 +0800130/* make 4096 as default size to ensure EFS,EXT4's erasing */
131#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700132#define MAX_PANEL_BUF_SIZE 196
Kun Liang2f1601a2013-08-12 16:29:54 +0800133
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700134#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700135#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800136
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800137#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
138
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700139#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
140
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800141#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700142static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700143#else
David Ng183a7422009-12-07 14:55:21 -0800144static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700145#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800146static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300147static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800148static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800149static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800150static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700151static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300152static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200153static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800154
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800155static const char *baseband_apq = " androidboot.baseband=apq";
156static const char *baseband_msm = " androidboot.baseband=msm";
157static const char *baseband_csfb = " androidboot.baseband=csfb";
158static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700159static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800160static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700161static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800162static const char *baseband_dsda = " androidboot.baseband=dsda";
163static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800164static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800165static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800166
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700167#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700168static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700169static const char *verified_state= " androidboot.verifiedbootstate=";
170
171//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700172
173struct verified_boot_verity_mode vbvm[] =
174{
175 {false, "logging"},
176 {true, "enforcing"},
177};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700178struct verified_boot_state_name vbsn[] =
179{
180 {GREEN, "green"},
181 {ORANGE, "orange"},
182 {YELLOW,"yellow"},
183 {RED,"red" },
184};
185#endif
186
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700187static unsigned page_size = 0;
188static unsigned page_mask = 0;
189static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
190static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700191static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800192static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700193static bool devinfo_present = true;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700194
Shashank Mittalcd98d472011-08-02 14:29:24 -0700195/* Assuming unauthorized kernel image by default */
196static int auth_kernel_img = 0;
197
lijuang511a2b52015-08-14 20:50:51 +0800198static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
vijay kumarc65876c2015-04-24 13:29:16 +0530199static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700200
vijay kumarca2e6812015-07-08 20:28:25 +0530201static char frp_ptns[2][8] = {"config","frp"};
202
lijuang511a2b52015-08-14 20:50:51 +0800203static const char *critical_flash_allowed_ptn[] = {
204 "aboot",
205 "rpm",
206 "tz",
207 "sbl",
208 "sdi",
209 "sbl1",
210 "xbl",
211 "hyp",
212 "pmic",
213 "bootloader",
214 "devinfo",
215 "partition"};
216
Dima Zavin42168f22009-01-30 11:52:22 -0800217struct atag_ptbl_entry
218{
219 char name[16];
220 unsigned offset;
221 unsigned size;
222 unsigned flags;
223};
224
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700225/*
226 * Partition info, required to be published
227 * for fastboot
228 */
229struct getvar_partition_info {
230 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
231 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
232 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
233 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
234 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
235};
236
237/*
238 * Right now, we are publishing the info for only
239 * three partitions
240 */
241struct getvar_partition_info part_info[] =
242{
243 { "system" , "partition-size:", "partition-type:", "", "ext4" },
244 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
245 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
246};
247
248char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700249char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800250char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700251char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530252char panel_display_mode[MAX_RSP_SIZE];
lijuang65c5a822015-08-29 16:35:36 +0800253char battery_voltage[MAX_RSP_SIZE];
lijuangf16461c2015-08-03 17:09:34 +0800254char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700255
Greg Griscod2471ef2011-07-14 13:00:42 -0700256extern int emmc_recovery_init(void);
257
Kinson Chik0b1c8162011-08-31 16:31:57 -0700258#if NO_KEYPAD_DRIVER
259extern int fastboot_trigger(void);
260#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700261
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800262static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700263{
264 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700265#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800266 if (is_arm64)
267 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
268 else
269 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700270 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
271 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700272#endif
273}
274
Dima Zavin42168f22009-01-30 11:52:22 -0800275static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
276{
277 struct atag_ptbl_entry atag_ptn;
278
279 memcpy(atag_ptn.name, ptn->name, 16);
280 atag_ptn.name[15] = '\0';
281 atag_ptn.offset = ptn->start;
282 atag_ptn.size = ptn->length;
283 atag_ptn.flags = ptn->flags;
284 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
285 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
286}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800287
Neeti Desaie245d492012-06-01 12:52:13 -0700288unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800289{
David Ng183a7422009-12-07 14:55:21 -0800290 int cmdline_len = 0;
291 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800292 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700293 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800294 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300295 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700296 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700297 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200298 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700299#if VERIFIED_BOOT
300 uint32_t boot_state = boot_verify_get_state();
301#endif
302
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200303#ifdef MDTP_SUPPORT
304 mdtp_activated(&is_mdtp_activated);
305#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800306
Brian Swetland9c4c0752009-01-25 16:23:50 -0800307 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800308 cmdline_len = strlen(cmdline);
309 have_cmdline = 1;
310 }
311 if (target_is_emmc_boot()) {
312 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800313#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700314 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
315 ASSERT(boot_dev_buf);
316 platform_boot_dev_cmdline(boot_dev_buf);
317 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700318#endif
David Ng183a7422009-12-07 14:55:21 -0800319 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800320
321 cmdline_len += strlen(usb_sn_cmdline);
322 cmdline_len += strlen(sn_buf);
323
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700324#if VERIFIED_BOOT
325 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700326 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700327#endif
328
Pavel Nedev5614d222013-06-17 18:01:02 +0300329 if (boot_into_recovery && gpt_exists)
330 cmdline_len += strlen(secondary_gpt_enable);
331
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200332 if(is_mdtp_activated)
333 cmdline_len += strlen(mdtp_activated_flag);
334
Pavel Nedev328ac822013-04-05 15:25:11 +0300335 if (boot_into_ffbm) {
336 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700337 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800338 /* reduce kernel console messages to speed-up boot */
339 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800340 } else if (boot_reason_alarm) {
341 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800342 } else if ((target_build_variant_user() || device.charger_screen_enabled)
343 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700344 pause_at_bootup = 1;
345 cmdline_len += strlen(battchg_pause);
346 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800347
Shashank Mittalcd98d472011-08-02 14:29:24 -0700348 if(target_use_signed_kernel() && auth_kernel_img) {
349 cmdline_len += strlen(auth_kernel);
350 }
351
Joonwoo Park61112782013-10-02 19:50:39 -0700352 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
353 "system",
354 target_boot_params,
355 sizeof(target_boot_params)) == 0) {
356 have_target_boot_params = 1;
357 cmdline_len += strlen(target_boot_params);
358 }
359
Ajay Dudanid04110c2011-01-17 23:55:07 -0800360 /* Determine correct androidboot.baseband to use */
361 switch(target_baseband())
362 {
363 case BASEBAND_APQ:
364 cmdline_len += strlen(baseband_apq);
365 break;
366
367 case BASEBAND_MSM:
368 cmdline_len += strlen(baseband_msm);
369 break;
370
371 case BASEBAND_CSFB:
372 cmdline_len += strlen(baseband_csfb);
373 break;
374
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800375 case BASEBAND_SVLTE2A:
376 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800377 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700378
379 case BASEBAND_MDM:
380 cmdline_len += strlen(baseband_mdm);
381 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700382
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800383 case BASEBAND_MDM2:
384 cmdline_len += strlen(baseband_mdm2);
385 break;
386
Amol Jadi5c61a952012-05-04 17:05:35 -0700387 case BASEBAND_SGLTE:
388 cmdline_len += strlen(baseband_sglte);
389 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530390
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800391 case BASEBAND_SGLTE2:
392 cmdline_len += strlen(baseband_sglte2);
393 break;
394
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530395 case BASEBAND_DSDA:
396 cmdline_len += strlen(baseband_dsda);
397 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800398
399 case BASEBAND_DSDA2:
400 cmdline_len += strlen(baseband_dsda2);
401 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800402 }
403
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800404 if (cmdline) {
405 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530406 target_display_panel_node(display_panel_buf,
407 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800408 strlen(display_panel_buf)) {
409 cmdline_len += strlen(display_panel_buf);
410 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700411 }
412
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800413 if (target_warm_boot()) {
414 warm_boot = true;
415 cmdline_len += strlen(warmboot_cmdline);
416 }
417
David Ng183a7422009-12-07 14:55:21 -0800418 if (cmdline_len > 0) {
419 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800420 unsigned char *dst;
421
422 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
423 ASSERT(cmdline_final != NULL);
424 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700425
Amol Jadi168b7712012-03-06 16:15:00 -0800426 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800427 if (have_cmdline) {
428 src = cmdline;
429 while ((*dst++ = *src++));
430 }
431 if (target_is_emmc_boot()) {
432 src = emmc_cmdline;
433 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700434 have_cmdline = 1;
435 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800436#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700437 src = boot_dev_buf;
438 if (have_cmdline) --dst;
439 while ((*dst++ = *src++));
440#endif
David Ngf773dde2010-07-26 19:55:08 -0700441 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800442
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700443#if VERIFIED_BOOT
444 src = verified_state;
445 if(have_cmdline) --dst;
446 have_cmdline = 1;
447 while ((*dst++ = *src++));
448 src = vbsn[boot_state].name;
449 if(have_cmdline) --dst;
450 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700451
452 src = verity_mode;
453 if(have_cmdline) --dst;
454 while ((*dst++ = *src++));
455 src = vbvm[device.verity_mode].name;
456 if(have_cmdline) -- dst;
457 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700458#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800459 src = usb_sn_cmdline;
460 if (have_cmdline) --dst;
461 have_cmdline = 1;
462 while ((*dst++ = *src++));
463 src = sn_buf;
464 if (have_cmdline) --dst;
465 have_cmdline = 1;
466 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800467 if (warm_boot) {
468 if (have_cmdline) --dst;
469 src = warmboot_cmdline;
470 while ((*dst++ = *src++));
471 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800472
Pavel Nedev5614d222013-06-17 18:01:02 +0300473 if (boot_into_recovery && gpt_exists) {
474 src = secondary_gpt_enable;
475 if (have_cmdline) --dst;
476 while ((*dst++ = *src++));
477 }
478
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200479 if (is_mdtp_activated) {
480 src = mdtp_activated_flag;
481 if (have_cmdline) --dst;
482 while ((*dst++ = *src++));
483 }
484
Pavel Nedev328ac822013-04-05 15:25:11 +0300485 if (boot_into_ffbm) {
486 src = androidboot_mode;
487 if (have_cmdline) --dst;
488 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700489 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300490 if (have_cmdline) --dst;
491 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800492 src = loglevel;
493 if (have_cmdline) --dst;
494 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800495 } else if (boot_reason_alarm) {
496 src = alarmboot_cmdline;
497 if (have_cmdline) --dst;
498 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300499 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700500 src = battchg_pause;
501 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800502 while ((*dst++ = *src++));
503 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800504
Shashank Mittalcd98d472011-08-02 14:29:24 -0700505 if(target_use_signed_kernel() && auth_kernel_img) {
506 src = auth_kernel;
507 if (have_cmdline) --dst;
508 while ((*dst++ = *src++));
509 }
510
Ajay Dudanid04110c2011-01-17 23:55:07 -0800511 switch(target_baseband())
512 {
513 case BASEBAND_APQ:
514 src = baseband_apq;
515 if (have_cmdline) --dst;
516 while ((*dst++ = *src++));
517 break;
518
519 case BASEBAND_MSM:
520 src = baseband_msm;
521 if (have_cmdline) --dst;
522 while ((*dst++ = *src++));
523 break;
524
525 case BASEBAND_CSFB:
526 src = baseband_csfb;
527 if (have_cmdline) --dst;
528 while ((*dst++ = *src++));
529 break;
530
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800531 case BASEBAND_SVLTE2A:
532 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800533 if (have_cmdline) --dst;
534 while ((*dst++ = *src++));
535 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700536
537 case BASEBAND_MDM:
538 src = baseband_mdm;
539 if (have_cmdline) --dst;
540 while ((*dst++ = *src++));
541 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700542
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800543 case BASEBAND_MDM2:
544 src = baseband_mdm2;
545 if (have_cmdline) --dst;
546 while ((*dst++ = *src++));
547 break;
548
Amol Jadi5c61a952012-05-04 17:05:35 -0700549 case BASEBAND_SGLTE:
550 src = baseband_sglte;
551 if (have_cmdline) --dst;
552 while ((*dst++ = *src++));
553 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530554
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800555 case BASEBAND_SGLTE2:
556 src = baseband_sglte2;
557 if (have_cmdline) --dst;
558 while ((*dst++ = *src++));
559 break;
560
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530561 case BASEBAND_DSDA:
562 src = baseband_dsda;
563 if (have_cmdline) --dst;
564 while ((*dst++ = *src++));
565 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800566
567 case BASEBAND_DSDA2:
568 src = baseband_dsda2;
569 if (have_cmdline) --dst;
570 while ((*dst++ = *src++));
571 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800572 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700573
574 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700575 src = display_panel_buf;
576 if (have_cmdline) --dst;
577 while ((*dst++ = *src++));
578 }
Joonwoo Park61112782013-10-02 19:50:39 -0700579
580 if (have_target_boot_params) {
581 if (have_cmdline) --dst;
582 src = target_boot_params;
583 while ((*dst++ = *src++));
584 }
Neeti Desaie245d492012-06-01 12:52:13 -0700585 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700586
587
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700588 if (boot_dev_buf)
589 free(boot_dev_buf);
590
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800591 if (cmdline_final)
592 dprintf(INFO, "cmdline: %s\n", cmdline_final);
593 else
594 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700595 return cmdline_final;
596}
597
598unsigned *atag_core(unsigned *ptr)
599{
600 /* CORE */
601 *ptr++ = 2;
602 *ptr++ = 0x54410001;
603
604 return ptr;
605
606}
607
608unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
609 unsigned ramdisk_size)
610{
611 if (ramdisk_size) {
612 *ptr++ = 4;
613 *ptr++ = 0x54420005;
614 *ptr++ = (unsigned)ramdisk;
615 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800616 }
617
Neeti Desaie245d492012-06-01 12:52:13 -0700618 return ptr;
619}
620
621unsigned *atag_ptable(unsigned **ptr_addr)
622{
623 int i;
624 struct ptable *ptable;
625
626 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700627 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
628 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700629 *(*ptr_addr)++ = 0x4d534d70;
630 for (i = 0; i < ptable->count; ++i)
631 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
632 }
633
634 return (*ptr_addr);
635}
636
637unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
638{
639 int cmdline_length = 0;
640 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700641 char *dest;
642
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800643 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700644 n = (cmdline_length + 4) & (~3);
645
646 *ptr++ = (n / 4) + 2;
647 *ptr++ = 0x54410009;
648 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800649 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700650 ptr += (n / 4);
651
652 return ptr;
653}
654
655unsigned *atag_end(unsigned *ptr)
656{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800657 /* END */
658 *ptr++ = 0;
659 *ptr++ = 0;
660
Neeti Desaie245d492012-06-01 12:52:13 -0700661 return ptr;
662}
663
664void generate_atags(unsigned *ptr, const char *cmdline,
665 void *ramdisk, unsigned ramdisk_size)
666{
667
668 ptr = atag_core(ptr);
669 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
670 ptr = target_atag_mem(ptr);
671
672 /* Skip NAND partition ATAGS for eMMC boot */
673 if (!target_is_emmc_boot()){
674 ptr = atag_ptable(&ptr);
675 }
676
677 ptr = atag_cmdline(ptr, cmdline);
678 ptr = atag_end(ptr);
679}
680
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700681typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700682void boot_linux(void *kernel, unsigned *tags,
683 const char *cmdline, unsigned machtype,
684 void *ramdisk, unsigned ramdisk_size)
685{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800686 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800687#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700688 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800689#endif
690
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700691 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800692 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800693 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800694
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530695 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700696
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800697 final_cmdline = update_cmdline((const char*)cmdline);
698
Neeti Desai17379b82012-06-04 18:42:53 -0700699#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800700 dprintf(INFO, "Updating device tree: start\n");
701
Neeti Desai17379b82012-06-04 18:42:53 -0700702 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530703 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700704 if(ret)
705 {
706 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
707 ASSERT(0);
708 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800709 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700710#else
Neeti Desaie245d492012-06-01 12:52:13 -0700711 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800712 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700713#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700714
Maria Yu52254c02014-07-04 16:14:54 +0800715 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700716
717#if VERIFIED_BOOT
718 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700719 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700720 {
721 dprintf(INFO, "Failed to write protect dev info\n");
722 ASSERT(0);
723 }
724#endif
725
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700726 /* Perform target specific cleanup */
727 target_uninit();
728
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800729 /* Turn off splash screen if enabled */
730#if DISPLAY_SPLASH_SCREEN
731 target_display_shutdown();
732#endif
733
734
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800735 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530736 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800737
738 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700739
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +0530740 /* Initialise wdog to catch early kernel crashes */
741#if WDOG_SUPPORT
742 msm_wdog_init();
743#endif
Amol Jadi4421e652011-06-16 15:00:48 -0700744 /* do any platform specific cleanup before kernel entry */
745 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700746
Brian Swetland9c4c0752009-01-25 16:23:50 -0800747 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700748
Amol Jadi504f9fe2012-08-16 13:56:48 -0700749#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800750 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700751#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700752 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800753
754 if (IS_ARM64(kptr))
755 /* Jump to a 64bit kernel */
756 scm_elexec_call((paddr_t)kernel, tags_phys);
757 else
758 /* Jump to a 32bit kernel */
759 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800760}
761
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700762/* Function to check if the memory address range falls within the aboot
763 * boundaries.
764 * start: Start of the memory region
765 * size: Size of the memory region
766 */
767int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
768{
769 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800770 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700771 return -1;
772
773 /* Check for memory overlap. */
774 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
775 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700776 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700777 return 0;
778 else
779 return -1;
780}
781
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800782#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800783
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800784BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800785#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800786BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800787#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800788
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700789static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
790{
791 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800792
793#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800794#if IMAGE_VERIF_ALGO_SHA1
795 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
796#else
797 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
798#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800799#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700800
801 /* Assume device is rooted at this time. */
802 device.is_tampered = 1;
803
804 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
805
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700806#if VERIFIED_BOOT
807 if(boot_into_recovery)
808 {
809 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530810 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700811 }
812 else
813 {
814 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530815 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700816 }
817 boot_verify_print_state();
818#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700819 ret = image_verify((unsigned char *)bootimg_addr,
820 (unsigned char *)(bootimg_addr + bootimg_size),
821 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800822 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700823#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700824 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
825
826 if (ret)
827 {
828 /* Authorized kernel */
829 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700830 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700831 }
832
Amit Blay4aa292f2015-04-28 21:55:59 +0300833#ifdef MDTP_SUPPORT
834 {
835 /* Verify MDTP lock.
836 * For boot & recovery partitions, use aboot's verification result.
837 */
838 mdtp_ext_partition_verification_t ext_partition;
839 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
840 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
841 ext_partition.page_size = 0; /* Not needed since already validated */
842 ext_partition.image_addr = 0; /* Not needed since already validated */
843 ext_partition.image_size = 0; /* Not needed since already validated */
844 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
845 mdtp_fwlock_verify_lock(&ext_partition);
846 }
847#endif /* MDTP_SUPPORT */
848
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700849#if USE_PCOM_SECBOOT
850 set_tamper_flag(device.is_tampered);
851#endif
852
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700853#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700854 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700855 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700856 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800857#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +0800858 display_bootverify_menu_thread(DISPLAY_MENU_RED);
lijuanga40d6302015-07-20 20:10:13 +0800859 wait_for_users_action();
860#else
861 dprintf(CRITICAL,
862 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
863 mdelay(5000);
864#endif
865
866 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700867 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800868#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +0800869 display_bootverify_menu_thread(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800870 wait_for_users_action();
871#else
872 dprintf(CRITICAL,
873 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
874 mdelay(5000);
875#endif
876 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700877 default:
lijuanga40d6302015-07-20 20:10:13 +0800878 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700879 }
880#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700881#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530882 if(device.is_tampered)
883 {
884 write_device_info_mmc(&device);
885 #ifdef TZ_TAMPER_FUSE
886 set_tamper_fuse_cmd();
887 #endif
888 #ifdef ASSERT_ON_TAMPER
889 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
890 ASSERT(0);
891 #endif
892 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700893#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700894}
895
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530896static bool check_format_bit()
897{
898 bool ret = false;
899 int index;
900 uint64_t offset;
901 struct boot_selection_info *in = NULL;
902 char *buf = NULL;
903
904 index = partition_get_index("bootselect");
905 if (index == INVALID_PTN)
906 {
907 dprintf(INFO, "Unable to locate /bootselect partition\n");
908 return ret;
909 }
910 offset = partition_get_offset(index);
911 if(!offset)
912 {
913 dprintf(INFO, "partition /bootselect doesn't exist\n");
914 return ret;
915 }
916 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
917 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530918 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530919 {
920 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
921 free(buf);
922 return ret;
923 }
924 in = (struct boot_selection_info *) buf;
925 if ((in->signature == BOOTSELECT_SIGNATURE) &&
926 (in->version == BOOTSELECT_VERSION)) {
927 if ((in->state_info & BOOTSELECT_FORMAT) &&
928 !(in->state_info & BOOTSELECT_FACTORY))
929 ret = true;
930 } else {
931 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
932 in->signature, in->version);
933 ASSERT(0);
934 }
935 free(buf);
936 return ret;
937}
938
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700939void boot_verifier_init()
940{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700941 uint32_t boot_state;
942 /* Check if device unlock */
943 if(device.is_unlocked)
944 {
945 boot_verify_send_event(DEV_UNLOCK);
946 boot_verify_print_state();
947 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
948 return;
949 }
950 else
951 {
952 boot_verify_send_event(BOOT_INIT);
953 }
954
955 /* Initialize keystore */
956 boot_state = boot_verify_keystore_init();
957 if(boot_state == YELLOW)
958 {
959 boot_verify_print_state();
960 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
961 }
962}
963
Shashank Mittal23b8f422010-04-16 19:27:21 -0700964int boot_linux_from_mmc(void)
965{
966 struct boot_img_hdr *hdr = (void*) buf;
967 struct boot_img_hdr *uhdr;
968 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700969 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700970 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700971 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700972
Shashank Mittalcd98d472011-08-02 14:29:24 -0700973 unsigned char *image_addr = 0;
974 unsigned kernel_actual;
975 unsigned ramdisk_actual;
976 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700977 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700978
Matthew Qin271927e2015-03-31 22:07:07 -0400979 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +0800980 unsigned int out_len = 0;
981 unsigned int out_avai_len = 0;
982 unsigned char *out_addr = NULL;
983 uint32_t dtb_offset = 0;
984 unsigned char *kernel_start_addr = NULL;
985 unsigned int kernel_size = 0;
986 int rc;
987
Neeti Desai465491e2012-07-31 12:53:35 -0700988#if DEVICE_TREE
989 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700990 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700991 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800992 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700993 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -0400994 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -0700995#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +0800996 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800997
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530998 if (check_format_bit())
999 boot_into_recovery = 1;
1000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001001 if (!boot_into_recovery) {
1002 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1003 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1004 if (rcode <= 0) {
1005 boot_into_ffbm = false;
1006 if (rcode < 0)
1007 dprintf(CRITICAL,"failed to get ffbm cookie");
1008 } else
1009 boot_into_ffbm = true;
1010 } else
1011 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001012 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1013 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1014 dprintf(INFO, "Unified boot method!\n");
1015 hdr = uhdr;
1016 goto unified_boot;
1017 }
Greg Griscod6250552011-06-29 14:40:23 -07001018 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001019 index = partition_get_index("boot");
1020 ptn = partition_get_offset(index);
1021 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001022 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1023 return -1;
1024 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001025 }
1026 else {
1027 index = partition_get_index("recovery");
1028 ptn = partition_get_offset(index);
1029 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001030 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1031 return -1;
1032 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001033 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001034 /* Set Lun for boot & recovery partitions */
1035 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001036
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301037 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001038 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1039 return -1;
1040 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001041
1042 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001043 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001044 return -1;
1045 }
1046
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001047 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301048
1049 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1050 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1051 return -1;
1052 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001053 page_size = hdr->page_size;
1054 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001055 }
1056
Matthew Qin49e51fa2015-02-09 10:40:45 +08001057 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1058 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001059
Matthew Qin49e51fa2015-02-09 10:40:45 +08001060 image_addr = (unsigned char *)target_get_scratch_address();
1061
1062#if DEVICE_TREE
1063 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1064 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1065#else
1066 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1067#endif
1068
1069#if VERIFIED_BOOT
1070 boot_verifier_init();
1071#endif
1072
1073 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
1074 {
1075 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1076 return -1;
1077 }
1078
Matthew Qinbb7923d2015-02-09 10:56:09 +08001079 /*
1080 * Update loading flow of bootimage to support compressed/uncompressed
1081 * bootimage on both 64bit and 32bit platform.
1082 * 1. Load bootimage from emmc partition onto DDR.
1083 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1084 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1085 * platform accordingly.
1086 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1087 */
1088
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001089 dprintf(INFO, "Loading (%s) image (%d): start\n",
1090 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001091 bs_set_timestamp(BS_KERNEL_LOAD_START);
1092
Matthew Qinbb7923d2015-02-09 10:56:09 +08001093 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001094 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1095 {
1096 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1097 return -1;
1098 }
1099
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001100 dprintf(INFO, "Loading (%s) image (%d): done\n",
1101 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1102
Matthew Qin49e51fa2015-02-09 10:40:45 +08001103 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1104
1105 /* Authenticate Kernel */
1106 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1107 (int) target_use_signed_kernel(),
1108 device.is_unlocked,
1109 device.is_tampered);
1110
1111 if(target_use_signed_kernel() && (!device.is_unlocked))
1112 {
1113 offset = imagesize_actual;
1114 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1115 {
1116 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1117 return -1;
1118 }
1119
1120 /* Read signature */
1121 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1122 {
1123 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1124 return -1;
1125 }
1126
1127 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
1128 } else {
1129 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1130 #ifdef TZ_SAVE_KERNEL_HASH
1131 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1132 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001133
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001134#if VERIFIED_BOOT
1135 if(boot_verify_get_state() == ORANGE)
1136 {
lijuanga40d6302015-07-20 20:10:13 +08001137#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +08001138 display_bootverify_menu_thread(DISPLAY_MENU_ORANGE);
lijuanga40d6302015-07-20 20:10:13 +08001139 wait_for_users_action();
1140#else
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001141 dprintf(CRITICAL,
lijuanga40d6302015-07-20 20:10:13 +08001142 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001143 mdelay(5000);
lijuanga40d6302015-07-20 20:10:13 +08001144#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001145 }
1146#endif
1147
Amit Blay4aa292f2015-04-28 21:55:59 +03001148#ifdef MDTP_SUPPORT
1149 {
1150 /* Verify MDTP lock.
1151 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1152 * since verification was skipped in aboot. The signature is not part of the loaded image.
1153 */
1154 mdtp_ext_partition_verification_t ext_partition;
1155 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1156 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1157 ext_partition.page_size = page_size;
1158 ext_partition.image_addr = (uint32)image_addr;
1159 ext_partition.image_size = imagesize_actual;
1160 ext_partition.sig_avail = FALSE;
1161 mdtp_fwlock_verify_lock(&ext_partition);
1162 }
1163#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001164 }
1165
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001166#if VERIFIED_BOOT
1167 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001168 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001169 ASSERT(0);
1170#endif
1171
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001172 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001173 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1174 * If not, continue booting.
1175 */
1176 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1177 {
1178 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1179 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001180 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001181 rc = decompress((unsigned char *)(image_addr + page_size),
1182 hdr->kernel_size, out_addr, out_avai_len,
1183 &dtb_offset, &out_len);
1184 if (rc)
1185 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001186 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001187 ASSERT(0);
1188 }
1189
Matthew Qin0b15b322015-05-19 05:20:54 -04001190 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001191 kptr = (struct kernel64_hdr *)out_addr;
1192 kernel_start_addr = out_addr;
1193 kernel_size = out_len;
1194 } else {
1195 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1196 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1197 kernel_size = hdr->kernel_size;
1198 }
1199
1200 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001201 * Update the kernel/ramdisk/tags address if the boot image header
1202 * has default values, these default values come from mkbootimg when
1203 * the boot image is flashed using fastboot flash:raw
1204 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001205 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001206
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001207 /* Get virtual addresses since the hdr saves physical addresses. */
1208 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1209 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1210 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001211
Matthew Qinbb7923d2015-02-09 10:56:09 +08001212 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001213 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001214 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001215 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1216 {
1217 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1218 return -1;
1219 }
1220
1221#ifndef DEVICE_TREE
1222 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1223 {
1224 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1225 return -1;
1226 }
1227#endif
1228
Matthew Qin49e51fa2015-02-09 10:40:45 +08001229 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001230 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001231 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001232
Matthew Qin49e51fa2015-02-09 10:40:45 +08001233 #if DEVICE_TREE
1234 if(hdr->dt_size) {
1235 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1236 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001237
Matthew Qin49e51fa2015-02-09 10:40:45 +08001238 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1239 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1240 return -1;
1241 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001242
Matthew Qin49e51fa2015-02-09 10:40:45 +08001243 /* Find index of device tree within device tree table */
1244 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1245 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1246 return -1;
1247 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001248
Matthew Qin271927e2015-03-31 22:07:07 -04001249 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1250 {
1251 unsigned int compressed_size = 0;
1252 out_addr += out_len;
1253 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001254 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001255 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1256 dt_entry.size, out_addr, out_avai_len,
1257 &compressed_size, &dtb_size);
1258 if (rc)
1259 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001260 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001261 ASSERT(0);
1262 }
1263
Matthew Qin0b15b322015-05-19 05:20:54 -04001264 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001265 best_match_dt_addr = out_addr;
1266 } else {
1267 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1268 dtb_size = dt_entry.size;
1269 }
1270
Matthew Qin49e51fa2015-02-09 10:40:45 +08001271 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001272 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001273 {
1274 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1275 return -1;
1276 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001277
Matthew Qin271927e2015-03-31 22:07:07 -04001278 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001279 } else {
1280 /* Validate the tags_addr */
1281 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001282 {
1283 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1284 return -1;
1285 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001286 /*
1287 * If appended dev tree is found, update the atags with
1288 * memory address to the DTB appended location on RAM.
1289 * Else update with the atags address in the kernel header
1290 */
1291 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001292 dtb = dev_tree_appended((void*)(image_addr + page_size),
1293 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001294 (void *)hdr->tags_addr);
1295 if (!dtb) {
1296 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001297 return -1;
1298 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001299 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001300 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001301
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001302 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1303 target_load_ssd_keystore();
1304
Shashank Mittal23b8f422010-04-16 19:27:21 -07001305unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001306
Dima Zavin77e41f32013-03-06 16:10:43 -08001307 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001308 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001309 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1310
1311 return 0;
1312}
1313
Dima Zavin214cc642009-01-26 11:16:21 -08001314int boot_linux_from_flash(void)
1315{
1316 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001317 struct ptentry *ptn;
1318 struct ptable *ptable;
1319 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001320
Shashank Mittalcd98d472011-08-02 14:29:24 -07001321 unsigned char *image_addr = 0;
1322 unsigned kernel_actual;
1323 unsigned ramdisk_actual;
1324 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001325 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001326
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001327#if DEVICE_TREE
1328 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001329 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001330 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001331 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001332#endif
1333
David Ng183a7422009-12-07 14:55:21 -08001334 if (target_is_emmc_boot()) {
1335 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1336 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1337 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1338 return -1;
1339 }
1340 goto continue_boot;
1341 }
1342
Dima Zavin214cc642009-01-26 11:16:21 -08001343 ptable = flash_get_ptable();
1344 if (ptable == NULL) {
1345 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1346 return -1;
1347 }
1348
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001349 if(!boot_into_recovery)
1350 {
1351 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001352
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001353 if (ptn == NULL) {
1354 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1355 return -1;
1356 }
1357 }
1358 else
1359 {
1360 ptn = ptable_find(ptable, "recovery");
1361 if (ptn == NULL) {
1362 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1363 return -1;
1364 }
Dima Zavin214cc642009-01-26 11:16:21 -08001365 }
1366
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001367 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001368 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1369 return -1;
1370 }
Dima Zavin214cc642009-01-26 11:16:21 -08001371
1372 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001373 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001374 return -1;
1375 }
1376
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001377 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001378 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 -08001379 return -1;
1380 }
1381
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001382 /*
1383 * Update the kernel/ramdisk/tags address if the boot image header
1384 * has default values, these default values come from mkbootimg when
1385 * the boot image is flashed using fastboot flash:raw
1386 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001387 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001388
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001389 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001390 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1391 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1392 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1393
1394 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1395 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1396
1397 /* Check if the addresses in the header are valid. */
1398 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1399 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1400 {
1401 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1402 return -1;
1403 }
1404
1405#ifndef DEVICE_TREE
1406 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1407 {
1408 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1409 return -1;
1410 }
1411#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001412
Shashank Mittalcd98d472011-08-02 14:29:24 -07001413 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001414 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001415 {
1416 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001417 offset = 0;
1418
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001419#if DEVICE_TREE
1420 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1421 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001422
1423 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1424 {
1425 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1426 return -1;
1427 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001428#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001429 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001430#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001431
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001432 dprintf(INFO, "Loading (%s) image (%d): start\n",
1433 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001434 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001435
Shashank Mittalcd98d472011-08-02 14:29:24 -07001436 /* Read image without signature */
1437 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1438 {
1439 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1440 return -1;
1441 }
Dima Zavin214cc642009-01-26 11:16:21 -08001442
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001443 dprintf(INFO, "Loading (%s) image (%d): done\n",
1444 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001445 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001446
Shashank Mittalcd98d472011-08-02 14:29:24 -07001447 offset = imagesize_actual;
1448 /* Read signature */
1449 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1450 {
1451 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001452 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001453 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001454
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301455 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001456
1457 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001458 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1459 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001460#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001461 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001462 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001463 {
1464 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1465 return -1;
1466 }
1467
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001468 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1469#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001470
1471 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001472 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001473 {
1474 write_device_info_flash(&device);
1475 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301476#if USE_PCOM_SECBOOT
1477 set_tamper_flag(device.is_tampered);
1478#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001479 }
1480 else
1481 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001482 offset = page_size;
1483
Amol Jadib6be5c12012-11-14 13:39:51 -08001484 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1485 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1486 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1487
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001488 dprintf(INFO, "Loading (%s) image (%d): start\n",
1489 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1490
Amol Jadi492d5a52013-03-15 16:12:34 -07001491 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001492
1493 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001494 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1495 return -1;
1496 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001497 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001498
Amol Jadib6be5c12012-11-14 13:39:51 -08001499 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001500 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1501 return -1;
1502 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001503 offset += ramdisk_actual;
1504
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001505 dprintf(INFO, "Loading (%s) image (%d): done\n",
1506 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1507
Amol Jadi492d5a52013-03-15 16:12:34 -07001508 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001509
1510 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001511 offset += second_actual;
1512 /* Second image loading not implemented. */
1513 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001514 }
1515
1516#if DEVICE_TREE
1517 if(hdr->dt_size != 0) {
1518
1519 /* Read the device tree table into buffer */
1520 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1521 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1522 return -1;
1523 }
1524
1525 table = (struct dt_table*) dt_buf;
1526
Deepa Dinamani19648b42013-09-05 17:05:55 -07001527 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001528 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1529 return -1;
1530 }
1531
Deepa Dinamani19648b42013-09-05 17:05:55 -07001532 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1533 if (!table)
1534 return -1;
1535
1536 /* Read the entire device tree table into buffer */
1537 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1538 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1539 return -1;
1540 }
1541
1542
Joel Kingaa335dc2013-06-03 16:11:08 -07001543 /* Find index of device tree within device tree table */
1544 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001545 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1546 return -1;
1547 }
1548
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001549 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001550 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001551 {
1552 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1553 return -1;
1554 }
1555
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001556 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001557 if(flash_read(ptn, offset + dt_entry.offset,
1558 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001559 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1560 return -1;
1561 }
1562 }
1563#endif
1564
Shashank Mittalcd98d472011-08-02 14:29:24 -07001565 }
David Ng183a7422009-12-07 14:55:21 -08001566continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001567
Dima Zavin214cc642009-01-26 11:16:21 -08001568 /* TODO: create/pass atags to kernel */
1569
Ajay Dudanie28a6072011-07-01 13:59:46 -07001570 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001571 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001572 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1573
1574 return 0;
1575}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001576
Shashank Mittal162244e2011-08-08 19:01:25 -07001577void write_device_info_mmc(device_info *dev)
1578{
Shashank Mittal162244e2011-08-08 19:01:25 -07001579 unsigned long long ptn = 0;
1580 unsigned long long size;
1581 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001582 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001583 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001584 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001585
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001586 if (devinfo_present)
1587 index = partition_get_index("devinfo");
1588 else
1589 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001590
Shashank Mittal162244e2011-08-08 19:01:25 -07001591 ptn = partition_get_offset(index);
1592 if(ptn == 0)
1593 {
1594 return;
1595 }
1596
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001597 lun = partition_get_lun(index);
1598 mmc_set_lun(lun);
1599
Shashank Mittal162244e2011-08-08 19:01:25 -07001600 size = partition_get_size(index);
1601
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001602 blocksize = mmc_get_device_blocksize();
1603
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001604 if (devinfo_present)
1605 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1606 else
1607 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1608 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001609 {
1610 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001611 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001612 }
1613}
1614
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001615void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001616{
Shashank Mittal162244e2011-08-08 19:01:25 -07001617 unsigned long long ptn = 0;
1618 unsigned long long size;
1619 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001620 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001621 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001622
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001623 if ((index = partition_get_index("devinfo")) < 0)
1624 {
1625 devinfo_present = false;
1626 index = partition_get_index("aboot");
1627 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001628
Shashank Mittal162244e2011-08-08 19:01:25 -07001629 ptn = partition_get_offset(index);
1630 if(ptn == 0)
1631 {
1632 return;
1633 }
1634
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001635 mmc_set_lun(partition_get_lun(index));
1636
Shashank Mittal162244e2011-08-08 19:01:25 -07001637 size = partition_get_size(index);
1638
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001639 blocksize = mmc_get_device_blocksize();
1640
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001641 if (devinfo_present)
1642 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1643 else
1644 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1645 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001646 {
1647 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001648 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001649 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001650}
1651
1652void write_device_info_flash(device_info *dev)
1653{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001654 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001655 struct ptentry *ptn;
1656 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001657 if(info == NULL)
1658 {
1659 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1660 ASSERT(0);
1661 }
1662 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001663 ptable = flash_get_ptable();
1664 if (ptable == NULL)
1665 {
1666 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1667 return;
1668 }
1669
1670 ptn = ptable_find(ptable, "devinfo");
1671 if (ptn == NULL)
1672 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001673 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001674 return;
1675 }
1676
1677 memcpy(info, dev, sizeof(device_info));
1678
1679 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1680 {
1681 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1682 return;
1683 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001684 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001685}
1686
vijay kumarc65876c2015-04-24 13:29:16 +05301687static int read_allow_oem_unlock(device_info *dev)
1688{
vijay kumarc65876c2015-04-24 13:29:16 +05301689 unsigned offset;
1690 int index;
1691 unsigned long long ptn;
1692 unsigned long long ptn_size;
1693 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001694 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301695
vijay kumarca2e6812015-07-08 20:28:25 +05301696 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301697 if (index == INVALID_PTN)
1698 {
vijay kumarca2e6812015-07-08 20:28:25 +05301699 index = partition_get_index(frp_ptns[1]);
1700 if (index == INVALID_PTN)
1701 {
1702 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1703 return -1;
1704 }
vijay kumarc65876c2015-04-24 13:29:16 +05301705 }
1706
1707 ptn = partition_get_offset(index);
1708 ptn_size = partition_get_size(index);
1709 offset = ptn_size - blocksize;
1710
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001711 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301712 {
1713 dprintf(CRITICAL, "Reading MMC failed\n");
1714 return -1;
1715 }
1716
1717 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1718 is_allow_unlock = buf[blocksize-1] & 0x01;
1719 return 0;
1720}
1721
1722static int write_allow_oem_unlock(bool allow_unlock)
1723{
vijay kumarc65876c2015-04-24 13:29:16 +05301724 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301725 int index;
1726 unsigned long long ptn;
1727 unsigned long long ptn_size;
1728 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001729 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301730
vijay kumarca2e6812015-07-08 20:28:25 +05301731 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301732 if (index == INVALID_PTN)
1733 {
vijay kumarca2e6812015-07-08 20:28:25 +05301734 index = partition_get_index(frp_ptns[1]);
1735 if (index == INVALID_PTN)
1736 {
1737 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1738 return -1;
1739 }
vijay kumarc65876c2015-04-24 13:29:16 +05301740 }
1741
1742 ptn = partition_get_offset(index);
1743 ptn_size = partition_get_size(index);
1744 offset = ptn_size - blocksize;
1745
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001746 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301747 {
1748 dprintf(CRITICAL, "Reading MMC failed\n");
1749 return -1;
1750 }
1751
1752 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1753 buf[blocksize-1] = allow_unlock;
1754 if (mmc_write(ptn + offset, blocksize, buf))
1755 {
1756 dprintf(CRITICAL, "Writing MMC failed\n");
1757 return -1;
1758 }
1759
1760 return 0;
1761}
1762
Shashank Mittal162244e2011-08-08 19:01:25 -07001763void read_device_info_flash(device_info *dev)
1764{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001765 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001766 struct ptentry *ptn;
1767 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001768 if(info == NULL)
1769 {
1770 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1771 ASSERT(0);
1772 }
1773 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001774 ptable = flash_get_ptable();
1775 if (ptable == NULL)
1776 {
1777 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1778 return;
1779 }
1780
1781 ptn = ptable_find(ptable, "devinfo");
1782 if (ptn == NULL)
1783 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001784 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001785 return;
1786 }
1787
1788 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1789 {
1790 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1791 return;
1792 }
1793
1794 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1795 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001796 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1797 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001798 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001799 write_device_info_flash(info);
1800 }
1801 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001802 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001803}
1804
1805void write_device_info(device_info *dev)
1806{
1807 if(target_is_emmc_boot())
1808 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001809 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1810 if(info == NULL)
1811 {
1812 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1813 ASSERT(0);
1814 }
1815 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001816 memcpy(info, dev, sizeof(struct device_info));
1817
1818#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001819 if (is_secure_boot_enable()) {
1820 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1821 ASSERT(0);
1822 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001823 else
1824 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001825#else
1826 write_device_info_mmc(info);
1827#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001828 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001829 }
1830 else
1831 {
1832 write_device_info_flash(dev);
1833 }
1834}
1835
1836void read_device_info(device_info *dev)
1837{
1838 if(target_is_emmc_boot())
1839 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001840 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1841 if(info == NULL)
1842 {
1843 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1844 ASSERT(0);
1845 }
1846 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001847
1848#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001849 if (is_secure_boot_enable()) {
1850 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1851 ASSERT(0);
1852 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001853 else
1854 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001855#else
1856 read_device_info_mmc(info);
1857#endif
1858
1859 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1860 {
1861 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08001862 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001863 info->is_unlocked = 0;
lijuang511a2b52015-08-14 20:50:51 +08001864 info->is_unlock_critical = 0;
1865 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001866 info->is_unlocked = 1;
lijuang511a2b52015-08-14 20:50:51 +08001867 info->is_unlock_critical = 1;
1868 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001869 info->is_tampered = 0;
1870 info->charger_screen_enabled = 0;
Sridhar Parasuram287e5062015-08-24 16:17:22 -07001871 info->verity_mode = 1; //enforcing by default
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001872 write_device_info(info);
1873 }
1874 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001875 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001876 }
1877 else
1878 {
1879 read_device_info_flash(dev);
1880 }
1881}
1882
1883void reset_device_info()
1884{
1885 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001886 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001887 write_device_info(&device);
1888}
1889
1890void set_device_root()
1891{
1892 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001893 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001894 write_device_info(&device);
1895}
1896
lijuang4ece1e72015-08-14 21:02:36 +08001897/* set device unlock value
1898 * Must check FRP before call this function
1899 * Need to wipe data when unlock status changed
1900 * type 0: oem unlock
1901 * type 1: unlock critical
1902 * status 0: unlock as false
1903 * status 1: lock as true
1904 */
1905void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08001906{
lijuang4ece1e72015-08-14 21:02:36 +08001907 if (type == UNLOCK)
1908 device.is_unlocked = status;
1909 else if (type == UNLOCK_CRITICAL)
1910 device.is_unlock_critical = status;
1911
1912 write_device_info(&device);
1913}
1914
1915static void set_device_unlock(int type, bool status)
1916{
1917 int is_unlocked = -1;
1918 char response[MAX_RSP_SIZE];
1919
1920 /* check device unlock status if it is as expected */
1921 if (type == UNLOCK)
1922 is_unlocked = device.is_unlocked;
1923 else if (type == UNLOCK_CRITICAL)
1924 is_unlocked = device.is_unlock_critical;
1925
1926 if (is_unlocked == status) {
1927 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
1928 fastboot_info(response);
1929 fastboot_okay("");
1930 return;
1931 }
1932
1933 /* status is true, it means to unlock device */
1934 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08001935 if(!is_allow_unlock) {
1936 fastboot_fail("oem unlock is not allowed");
1937 return;
1938 }
1939
lijuang4ece1e72015-08-14 21:02:36 +08001940#if FBCON_DISPLAY_MSG
1941 display_unlock_menu_thread(type);
1942 fastboot_okay("");
1943 return;
1944#else
1945 if (type == UNLOCK) {
1946 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
1947 return;
1948 }
1949#endif
lijuang21f12f52015-08-22 16:22:19 +08001950 }
lijuang4ece1e72015-08-14 21:02:36 +08001951
1952 set_device_unlock_value(type, status);
1953
1954 /* wipe data */
1955 struct recovery_message msg;
1956
1957 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
1958 write_misc(0, &msg, sizeof(msg));
1959
1960 fastboot_okay("");
1961 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08001962}
1963
lijuang511a2b52015-08-14 20:50:51 +08001964static bool critical_flash_allowed(const char * entry)
1965{
1966 uint32_t i = 0;
1967 if (entry == NULL)
1968 return false;
1969
1970 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
1971 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
1972 return true;
1973 }
1974 return false;
1975}
1976
Amol Jadicb524072012-08-09 16:40:18 -07001977#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001978int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
Amol Jadicb524072012-08-09 16:40:18 -07001979{
1980 uint32 dt_image_offset = 0;
1981 uint32_t n;
1982 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001983 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001984 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001985 unsigned int compressed_size = 0;
1986 unsigned int dtb_size = 0;
1987 unsigned int out_avai_len = 0;
1988 unsigned char *out_addr = NULL;
1989 unsigned char *best_match_dt_addr = NULL;
1990 int rc;
Amol Jadicb524072012-08-09 16:40:18 -07001991
1992 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1993
Amol Jadicb524072012-08-09 16:40:18 -07001994 if(hdr->dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001995 /* add kernel offset */
1996 dt_image_offset += page_size;
1997 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1998 dt_image_offset += n;
1999
2000 /* add ramdisk offset */
2001 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2002 dt_image_offset += n;
2003
2004 /* add second offset */
2005 if(hdr->second_size != 0) {
2006 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2007 dt_image_offset += n;
2008 }
2009
2010 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002011 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002012
Deepa Dinamani19648b42013-09-05 17:05:55 -07002013 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002014 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2015 return -1;
2016 }
Joel Kingaa335dc2013-06-03 16:11:08 -07002017 /* Find index of device tree within device tree table */
2018 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002019 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2020 return -1;
2021 }
2022
Matthew Qin271927e2015-03-31 22:07:07 -04002023 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2024 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2025 {
2026 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2027 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002028 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002029 rc = decompress(best_match_dt_addr,
2030 dt_entry.size, out_addr, out_avai_len,
2031 &compressed_size, &dtb_size);
2032 if (rc)
2033 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002034 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002035 ASSERT(0);
2036 }
2037
Matthew Qin0b15b322015-05-19 05:20:54 -04002038 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002039 best_match_dt_addr = out_addr;
2040 } else {
2041 dtb_size = dt_entry.size;
2042 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002043 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002044 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002045 {
2046 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
2047 return -1;
2048 }
2049
Amol Jadicb524072012-08-09 16:40:18 -07002050 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002051 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002052 } else
2053 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002054
2055 /* Everything looks fine. Return success. */
2056 return 0;
2057}
2058#endif
2059
Brian Swetland9c4c0752009-01-25 16:23:50 -08002060void cmd_boot(const char *arg, void *data, unsigned sz)
2061{
Amit Blay8a510302015-08-17 09:20:01 +03002062#ifdef MDTP_SUPPORT
2063 static bool is_mdtp_activated = 0;
2064#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002065 unsigned kernel_actual;
2066 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002067 uint32_t image_actual;
2068 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002069 uint32_t sig_actual = SIGNATURE_SIZE;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002070 struct boot_img_hdr *hdr = NULL;
2071 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002072 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002073 int ret = 0;
2074 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002075 unsigned int out_len = 0;
2076 unsigned int out_avai_len = 0;
2077 unsigned char *out_addr = NULL;
2078 uint32_t dtb_offset = 0;
2079 unsigned char *kernel_start_addr = NULL;
2080 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002081 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002082
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002083#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002084 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002085 {
2086 fastboot_fail("unlock device to use this command");
2087 return;
2088 }
2089#endif
2090
Brian Swetland9c4c0752009-01-25 16:23:50 -08002091 if (sz < sizeof(hdr)) {
2092 fastboot_fail("invalid bootimage header");
2093 return;
2094 }
2095
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002096 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002097
2098 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002099 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002100
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002101 if(target_is_emmc_boot() && hdr->page_size) {
2102 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002103 page_mask = page_size - 1;
2104 }
2105
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002106 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2107 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002108#if DEVICE_TREE
2109 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2110#endif
2111
2112 image_actual = ADD_OF(page_size, kernel_actual);
2113 image_actual = ADD_OF(image_actual, ramdisk_actual);
2114 image_actual = ADD_OF(image_actual, dt_actual);
2115
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002116 if (target_use_signed_kernel() && (!device.is_unlocked))
2117 image_actual = ADD_OF(image_actual, sig_actual);
2118
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002119 /* sz should have atleast raw boot image */
2120 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002121 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002122 return;
2123 }
2124
2125 /* Verify the boot image
2126 * device & page_size are initialized in aboot_init
2127 */
2128 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002129 /* Pass size excluding signature size, otherwise we would try to
2130 * access signature beyond its length
2131 */
2132 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002133
Amit Blay4aa292f2015-04-28 21:55:59 +03002134#ifdef MDTP_SUPPORT
2135 else
2136 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002137 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002138 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002139
2140 if (!is_mdtp_activated) {
2141 ext_partition.partition = MDTP_PARTITION_NONE;
2142 mdtp_fwlock_verify_lock(&ext_partition);
2143 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002144 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002145
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002146 mdtp_activated(&is_mdtp_activated);
2147 if(is_mdtp_activated){
2148 dprintf(CRITICAL, "fastboot boot command is not available.\n");
2149 return;
2150 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002151#endif /* MDTP_SUPPORT */
2152
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002153 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002154 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2155 * If not, continue booting.
2156 */
2157 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2158 {
2159 out_addr = (unsigned char *)target_get_scratch_address();
2160 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2161 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002162 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002163 ret = decompress((unsigned char *)(ptr + page_size),
2164 hdr->kernel_size, out_addr, out_avai_len,
2165 &dtb_offset, &out_len);
2166 if (ret)
2167 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002168 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002169 ASSERT(0);
2170 }
2171
Matthew Qin0b15b322015-05-19 05:20:54 -04002172 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002173 kptr = (struct kernel64_hdr *)out_addr;
2174 kernel_start_addr = out_addr;
2175 kernel_size = out_len;
2176 } else {
2177 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2178 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2179 kernel_size = hdr->kernel_size;
2180 }
2181
2182 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002183 * Update the kernel/ramdisk/tags address if the boot image header
2184 * has default values, these default values come from mkbootimg when
2185 * the boot image is flashed using fastboot flash:raw
2186 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002187 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002188
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002189 /* Get virtual addresses since the hdr saves physical addresses. */
2190 hdr->kernel_addr = VA(hdr->kernel_addr);
2191 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2192 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002193
Matthew Qinbb7923d2015-02-09 10:56:09 +08002194 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002195 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002196 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002197 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2198 {
2199 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002200 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002201 }
2202
Amol Jadicb524072012-08-09 16:40:18 -07002203#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002204 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002205 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002206 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002207
2208 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002209#else
2210 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2211 {
2212 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002213 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002214 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002215#endif
2216
2217 /* Load ramdisk & kernel */
2218 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002219 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002220
2221#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002222 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2223 {
2224 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2225 return;
2226 }
2227
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002228 /*
2229 * If dtb is not found look for appended DTB in the kernel.
2230 * If appended dev tree is found, update the atags with
2231 * memory address to the DTB appended location on RAM.
2232 * Else update with the atags address in the kernel header
2233 */
2234 if (!dtb_copied) {
2235 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002236 dtb = dev_tree_appended((void*)(ptr + page_size),
2237 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002238 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002239 if (!dtb) {
2240 fastboot_fail("dtb not found");
2241 return;
2242 }
Amol Jadicb524072012-08-09 16:40:18 -07002243 }
2244#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002245
2246 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002247 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002248
Dima Zavin77e41f32013-03-06 16:10:43 -08002249 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002250 (const char*) hdr->cmdline, board_machtype(),
2251 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002252}
2253
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002254void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002255{
2256 struct ptentry *ptn;
2257 struct ptable *ptable;
2258
2259 ptable = flash_get_ptable();
2260 if (ptable == NULL) {
2261 fastboot_fail("partition table doesn't exist");
2262 return;
2263 }
2264
2265 ptn = ptable_find(ptable, arg);
2266 if (ptn == NULL) {
2267 fastboot_fail("unknown partition name");
2268 return;
2269 }
2270
2271 if (flash_erase(ptn)) {
2272 fastboot_fail("failed to erase partition");
2273 return;
2274 }
2275 fastboot_okay("");
2276}
2277
Bikas Gurungd48bd242010-09-04 19:54:32 -07002278
2279void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2280{
2281 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002282 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002283 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002284 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002285
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002286#if VERIFIED_BOOT
2287 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2288 {
2289 if(!device.is_unlocked)
2290 {
2291 fastboot_fail("unlock device to erase keystore");
2292 return;
2293 }
2294 }
2295#endif
2296
Kinson Chikf1a43512011-07-14 11:28:39 -07002297 index = partition_get_index(arg);
2298 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002299 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002300
Kinson Chikf1a43512011-07-14 11:28:39 -07002301 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002302 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002303 return;
2304 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002305
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002306 lun = partition_get_lun(index);
2307 mmc_set_lun(lun);
2308
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002309 if (platform_boot_dev_isemmc())
2310 {
2311 if (mmc_erase_card(ptn, size)) {
2312 fastboot_fail("failed to erase partition\n");
2313 return;
2314 }
2315 } else {
2316 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2317 size = partition_get_size(index);
2318 if (size > DEFAULT_ERASE_SIZE)
2319 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002320
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002321 /* Simple inefficient version of erase. Just writing
2322 0 in first several blocks */
2323 if (mmc_write(ptn , size, (unsigned int *)out)) {
2324 fastboot_fail("failed to erase partition");
2325 return;
2326 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002327 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002328#if VERIFIED_BOOT
2329 if(!(strncmp(arg, "userdata", 8)))
2330 if(send_delete_keys_to_tz())
2331 ASSERT(0);
2332#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002333 fastboot_okay("");
2334}
2335
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002336void cmd_erase(const char *arg, void *data, unsigned sz)
2337{
lijuang65c5a822015-08-29 16:35:36 +08002338#if CHECK_BAT_VOLTAGE
2339 if (!target_battery_soc_ok()) {
2340 fastboot_fail("Warning: battery's capacity is very low\n");
2341 return;
2342 }
2343#endif
2344
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002345#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002346 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002347 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002348 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002349 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002350 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002351 return;
2352 }
2353 }
2354#endif
2355
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002356 if(target_is_emmc_boot())
2357 cmd_erase_mmc(arg, data, sz);
2358 else
2359 cmd_erase_nand(arg, data, sz);
2360}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002361
Ajay Dudani5c761132011-04-07 20:19:04 -07002362void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002363{
2364 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002365 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002366 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002367 char *token = NULL;
2368 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002369 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002370 uint8_t lun = 0;
2371 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002372
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002373 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002374 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002375 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002376 if(token)
2377 {
2378 lun = atoi(token);
2379 mmc_set_lun(lun);
2380 lun_set = true;
2381 }
2382
Mao Jinlong226f33a2014-07-04 17:24:10 +08002383 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002384 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002385 if (!strcmp(pname, "partition"))
2386 {
2387 dprintf(INFO, "Attempt to write partition image.\n");
2388 if (write_partition(sz, (unsigned char *) data)) {
2389 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002390 return;
2391 }
2392 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002393 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002394 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002395#if VERIFIED_BOOT
2396 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2397 {
2398 if(!device.is_unlocked)
2399 {
2400 fastboot_fail("unlock device to flash keystore");
2401 return;
2402 }
2403 if(!boot_verify_validate_keystore((unsigned char *)data))
2404 {
2405 fastboot_fail("image is not a keystore file");
2406 return;
2407 }
2408 }
2409#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002410 index = partition_get_index(pname);
2411 ptn = partition_get_offset(index);
2412 if(ptn == 0) {
2413 fastboot_fail("partition table doesn't exist");
2414 return;
2415 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002416
Mao Jinlong226f33a2014-07-04 17:24:10 +08002417 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2418 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2419 fastboot_fail("image is not a boot image");
2420 return;
2421 }
2422 }
2423
2424 if(!lun_set)
2425 {
2426 lun = partition_get_lun(index);
2427 mmc_set_lun(lun);
2428 }
2429
2430 size = partition_get_size(index);
2431 if (ROUND_TO_PAGE(sz,511) > size) {
2432 fastboot_fail("size too large");
2433 return;
2434 }
2435 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2436 fastboot_fail("flash write failure");
2437 return;
2438 }
Greg Grisco6e754772011-06-23 12:19:39 -07002439 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002440 }
2441 fastboot_okay("");
2442 return;
2443}
2444
Ajay Dudanide984792015-03-02 09:57:41 -08002445void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2446{
2447 int i, images;
2448 meta_header_t *meta_header;
2449 img_header_entry_t *img_header_entry;
2450
2451 meta_header = (meta_header_t*) data;
2452 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2453
2454 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2455
2456 for (i=0; i<images; i++) {
2457
2458 if((img_header_entry[i].ptn_name == NULL) ||
2459 (img_header_entry[i].start_offset == 0) ||
2460 (img_header_entry[i].size == 0))
2461 break;
2462
2463 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2464 (void *) data + img_header_entry[i].start_offset,
2465 img_header_entry[i].size);
2466 }
2467
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002468 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2469 {
2470 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2471 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2472 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2473 }
2474 else
2475 {
2476 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2477 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2478 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2479 }
2480
2481 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002482 fastboot_okay("");
2483 return;
2484}
2485
Ajay Dudani5c761132011-04-07 20:19:04 -07002486void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2487{
2488 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002489 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002490 uint32_t *fill_buf = NULL;
2491 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002492 sparse_header_t *sparse_header;
2493 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002494 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002495 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302496 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002497 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302498 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002499 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302500 /*End of the sparse image address*/
2501 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002502
Kinson Chikf1a43512011-07-14 11:28:39 -07002503 index = partition_get_index(arg);
2504 ptn = partition_get_offset(index);
2505 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002506 fastboot_fail("partition table doesn't exist");
2507 return;
2508 }
2509
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302510 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302511
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002512 lun = partition_get_lun(index);
2513 mmc_set_lun(lun);
2514
vijay kumar800255e2015-04-24 20:26:19 +05302515 if (sz < sizeof(sparse_header_t)) {
2516 fastboot_fail("size too low");
2517 return;
2518 }
2519
Ajay Dudani5c761132011-04-07 20:19:04 -07002520 /* Read and skip over sparse image header */
2521 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302522
vijay kumar1321f342015-03-27 12:13:42 +05302523 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002524 fastboot_fail("size too large");
2525 return;
2526 }
2527
vijay kumarde4fcf62015-04-23 13:05:49 +05302528 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302529
2530 if (data_end < (uint32_t)data) {
2531 fastboot_fail("buffer overreads occured due to invalid sparse header");
2532 return;
2533 }
2534
vijay kumarde4fcf62015-04-23 13:05:49 +05302535 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002536 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302537 fastboot_fail("sparse header size mismatch");
2538 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002539 }
2540
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002541 dprintf (SPEW, "=== Sparse Image Header ===\n");
2542 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2543 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2544 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2545 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2546 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2547 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2548 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2549 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002550
2551 /* Start processing chunks */
2552 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2553 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302554 /* Make sure the total image size does not exceed the partition size */
2555 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2556 fastboot_fail("size too large");
2557 return;
2558 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002559 /* Read and skip over chunk header */
2560 chunk_header = (chunk_header_t *) data;
2561 data += sizeof(chunk_header_t);
2562
vijay kumar800255e2015-04-24 20:26:19 +05302563 if (data_end < (uint32_t)data) {
2564 fastboot_fail("buffer overreads occured due to invalid sparse header");
2565 return;
2566 }
2567
Ajay Dudani5c761132011-04-07 20:19:04 -07002568 dprintf (SPEW, "=== Chunk Header ===\n");
2569 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2570 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2571 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2572
vijay kumar800255e2015-04-24 20:26:19 +05302573 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002574 {
vijay kumar800255e2015-04-24 20:26:19 +05302575 fastboot_fail("chunk header size mismatch");
2576 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002577 }
2578
wufeng.jiang813dc352015-06-02 23:02:46 -04002579 if (!sparse_header->blk_sz ){
2580 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302581 return;
2582 }
2583
wufeng.jiang813dc352015-06-02 23:02:46 -04002584 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2585
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302586 /* Make sure that the chunk size calculated from sparse image does not
2587 * exceed partition size
2588 */
2589 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2590 {
2591 fastboot_fail("Chunk data size exceeds partition size");
2592 return;
2593 }
2594
Ajay Dudani5c761132011-04-07 20:19:04 -07002595 switch (chunk_header->chunk_type)
2596 {
2597 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002598 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002599 chunk_data_sz))
2600 {
2601 fastboot_fail("Bogus chunk size for chunk type Raw");
2602 return;
2603 }
2604
vijay kumar800255e2015-04-24 20:26:19 +05302605 if (data_end < (uint32_t)data + chunk_data_sz) {
2606 fastboot_fail("buffer overreads occured due to invalid sparse header");
2607 return;
2608 }
2609
wufeng.jiang813dc352015-06-02 23:02:46 -04002610 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2611 otherwise it will return in the line above
2612 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002613 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002614 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002615 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002616 {
2617 fastboot_fail("flash write failure");
2618 return;
2619 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302620 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2621 fastboot_fail("Bogus size for RAW chunk type");
2622 return;
2623 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002624 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002625 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002626 break;
2627
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002628 case CHUNK_TYPE_FILL:
2629 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2630 sizeof(uint32_t)))
2631 {
2632 fastboot_fail("Bogus chunk size for chunk type FILL");
2633 return;
2634 }
2635
2636 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2637 if (!fill_buf)
2638 {
2639 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2640 return;
2641 }
2642
vijay kumar800255e2015-04-24 20:26:19 +05302643 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2644 fastboot_fail("buffer overreads occured due to invalid sparse header");
2645 return;
2646 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002647 fill_val = *(uint32_t *)data;
2648 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002649
2650 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2651 {
2652 fill_buf[i] = fill_val;
2653 }
2654
wufeng.jiang813dc352015-06-02 23:02:46 -04002655 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002656 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302657 /* Make sure that the data written to partition does not exceed partition size */
2658 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2659 {
2660 fastboot_fail("Chunk data size for fill type exceeds partition size");
2661 return;
2662 }
2663
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002664 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2665 sparse_header->blk_sz,
2666 fill_buf))
2667 {
2668 fastboot_fail("flash write failure");
2669 free(fill_buf);
2670 return;
2671 }
2672
2673 total_blocks++;
2674 }
2675
2676 free(fill_buf);
2677 break;
2678
Ajay Dudani5c761132011-04-07 20:19:04 -07002679 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302680 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2681 fastboot_fail("bogus size for chunk DONT CARE type");
2682 return;
2683 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002684 total_blocks += chunk_header->chunk_sz;
2685 break;
2686
Ajay Dudani5c761132011-04-07 20:19:04 -07002687 case CHUNK_TYPE_CRC:
2688 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2689 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002690 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002691 return;
2692 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302693 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2694 fastboot_fail("bogus size for chunk CRC type");
2695 return;
2696 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002697 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302698 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2699 fastboot_fail("integer overflow occured");
2700 return;
2701 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002702 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302703 if (data_end < (uint32_t)data) {
2704 fastboot_fail("buffer overreads occured due to invalid sparse header");
2705 return;
2706 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002707 break;
2708
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002709 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002710 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002711 fastboot_fail("Unknown chunk type");
2712 return;
2713 }
2714 }
2715
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002716 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2717 total_blocks, sparse_header->total_blks);
2718
2719 if(total_blocks != sparse_header->total_blks)
2720 {
2721 fastboot_fail("sparse image write failure");
2722 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002723
2724 fastboot_okay("");
2725 return;
2726}
2727
2728void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2729{
2730 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002731 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002732
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002733#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002734 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2735 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002736 int ret=0;
2737 uint32 major_version=0;
2738 uint32 minor_version=0;
2739
2740 ret = scm_svc_version(&major_version,&minor_version);
2741 if(!ret)
2742 {
2743 if(major_version >= 2)
2744 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002745 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002746 {
2747 ret = encrypt_scm((uint32 **) &data, &sz);
2748 if (ret != 0) {
2749 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2750 return;
2751 }
2752
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002753 /* Protect only for SSD */
2754 if (!strcmp(arg, "ssd")) {
2755 ret = scm_protect_keystore((uint32 *) data, sz);
2756 if (ret != 0) {
2757 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2758 return;
2759 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002760 }
2761 }
2762 else
2763 {
2764 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2765 if(ret != 0)
2766 {
2767 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2768 return;
2769 }
2770 }
2771 }
2772 else
2773 {
2774 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2775 magic_number[1] == DECRYPT_MAGIC_1)
2776 {
2777 ret = decrypt_scm((uint32 **) &data, &sz);
2778 if (ret != 0) {
2779 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2780 return;
2781 }
2782 }
2783 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2784 magic_number[1] == ENCRYPT_MAGIC_1)
2785 {
2786 ret = encrypt_scm((uint32 **) &data, &sz);
2787 if (ret != 0) {
2788 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2789 return;
2790 }
2791 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002792 }
2793 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002794 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002795 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002796 dprintf(CRITICAL,"INVALID SVC Version\n");
2797 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002798 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002799#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002800
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002801#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002802 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002803 {
lijuang511a2b52015-08-14 20:50:51 +08002804 /* if device is locked:
2805 * common partition will not allow to be flashed
2806 * critical partition will allow to flash image.
2807 */
2808 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
2809 fastboot_fail("Partition flashing is not allowed");
2810 return;
2811 }
2812
2813 /* if device critical is locked:
2814 * common partition will allow to be flashed
2815 * critical partition will not allow to flash image.
2816 */
2817 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
2818 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002819 return;
2820 }
2821 }
2822#endif
2823
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002824 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002825 meta_header = (meta_header_t *) data;
2826 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002827 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002828 else if (meta_header->magic == META_HEADER_MAGIC)
2829 cmd_flash_meta_img(arg, data, sz);
2830 else
2831 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002832
2833#if VERIFIED_BOOT
2834 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
2835 {
2836 // reset dm_verity mode to enforcing
2837 device.verity_mode = 1;
2838 write_device_info(&device);
2839 }
2840#endif
2841
Ajay Dudani5c761132011-04-07 20:19:04 -07002842 return;
2843}
2844
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002845void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2846{
2847 struct ptentry *sys_ptn;
2848 struct ptable *ptable;
2849
2850 ptable = flash_get_ptable();
2851 if (ptable == NULL) {
2852 fastboot_fail("partition table doesn't exist");
2853 return;
2854 }
2855
2856 sys_ptn = ptable_find(ptable, "system");
2857 if (sys_ptn == NULL) {
2858 fastboot_fail("system partition not found");
2859 return;
2860 }
2861
2862 sz = ROUND_TO_PAGE(sz, page_mask);
2863 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2864 fastboot_fail("update_ubi_vol failed");
2865 else
2866 fastboot_okay("");
2867}
2868
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002869void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002870{
2871 struct ptentry *ptn;
2872 struct ptable *ptable;
2873 unsigned extra = 0;
2874
2875 ptable = flash_get_ptable();
2876 if (ptable == NULL) {
2877 fastboot_fail("partition table doesn't exist");
2878 return;
2879 }
2880
2881 ptn = ptable_find(ptable, arg);
2882 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002883 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2884 arg);
2885 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002886 return;
2887 }
2888
2889 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2890 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2891 fastboot_fail("image is not a boot image");
2892 return;
2893 }
2894 }
2895
Amol Jadi5c61a952012-05-04 17:05:35 -07002896 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002897 || !strcmp(ptn->name, "userdata")
2898 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002899 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002900 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002901 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002902 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002903 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002904
2905 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002906 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2907 if (flash_ubi_img(ptn, data, sz)) {
2908 fastboot_fail("flash write failure");
2909 return;
2910 }
2911 } else {
2912 if (flash_write(ptn, extra, data, sz)) {
2913 fastboot_fail("flash write failure");
2914 return;
2915 }
Dima Zavin214cc642009-01-26 11:16:21 -08002916 }
2917 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2918 fastboot_okay("");
2919}
2920
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002921void cmd_flash(const char *arg, void *data, unsigned sz)
2922{
lijuang65c5a822015-08-29 16:35:36 +08002923#if CHECK_BAT_VOLTAGE
2924 if (!target_battery_soc_ok()) {
2925 fastboot_fail("Warning: battery's capacity is very low\n");
2926 return;
2927 }
2928#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002929 if(target_is_emmc_boot())
2930 cmd_flash_mmc(arg, data, sz);
2931 else
2932 cmd_flash_nand(arg, data, sz);
2933}
2934
Dima Zavin214cc642009-01-26 11:16:21 -08002935void cmd_continue(const char *arg, void *data, unsigned sz)
2936{
2937 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002938 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002939
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002940 if (target_is_emmc_boot())
2941 {
lijuanga40d6302015-07-20 20:10:13 +08002942#if FBCON_DISPLAY_MSG
2943 keys_detect_init();
2944#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002945 boot_linux_from_mmc();
2946 }
2947 else
2948 {
2949 boot_linux_from_flash();
2950 }
Dima Zavin214cc642009-01-26 11:16:21 -08002951}
2952
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002953void cmd_reboot(const char *arg, void *data, unsigned sz)
2954{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002955 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002956 fastboot_okay("");
2957 reboot_device(0);
2958}
2959
2960void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2961{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002962 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002963 fastboot_okay("");
2964 reboot_device(FASTBOOT_MODE);
2965}
2966
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002967void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2968{
2969 dprintf(INFO, "Enabling charger screen check\n");
2970 device.charger_screen_enabled = 1;
2971 write_device_info(&device);
2972 fastboot_okay("");
2973}
2974
2975void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2976{
2977 dprintf(INFO, "Disabling charger screen check\n");
2978 device.charger_screen_enabled = 0;
2979 write_device_info(&device);
2980 fastboot_okay("");
2981}
2982
lijuanga25d8bb2015-09-16 13:11:52 +08002983void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
2984{
2985 char *p = NULL;
2986 const char *delim = " \t\n\r";
2987
2988 if (arg) {
2989 p = strtok((char *)arg, delim);
2990 if (p) {
2991 if (!strncmp(p, "0", 1)) {
2992 device.charger_screen_enabled = 0;
2993 } else if (!strncmp(p, "1", 1)) {
2994 device.charger_screen_enabled = 1;
2995 }
2996 }
2997 }
2998
2999 /* update charger_screen_enabled value for getvar
3000 * command
3001 */
3002 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3003 device.charger_screen_enabled);
3004
3005 write_device_info(&device);
3006 fastboot_okay("");
3007}
3008
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303009void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3010{
3011 dprintf(INFO, "Selecting display panel %s\n", arg);
3012 if (arg)
3013 strlcpy(device.display_panel, arg,
3014 sizeof(device.display_panel));
3015 write_device_info(&device);
3016 fastboot_okay("");
3017}
3018
Shashank Mittal162244e2011-08-08 19:01:25 -07003019void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3020{
lijuang4ece1e72015-08-14 21:02:36 +08003021 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303022}
3023
3024void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3025{
lijuang4ece1e72015-08-14 21:02:36 +08003026 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303027 if(!is_allow_unlock) {
3028 fastboot_fail("oem unlock is not allowed");
3029 return;
3030 }
3031
lijuang4ece1e72015-08-14 21:02:36 +08003032 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303033
lijuang4ece1e72015-08-14 21:02:36 +08003034 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303035 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003036
vijay kumarc65876c2015-04-24 13:29:16 +05303037 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3038 write_misc(0, &msg, sizeof(msg));
3039
3040 fastboot_okay("");
3041 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003042 }
3043 fastboot_okay("");
3044}
3045
3046void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3047{
lijuang4ece1e72015-08-14 21:02:36 +08003048 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003049}
3050
Shashank Mittala0032282011-08-26 14:50:11 -07003051void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3052{
lijuang511a2b52015-08-14 20:50:51 +08003053 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003054 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003055 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003056 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3057 fastboot_info(response);
lijuang511a2b52015-08-14 20:50:51 +08003058 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3059 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003060 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003061 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303062 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3063 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003064 fastboot_okay("");
3065}
3066
lijuang511a2b52015-08-14 20:50:51 +08003067void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3068{
3069 char response[MAX_RSP_SIZE];
3070 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3071 fastboot_info(response);
3072 fastboot_okay("");
3073}
3074
3075void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3076{
lijuang4ece1e72015-08-14 21:02:36 +08003077 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003078}
3079
3080void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3081{
lijuang4ece1e72015-08-14 21:02:36 +08003082 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003083}
3084
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003085void cmd_preflash(const char *arg, void *data, unsigned sz)
3086{
3087 fastboot_okay("");
3088}
3089
Mao Flynn7b379f32015-04-20 00:28:30 +08003090static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303091
Mao Flynn7b379f32015-04-20 00:28:30 +08003092int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303093{
Mao Flynn7b379f32015-04-20 00:28:30 +08003094 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303095 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003096 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303097 return -1;
3098 return 0;
3099}
3100
Mao Flynn7b379f32015-04-20 00:28:30 +08003101int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003102{
3103 struct ptentry *ptn;
3104 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003105 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003106 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003107
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303108 ptable = flash_get_ptable();
3109 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003110 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3111 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303112 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003113
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303114 ptn = ptable_find(ptable, "splash");
3115 if (ptn == NULL) {
3116 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003117 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303118 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003119 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303120 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003121 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303122 }
3123
Mao Flynn7b379f32015-04-20 00:28:30 +08003124 header = (struct logo_img_header *)logo_header;
3125 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303126 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003127 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303128 }
3129
3130 fb_display = fbcon_display();
3131 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003132 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3133 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3134
3135 /* if the logo is full-screen size, remove "fbcon_clear()" */
3136 if ((header->width != fb_display->width)
3137 || (header->height != fb_display->height))
3138 fbcon_clear();
3139
3140 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3141 (uint32_t *)base,
3142 (header->blocks * 512))) {
3143 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3144 return -1;
3145 }
3146 fbcon_extract_to_screen(header, base);
3147 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003148 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003149
3150 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3151 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3152 return -1;
3153 }
3154
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303155 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003156 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3157 (uint32_t *)base,
3158 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303159 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303160 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003161 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003162 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303163 }
3164
Mao Flynn7b379f32015-04-20 00:28:30 +08003165 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303166}
3167
Mao Flynn7b379f32015-04-20 00:28:30 +08003168int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303169{
3170 int index = INVALID_PTN;
3171 unsigned long long ptn = 0;
3172 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003173 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003174 uint32_t blocksize, realsize, readsize;
3175 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303176
3177 index = partition_get_index("splash");
3178 if (index == 0) {
3179 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003180 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303181 }
3182
3183 ptn = partition_get_offset(index);
3184 if (ptn == 0) {
3185 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003186 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303187 }
3188
Mao Flynn1893a7f2015-06-03 12:03:36 +08003189 mmc_set_lun(partition_get_lun(index));
3190
3191 blocksize = mmc_get_device_blocksize();
3192 if (blocksize == 0) {
3193 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3194 return -1;
3195 }
3196
3197 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003198 if (!fb_display)
3199 {
3200 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3201 return -1;
3202 }
3203
Mao Flynn1893a7f2015-06-03 12:03:36 +08003204 base = (uint8_t *) fb_display->base;
3205
3206 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303207 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003208 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303209 }
3210
Mao Flynn1893a7f2015-06-03 12:03:36 +08003211 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003212 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303213 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003214 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303215 }
3216
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303217 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003218 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3219 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003220
Mao Flynn1893a7f2015-06-03 12:03:36 +08003221 realsize = header->blocks * 512;
3222 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3223
3224 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003225 if ((header->width != fb_display->width)
3226 || (header->height != fb_display->height))
3227 fbcon_clear();
3228
Mao Flynn1893a7f2015-06-03 12:03:36 +08003229 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003230 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3231 return -1;
3232 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003233
Mao Flynn1893a7f2015-06-03 12:03:36 +08003234 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3235 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303236
Mao Flynn1893a7f2015-06-03 12:03:36 +08003237 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3238 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3239 return -1;
3240 }
3241
3242 realsize = header->width * header->height * fb_display->bpp / 8;
3243 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3244
3245 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3246 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3247 fbcon_clear();
3248 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3249 return -1;
3250 }
3251 } else {
3252 if (mmc_read(ptn + blocksize ,
3253 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3254 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3255 return -1;
3256 }
3257 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3258 }
3259 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303260 }
3261
Mao Flynn7b379f32015-04-20 00:28:30 +08003262 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303263}
3264
Mao Flynn7b379f32015-04-20 00:28:30 +08003265int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303266{
3267 if (target_is_emmc_boot()) {
3268 return splash_screen_mmc();
3269 } else {
3270 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003271 }
3272}
3273
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003274/* Get the size from partiton name */
3275static void get_partition_size(const char *arg, char *response)
3276{
3277 uint64_t ptn = 0;
3278 uint64_t size;
3279 int index = INVALID_PTN;
3280
3281 index = partition_get_index(arg);
3282
3283 if (index == INVALID_PTN)
3284 {
3285 dprintf(CRITICAL, "Invalid partition index\n");
3286 return;
3287 }
3288
3289 ptn = partition_get_offset(index);
3290
3291 if(!ptn)
3292 {
3293 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3294 return;
3295 }
3296
3297 size = partition_get_size(index);
3298
3299 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3300 return;
3301}
3302
3303/*
3304 * Publish the partition type & size info
3305 * fastboot getvar will publish the required information.
3306 * fastboot getvar partition_size:<partition_name>: partition size in hex
3307 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3308 */
3309static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3310{
3311 uint8_t i;
3312
3313 for (i = 0; i < num_parts; i++) {
3314 get_partition_size(info[i].part_name, info[i].size_response);
3315
3316 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3317 {
3318 dprintf(CRITICAL, "partition size name truncated\n");
3319 return;
3320 }
3321 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3322 {
3323 dprintf(CRITICAL, "partition type name truncated\n");
3324 return;
3325 }
3326
3327 /* publish partition size & type info */
3328 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3329 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3330 }
3331}
3332
lijuang4ece1e72015-08-14 21:02:36 +08003333void get_product_name(unsigned char *buf)
3334{
3335 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3336 return;
3337}
3338
3339void get_bootloader_version(unsigned char *buf)
3340{
3341 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3342 return;
3343}
3344
3345void get_baseband_version(unsigned char *buf)
3346{
3347 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3348 return;
3349}
3350
3351bool is_device_locked()
3352{
3353 return device.is_unlocked ? false:true;
3354}
3355
Amol Jadi5edf3552013-07-23 14:15:34 -07003356/* register commands and variables for fastboot */
3357void aboot_fastboot_register_commands(void)
3358{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003359 int i;
lijuangf16461c2015-08-03 17:09:34 +08003360 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003361
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003362 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003363 /* By default the enabled list is empty. */
3364 {"", NULL},
3365 /* move commands enclosed within the below ifndef to here
3366 * if they need to be enabled in user build.
3367 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003368#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003369 /* Register the following commands only for non-user builds */
3370 {"flash:", cmd_flash},
3371 {"erase:", cmd_erase},
3372 {"boot", cmd_boot},
3373 {"continue", cmd_continue},
3374 {"reboot", cmd_reboot},
3375 {"reboot-bootloader", cmd_reboot_bootloader},
3376 {"oem unlock", cmd_oem_unlock},
3377 {"oem unlock-go", cmd_oem_unlock_go},
3378 {"oem lock", cmd_oem_lock},
3379 {"flashing unlock", cmd_oem_unlock},
3380 {"flashing lock", cmd_oem_lock},
3381 {"flashing lock_critical", cmd_flashing_lock_critical},
3382 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3383 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3384 {"oem device-info", cmd_oem_devinfo},
3385 {"preflash", cmd_preflash},
3386 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3387 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003388 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003389 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003390#endif
lijuang511a2b52015-08-14 20:50:51 +08003391 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003392
3393 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3394 for (i = 1; i < fastboot_cmds_count; i++)
3395 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3396
Amol Jadi5edf3552013-07-23 14:15:34 -07003397 /* publish variables and their values */
3398 fastboot_publish("product", TARGET(BOARD));
3399 fastboot_publish("kernel", "lk");
3400 fastboot_publish("serialno", sn_buf);
3401
3402 /*
3403 * partition info is supported only for emmc partitions
3404 * Calling this for NAND prints some error messages which
3405 * is harmless but misleading. Avoid calling this for NAND
3406 * devices.
3407 */
3408 if (target_is_emmc_boot())
3409 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3410
3411 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003412 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3413 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003414 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003415 /* Is the charger screen check enabled */
3416 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3417 device.charger_screen_enabled);
3418 fastboot_publish("charger-screen-enabled",
3419 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003420 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303421 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3422 device.display_panel);
3423 fastboot_publish("display-panel",
3424 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003425 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3426 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003427 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3428 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3429 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3430 target_is_emmc_boot()? "eMMC":"UFS");
3431 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003432#if CHECK_BAT_VOLTAGE
3433 snprintf(battery_voltage, MAX_RSP_SIZE, "%d",
3434 target_get_battery_voltage());
3435 fastboot_publish("battery-voltage", (const char *) battery_voltage);
3436 fastboot_publish("battery-soc-ok", target_battery_soc_ok()? "yes":"no");
3437#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003438}
3439
Brian Swetland9c4c0752009-01-25 16:23:50 -08003440void aboot_init(const struct app_descriptor *app)
3441{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003442 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03003443 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003444
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003445 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003446 if (target_is_emmc_boot())
3447 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003448 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003449 page_mask = page_size - 1;
3450 }
3451 else
3452 {
3453 page_size = flash_page_size();
3454 page_mask = page_size - 1;
3455 }
3456
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003457 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3458
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003459 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303460 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003461
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003462 /* Display splash screen if enabled */
3463#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003464#if NO_ALARM_DISPLAY
3465 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003466#endif
lijuang99c02d82015-02-13 19:04:34 +08003467 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003468#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003469 /* Wait if the display shutdown is in progress */
3470 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003471 if (!pm_appsbl_display_init_done())
3472 target_display_init(device.display_panel);
3473 else
3474 display_image_on_screen();
3475#else
lijuang99c02d82015-02-13 19:04:34 +08003476 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003477#endif
lijuang99c02d82015-02-13 19:04:34 +08003478 dprintf(SPEW, "Display Init: Done\n");
3479#if NO_ALARM_DISPLAY
3480 }
3481#endif
3482#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003483
Greg Griscod6250552011-06-29 14:40:23 -07003484 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003485 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003486
Dhaval Patel223ec952013-07-18 14:49:44 -07003487 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3488
Matthew Qindefd5562014-07-11 18:02:40 +08003489 /*
3490 * Check power off reason if user force reset,
3491 * if yes phone will do normal boot.
3492 */
3493 if (is_user_force_reset())
3494 goto normal_boot;
3495
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003496 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003497 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003498 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003499 dprintf(ALWAYS,"dload mode key sequence detected\n");
3500 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003501 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003502 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003503 }
3504 else
3505 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303506 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003507 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003508 }
3509 boot_into_fastboot = true;
3510 }
3511 if (!boot_into_fastboot)
3512 {
3513 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3514 boot_into_recovery = 1;
3515 if (!boot_into_recovery &&
3516 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003517 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003518 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003519 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003520 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003521 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003522 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003523
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003524#if USE_PON_REBOOT_REG
3525 reboot_mode = check_hard_reboot_mode();
3526#else
Ajay Dudani77421292010-10-27 19:34:06 -07003527 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003528#endif
3529 if (reboot_mode == RECOVERY_MODE)
3530 {
Ajay Dudani77421292010-10-27 19:34:06 -07003531 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003532 }
3533 else if(reboot_mode == FASTBOOT_MODE)
3534 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003535 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003536 }
3537 else if(reboot_mode == ALARM_BOOT)
3538 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003539 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003540 }
3541 else if (reboot_mode == DM_VERITY_ENFORCING)
3542 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003543 device.verity_mode = 1;
3544 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003545 }
3546 else if (reboot_mode == DM_VERITY_LOGGING)
3547 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003548 device.verity_mode = 0;
3549 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003550 }
3551 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3552 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003553 if(send_delete_keys_to_tz())
3554 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003555 }
3556
Matthew Qindefd5562014-07-11 18:02:40 +08003557normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003558 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003559 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003560 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003561 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003562 if(emmc_recovery_init())
3563 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3564 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003565 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003566 if((device.is_unlocked) || (device.is_tampered))
3567 {
3568 #ifdef TZ_TAMPER_FUSE
3569 set_tamper_fuse_cmd();
3570 #endif
3571 #if USE_PCOM_SECBOOT
3572 set_tamper_flag(device.is_tampered);
3573 #endif
3574 }
Shashank Mittala0032282011-08-26 14:50:11 -07003575 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003576
Pavel Nedev5d91d412013-04-29 11:34:24 +03003577 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003578 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003579 else
3580 {
3581 recovery_init();
3582 #if USE_PCOM_SECBOOT
3583 if((device.is_unlocked) || (device.is_tampered))
3584 set_tamper_flag(device.is_tampered);
3585 #endif
3586 boot_linux_from_flash();
3587 }
3588 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3589 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003590 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003591
Amol Jadi5edf3552013-07-23 14:15:34 -07003592 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003593
Amol Jadi5edf3552013-07-23 14:15:34 -07003594 /* register aboot specific fastboot commands */
3595 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003596
Amol Jadi5edf3552013-07-23 14:15:34 -07003597 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003598 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003599
3600 /* initialize and start fastboot */
3601 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003602#if FBCON_DISPLAY_MSG
3603 display_fastboot_menu_thread();
3604#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003605}
3606
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003607uint32_t get_page_size()
3608{
3609 return page_size;
3610}
3611
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003612/*
3613 * Calculated and save hash (SHA256) for non-signed boot image.
3614 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003615 * @param image_addr - Boot image address
3616 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003617 *
3618 * @return int - 0 on success, negative value on failure.
3619 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003620static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003621{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003622 unsigned int digest[8];
3623#if IMAGE_VERIF_ALGO_SHA1
3624 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3625#else
3626 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3627#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003628
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003629 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003630 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003631
3632 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003633 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003634
3635 return 0;
3636}
3637
Brian Swetland9c4c0752009-01-25 16:23:50 -08003638APP_START(aboot)
3639 .init = aboot_init,
3640APP_END