blob: dbb34434601f9c111abc9609ab0f70d5fda14b40 [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 Parasuramd69f7902015-07-10 13:31:17 -0700327 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700328#endif
329
Pavel Nedev5614d222013-06-17 18:01:02 +0300330 if (boot_into_recovery && gpt_exists)
331 cmdline_len += strlen(secondary_gpt_enable);
332
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200333 if(is_mdtp_activated)
334 cmdline_len += strlen(mdtp_activated_flag);
335
Pavel Nedev328ac822013-04-05 15:25:11 +0300336 if (boot_into_ffbm) {
337 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700338 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800339 /* reduce kernel console messages to speed-up boot */
340 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800341 } else if (boot_reason_alarm) {
342 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800343 } else if ((target_build_variant_user() || device.charger_screen_enabled)
344 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700345 pause_at_bootup = 1;
346 cmdline_len += strlen(battchg_pause);
347 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800348
Shashank Mittalcd98d472011-08-02 14:29:24 -0700349 if(target_use_signed_kernel() && auth_kernel_img) {
350 cmdline_len += strlen(auth_kernel);
351 }
352
Joonwoo Park61112782013-10-02 19:50:39 -0700353 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
354 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530355 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700356 have_target_boot_params = 1;
357 cmdline_len += strlen(target_boot_params);
358 }
359
Ajay Dudanid04110c2011-01-17 23:55:07 -0800360 /* Determine correct androidboot.baseband to use */
361 switch(target_baseband())
362 {
363 case BASEBAND_APQ:
364 cmdline_len += strlen(baseband_apq);
365 break;
366
367 case BASEBAND_MSM:
368 cmdline_len += strlen(baseband_msm);
369 break;
370
371 case BASEBAND_CSFB:
372 cmdline_len += strlen(baseband_csfb);
373 break;
374
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800375 case BASEBAND_SVLTE2A:
376 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800377 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700378
379 case BASEBAND_MDM:
380 cmdline_len += strlen(baseband_mdm);
381 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700382
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800383 case BASEBAND_MDM2:
384 cmdline_len += strlen(baseband_mdm2);
385 break;
386
Amol Jadi5c61a952012-05-04 17:05:35 -0700387 case BASEBAND_SGLTE:
388 cmdline_len += strlen(baseband_sglte);
389 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530390
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800391 case BASEBAND_SGLTE2:
392 cmdline_len += strlen(baseband_sglte2);
393 break;
394
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530395 case BASEBAND_DSDA:
396 cmdline_len += strlen(baseband_dsda);
397 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800398
399 case BASEBAND_DSDA2:
400 cmdline_len += strlen(baseband_dsda2);
401 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800402 }
403
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800404 if (cmdline) {
405 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530406 target_display_panel_node(display_panel_buf,
407 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800408 strlen(display_panel_buf)) {
409 cmdline_len += strlen(display_panel_buf);
410 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700411 }
412
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800413 if (target_warm_boot()) {
414 warm_boot = true;
415 cmdline_len += strlen(warmboot_cmdline);
416 }
417
David Ng183a7422009-12-07 14:55:21 -0800418 if (cmdline_len > 0) {
419 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800420 unsigned char *dst;
421
422 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
423 ASSERT(cmdline_final != NULL);
424 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700425
Amol Jadi168b7712012-03-06 16:15:00 -0800426 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800427 if (have_cmdline) {
428 src = cmdline;
429 while ((*dst++ = *src++));
430 }
431 if (target_is_emmc_boot()) {
432 src = emmc_cmdline;
433 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700434 have_cmdline = 1;
435 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800436#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700437 src = boot_dev_buf;
438 if (have_cmdline) --dst;
439 while ((*dst++ = *src++));
440#endif
David Ngf773dde2010-07-26 19:55:08 -0700441 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800442
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700443#if VERIFIED_BOOT
444 src = verified_state;
445 if(have_cmdline) --dst;
446 have_cmdline = 1;
447 while ((*dst++ = *src++));
448 src = vbsn[boot_state].name;
449 if(have_cmdline) --dst;
450 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700451
452 src = verity_mode;
453 if(have_cmdline) --dst;
454 while ((*dst++ = *src++));
455 src = vbvm[device.verity_mode].name;
456 if(have_cmdline) -- dst;
457 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700458#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800459 src = usb_sn_cmdline;
460 if (have_cmdline) --dst;
461 have_cmdline = 1;
462 while ((*dst++ = *src++));
463 src = sn_buf;
464 if (have_cmdline) --dst;
465 have_cmdline = 1;
466 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800467 if (warm_boot) {
468 if (have_cmdline) --dst;
469 src = warmboot_cmdline;
470 while ((*dst++ = *src++));
471 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800472
Pavel Nedev5614d222013-06-17 18:01:02 +0300473 if (boot_into_recovery && gpt_exists) {
474 src = secondary_gpt_enable;
475 if (have_cmdline) --dst;
476 while ((*dst++ = *src++));
477 }
478
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200479 if (is_mdtp_activated) {
480 src = mdtp_activated_flag;
481 if (have_cmdline) --dst;
482 while ((*dst++ = *src++));
483 }
484
Pavel Nedev328ac822013-04-05 15:25:11 +0300485 if (boot_into_ffbm) {
486 src = androidboot_mode;
487 if (have_cmdline) --dst;
488 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700489 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300490 if (have_cmdline) --dst;
491 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800492 src = loglevel;
493 if (have_cmdline) --dst;
494 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800495 } else if (boot_reason_alarm) {
496 src = alarmboot_cmdline;
497 if (have_cmdline) --dst;
498 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300499 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700500 src = battchg_pause;
501 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800502 while ((*dst++ = *src++));
503 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800504
Shashank Mittalcd98d472011-08-02 14:29:24 -0700505 if(target_use_signed_kernel() && auth_kernel_img) {
506 src = auth_kernel;
507 if (have_cmdline) --dst;
508 while ((*dst++ = *src++));
509 }
510
Ajay Dudanid04110c2011-01-17 23:55:07 -0800511 switch(target_baseband())
512 {
513 case BASEBAND_APQ:
514 src = baseband_apq;
515 if (have_cmdline) --dst;
516 while ((*dst++ = *src++));
517 break;
518
519 case BASEBAND_MSM:
520 src = baseband_msm;
521 if (have_cmdline) --dst;
522 while ((*dst++ = *src++));
523 break;
524
525 case BASEBAND_CSFB:
526 src = baseband_csfb;
527 if (have_cmdline) --dst;
528 while ((*dst++ = *src++));
529 break;
530
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800531 case BASEBAND_SVLTE2A:
532 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800533 if (have_cmdline) --dst;
534 while ((*dst++ = *src++));
535 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700536
537 case BASEBAND_MDM:
538 src = baseband_mdm;
539 if (have_cmdline) --dst;
540 while ((*dst++ = *src++));
541 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700542
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800543 case BASEBAND_MDM2:
544 src = baseband_mdm2;
545 if (have_cmdline) --dst;
546 while ((*dst++ = *src++));
547 break;
548
Amol Jadi5c61a952012-05-04 17:05:35 -0700549 case BASEBAND_SGLTE:
550 src = baseband_sglte;
551 if (have_cmdline) --dst;
552 while ((*dst++ = *src++));
553 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530554
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800555 case BASEBAND_SGLTE2:
556 src = baseband_sglte2;
557 if (have_cmdline) --dst;
558 while ((*dst++ = *src++));
559 break;
560
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530561 case BASEBAND_DSDA:
562 src = baseband_dsda;
563 if (have_cmdline) --dst;
564 while ((*dst++ = *src++));
565 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800566
567 case BASEBAND_DSDA2:
568 src = baseband_dsda2;
569 if (have_cmdline) --dst;
570 while ((*dst++ = *src++));
571 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800572 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700573
574 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700575 src = display_panel_buf;
576 if (have_cmdline) --dst;
577 while ((*dst++ = *src++));
578 }
Joonwoo Park61112782013-10-02 19:50:39 -0700579
580 if (have_target_boot_params) {
581 if (have_cmdline) --dst;
582 src = target_boot_params;
583 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530584 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700585 }
Neeti Desaie245d492012-06-01 12:52:13 -0700586 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700587
588
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700589 if (boot_dev_buf)
590 free(boot_dev_buf);
591
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800592 if (cmdline_final)
593 dprintf(INFO, "cmdline: %s\n", cmdline_final);
594 else
595 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700596 return cmdline_final;
597}
598
599unsigned *atag_core(unsigned *ptr)
600{
601 /* CORE */
602 *ptr++ = 2;
603 *ptr++ = 0x54410001;
604
605 return ptr;
606
607}
608
609unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
610 unsigned ramdisk_size)
611{
612 if (ramdisk_size) {
613 *ptr++ = 4;
614 *ptr++ = 0x54420005;
615 *ptr++ = (unsigned)ramdisk;
616 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800617 }
618
Neeti Desaie245d492012-06-01 12:52:13 -0700619 return ptr;
620}
621
622unsigned *atag_ptable(unsigned **ptr_addr)
623{
624 int i;
625 struct ptable *ptable;
626
627 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700628 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
629 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700630 *(*ptr_addr)++ = 0x4d534d70;
631 for (i = 0; i < ptable->count; ++i)
632 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
633 }
634
635 return (*ptr_addr);
636}
637
638unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
639{
640 int cmdline_length = 0;
641 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700642 char *dest;
643
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800644 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700645 n = (cmdline_length + 4) & (~3);
646
647 *ptr++ = (n / 4) + 2;
648 *ptr++ = 0x54410009;
649 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800650 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700651 ptr += (n / 4);
652
653 return ptr;
654}
655
656unsigned *atag_end(unsigned *ptr)
657{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800658 /* END */
659 *ptr++ = 0;
660 *ptr++ = 0;
661
Neeti Desaie245d492012-06-01 12:52:13 -0700662 return ptr;
663}
664
665void generate_atags(unsigned *ptr, const char *cmdline,
666 void *ramdisk, unsigned ramdisk_size)
667{
668
669 ptr = atag_core(ptr);
670 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
671 ptr = target_atag_mem(ptr);
672
673 /* Skip NAND partition ATAGS for eMMC boot */
674 if (!target_is_emmc_boot()){
675 ptr = atag_ptable(&ptr);
676 }
677
678 ptr = atag_cmdline(ptr, cmdline);
679 ptr = atag_end(ptr);
680}
681
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700682typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700683void boot_linux(void *kernel, unsigned *tags,
684 const char *cmdline, unsigned machtype,
685 void *ramdisk, unsigned ramdisk_size)
686{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800687 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800688#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700689 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800690#endif
691
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700692 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800693 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800694 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800695
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530696 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700697
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800698 final_cmdline = update_cmdline((const char*)cmdline);
699
Neeti Desai17379b82012-06-04 18:42:53 -0700700#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800701 dprintf(INFO, "Updating device tree: start\n");
702
Neeti Desai17379b82012-06-04 18:42:53 -0700703 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530704 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700705 if(ret)
706 {
707 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
708 ASSERT(0);
709 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800710 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700711#else
Neeti Desaie245d492012-06-01 12:52:13 -0700712 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800713 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700714#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700715
Maria Yu52254c02014-07-04 16:14:54 +0800716 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700717
718#if VERIFIED_BOOT
719 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700720 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700721 {
722 dprintf(INFO, "Failed to write protect dev info\n");
723 ASSERT(0);
724 }
725#endif
726
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700727 /* Perform target specific cleanup */
728 target_uninit();
729
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800730 /* Turn off splash screen if enabled */
731#if DISPLAY_SPLASH_SCREEN
732 target_display_shutdown();
733#endif
734
735
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800736 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530737 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800738
739 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700740
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +0530741 /* Initialise wdog to catch early kernel crashes */
742#if WDOG_SUPPORT
743 msm_wdog_init();
744#endif
Amol Jadi4421e652011-06-16 15:00:48 -0700745 /* do any platform specific cleanup before kernel entry */
746 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700747
Brian Swetland9c4c0752009-01-25 16:23:50 -0800748 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700749
Amol Jadi504f9fe2012-08-16 13:56:48 -0700750#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800751 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700752#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700753 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800754
755 if (IS_ARM64(kptr))
756 /* Jump to a 64bit kernel */
757 scm_elexec_call((paddr_t)kernel, tags_phys);
758 else
759 /* Jump to a 32bit kernel */
760 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800761}
762
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700763/* Function to check if the memory address range falls within the aboot
764 * boundaries.
765 * start: Start of the memory region
766 * size: Size of the memory region
767 */
768int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
769{
770 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800771 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700772 return -1;
773
774 /* Check for memory overlap. */
775 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
776 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700777 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700778 return 0;
779 else
780 return -1;
781}
782
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800783#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800784
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800785BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800786#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800787BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800788#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800789
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700790static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
791{
792 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800793
794#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800795#if IMAGE_VERIF_ALGO_SHA1
796 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
797#else
798 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
799#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800800#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700801
802 /* Assume device is rooted at this time. */
803 device.is_tampered = 1;
804
805 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
806
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700807#if VERIFIED_BOOT
808 if(boot_into_recovery)
809 {
810 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530811 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700812 }
813 else
814 {
815 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530816 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700817 }
818 boot_verify_print_state();
819#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700820 ret = image_verify((unsigned char *)bootimg_addr,
821 (unsigned char *)(bootimg_addr + bootimg_size),
822 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800823 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700824#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700825 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
826
827 if (ret)
828 {
829 /* Authorized kernel */
830 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700831 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700832 }
833
Amit Blay4aa292f2015-04-28 21:55:59 +0300834#ifdef MDTP_SUPPORT
835 {
836 /* Verify MDTP lock.
837 * For boot & recovery partitions, use aboot's verification result.
838 */
839 mdtp_ext_partition_verification_t ext_partition;
840 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
841 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
842 ext_partition.page_size = 0; /* Not needed since already validated */
843 ext_partition.image_addr = 0; /* Not needed since already validated */
844 ext_partition.image_size = 0; /* Not needed since already validated */
845 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
846 mdtp_fwlock_verify_lock(&ext_partition);
847 }
848#endif /* MDTP_SUPPORT */
849
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700850#if USE_PCOM_SECBOOT
851 set_tamper_flag(device.is_tampered);
852#endif
853
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700854#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700855 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700856 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700857 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800858#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +0800859 display_bootverify_menu_thread(DISPLAY_MENU_RED);
lijuanga40d6302015-07-20 20:10:13 +0800860 wait_for_users_action();
861#else
862 dprintf(CRITICAL,
863 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
864 mdelay(5000);
865#endif
866
867 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700868 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800869#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +0800870 display_bootverify_menu_thread(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800871 wait_for_users_action();
872#else
873 dprintf(CRITICAL,
874 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
875 mdelay(5000);
876#endif
877 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700878 default:
lijuanga40d6302015-07-20 20:10:13 +0800879 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700880 }
881#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700882#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530883 if(device.is_tampered)
884 {
885 write_device_info_mmc(&device);
886 #ifdef TZ_TAMPER_FUSE
887 set_tamper_fuse_cmd();
888 #endif
889 #ifdef ASSERT_ON_TAMPER
890 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
891 ASSERT(0);
892 #endif
893 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700894#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700895}
896
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530897static bool check_format_bit()
898{
899 bool ret = false;
900 int index;
901 uint64_t offset;
902 struct boot_selection_info *in = NULL;
903 char *buf = NULL;
904
905 index = partition_get_index("bootselect");
906 if (index == INVALID_PTN)
907 {
908 dprintf(INFO, "Unable to locate /bootselect partition\n");
909 return ret;
910 }
911 offset = partition_get_offset(index);
912 if(!offset)
913 {
914 dprintf(INFO, "partition /bootselect doesn't exist\n");
915 return ret;
916 }
917 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
918 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530919 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530920 {
921 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
922 free(buf);
923 return ret;
924 }
925 in = (struct boot_selection_info *) buf;
926 if ((in->signature == BOOTSELECT_SIGNATURE) &&
927 (in->version == BOOTSELECT_VERSION)) {
928 if ((in->state_info & BOOTSELECT_FORMAT) &&
929 !(in->state_info & BOOTSELECT_FACTORY))
930 ret = true;
931 } else {
932 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
933 in->signature, in->version);
934 ASSERT(0);
935 }
936 free(buf);
937 return ret;
938}
939
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700940void boot_verifier_init()
941{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700942 uint32_t boot_state;
943 /* Check if device unlock */
944 if(device.is_unlocked)
945 {
946 boot_verify_send_event(DEV_UNLOCK);
947 boot_verify_print_state();
948 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
949 return;
950 }
951 else
952 {
953 boot_verify_send_event(BOOT_INIT);
954 }
955
956 /* Initialize keystore */
957 boot_state = boot_verify_keystore_init();
958 if(boot_state == YELLOW)
959 {
960 boot_verify_print_state();
961 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
962 }
963}
964
Shashank Mittal23b8f422010-04-16 19:27:21 -0700965int boot_linux_from_mmc(void)
966{
967 struct boot_img_hdr *hdr = (void*) buf;
968 struct boot_img_hdr *uhdr;
969 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700970 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700971 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700972 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700973
Shashank Mittalcd98d472011-08-02 14:29:24 -0700974 unsigned char *image_addr = 0;
975 unsigned kernel_actual;
976 unsigned ramdisk_actual;
977 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700978 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700979
Matthew Qin271927e2015-03-31 22:07:07 -0400980 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +0800981 unsigned int out_len = 0;
982 unsigned int out_avai_len = 0;
983 unsigned char *out_addr = NULL;
984 uint32_t dtb_offset = 0;
985 unsigned char *kernel_start_addr = NULL;
986 unsigned int kernel_size = 0;
987 int rc;
988
Neeti Desai465491e2012-07-31 12:53:35 -0700989#if DEVICE_TREE
990 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700991 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700992 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800993 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700994 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -0400995 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -0700996#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +0800997 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800998
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530999 if (check_format_bit())
1000 boot_into_recovery = 1;
1001
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001002 if (!boot_into_recovery) {
1003 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1004 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1005 if (rcode <= 0) {
1006 boot_into_ffbm = false;
1007 if (rcode < 0)
1008 dprintf(CRITICAL,"failed to get ffbm cookie");
1009 } else
1010 boot_into_ffbm = true;
1011 } else
1012 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001013 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1014 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1015 dprintf(INFO, "Unified boot method!\n");
1016 hdr = uhdr;
1017 goto unified_boot;
1018 }
Greg Griscod6250552011-06-29 14:40:23 -07001019 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001020 index = partition_get_index("boot");
1021 ptn = partition_get_offset(index);
1022 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001023 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1024 return -1;
1025 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001026 }
1027 else {
1028 index = partition_get_index("recovery");
1029 ptn = partition_get_offset(index);
1030 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001031 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1032 return -1;
1033 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001034 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001035 /* Set Lun for boot & recovery partitions */
1036 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001037
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301038 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001039 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1040 return -1;
1041 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001042
1043 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001044 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001045 return -1;
1046 }
1047
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001048 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301049
1050 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1051 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1052 return -1;
1053 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001054 page_size = hdr->page_size;
1055 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001056 }
1057
Matthew Qin49e51fa2015-02-09 10:40:45 +08001058 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1059 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001060
Matthew Qin49e51fa2015-02-09 10:40:45 +08001061 image_addr = (unsigned char *)target_get_scratch_address();
1062
1063#if DEVICE_TREE
1064 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1065 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1066#else
1067 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1068#endif
1069
1070#if VERIFIED_BOOT
1071 boot_verifier_init();
1072#endif
1073
1074 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
1075 {
1076 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1077 return -1;
1078 }
1079
Matthew Qinbb7923d2015-02-09 10:56:09 +08001080 /*
1081 * Update loading flow of bootimage to support compressed/uncompressed
1082 * bootimage on both 64bit and 32bit platform.
1083 * 1. Load bootimage from emmc partition onto DDR.
1084 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1085 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1086 * platform accordingly.
1087 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1088 */
1089
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001090 dprintf(INFO, "Loading (%s) image (%d): start\n",
1091 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001092 bs_set_timestamp(BS_KERNEL_LOAD_START);
1093
Matthew Qinbb7923d2015-02-09 10:56:09 +08001094 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001095 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1096 {
1097 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1098 return -1;
1099 }
1100
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001101 dprintf(INFO, "Loading (%s) image (%d): done\n",
1102 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1103
Matthew Qin49e51fa2015-02-09 10:40:45 +08001104 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1105
1106 /* Authenticate Kernel */
1107 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1108 (int) target_use_signed_kernel(),
1109 device.is_unlocked,
1110 device.is_tampered);
1111
1112 if(target_use_signed_kernel() && (!device.is_unlocked))
1113 {
1114 offset = imagesize_actual;
1115 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1116 {
1117 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1118 return -1;
1119 }
1120
1121 /* Read signature */
1122 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1123 {
1124 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1125 return -1;
1126 }
1127
1128 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
1129 } else {
1130 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1131 #ifdef TZ_SAVE_KERNEL_HASH
1132 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1133 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001134
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001135#if VERIFIED_BOOT
1136 if(boot_verify_get_state() == ORANGE)
1137 {
lijuanga40d6302015-07-20 20:10:13 +08001138#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +08001139 display_bootverify_menu_thread(DISPLAY_MENU_ORANGE);
lijuanga40d6302015-07-20 20:10:13 +08001140 wait_for_users_action();
1141#else
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001142 dprintf(CRITICAL,
lijuanga40d6302015-07-20 20:10:13 +08001143 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001144 mdelay(5000);
lijuanga40d6302015-07-20 20:10:13 +08001145#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001146 }
1147#endif
1148
Amit Blay4aa292f2015-04-28 21:55:59 +03001149#ifdef MDTP_SUPPORT
1150 {
1151 /* Verify MDTP lock.
1152 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1153 * since verification was skipped in aboot. The signature is not part of the loaded image.
1154 */
1155 mdtp_ext_partition_verification_t ext_partition;
1156 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1157 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1158 ext_partition.page_size = page_size;
1159 ext_partition.image_addr = (uint32)image_addr;
1160 ext_partition.image_size = imagesize_actual;
1161 ext_partition.sig_avail = FALSE;
1162 mdtp_fwlock_verify_lock(&ext_partition);
1163 }
1164#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001165 }
1166
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001167#if VERIFIED_BOOT
1168 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001169 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001170 ASSERT(0);
1171#endif
1172
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001173 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001174 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1175 * If not, continue booting.
1176 */
1177 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1178 {
1179 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1180 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001181 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001182 rc = decompress((unsigned char *)(image_addr + page_size),
1183 hdr->kernel_size, out_addr, out_avai_len,
1184 &dtb_offset, &out_len);
1185 if (rc)
1186 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001187 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001188 ASSERT(0);
1189 }
1190
Matthew Qin0b15b322015-05-19 05:20:54 -04001191 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001192 kptr = (struct kernel64_hdr *)out_addr;
1193 kernel_start_addr = out_addr;
1194 kernel_size = out_len;
1195 } else {
1196 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1197 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1198 kernel_size = hdr->kernel_size;
1199 }
1200
1201 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001202 * Update the kernel/ramdisk/tags address if the boot image header
1203 * has default values, these default values come from mkbootimg when
1204 * the boot image is flashed using fastboot flash:raw
1205 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001206 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001207
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001208 /* Get virtual addresses since the hdr saves physical addresses. */
1209 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1210 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1211 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001212
Matthew Qinbb7923d2015-02-09 10:56:09 +08001213 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001214 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001215 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001216 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1217 {
1218 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1219 return -1;
1220 }
1221
1222#ifndef DEVICE_TREE
1223 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1224 {
1225 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1226 return -1;
1227 }
1228#endif
1229
Matthew Qin49e51fa2015-02-09 10:40:45 +08001230 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001231 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001232 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001233
Matthew Qin49e51fa2015-02-09 10:40:45 +08001234 #if DEVICE_TREE
1235 if(hdr->dt_size) {
1236 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1237 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001238
Matthew Qin49e51fa2015-02-09 10:40:45 +08001239 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1240 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1241 return -1;
1242 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001243
Matthew Qin49e51fa2015-02-09 10:40:45 +08001244 /* Find index of device tree within device tree table */
1245 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1246 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1247 return -1;
1248 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001249
Matthew Qin271927e2015-03-31 22:07:07 -04001250 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1251 {
1252 unsigned int compressed_size = 0;
1253 out_addr += out_len;
1254 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001255 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001256 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1257 dt_entry.size, out_addr, out_avai_len,
1258 &compressed_size, &dtb_size);
1259 if (rc)
1260 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001261 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001262 ASSERT(0);
1263 }
1264
Matthew Qin0b15b322015-05-19 05:20:54 -04001265 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001266 best_match_dt_addr = out_addr;
1267 } else {
1268 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1269 dtb_size = dt_entry.size;
1270 }
1271
Matthew Qin49e51fa2015-02-09 10:40:45 +08001272 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001273 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001274 {
1275 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1276 return -1;
1277 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001278
Matthew Qin271927e2015-03-31 22:07:07 -04001279 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001280 } else {
1281 /* Validate the tags_addr */
1282 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001283 {
1284 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1285 return -1;
1286 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001287 /*
1288 * If appended dev tree is found, update the atags with
1289 * memory address to the DTB appended location on RAM.
1290 * Else update with the atags address in the kernel header
1291 */
1292 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001293 dtb = dev_tree_appended((void*)(image_addr + page_size),
1294 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001295 (void *)hdr->tags_addr);
1296 if (!dtb) {
1297 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001298 return -1;
1299 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001300 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001301 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001302
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001303 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1304 target_load_ssd_keystore();
1305
Shashank Mittal23b8f422010-04-16 19:27:21 -07001306unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001307
Dima Zavin77e41f32013-03-06 16:10:43 -08001308 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001309 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001310 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1311
1312 return 0;
1313}
1314
Dima Zavin214cc642009-01-26 11:16:21 -08001315int boot_linux_from_flash(void)
1316{
1317 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001318 struct ptentry *ptn;
1319 struct ptable *ptable;
1320 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001321
Shashank Mittalcd98d472011-08-02 14:29:24 -07001322 unsigned char *image_addr = 0;
1323 unsigned kernel_actual;
1324 unsigned ramdisk_actual;
1325 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001326 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001327
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001328#if DEVICE_TREE
1329 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001330 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001331 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001332 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001333#endif
1334
David Ng183a7422009-12-07 14:55:21 -08001335 if (target_is_emmc_boot()) {
1336 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1337 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1338 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1339 return -1;
1340 }
1341 goto continue_boot;
1342 }
1343
Dima Zavin214cc642009-01-26 11:16:21 -08001344 ptable = flash_get_ptable();
1345 if (ptable == NULL) {
1346 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1347 return -1;
1348 }
1349
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001350 if(!boot_into_recovery)
1351 {
1352 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001353
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001354 if (ptn == NULL) {
1355 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1356 return -1;
1357 }
1358 }
1359 else
1360 {
1361 ptn = ptable_find(ptable, "recovery");
1362 if (ptn == NULL) {
1363 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1364 return -1;
1365 }
Dima Zavin214cc642009-01-26 11:16:21 -08001366 }
1367
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001368 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001369 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1370 return -1;
1371 }
Dima Zavin214cc642009-01-26 11:16:21 -08001372
1373 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001374 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001375 return -1;
1376 }
1377
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001378 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001379 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 -08001380 return -1;
1381 }
1382
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001383 /*
1384 * Update the kernel/ramdisk/tags address if the boot image header
1385 * has default values, these default values come from mkbootimg when
1386 * the boot image is flashed using fastboot flash:raw
1387 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001388 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001389
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001390 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001391 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1392 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1393 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1394
1395 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1396 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1397
1398 /* Check if the addresses in the header are valid. */
1399 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1400 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1401 {
1402 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1403 return -1;
1404 }
1405
1406#ifndef DEVICE_TREE
1407 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1408 {
1409 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1410 return -1;
1411 }
1412#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001413
Shashank Mittalcd98d472011-08-02 14:29:24 -07001414 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001415 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001416 {
1417 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001418 offset = 0;
1419
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001420#if DEVICE_TREE
1421 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1422 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001423
1424 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1425 {
1426 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1427 return -1;
1428 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001429#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001430 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001431#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001432
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001433 dprintf(INFO, "Loading (%s) image (%d): start\n",
1434 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001435 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001436
Shashank Mittalcd98d472011-08-02 14:29:24 -07001437 /* Read image without signature */
1438 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1439 {
1440 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1441 return -1;
1442 }
Dima Zavin214cc642009-01-26 11:16:21 -08001443
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001444 dprintf(INFO, "Loading (%s) image (%d): done\n",
1445 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001446 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001447
Shashank Mittalcd98d472011-08-02 14:29:24 -07001448 offset = imagesize_actual;
1449 /* Read signature */
1450 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1451 {
1452 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001453 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001454 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001455
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301456 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001457
1458 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001459 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1460 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001461#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001462 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001463 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001464 {
1465 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1466 return -1;
1467 }
1468
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001469 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1470#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001471
1472 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001473 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001474 {
1475 write_device_info_flash(&device);
1476 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301477#if USE_PCOM_SECBOOT
1478 set_tamper_flag(device.is_tampered);
1479#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001480 }
1481 else
1482 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001483 offset = page_size;
1484
Amol Jadib6be5c12012-11-14 13:39:51 -08001485 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1486 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1487 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1488
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001489 dprintf(INFO, "Loading (%s) image (%d): start\n",
1490 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1491
Amol Jadi492d5a52013-03-15 16:12:34 -07001492 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001493
1494 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001495 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1496 return -1;
1497 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001498 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001499
Amol Jadib6be5c12012-11-14 13:39:51 -08001500 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001501 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1502 return -1;
1503 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001504 offset += ramdisk_actual;
1505
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001506 dprintf(INFO, "Loading (%s) image (%d): done\n",
1507 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1508
Amol Jadi492d5a52013-03-15 16:12:34 -07001509 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001510
1511 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001512 offset += second_actual;
1513 /* Second image loading not implemented. */
1514 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001515 }
1516
1517#if DEVICE_TREE
1518 if(hdr->dt_size != 0) {
1519
1520 /* Read the device tree table into buffer */
1521 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1522 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1523 return -1;
1524 }
1525
1526 table = (struct dt_table*) dt_buf;
1527
Deepa Dinamani19648b42013-09-05 17:05:55 -07001528 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001529 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1530 return -1;
1531 }
1532
Deepa Dinamani19648b42013-09-05 17:05:55 -07001533 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1534 if (!table)
1535 return -1;
1536
1537 /* Read the entire device tree table into buffer */
1538 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1539 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1540 return -1;
1541 }
1542
1543
Joel Kingaa335dc2013-06-03 16:11:08 -07001544 /* Find index of device tree within device tree table */
1545 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001546 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1547 return -1;
1548 }
1549
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001550 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001551 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001552 {
1553 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1554 return -1;
1555 }
1556
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001557 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001558 if(flash_read(ptn, offset + dt_entry.offset,
1559 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001560 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1561 return -1;
1562 }
1563 }
1564#endif
1565
Shashank Mittalcd98d472011-08-02 14:29:24 -07001566 }
David Ng183a7422009-12-07 14:55:21 -08001567continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001568
Dima Zavin214cc642009-01-26 11:16:21 -08001569 /* TODO: create/pass atags to kernel */
1570
Ajay Dudanie28a6072011-07-01 13:59:46 -07001571 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001572 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001573 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1574
1575 return 0;
1576}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001577
Shashank Mittal162244e2011-08-08 19:01:25 -07001578void write_device_info_mmc(device_info *dev)
1579{
Shashank Mittal162244e2011-08-08 19:01:25 -07001580 unsigned long long ptn = 0;
1581 unsigned long long size;
1582 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001583 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001584 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001585 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001586
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001587 if (devinfo_present)
1588 index = partition_get_index("devinfo");
1589 else
1590 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001591
Shashank Mittal162244e2011-08-08 19:01:25 -07001592 ptn = partition_get_offset(index);
1593 if(ptn == 0)
1594 {
1595 return;
1596 }
1597
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001598 lun = partition_get_lun(index);
1599 mmc_set_lun(lun);
1600
Shashank Mittal162244e2011-08-08 19:01:25 -07001601 size = partition_get_size(index);
1602
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001603 blocksize = mmc_get_device_blocksize();
1604
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001605 if (devinfo_present)
1606 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1607 else
1608 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1609 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001610 {
1611 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001612 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001613 }
1614}
1615
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001616void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001617{
Shashank Mittal162244e2011-08-08 19:01:25 -07001618 unsigned long long ptn = 0;
1619 unsigned long long size;
1620 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001621 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001622 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001623
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001624 if ((index = partition_get_index("devinfo")) < 0)
1625 {
1626 devinfo_present = false;
1627 index = partition_get_index("aboot");
1628 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001629
Shashank Mittal162244e2011-08-08 19:01:25 -07001630 ptn = partition_get_offset(index);
1631 if(ptn == 0)
1632 {
1633 return;
1634 }
1635
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001636 mmc_set_lun(partition_get_lun(index));
1637
Shashank Mittal162244e2011-08-08 19:01:25 -07001638 size = partition_get_size(index);
1639
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001640 blocksize = mmc_get_device_blocksize();
1641
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001642 if (devinfo_present)
1643 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1644 else
1645 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1646 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001647 {
1648 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001649 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001650 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001651}
1652
1653void write_device_info_flash(device_info *dev)
1654{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001655 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001656 struct ptentry *ptn;
1657 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001658 if(info == NULL)
1659 {
1660 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1661 ASSERT(0);
1662 }
1663 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001664 ptable = flash_get_ptable();
1665 if (ptable == NULL)
1666 {
1667 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1668 return;
1669 }
1670
1671 ptn = ptable_find(ptable, "devinfo");
1672 if (ptn == NULL)
1673 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001674 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001675 return;
1676 }
1677
1678 memcpy(info, dev, sizeof(device_info));
1679
1680 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1681 {
1682 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1683 return;
1684 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001685 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001686}
1687
vijay kumarc65876c2015-04-24 13:29:16 +05301688static int read_allow_oem_unlock(device_info *dev)
1689{
vijay kumarc65876c2015-04-24 13:29:16 +05301690 unsigned offset;
1691 int index;
1692 unsigned long long ptn;
1693 unsigned long long ptn_size;
1694 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001695 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301696
vijay kumarca2e6812015-07-08 20:28:25 +05301697 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301698 if (index == INVALID_PTN)
1699 {
vijay kumarca2e6812015-07-08 20:28:25 +05301700 index = partition_get_index(frp_ptns[1]);
1701 if (index == INVALID_PTN)
1702 {
1703 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1704 return -1;
1705 }
vijay kumarc65876c2015-04-24 13:29:16 +05301706 }
1707
1708 ptn = partition_get_offset(index);
1709 ptn_size = partition_get_size(index);
1710 offset = ptn_size - blocksize;
1711
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001712 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301713 {
1714 dprintf(CRITICAL, "Reading MMC failed\n");
1715 return -1;
1716 }
1717
1718 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1719 is_allow_unlock = buf[blocksize-1] & 0x01;
1720 return 0;
1721}
1722
1723static int write_allow_oem_unlock(bool allow_unlock)
1724{
vijay kumarc65876c2015-04-24 13:29:16 +05301725 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301726 int index;
1727 unsigned long long ptn;
1728 unsigned long long ptn_size;
1729 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001730 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301731
vijay kumarca2e6812015-07-08 20:28:25 +05301732 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301733 if (index == INVALID_PTN)
1734 {
vijay kumarca2e6812015-07-08 20:28:25 +05301735 index = partition_get_index(frp_ptns[1]);
1736 if (index == INVALID_PTN)
1737 {
1738 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1739 return -1;
1740 }
vijay kumarc65876c2015-04-24 13:29:16 +05301741 }
1742
1743 ptn = partition_get_offset(index);
1744 ptn_size = partition_get_size(index);
1745 offset = ptn_size - blocksize;
1746
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001747 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301748 {
1749 dprintf(CRITICAL, "Reading MMC failed\n");
1750 return -1;
1751 }
1752
1753 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1754 buf[blocksize-1] = allow_unlock;
1755 if (mmc_write(ptn + offset, blocksize, buf))
1756 {
1757 dprintf(CRITICAL, "Writing MMC failed\n");
1758 return -1;
1759 }
1760
1761 return 0;
1762}
1763
Shashank Mittal162244e2011-08-08 19:01:25 -07001764void read_device_info_flash(device_info *dev)
1765{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001766 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001767 struct ptentry *ptn;
1768 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001769 if(info == NULL)
1770 {
1771 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1772 ASSERT(0);
1773 }
1774 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001775 ptable = flash_get_ptable();
1776 if (ptable == NULL)
1777 {
1778 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1779 return;
1780 }
1781
1782 ptn = ptable_find(ptable, "devinfo");
1783 if (ptn == NULL)
1784 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001785 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001786 return;
1787 }
1788
1789 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1790 {
1791 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1792 return;
1793 }
1794
1795 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1796 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001797 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1798 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001799 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001800 write_device_info_flash(info);
1801 }
1802 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001803 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001804}
1805
1806void write_device_info(device_info *dev)
1807{
1808 if(target_is_emmc_boot())
1809 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001810 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1811 if(info == NULL)
1812 {
1813 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1814 ASSERT(0);
1815 }
1816 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001817 memcpy(info, dev, sizeof(struct device_info));
1818
1819#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001820 if (is_secure_boot_enable()) {
1821 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1822 ASSERT(0);
1823 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001824 else
1825 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001826#else
1827 write_device_info_mmc(info);
1828#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001829 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001830 }
1831 else
1832 {
1833 write_device_info_flash(dev);
1834 }
1835}
1836
1837void read_device_info(device_info *dev)
1838{
1839 if(target_is_emmc_boot())
1840 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001841 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1842 if(info == NULL)
1843 {
1844 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1845 ASSERT(0);
1846 }
1847 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001848
1849#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001850 if (is_secure_boot_enable()) {
1851 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1852 ASSERT(0);
1853 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001854 else
1855 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001856#else
1857 read_device_info_mmc(info);
1858#endif
1859
1860 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1861 {
1862 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08001863 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001864 info->is_unlocked = 0;
lijuang511a2b52015-08-14 20:50:51 +08001865 info->is_unlock_critical = 0;
1866 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001867 info->is_unlocked = 1;
lijuang511a2b52015-08-14 20:50:51 +08001868 info->is_unlock_critical = 1;
1869 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001870 info->is_tampered = 0;
1871 info->charger_screen_enabled = 0;
Sridhar Parasuram287e5062015-08-24 16:17:22 -07001872 info->verity_mode = 1; //enforcing by default
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001873 write_device_info(info);
1874 }
1875 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001876 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001877 }
1878 else
1879 {
1880 read_device_info_flash(dev);
1881 }
1882}
1883
1884void reset_device_info()
1885{
1886 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001887 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001888 write_device_info(&device);
1889}
1890
1891void set_device_root()
1892{
1893 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001894 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001895 write_device_info(&device);
1896}
1897
lijuang4ece1e72015-08-14 21:02:36 +08001898/* set device unlock value
1899 * Must check FRP before call this function
1900 * Need to wipe data when unlock status changed
1901 * type 0: oem unlock
1902 * type 1: unlock critical
1903 * status 0: unlock as false
1904 * status 1: lock as true
1905 */
1906void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08001907{
lijuang4ece1e72015-08-14 21:02:36 +08001908 if (type == UNLOCK)
1909 device.is_unlocked = status;
1910 else if (type == UNLOCK_CRITICAL)
1911 device.is_unlock_critical = status;
1912
1913 write_device_info(&device);
1914}
1915
1916static void set_device_unlock(int type, bool status)
1917{
1918 int is_unlocked = -1;
1919 char response[MAX_RSP_SIZE];
1920
1921 /* check device unlock status if it is as expected */
1922 if (type == UNLOCK)
1923 is_unlocked = device.is_unlocked;
1924 else if (type == UNLOCK_CRITICAL)
1925 is_unlocked = device.is_unlock_critical;
1926
1927 if (is_unlocked == status) {
1928 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
1929 fastboot_info(response);
1930 fastboot_okay("");
1931 return;
1932 }
1933
1934 /* status is true, it means to unlock device */
1935 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08001936 if(!is_allow_unlock) {
1937 fastboot_fail("oem unlock is not allowed");
1938 return;
1939 }
1940
lijuang4ece1e72015-08-14 21:02:36 +08001941#if FBCON_DISPLAY_MSG
1942 display_unlock_menu_thread(type);
1943 fastboot_okay("");
1944 return;
1945#else
1946 if (type == UNLOCK) {
1947 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
1948 return;
1949 }
1950#endif
lijuang21f12f52015-08-22 16:22:19 +08001951 }
lijuang4ece1e72015-08-14 21:02:36 +08001952
1953 set_device_unlock_value(type, status);
1954
1955 /* wipe data */
1956 struct recovery_message msg;
1957
1958 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
1959 write_misc(0, &msg, sizeof(msg));
1960
1961 fastboot_okay("");
1962 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08001963}
1964
lijuang511a2b52015-08-14 20:50:51 +08001965static bool critical_flash_allowed(const char * entry)
1966{
1967 uint32_t i = 0;
1968 if (entry == NULL)
1969 return false;
1970
1971 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
1972 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
1973 return true;
1974 }
1975 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04001976}
1977
1978#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07001979int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
1980{
1981 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07001982 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07001983 struct dt_table *table;
1984 struct dt_entry dt_entry;
1985 uint32_t dt_hdr_size;
1986 unsigned int compressed_size = 0;
1987 unsigned int dtb_size = 0;
1988 unsigned int out_avai_len = 0;
1989 unsigned char *out_addr = NULL;
1990 unsigned char *best_match_dt_addr = NULL;
1991 int rc;
1992
1993 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1994
1995 if(hdr->dt_size != 0) {
1996 /* add kernel offset */
1997 dt_image_offset += page_size;
1998 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001999 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002000
Deepa Dinamani19648b42013-09-05 17:05:55 -07002001 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002002 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2003 dt_image_offset += n;
2004
Joel Kingaa335dc2013-06-03 16:11:08 -07002005 /* add second offset */
2006 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002007 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2008 dt_image_offset += n;
2009 }
2010
Matthew Qin271927e2015-03-31 22:07:07 -04002011 /* offset now point to start of dt.img */
2012 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2013
2014 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2015 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002016 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002017 }
2018 /* Find index of device tree within device tree table */
2019 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2020 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2021 return -1;
Matthew Qin0b15b322015-05-19 05:20:54 -04002022 }
Matthew Qin271927e2015-03-31 22:07:07 -04002023
2024 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2025 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
Matthew Qin0b15b322015-05-19 05:20:54 -04002026 {
Matthew Qin271927e2015-03-31 22:07:07 -04002027 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2028 out_avai_len = target_get_max_flash_size() - scratch_offset;
2029 dprintf(INFO, "decompressing dtb: start\n");
2030 rc = decompress(best_match_dt_addr,
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002031 dt_entry.size, out_addr, out_avai_len,
Matthew Qin271927e2015-03-31 22:07:07 -04002032 &compressed_size, &dtb_size);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002033 if (rc)
2034 {
2035 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
2036 ASSERT(0);
2037 }
Amol Jadicb524072012-08-09 16:40:18 -07002038
Matthew Qin271927e2015-03-31 22:07:07 -04002039 dprintf(INFO, "decompressing dtb: done\n");
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002040 best_match_dt_addr = out_addr;
2041 } else {
Amol Jadicb524072012-08-09 16:40:18 -07002042 dtb_size = dt_entry.size;
2043 }
2044 /* Validate and Read device device tree in the "tags_add */
2045 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
2046 {
2047 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002048 return -1;
2049 }
Amit Blay8a510302015-08-17 09:20:01 +03002050
2051 /* Read device device tree in the "tags_add */
2052 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002053 } else
2054 return -1;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002055
2056 /* Everything looks fine. Return success. */
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002057 return 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002058}
2059#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002060
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002061void cmd_boot(const char *arg, void *data, unsigned sz)
2062{
Matthew Qinbb7923d2015-02-09 10:56:09 +08002063#ifdef MDTP_SUPPORT
2064 static bool is_mdtp_activated = 0;
2065#endif /* MDTP_SUPPORT */
2066 unsigned kernel_actual;
2067 unsigned ramdisk_actual;
2068 uint32_t image_actual;
Matthew Qin271927e2015-03-31 22:07:07 -04002069 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002070 uint32_t sig_actual = SIGNATURE_SIZE;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002071 struct boot_img_hdr *hdr = NULL;
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002072 struct kernel64_hdr *kptr = NULL;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002073 char *ptr = ((char*) data);
2074 int ret = 0;
2075 uint8_t dtb_copied = 0;
2076 unsigned int out_len = 0;
2077 unsigned int out_avai_len = 0;
2078 unsigned char *out_addr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002079 uint32_t dtb_offset = 0;
2080 unsigned char *kernel_start_addr = NULL;
2081 unsigned int kernel_size = 0;
2082 unsigned int scratch_offset = 0;
2083
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002084#if VERIFIED_BOOT
Brian Swetland9c4c0752009-01-25 16:23:50 -08002085 if(target_build_variant_user() && !device.is_unlocked)
2086 {
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002087 fastboot_fail("unlock device to use this command");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002088 return;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002089 }
2090#endif
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002091
2092 if (sz < sizeof(hdr)) {
2093 fastboot_fail("invalid bootimage header");
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002094 return;
2095 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002096
2097 hdr = (struct boot_img_hdr *)data;
2098
2099 /* ensure commandline is terminated */
2100 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2101
2102 if(target_is_emmc_boot() && hdr->page_size) {
2103 page_size = hdr->page_size;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002104 page_mask = page_size - 1;
2105 }
2106
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002107 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2108 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002109#if DEVICE_TREE
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002110 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2111#endif
2112
2113 image_actual = ADD_OF(page_size, kernel_actual);
2114 image_actual = ADD_OF(image_actual, ramdisk_actual);
2115 image_actual = ADD_OF(image_actual, dt_actual);
2116
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002117 if (target_use_signed_kernel() && (!device.is_unlocked))
2118 image_actual = ADD_OF(image_actual, sig_actual);
2119
2120 /* sz should have atleast raw boot image */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002121 if (image_actual > sz) {
Amit Blay4aa292f2015-04-28 21:55:59 +03002122 fastboot_fail("bootimage: incomplete or not signed");
2123 return;
2124 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002125
Amit Blay4aa292f2015-04-28 21:55:59 +03002126 /* Verify the boot image
Amit Blay8a510302015-08-17 09:20:01 +03002127 * device & page_size are initialized in aboot_init
2128 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002129 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002130 /* Pass size excluding signature size, otherwise we would try to
2131 * access signature beyond its length
Amit Blay4aa292f2015-04-28 21:55:59 +03002132 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002133#if VERIFIED_BOOT
2134 boot_verifier_init();
2135#endif
Amit Blay4aa292f2015-04-28 21:55:59 +03002136 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002137 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002138#ifdef MDTP_SUPPORT
2139 else
2140 {
2141 /* fastboot boot is not allowed when MDTP is activated */
2142 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002143
2144 if (!is_mdtp_activated) {
2145 ext_partition.partition = MDTP_PARTITION_NONE;
2146 mdtp_fwlock_verify_lock(&ext_partition);
2147 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002148 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002149
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002150 mdtp_activated(&is_mdtp_activated);
2151 if(is_mdtp_activated){
2152 dprintf(CRITICAL, "fastboot boot command is not available.\n");
2153 return;
2154 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002155#endif /* MDTP_SUPPORT */
2156
Brian Swetland9c4c0752009-01-25 16:23:50 -08002157 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002158 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2159 * If not, continue booting.
2160 */
2161 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2162 {
2163 out_addr = (unsigned char *)target_get_scratch_address();
2164 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2165 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002166 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002167 ret = decompress((unsigned char *)(ptr + page_size),
2168 hdr->kernel_size, out_addr, out_avai_len,
2169 &dtb_offset, &out_len);
2170 if (ret)
2171 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002172 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002173 ASSERT(0);
2174 }
2175
Matthew Qin0b15b322015-05-19 05:20:54 -04002176 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002177 kptr = (struct kernel64_hdr *)out_addr;
2178 kernel_start_addr = out_addr;
2179 kernel_size = out_len;
2180 } else {
2181 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2182 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2183 kernel_size = hdr->kernel_size;
2184 }
2185
2186 /*
Amol Jadicb524072012-08-09 16:40:18 -07002187 * Update the kernel/ramdisk/tags address if the boot image header
2188 * has default values, these default values come from mkbootimg when
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002189 * the boot image is flashed using fastboot flash:raw
2190 */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002191 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
2192
2193 /* Get virtual addresses since the hdr saves physical addresses. */
2194 hdr->kernel_addr = VA(hdr->kernel_addr);
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002195 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002196 hdr->tags_addr = VA(hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002197
Matthew Qinbb7923d2015-02-09 10:56:09 +08002198 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002199 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002200 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Brian Swetland9c4c0752009-01-25 16:23:50 -08002201 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2202 {
Dima Zavin214cc642009-01-26 11:16:21 -08002203 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
2204 return;
2205 }
2206
Dima Zavin214cc642009-01-26 11:16:21 -08002207#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002208 scratch_offset = image_actual + page_size + out_len;
Dima Zavin214cc642009-01-26 11:16:21 -08002209 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002210 ret = copy_dtb(data, scratch_offset);
Dima Zavin214cc642009-01-26 11:16:21 -08002211
2212 dtb_copied = !ret ? 1 : 0;
2213#else
2214 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2215 {
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002216 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2217 return;
2218 }
2219#endif
2220
2221 /* Load ramdisk & kernel */
2222 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002223 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002224
2225#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002226 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2227 {
2228 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2229 return;
2230 }
2231
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002232 /*
2233 * If dtb is not found look for appended DTB in the kernel.
2234 * If appended dev tree is found, update the atags with
2235 * memory address to the DTB appended location on RAM.
2236 * Else update with the atags address in the kernel header
2237 */
2238 if (!dtb_copied) {
2239 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002240 dtb = dev_tree_appended((void*)(ptr + page_size),
2241 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002242 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002243 if (!dtb) {
2244 fastboot_fail("dtb not found");
2245 return;
2246 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002247 }
neetidb4b24d62012-01-20 12:13:09 -08002248#endif
Kinson Chikf1a43512011-07-14 11:28:39 -07002249
Bikas Gurungd48bd242010-09-04 19:54:32 -07002250 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002251 fastboot_stop();
Kinson Chikf1a43512011-07-14 11:28:39 -07002252
Neeti Desaica8c9602011-10-06 11:40:00 -07002253 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Kinson Chikf1a43512011-07-14 11:28:39 -07002254 (const char*) hdr->cmdline, board_machtype(),
Neeti Desaica8c9602011-10-06 11:40:00 -07002255 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002256}
2257
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002258void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Brian Swetland9c4c0752009-01-25 16:23:50 -08002259{
2260 struct ptentry *ptn;
Dima Zavin214cc642009-01-26 11:16:21 -08002261 struct ptable *ptable;
2262
2263 ptable = flash_get_ptable();
2264 if (ptable == NULL) {
2265 fastboot_fail("partition table doesn't exist");
2266 return;
2267 }
2268
2269 ptn = ptable_find(ptable, arg);
2270 if (ptn == NULL) {
2271 fastboot_fail("unknown partition name");
2272 return;
2273 }
2274
2275 if (flash_erase(ptn)) {
2276 fastboot_fail("failed to erase partition");
2277 return;
2278 }
2279 fastboot_okay("");
2280}
2281
2282
2283void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2284{
Bikas Gurungd48bd242010-09-04 19:54:32 -07002285 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002286 unsigned long long size = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002287 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002288 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002289
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002290#if VERIFIED_BOOT
2291 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2292 {
2293 if(!device.is_unlocked)
2294 {
2295 fastboot_fail("unlock device to erase keystore");
2296 return;
2297 }
2298 }
2299#endif
2300
Bikas Gurungd48bd242010-09-04 19:54:32 -07002301 index = partition_get_index(arg);
2302 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002303 size = partition_get_size(index);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002304
2305 if(ptn == 0) {
2306 fastboot_fail("Partition table doesn't exist\n");
2307 return;
2308 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002309
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002310 lun = partition_get_lun(index);
2311 mmc_set_lun(lun);
2312
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002313 if (platform_boot_dev_isemmc())
2314 {
2315 if (mmc_erase_card(ptn, size)) {
2316 fastboot_fail("failed to erase partition\n");
2317 return;
2318 }
2319 } else {
2320 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2321 size = partition_get_size(index);
2322 if (size > DEFAULT_ERASE_SIZE)
2323 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002324
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002325 /* Simple inefficient version of erase. Just writing
2326 0 in first several blocks */
2327 if (mmc_write(ptn , size, (unsigned int *)out)) {
2328 fastboot_fail("failed to erase partition");
2329 return;
2330 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002331 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002332#if VERIFIED_BOOT
2333 if(!(strncmp(arg, "userdata", 8)))
2334 if(send_delete_keys_to_tz())
2335 ASSERT(0);
2336#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002337 fastboot_okay("");
2338}
2339
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002340void cmd_erase(const char *arg, void *data, unsigned sz)
2341{
lijuang65c5a822015-08-29 16:35:36 +08002342#if CHECK_BAT_VOLTAGE
2343 if (!target_battery_soc_ok()) {
2344 fastboot_fail("Warning: battery's capacity is very low\n");
2345 return;
2346 }
2347#endif
2348
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002349#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002350 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002351 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002352 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002353 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002354 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002355 return;
2356 }
2357 }
2358#endif
2359
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002360 if(target_is_emmc_boot())
2361 cmd_erase_mmc(arg, data, sz);
2362 else
2363 cmd_erase_nand(arg, data, sz);
2364}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002365
Channagoud Kadabiad259832015-05-29 11:14:17 -07002366static uint32_t aboot_get_secret_key()
2367{
2368 /* 0 is invalid secret key, update this implementation to return
2369 * device specific unique secret key
2370 */
2371 return 0;
2372}
2373
Ajay Dudani5c761132011-04-07 20:19:04 -07002374void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002375{
2376 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002377 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002378 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002379 char *token = NULL;
2380 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002381 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002382 uint8_t lun = 0;
2383 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002384
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002385 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002386 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002387 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002388 if(token)
2389 {
2390 lun = atoi(token);
2391 mmc_set_lun(lun);
2392 lun_set = true;
2393 }
2394
Mao Jinlong226f33a2014-07-04 17:24:10 +08002395 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002396 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002397 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2398 {
2399 if (!aboot_frp_unlock(pname, data, sz))
2400 {
2401 fastboot_info("FRP unlock successful");
2402 fastboot_okay("");
2403 }
2404 else
2405 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2406
2407 return;
2408 }
2409
Mao Jinlong226f33a2014-07-04 17:24:10 +08002410 if (!strcmp(pname, "partition"))
2411 {
2412 dprintf(INFO, "Attempt to write partition image.\n");
2413 if (write_partition(sz, (unsigned char *) data)) {
2414 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002415 return;
2416 }
2417 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002418 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002419 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002420#if VERIFIED_BOOT
2421 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2422 {
2423 if(!device.is_unlocked)
2424 {
2425 fastboot_fail("unlock device to flash keystore");
2426 return;
2427 }
2428 if(!boot_verify_validate_keystore((unsigned char *)data))
2429 {
2430 fastboot_fail("image is not a keystore file");
2431 return;
2432 }
2433 }
2434#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002435 index = partition_get_index(pname);
2436 ptn = partition_get_offset(index);
2437 if(ptn == 0) {
2438 fastboot_fail("partition table doesn't exist");
2439 return;
2440 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002441
Mao Jinlong226f33a2014-07-04 17:24:10 +08002442 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2443 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2444 fastboot_fail("image is not a boot image");
2445 return;
2446 }
2447 }
2448
2449 if(!lun_set)
2450 {
2451 lun = partition_get_lun(index);
2452 mmc_set_lun(lun);
2453 }
2454
2455 size = partition_get_size(index);
2456 if (ROUND_TO_PAGE(sz,511) > size) {
2457 fastboot_fail("size too large");
2458 return;
2459 }
2460 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2461 fastboot_fail("flash write failure");
2462 return;
2463 }
Greg Grisco6e754772011-06-23 12:19:39 -07002464 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002465 }
2466 fastboot_okay("");
2467 return;
2468}
2469
Ajay Dudanide984792015-03-02 09:57:41 -08002470void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2471{
2472 int i, images;
2473 meta_header_t *meta_header;
2474 img_header_entry_t *img_header_entry;
2475
2476 meta_header = (meta_header_t*) data;
2477 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2478
2479 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2480
2481 for (i=0; i<images; i++) {
2482
2483 if((img_header_entry[i].ptn_name == NULL) ||
2484 (img_header_entry[i].start_offset == 0) ||
2485 (img_header_entry[i].size == 0))
2486 break;
2487
2488 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2489 (void *) data + img_header_entry[i].start_offset,
2490 img_header_entry[i].size);
2491 }
2492
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002493 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2494 {
2495 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2496 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2497 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2498 }
2499 else
2500 {
2501 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2502 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2503 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2504 }
2505
2506 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002507 fastboot_okay("");
2508 return;
2509}
2510
Ajay Dudani5c761132011-04-07 20:19:04 -07002511void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2512{
2513 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002514 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002515 uint32_t *fill_buf = NULL;
2516 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002517 sparse_header_t *sparse_header;
2518 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002519 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002520 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302521 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002522 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302523 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002524 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302525 /*End of the sparse image address*/
2526 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002527
Kinson Chikf1a43512011-07-14 11:28:39 -07002528 index = partition_get_index(arg);
2529 ptn = partition_get_offset(index);
2530 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002531 fastboot_fail("partition table doesn't exist");
2532 return;
2533 }
2534
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302535 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302536
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002537 lun = partition_get_lun(index);
2538 mmc_set_lun(lun);
2539
vijay kumar800255e2015-04-24 20:26:19 +05302540 if (sz < sizeof(sparse_header_t)) {
2541 fastboot_fail("size too low");
2542 return;
2543 }
2544
Ajay Dudani5c761132011-04-07 20:19:04 -07002545 /* Read and skip over sparse image header */
2546 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302547
vijay kumar1321f342015-03-27 12:13:42 +05302548 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002549 fastboot_fail("size too large");
2550 return;
2551 }
2552
vijay kumarde4fcf62015-04-23 13:05:49 +05302553 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302554
2555 if (data_end < (uint32_t)data) {
2556 fastboot_fail("buffer overreads occured due to invalid sparse header");
2557 return;
2558 }
2559
vijay kumarde4fcf62015-04-23 13:05:49 +05302560 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002561 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302562 fastboot_fail("sparse header size mismatch");
2563 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002564 }
2565
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002566 dprintf (SPEW, "=== Sparse Image Header ===\n");
2567 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2568 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2569 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2570 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2571 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2572 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2573 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2574 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002575
2576 /* Start processing chunks */
2577 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2578 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302579 /* Make sure the total image size does not exceed the partition size */
2580 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2581 fastboot_fail("size too large");
2582 return;
2583 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002584 /* Read and skip over chunk header */
2585 chunk_header = (chunk_header_t *) data;
2586 data += sizeof(chunk_header_t);
2587
vijay kumar800255e2015-04-24 20:26:19 +05302588 if (data_end < (uint32_t)data) {
2589 fastboot_fail("buffer overreads occured due to invalid sparse header");
2590 return;
2591 }
2592
Ajay Dudani5c761132011-04-07 20:19:04 -07002593 dprintf (SPEW, "=== Chunk Header ===\n");
2594 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2595 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2596 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2597
vijay kumar800255e2015-04-24 20:26:19 +05302598 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002599 {
vijay kumar800255e2015-04-24 20:26:19 +05302600 fastboot_fail("chunk header size mismatch");
2601 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002602 }
2603
wufeng.jiang813dc352015-06-02 23:02:46 -04002604 if (!sparse_header->blk_sz ){
2605 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302606 return;
2607 }
2608
wufeng.jiang813dc352015-06-02 23:02:46 -04002609 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2610
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302611 /* Make sure that the chunk size calculated from sparse image does not
2612 * exceed partition size
2613 */
2614 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2615 {
2616 fastboot_fail("Chunk data size exceeds partition size");
2617 return;
2618 }
2619
Ajay Dudani5c761132011-04-07 20:19:04 -07002620 switch (chunk_header->chunk_type)
2621 {
2622 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002623 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002624 chunk_data_sz))
2625 {
2626 fastboot_fail("Bogus chunk size for chunk type Raw");
2627 return;
2628 }
2629
vijay kumar800255e2015-04-24 20:26:19 +05302630 if (data_end < (uint32_t)data + chunk_data_sz) {
2631 fastboot_fail("buffer overreads occured due to invalid sparse header");
2632 return;
2633 }
2634
wufeng.jiang813dc352015-06-02 23:02:46 -04002635 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2636 otherwise it will return in the line above
2637 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002638 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002639 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002640 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002641 {
2642 fastboot_fail("flash write failure");
2643 return;
2644 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302645 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2646 fastboot_fail("Bogus size for RAW chunk type");
2647 return;
2648 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002649 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002650 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002651 break;
2652
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002653 case CHUNK_TYPE_FILL:
2654 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2655 sizeof(uint32_t)))
2656 {
2657 fastboot_fail("Bogus chunk size for chunk type FILL");
2658 return;
2659 }
2660
2661 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2662 if (!fill_buf)
2663 {
2664 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2665 return;
2666 }
2667
vijay kumar800255e2015-04-24 20:26:19 +05302668 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2669 fastboot_fail("buffer overreads occured due to invalid sparse header");
2670 return;
2671 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002672 fill_val = *(uint32_t *)data;
2673 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002674
2675 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2676 {
2677 fill_buf[i] = fill_val;
2678 }
2679
wufeng.jiang813dc352015-06-02 23:02:46 -04002680 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002681 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302682 /* Make sure that the data written to partition does not exceed partition size */
2683 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2684 {
2685 fastboot_fail("Chunk data size for fill type exceeds partition size");
2686 return;
2687 }
2688
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002689 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2690 sparse_header->blk_sz,
2691 fill_buf))
2692 {
2693 fastboot_fail("flash write failure");
2694 free(fill_buf);
2695 return;
2696 }
2697
2698 total_blocks++;
2699 }
2700
2701 free(fill_buf);
2702 break;
2703
Ajay Dudani5c761132011-04-07 20:19:04 -07002704 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302705 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2706 fastboot_fail("bogus size for chunk DONT CARE type");
2707 return;
2708 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002709 total_blocks += chunk_header->chunk_sz;
2710 break;
2711
Ajay Dudani5c761132011-04-07 20:19:04 -07002712 case CHUNK_TYPE_CRC:
2713 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2714 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002715 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002716 return;
2717 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302718 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2719 fastboot_fail("bogus size for chunk CRC type");
2720 return;
2721 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002722 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302723 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2724 fastboot_fail("integer overflow occured");
2725 return;
2726 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002727 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302728 if (data_end < (uint32_t)data) {
2729 fastboot_fail("buffer overreads occured due to invalid sparse header");
2730 return;
2731 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002732 break;
2733
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002734 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002735 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002736 fastboot_fail("Unknown chunk type");
2737 return;
2738 }
2739 }
2740
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002741 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2742 total_blocks, sparse_header->total_blks);
2743
2744 if(total_blocks != sparse_header->total_blks)
2745 {
2746 fastboot_fail("sparse image write failure");
2747 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002748
2749 fastboot_okay("");
2750 return;
2751}
2752
2753void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2754{
2755 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002756 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002757
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002758#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002759 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2760 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002761 int ret=0;
2762 uint32 major_version=0;
2763 uint32 minor_version=0;
2764
2765 ret = scm_svc_version(&major_version,&minor_version);
2766 if(!ret)
2767 {
2768 if(major_version >= 2)
2769 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002770 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002771 {
2772 ret = encrypt_scm((uint32 **) &data, &sz);
2773 if (ret != 0) {
2774 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2775 return;
2776 }
2777
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002778 /* Protect only for SSD */
2779 if (!strcmp(arg, "ssd")) {
2780 ret = scm_protect_keystore((uint32 *) data, sz);
2781 if (ret != 0) {
2782 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2783 return;
2784 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002785 }
2786 }
2787 else
2788 {
2789 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2790 if(ret != 0)
2791 {
2792 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2793 return;
2794 }
2795 }
2796 }
2797 else
2798 {
2799 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2800 magic_number[1] == DECRYPT_MAGIC_1)
2801 {
2802 ret = decrypt_scm((uint32 **) &data, &sz);
2803 if (ret != 0) {
2804 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2805 return;
2806 }
2807 }
2808 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2809 magic_number[1] == ENCRYPT_MAGIC_1)
2810 {
2811 ret = encrypt_scm((uint32 **) &data, &sz);
2812 if (ret != 0) {
2813 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2814 return;
2815 }
2816 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002817 }
2818 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002819 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002820 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002821 dprintf(CRITICAL,"INVALID SVC Version\n");
2822 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002823 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002824#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002825
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002826#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002827 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002828 {
lijuang511a2b52015-08-14 20:50:51 +08002829 /* if device is locked:
2830 * common partition will not allow to be flashed
2831 * critical partition will allow to flash image.
2832 */
2833 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
2834 fastboot_fail("Partition flashing is not allowed");
2835 return;
2836 }
2837
2838 /* if device critical is locked:
2839 * common partition will allow to be flashed
2840 * critical partition will not allow to flash image.
2841 */
2842 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
2843 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002844 return;
2845 }
2846 }
2847#endif
2848
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002849 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002850 meta_header = (meta_header_t *) data;
2851 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002852 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002853 else if (meta_header->magic == META_HEADER_MAGIC)
2854 cmd_flash_meta_img(arg, data, sz);
2855 else
2856 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002857
2858#if VERIFIED_BOOT
2859 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
2860 {
2861 // reset dm_verity mode to enforcing
2862 device.verity_mode = 1;
2863 write_device_info(&device);
2864 }
2865#endif
2866
Ajay Dudani5c761132011-04-07 20:19:04 -07002867 return;
2868}
2869
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002870void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2871{
2872 struct ptentry *sys_ptn;
2873 struct ptable *ptable;
2874
2875 ptable = flash_get_ptable();
2876 if (ptable == NULL) {
2877 fastboot_fail("partition table doesn't exist");
2878 return;
2879 }
2880
2881 sys_ptn = ptable_find(ptable, "system");
2882 if (sys_ptn == NULL) {
2883 fastboot_fail("system partition not found");
2884 return;
2885 }
2886
2887 sz = ROUND_TO_PAGE(sz, page_mask);
2888 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2889 fastboot_fail("update_ubi_vol failed");
2890 else
2891 fastboot_okay("");
2892}
2893
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002894void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002895{
2896 struct ptentry *ptn;
2897 struct ptable *ptable;
2898 unsigned extra = 0;
2899
2900 ptable = flash_get_ptable();
2901 if (ptable == NULL) {
2902 fastboot_fail("partition table doesn't exist");
2903 return;
2904 }
2905
2906 ptn = ptable_find(ptable, arg);
2907 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002908 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2909 arg);
2910 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002911 return;
2912 }
2913
2914 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2915 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2916 fastboot_fail("image is not a boot image");
2917 return;
2918 }
2919 }
2920
Amol Jadi5c61a952012-05-04 17:05:35 -07002921 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002922 || !strcmp(ptn->name, "userdata")
2923 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002924 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002925 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002926 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002927 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002928 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002929
2930 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002931 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2932 if (flash_ubi_img(ptn, data, sz)) {
2933 fastboot_fail("flash write failure");
2934 return;
2935 }
2936 } else {
2937 if (flash_write(ptn, extra, data, sz)) {
2938 fastboot_fail("flash write failure");
2939 return;
2940 }
Dima Zavin214cc642009-01-26 11:16:21 -08002941 }
2942 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2943 fastboot_okay("");
2944}
2945
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002946void cmd_flash(const char *arg, void *data, unsigned sz)
2947{
lijuang65c5a822015-08-29 16:35:36 +08002948#if CHECK_BAT_VOLTAGE
2949 if (!target_battery_soc_ok()) {
2950 fastboot_fail("Warning: battery's capacity is very low\n");
2951 return;
2952 }
2953#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002954 if(target_is_emmc_boot())
2955 cmd_flash_mmc(arg, data, sz);
2956 else
2957 cmd_flash_nand(arg, data, sz);
2958}
2959
Dima Zavin214cc642009-01-26 11:16:21 -08002960void cmd_continue(const char *arg, void *data, unsigned sz)
2961{
2962 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002963 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002964
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002965 if (target_is_emmc_boot())
2966 {
lijuanga40d6302015-07-20 20:10:13 +08002967#if FBCON_DISPLAY_MSG
2968 keys_detect_init();
2969#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002970 boot_linux_from_mmc();
2971 }
2972 else
2973 {
2974 boot_linux_from_flash();
2975 }
Dima Zavin214cc642009-01-26 11:16:21 -08002976}
2977
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002978void cmd_reboot(const char *arg, void *data, unsigned sz)
2979{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002980 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002981 fastboot_okay("");
2982 reboot_device(0);
2983}
2984
2985void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2986{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002987 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002988 fastboot_okay("");
2989 reboot_device(FASTBOOT_MODE);
2990}
2991
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002992void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2993{
2994 dprintf(INFO, "Enabling charger screen check\n");
2995 device.charger_screen_enabled = 1;
2996 write_device_info(&device);
2997 fastboot_okay("");
2998}
2999
3000void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3001{
3002 dprintf(INFO, "Disabling charger screen check\n");
3003 device.charger_screen_enabled = 0;
3004 write_device_info(&device);
3005 fastboot_okay("");
3006}
3007
lijuanga25d8bb2015-09-16 13:11:52 +08003008void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3009{
3010 char *p = NULL;
3011 const char *delim = " \t\n\r";
3012
3013 if (arg) {
3014 p = strtok((char *)arg, delim);
3015 if (p) {
3016 if (!strncmp(p, "0", 1)) {
3017 device.charger_screen_enabled = 0;
3018 } else if (!strncmp(p, "1", 1)) {
3019 device.charger_screen_enabled = 1;
3020 }
3021 }
3022 }
3023
3024 /* update charger_screen_enabled value for getvar
3025 * command
3026 */
3027 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3028 device.charger_screen_enabled);
3029
3030 write_device_info(&device);
3031 fastboot_okay("");
3032}
3033
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303034void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3035{
3036 dprintf(INFO, "Selecting display panel %s\n", arg);
3037 if (arg)
3038 strlcpy(device.display_panel, arg,
3039 sizeof(device.display_panel));
3040 write_device_info(&device);
3041 fastboot_okay("");
3042}
3043
Shashank Mittal162244e2011-08-08 19:01:25 -07003044void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3045{
lijuang4ece1e72015-08-14 21:02:36 +08003046 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303047}
3048
3049void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3050{
lijuang4ece1e72015-08-14 21:02:36 +08003051 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303052 if(!is_allow_unlock) {
3053 fastboot_fail("oem unlock is not allowed");
3054 return;
3055 }
3056
lijuang4ece1e72015-08-14 21:02:36 +08003057 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303058
lijuang4ece1e72015-08-14 21:02:36 +08003059 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303060 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003061
vijay kumarc65876c2015-04-24 13:29:16 +05303062 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3063 write_misc(0, &msg, sizeof(msg));
3064
3065 fastboot_okay("");
3066 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003067 }
3068 fastboot_okay("");
3069}
3070
Channagoud Kadabiad259832015-05-29 11:14:17 -07003071static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3072{
3073 int ret = 1;
3074 uint32_t secret_key;
3075 char seckey_buffer[MAX_RSP_SIZE];
3076
3077 secret_key = aboot_get_secret_key();
3078 if (secret_key)
3079 {
3080 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3081 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3082 {
3083 is_allow_unlock = true;
3084 write_allow_oem_unlock(is_allow_unlock);
3085 ret = 0;
3086 }
3087 }
3088 return ret;
3089}
3090
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003091void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3092{
lijuang4ece1e72015-08-14 21:02:36 +08003093 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003094}
3095
Shashank Mittala0032282011-08-26 14:50:11 -07003096void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3097{
lijuang511a2b52015-08-14 20:50:51 +08003098 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003099 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003100 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003101 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3102 fastboot_info(response);
lijuang511a2b52015-08-14 20:50:51 +08003103 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3104 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003105 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003106 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303107 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3108 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003109 fastboot_okay("");
3110}
3111
lijuang511a2b52015-08-14 20:50:51 +08003112void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3113{
3114 char response[MAX_RSP_SIZE];
3115 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3116 fastboot_info(response);
3117 fastboot_okay("");
3118}
3119
3120void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3121{
lijuang4ece1e72015-08-14 21:02:36 +08003122 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003123}
3124
3125void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3126{
lijuang4ece1e72015-08-14 21:02:36 +08003127 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003128}
3129
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003130void cmd_preflash(const char *arg, void *data, unsigned sz)
3131{
3132 fastboot_okay("");
3133}
3134
Mao Flynn7b379f32015-04-20 00:28:30 +08003135static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303136
Mao Flynn7b379f32015-04-20 00:28:30 +08003137int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303138{
Mao Flynn7b379f32015-04-20 00:28:30 +08003139 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303140 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003141 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303142 return -1;
3143 return 0;
3144}
3145
Mao Flynn7b379f32015-04-20 00:28:30 +08003146int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003147{
3148 struct ptentry *ptn;
3149 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003150 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003151 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003152
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303153 ptable = flash_get_ptable();
3154 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003155 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3156 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303157 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003158
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303159 ptn = ptable_find(ptable, "splash");
3160 if (ptn == NULL) {
3161 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003162 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303163 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003164 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303165 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003166 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303167 }
3168
Mao Flynn7b379f32015-04-20 00:28:30 +08003169 header = (struct logo_img_header *)logo_header;
3170 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303171 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003172 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303173 }
3174
3175 fb_display = fbcon_display();
3176 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003177 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3178 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3179
3180 /* if the logo is full-screen size, remove "fbcon_clear()" */
3181 if ((header->width != fb_display->width)
3182 || (header->height != fb_display->height))
3183 fbcon_clear();
3184
3185 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3186 (uint32_t *)base,
3187 (header->blocks * 512))) {
3188 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3189 return -1;
3190 }
3191 fbcon_extract_to_screen(header, base);
3192 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003193 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003194
3195 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3196 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3197 return -1;
3198 }
3199
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303200 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003201 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3202 (uint32_t *)base,
3203 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303204 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303205 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003206 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003207 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303208 }
3209
Mao Flynn7b379f32015-04-20 00:28:30 +08003210 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303211}
3212
Mao Flynn7b379f32015-04-20 00:28:30 +08003213int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303214{
3215 int index = INVALID_PTN;
3216 unsigned long long ptn = 0;
3217 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003218 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003219 uint32_t blocksize, realsize, readsize;
3220 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303221
3222 index = partition_get_index("splash");
3223 if (index == 0) {
3224 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003225 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303226 }
3227
3228 ptn = partition_get_offset(index);
3229 if (ptn == 0) {
3230 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003231 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303232 }
3233
Mao Flynn1893a7f2015-06-03 12:03:36 +08003234 mmc_set_lun(partition_get_lun(index));
3235
3236 blocksize = mmc_get_device_blocksize();
3237 if (blocksize == 0) {
3238 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3239 return -1;
3240 }
3241
3242 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003243 if (!fb_display)
3244 {
3245 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3246 return -1;
3247 }
3248
Mao Flynn1893a7f2015-06-03 12:03:36 +08003249 base = (uint8_t *) fb_display->base;
3250
3251 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303252 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003253 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303254 }
3255
Mao Flynn1893a7f2015-06-03 12:03:36 +08003256 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003257 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303258 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003259 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303260 }
3261
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303262 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003263 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3264 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003265
Mao Flynn1893a7f2015-06-03 12:03:36 +08003266 realsize = header->blocks * 512;
3267 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3268
3269 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003270 if ((header->width != fb_display->width)
3271 || (header->height != fb_display->height))
3272 fbcon_clear();
3273
Mao Flynn1893a7f2015-06-03 12:03:36 +08003274 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003275 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3276 return -1;
3277 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003278
Mao Flynn1893a7f2015-06-03 12:03:36 +08003279 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3280 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303281
Mao Flynn1893a7f2015-06-03 12:03:36 +08003282 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3283 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3284 return -1;
3285 }
3286
3287 realsize = header->width * header->height * fb_display->bpp / 8;
3288 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3289
3290 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3291 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3292 fbcon_clear();
3293 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3294 return -1;
3295 }
3296 } else {
3297 if (mmc_read(ptn + blocksize ,
3298 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3299 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3300 return -1;
3301 }
3302 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3303 }
3304 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303305 }
3306
Mao Flynn7b379f32015-04-20 00:28:30 +08003307 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303308}
3309
Mao Flynn7b379f32015-04-20 00:28:30 +08003310int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303311{
3312 if (target_is_emmc_boot()) {
3313 return splash_screen_mmc();
3314 } else {
3315 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003316 }
3317}
3318
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003319/* Get the size from partiton name */
3320static void get_partition_size(const char *arg, char *response)
3321{
3322 uint64_t ptn = 0;
3323 uint64_t size;
3324 int index = INVALID_PTN;
3325
3326 index = partition_get_index(arg);
3327
3328 if (index == INVALID_PTN)
3329 {
3330 dprintf(CRITICAL, "Invalid partition index\n");
3331 return;
3332 }
3333
3334 ptn = partition_get_offset(index);
3335
3336 if(!ptn)
3337 {
3338 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3339 return;
3340 }
3341
3342 size = partition_get_size(index);
3343
3344 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3345 return;
3346}
3347
3348/*
3349 * Publish the partition type & size info
3350 * fastboot getvar will publish the required information.
3351 * fastboot getvar partition_size:<partition_name>: partition size in hex
3352 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3353 */
3354static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3355{
3356 uint8_t i;
3357
3358 for (i = 0; i < num_parts; i++) {
3359 get_partition_size(info[i].part_name, info[i].size_response);
3360
3361 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3362 {
3363 dprintf(CRITICAL, "partition size name truncated\n");
3364 return;
3365 }
3366 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3367 {
3368 dprintf(CRITICAL, "partition type name truncated\n");
3369 return;
3370 }
3371
3372 /* publish partition size & type info */
3373 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3374 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3375 }
3376}
3377
lijuang4ece1e72015-08-14 21:02:36 +08003378void get_product_name(unsigned char *buf)
3379{
3380 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3381 return;
3382}
3383
3384void get_bootloader_version(unsigned char *buf)
3385{
3386 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3387 return;
3388}
3389
3390void get_baseband_version(unsigned char *buf)
3391{
3392 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3393 return;
3394}
3395
3396bool is_device_locked()
3397{
3398 return device.is_unlocked ? false:true;
3399}
3400
Amol Jadi5edf3552013-07-23 14:15:34 -07003401/* register commands and variables for fastboot */
3402void aboot_fastboot_register_commands(void)
3403{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003404 int i;
lijuangf16461c2015-08-03 17:09:34 +08003405 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003406
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003407 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003408 /* By default the enabled list is empty. */
3409 {"", NULL},
3410 /* move commands enclosed within the below ifndef to here
3411 * if they need to be enabled in user build.
3412 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003413#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003414 /* Register the following commands only for non-user builds */
3415 {"flash:", cmd_flash},
3416 {"erase:", cmd_erase},
3417 {"boot", cmd_boot},
3418 {"continue", cmd_continue},
3419 {"reboot", cmd_reboot},
3420 {"reboot-bootloader", cmd_reboot_bootloader},
3421 {"oem unlock", cmd_oem_unlock},
3422 {"oem unlock-go", cmd_oem_unlock_go},
3423 {"oem lock", cmd_oem_lock},
3424 {"flashing unlock", cmd_oem_unlock},
3425 {"flashing lock", cmd_oem_lock},
3426 {"flashing lock_critical", cmd_flashing_lock_critical},
3427 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3428 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3429 {"oem device-info", cmd_oem_devinfo},
3430 {"preflash", cmd_preflash},
3431 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3432 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003433 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003434 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003435#endif
lijuang511a2b52015-08-14 20:50:51 +08003436 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003437
3438 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3439 for (i = 1; i < fastboot_cmds_count; i++)
3440 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3441
Amol Jadi5edf3552013-07-23 14:15:34 -07003442 /* publish variables and their values */
3443 fastboot_publish("product", TARGET(BOARD));
3444 fastboot_publish("kernel", "lk");
3445 fastboot_publish("serialno", sn_buf);
3446
3447 /*
3448 * partition info is supported only for emmc partitions
3449 * Calling this for NAND prints some error messages which
3450 * is harmless but misleading. Avoid calling this for NAND
3451 * devices.
3452 */
3453 if (target_is_emmc_boot())
3454 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3455
3456 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003457 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3458 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003459 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003460 /* Is the charger screen check enabled */
3461 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3462 device.charger_screen_enabled);
3463 fastboot_publish("charger-screen-enabled",
3464 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003465 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303466 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3467 device.display_panel);
3468 fastboot_publish("display-panel",
3469 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003470 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3471 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003472 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3473 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3474 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3475 target_is_emmc_boot()? "eMMC":"UFS");
3476 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003477#if CHECK_BAT_VOLTAGE
3478 snprintf(battery_voltage, MAX_RSP_SIZE, "%d",
3479 target_get_battery_voltage());
3480 fastboot_publish("battery-voltage", (const char *) battery_voltage);
3481 fastboot_publish("battery-soc-ok", target_battery_soc_ok()? "yes":"no");
3482#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003483}
3484
Brian Swetland9c4c0752009-01-25 16:23:50 -08003485void aboot_init(const struct app_descriptor *app)
3486{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003487 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03003488 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003489
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003490 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003491 if (target_is_emmc_boot())
3492 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003493 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003494 page_mask = page_size - 1;
3495 }
3496 else
3497 {
3498 page_size = flash_page_size();
3499 page_mask = page_size - 1;
3500 }
3501
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003502 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3503
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003504 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303505 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003506
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003507 /* Display splash screen if enabled */
3508#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003509#if NO_ALARM_DISPLAY
3510 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003511#endif
lijuang99c02d82015-02-13 19:04:34 +08003512 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003513#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003514 /* Wait if the display shutdown is in progress */
3515 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003516 if (!pm_appsbl_display_init_done())
3517 target_display_init(device.display_panel);
3518 else
3519 display_image_on_screen();
3520#else
lijuang99c02d82015-02-13 19:04:34 +08003521 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003522#endif
lijuang99c02d82015-02-13 19:04:34 +08003523 dprintf(SPEW, "Display Init: Done\n");
3524#if NO_ALARM_DISPLAY
3525 }
3526#endif
3527#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003528
Greg Griscod6250552011-06-29 14:40:23 -07003529 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003530 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003531
Dhaval Patel223ec952013-07-18 14:49:44 -07003532 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3533
Matthew Qindefd5562014-07-11 18:02:40 +08003534 /*
3535 * Check power off reason if user force reset,
3536 * if yes phone will do normal boot.
3537 */
3538 if (is_user_force_reset())
3539 goto normal_boot;
3540
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003541 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003542 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003543 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003544 dprintf(ALWAYS,"dload mode key sequence detected\n");
3545 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003546 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003547 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003548 }
3549 else
3550 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303551 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003552 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003553 }
3554 boot_into_fastboot = true;
3555 }
3556 if (!boot_into_fastboot)
3557 {
3558 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3559 boot_into_recovery = 1;
3560 if (!boot_into_recovery &&
3561 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003562 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003563 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003564 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003565 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003566 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003567 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003568
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003569#if USE_PON_REBOOT_REG
3570 reboot_mode = check_hard_reboot_mode();
3571#else
Ajay Dudani77421292010-10-27 19:34:06 -07003572 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003573#endif
3574 if (reboot_mode == RECOVERY_MODE)
3575 {
Ajay Dudani77421292010-10-27 19:34:06 -07003576 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003577 }
3578 else if(reboot_mode == FASTBOOT_MODE)
3579 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003580 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003581 }
3582 else if(reboot_mode == ALARM_BOOT)
3583 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003584 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003585 }
3586 else if (reboot_mode == DM_VERITY_ENFORCING)
3587 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003588 device.verity_mode = 1;
3589 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003590 }
3591 else if (reboot_mode == DM_VERITY_LOGGING)
3592 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003593 device.verity_mode = 0;
3594 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003595 }
3596 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3597 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003598 if(send_delete_keys_to_tz())
3599 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003600 }
3601
Matthew Qindefd5562014-07-11 18:02:40 +08003602normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003603 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003604 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003605 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003606 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003607 if(emmc_recovery_init())
3608 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3609 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003610 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003611 if((device.is_unlocked) || (device.is_tampered))
3612 {
3613 #ifdef TZ_TAMPER_FUSE
3614 set_tamper_fuse_cmd();
3615 #endif
3616 #if USE_PCOM_SECBOOT
3617 set_tamper_flag(device.is_tampered);
3618 #endif
3619 }
Shashank Mittala0032282011-08-26 14:50:11 -07003620 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003621
Pavel Nedev5d91d412013-04-29 11:34:24 +03003622 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003623 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003624 else
3625 {
3626 recovery_init();
3627 #if USE_PCOM_SECBOOT
3628 if((device.is_unlocked) || (device.is_tampered))
3629 set_tamper_flag(device.is_tampered);
3630 #endif
3631 boot_linux_from_flash();
3632 }
3633 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3634 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003635 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003636
Amol Jadi5edf3552013-07-23 14:15:34 -07003637 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003638
Amol Jadi5edf3552013-07-23 14:15:34 -07003639 /* register aboot specific fastboot commands */
3640 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003641
Amol Jadi5edf3552013-07-23 14:15:34 -07003642 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003643 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003644
3645 /* initialize and start fastboot */
3646 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003647#if FBCON_DISPLAY_MSG
3648 display_fastboot_menu_thread();
3649#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003650}
3651
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003652uint32_t get_page_size()
3653{
3654 return page_size;
3655}
3656
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003657/*
3658 * Calculated and save hash (SHA256) for non-signed boot image.
3659 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003660 * @param image_addr - Boot image address
3661 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003662 *
3663 * @return int - 0 on success, negative value on failure.
3664 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003665static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003666{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003667 unsigned int digest[8];
3668#if IMAGE_VERIF_ALGO_SHA1
3669 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3670#else
3671 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3672#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003673
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003674 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003675 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003676
3677 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003678 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003679
3680 return 0;
3681}
3682
Brian Swetland9c4c0752009-01-25 16:23:50 -08003683APP_START(aboot)
3684 .init = aboot_init,
3685APP_END