blob: 9acde54c954bf16903da6def12d2370ed7d1c670 [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
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800727 /* Turn off splash screen if enabled */
728#if DISPLAY_SPLASH_SCREEN
729 target_display_shutdown();
730#endif
731
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700732 /* Perform target specific cleanup */
733 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800734
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800735 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530736 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800737
738 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700739
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +0530740 /* Initialise wdog to catch early kernel crashes */
741#if WDOG_SUPPORT
742 msm_wdog_init();
743#endif
Amol Jadi4421e652011-06-16 15:00:48 -0700744 /* do any platform specific cleanup before kernel entry */
745 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700746
Brian Swetland9c4c0752009-01-25 16:23:50 -0800747 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700748
Amol Jadi504f9fe2012-08-16 13:56:48 -0700749#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800750 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700751#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700752 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800753
754 if (IS_ARM64(kptr))
755 /* Jump to a 64bit kernel */
756 scm_elexec_call((paddr_t)kernel, tags_phys);
757 else
758 /* Jump to a 32bit kernel */
759 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800760}
761
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700762/* Function to check if the memory address range falls within the aboot
763 * boundaries.
764 * start: Start of the memory region
765 * size: Size of the memory region
766 */
767int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
768{
769 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800770 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700771 return -1;
772
773 /* Check for memory overlap. */
774 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
775 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700776 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700777 return 0;
778 else
779 return -1;
780}
781
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800782#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800783
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800784BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800785#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800786BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800787#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800788
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700789static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
790{
791 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800792
793#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800794#if IMAGE_VERIF_ALGO_SHA1
795 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
796#else
797 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
798#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800799#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700800
801 /* Assume device is rooted at this time. */
802 device.is_tampered = 1;
803
804 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
805
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700806#if VERIFIED_BOOT
807 if(boot_into_recovery)
808 {
809 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530810 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700811 }
812 else
813 {
814 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530815 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700816 }
817 boot_verify_print_state();
818#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700819 ret = image_verify((unsigned char *)bootimg_addr,
820 (unsigned char *)(bootimg_addr + bootimg_size),
821 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800822 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700823#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700824 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
825
826 if (ret)
827 {
828 /* Authorized kernel */
829 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700830 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700831 }
832
Amit Blay4aa292f2015-04-28 21:55:59 +0300833#ifdef MDTP_SUPPORT
834 {
835 /* Verify MDTP lock.
836 * For boot & recovery partitions, use aboot's verification result.
837 */
838 mdtp_ext_partition_verification_t ext_partition;
839 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
840 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
841 ext_partition.page_size = 0; /* Not needed since already validated */
842 ext_partition.image_addr = 0; /* Not needed since already validated */
843 ext_partition.image_size = 0; /* Not needed since already validated */
844 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
845 mdtp_fwlock_verify_lock(&ext_partition);
846 }
847#endif /* MDTP_SUPPORT */
848
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700849#if USE_PCOM_SECBOOT
850 set_tamper_flag(device.is_tampered);
851#endif
852
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700853#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700854 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700855 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700856 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800857#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +0800858 display_bootverify_menu_thread(DISPLAY_MENU_RED);
lijuanga40d6302015-07-20 20:10:13 +0800859 wait_for_users_action();
860#else
861 dprintf(CRITICAL,
862 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
863 mdelay(5000);
864#endif
865
866 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700867 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800868#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +0800869 display_bootverify_menu_thread(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800870 wait_for_users_action();
871#else
872 dprintf(CRITICAL,
873 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
874 mdelay(5000);
875#endif
876 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700877 default:
lijuanga40d6302015-07-20 20:10:13 +0800878 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700879 }
880#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700881#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530882 if(device.is_tampered)
883 {
884 write_device_info_mmc(&device);
885 #ifdef TZ_TAMPER_FUSE
886 set_tamper_fuse_cmd();
887 #endif
888 #ifdef ASSERT_ON_TAMPER
889 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
890 ASSERT(0);
891 #endif
892 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700893#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700894}
895
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530896static bool check_format_bit()
897{
898 bool ret = false;
899 int index;
900 uint64_t offset;
901 struct boot_selection_info *in = NULL;
902 char *buf = NULL;
903
904 index = partition_get_index("bootselect");
905 if (index == INVALID_PTN)
906 {
907 dprintf(INFO, "Unable to locate /bootselect partition\n");
908 return ret;
909 }
910 offset = partition_get_offset(index);
911 if(!offset)
912 {
913 dprintf(INFO, "partition /bootselect doesn't exist\n");
914 return ret;
915 }
916 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
917 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530918 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530919 {
920 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
921 free(buf);
922 return ret;
923 }
924 in = (struct boot_selection_info *) buf;
925 if ((in->signature == BOOTSELECT_SIGNATURE) &&
926 (in->version == BOOTSELECT_VERSION)) {
927 if ((in->state_info & BOOTSELECT_FORMAT) &&
928 !(in->state_info & BOOTSELECT_FACTORY))
929 ret = true;
930 } else {
931 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
932 in->signature, in->version);
933 ASSERT(0);
934 }
935 free(buf);
936 return ret;
937}
938
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700939void boot_verifier_init()
940{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700941 uint32_t boot_state;
942 /* Check if device unlock */
943 if(device.is_unlocked)
944 {
945 boot_verify_send_event(DEV_UNLOCK);
946 boot_verify_print_state();
947 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
948 return;
949 }
950 else
951 {
952 boot_verify_send_event(BOOT_INIT);
953 }
954
955 /* Initialize keystore */
956 boot_state = boot_verify_keystore_init();
957 if(boot_state == YELLOW)
958 {
959 boot_verify_print_state();
960 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
961 }
962}
963
Shashank Mittal23b8f422010-04-16 19:27:21 -0700964int boot_linux_from_mmc(void)
965{
966 struct boot_img_hdr *hdr = (void*) buf;
967 struct boot_img_hdr *uhdr;
968 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700969 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700970 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700971 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700972
Shashank Mittalcd98d472011-08-02 14:29:24 -0700973 unsigned char *image_addr = 0;
974 unsigned kernel_actual;
975 unsigned ramdisk_actual;
976 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700977 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700978
Matthew Qin271927e2015-03-31 22:07:07 -0400979 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +0800980 unsigned int out_len = 0;
981 unsigned int out_avai_len = 0;
982 unsigned char *out_addr = NULL;
983 uint32_t dtb_offset = 0;
984 unsigned char *kernel_start_addr = NULL;
985 unsigned int kernel_size = 0;
986 int rc;
987
Neeti Desai465491e2012-07-31 12:53:35 -0700988#if DEVICE_TREE
989 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700990 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700991 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800992 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700993 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -0400994 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -0700995#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +0800996 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800997
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530998 if (check_format_bit())
999 boot_into_recovery = 1;
1000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001001 if (!boot_into_recovery) {
1002 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1003 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1004 if (rcode <= 0) {
1005 boot_into_ffbm = false;
1006 if (rcode < 0)
1007 dprintf(CRITICAL,"failed to get ffbm cookie");
1008 } else
1009 boot_into_ffbm = true;
1010 } else
1011 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001012 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1013 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1014 dprintf(INFO, "Unified boot method!\n");
1015 hdr = uhdr;
1016 goto unified_boot;
1017 }
Greg Griscod6250552011-06-29 14:40:23 -07001018 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001019 index = partition_get_index("boot");
1020 ptn = partition_get_offset(index);
1021 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001022 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1023 return -1;
1024 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001025 }
1026 else {
1027 index = partition_get_index("recovery");
1028 ptn = partition_get_offset(index);
1029 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001030 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1031 return -1;
1032 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001033 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001034 /* Set Lun for boot & recovery partitions */
1035 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001036
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301037 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001038 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1039 return -1;
1040 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001041
1042 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001043 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001044 return -1;
1045 }
1046
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001047 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301048
1049 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1050 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1051 return -1;
1052 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001053 page_size = hdr->page_size;
1054 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001055 }
1056
Matthew Qin49e51fa2015-02-09 10:40:45 +08001057 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1058 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001059
Matthew Qin49e51fa2015-02-09 10:40:45 +08001060 image_addr = (unsigned char *)target_get_scratch_address();
1061
1062#if DEVICE_TREE
1063 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1064 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1065#else
1066 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1067#endif
1068
1069#if VERIFIED_BOOT
1070 boot_verifier_init();
1071#endif
1072
1073 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
1074 {
1075 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1076 return -1;
1077 }
1078
Matthew Qinbb7923d2015-02-09 10:56:09 +08001079 /*
1080 * Update loading flow of bootimage to support compressed/uncompressed
1081 * bootimage on both 64bit and 32bit platform.
1082 * 1. Load bootimage from emmc partition onto DDR.
1083 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1084 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1085 * platform accordingly.
1086 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1087 */
1088
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001089 dprintf(INFO, "Loading (%s) image (%d): start\n",
1090 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001091 bs_set_timestamp(BS_KERNEL_LOAD_START);
1092
Matthew Qinbb7923d2015-02-09 10:56:09 +08001093 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001094 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1095 {
1096 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1097 return -1;
1098 }
1099
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001100 dprintf(INFO, "Loading (%s) image (%d): done\n",
1101 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1102
Matthew Qin49e51fa2015-02-09 10:40:45 +08001103 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1104
1105 /* Authenticate Kernel */
1106 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1107 (int) target_use_signed_kernel(),
1108 device.is_unlocked,
1109 device.is_tampered);
1110
1111 if(target_use_signed_kernel() && (!device.is_unlocked))
1112 {
1113 offset = imagesize_actual;
1114 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1115 {
1116 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1117 return -1;
1118 }
1119
1120 /* Read signature */
1121 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1122 {
1123 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1124 return -1;
1125 }
1126
1127 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
1128 } else {
1129 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1130 #ifdef TZ_SAVE_KERNEL_HASH
1131 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1132 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001133
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001134#if VERIFIED_BOOT
1135 if(boot_verify_get_state() == ORANGE)
1136 {
lijuanga40d6302015-07-20 20:10:13 +08001137#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +08001138 display_bootverify_menu_thread(DISPLAY_MENU_ORANGE);
lijuanga40d6302015-07-20 20:10:13 +08001139 wait_for_users_action();
1140#else
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001141 dprintf(CRITICAL,
lijuanga40d6302015-07-20 20:10:13 +08001142 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001143 mdelay(5000);
lijuanga40d6302015-07-20 20:10:13 +08001144#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001145 }
1146#endif
1147
Amit Blay4aa292f2015-04-28 21:55:59 +03001148#ifdef MDTP_SUPPORT
1149 {
1150 /* Verify MDTP lock.
1151 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1152 * since verification was skipped in aboot. The signature is not part of the loaded image.
1153 */
1154 mdtp_ext_partition_verification_t ext_partition;
1155 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1156 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1157 ext_partition.page_size = page_size;
1158 ext_partition.image_addr = (uint32)image_addr;
1159 ext_partition.image_size = imagesize_actual;
1160 ext_partition.sig_avail = FALSE;
1161 mdtp_fwlock_verify_lock(&ext_partition);
1162 }
1163#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001164 }
1165
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001166#if VERIFIED_BOOT
1167 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001168 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001169 ASSERT(0);
1170#endif
1171
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001172 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001173 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1174 * If not, continue booting.
1175 */
1176 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1177 {
1178 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1179 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001180 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001181 rc = decompress((unsigned char *)(image_addr + page_size),
1182 hdr->kernel_size, out_addr, out_avai_len,
1183 &dtb_offset, &out_len);
1184 if (rc)
1185 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001186 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001187 ASSERT(0);
1188 }
1189
Matthew Qin0b15b322015-05-19 05:20:54 -04001190 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001191 kptr = (struct kernel64_hdr *)out_addr;
1192 kernel_start_addr = out_addr;
1193 kernel_size = out_len;
1194 } else {
1195 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1196 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1197 kernel_size = hdr->kernel_size;
1198 }
1199
1200 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001201 * Update the kernel/ramdisk/tags address if the boot image header
1202 * has default values, these default values come from mkbootimg when
1203 * the boot image is flashed using fastboot flash:raw
1204 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001205 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001206
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001207 /* Get virtual addresses since the hdr saves physical addresses. */
1208 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1209 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1210 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001211
Matthew Qinbb7923d2015-02-09 10:56:09 +08001212 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001213 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001214 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001215 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1216 {
1217 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1218 return -1;
1219 }
1220
1221#ifndef DEVICE_TREE
1222 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1223 {
1224 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1225 return -1;
1226 }
1227#endif
1228
Matthew Qin49e51fa2015-02-09 10:40:45 +08001229 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001230 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001231 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001232
Matthew Qin49e51fa2015-02-09 10:40:45 +08001233 #if DEVICE_TREE
1234 if(hdr->dt_size) {
1235 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1236 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001237
Matthew Qin49e51fa2015-02-09 10:40:45 +08001238 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1239 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1240 return -1;
1241 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001242
Matthew Qin49e51fa2015-02-09 10:40:45 +08001243 /* Find index of device tree within device tree table */
1244 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1245 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1246 return -1;
1247 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001248
Matthew Qin271927e2015-03-31 22:07:07 -04001249 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1250 {
1251 unsigned int compressed_size = 0;
1252 out_addr += out_len;
1253 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001254 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001255 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1256 dt_entry.size, out_addr, out_avai_len,
1257 &compressed_size, &dtb_size);
1258 if (rc)
1259 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001260 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001261 ASSERT(0);
1262 }
1263
Matthew Qin0b15b322015-05-19 05:20:54 -04001264 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001265 best_match_dt_addr = out_addr;
1266 } else {
1267 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1268 dtb_size = dt_entry.size;
1269 }
1270
Matthew Qin49e51fa2015-02-09 10:40:45 +08001271 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001272 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001273 {
1274 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1275 return -1;
1276 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001277
Matthew Qin271927e2015-03-31 22:07:07 -04001278 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001279 } else {
1280 /* Validate the tags_addr */
1281 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001282 {
1283 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1284 return -1;
1285 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001286 /*
1287 * If appended dev tree is found, update the atags with
1288 * memory address to the DTB appended location on RAM.
1289 * Else update with the atags address in the kernel header
1290 */
1291 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001292 dtb = dev_tree_appended((void*)(image_addr + page_size),
1293 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001294 (void *)hdr->tags_addr);
1295 if (!dtb) {
1296 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001297 return -1;
1298 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001299 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001300 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001301
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001302 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1303 target_load_ssd_keystore();
1304
Shashank Mittal23b8f422010-04-16 19:27:21 -07001305unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001306
Dima Zavin77e41f32013-03-06 16:10:43 -08001307 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001308 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001309 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1310
1311 return 0;
1312}
1313
Dima Zavin214cc642009-01-26 11:16:21 -08001314int boot_linux_from_flash(void)
1315{
1316 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001317 struct ptentry *ptn;
1318 struct ptable *ptable;
1319 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001320
Shashank Mittalcd98d472011-08-02 14:29:24 -07001321 unsigned char *image_addr = 0;
1322 unsigned kernel_actual;
1323 unsigned ramdisk_actual;
1324 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001325 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001326
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001327#if DEVICE_TREE
1328 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001329 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001330 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001331 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001332#endif
1333
David Ng183a7422009-12-07 14:55:21 -08001334 if (target_is_emmc_boot()) {
1335 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1336 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1337 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1338 return -1;
1339 }
1340 goto continue_boot;
1341 }
1342
Dima Zavin214cc642009-01-26 11:16:21 -08001343 ptable = flash_get_ptable();
1344 if (ptable == NULL) {
1345 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1346 return -1;
1347 }
1348
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001349 if(!boot_into_recovery)
1350 {
1351 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001352
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001353 if (ptn == NULL) {
1354 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1355 return -1;
1356 }
1357 }
1358 else
1359 {
1360 ptn = ptable_find(ptable, "recovery");
1361 if (ptn == NULL) {
1362 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1363 return -1;
1364 }
Dima Zavin214cc642009-01-26 11:16:21 -08001365 }
1366
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001367 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001368 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1369 return -1;
1370 }
Dima Zavin214cc642009-01-26 11:16:21 -08001371
1372 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001373 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001374 return -1;
1375 }
1376
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001377 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001378 dprintf(CRITICAL, "ERROR: Invalid boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001379 return -1;
1380 }
1381
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001382 /*
1383 * Update the kernel/ramdisk/tags address if the boot image header
1384 * has default values, these default values come from mkbootimg when
1385 * the boot image is flashed using fastboot flash:raw
1386 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001387 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001388
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001389 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001390 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1391 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1392 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1393
1394 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1395 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1396
1397 /* Check if the addresses in the header are valid. */
1398 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1399 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1400 {
1401 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1402 return -1;
1403 }
1404
1405#ifndef DEVICE_TREE
1406 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1407 {
1408 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1409 return -1;
1410 }
1411#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001412
Shashank Mittalcd98d472011-08-02 14:29:24 -07001413 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001414 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001415 {
1416 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001417 offset = 0;
1418
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001419#if DEVICE_TREE
1420 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1421 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001422
1423 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1424 {
1425 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1426 return -1;
1427 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001428#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001429 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001430#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001431
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001432 dprintf(INFO, "Loading (%s) image (%d): start\n",
1433 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001434 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001435
Shashank Mittalcd98d472011-08-02 14:29:24 -07001436 /* Read image without signature */
1437 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1438 {
1439 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1440 return -1;
1441 }
Dima Zavin214cc642009-01-26 11:16:21 -08001442
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001443 dprintf(INFO, "Loading (%s) image (%d): done\n",
1444 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001445 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001446
Shashank Mittalcd98d472011-08-02 14:29:24 -07001447 offset = imagesize_actual;
1448 /* Read signature */
1449 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1450 {
1451 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001452 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001453 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001454
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301455 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001456
1457 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001458 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1459 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001460#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001461 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001462 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001463 {
1464 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1465 return -1;
1466 }
1467
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001468 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1469#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001470
1471 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001472 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001473 {
1474 write_device_info_flash(&device);
1475 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301476#if USE_PCOM_SECBOOT
1477 set_tamper_flag(device.is_tampered);
1478#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001479 }
1480 else
1481 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001482 offset = page_size;
1483
Amol Jadib6be5c12012-11-14 13:39:51 -08001484 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1485 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1486 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1487
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001488 dprintf(INFO, "Loading (%s) image (%d): start\n",
1489 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1490
Amol Jadi492d5a52013-03-15 16:12:34 -07001491 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001492
1493 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001494 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1495 return -1;
1496 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001497 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001498
Amol Jadib6be5c12012-11-14 13:39:51 -08001499 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001500 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1501 return -1;
1502 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001503 offset += ramdisk_actual;
1504
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001505 dprintf(INFO, "Loading (%s) image (%d): done\n",
1506 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1507
Amol Jadi492d5a52013-03-15 16:12:34 -07001508 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001509
1510 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001511 offset += second_actual;
1512 /* Second image loading not implemented. */
1513 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001514 }
1515
1516#if DEVICE_TREE
1517 if(hdr->dt_size != 0) {
1518
1519 /* Read the device tree table into buffer */
1520 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1521 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1522 return -1;
1523 }
1524
1525 table = (struct dt_table*) dt_buf;
1526
Deepa Dinamani19648b42013-09-05 17:05:55 -07001527 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001528 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1529 return -1;
1530 }
1531
Deepa Dinamani19648b42013-09-05 17:05:55 -07001532 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1533 if (!table)
1534 return -1;
1535
1536 /* Read the entire device tree table into buffer */
1537 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1538 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1539 return -1;
1540 }
1541
1542
Joel Kingaa335dc2013-06-03 16:11:08 -07001543 /* Find index of device tree within device tree table */
1544 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001545 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1546 return -1;
1547 }
1548
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001549 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001550 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001551 {
1552 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1553 return -1;
1554 }
1555
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001556 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001557 if(flash_read(ptn, offset + dt_entry.offset,
1558 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001559 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1560 return -1;
1561 }
1562 }
1563#endif
1564
Shashank Mittalcd98d472011-08-02 14:29:24 -07001565 }
David Ng183a7422009-12-07 14:55:21 -08001566continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001567
Dima Zavin214cc642009-01-26 11:16:21 -08001568 /* TODO: create/pass atags to kernel */
1569
Ajay Dudanie28a6072011-07-01 13:59:46 -07001570 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001571 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001572 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1573
1574 return 0;
1575}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001576
Shashank Mittal162244e2011-08-08 19:01:25 -07001577void write_device_info_mmc(device_info *dev)
1578{
Shashank Mittal162244e2011-08-08 19:01:25 -07001579 unsigned long long ptn = 0;
1580 unsigned long long size;
1581 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001582 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001583 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001584 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001585
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001586 if (devinfo_present)
1587 index = partition_get_index("devinfo");
1588 else
1589 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001590
Shashank Mittal162244e2011-08-08 19:01:25 -07001591 ptn = partition_get_offset(index);
1592 if(ptn == 0)
1593 {
1594 return;
1595 }
1596
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001597 lun = partition_get_lun(index);
1598 mmc_set_lun(lun);
1599
Shashank Mittal162244e2011-08-08 19:01:25 -07001600 size = partition_get_size(index);
1601
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001602 blocksize = mmc_get_device_blocksize();
1603
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001604 if (devinfo_present)
1605 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1606 else
1607 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1608 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001609 {
1610 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001611 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001612 }
1613}
1614
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001615void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001616{
Shashank Mittal162244e2011-08-08 19:01:25 -07001617 unsigned long long ptn = 0;
1618 unsigned long long size;
1619 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001620 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001621 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001622
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001623 if ((index = partition_get_index("devinfo")) < 0)
1624 {
1625 devinfo_present = false;
1626 index = partition_get_index("aboot");
1627 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001628
Shashank Mittal162244e2011-08-08 19:01:25 -07001629 ptn = partition_get_offset(index);
1630 if(ptn == 0)
1631 {
1632 return;
1633 }
1634
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001635 mmc_set_lun(partition_get_lun(index));
1636
Shashank Mittal162244e2011-08-08 19:01:25 -07001637 size = partition_get_size(index);
1638
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001639 blocksize = mmc_get_device_blocksize();
1640
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001641 if (devinfo_present)
1642 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1643 else
1644 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1645 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001646 {
1647 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001648 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001649 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001650}
1651
1652void write_device_info_flash(device_info *dev)
1653{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001654 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001655 struct ptentry *ptn;
1656 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001657 if(info == NULL)
1658 {
1659 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1660 ASSERT(0);
1661 }
1662 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001663 ptable = flash_get_ptable();
1664 if (ptable == NULL)
1665 {
1666 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1667 return;
1668 }
1669
1670 ptn = ptable_find(ptable, "devinfo");
1671 if (ptn == NULL)
1672 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001673 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001674 return;
1675 }
1676
1677 memcpy(info, dev, sizeof(device_info));
1678
1679 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1680 {
1681 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1682 return;
1683 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001684 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001685}
1686
vijay kumarc65876c2015-04-24 13:29:16 +05301687static int read_allow_oem_unlock(device_info *dev)
1688{
vijay kumarc65876c2015-04-24 13:29:16 +05301689 unsigned offset;
1690 int index;
1691 unsigned long long ptn;
1692 unsigned long long ptn_size;
1693 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001694 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301695
vijay kumarca2e6812015-07-08 20:28:25 +05301696 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301697 if (index == INVALID_PTN)
1698 {
vijay kumarca2e6812015-07-08 20:28:25 +05301699 index = partition_get_index(frp_ptns[1]);
1700 if (index == INVALID_PTN)
1701 {
1702 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1703 return -1;
1704 }
vijay kumarc65876c2015-04-24 13:29:16 +05301705 }
1706
1707 ptn = partition_get_offset(index);
1708 ptn_size = partition_get_size(index);
1709 offset = ptn_size - blocksize;
1710
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001711 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301712 {
1713 dprintf(CRITICAL, "Reading MMC failed\n");
1714 return -1;
1715 }
1716
1717 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1718 is_allow_unlock = buf[blocksize-1] & 0x01;
1719 return 0;
1720}
1721
1722static int write_allow_oem_unlock(bool allow_unlock)
1723{
vijay kumarc65876c2015-04-24 13:29:16 +05301724 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301725 int index;
1726 unsigned long long ptn;
1727 unsigned long long ptn_size;
1728 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001729 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301730
vijay kumarca2e6812015-07-08 20:28:25 +05301731 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301732 if (index == INVALID_PTN)
1733 {
vijay kumarca2e6812015-07-08 20:28:25 +05301734 index = partition_get_index(frp_ptns[1]);
1735 if (index == INVALID_PTN)
1736 {
1737 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1738 return -1;
1739 }
vijay kumarc65876c2015-04-24 13:29:16 +05301740 }
1741
1742 ptn = partition_get_offset(index);
1743 ptn_size = partition_get_size(index);
1744 offset = ptn_size - blocksize;
1745
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001746 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301747 {
1748 dprintf(CRITICAL, "Reading MMC failed\n");
1749 return -1;
1750 }
1751
1752 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1753 buf[blocksize-1] = allow_unlock;
1754 if (mmc_write(ptn + offset, blocksize, buf))
1755 {
1756 dprintf(CRITICAL, "Writing MMC failed\n");
1757 return -1;
1758 }
1759
1760 return 0;
1761}
1762
Shashank Mittal162244e2011-08-08 19:01:25 -07001763void read_device_info_flash(device_info *dev)
1764{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001765 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001766 struct ptentry *ptn;
1767 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001768 if(info == NULL)
1769 {
1770 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1771 ASSERT(0);
1772 }
1773 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001774 ptable = flash_get_ptable();
1775 if (ptable == NULL)
1776 {
1777 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1778 return;
1779 }
1780
1781 ptn = ptable_find(ptable, "devinfo");
1782 if (ptn == NULL)
1783 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001784 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001785 return;
1786 }
1787
1788 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1789 {
1790 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1791 return;
1792 }
1793
1794 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1795 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001796 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1797 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001798 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001799 write_device_info_flash(info);
1800 }
1801 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001802 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001803}
1804
1805void write_device_info(device_info *dev)
1806{
1807 if(target_is_emmc_boot())
1808 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001809 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1810 if(info == NULL)
1811 {
1812 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1813 ASSERT(0);
1814 }
1815 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001816 memcpy(info, dev, sizeof(struct device_info));
1817
1818#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001819 if (is_secure_boot_enable()) {
1820 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1821 ASSERT(0);
1822 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001823 else
1824 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001825#else
1826 write_device_info_mmc(info);
1827#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001828 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001829 }
1830 else
1831 {
1832 write_device_info_flash(dev);
1833 }
1834}
1835
1836void read_device_info(device_info *dev)
1837{
1838 if(target_is_emmc_boot())
1839 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001840 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1841 if(info == NULL)
1842 {
1843 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1844 ASSERT(0);
1845 }
1846 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001847
1848#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001849 if (is_secure_boot_enable()) {
1850 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1851 ASSERT(0);
1852 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001853 else
1854 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001855#else
1856 read_device_info_mmc(info);
1857#endif
1858
1859 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1860 {
1861 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08001862 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001863 info->is_unlocked = 0;
lijuang511a2b52015-08-14 20:50:51 +08001864 info->is_unlock_critical = 0;
1865 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001866 info->is_unlocked = 1;
lijuang511a2b52015-08-14 20:50:51 +08001867 info->is_unlock_critical = 1;
1868 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001869 info->is_tampered = 0;
1870 info->charger_screen_enabled = 0;
Sridhar Parasuram287e5062015-08-24 16:17:22 -07001871 info->verity_mode = 1; //enforcing by default
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001872 write_device_info(info);
1873 }
1874 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001875 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001876 }
1877 else
1878 {
1879 read_device_info_flash(dev);
1880 }
1881}
1882
1883void reset_device_info()
1884{
1885 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001886 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001887 write_device_info(&device);
1888}
1889
1890void set_device_root()
1891{
1892 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001893 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001894 write_device_info(&device);
1895}
1896
lijuang4ece1e72015-08-14 21:02:36 +08001897/* set device unlock value
1898 * Must check FRP before call this function
1899 * Need to wipe data when unlock status changed
1900 * type 0: oem unlock
1901 * type 1: unlock critical
1902 * status 0: unlock as false
1903 * status 1: lock as true
1904 */
1905void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08001906{
lijuang4ece1e72015-08-14 21:02:36 +08001907 if (type == UNLOCK)
1908 device.is_unlocked = status;
1909 else if (type == UNLOCK_CRITICAL)
1910 device.is_unlock_critical = status;
1911
1912 write_device_info(&device);
1913}
1914
1915static void set_device_unlock(int type, bool status)
1916{
1917 int is_unlocked = -1;
1918 char response[MAX_RSP_SIZE];
1919
1920 /* check device unlock status if it is as expected */
1921 if (type == UNLOCK)
1922 is_unlocked = device.is_unlocked;
1923 else if (type == UNLOCK_CRITICAL)
1924 is_unlocked = device.is_unlock_critical;
1925
1926 if (is_unlocked == status) {
1927 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
1928 fastboot_info(response);
1929 fastboot_okay("");
1930 return;
1931 }
1932
1933 /* status is true, it means to unlock device */
1934 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08001935 if(!is_allow_unlock) {
1936 fastboot_fail("oem unlock is not allowed");
1937 return;
1938 }
1939
lijuang4ece1e72015-08-14 21:02:36 +08001940#if FBCON_DISPLAY_MSG
1941 display_unlock_menu_thread(type);
1942 fastboot_okay("");
1943 return;
1944#else
1945 if (type == UNLOCK) {
1946 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
1947 return;
1948 }
1949#endif
lijuang21f12f52015-08-22 16:22:19 +08001950 }
lijuang4ece1e72015-08-14 21:02:36 +08001951
1952 set_device_unlock_value(type, status);
1953
1954 /* wipe data */
1955 struct recovery_message msg;
1956
1957 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
1958 write_misc(0, &msg, sizeof(msg));
1959
1960 fastboot_okay("");
1961 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08001962}
1963
lijuang511a2b52015-08-14 20:50:51 +08001964static bool critical_flash_allowed(const char * entry)
1965{
1966 uint32_t i = 0;
1967 if (entry == NULL)
1968 return false;
1969
1970 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
1971 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
1972 return true;
1973 }
1974 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04001975}
1976
1977#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07001978int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
1979{
1980 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07001981 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07001982 struct dt_table *table;
1983 struct dt_entry dt_entry;
1984 uint32_t dt_hdr_size;
1985 unsigned int compressed_size = 0;
1986 unsigned int dtb_size = 0;
1987 unsigned int out_avai_len = 0;
1988 unsigned char *out_addr = NULL;
1989 unsigned char *best_match_dt_addr = NULL;
1990 int rc;
1991
1992 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1993
1994 if(hdr->dt_size != 0) {
1995 /* add kernel offset */
1996 dt_image_offset += page_size;
1997 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001998 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07001999
Deepa Dinamani19648b42013-09-05 17:05:55 -07002000 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002001 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2002 dt_image_offset += n;
2003
Joel Kingaa335dc2013-06-03 16:11:08 -07002004 /* add second offset */
2005 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002006 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2007 dt_image_offset += n;
2008 }
2009
Matthew Qin271927e2015-03-31 22:07:07 -04002010 /* offset now point to start of dt.img */
2011 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2012
2013 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2014 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002015 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002016 }
2017 /* Find index of device tree within device tree table */
2018 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2019 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2020 return -1;
Matthew Qin0b15b322015-05-19 05:20:54 -04002021 }
Matthew Qin271927e2015-03-31 22:07:07 -04002022
2023 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2024 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
Matthew Qin0b15b322015-05-19 05:20:54 -04002025 {
Matthew Qin271927e2015-03-31 22:07:07 -04002026 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2027 out_avai_len = target_get_max_flash_size() - scratch_offset;
2028 dprintf(INFO, "decompressing dtb: start\n");
2029 rc = decompress(best_match_dt_addr,
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002030 dt_entry.size, out_addr, out_avai_len,
Matthew Qin271927e2015-03-31 22:07:07 -04002031 &compressed_size, &dtb_size);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002032 if (rc)
2033 {
2034 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
2035 ASSERT(0);
2036 }
Amol Jadicb524072012-08-09 16:40:18 -07002037
Matthew Qin271927e2015-03-31 22:07:07 -04002038 dprintf(INFO, "decompressing dtb: done\n");
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002039 best_match_dt_addr = out_addr;
2040 } else {
Amol Jadicb524072012-08-09 16:40:18 -07002041 dtb_size = dt_entry.size;
2042 }
2043 /* Validate and Read device device tree in the "tags_add */
2044 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
2045 {
2046 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002047 return -1;
2048 }
Amit Blay8a510302015-08-17 09:20:01 +03002049
2050 /* Read device device tree in the "tags_add */
2051 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002052 } else
2053 return -1;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002054
2055 /* Everything looks fine. Return success. */
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002056 return 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002057}
2058#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002059
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002060void cmd_boot(const char *arg, void *data, unsigned sz)
2061{
Matthew Qinbb7923d2015-02-09 10:56:09 +08002062#ifdef MDTP_SUPPORT
2063 static bool is_mdtp_activated = 0;
2064#endif /* MDTP_SUPPORT */
2065 unsigned kernel_actual;
2066 unsigned ramdisk_actual;
2067 uint32_t image_actual;
Matthew Qin271927e2015-03-31 22:07:07 -04002068 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002069 uint32_t sig_actual = SIGNATURE_SIZE;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002070 struct boot_img_hdr *hdr = NULL;
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002071 struct kernel64_hdr *kptr = NULL;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002072 char *ptr = ((char*) data);
2073 int ret = 0;
2074 uint8_t dtb_copied = 0;
2075 unsigned int out_len = 0;
2076 unsigned int out_avai_len = 0;
2077 unsigned char *out_addr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002078 uint32_t dtb_offset = 0;
2079 unsigned char *kernel_start_addr = NULL;
2080 unsigned int kernel_size = 0;
2081 unsigned int scratch_offset = 0;
2082
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002083#if VERIFIED_BOOT
Brian Swetland9c4c0752009-01-25 16:23:50 -08002084 if(target_build_variant_user() && !device.is_unlocked)
2085 {
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002086 fastboot_fail("unlock device to use this command");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002087 return;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002088 }
2089#endif
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002090
2091 if (sz < sizeof(hdr)) {
2092 fastboot_fail("invalid bootimage header");
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002093 return;
2094 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002095
2096 hdr = (struct boot_img_hdr *)data;
2097
2098 /* ensure commandline is terminated */
2099 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2100
2101 if(target_is_emmc_boot() && hdr->page_size) {
2102 page_size = hdr->page_size;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002103 page_mask = page_size - 1;
2104 }
2105
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002106 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2107 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002108#if DEVICE_TREE
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002109 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2110#endif
2111
2112 image_actual = ADD_OF(page_size, kernel_actual);
2113 image_actual = ADD_OF(image_actual, ramdisk_actual);
2114 image_actual = ADD_OF(image_actual, dt_actual);
2115
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002116 if (target_use_signed_kernel() && (!device.is_unlocked))
2117 image_actual = ADD_OF(image_actual, sig_actual);
2118
2119 /* sz should have atleast raw boot image */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002120 if (image_actual > sz) {
Amit Blay4aa292f2015-04-28 21:55:59 +03002121 fastboot_fail("bootimage: incomplete or not signed");
2122 return;
2123 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002124
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002125 // Initialize boot state before trying to verify boot.img
2126#if VERIFIED_BOOT
2127 boot_verifier_init();
2128#endif
2129
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002130 /* Verify the boot image
2131 * device & page_size are initialized in aboot_init
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002132 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002133 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002134 /* Pass size excluding signature size, otherwise we would try to
2135 * access signature beyond its length
2136 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002137 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002138 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002139#ifdef MDTP_SUPPORT
2140 else
2141 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002142 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002143 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002144
2145 if (!is_mdtp_activated) {
2146 ext_partition.partition = MDTP_PARTITION_NONE;
2147 mdtp_fwlock_verify_lock(&ext_partition);
2148 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002149 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002150
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002151 mdtp_activated(&is_mdtp_activated);
2152 if(is_mdtp_activated){
2153 dprintf(CRITICAL, "fastboot boot command is not available.\n");
2154 return;
2155 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002156#endif /* MDTP_SUPPORT */
2157
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002158#if VERIFIED_BOOT
2159 // send root of trust
2160 if(!send_rot_command((uint32_t)device.is_unlocked))
2161 ASSERT(0);
2162#endif
2163
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002164 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002165 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2166 * If not, continue booting.
2167 */
2168 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2169 {
2170 out_addr = (unsigned char *)target_get_scratch_address();
2171 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2172 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002173 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002174 ret = decompress((unsigned char *)(ptr + page_size),
2175 hdr->kernel_size, out_addr, out_avai_len,
2176 &dtb_offset, &out_len);
2177 if (ret)
2178 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002179 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002180 ASSERT(0);
2181 }
2182
Matthew Qin0b15b322015-05-19 05:20:54 -04002183 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002184 kptr = (struct kernel64_hdr *)out_addr;
2185 kernel_start_addr = out_addr;
2186 kernel_size = out_len;
2187 } else {
2188 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2189 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2190 kernel_size = hdr->kernel_size;
2191 }
2192
2193 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002194 * Update the kernel/ramdisk/tags address if the boot image header
2195 * has default values, these default values come from mkbootimg when
2196 * the boot image is flashed using fastboot flash:raw
2197 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002198 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002199
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002200 /* Get virtual addresses since the hdr saves physical addresses. */
2201 hdr->kernel_addr = VA(hdr->kernel_addr);
2202 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2203 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002204
Matthew Qinbb7923d2015-02-09 10:56:09 +08002205 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002206 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002207 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002208 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2209 {
2210 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002211 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002212 }
2213
Amol Jadicb524072012-08-09 16:40:18 -07002214#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002215 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002216 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002217 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002218
2219 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002220#else
2221 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2222 {
2223 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002224 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002225 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002226#endif
2227
2228 /* Load ramdisk & kernel */
2229 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002230 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002231
2232#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002233 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2234 {
2235 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2236 return;
2237 }
2238
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002239 /*
2240 * If dtb is not found look for appended DTB in the kernel.
2241 * If appended dev tree is found, update the atags with
2242 * memory address to the DTB appended location on RAM.
2243 * Else update with the atags address in the kernel header
2244 */
2245 if (!dtb_copied) {
2246 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002247 dtb = dev_tree_appended((void*)(ptr + page_size),
2248 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002249 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002250 if (!dtb) {
2251 fastboot_fail("dtb not found");
2252 return;
2253 }
Amol Jadicb524072012-08-09 16:40:18 -07002254 }
2255#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002256
2257 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002258 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002259
Dima Zavin77e41f32013-03-06 16:10:43 -08002260 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002261 (const char*) hdr->cmdline, board_machtype(),
2262 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002263}
2264
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002265void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002266{
2267 struct ptentry *ptn;
2268 struct ptable *ptable;
2269
2270 ptable = flash_get_ptable();
2271 if (ptable == NULL) {
2272 fastboot_fail("partition table doesn't exist");
2273 return;
2274 }
2275
2276 ptn = ptable_find(ptable, arg);
2277 if (ptn == NULL) {
2278 fastboot_fail("unknown partition name");
2279 return;
2280 }
2281
2282 if (flash_erase(ptn)) {
2283 fastboot_fail("failed to erase partition");
2284 return;
2285 }
2286 fastboot_okay("");
2287}
2288
Bikas Gurungd48bd242010-09-04 19:54:32 -07002289
2290void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2291{
2292 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002293 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002294 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002295 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002296
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002297#if VERIFIED_BOOT
2298 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2299 {
2300 if(!device.is_unlocked)
2301 {
2302 fastboot_fail("unlock device to erase keystore");
2303 return;
2304 }
2305 }
2306#endif
2307
Kinson Chikf1a43512011-07-14 11:28:39 -07002308 index = partition_get_index(arg);
2309 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002310 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002311
Kinson Chikf1a43512011-07-14 11:28:39 -07002312 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002313 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002314 return;
2315 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002316
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002317 lun = partition_get_lun(index);
2318 mmc_set_lun(lun);
2319
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002320 if (platform_boot_dev_isemmc())
2321 {
2322 if (mmc_erase_card(ptn, size)) {
2323 fastboot_fail("failed to erase partition\n");
2324 return;
2325 }
2326 } else {
2327 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2328 size = partition_get_size(index);
2329 if (size > DEFAULT_ERASE_SIZE)
2330 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002331
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002332 /* Simple inefficient version of erase. Just writing
2333 0 in first several blocks */
2334 if (mmc_write(ptn , size, (unsigned int *)out)) {
2335 fastboot_fail("failed to erase partition");
2336 return;
2337 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002338 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002339#if VERIFIED_BOOT
2340 if(!(strncmp(arg, "userdata", 8)))
2341 if(send_delete_keys_to_tz())
2342 ASSERT(0);
2343#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002344 fastboot_okay("");
2345}
2346
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002347void cmd_erase(const char *arg, void *data, unsigned sz)
2348{
lijuang65c5a822015-08-29 16:35:36 +08002349#if CHECK_BAT_VOLTAGE
2350 if (!target_battery_soc_ok()) {
2351 fastboot_fail("Warning: battery's capacity is very low\n");
2352 return;
2353 }
2354#endif
2355
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002356#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002357 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002358 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002359 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002360 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002361 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002362 return;
2363 }
2364 }
2365#endif
2366
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002367 if(target_is_emmc_boot())
2368 cmd_erase_mmc(arg, data, sz);
2369 else
2370 cmd_erase_nand(arg, data, sz);
2371}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002372
Channagoud Kadabiad259832015-05-29 11:14:17 -07002373static uint32_t aboot_get_secret_key()
2374{
2375 /* 0 is invalid secret key, update this implementation to return
2376 * device specific unique secret key
2377 */
2378 return 0;
2379}
2380
Ajay Dudani5c761132011-04-07 20:19:04 -07002381void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002382{
2383 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002384 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002385 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002386 char *token = NULL;
2387 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002388 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002389 uint8_t lun = 0;
2390 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002391
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002392 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002393 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002394 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002395 if(token)
2396 {
2397 lun = atoi(token);
2398 mmc_set_lun(lun);
2399 lun_set = true;
2400 }
2401
Mao Jinlong226f33a2014-07-04 17:24:10 +08002402 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002403 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002404 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2405 {
2406 if (!aboot_frp_unlock(pname, data, sz))
2407 {
2408 fastboot_info("FRP unlock successful");
2409 fastboot_okay("");
2410 }
2411 else
2412 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2413
2414 return;
2415 }
2416
Mao Jinlong226f33a2014-07-04 17:24:10 +08002417 if (!strcmp(pname, "partition"))
2418 {
2419 dprintf(INFO, "Attempt to write partition image.\n");
2420 if (write_partition(sz, (unsigned char *) data)) {
2421 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002422 return;
2423 }
2424 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002425 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002426 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002427#if VERIFIED_BOOT
2428 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2429 {
2430 if(!device.is_unlocked)
2431 {
2432 fastboot_fail("unlock device to flash keystore");
2433 return;
2434 }
2435 if(!boot_verify_validate_keystore((unsigned char *)data))
2436 {
2437 fastboot_fail("image is not a keystore file");
2438 return;
2439 }
2440 }
2441#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002442 index = partition_get_index(pname);
2443 ptn = partition_get_offset(index);
2444 if(ptn == 0) {
2445 fastboot_fail("partition table doesn't exist");
2446 return;
2447 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002448
Mao Jinlong226f33a2014-07-04 17:24:10 +08002449 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2450 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2451 fastboot_fail("image is not a boot image");
2452 return;
2453 }
2454 }
2455
2456 if(!lun_set)
2457 {
2458 lun = partition_get_lun(index);
2459 mmc_set_lun(lun);
2460 }
2461
2462 size = partition_get_size(index);
2463 if (ROUND_TO_PAGE(sz,511) > size) {
2464 fastboot_fail("size too large");
2465 return;
2466 }
2467 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2468 fastboot_fail("flash write failure");
2469 return;
2470 }
Greg Grisco6e754772011-06-23 12:19:39 -07002471 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002472 }
2473 fastboot_okay("");
2474 return;
2475}
2476
Ajay Dudanide984792015-03-02 09:57:41 -08002477void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2478{
2479 int i, images;
2480 meta_header_t *meta_header;
2481 img_header_entry_t *img_header_entry;
2482
2483 meta_header = (meta_header_t*) data;
2484 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2485
2486 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2487
2488 for (i=0; i<images; i++) {
2489
2490 if((img_header_entry[i].ptn_name == NULL) ||
2491 (img_header_entry[i].start_offset == 0) ||
2492 (img_header_entry[i].size == 0))
2493 break;
2494
2495 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2496 (void *) data + img_header_entry[i].start_offset,
2497 img_header_entry[i].size);
2498 }
2499
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002500 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2501 {
2502 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2503 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2504 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2505 }
2506 else
2507 {
2508 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2509 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2510 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2511 }
2512
2513 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002514 fastboot_okay("");
2515 return;
2516}
2517
Ajay Dudani5c761132011-04-07 20:19:04 -07002518void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2519{
2520 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002521 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002522 uint32_t *fill_buf = NULL;
2523 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002524 sparse_header_t *sparse_header;
2525 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002526 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002527 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302528 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002529 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302530 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002531 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302532 /*End of the sparse image address*/
2533 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002534
Kinson Chikf1a43512011-07-14 11:28:39 -07002535 index = partition_get_index(arg);
2536 ptn = partition_get_offset(index);
2537 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002538 fastboot_fail("partition table doesn't exist");
2539 return;
2540 }
2541
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302542 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302543
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002544 lun = partition_get_lun(index);
2545 mmc_set_lun(lun);
2546
vijay kumar800255e2015-04-24 20:26:19 +05302547 if (sz < sizeof(sparse_header_t)) {
2548 fastboot_fail("size too low");
2549 return;
2550 }
2551
Ajay Dudani5c761132011-04-07 20:19:04 -07002552 /* Read and skip over sparse image header */
2553 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302554
vijay kumar1321f342015-03-27 12:13:42 +05302555 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002556 fastboot_fail("size too large");
2557 return;
2558 }
2559
vijay kumarde4fcf62015-04-23 13:05:49 +05302560 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302561
2562 if (data_end < (uint32_t)data) {
2563 fastboot_fail("buffer overreads occured due to invalid sparse header");
2564 return;
2565 }
2566
vijay kumarde4fcf62015-04-23 13:05:49 +05302567 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002568 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302569 fastboot_fail("sparse header size mismatch");
2570 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002571 }
2572
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002573 dprintf (SPEW, "=== Sparse Image Header ===\n");
2574 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2575 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2576 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2577 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2578 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2579 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2580 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2581 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002582
2583 /* Start processing chunks */
2584 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2585 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302586 /* Make sure the total image size does not exceed the partition size */
2587 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2588 fastboot_fail("size too large");
2589 return;
2590 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002591 /* Read and skip over chunk header */
2592 chunk_header = (chunk_header_t *) data;
2593 data += sizeof(chunk_header_t);
2594
vijay kumar800255e2015-04-24 20:26:19 +05302595 if (data_end < (uint32_t)data) {
2596 fastboot_fail("buffer overreads occured due to invalid sparse header");
2597 return;
2598 }
2599
Ajay Dudani5c761132011-04-07 20:19:04 -07002600 dprintf (SPEW, "=== Chunk Header ===\n");
2601 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2602 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2603 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2604
vijay kumar800255e2015-04-24 20:26:19 +05302605 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002606 {
vijay kumar800255e2015-04-24 20:26:19 +05302607 fastboot_fail("chunk header size mismatch");
2608 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002609 }
2610
wufeng.jiang813dc352015-06-02 23:02:46 -04002611 if (!sparse_header->blk_sz ){
2612 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302613 return;
2614 }
2615
wufeng.jiang813dc352015-06-02 23:02:46 -04002616 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2617
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302618 /* Make sure that the chunk size calculated from sparse image does not
2619 * exceed partition size
2620 */
2621 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2622 {
2623 fastboot_fail("Chunk data size exceeds partition size");
2624 return;
2625 }
2626
Ajay Dudani5c761132011-04-07 20:19:04 -07002627 switch (chunk_header->chunk_type)
2628 {
2629 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002630 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002631 chunk_data_sz))
2632 {
2633 fastboot_fail("Bogus chunk size for chunk type Raw");
2634 return;
2635 }
2636
vijay kumar800255e2015-04-24 20:26:19 +05302637 if (data_end < (uint32_t)data + chunk_data_sz) {
2638 fastboot_fail("buffer overreads occured due to invalid sparse header");
2639 return;
2640 }
2641
wufeng.jiang813dc352015-06-02 23:02:46 -04002642 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2643 otherwise it will return in the line above
2644 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002645 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002646 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002647 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002648 {
2649 fastboot_fail("flash write failure");
2650 return;
2651 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302652 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2653 fastboot_fail("Bogus size for RAW chunk type");
2654 return;
2655 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002656 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002657 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002658 break;
2659
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002660 case CHUNK_TYPE_FILL:
2661 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2662 sizeof(uint32_t)))
2663 {
2664 fastboot_fail("Bogus chunk size for chunk type FILL");
2665 return;
2666 }
2667
2668 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2669 if (!fill_buf)
2670 {
2671 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2672 return;
2673 }
2674
vijay kumar800255e2015-04-24 20:26:19 +05302675 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2676 fastboot_fail("buffer overreads occured due to invalid sparse header");
2677 return;
2678 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002679 fill_val = *(uint32_t *)data;
2680 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002681
2682 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2683 {
2684 fill_buf[i] = fill_val;
2685 }
2686
wufeng.jiang813dc352015-06-02 23:02:46 -04002687 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002688 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302689 /* Make sure that the data written to partition does not exceed partition size */
2690 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2691 {
2692 fastboot_fail("Chunk data size for fill type exceeds partition size");
2693 return;
2694 }
2695
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002696 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2697 sparse_header->blk_sz,
2698 fill_buf))
2699 {
2700 fastboot_fail("flash write failure");
2701 free(fill_buf);
2702 return;
2703 }
2704
2705 total_blocks++;
2706 }
2707
2708 free(fill_buf);
2709 break;
2710
Ajay Dudani5c761132011-04-07 20:19:04 -07002711 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302712 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2713 fastboot_fail("bogus size for chunk DONT CARE type");
2714 return;
2715 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002716 total_blocks += chunk_header->chunk_sz;
2717 break;
2718
Ajay Dudani5c761132011-04-07 20:19:04 -07002719 case CHUNK_TYPE_CRC:
2720 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2721 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002722 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002723 return;
2724 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302725 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2726 fastboot_fail("bogus size for chunk CRC type");
2727 return;
2728 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002729 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302730 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2731 fastboot_fail("integer overflow occured");
2732 return;
2733 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002734 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302735 if (data_end < (uint32_t)data) {
2736 fastboot_fail("buffer overreads occured due to invalid sparse header");
2737 return;
2738 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002739 break;
2740
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002741 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002742 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002743 fastboot_fail("Unknown chunk type");
2744 return;
2745 }
2746 }
2747
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002748 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2749 total_blocks, sparse_header->total_blks);
2750
2751 if(total_blocks != sparse_header->total_blks)
2752 {
2753 fastboot_fail("sparse image write failure");
2754 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002755
2756 fastboot_okay("");
2757 return;
2758}
2759
2760void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2761{
2762 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002763 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002764
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002765#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002766 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2767 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002768 int ret=0;
2769 uint32 major_version=0;
2770 uint32 minor_version=0;
2771
2772 ret = scm_svc_version(&major_version,&minor_version);
2773 if(!ret)
2774 {
2775 if(major_version >= 2)
2776 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002777 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002778 {
2779 ret = encrypt_scm((uint32 **) &data, &sz);
2780 if (ret != 0) {
2781 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2782 return;
2783 }
2784
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002785 /* Protect only for SSD */
2786 if (!strcmp(arg, "ssd")) {
2787 ret = scm_protect_keystore((uint32 *) data, sz);
2788 if (ret != 0) {
2789 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2790 return;
2791 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002792 }
2793 }
2794 else
2795 {
2796 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2797 if(ret != 0)
2798 {
2799 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2800 return;
2801 }
2802 }
2803 }
2804 else
2805 {
2806 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2807 magic_number[1] == DECRYPT_MAGIC_1)
2808 {
2809 ret = decrypt_scm((uint32 **) &data, &sz);
2810 if (ret != 0) {
2811 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2812 return;
2813 }
2814 }
2815 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2816 magic_number[1] == ENCRYPT_MAGIC_1)
2817 {
2818 ret = encrypt_scm((uint32 **) &data, &sz);
2819 if (ret != 0) {
2820 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2821 return;
2822 }
2823 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002824 }
2825 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002826 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002827 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002828 dprintf(CRITICAL,"INVALID SVC Version\n");
2829 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002830 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002831#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002832
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002833#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002834 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002835 {
lijuang511a2b52015-08-14 20:50:51 +08002836 /* if device is locked:
2837 * common partition will not allow to be flashed
2838 * critical partition will allow to flash image.
2839 */
2840 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
2841 fastboot_fail("Partition flashing is not allowed");
2842 return;
2843 }
2844
2845 /* if device critical is locked:
2846 * common partition will allow to be flashed
2847 * critical partition will not allow to flash image.
2848 */
2849 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
2850 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002851 return;
2852 }
2853 }
2854#endif
2855
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002856 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002857 meta_header = (meta_header_t *) data;
2858 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002859 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002860 else if (meta_header->magic == META_HEADER_MAGIC)
2861 cmd_flash_meta_img(arg, data, sz);
2862 else
2863 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002864
2865#if VERIFIED_BOOT
2866 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
2867 {
2868 // reset dm_verity mode to enforcing
2869 device.verity_mode = 1;
2870 write_device_info(&device);
2871 }
2872#endif
2873
Ajay Dudani5c761132011-04-07 20:19:04 -07002874 return;
2875}
2876
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002877void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2878{
2879 struct ptentry *sys_ptn;
2880 struct ptable *ptable;
2881
2882 ptable = flash_get_ptable();
2883 if (ptable == NULL) {
2884 fastboot_fail("partition table doesn't exist");
2885 return;
2886 }
2887
2888 sys_ptn = ptable_find(ptable, "system");
2889 if (sys_ptn == NULL) {
2890 fastboot_fail("system partition not found");
2891 return;
2892 }
2893
2894 sz = ROUND_TO_PAGE(sz, page_mask);
2895 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2896 fastboot_fail("update_ubi_vol failed");
2897 else
2898 fastboot_okay("");
2899}
2900
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002901void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002902{
2903 struct ptentry *ptn;
2904 struct ptable *ptable;
2905 unsigned extra = 0;
2906
2907 ptable = flash_get_ptable();
2908 if (ptable == NULL) {
2909 fastboot_fail("partition table doesn't exist");
2910 return;
2911 }
2912
2913 ptn = ptable_find(ptable, arg);
2914 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002915 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2916 arg);
2917 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002918 return;
2919 }
2920
2921 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2922 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2923 fastboot_fail("image is not a boot image");
2924 return;
2925 }
2926 }
2927
Amol Jadi5c61a952012-05-04 17:05:35 -07002928 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002929 || !strcmp(ptn->name, "userdata")
2930 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002931 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002932 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002933 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002934 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002935 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002936
2937 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002938 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2939 if (flash_ubi_img(ptn, data, sz)) {
2940 fastboot_fail("flash write failure");
2941 return;
2942 }
2943 } else {
2944 if (flash_write(ptn, extra, data, sz)) {
2945 fastboot_fail("flash write failure");
2946 return;
2947 }
Dima Zavin214cc642009-01-26 11:16:21 -08002948 }
2949 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2950 fastboot_okay("");
2951}
2952
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002953void cmd_flash(const char *arg, void *data, unsigned sz)
2954{
lijuang65c5a822015-08-29 16:35:36 +08002955#if CHECK_BAT_VOLTAGE
2956 if (!target_battery_soc_ok()) {
2957 fastboot_fail("Warning: battery's capacity is very low\n");
2958 return;
2959 }
2960#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002961 if(target_is_emmc_boot())
2962 cmd_flash_mmc(arg, data, sz);
2963 else
2964 cmd_flash_nand(arg, data, sz);
2965}
2966
Dima Zavin214cc642009-01-26 11:16:21 -08002967void cmd_continue(const char *arg, void *data, unsigned sz)
2968{
2969 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002970 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002971
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002972 if (target_is_emmc_boot())
2973 {
lijuanga40d6302015-07-20 20:10:13 +08002974#if FBCON_DISPLAY_MSG
2975 keys_detect_init();
2976#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002977 boot_linux_from_mmc();
2978 }
2979 else
2980 {
2981 boot_linux_from_flash();
2982 }
Dima Zavin214cc642009-01-26 11:16:21 -08002983}
2984
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002985void cmd_reboot(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(0);
2990}
2991
2992void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2993{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002994 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002995 fastboot_okay("");
2996 reboot_device(FASTBOOT_MODE);
2997}
2998
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002999void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3000{
3001 dprintf(INFO, "Enabling charger screen check\n");
3002 device.charger_screen_enabled = 1;
3003 write_device_info(&device);
3004 fastboot_okay("");
3005}
3006
3007void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3008{
3009 dprintf(INFO, "Disabling charger screen check\n");
3010 device.charger_screen_enabled = 0;
3011 write_device_info(&device);
3012 fastboot_okay("");
3013}
3014
lijuanga25d8bb2015-09-16 13:11:52 +08003015void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3016{
3017 char *p = NULL;
3018 const char *delim = " \t\n\r";
3019
3020 if (arg) {
3021 p = strtok((char *)arg, delim);
3022 if (p) {
3023 if (!strncmp(p, "0", 1)) {
3024 device.charger_screen_enabled = 0;
3025 } else if (!strncmp(p, "1", 1)) {
3026 device.charger_screen_enabled = 1;
3027 }
3028 }
3029 }
3030
3031 /* update charger_screen_enabled value for getvar
3032 * command
3033 */
3034 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3035 device.charger_screen_enabled);
3036
3037 write_device_info(&device);
3038 fastboot_okay("");
3039}
3040
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303041void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3042{
3043 dprintf(INFO, "Selecting display panel %s\n", arg);
3044 if (arg)
3045 strlcpy(device.display_panel, arg,
3046 sizeof(device.display_panel));
3047 write_device_info(&device);
3048 fastboot_okay("");
3049}
3050
Shashank Mittal162244e2011-08-08 19:01:25 -07003051void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3052{
lijuang4ece1e72015-08-14 21:02:36 +08003053 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303054}
3055
3056void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3057{
lijuang4ece1e72015-08-14 21:02:36 +08003058 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303059 if(!is_allow_unlock) {
3060 fastboot_fail("oem unlock is not allowed");
3061 return;
3062 }
3063
lijuang4ece1e72015-08-14 21:02:36 +08003064 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303065
lijuang4ece1e72015-08-14 21:02:36 +08003066 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303067 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003068
vijay kumarc65876c2015-04-24 13:29:16 +05303069 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3070 write_misc(0, &msg, sizeof(msg));
3071
3072 fastboot_okay("");
3073 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003074 }
3075 fastboot_okay("");
3076}
3077
Channagoud Kadabiad259832015-05-29 11:14:17 -07003078static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3079{
3080 int ret = 1;
3081 uint32_t secret_key;
3082 char seckey_buffer[MAX_RSP_SIZE];
3083
3084 secret_key = aboot_get_secret_key();
3085 if (secret_key)
3086 {
3087 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3088 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3089 {
3090 is_allow_unlock = true;
3091 write_allow_oem_unlock(is_allow_unlock);
3092 ret = 0;
3093 }
3094 }
3095 return ret;
3096}
3097
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003098void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3099{
lijuang4ece1e72015-08-14 21:02:36 +08003100 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003101}
3102
Shashank Mittala0032282011-08-26 14:50:11 -07003103void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3104{
lijuang511a2b52015-08-14 20:50:51 +08003105 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003106 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003107 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003108 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3109 fastboot_info(response);
lijuang511a2b52015-08-14 20:50:51 +08003110 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3111 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003112 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003113 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303114 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3115 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003116 fastboot_okay("");
3117}
3118
lijuang511a2b52015-08-14 20:50:51 +08003119void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3120{
3121 char response[MAX_RSP_SIZE];
3122 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3123 fastboot_info(response);
3124 fastboot_okay("");
3125}
3126
3127void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3128{
lijuang4ece1e72015-08-14 21:02:36 +08003129 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003130}
3131
3132void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3133{
lijuang4ece1e72015-08-14 21:02:36 +08003134 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003135}
3136
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003137void cmd_preflash(const char *arg, void *data, unsigned sz)
3138{
3139 fastboot_okay("");
3140}
3141
Mao Flynn7b379f32015-04-20 00:28:30 +08003142static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303143
Mao Flynn7b379f32015-04-20 00:28:30 +08003144int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303145{
Mao Flynn7b379f32015-04-20 00:28:30 +08003146 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303147 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003148 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303149 return -1;
3150 return 0;
3151}
3152
Mao Flynn7b379f32015-04-20 00:28:30 +08003153int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003154{
3155 struct ptentry *ptn;
3156 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003157 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003158 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003159
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303160 ptable = flash_get_ptable();
3161 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003162 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3163 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303164 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003165
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303166 ptn = ptable_find(ptable, "splash");
3167 if (ptn == NULL) {
3168 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003169 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303170 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003171 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303172 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003173 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303174 }
3175
Mao Flynn7b379f32015-04-20 00:28:30 +08003176 header = (struct logo_img_header *)logo_header;
3177 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303178 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003179 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303180 }
3181
3182 fb_display = fbcon_display();
3183 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003184 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3185 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3186
3187 /* if the logo is full-screen size, remove "fbcon_clear()" */
3188 if ((header->width != fb_display->width)
3189 || (header->height != fb_display->height))
3190 fbcon_clear();
3191
3192 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3193 (uint32_t *)base,
3194 (header->blocks * 512))) {
3195 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3196 return -1;
3197 }
3198 fbcon_extract_to_screen(header, base);
3199 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003200 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003201
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003202 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3203 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003204 return -1;
3205 }
3206
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303207 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003208 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3209 (uint32_t *)base,
3210 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303211 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303212 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003213 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003214 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303215 }
3216
Mao Flynn7b379f32015-04-20 00:28:30 +08003217 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303218}
3219
Mao Flynn7b379f32015-04-20 00:28:30 +08003220int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303221{
3222 int index = INVALID_PTN;
3223 unsigned long long ptn = 0;
3224 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003225 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003226 uint32_t blocksize, realsize, readsize;
3227 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303228
3229 index = partition_get_index("splash");
3230 if (index == 0) {
3231 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003232 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303233 }
3234
3235 ptn = partition_get_offset(index);
3236 if (ptn == 0) {
3237 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003238 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303239 }
3240
Mao Flynn1893a7f2015-06-03 12:03:36 +08003241 mmc_set_lun(partition_get_lun(index));
3242
3243 blocksize = mmc_get_device_blocksize();
3244 if (blocksize == 0) {
3245 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3246 return -1;
3247 }
3248
3249 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003250 if (!fb_display)
3251 {
3252 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3253 return -1;
3254 }
3255
Mao Flynn1893a7f2015-06-03 12:03:36 +08003256 base = (uint8_t *) fb_display->base;
3257
3258 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303259 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003260 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303261 }
3262
Mao Flynn1893a7f2015-06-03 12:03:36 +08003263 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003264 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303265 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003266 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303267 }
3268
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303269 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003270 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3271 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003272
Mao Flynn1893a7f2015-06-03 12:03:36 +08003273 realsize = header->blocks * 512;
3274 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3275
3276 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003277 if ((header->width != fb_display->width)
3278 || (header->height != fb_display->height))
3279 fbcon_clear();
3280
Mao Flynn1893a7f2015-06-03 12:03:36 +08003281 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003282 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3283 return -1;
3284 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003285
Mao Flynn1893a7f2015-06-03 12:03:36 +08003286 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3287 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303288
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003289 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3290 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003291 return -1;
3292 }
3293
3294 realsize = header->width * header->height * fb_display->bpp / 8;
3295 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3296
3297 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3298 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3299 fbcon_clear();
3300 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3301 return -1;
3302 }
3303 } else {
3304 if (mmc_read(ptn + blocksize ,
3305 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3306 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3307 return -1;
3308 }
3309 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3310 }
3311 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303312 }
3313
Mao Flynn7b379f32015-04-20 00:28:30 +08003314 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303315}
3316
Mao Flynn7b379f32015-04-20 00:28:30 +08003317int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303318{
3319 if (target_is_emmc_boot()) {
3320 return splash_screen_mmc();
3321 } else {
3322 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003323 }
3324}
3325
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003326/* Get the size from partiton name */
3327static void get_partition_size(const char *arg, char *response)
3328{
3329 uint64_t ptn = 0;
3330 uint64_t size;
3331 int index = INVALID_PTN;
3332
3333 index = partition_get_index(arg);
3334
3335 if (index == INVALID_PTN)
3336 {
3337 dprintf(CRITICAL, "Invalid partition index\n");
3338 return;
3339 }
3340
3341 ptn = partition_get_offset(index);
3342
3343 if(!ptn)
3344 {
3345 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3346 return;
3347 }
3348
3349 size = partition_get_size(index);
3350
3351 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3352 return;
3353}
3354
3355/*
3356 * Publish the partition type & size info
3357 * fastboot getvar will publish the required information.
3358 * fastboot getvar partition_size:<partition_name>: partition size in hex
3359 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3360 */
3361static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3362{
3363 uint8_t i;
3364
3365 for (i = 0; i < num_parts; i++) {
3366 get_partition_size(info[i].part_name, info[i].size_response);
3367
3368 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3369 {
3370 dprintf(CRITICAL, "partition size name truncated\n");
3371 return;
3372 }
3373 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3374 {
3375 dprintf(CRITICAL, "partition type name truncated\n");
3376 return;
3377 }
3378
3379 /* publish partition size & type info */
3380 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3381 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3382 }
3383}
3384
lijuang4ece1e72015-08-14 21:02:36 +08003385void get_product_name(unsigned char *buf)
3386{
3387 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3388 return;
3389}
3390
3391void get_bootloader_version(unsigned char *buf)
3392{
3393 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3394 return;
3395}
3396
3397void get_baseband_version(unsigned char *buf)
3398{
3399 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3400 return;
3401}
3402
3403bool is_device_locked()
3404{
3405 return device.is_unlocked ? false:true;
3406}
3407
Amol Jadi5edf3552013-07-23 14:15:34 -07003408/* register commands and variables for fastboot */
3409void aboot_fastboot_register_commands(void)
3410{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003411 int i;
lijuangf16461c2015-08-03 17:09:34 +08003412 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003413
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003414 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003415 /* By default the enabled list is empty. */
3416 {"", NULL},
3417 /* move commands enclosed within the below ifndef to here
3418 * if they need to be enabled in user build.
3419 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003420#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003421 /* Register the following commands only for non-user builds */
3422 {"flash:", cmd_flash},
3423 {"erase:", cmd_erase},
3424 {"boot", cmd_boot},
3425 {"continue", cmd_continue},
3426 {"reboot", cmd_reboot},
3427 {"reboot-bootloader", cmd_reboot_bootloader},
3428 {"oem unlock", cmd_oem_unlock},
3429 {"oem unlock-go", cmd_oem_unlock_go},
3430 {"oem lock", cmd_oem_lock},
3431 {"flashing unlock", cmd_oem_unlock},
3432 {"flashing lock", cmd_oem_lock},
3433 {"flashing lock_critical", cmd_flashing_lock_critical},
3434 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3435 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3436 {"oem device-info", cmd_oem_devinfo},
3437 {"preflash", cmd_preflash},
3438 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3439 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003440 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003441 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003442#endif
lijuang511a2b52015-08-14 20:50:51 +08003443 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003444
3445 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3446 for (i = 1; i < fastboot_cmds_count; i++)
3447 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3448
Amol Jadi5edf3552013-07-23 14:15:34 -07003449 /* publish variables and their values */
3450 fastboot_publish("product", TARGET(BOARD));
3451 fastboot_publish("kernel", "lk");
3452 fastboot_publish("serialno", sn_buf);
3453
3454 /*
3455 * partition info is supported only for emmc partitions
3456 * Calling this for NAND prints some error messages which
3457 * is harmless but misleading. Avoid calling this for NAND
3458 * devices.
3459 */
3460 if (target_is_emmc_boot())
3461 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3462
3463 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003464 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3465 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003466 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003467 /* Is the charger screen check enabled */
3468 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3469 device.charger_screen_enabled);
3470 fastboot_publish("charger-screen-enabled",
3471 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003472 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303473 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3474 device.display_panel);
3475 fastboot_publish("display-panel",
3476 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003477 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3478 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003479 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3480 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3481 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3482 target_is_emmc_boot()? "eMMC":"UFS");
3483 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003484#if CHECK_BAT_VOLTAGE
3485 snprintf(battery_voltage, MAX_RSP_SIZE, "%d",
3486 target_get_battery_voltage());
3487 fastboot_publish("battery-voltage", (const char *) battery_voltage);
3488 fastboot_publish("battery-soc-ok", target_battery_soc_ok()? "yes":"no");
3489#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003490}
3491
Brian Swetland9c4c0752009-01-25 16:23:50 -08003492void aboot_init(const struct app_descriptor *app)
3493{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003494 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03003495 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003496
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003497 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003498 if (target_is_emmc_boot())
3499 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003500 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003501 page_mask = page_size - 1;
3502 }
3503 else
3504 {
3505 page_size = flash_page_size();
3506 page_mask = page_size - 1;
3507 }
3508
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003509 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3510
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003511 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303512 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003513
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003514 /* Display splash screen if enabled */
3515#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003516#if NO_ALARM_DISPLAY
3517 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003518#endif
lijuang99c02d82015-02-13 19:04:34 +08003519 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003520#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003521 /* Wait if the display shutdown is in progress */
3522 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003523 if (!pm_appsbl_display_init_done())
3524 target_display_init(device.display_panel);
3525 else
3526 display_image_on_screen();
3527#else
lijuang99c02d82015-02-13 19:04:34 +08003528 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003529#endif
lijuang99c02d82015-02-13 19:04:34 +08003530 dprintf(SPEW, "Display Init: Done\n");
3531#if NO_ALARM_DISPLAY
3532 }
3533#endif
3534#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003535
Greg Griscod6250552011-06-29 14:40:23 -07003536 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003537 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003538
Dhaval Patel223ec952013-07-18 14:49:44 -07003539 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3540
Matthew Qindefd5562014-07-11 18:02:40 +08003541 /*
3542 * Check power off reason if user force reset,
3543 * if yes phone will do normal boot.
3544 */
3545 if (is_user_force_reset())
3546 goto normal_boot;
3547
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003548 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003549 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003550 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003551 dprintf(ALWAYS,"dload mode key sequence detected\n");
3552 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003553 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003554 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003555 }
3556 else
3557 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303558 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003559 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003560 }
3561 boot_into_fastboot = true;
3562 }
3563 if (!boot_into_fastboot)
3564 {
3565 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3566 boot_into_recovery = 1;
3567 if (!boot_into_recovery &&
3568 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003569 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003570 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003571 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003572 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003573 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003574 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003575
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003576#if USE_PON_REBOOT_REG
3577 reboot_mode = check_hard_reboot_mode();
3578#else
Ajay Dudani77421292010-10-27 19:34:06 -07003579 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003580#endif
3581 if (reboot_mode == RECOVERY_MODE)
3582 {
Ajay Dudani77421292010-10-27 19:34:06 -07003583 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003584 }
3585 else if(reboot_mode == FASTBOOT_MODE)
3586 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003587 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003588 }
3589 else if(reboot_mode == ALARM_BOOT)
3590 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003591 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003592 }
3593 else if (reboot_mode == DM_VERITY_ENFORCING)
3594 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003595 device.verity_mode = 1;
3596 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003597 }
3598 else if (reboot_mode == DM_VERITY_LOGGING)
3599 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003600 device.verity_mode = 0;
3601 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003602 }
3603 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3604 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003605 if(send_delete_keys_to_tz())
3606 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003607 }
3608
Matthew Qindefd5562014-07-11 18:02:40 +08003609normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003610 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003611 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003612 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003613 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003614 if(emmc_recovery_init())
3615 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3616 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003617 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003618 if((device.is_unlocked) || (device.is_tampered))
3619 {
3620 #ifdef TZ_TAMPER_FUSE
3621 set_tamper_fuse_cmd();
3622 #endif
3623 #if USE_PCOM_SECBOOT
3624 set_tamper_flag(device.is_tampered);
3625 #endif
3626 }
Shashank Mittala0032282011-08-26 14:50:11 -07003627 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003628
Pavel Nedev5d91d412013-04-29 11:34:24 +03003629 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003630 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003631 else
3632 {
3633 recovery_init();
3634 #if USE_PCOM_SECBOOT
3635 if((device.is_unlocked) || (device.is_tampered))
3636 set_tamper_flag(device.is_tampered);
3637 #endif
3638 boot_linux_from_flash();
3639 }
3640 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3641 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003642 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003643
Amol Jadi5edf3552013-07-23 14:15:34 -07003644 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003645
Amol Jadi5edf3552013-07-23 14:15:34 -07003646 /* register aboot specific fastboot commands */
3647 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003648
Amol Jadi5edf3552013-07-23 14:15:34 -07003649 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003650 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003651
3652 /* initialize and start fastboot */
3653 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003654#if FBCON_DISPLAY_MSG
3655 display_fastboot_menu_thread();
3656#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003657}
3658
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003659uint32_t get_page_size()
3660{
3661 return page_size;
3662}
3663
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003664/*
3665 * Calculated and save hash (SHA256) for non-signed boot image.
3666 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003667 * @param image_addr - Boot image address
3668 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003669 *
3670 * @return int - 0 on success, negative value on failure.
3671 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003672static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003673{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003674 unsigned int digest[8];
3675#if IMAGE_VERIF_ALGO_SHA1
3676 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3677#else
3678 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3679#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003680
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003681 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003682 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003683
3684 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003685 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003686
3687 return 0;
3688}
3689
Brian Swetland9c4c0752009-01-25 16:23:50 -08003690APP_START(aboot)
3691 .init = aboot_init,
3692APP_END