blob: a746984ec3056b62a7979637f07f5470a30db068 [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,
vijay kumar870515d2015-08-31 16:37:24 +0530100 char **buf);
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);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700106static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700107
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700108/* fastboot command function pointer */
109typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
110
111struct fastboot_cmd_desc {
112 char * name;
113 fastboot_cmd_fn cb;
114};
115
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700116#define EXPAND(NAME) #NAME
117#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700118
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800119#ifdef MEMBASE
120#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
121#else
David Ng183a7422009-12-07 14:55:21 -0800122#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800123#endif
124
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700125#ifndef MEMSIZE
126#define MEMSIZE 1024*1024
127#endif
128
129#define MAX_TAGS_SIZE 1024
130
Kun Liang2f1601a2013-08-12 16:29:54 +0800131/* make 4096 as default size to ensure EFS,EXT4's erasing */
132#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700133#define MAX_PANEL_BUF_SIZE 196
Kun Liang2f1601a2013-08-12 16:29:54 +0800134
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700135#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700136#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800137
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800138#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
139
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700140#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
141
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800142#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700143static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700144#else
David Ng183a7422009-12-07 14:55:21 -0800145static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700146#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800147static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300148static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800149static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800150static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800151static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700152static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300153static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200154static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800155
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800156static const char *baseband_apq = " androidboot.baseband=apq";
157static const char *baseband_msm = " androidboot.baseband=msm";
158static const char *baseband_csfb = " androidboot.baseband=csfb";
159static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700160static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800161static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700162static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800163static const char *baseband_dsda = " androidboot.baseband=dsda";
164static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800165static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800166static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800167
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700168#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700169static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700170static const char *verified_state= " androidboot.verifiedbootstate=";
171
172//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700173
174struct verified_boot_verity_mode vbvm[] =
175{
176 {false, "logging"},
177 {true, "enforcing"},
178};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700179struct verified_boot_state_name vbsn[] =
180{
181 {GREEN, "green"},
182 {ORANGE, "orange"},
183 {YELLOW,"yellow"},
184 {RED,"red" },
185};
186#endif
187
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700188static unsigned page_size = 0;
189static unsigned page_mask = 0;
190static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
191static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530192static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800193static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700194static bool devinfo_present = true;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700195
Shashank Mittalcd98d472011-08-02 14:29:24 -0700196/* Assuming unauthorized kernel image by default */
197static int auth_kernel_img = 0;
198
lijuang511a2b52015-08-14 20:50:51 +0800199static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
vijay kumarc65876c2015-04-24 13:29:16 +0530200static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700201
vijay kumarca2e6812015-07-08 20:28:25 +0530202static char frp_ptns[2][8] = {"config","frp"};
203
lijuang511a2b52015-08-14 20:50:51 +0800204static const char *critical_flash_allowed_ptn[] = {
205 "aboot",
206 "rpm",
207 "tz",
208 "sbl",
209 "sdi",
210 "sbl1",
211 "xbl",
212 "hyp",
213 "pmic",
214 "bootloader",
215 "devinfo",
216 "partition"};
217
Dima Zavin42168f22009-01-30 11:52:22 -0800218struct atag_ptbl_entry
219{
220 char name[16];
221 unsigned offset;
222 unsigned size;
223 unsigned flags;
224};
225
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700226/*
227 * Partition info, required to be published
228 * for fastboot
229 */
230struct getvar_partition_info {
231 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
232 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
233 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
234 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
235 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
236};
237
238/*
239 * Right now, we are publishing the info for only
240 * three partitions
241 */
242struct getvar_partition_info part_info[] =
243{
244 { "system" , "partition-size:", "partition-type:", "", "ext4" },
245 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
246 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
247};
248
249char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700250char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800251char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700252char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530253char panel_display_mode[MAX_RSP_SIZE];
lijuang65c5a822015-08-29 16:35:36 +0800254char battery_voltage[MAX_RSP_SIZE];
lijuangf16461c2015-08-03 17:09:34 +0800255char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700256
Greg Griscod2471ef2011-07-14 13:00:42 -0700257extern int emmc_recovery_init(void);
258
Kinson Chik0b1c8162011-08-31 16:31:57 -0700259#if NO_KEYPAD_DRIVER
260extern int fastboot_trigger(void);
261#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700262
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800263static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700264{
265 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700266#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800267 if (is_arm64)
268 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
269 else
270 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700271 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
272 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700273#endif
274}
275
Dima Zavin42168f22009-01-30 11:52:22 -0800276static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
277{
278 struct atag_ptbl_entry atag_ptn;
279
280 memcpy(atag_ptn.name, ptn->name, 16);
281 atag_ptn.name[15] = '\0';
282 atag_ptn.offset = ptn->start;
283 atag_ptn.size = ptn->length;
284 atag_ptn.flags = ptn->flags;
285 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
286 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
287}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800288
Neeti Desaie245d492012-06-01 12:52:13 -0700289unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800290{
David Ng183a7422009-12-07 14:55:21 -0800291 int cmdline_len = 0;
292 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800293 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700294 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800295 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300296 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700297 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700298 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200299 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700300#if VERIFIED_BOOT
301 uint32_t boot_state = boot_verify_get_state();
302#endif
303
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200304#ifdef MDTP_SUPPORT
305 mdtp_activated(&is_mdtp_activated);
306#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800307
Brian Swetland9c4c0752009-01-25 16:23:50 -0800308 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800309 cmdline_len = strlen(cmdline);
310 have_cmdline = 1;
311 }
312 if (target_is_emmc_boot()) {
313 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800314#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700315 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
316 ASSERT(boot_dev_buf);
317 platform_boot_dev_cmdline(boot_dev_buf);
318 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700319#endif
David Ng183a7422009-12-07 14:55:21 -0800320 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800321
322 cmdline_len += strlen(usb_sn_cmdline);
323 cmdline_len += strlen(sn_buf);
324
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700325#if VERIFIED_BOOT
326 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700327 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
328 {
329 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
330 ASSERT(0);
331 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700332 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700333#endif
334
Pavel Nedev5614d222013-06-17 18:01:02 +0300335 if (boot_into_recovery && gpt_exists)
336 cmdline_len += strlen(secondary_gpt_enable);
337
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200338 if(is_mdtp_activated)
339 cmdline_len += strlen(mdtp_activated_flag);
340
Pavel Nedev328ac822013-04-05 15:25:11 +0300341 if (boot_into_ffbm) {
342 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700343 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800344 /* reduce kernel console messages to speed-up boot */
345 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800346 } else if (boot_reason_alarm) {
347 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800348 } else if ((target_build_variant_user() || device.charger_screen_enabled)
349 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700350 pause_at_bootup = 1;
351 cmdline_len += strlen(battchg_pause);
352 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800353
Shashank Mittalcd98d472011-08-02 14:29:24 -0700354 if(target_use_signed_kernel() && auth_kernel_img) {
355 cmdline_len += strlen(auth_kernel);
356 }
357
Joonwoo Park61112782013-10-02 19:50:39 -0700358 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
359 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530360 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700361 have_target_boot_params = 1;
362 cmdline_len += strlen(target_boot_params);
363 }
364
Ajay Dudanid04110c2011-01-17 23:55:07 -0800365 /* Determine correct androidboot.baseband to use */
366 switch(target_baseband())
367 {
368 case BASEBAND_APQ:
369 cmdline_len += strlen(baseband_apq);
370 break;
371
372 case BASEBAND_MSM:
373 cmdline_len += strlen(baseband_msm);
374 break;
375
376 case BASEBAND_CSFB:
377 cmdline_len += strlen(baseband_csfb);
378 break;
379
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800380 case BASEBAND_SVLTE2A:
381 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800382 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700383
384 case BASEBAND_MDM:
385 cmdline_len += strlen(baseband_mdm);
386 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700387
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800388 case BASEBAND_MDM2:
389 cmdline_len += strlen(baseband_mdm2);
390 break;
391
Amol Jadi5c61a952012-05-04 17:05:35 -0700392 case BASEBAND_SGLTE:
393 cmdline_len += strlen(baseband_sglte);
394 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530395
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800396 case BASEBAND_SGLTE2:
397 cmdline_len += strlen(baseband_sglte2);
398 break;
399
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530400 case BASEBAND_DSDA:
401 cmdline_len += strlen(baseband_dsda);
402 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800403
404 case BASEBAND_DSDA2:
405 cmdline_len += strlen(baseband_dsda2);
406 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800407 }
408
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800409 if (cmdline) {
410 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530411 target_display_panel_node(display_panel_buf,
412 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800413 strlen(display_panel_buf)) {
414 cmdline_len += strlen(display_panel_buf);
415 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700416 }
417
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800418 if (target_warm_boot()) {
419 warm_boot = true;
420 cmdline_len += strlen(warmboot_cmdline);
421 }
422
David Ng183a7422009-12-07 14:55:21 -0800423 if (cmdline_len > 0) {
424 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800425 unsigned char *dst;
426
427 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
428 ASSERT(cmdline_final != NULL);
429 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700430
Amol Jadi168b7712012-03-06 16:15:00 -0800431 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800432 if (have_cmdline) {
433 src = cmdline;
434 while ((*dst++ = *src++));
435 }
436 if (target_is_emmc_boot()) {
437 src = emmc_cmdline;
438 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700439 have_cmdline = 1;
440 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800441#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700442 src = boot_dev_buf;
443 if (have_cmdline) --dst;
444 while ((*dst++ = *src++));
445#endif
David Ngf773dde2010-07-26 19:55:08 -0700446 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800447
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700448#if VERIFIED_BOOT
449 src = verified_state;
450 if(have_cmdline) --dst;
451 have_cmdline = 1;
452 while ((*dst++ = *src++));
453 src = vbsn[boot_state].name;
454 if(have_cmdline) --dst;
455 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700456
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700457 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
458 {
459 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
460 ASSERT(0);
461 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700462 src = verity_mode;
463 if(have_cmdline) --dst;
464 while ((*dst++ = *src++));
465 src = vbvm[device.verity_mode].name;
466 if(have_cmdline) -- dst;
467 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700468#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800469 src = usb_sn_cmdline;
470 if (have_cmdline) --dst;
471 have_cmdline = 1;
472 while ((*dst++ = *src++));
473 src = sn_buf;
474 if (have_cmdline) --dst;
475 have_cmdline = 1;
476 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800477 if (warm_boot) {
478 if (have_cmdline) --dst;
479 src = warmboot_cmdline;
480 while ((*dst++ = *src++));
481 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800482
Pavel Nedev5614d222013-06-17 18:01:02 +0300483 if (boot_into_recovery && gpt_exists) {
484 src = secondary_gpt_enable;
485 if (have_cmdline) --dst;
486 while ((*dst++ = *src++));
487 }
488
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200489 if (is_mdtp_activated) {
490 src = mdtp_activated_flag;
491 if (have_cmdline) --dst;
492 while ((*dst++ = *src++));
493 }
494
Pavel Nedev328ac822013-04-05 15:25:11 +0300495 if (boot_into_ffbm) {
496 src = androidboot_mode;
497 if (have_cmdline) --dst;
498 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700499 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300500 if (have_cmdline) --dst;
501 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800502 src = loglevel;
503 if (have_cmdline) --dst;
504 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800505 } else if (boot_reason_alarm) {
506 src = alarmboot_cmdline;
507 if (have_cmdline) --dst;
508 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300509 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700510 src = battchg_pause;
511 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800512 while ((*dst++ = *src++));
513 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800514
Shashank Mittalcd98d472011-08-02 14:29:24 -0700515 if(target_use_signed_kernel() && auth_kernel_img) {
516 src = auth_kernel;
517 if (have_cmdline) --dst;
518 while ((*dst++ = *src++));
519 }
520
Ajay Dudanid04110c2011-01-17 23:55:07 -0800521 switch(target_baseband())
522 {
523 case BASEBAND_APQ:
524 src = baseband_apq;
525 if (have_cmdline) --dst;
526 while ((*dst++ = *src++));
527 break;
528
529 case BASEBAND_MSM:
530 src = baseband_msm;
531 if (have_cmdline) --dst;
532 while ((*dst++ = *src++));
533 break;
534
535 case BASEBAND_CSFB:
536 src = baseband_csfb;
537 if (have_cmdline) --dst;
538 while ((*dst++ = *src++));
539 break;
540
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800541 case BASEBAND_SVLTE2A:
542 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800543 if (have_cmdline) --dst;
544 while ((*dst++ = *src++));
545 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700546
547 case BASEBAND_MDM:
548 src = baseband_mdm;
549 if (have_cmdline) --dst;
550 while ((*dst++ = *src++));
551 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700552
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800553 case BASEBAND_MDM2:
554 src = baseband_mdm2;
555 if (have_cmdline) --dst;
556 while ((*dst++ = *src++));
557 break;
558
Amol Jadi5c61a952012-05-04 17:05:35 -0700559 case BASEBAND_SGLTE:
560 src = baseband_sglte;
561 if (have_cmdline) --dst;
562 while ((*dst++ = *src++));
563 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530564
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800565 case BASEBAND_SGLTE2:
566 src = baseband_sglte2;
567 if (have_cmdline) --dst;
568 while ((*dst++ = *src++));
569 break;
570
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530571 case BASEBAND_DSDA:
572 src = baseband_dsda;
573 if (have_cmdline) --dst;
574 while ((*dst++ = *src++));
575 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800576
577 case BASEBAND_DSDA2:
578 src = baseband_dsda2;
579 if (have_cmdline) --dst;
580 while ((*dst++ = *src++));
581 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800582 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700583
584 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700585 src = display_panel_buf;
586 if (have_cmdline) --dst;
587 while ((*dst++ = *src++));
588 }
Joonwoo Park61112782013-10-02 19:50:39 -0700589
590 if (have_target_boot_params) {
591 if (have_cmdline) --dst;
592 src = target_boot_params;
593 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530594 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700595 }
Neeti Desaie245d492012-06-01 12:52:13 -0700596 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700597
598
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700599 if (boot_dev_buf)
600 free(boot_dev_buf);
601
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800602 if (cmdline_final)
603 dprintf(INFO, "cmdline: %s\n", cmdline_final);
604 else
605 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700606 return cmdline_final;
607}
608
609unsigned *atag_core(unsigned *ptr)
610{
611 /* CORE */
612 *ptr++ = 2;
613 *ptr++ = 0x54410001;
614
615 return ptr;
616
617}
618
619unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
620 unsigned ramdisk_size)
621{
622 if (ramdisk_size) {
623 *ptr++ = 4;
624 *ptr++ = 0x54420005;
625 *ptr++ = (unsigned)ramdisk;
626 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800627 }
628
Neeti Desaie245d492012-06-01 12:52:13 -0700629 return ptr;
630}
631
632unsigned *atag_ptable(unsigned **ptr_addr)
633{
634 int i;
635 struct ptable *ptable;
636
637 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700638 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
639 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700640 *(*ptr_addr)++ = 0x4d534d70;
641 for (i = 0; i < ptable->count; ++i)
642 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
643 }
644
645 return (*ptr_addr);
646}
647
648unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
649{
650 int cmdline_length = 0;
651 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700652 char *dest;
653
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800654 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700655 n = (cmdline_length + 4) & (~3);
656
657 *ptr++ = (n / 4) + 2;
658 *ptr++ = 0x54410009;
659 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800660 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700661 ptr += (n / 4);
662
663 return ptr;
664}
665
666unsigned *atag_end(unsigned *ptr)
667{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800668 /* END */
669 *ptr++ = 0;
670 *ptr++ = 0;
671
Neeti Desaie245d492012-06-01 12:52:13 -0700672 return ptr;
673}
674
675void generate_atags(unsigned *ptr, const char *cmdline,
676 void *ramdisk, unsigned ramdisk_size)
677{
678
679 ptr = atag_core(ptr);
680 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
681 ptr = target_atag_mem(ptr);
682
683 /* Skip NAND partition ATAGS for eMMC boot */
684 if (!target_is_emmc_boot()){
685 ptr = atag_ptable(&ptr);
686 }
687
688 ptr = atag_cmdline(ptr, cmdline);
689 ptr = atag_end(ptr);
690}
691
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700692typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700693void boot_linux(void *kernel, unsigned *tags,
694 const char *cmdline, unsigned machtype,
695 void *ramdisk, unsigned ramdisk_size)
696{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800697 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800698#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700699 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800700#endif
701
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700702 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800703 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800704 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800705
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530706 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700707
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800708 final_cmdline = update_cmdline((const char*)cmdline);
709
Neeti Desai17379b82012-06-04 18:42:53 -0700710#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800711 dprintf(INFO, "Updating device tree: start\n");
712
Neeti Desai17379b82012-06-04 18:42:53 -0700713 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530714 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700715 if(ret)
716 {
717 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
718 ASSERT(0);
719 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800720 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700721#else
Neeti Desaie245d492012-06-01 12:52:13 -0700722 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800723 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700724#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700725
Maria Yu52254c02014-07-04 16:14:54 +0800726 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700727
728#if VERIFIED_BOOT
729 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700730 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700731 {
732 dprintf(INFO, "Failed to write protect dev info\n");
733 ASSERT(0);
734 }
735#endif
736
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700737 /* Perform target specific cleanup */
738 target_uninit();
739
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800740 /* Turn off splash screen if enabled */
741#if DISPLAY_SPLASH_SCREEN
742 target_display_shutdown();
743#endif
744
745
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800746 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530747 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800748
749 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700750
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +0530751 /* Initialise wdog to catch early kernel crashes */
752#if WDOG_SUPPORT
753 msm_wdog_init();
754#endif
Amol Jadi4421e652011-06-16 15:00:48 -0700755 /* do any platform specific cleanup before kernel entry */
756 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700757
Brian Swetland9c4c0752009-01-25 16:23:50 -0800758 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700759
Amol Jadi504f9fe2012-08-16 13:56:48 -0700760#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800761 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700762#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700763 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800764
765 if (IS_ARM64(kptr))
766 /* Jump to a 64bit kernel */
767 scm_elexec_call((paddr_t)kernel, tags_phys);
768 else
769 /* Jump to a 32bit kernel */
770 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800771}
772
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700773/* Function to check if the memory address range falls within the aboot
774 * boundaries.
775 * start: Start of the memory region
776 * size: Size of the memory region
777 */
778int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
779{
780 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800781 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700782 return -1;
783
784 /* Check for memory overlap. */
785 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
786 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700787 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700788 return 0;
789 else
790 return -1;
791}
792
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800793#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800794
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800795BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800796#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800797BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800798#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800799
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700800static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
801{
802 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800803
804#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800805#if IMAGE_VERIF_ALGO_SHA1
806 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
807#else
808 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
809#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800810#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700811
812 /* Assume device is rooted at this time. */
813 device.is_tampered = 1;
814
815 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
816
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700817#if VERIFIED_BOOT
818 if(boot_into_recovery)
819 {
820 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530821 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700822 }
823 else
824 {
825 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530826 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700827 }
828 boot_verify_print_state();
829#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700830 ret = image_verify((unsigned char *)bootimg_addr,
831 (unsigned char *)(bootimg_addr + bootimg_size),
832 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800833 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700834#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700835 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
836
837 if (ret)
838 {
839 /* Authorized kernel */
840 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700841 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700842 }
843
Amit Blay4aa292f2015-04-28 21:55:59 +0300844#ifdef MDTP_SUPPORT
845 {
846 /* Verify MDTP lock.
847 * For boot & recovery partitions, use aboot's verification result.
848 */
849 mdtp_ext_partition_verification_t ext_partition;
850 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
851 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
852 ext_partition.page_size = 0; /* Not needed since already validated */
853 ext_partition.image_addr = 0; /* Not needed since already validated */
854 ext_partition.image_size = 0; /* Not needed since already validated */
855 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
856 mdtp_fwlock_verify_lock(&ext_partition);
857 }
858#endif /* MDTP_SUPPORT */
859
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700860#if USE_PCOM_SECBOOT
861 set_tamper_flag(device.is_tampered);
862#endif
863
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700864#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700865 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700866 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700867 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800868#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +0800869 display_bootverify_menu_thread(DISPLAY_MENU_RED);
lijuanga40d6302015-07-20 20:10:13 +0800870 wait_for_users_action();
871#else
872 dprintf(CRITICAL,
873 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
874 mdelay(5000);
875#endif
876
877 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700878 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800879#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +0800880 display_bootverify_menu_thread(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800881 wait_for_users_action();
882#else
883 dprintf(CRITICAL,
884 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
885 mdelay(5000);
886#endif
887 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700888 default:
lijuanga40d6302015-07-20 20:10:13 +0800889 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700890 }
891#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700892#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530893 if(device.is_tampered)
894 {
895 write_device_info_mmc(&device);
896 #ifdef TZ_TAMPER_FUSE
897 set_tamper_fuse_cmd();
898 #endif
899 #ifdef ASSERT_ON_TAMPER
900 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
901 ASSERT(0);
902 #endif
903 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700904#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700905}
906
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530907static bool check_format_bit()
908{
909 bool ret = false;
910 int index;
911 uint64_t offset;
912 struct boot_selection_info *in = NULL;
913 char *buf = NULL;
914
915 index = partition_get_index("bootselect");
916 if (index == INVALID_PTN)
917 {
918 dprintf(INFO, "Unable to locate /bootselect partition\n");
919 return ret;
920 }
921 offset = partition_get_offset(index);
922 if(!offset)
923 {
924 dprintf(INFO, "partition /bootselect doesn't exist\n");
925 return ret;
926 }
927 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
928 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530929 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530930 {
931 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
932 free(buf);
933 return ret;
934 }
935 in = (struct boot_selection_info *) buf;
936 if ((in->signature == BOOTSELECT_SIGNATURE) &&
937 (in->version == BOOTSELECT_VERSION)) {
938 if ((in->state_info & BOOTSELECT_FORMAT) &&
939 !(in->state_info & BOOTSELECT_FACTORY))
940 ret = true;
941 } else {
942 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
943 in->signature, in->version);
944 ASSERT(0);
945 }
946 free(buf);
947 return ret;
948}
949
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700950void boot_verifier_init()
951{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700952 uint32_t boot_state;
953 /* Check if device unlock */
954 if(device.is_unlocked)
955 {
956 boot_verify_send_event(DEV_UNLOCK);
957 boot_verify_print_state();
958 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
959 return;
960 }
961 else
962 {
963 boot_verify_send_event(BOOT_INIT);
964 }
965
966 /* Initialize keystore */
967 boot_state = boot_verify_keystore_init();
968 if(boot_state == YELLOW)
969 {
970 boot_verify_print_state();
971 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
972 }
973}
974
Shashank Mittal23b8f422010-04-16 19:27:21 -0700975int boot_linux_from_mmc(void)
976{
977 struct boot_img_hdr *hdr = (void*) buf;
978 struct boot_img_hdr *uhdr;
979 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700980 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700981 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700982 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700983
Shashank Mittalcd98d472011-08-02 14:29:24 -0700984 unsigned char *image_addr = 0;
985 unsigned kernel_actual;
986 unsigned ramdisk_actual;
987 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700988 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700989
Matthew Qin271927e2015-03-31 22:07:07 -0400990 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +0800991 unsigned int out_len = 0;
992 unsigned int out_avai_len = 0;
993 unsigned char *out_addr = NULL;
994 uint32_t dtb_offset = 0;
995 unsigned char *kernel_start_addr = NULL;
996 unsigned int kernel_size = 0;
997 int rc;
998
Neeti Desai465491e2012-07-31 12:53:35 -0700999#if DEVICE_TREE
1000 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001001 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001002 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001003 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001004 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001005 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001006#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001007 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001008
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301009 if (check_format_bit())
1010 boot_into_recovery = 1;
1011
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001012 if (!boot_into_recovery) {
1013 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1014 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1015 if (rcode <= 0) {
1016 boot_into_ffbm = false;
1017 if (rcode < 0)
1018 dprintf(CRITICAL,"failed to get ffbm cookie");
1019 } else
1020 boot_into_ffbm = true;
1021 } else
1022 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001023 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1024 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1025 dprintf(INFO, "Unified boot method!\n");
1026 hdr = uhdr;
1027 goto unified_boot;
1028 }
Greg Griscod6250552011-06-29 14:40:23 -07001029 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001030 index = partition_get_index("boot");
1031 ptn = partition_get_offset(index);
1032 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001033 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1034 return -1;
1035 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001036 }
1037 else {
1038 index = partition_get_index("recovery");
1039 ptn = partition_get_offset(index);
1040 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001041 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1042 return -1;
1043 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001044 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001045 /* Set Lun for boot & recovery partitions */
1046 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001047
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301048 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001049 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1050 return -1;
1051 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001052
1053 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001054 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001055 return -1;
1056 }
1057
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001058 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301059
1060 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1061 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1062 return -1;
1063 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001064 page_size = hdr->page_size;
1065 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001066 }
1067
Matthew Qin49e51fa2015-02-09 10:40:45 +08001068 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1069 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001070
Matthew Qin49e51fa2015-02-09 10:40:45 +08001071 image_addr = (unsigned char *)target_get_scratch_address();
1072
1073#if DEVICE_TREE
1074 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1075 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1076#else
1077 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1078#endif
1079
1080#if VERIFIED_BOOT
1081 boot_verifier_init();
1082#endif
1083
1084 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
1085 {
1086 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1087 return -1;
1088 }
1089
Matthew Qinbb7923d2015-02-09 10:56:09 +08001090 /*
1091 * Update loading flow of bootimage to support compressed/uncompressed
1092 * bootimage on both 64bit and 32bit platform.
1093 * 1. Load bootimage from emmc partition onto DDR.
1094 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1095 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1096 * platform accordingly.
1097 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1098 */
1099
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001100 dprintf(INFO, "Loading (%s) image (%d): start\n",
1101 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001102 bs_set_timestamp(BS_KERNEL_LOAD_START);
1103
Matthew Qinbb7923d2015-02-09 10:56:09 +08001104 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001105 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1106 {
1107 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1108 return -1;
1109 }
1110
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001111 dprintf(INFO, "Loading (%s) image (%d): done\n",
1112 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1113
Matthew Qin49e51fa2015-02-09 10:40:45 +08001114 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1115
1116 /* Authenticate Kernel */
1117 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1118 (int) target_use_signed_kernel(),
1119 device.is_unlocked,
1120 device.is_tampered);
1121
1122 if(target_use_signed_kernel() && (!device.is_unlocked))
1123 {
1124 offset = imagesize_actual;
1125 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1126 {
1127 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1128 return -1;
1129 }
1130
1131 /* Read signature */
1132 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1133 {
1134 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1135 return -1;
1136 }
1137
1138 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
1139 } else {
1140 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1141 #ifdef TZ_SAVE_KERNEL_HASH
1142 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1143 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001144
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001145#if VERIFIED_BOOT
1146 if(boot_verify_get_state() == ORANGE)
1147 {
lijuanga40d6302015-07-20 20:10:13 +08001148#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +08001149 display_bootverify_menu_thread(DISPLAY_MENU_ORANGE);
lijuanga40d6302015-07-20 20:10:13 +08001150 wait_for_users_action();
1151#else
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001152 dprintf(CRITICAL,
lijuanga40d6302015-07-20 20:10:13 +08001153 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001154 mdelay(5000);
lijuanga40d6302015-07-20 20:10:13 +08001155#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001156 }
1157#endif
1158
Amit Blay4aa292f2015-04-28 21:55:59 +03001159#ifdef MDTP_SUPPORT
1160 {
1161 /* Verify MDTP lock.
1162 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1163 * since verification was skipped in aboot. The signature is not part of the loaded image.
1164 */
1165 mdtp_ext_partition_verification_t ext_partition;
1166 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1167 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1168 ext_partition.page_size = page_size;
1169 ext_partition.image_addr = (uint32)image_addr;
1170 ext_partition.image_size = imagesize_actual;
1171 ext_partition.sig_avail = FALSE;
1172 mdtp_fwlock_verify_lock(&ext_partition);
1173 }
1174#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001175 }
1176
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001177#if VERIFIED_BOOT
1178 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001179 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001180 ASSERT(0);
1181#endif
1182
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001183 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001184 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1185 * If not, continue booting.
1186 */
1187 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1188 {
1189 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1190 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001191 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001192 rc = decompress((unsigned char *)(image_addr + page_size),
1193 hdr->kernel_size, out_addr, out_avai_len,
1194 &dtb_offset, &out_len);
1195 if (rc)
1196 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001197 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001198 ASSERT(0);
1199 }
1200
Matthew Qin0b15b322015-05-19 05:20:54 -04001201 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001202 kptr = (struct kernel64_hdr *)out_addr;
1203 kernel_start_addr = out_addr;
1204 kernel_size = out_len;
1205 } else {
1206 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1207 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1208 kernel_size = hdr->kernel_size;
1209 }
1210
1211 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001212 * Update the kernel/ramdisk/tags address if the boot image header
1213 * has default values, these default values come from mkbootimg when
1214 * the boot image is flashed using fastboot flash:raw
1215 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001216 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001217
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001218 /* Get virtual addresses since the hdr saves physical addresses. */
1219 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1220 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1221 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001222
Matthew Qinbb7923d2015-02-09 10:56:09 +08001223 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001224 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001225 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001226 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1227 {
1228 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1229 return -1;
1230 }
1231
1232#ifndef DEVICE_TREE
1233 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1234 {
1235 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1236 return -1;
1237 }
1238#endif
1239
Matthew Qin49e51fa2015-02-09 10:40:45 +08001240 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001241 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001242 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001243
Matthew Qin49e51fa2015-02-09 10:40:45 +08001244 #if DEVICE_TREE
1245 if(hdr->dt_size) {
1246 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1247 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001248
Matthew Qin49e51fa2015-02-09 10:40:45 +08001249 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1250 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1251 return -1;
1252 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001253
Matthew Qin49e51fa2015-02-09 10:40:45 +08001254 /* Find index of device tree within device tree table */
1255 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1256 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1257 return -1;
1258 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001259
Matthew Qin271927e2015-03-31 22:07:07 -04001260 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1261 {
1262 unsigned int compressed_size = 0;
1263 out_addr += out_len;
1264 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001265 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001266 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1267 dt_entry.size, out_addr, out_avai_len,
1268 &compressed_size, &dtb_size);
1269 if (rc)
1270 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001271 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001272 ASSERT(0);
1273 }
1274
Matthew Qin0b15b322015-05-19 05:20:54 -04001275 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001276 best_match_dt_addr = out_addr;
1277 } else {
1278 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1279 dtb_size = dt_entry.size;
1280 }
1281
Matthew Qin49e51fa2015-02-09 10:40:45 +08001282 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001283 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001284 {
1285 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1286 return -1;
1287 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001288
Matthew Qin271927e2015-03-31 22:07:07 -04001289 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001290 } else {
1291 /* Validate the tags_addr */
1292 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001293 {
1294 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1295 return -1;
1296 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001297 /*
1298 * If appended dev tree is found, update the atags with
1299 * memory address to the DTB appended location on RAM.
1300 * Else update with the atags address in the kernel header
1301 */
1302 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001303 dtb = dev_tree_appended((void*)(image_addr + page_size),
1304 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001305 (void *)hdr->tags_addr);
1306 if (!dtb) {
1307 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001308 return -1;
1309 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001310 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001311 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001312
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001313 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1314 target_load_ssd_keystore();
1315
Shashank Mittal23b8f422010-04-16 19:27:21 -07001316unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001317
Dima Zavin77e41f32013-03-06 16:10:43 -08001318 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001319 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001320 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1321
1322 return 0;
1323}
1324
Dima Zavin214cc642009-01-26 11:16:21 -08001325int boot_linux_from_flash(void)
1326{
1327 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001328 struct ptentry *ptn;
1329 struct ptable *ptable;
1330 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001331
Shashank Mittalcd98d472011-08-02 14:29:24 -07001332 unsigned char *image_addr = 0;
1333 unsigned kernel_actual;
1334 unsigned ramdisk_actual;
1335 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001336 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001337
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001338#if DEVICE_TREE
1339 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001340 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001341 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001342 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001343#endif
1344
David Ng183a7422009-12-07 14:55:21 -08001345 if (target_is_emmc_boot()) {
1346 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1347 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1348 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1349 return -1;
1350 }
1351 goto continue_boot;
1352 }
1353
Dima Zavin214cc642009-01-26 11:16:21 -08001354 ptable = flash_get_ptable();
1355 if (ptable == NULL) {
1356 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1357 return -1;
1358 }
1359
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001360 if(!boot_into_recovery)
1361 {
1362 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001363
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001364 if (ptn == NULL) {
1365 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1366 return -1;
1367 }
1368 }
1369 else
1370 {
1371 ptn = ptable_find(ptable, "recovery");
1372 if (ptn == NULL) {
1373 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1374 return -1;
1375 }
Dima Zavin214cc642009-01-26 11:16:21 -08001376 }
1377
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001378 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001379 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1380 return -1;
1381 }
Dima Zavin214cc642009-01-26 11:16:21 -08001382
1383 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001384 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001385 return -1;
1386 }
1387
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001388 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001389 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 -08001390 return -1;
1391 }
1392
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001393 /*
1394 * Update the kernel/ramdisk/tags address if the boot image header
1395 * has default values, these default values come from mkbootimg when
1396 * the boot image is flashed using fastboot flash:raw
1397 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001398 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001399
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001400 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001401 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1402 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1403 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1404
1405 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1406 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1407
1408 /* Check if the addresses in the header are valid. */
1409 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1410 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1411 {
1412 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1413 return -1;
1414 }
1415
1416#ifndef DEVICE_TREE
1417 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1418 {
1419 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1420 return -1;
1421 }
1422#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001423
Shashank Mittalcd98d472011-08-02 14:29:24 -07001424 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001425 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001426 {
1427 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001428 offset = 0;
1429
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001430#if DEVICE_TREE
1431 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1432 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001433
1434 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1435 {
1436 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1437 return -1;
1438 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001439#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001440 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001441#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001442
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001443 dprintf(INFO, "Loading (%s) image (%d): start\n",
1444 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001445 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001446
Shashank Mittalcd98d472011-08-02 14:29:24 -07001447 /* Read image without signature */
1448 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1449 {
1450 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1451 return -1;
1452 }
Dima Zavin214cc642009-01-26 11:16:21 -08001453
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001454 dprintf(INFO, "Loading (%s) image (%d): done\n",
1455 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001456 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001457
Shashank Mittalcd98d472011-08-02 14:29:24 -07001458 offset = imagesize_actual;
1459 /* Read signature */
1460 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1461 {
1462 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001463 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001464 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001465
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301466 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001467
1468 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001469 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1470 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001471#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001472 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001473 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001474 {
1475 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1476 return -1;
1477 }
1478
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001479 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1480#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001481
1482 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001483 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001484 {
1485 write_device_info_flash(&device);
1486 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301487#if USE_PCOM_SECBOOT
1488 set_tamper_flag(device.is_tampered);
1489#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001490 }
1491 else
1492 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001493 offset = page_size;
1494
Amol Jadib6be5c12012-11-14 13:39:51 -08001495 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1496 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1497 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1498
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001499 dprintf(INFO, "Loading (%s) image (%d): start\n",
1500 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1501
Amol Jadi492d5a52013-03-15 16:12:34 -07001502 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001503
1504 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001505 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1506 return -1;
1507 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001508 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001509
Amol Jadib6be5c12012-11-14 13:39:51 -08001510 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001511 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1512 return -1;
1513 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001514 offset += ramdisk_actual;
1515
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001516 dprintf(INFO, "Loading (%s) image (%d): done\n",
1517 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1518
Amol Jadi492d5a52013-03-15 16:12:34 -07001519 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001520
1521 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001522 offset += second_actual;
1523 /* Second image loading not implemented. */
1524 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001525 }
1526
1527#if DEVICE_TREE
1528 if(hdr->dt_size != 0) {
1529
1530 /* Read the device tree table into buffer */
1531 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1532 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1533 return -1;
1534 }
1535
1536 table = (struct dt_table*) dt_buf;
1537
Deepa Dinamani19648b42013-09-05 17:05:55 -07001538 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001539 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1540 return -1;
1541 }
1542
Deepa Dinamani19648b42013-09-05 17:05:55 -07001543 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1544 if (!table)
1545 return -1;
1546
1547 /* Read the entire device tree table into buffer */
1548 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1549 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1550 return -1;
1551 }
1552
1553
Joel Kingaa335dc2013-06-03 16:11:08 -07001554 /* Find index of device tree within device tree table */
1555 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001556 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1557 return -1;
1558 }
1559
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001560 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001561 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001562 {
1563 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1564 return -1;
1565 }
1566
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001567 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001568 if(flash_read(ptn, offset + dt_entry.offset,
1569 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001570 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1571 return -1;
1572 }
1573 }
1574#endif
1575
Shashank Mittalcd98d472011-08-02 14:29:24 -07001576 }
David Ng183a7422009-12-07 14:55:21 -08001577continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001578
Dima Zavin214cc642009-01-26 11:16:21 -08001579 /* TODO: create/pass atags to kernel */
1580
Ajay Dudanie28a6072011-07-01 13:59:46 -07001581 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001582 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001583 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1584
1585 return 0;
1586}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001587
Shashank Mittal162244e2011-08-08 19:01:25 -07001588void write_device_info_mmc(device_info *dev)
1589{
Shashank Mittal162244e2011-08-08 19:01:25 -07001590 unsigned long long ptn = 0;
1591 unsigned long long size;
1592 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001593 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001594 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001595 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001596
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001597 if (devinfo_present)
1598 index = partition_get_index("devinfo");
1599 else
1600 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001601
Shashank Mittal162244e2011-08-08 19:01:25 -07001602 ptn = partition_get_offset(index);
1603 if(ptn == 0)
1604 {
1605 return;
1606 }
1607
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001608 lun = partition_get_lun(index);
1609 mmc_set_lun(lun);
1610
Shashank Mittal162244e2011-08-08 19:01:25 -07001611 size = partition_get_size(index);
1612
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001613 blocksize = mmc_get_device_blocksize();
1614
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001615 if (devinfo_present)
1616 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1617 else
1618 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1619 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001620 {
1621 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001622 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001623 }
1624}
1625
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001626void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001627{
Shashank Mittal162244e2011-08-08 19:01:25 -07001628 unsigned long long ptn = 0;
1629 unsigned long long size;
1630 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001631 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001632 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001633
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001634 if ((index = partition_get_index("devinfo")) < 0)
1635 {
1636 devinfo_present = false;
1637 index = partition_get_index("aboot");
1638 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001639
Shashank Mittal162244e2011-08-08 19:01:25 -07001640 ptn = partition_get_offset(index);
1641 if(ptn == 0)
1642 {
1643 return;
1644 }
1645
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001646 mmc_set_lun(partition_get_lun(index));
1647
Shashank Mittal162244e2011-08-08 19:01:25 -07001648 size = partition_get_size(index);
1649
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001650 blocksize = mmc_get_device_blocksize();
1651
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001652 if (devinfo_present)
1653 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1654 else
1655 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1656 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001657 {
1658 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001659 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001660 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001661}
1662
1663void write_device_info_flash(device_info *dev)
1664{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001665 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001666 struct ptentry *ptn;
1667 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001668 if(info == NULL)
1669 {
1670 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1671 ASSERT(0);
1672 }
1673 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001674 ptable = flash_get_ptable();
1675 if (ptable == NULL)
1676 {
1677 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1678 return;
1679 }
1680
1681 ptn = ptable_find(ptable, "devinfo");
1682 if (ptn == NULL)
1683 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001684 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001685 return;
1686 }
1687
1688 memcpy(info, dev, sizeof(device_info));
1689
1690 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1691 {
1692 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1693 return;
1694 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001695 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001696}
1697
vijay kumarc65876c2015-04-24 13:29:16 +05301698static int read_allow_oem_unlock(device_info *dev)
1699{
vijay kumarc65876c2015-04-24 13:29:16 +05301700 unsigned offset;
1701 int index;
1702 unsigned long long ptn;
1703 unsigned long long ptn_size;
1704 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001705 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301706
vijay kumarca2e6812015-07-08 20:28:25 +05301707 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301708 if (index == INVALID_PTN)
1709 {
vijay kumarca2e6812015-07-08 20:28:25 +05301710 index = partition_get_index(frp_ptns[1]);
1711 if (index == INVALID_PTN)
1712 {
1713 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1714 return -1;
1715 }
vijay kumarc65876c2015-04-24 13:29:16 +05301716 }
1717
1718 ptn = partition_get_offset(index);
1719 ptn_size = partition_get_size(index);
1720 offset = ptn_size - blocksize;
1721
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001722 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301723 {
1724 dprintf(CRITICAL, "Reading MMC failed\n");
1725 return -1;
1726 }
1727
1728 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1729 is_allow_unlock = buf[blocksize-1] & 0x01;
1730 return 0;
1731}
1732
1733static int write_allow_oem_unlock(bool allow_unlock)
1734{
vijay kumarc65876c2015-04-24 13:29:16 +05301735 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301736 int index;
1737 unsigned long long ptn;
1738 unsigned long long ptn_size;
1739 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001740 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301741
vijay kumarca2e6812015-07-08 20:28:25 +05301742 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301743 if (index == INVALID_PTN)
1744 {
vijay kumarca2e6812015-07-08 20:28:25 +05301745 index = partition_get_index(frp_ptns[1]);
1746 if (index == INVALID_PTN)
1747 {
1748 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1749 return -1;
1750 }
vijay kumarc65876c2015-04-24 13:29:16 +05301751 }
1752
1753 ptn = partition_get_offset(index);
1754 ptn_size = partition_get_size(index);
1755 offset = ptn_size - blocksize;
1756
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001757 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301758 {
1759 dprintf(CRITICAL, "Reading MMC failed\n");
1760 return -1;
1761 }
1762
1763 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1764 buf[blocksize-1] = allow_unlock;
1765 if (mmc_write(ptn + offset, blocksize, buf))
1766 {
1767 dprintf(CRITICAL, "Writing MMC failed\n");
1768 return -1;
1769 }
1770
1771 return 0;
1772}
1773
Shashank Mittal162244e2011-08-08 19:01:25 -07001774void read_device_info_flash(device_info *dev)
1775{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001776 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001777 struct ptentry *ptn;
1778 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001779 if(info == NULL)
1780 {
1781 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1782 ASSERT(0);
1783 }
1784 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001785 ptable = flash_get_ptable();
1786 if (ptable == NULL)
1787 {
1788 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1789 return;
1790 }
1791
1792 ptn = ptable_find(ptable, "devinfo");
1793 if (ptn == NULL)
1794 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001795 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001796 return;
1797 }
1798
1799 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1800 {
1801 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1802 return;
1803 }
1804
1805 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1806 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001807 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1808 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001809 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001810 write_device_info_flash(info);
1811 }
1812 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001813 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001814}
1815
1816void write_device_info(device_info *dev)
1817{
1818 if(target_is_emmc_boot())
1819 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001820 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1821 if(info == NULL)
1822 {
1823 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1824 ASSERT(0);
1825 }
1826 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001827 memcpy(info, dev, sizeof(struct device_info));
1828
1829#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001830 if (is_secure_boot_enable()) {
1831 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1832 ASSERT(0);
1833 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001834 else
1835 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001836#else
1837 write_device_info_mmc(info);
1838#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001839 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001840 }
1841 else
1842 {
1843 write_device_info_flash(dev);
1844 }
1845}
1846
1847void read_device_info(device_info *dev)
1848{
1849 if(target_is_emmc_boot())
1850 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001851 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1852 if(info == NULL)
1853 {
1854 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1855 ASSERT(0);
1856 }
1857 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001858
1859#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001860 if (is_secure_boot_enable()) {
1861 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1862 ASSERT(0);
1863 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001864 else
1865 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001866#else
1867 read_device_info_mmc(info);
1868#endif
1869
1870 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1871 {
1872 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08001873 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001874 info->is_unlocked = 0;
lijuang511a2b52015-08-14 20:50:51 +08001875 info->is_unlock_critical = 0;
1876 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001877 info->is_unlocked = 1;
lijuang511a2b52015-08-14 20:50:51 +08001878 info->is_unlock_critical = 1;
1879 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001880 info->is_tampered = 0;
1881 info->charger_screen_enabled = 0;
Sridhar Parasuram287e5062015-08-24 16:17:22 -07001882 info->verity_mode = 1; //enforcing by default
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001883 write_device_info(info);
1884 }
1885 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001886 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001887 }
1888 else
1889 {
1890 read_device_info_flash(dev);
1891 }
1892}
1893
1894void reset_device_info()
1895{
1896 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001897 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001898 write_device_info(&device);
1899}
1900
1901void set_device_root()
1902{
1903 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001904 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001905 write_device_info(&device);
1906}
1907
lijuang4ece1e72015-08-14 21:02:36 +08001908/* set device unlock value
1909 * Must check FRP before call this function
1910 * Need to wipe data when unlock status changed
1911 * type 0: oem unlock
1912 * type 1: unlock critical
1913 * status 0: unlock as false
1914 * status 1: lock as true
1915 */
1916void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08001917{
lijuang4ece1e72015-08-14 21:02:36 +08001918 if (type == UNLOCK)
1919 device.is_unlocked = status;
1920 else if (type == UNLOCK_CRITICAL)
1921 device.is_unlock_critical = status;
1922
1923 write_device_info(&device);
1924}
1925
1926static void set_device_unlock(int type, bool status)
1927{
1928 int is_unlocked = -1;
1929 char response[MAX_RSP_SIZE];
1930
1931 /* check device unlock status if it is as expected */
1932 if (type == UNLOCK)
1933 is_unlocked = device.is_unlocked;
1934 else if (type == UNLOCK_CRITICAL)
1935 is_unlocked = device.is_unlock_critical;
1936
1937 if (is_unlocked == status) {
1938 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
1939 fastboot_info(response);
1940 fastboot_okay("");
1941 return;
1942 }
1943
1944 /* status is true, it means to unlock device */
1945 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08001946 if(!is_allow_unlock) {
1947 fastboot_fail("oem unlock is not allowed");
1948 return;
1949 }
1950
lijuang4ece1e72015-08-14 21:02:36 +08001951#if FBCON_DISPLAY_MSG
1952 display_unlock_menu_thread(type);
1953 fastboot_okay("");
1954 return;
1955#else
1956 if (type == UNLOCK) {
1957 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
1958 return;
1959 }
1960#endif
lijuang21f12f52015-08-22 16:22:19 +08001961 }
lijuang4ece1e72015-08-14 21:02:36 +08001962
1963 set_device_unlock_value(type, status);
1964
1965 /* wipe data */
1966 struct recovery_message msg;
1967
1968 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
1969 write_misc(0, &msg, sizeof(msg));
1970
1971 fastboot_okay("");
1972 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08001973}
1974
lijuang511a2b52015-08-14 20:50:51 +08001975static bool critical_flash_allowed(const char * entry)
1976{
1977 uint32_t i = 0;
1978 if (entry == NULL)
1979 return false;
1980
1981 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
1982 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
1983 return true;
1984 }
1985 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04001986}
1987
1988#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07001989int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
1990{
1991 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07001992 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07001993 struct dt_table *table;
1994 struct dt_entry dt_entry;
1995 uint32_t dt_hdr_size;
1996 unsigned int compressed_size = 0;
1997 unsigned int dtb_size = 0;
1998 unsigned int out_avai_len = 0;
1999 unsigned char *out_addr = NULL;
2000 unsigned char *best_match_dt_addr = NULL;
2001 int rc;
2002
2003 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2004
2005 if(hdr->dt_size != 0) {
2006 /* add kernel offset */
2007 dt_image_offset += page_size;
2008 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002009 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002010
Deepa Dinamani19648b42013-09-05 17:05:55 -07002011 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002012 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2013 dt_image_offset += n;
2014
Joel Kingaa335dc2013-06-03 16:11:08 -07002015 /* add second offset */
2016 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002017 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2018 dt_image_offset += n;
2019 }
2020
Matthew Qin271927e2015-03-31 22:07:07 -04002021 /* offset now point to start of dt.img */
2022 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2023
2024 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2025 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002026 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002027 }
2028 /* Find index of device tree within device tree table */
2029 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2030 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2031 return -1;
Matthew Qin0b15b322015-05-19 05:20:54 -04002032 }
Matthew Qin271927e2015-03-31 22:07:07 -04002033
2034 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2035 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
Matthew Qin0b15b322015-05-19 05:20:54 -04002036 {
Matthew Qin271927e2015-03-31 22:07:07 -04002037 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2038 out_avai_len = target_get_max_flash_size() - scratch_offset;
2039 dprintf(INFO, "decompressing dtb: start\n");
2040 rc = decompress(best_match_dt_addr,
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002041 dt_entry.size, out_addr, out_avai_len,
Matthew Qin271927e2015-03-31 22:07:07 -04002042 &compressed_size, &dtb_size);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002043 if (rc)
2044 {
2045 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
2046 ASSERT(0);
2047 }
Amol Jadicb524072012-08-09 16:40:18 -07002048
Matthew Qin271927e2015-03-31 22:07:07 -04002049 dprintf(INFO, "decompressing dtb: done\n");
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002050 best_match_dt_addr = out_addr;
2051 } else {
Amol Jadicb524072012-08-09 16:40:18 -07002052 dtb_size = dt_entry.size;
2053 }
2054 /* Validate and Read device device tree in the "tags_add */
2055 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
2056 {
2057 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002058 return -1;
2059 }
Amit Blay8a510302015-08-17 09:20:01 +03002060
2061 /* Read device device tree in the "tags_add */
2062 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002063 } else
2064 return -1;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002065
2066 /* Everything looks fine. Return success. */
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002067 return 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002068}
2069#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002070
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002071void cmd_boot(const char *arg, void *data, unsigned sz)
2072{
Matthew Qinbb7923d2015-02-09 10:56:09 +08002073#ifdef MDTP_SUPPORT
2074 static bool is_mdtp_activated = 0;
2075#endif /* MDTP_SUPPORT */
2076 unsigned kernel_actual;
2077 unsigned ramdisk_actual;
2078 uint32_t image_actual;
Matthew Qin271927e2015-03-31 22:07:07 -04002079 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002080 uint32_t sig_actual = SIGNATURE_SIZE;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002081 struct boot_img_hdr *hdr = NULL;
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002082 struct kernel64_hdr *kptr = NULL;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002083 char *ptr = ((char*) data);
2084 int ret = 0;
2085 uint8_t dtb_copied = 0;
2086 unsigned int out_len = 0;
2087 unsigned int out_avai_len = 0;
2088 unsigned char *out_addr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002089 uint32_t dtb_offset = 0;
2090 unsigned char *kernel_start_addr = NULL;
2091 unsigned int kernel_size = 0;
2092 unsigned int scratch_offset = 0;
2093
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002094#if VERIFIED_BOOT
Brian Swetland9c4c0752009-01-25 16:23:50 -08002095 if(target_build_variant_user() && !device.is_unlocked)
2096 {
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002097 fastboot_fail("unlock device to use this command");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002098 return;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002099 }
2100#endif
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002101
2102 if (sz < sizeof(hdr)) {
2103 fastboot_fail("invalid bootimage header");
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002104 return;
2105 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002106
2107 hdr = (struct boot_img_hdr *)data;
2108
2109 /* ensure commandline is terminated */
2110 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2111
2112 if(target_is_emmc_boot() && hdr->page_size) {
2113 page_size = hdr->page_size;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002114 page_mask = page_size - 1;
2115 }
2116
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002117 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2118 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002119#if DEVICE_TREE
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002120 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2121#endif
2122
2123 image_actual = ADD_OF(page_size, kernel_actual);
2124 image_actual = ADD_OF(image_actual, ramdisk_actual);
2125 image_actual = ADD_OF(image_actual, dt_actual);
2126
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002127 if (target_use_signed_kernel() && (!device.is_unlocked))
2128 image_actual = ADD_OF(image_actual, sig_actual);
2129
2130 /* sz should have atleast raw boot image */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002131 if (image_actual > sz) {
Amit Blay4aa292f2015-04-28 21:55:59 +03002132 fastboot_fail("bootimage: incomplete or not signed");
2133 return;
2134 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002135
Amit Blay4aa292f2015-04-28 21:55:59 +03002136 /* Verify the boot image
Amit Blay8a510302015-08-17 09:20:01 +03002137 * device & page_size are initialized in aboot_init
2138 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002139 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002140 /* Pass size excluding signature size, otherwise we would try to
2141 * access signature beyond its length
Amit Blay4aa292f2015-04-28 21:55:59 +03002142 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002143#if VERIFIED_BOOT
2144 boot_verifier_init();
2145#endif
Amit Blay4aa292f2015-04-28 21:55:59 +03002146 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002147 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002148#ifdef MDTP_SUPPORT
2149 else
2150 {
2151 /* fastboot boot is not allowed when MDTP is activated */
2152 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002153
2154 if (!is_mdtp_activated) {
2155 ext_partition.partition = MDTP_PARTITION_NONE;
2156 mdtp_fwlock_verify_lock(&ext_partition);
2157 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002158 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002159
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002160 mdtp_activated(&is_mdtp_activated);
2161 if(is_mdtp_activated){
2162 dprintf(CRITICAL, "fastboot boot command is not available.\n");
2163 return;
2164 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002165#endif /* MDTP_SUPPORT */
2166
Brian Swetland9c4c0752009-01-25 16:23:50 -08002167 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002168 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2169 * If not, continue booting.
2170 */
2171 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2172 {
2173 out_addr = (unsigned char *)target_get_scratch_address();
2174 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2175 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002176 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002177 ret = decompress((unsigned char *)(ptr + page_size),
2178 hdr->kernel_size, out_addr, out_avai_len,
2179 &dtb_offset, &out_len);
2180 if (ret)
2181 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002182 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002183 ASSERT(0);
2184 }
2185
Matthew Qin0b15b322015-05-19 05:20:54 -04002186 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002187 kptr = (struct kernel64_hdr *)out_addr;
2188 kernel_start_addr = out_addr;
2189 kernel_size = out_len;
2190 } else {
2191 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2192 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2193 kernel_size = hdr->kernel_size;
2194 }
2195
2196 /*
Amol Jadicb524072012-08-09 16:40:18 -07002197 * Update the kernel/ramdisk/tags address if the boot image header
2198 * has default values, these default values come from mkbootimg when
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002199 * the boot image is flashed using fastboot flash:raw
2200 */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002201 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
2202
2203 /* Get virtual addresses since the hdr saves physical addresses. */
2204 hdr->kernel_addr = VA(hdr->kernel_addr);
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002205 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002206 hdr->tags_addr = VA(hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002207
Matthew Qinbb7923d2015-02-09 10:56:09 +08002208 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002209 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002210 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Brian Swetland9c4c0752009-01-25 16:23:50 -08002211 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2212 {
Dima Zavin214cc642009-01-26 11:16:21 -08002213 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
2214 return;
2215 }
2216
Dima Zavin214cc642009-01-26 11:16:21 -08002217#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002218 scratch_offset = image_actual + page_size + out_len;
Dima Zavin214cc642009-01-26 11:16:21 -08002219 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002220 ret = copy_dtb(data, scratch_offset);
Dima Zavin214cc642009-01-26 11:16:21 -08002221
2222 dtb_copied = !ret ? 1 : 0;
2223#else
2224 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2225 {
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002226 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2227 return;
2228 }
2229#endif
2230
2231 /* Load ramdisk & kernel */
2232 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002233 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002234
2235#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002236 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2237 {
2238 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2239 return;
2240 }
2241
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002242 /*
2243 * If dtb is not found look for appended DTB in the kernel.
2244 * If appended dev tree is found, update the atags with
2245 * memory address to the DTB appended location on RAM.
2246 * Else update with the atags address in the kernel header
2247 */
2248 if (!dtb_copied) {
2249 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002250 dtb = dev_tree_appended((void*)(ptr + page_size),
2251 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002252 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002253 if (!dtb) {
2254 fastboot_fail("dtb not found");
2255 return;
2256 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002257 }
neetidb4b24d62012-01-20 12:13:09 -08002258#endif
Kinson Chikf1a43512011-07-14 11:28:39 -07002259
Bikas Gurungd48bd242010-09-04 19:54:32 -07002260 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002261 fastboot_stop();
Kinson Chikf1a43512011-07-14 11:28:39 -07002262
Neeti Desaica8c9602011-10-06 11:40:00 -07002263 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Kinson Chikf1a43512011-07-14 11:28:39 -07002264 (const char*) hdr->cmdline, board_machtype(),
Neeti Desaica8c9602011-10-06 11:40:00 -07002265 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002266}
2267
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002268void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Brian Swetland9c4c0752009-01-25 16:23:50 -08002269{
2270 struct ptentry *ptn;
Dima Zavin214cc642009-01-26 11:16:21 -08002271 struct ptable *ptable;
2272
2273 ptable = flash_get_ptable();
2274 if (ptable == NULL) {
2275 fastboot_fail("partition table doesn't exist");
2276 return;
2277 }
2278
2279 ptn = ptable_find(ptable, arg);
2280 if (ptn == NULL) {
2281 fastboot_fail("unknown partition name");
2282 return;
2283 }
2284
2285 if (flash_erase(ptn)) {
2286 fastboot_fail("failed to erase partition");
2287 return;
2288 }
2289 fastboot_okay("");
2290}
2291
2292
2293void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2294{
Bikas Gurungd48bd242010-09-04 19:54:32 -07002295 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002296 unsigned long long size = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002297 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002298 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002299
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002300#if VERIFIED_BOOT
2301 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2302 {
2303 if(!device.is_unlocked)
2304 {
2305 fastboot_fail("unlock device to erase keystore");
2306 return;
2307 }
2308 }
2309#endif
2310
Bikas Gurungd48bd242010-09-04 19:54:32 -07002311 index = partition_get_index(arg);
2312 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002313 size = partition_get_size(index);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002314
2315 if(ptn == 0) {
2316 fastboot_fail("Partition table doesn't exist\n");
2317 return;
2318 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002319
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002320 lun = partition_get_lun(index);
2321 mmc_set_lun(lun);
2322
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002323 if (platform_boot_dev_isemmc())
2324 {
2325 if (mmc_erase_card(ptn, size)) {
2326 fastboot_fail("failed to erase partition\n");
2327 return;
2328 }
2329 } else {
2330 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2331 size = partition_get_size(index);
2332 if (size > DEFAULT_ERASE_SIZE)
2333 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002334
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002335 /* Simple inefficient version of erase. Just writing
2336 0 in first several blocks */
2337 if (mmc_write(ptn , size, (unsigned int *)out)) {
2338 fastboot_fail("failed to erase partition");
2339 return;
2340 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002341 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002342#if VERIFIED_BOOT
2343 if(!(strncmp(arg, "userdata", 8)))
2344 if(send_delete_keys_to_tz())
2345 ASSERT(0);
2346#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002347 fastboot_okay("");
2348}
2349
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002350void cmd_erase(const char *arg, void *data, unsigned sz)
2351{
lijuang65c5a822015-08-29 16:35:36 +08002352#if CHECK_BAT_VOLTAGE
2353 if (!target_battery_soc_ok()) {
2354 fastboot_fail("Warning: battery's capacity is very low\n");
2355 return;
2356 }
2357#endif
2358
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002359#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002360 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002361 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002362 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002363 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002364 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002365 return;
2366 }
2367 }
2368#endif
2369
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002370 if(target_is_emmc_boot())
2371 cmd_erase_mmc(arg, data, sz);
2372 else
2373 cmd_erase_nand(arg, data, sz);
2374}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002375
Channagoud Kadabiad259832015-05-29 11:14:17 -07002376static uint32_t aboot_get_secret_key()
2377{
2378 /* 0 is invalid secret key, update this implementation to return
2379 * device specific unique secret key
2380 */
2381 return 0;
2382}
2383
Ajay Dudani5c761132011-04-07 20:19:04 -07002384void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002385{
2386 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002387 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002388 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002389 char *token = NULL;
2390 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002391 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002392 uint8_t lun = 0;
2393 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002394
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002395 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002396 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002397 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002398 if(token)
2399 {
2400 lun = atoi(token);
2401 mmc_set_lun(lun);
2402 lun_set = true;
2403 }
2404
Mao Jinlong226f33a2014-07-04 17:24:10 +08002405 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002406 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002407 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2408 {
2409 if (!aboot_frp_unlock(pname, data, sz))
2410 {
2411 fastboot_info("FRP unlock successful");
2412 fastboot_okay("");
2413 }
2414 else
2415 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2416
2417 return;
2418 }
2419
Mao Jinlong226f33a2014-07-04 17:24:10 +08002420 if (!strcmp(pname, "partition"))
2421 {
2422 dprintf(INFO, "Attempt to write partition image.\n");
2423 if (write_partition(sz, (unsigned char *) data)) {
2424 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002425 return;
2426 }
2427 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002428 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002429 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002430#if VERIFIED_BOOT
2431 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2432 {
2433 if(!device.is_unlocked)
2434 {
2435 fastboot_fail("unlock device to flash keystore");
2436 return;
2437 }
2438 if(!boot_verify_validate_keystore((unsigned char *)data))
2439 {
2440 fastboot_fail("image is not a keystore file");
2441 return;
2442 }
2443 }
2444#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002445 index = partition_get_index(pname);
2446 ptn = partition_get_offset(index);
2447 if(ptn == 0) {
2448 fastboot_fail("partition table doesn't exist");
2449 return;
2450 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002451
Mao Jinlong226f33a2014-07-04 17:24:10 +08002452 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2453 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2454 fastboot_fail("image is not a boot image");
2455 return;
2456 }
2457 }
2458
2459 if(!lun_set)
2460 {
2461 lun = partition_get_lun(index);
2462 mmc_set_lun(lun);
2463 }
2464
2465 size = partition_get_size(index);
2466 if (ROUND_TO_PAGE(sz,511) > size) {
2467 fastboot_fail("size too large");
2468 return;
2469 }
2470 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2471 fastboot_fail("flash write failure");
2472 return;
2473 }
Greg Grisco6e754772011-06-23 12:19:39 -07002474 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002475 }
2476 fastboot_okay("");
2477 return;
2478}
2479
Ajay Dudanide984792015-03-02 09:57:41 -08002480void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2481{
2482 int i, images;
2483 meta_header_t *meta_header;
2484 img_header_entry_t *img_header_entry;
2485
2486 meta_header = (meta_header_t*) data;
2487 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2488
2489 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2490
2491 for (i=0; i<images; i++) {
2492
2493 if((img_header_entry[i].ptn_name == NULL) ||
2494 (img_header_entry[i].start_offset == 0) ||
2495 (img_header_entry[i].size == 0))
2496 break;
2497
2498 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2499 (void *) data + img_header_entry[i].start_offset,
2500 img_header_entry[i].size);
2501 }
2502
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002503 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2504 {
2505 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2506 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2507 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2508 }
2509 else
2510 {
2511 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2512 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2513 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2514 }
2515
2516 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002517 fastboot_okay("");
2518 return;
2519}
2520
Ajay Dudani5c761132011-04-07 20:19:04 -07002521void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2522{
2523 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002524 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002525 uint32_t *fill_buf = NULL;
2526 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002527 sparse_header_t *sparse_header;
2528 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002529 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002530 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302531 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002532 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302533 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002534 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302535 /*End of the sparse image address*/
2536 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002537
Kinson Chikf1a43512011-07-14 11:28:39 -07002538 index = partition_get_index(arg);
2539 ptn = partition_get_offset(index);
2540 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002541 fastboot_fail("partition table doesn't exist");
2542 return;
2543 }
2544
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302545 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302546
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002547 lun = partition_get_lun(index);
2548 mmc_set_lun(lun);
2549
vijay kumar800255e2015-04-24 20:26:19 +05302550 if (sz < sizeof(sparse_header_t)) {
2551 fastboot_fail("size too low");
2552 return;
2553 }
2554
Ajay Dudani5c761132011-04-07 20:19:04 -07002555 /* Read and skip over sparse image header */
2556 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302557
vijay kumar1321f342015-03-27 12:13:42 +05302558 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002559 fastboot_fail("size too large");
2560 return;
2561 }
2562
vijay kumarde4fcf62015-04-23 13:05:49 +05302563 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302564
2565 if (data_end < (uint32_t)data) {
2566 fastboot_fail("buffer overreads occured due to invalid sparse header");
2567 return;
2568 }
2569
vijay kumarde4fcf62015-04-23 13:05:49 +05302570 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002571 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302572 fastboot_fail("sparse header size mismatch");
2573 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002574 }
2575
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002576 dprintf (SPEW, "=== Sparse Image Header ===\n");
2577 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2578 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2579 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2580 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2581 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2582 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2583 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2584 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002585
2586 /* Start processing chunks */
2587 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2588 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302589 /* Make sure the total image size does not exceed the partition size */
2590 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2591 fastboot_fail("size too large");
2592 return;
2593 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002594 /* Read and skip over chunk header */
2595 chunk_header = (chunk_header_t *) data;
2596 data += sizeof(chunk_header_t);
2597
vijay kumar800255e2015-04-24 20:26:19 +05302598 if (data_end < (uint32_t)data) {
2599 fastboot_fail("buffer overreads occured due to invalid sparse header");
2600 return;
2601 }
2602
Ajay Dudani5c761132011-04-07 20:19:04 -07002603 dprintf (SPEW, "=== Chunk Header ===\n");
2604 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2605 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2606 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2607
vijay kumar800255e2015-04-24 20:26:19 +05302608 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002609 {
vijay kumar800255e2015-04-24 20:26:19 +05302610 fastboot_fail("chunk header size mismatch");
2611 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002612 }
2613
wufeng.jiang813dc352015-06-02 23:02:46 -04002614 if (!sparse_header->blk_sz ){
2615 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302616 return;
2617 }
2618
wufeng.jiang813dc352015-06-02 23:02:46 -04002619 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2620
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302621 /* Make sure that the chunk size calculated from sparse image does not
2622 * exceed partition size
2623 */
2624 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2625 {
2626 fastboot_fail("Chunk data size exceeds partition size");
2627 return;
2628 }
2629
Ajay Dudani5c761132011-04-07 20:19:04 -07002630 switch (chunk_header->chunk_type)
2631 {
2632 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002633 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002634 chunk_data_sz))
2635 {
2636 fastboot_fail("Bogus chunk size for chunk type Raw");
2637 return;
2638 }
2639
vijay kumar800255e2015-04-24 20:26:19 +05302640 if (data_end < (uint32_t)data + chunk_data_sz) {
2641 fastboot_fail("buffer overreads occured due to invalid sparse header");
2642 return;
2643 }
2644
wufeng.jiang813dc352015-06-02 23:02:46 -04002645 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2646 otherwise it will return in the line above
2647 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002648 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002649 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002650 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002651 {
2652 fastboot_fail("flash write failure");
2653 return;
2654 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302655 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2656 fastboot_fail("Bogus size for RAW chunk type");
2657 return;
2658 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002659 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002660 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002661 break;
2662
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002663 case CHUNK_TYPE_FILL:
2664 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2665 sizeof(uint32_t)))
2666 {
2667 fastboot_fail("Bogus chunk size for chunk type FILL");
2668 return;
2669 }
2670
2671 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2672 if (!fill_buf)
2673 {
2674 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2675 return;
2676 }
2677
vijay kumar800255e2015-04-24 20:26:19 +05302678 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2679 fastboot_fail("buffer overreads occured due to invalid sparse header");
2680 return;
2681 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002682 fill_val = *(uint32_t *)data;
2683 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002684
2685 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2686 {
2687 fill_buf[i] = fill_val;
2688 }
2689
wufeng.jiang813dc352015-06-02 23:02:46 -04002690 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002691 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302692 /* Make sure that the data written to partition does not exceed partition size */
2693 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2694 {
2695 fastboot_fail("Chunk data size for fill type exceeds partition size");
2696 return;
2697 }
2698
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002699 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2700 sparse_header->blk_sz,
2701 fill_buf))
2702 {
2703 fastboot_fail("flash write failure");
2704 free(fill_buf);
2705 return;
2706 }
2707
2708 total_blocks++;
2709 }
2710
2711 free(fill_buf);
2712 break;
2713
Ajay Dudani5c761132011-04-07 20:19:04 -07002714 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302715 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2716 fastboot_fail("bogus size for chunk DONT CARE type");
2717 return;
2718 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002719 total_blocks += chunk_header->chunk_sz;
2720 break;
2721
Ajay Dudani5c761132011-04-07 20:19:04 -07002722 case CHUNK_TYPE_CRC:
2723 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2724 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002725 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002726 return;
2727 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302728 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2729 fastboot_fail("bogus size for chunk CRC type");
2730 return;
2731 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002732 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302733 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2734 fastboot_fail("integer overflow occured");
2735 return;
2736 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002737 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302738 if (data_end < (uint32_t)data) {
2739 fastboot_fail("buffer overreads occured due to invalid sparse header");
2740 return;
2741 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002742 break;
2743
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002744 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002745 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002746 fastboot_fail("Unknown chunk type");
2747 return;
2748 }
2749 }
2750
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002751 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2752 total_blocks, sparse_header->total_blks);
2753
2754 if(total_blocks != sparse_header->total_blks)
2755 {
2756 fastboot_fail("sparse image write failure");
2757 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002758
2759 fastboot_okay("");
2760 return;
2761}
2762
2763void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2764{
2765 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002766 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002767
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002768#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002769 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2770 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002771 int ret=0;
2772 uint32 major_version=0;
2773 uint32 minor_version=0;
2774
2775 ret = scm_svc_version(&major_version,&minor_version);
2776 if(!ret)
2777 {
2778 if(major_version >= 2)
2779 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002780 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002781 {
2782 ret = encrypt_scm((uint32 **) &data, &sz);
2783 if (ret != 0) {
2784 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2785 return;
2786 }
2787
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002788 /* Protect only for SSD */
2789 if (!strcmp(arg, "ssd")) {
2790 ret = scm_protect_keystore((uint32 *) data, sz);
2791 if (ret != 0) {
2792 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2793 return;
2794 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002795 }
2796 }
2797 else
2798 {
2799 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2800 if(ret != 0)
2801 {
2802 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2803 return;
2804 }
2805 }
2806 }
2807 else
2808 {
2809 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2810 magic_number[1] == DECRYPT_MAGIC_1)
2811 {
2812 ret = decrypt_scm((uint32 **) &data, &sz);
2813 if (ret != 0) {
2814 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2815 return;
2816 }
2817 }
2818 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2819 magic_number[1] == ENCRYPT_MAGIC_1)
2820 {
2821 ret = encrypt_scm((uint32 **) &data, &sz);
2822 if (ret != 0) {
2823 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2824 return;
2825 }
2826 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002827 }
2828 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002829 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002830 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002831 dprintf(CRITICAL,"INVALID SVC Version\n");
2832 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002833 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002834#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002835
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002836#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002837 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002838 {
lijuang511a2b52015-08-14 20:50:51 +08002839 /* if device is locked:
2840 * common partition will not allow to be flashed
2841 * critical partition will allow to flash image.
2842 */
2843 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
2844 fastboot_fail("Partition flashing is not allowed");
2845 return;
2846 }
2847
2848 /* if device critical is locked:
2849 * common partition will allow to be flashed
2850 * critical partition will not allow to flash image.
2851 */
2852 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
2853 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002854 return;
2855 }
2856 }
2857#endif
2858
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002859 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002860 meta_header = (meta_header_t *) data;
2861 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002862 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002863 else if (meta_header->magic == META_HEADER_MAGIC)
2864 cmd_flash_meta_img(arg, data, sz);
2865 else
2866 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002867
2868#if VERIFIED_BOOT
2869 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
2870 {
2871 // reset dm_verity mode to enforcing
2872 device.verity_mode = 1;
2873 write_device_info(&device);
2874 }
2875#endif
2876
Ajay Dudani5c761132011-04-07 20:19:04 -07002877 return;
2878}
2879
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002880void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2881{
2882 struct ptentry *sys_ptn;
2883 struct ptable *ptable;
2884
2885 ptable = flash_get_ptable();
2886 if (ptable == NULL) {
2887 fastboot_fail("partition table doesn't exist");
2888 return;
2889 }
2890
2891 sys_ptn = ptable_find(ptable, "system");
2892 if (sys_ptn == NULL) {
2893 fastboot_fail("system partition not found");
2894 return;
2895 }
2896
2897 sz = ROUND_TO_PAGE(sz, page_mask);
2898 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2899 fastboot_fail("update_ubi_vol failed");
2900 else
2901 fastboot_okay("");
2902}
2903
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002904void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002905{
2906 struct ptentry *ptn;
2907 struct ptable *ptable;
2908 unsigned extra = 0;
2909
2910 ptable = flash_get_ptable();
2911 if (ptable == NULL) {
2912 fastboot_fail("partition table doesn't exist");
2913 return;
2914 }
2915
2916 ptn = ptable_find(ptable, arg);
2917 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002918 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2919 arg);
2920 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002921 return;
2922 }
2923
2924 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2925 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2926 fastboot_fail("image is not a boot image");
2927 return;
2928 }
2929 }
2930
Amol Jadi5c61a952012-05-04 17:05:35 -07002931 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002932 || !strcmp(ptn->name, "userdata")
2933 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002934 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002935 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002936 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002937 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002938 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002939
2940 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002941 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2942 if (flash_ubi_img(ptn, data, sz)) {
2943 fastboot_fail("flash write failure");
2944 return;
2945 }
2946 } else {
2947 if (flash_write(ptn, extra, data, sz)) {
2948 fastboot_fail("flash write failure");
2949 return;
2950 }
Dima Zavin214cc642009-01-26 11:16:21 -08002951 }
2952 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2953 fastboot_okay("");
2954}
2955
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002956void cmd_flash(const char *arg, void *data, unsigned sz)
2957{
lijuang65c5a822015-08-29 16:35:36 +08002958#if CHECK_BAT_VOLTAGE
2959 if (!target_battery_soc_ok()) {
2960 fastboot_fail("Warning: battery's capacity is very low\n");
2961 return;
2962 }
2963#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002964 if(target_is_emmc_boot())
2965 cmd_flash_mmc(arg, data, sz);
2966 else
2967 cmd_flash_nand(arg, data, sz);
2968}
2969
Dima Zavin214cc642009-01-26 11:16:21 -08002970void cmd_continue(const char *arg, void *data, unsigned sz)
2971{
2972 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002973 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002974
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002975 if (target_is_emmc_boot())
2976 {
lijuanga40d6302015-07-20 20:10:13 +08002977#if FBCON_DISPLAY_MSG
2978 keys_detect_init();
2979#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002980 boot_linux_from_mmc();
2981 }
2982 else
2983 {
2984 boot_linux_from_flash();
2985 }
Dima Zavin214cc642009-01-26 11:16:21 -08002986}
2987
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002988void cmd_reboot(const char *arg, void *data, unsigned sz)
2989{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002990 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002991 fastboot_okay("");
2992 reboot_device(0);
2993}
2994
2995void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2996{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002997 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002998 fastboot_okay("");
2999 reboot_device(FASTBOOT_MODE);
3000}
3001
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003002void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3003{
3004 dprintf(INFO, "Enabling charger screen check\n");
3005 device.charger_screen_enabled = 1;
3006 write_device_info(&device);
3007 fastboot_okay("");
3008}
3009
3010void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3011{
3012 dprintf(INFO, "Disabling charger screen check\n");
3013 device.charger_screen_enabled = 0;
3014 write_device_info(&device);
3015 fastboot_okay("");
3016}
3017
lijuanga25d8bb2015-09-16 13:11:52 +08003018void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3019{
3020 char *p = NULL;
3021 const char *delim = " \t\n\r";
3022
3023 if (arg) {
3024 p = strtok((char *)arg, delim);
3025 if (p) {
3026 if (!strncmp(p, "0", 1)) {
3027 device.charger_screen_enabled = 0;
3028 } else if (!strncmp(p, "1", 1)) {
3029 device.charger_screen_enabled = 1;
3030 }
3031 }
3032 }
3033
3034 /* update charger_screen_enabled value for getvar
3035 * command
3036 */
3037 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3038 device.charger_screen_enabled);
3039
3040 write_device_info(&device);
3041 fastboot_okay("");
3042}
3043
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303044void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3045{
3046 dprintf(INFO, "Selecting display panel %s\n", arg);
3047 if (arg)
3048 strlcpy(device.display_panel, arg,
3049 sizeof(device.display_panel));
3050 write_device_info(&device);
3051 fastboot_okay("");
3052}
3053
Shashank Mittal162244e2011-08-08 19:01:25 -07003054void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3055{
lijuang4ece1e72015-08-14 21:02:36 +08003056 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303057}
3058
3059void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3060{
lijuang4ece1e72015-08-14 21:02:36 +08003061 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303062 if(!is_allow_unlock) {
3063 fastboot_fail("oem unlock is not allowed");
3064 return;
3065 }
3066
lijuang4ece1e72015-08-14 21:02:36 +08003067 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303068
lijuang4ece1e72015-08-14 21:02:36 +08003069 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303070 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003071
vijay kumarc65876c2015-04-24 13:29:16 +05303072 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3073 write_misc(0, &msg, sizeof(msg));
3074
3075 fastboot_okay("");
3076 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003077 }
3078 fastboot_okay("");
3079}
3080
Channagoud Kadabiad259832015-05-29 11:14:17 -07003081static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3082{
3083 int ret = 1;
3084 uint32_t secret_key;
3085 char seckey_buffer[MAX_RSP_SIZE];
3086
3087 secret_key = aboot_get_secret_key();
3088 if (secret_key)
3089 {
3090 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3091 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3092 {
3093 is_allow_unlock = true;
3094 write_allow_oem_unlock(is_allow_unlock);
3095 ret = 0;
3096 }
3097 }
3098 return ret;
3099}
3100
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003101void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3102{
lijuang4ece1e72015-08-14 21:02:36 +08003103 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003104}
3105
Shashank Mittala0032282011-08-26 14:50:11 -07003106void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3107{
lijuang511a2b52015-08-14 20:50:51 +08003108 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003109 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003110 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003111 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3112 fastboot_info(response);
lijuang511a2b52015-08-14 20:50:51 +08003113 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3114 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003115 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003116 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303117 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3118 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003119 fastboot_okay("");
3120}
3121
lijuang511a2b52015-08-14 20:50:51 +08003122void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3123{
3124 char response[MAX_RSP_SIZE];
3125 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3126 fastboot_info(response);
3127 fastboot_okay("");
3128}
3129
3130void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3131{
lijuang4ece1e72015-08-14 21:02:36 +08003132 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003133}
3134
3135void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3136{
lijuang4ece1e72015-08-14 21:02:36 +08003137 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003138}
3139
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003140void cmd_preflash(const char *arg, void *data, unsigned sz)
3141{
3142 fastboot_okay("");
3143}
3144
Mao Flynn7b379f32015-04-20 00:28:30 +08003145static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303146
Mao Flynn7b379f32015-04-20 00:28:30 +08003147int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303148{
Mao Flynn7b379f32015-04-20 00:28:30 +08003149 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303150 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003151 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303152 return -1;
3153 return 0;
3154}
3155
Mao Flynn7b379f32015-04-20 00:28:30 +08003156int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003157{
3158 struct ptentry *ptn;
3159 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003160 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003161 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003162
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303163 ptable = flash_get_ptable();
3164 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003165 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3166 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303167 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003168
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303169 ptn = ptable_find(ptable, "splash");
3170 if (ptn == NULL) {
3171 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003172 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303173 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003174 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303175 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003176 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303177 }
3178
Mao Flynn7b379f32015-04-20 00:28:30 +08003179 header = (struct logo_img_header *)logo_header;
3180 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303181 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003182 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303183 }
3184
3185 fb_display = fbcon_display();
3186 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003187 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3188 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3189
3190 /* if the logo is full-screen size, remove "fbcon_clear()" */
3191 if ((header->width != fb_display->width)
3192 || (header->height != fb_display->height))
3193 fbcon_clear();
3194
3195 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3196 (uint32_t *)base,
3197 (header->blocks * 512))) {
3198 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3199 return -1;
3200 }
3201 fbcon_extract_to_screen(header, base);
3202 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003203 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003204
3205 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3206 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3207 return -1;
3208 }
3209
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303210 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003211 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3212 (uint32_t *)base,
3213 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303214 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303215 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003216 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003217 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303218 }
3219
Mao Flynn7b379f32015-04-20 00:28:30 +08003220 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303221}
3222
Mao Flynn7b379f32015-04-20 00:28:30 +08003223int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303224{
3225 int index = INVALID_PTN;
3226 unsigned long long ptn = 0;
3227 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003228 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003229 uint32_t blocksize, realsize, readsize;
3230 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303231
3232 index = partition_get_index("splash");
3233 if (index == 0) {
3234 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003235 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303236 }
3237
3238 ptn = partition_get_offset(index);
3239 if (ptn == 0) {
3240 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003241 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303242 }
3243
Mao Flynn1893a7f2015-06-03 12:03:36 +08003244 mmc_set_lun(partition_get_lun(index));
3245
3246 blocksize = mmc_get_device_blocksize();
3247 if (blocksize == 0) {
3248 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3249 return -1;
3250 }
3251
3252 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003253 if (!fb_display)
3254 {
3255 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3256 return -1;
3257 }
3258
Mao Flynn1893a7f2015-06-03 12:03:36 +08003259 base = (uint8_t *) fb_display->base;
3260
3261 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303262 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003263 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303264 }
3265
Mao Flynn1893a7f2015-06-03 12:03:36 +08003266 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003267 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303268 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003269 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303270 }
3271
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303272 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003273 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3274 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003275
Mao Flynn1893a7f2015-06-03 12:03:36 +08003276 realsize = header->blocks * 512;
3277 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3278
3279 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003280 if ((header->width != fb_display->width)
3281 || (header->height != fb_display->height))
3282 fbcon_clear();
3283
Mao Flynn1893a7f2015-06-03 12:03:36 +08003284 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003285 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3286 return -1;
3287 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003288
Mao Flynn1893a7f2015-06-03 12:03:36 +08003289 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3290 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303291
Mao Flynn1893a7f2015-06-03 12:03:36 +08003292 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3293 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3294 return -1;
3295 }
3296
3297 realsize = header->width * header->height * fb_display->bpp / 8;
3298 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3299
3300 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3301 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3302 fbcon_clear();
3303 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3304 return -1;
3305 }
3306 } else {
3307 if (mmc_read(ptn + blocksize ,
3308 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3309 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3310 return -1;
3311 }
3312 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3313 }
3314 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303315 }
3316
Mao Flynn7b379f32015-04-20 00:28:30 +08003317 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303318}
3319
Mao Flynn7b379f32015-04-20 00:28:30 +08003320int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303321{
3322 if (target_is_emmc_boot()) {
3323 return splash_screen_mmc();
3324 } else {
3325 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003326 }
3327}
3328
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003329/* Get the size from partiton name */
3330static void get_partition_size(const char *arg, char *response)
3331{
3332 uint64_t ptn = 0;
3333 uint64_t size;
3334 int index = INVALID_PTN;
3335
3336 index = partition_get_index(arg);
3337
3338 if (index == INVALID_PTN)
3339 {
3340 dprintf(CRITICAL, "Invalid partition index\n");
3341 return;
3342 }
3343
3344 ptn = partition_get_offset(index);
3345
3346 if(!ptn)
3347 {
3348 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3349 return;
3350 }
3351
3352 size = partition_get_size(index);
3353
3354 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3355 return;
3356}
3357
3358/*
3359 * Publish the partition type & size info
3360 * fastboot getvar will publish the required information.
3361 * fastboot getvar partition_size:<partition_name>: partition size in hex
3362 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3363 */
3364static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3365{
3366 uint8_t i;
3367
3368 for (i = 0; i < num_parts; i++) {
3369 get_partition_size(info[i].part_name, info[i].size_response);
3370
3371 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3372 {
3373 dprintf(CRITICAL, "partition size name truncated\n");
3374 return;
3375 }
3376 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3377 {
3378 dprintf(CRITICAL, "partition type name truncated\n");
3379 return;
3380 }
3381
3382 /* publish partition size & type info */
3383 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3384 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3385 }
3386}
3387
lijuang4ece1e72015-08-14 21:02:36 +08003388void get_product_name(unsigned char *buf)
3389{
3390 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3391 return;
3392}
3393
3394void get_bootloader_version(unsigned char *buf)
3395{
3396 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3397 return;
3398}
3399
3400void get_baseband_version(unsigned char *buf)
3401{
3402 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3403 return;
3404}
3405
3406bool is_device_locked()
3407{
3408 return device.is_unlocked ? false:true;
3409}
3410
Amol Jadi5edf3552013-07-23 14:15:34 -07003411/* register commands and variables for fastboot */
3412void aboot_fastboot_register_commands(void)
3413{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003414 int i;
lijuangf16461c2015-08-03 17:09:34 +08003415 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003416
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003417 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003418 /* By default the enabled list is empty. */
3419 {"", NULL},
3420 /* move commands enclosed within the below ifndef to here
3421 * if they need to be enabled in user build.
3422 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003423#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003424 /* Register the following commands only for non-user builds */
3425 {"flash:", cmd_flash},
3426 {"erase:", cmd_erase},
3427 {"boot", cmd_boot},
3428 {"continue", cmd_continue},
3429 {"reboot", cmd_reboot},
3430 {"reboot-bootloader", cmd_reboot_bootloader},
3431 {"oem unlock", cmd_oem_unlock},
3432 {"oem unlock-go", cmd_oem_unlock_go},
3433 {"oem lock", cmd_oem_lock},
3434 {"flashing unlock", cmd_oem_unlock},
3435 {"flashing lock", cmd_oem_lock},
3436 {"flashing lock_critical", cmd_flashing_lock_critical},
3437 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3438 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3439 {"oem device-info", cmd_oem_devinfo},
3440 {"preflash", cmd_preflash},
3441 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3442 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003443 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003444 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003445#endif
lijuang511a2b52015-08-14 20:50:51 +08003446 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003447
3448 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3449 for (i = 1; i < fastboot_cmds_count; i++)
3450 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3451
Amol Jadi5edf3552013-07-23 14:15:34 -07003452 /* publish variables and their values */
3453 fastboot_publish("product", TARGET(BOARD));
3454 fastboot_publish("kernel", "lk");
3455 fastboot_publish("serialno", sn_buf);
3456
3457 /*
3458 * partition info is supported only for emmc partitions
3459 * Calling this for NAND prints some error messages which
3460 * is harmless but misleading. Avoid calling this for NAND
3461 * devices.
3462 */
3463 if (target_is_emmc_boot())
3464 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3465
3466 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003467 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3468 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003469 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003470 /* Is the charger screen check enabled */
3471 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3472 device.charger_screen_enabled);
3473 fastboot_publish("charger-screen-enabled",
3474 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003475 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303476 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3477 device.display_panel);
3478 fastboot_publish("display-panel",
3479 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003480 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3481 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003482 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3483 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3484 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3485 target_is_emmc_boot()? "eMMC":"UFS");
3486 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003487#if CHECK_BAT_VOLTAGE
3488 snprintf(battery_voltage, MAX_RSP_SIZE, "%d",
3489 target_get_battery_voltage());
3490 fastboot_publish("battery-voltage", (const char *) battery_voltage);
3491 fastboot_publish("battery-soc-ok", target_battery_soc_ok()? "yes":"no");
3492#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003493}
3494
Brian Swetland9c4c0752009-01-25 16:23:50 -08003495void aboot_init(const struct app_descriptor *app)
3496{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003497 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03003498 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003499
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003500 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003501 if (target_is_emmc_boot())
3502 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003503 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003504 page_mask = page_size - 1;
3505 }
3506 else
3507 {
3508 page_size = flash_page_size();
3509 page_mask = page_size - 1;
3510 }
3511
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003512 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3513
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003514 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303515 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003516
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003517 /* Display splash screen if enabled */
3518#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003519#if NO_ALARM_DISPLAY
3520 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003521#endif
lijuang99c02d82015-02-13 19:04:34 +08003522 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003523#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003524 /* Wait if the display shutdown is in progress */
3525 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003526 if (!pm_appsbl_display_init_done())
3527 target_display_init(device.display_panel);
3528 else
3529 display_image_on_screen();
3530#else
lijuang99c02d82015-02-13 19:04:34 +08003531 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003532#endif
lijuang99c02d82015-02-13 19:04:34 +08003533 dprintf(SPEW, "Display Init: Done\n");
3534#if NO_ALARM_DISPLAY
3535 }
3536#endif
3537#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003538
Greg Griscod6250552011-06-29 14:40:23 -07003539 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003540 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003541
Dhaval Patel223ec952013-07-18 14:49:44 -07003542 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3543
Matthew Qindefd5562014-07-11 18:02:40 +08003544 /*
3545 * Check power off reason if user force reset,
3546 * if yes phone will do normal boot.
3547 */
3548 if (is_user_force_reset())
3549 goto normal_boot;
3550
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003551 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003552 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003553 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003554 dprintf(ALWAYS,"dload mode key sequence detected\n");
3555 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003556 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003557 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003558 }
3559 else
3560 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303561 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003562 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003563 }
3564 boot_into_fastboot = true;
3565 }
3566 if (!boot_into_fastboot)
3567 {
3568 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3569 boot_into_recovery = 1;
3570 if (!boot_into_recovery &&
3571 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003572 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003573 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003574 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003575 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003576 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003577 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003578
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003579#if USE_PON_REBOOT_REG
3580 reboot_mode = check_hard_reboot_mode();
3581#else
Ajay Dudani77421292010-10-27 19:34:06 -07003582 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003583#endif
3584 if (reboot_mode == RECOVERY_MODE)
3585 {
Ajay Dudani77421292010-10-27 19:34:06 -07003586 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003587 }
3588 else if(reboot_mode == FASTBOOT_MODE)
3589 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003590 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003591 }
3592 else if(reboot_mode == ALARM_BOOT)
3593 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003594 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003595 }
3596 else if (reboot_mode == DM_VERITY_ENFORCING)
3597 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003598 device.verity_mode = 1;
3599 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003600 }
3601 else if (reboot_mode == DM_VERITY_LOGGING)
3602 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003603 device.verity_mode = 0;
3604 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003605 }
3606 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3607 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003608 if(send_delete_keys_to_tz())
3609 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003610 }
3611
Matthew Qindefd5562014-07-11 18:02:40 +08003612normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003613 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003614 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003615 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003616 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003617 if(emmc_recovery_init())
3618 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3619 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003620 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003621 if((device.is_unlocked) || (device.is_tampered))
3622 {
3623 #ifdef TZ_TAMPER_FUSE
3624 set_tamper_fuse_cmd();
3625 #endif
3626 #if USE_PCOM_SECBOOT
3627 set_tamper_flag(device.is_tampered);
3628 #endif
3629 }
Shashank Mittala0032282011-08-26 14:50:11 -07003630 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003631
Pavel Nedev5d91d412013-04-29 11:34:24 +03003632 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003633 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003634 else
3635 {
3636 recovery_init();
3637 #if USE_PCOM_SECBOOT
3638 if((device.is_unlocked) || (device.is_tampered))
3639 set_tamper_flag(device.is_tampered);
3640 #endif
3641 boot_linux_from_flash();
3642 }
3643 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3644 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003645 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003646
Amol Jadi5edf3552013-07-23 14:15:34 -07003647 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003648
Amol Jadi5edf3552013-07-23 14:15:34 -07003649 /* register aboot specific fastboot commands */
3650 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003651
Amol Jadi5edf3552013-07-23 14:15:34 -07003652 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003653 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003654
3655 /* initialize and start fastboot */
3656 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003657#if FBCON_DISPLAY_MSG
3658 display_fastboot_menu_thread();
3659#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003660}
3661
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003662uint32_t get_page_size()
3663{
3664 return page_size;
3665}
3666
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003667/*
3668 * Calculated and save hash (SHA256) for non-signed boot image.
3669 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003670 * @param image_addr - Boot image address
3671 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003672 *
3673 * @return int - 0 on success, negative value on failure.
3674 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003675static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003676{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003677 unsigned int digest[8];
3678#if IMAGE_VERIF_ALGO_SHA1
3679 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3680#else
3681 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3682#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003683
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003684 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003685 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003686
3687 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003688 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003689
3690 return 0;
3691}
3692
Brian Swetland9c4c0752009-01-25 16:23:50 -08003693APP_START(aboot)
3694 .init = aboot_init,
3695APP_END