blob: d8212ada79e319726f53848ccfdaacf2bf004a33 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08005 * Copyright (c) 2009-2015, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080060#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053061#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080062#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080063#if USE_RPMB_FOR_DEVINFO
64#include <rpmb.h>
65#endif
Dima Zavin214cc642009-01-26 11:16:21 -080066
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070067#if ENABLE_WBC
68#include <pm_app_smbchg.h>
69#endif
70
Neeti Desai17379b82012-06-04 18:42:53 -070071#if DEVICE_TREE
72#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070073#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070074#endif
75
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053076#if WDOG_SUPPORT
77#include <wdog.h>
78#endif
79
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070080#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070081#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080082#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080083#include "bootimg.h"
84#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070085#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080086#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070087#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070088#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070089#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020091#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070092#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080093#include <menu_keys_detect.h>
94#include <display_menu.h>
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070095
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070096extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070097extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070098extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070099extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530100 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700101
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700102void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700103void write_device_info_mmc(device_info *dev);
104void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700105static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700106static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700107
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700108/* fastboot command function pointer */
109typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
110
111struct fastboot_cmd_desc {
112 char * name;
113 fastboot_cmd_fn cb;
114};
115
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700116#define EXPAND(NAME) #NAME
117#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700118
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800119#ifdef MEMBASE
120#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
121#else
David Ng183a7422009-12-07 14:55:21 -0800122#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800123#endif
124
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700125#ifndef MEMSIZE
126#define MEMSIZE 1024*1024
127#endif
128
129#define MAX_TAGS_SIZE 1024
130
Kun Liang2f1601a2013-08-12 16:29:54 +0800131/* make 4096 as default size to ensure EFS,EXT4's erasing */
132#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700133#define MAX_PANEL_BUF_SIZE 196
Kun Liang2f1601a2013-08-12 16:29:54 +0800134
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700135#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700136#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800137
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800138#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
139
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700140#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
141
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800142#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700143static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700144#else
David Ng183a7422009-12-07 14:55:21 -0800145static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700146#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800147static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300148static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800149static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800150static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800151static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700152static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300153static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200154static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800155
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800156static const char *baseband_apq = " androidboot.baseband=apq";
157static const char *baseband_msm = " androidboot.baseband=msm";
158static const char *baseband_csfb = " androidboot.baseband=csfb";
159static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700160static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800161static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700162static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800163static const char *baseband_dsda = " androidboot.baseband=dsda";
164static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800165static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800166static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800167
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700168#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700169static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700170static const char *verified_state= " androidboot.verifiedbootstate=";
171
172//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700173
174struct verified_boot_verity_mode vbvm[] =
175{
176 {false, "logging"},
177 {true, "enforcing"},
178};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700179struct verified_boot_state_name vbsn[] =
180{
181 {GREEN, "green"},
182 {ORANGE, "orange"},
183 {YELLOW,"yellow"},
184 {RED,"red" },
185};
186#endif
187
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700188static unsigned page_size = 0;
189static unsigned page_mask = 0;
190static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
191static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530192static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800193static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700194static bool devinfo_present = true;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700195
Shashank Mittalcd98d472011-08-02 14:29:24 -0700196/* Assuming unauthorized kernel image by default */
197static int auth_kernel_img = 0;
198
lijuang511a2b52015-08-14 20:50:51 +0800199static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
vijay kumarc65876c2015-04-24 13:29:16 +0530200static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700201
vijay kumarca2e6812015-07-08 20:28:25 +0530202static char frp_ptns[2][8] = {"config","frp"};
203
lijuang511a2b52015-08-14 20:50:51 +0800204static const char *critical_flash_allowed_ptn[] = {
205 "aboot",
206 "rpm",
207 "tz",
208 "sbl",
209 "sdi",
210 "sbl1",
211 "xbl",
212 "hyp",
213 "pmic",
214 "bootloader",
215 "devinfo",
216 "partition"};
217
Dima Zavin42168f22009-01-30 11:52:22 -0800218struct atag_ptbl_entry
219{
220 char name[16];
221 unsigned offset;
222 unsigned size;
223 unsigned flags;
224};
225
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700226/*
227 * Partition info, required to be published
228 * for fastboot
229 */
230struct getvar_partition_info {
231 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
232 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
233 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
234 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
235 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
236};
237
238/*
239 * Right now, we are publishing the info for only
240 * three partitions
241 */
242struct getvar_partition_info part_info[] =
243{
244 { "system" , "partition-size:", "partition-type:", "", "ext4" },
245 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
246 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
247};
248
249char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700250char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800251char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700252char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530253char panel_display_mode[MAX_RSP_SIZE];
lijuang65c5a822015-08-29 16:35:36 +0800254char battery_voltage[MAX_RSP_SIZE];
lijuangf16461c2015-08-03 17:09:34 +0800255char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700256
Greg Griscod2471ef2011-07-14 13:00:42 -0700257extern int emmc_recovery_init(void);
258
Kinson Chik0b1c8162011-08-31 16:31:57 -0700259#if NO_KEYPAD_DRIVER
260extern int fastboot_trigger(void);
261#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700262
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800263static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700264{
265 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700266#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800267 if (is_arm64)
268 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
269 else
270 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700271 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
272 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700273#endif
274}
275
Dima Zavin42168f22009-01-30 11:52:22 -0800276static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
277{
278 struct atag_ptbl_entry atag_ptn;
279
280 memcpy(atag_ptn.name, ptn->name, 16);
281 atag_ptn.name[15] = '\0';
282 atag_ptn.offset = ptn->start;
283 atag_ptn.size = ptn->length;
284 atag_ptn.flags = ptn->flags;
285 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
286 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
287}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800288
Neeti Desaie245d492012-06-01 12:52:13 -0700289unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800290{
David Ng183a7422009-12-07 14:55:21 -0800291 int cmdline_len = 0;
292 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800293 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700294 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800295 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300296 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700297 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700298 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200299 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700300#if VERIFIED_BOOT
301 uint32_t boot_state = boot_verify_get_state();
302#endif
303
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200304#ifdef MDTP_SUPPORT
305 mdtp_activated(&is_mdtp_activated);
306#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800307
Brian Swetland9c4c0752009-01-25 16:23:50 -0800308 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800309 cmdline_len = strlen(cmdline);
310 have_cmdline = 1;
311 }
312 if (target_is_emmc_boot()) {
313 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800314#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700315 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
316 ASSERT(boot_dev_buf);
317 platform_boot_dev_cmdline(boot_dev_buf);
318 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700319#endif
David Ng183a7422009-12-07 14:55:21 -0800320 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800321
322 cmdline_len += strlen(usb_sn_cmdline);
323 cmdline_len += strlen(sn_buf);
324
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700325#if VERIFIED_BOOT
326 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700327 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700328#endif
329
Pavel Nedev5614d222013-06-17 18:01:02 +0300330 if (boot_into_recovery && gpt_exists)
331 cmdline_len += strlen(secondary_gpt_enable);
332
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200333 if(is_mdtp_activated)
334 cmdline_len += strlen(mdtp_activated_flag);
335
Pavel Nedev328ac822013-04-05 15:25:11 +0300336 if (boot_into_ffbm) {
337 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700338 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800339 /* reduce kernel console messages to speed-up boot */
340 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800341 } else if (boot_reason_alarm) {
342 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800343 } else if ((target_build_variant_user() || device.charger_screen_enabled)
344 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700345 pause_at_bootup = 1;
346 cmdline_len += strlen(battchg_pause);
347 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800348
Shashank Mittalcd98d472011-08-02 14:29:24 -0700349 if(target_use_signed_kernel() && auth_kernel_img) {
350 cmdline_len += strlen(auth_kernel);
351 }
352
Joonwoo Park61112782013-10-02 19:50:39 -0700353 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
354 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530355 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700356 have_target_boot_params = 1;
357 cmdline_len += strlen(target_boot_params);
358 }
359
Ajay Dudanid04110c2011-01-17 23:55:07 -0800360 /* Determine correct androidboot.baseband to use */
361 switch(target_baseband())
362 {
363 case BASEBAND_APQ:
364 cmdline_len += strlen(baseband_apq);
365 break;
366
367 case BASEBAND_MSM:
368 cmdline_len += strlen(baseband_msm);
369 break;
370
371 case BASEBAND_CSFB:
372 cmdline_len += strlen(baseband_csfb);
373 break;
374
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800375 case BASEBAND_SVLTE2A:
376 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800377 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700378
379 case BASEBAND_MDM:
380 cmdline_len += strlen(baseband_mdm);
381 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700382
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800383 case BASEBAND_MDM2:
384 cmdline_len += strlen(baseband_mdm2);
385 break;
386
Amol Jadi5c61a952012-05-04 17:05:35 -0700387 case BASEBAND_SGLTE:
388 cmdline_len += strlen(baseband_sglte);
389 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530390
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800391 case BASEBAND_SGLTE2:
392 cmdline_len += strlen(baseband_sglte2);
393 break;
394
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530395 case BASEBAND_DSDA:
396 cmdline_len += strlen(baseband_dsda);
397 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800398
399 case BASEBAND_DSDA2:
400 cmdline_len += strlen(baseband_dsda2);
401 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800402 }
403
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800404 if (cmdline) {
405 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530406 target_display_panel_node(display_panel_buf,
407 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800408 strlen(display_panel_buf)) {
409 cmdline_len += strlen(display_panel_buf);
410 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700411 }
412
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800413 if (target_warm_boot()) {
414 warm_boot = true;
415 cmdline_len += strlen(warmboot_cmdline);
416 }
417
David Ng183a7422009-12-07 14:55:21 -0800418 if (cmdline_len > 0) {
419 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800420 unsigned char *dst;
421
422 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
423 ASSERT(cmdline_final != NULL);
424 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700425
Amol Jadi168b7712012-03-06 16:15:00 -0800426 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800427 if (have_cmdline) {
428 src = cmdline;
429 while ((*dst++ = *src++));
430 }
431 if (target_is_emmc_boot()) {
432 src = emmc_cmdline;
433 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700434 have_cmdline = 1;
435 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800436#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700437 src = boot_dev_buf;
438 if (have_cmdline) --dst;
439 while ((*dst++ = *src++));
440#endif
David Ngf773dde2010-07-26 19:55:08 -0700441 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800442
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700443#if VERIFIED_BOOT
444 src = verified_state;
445 if(have_cmdline) --dst;
446 have_cmdline = 1;
447 while ((*dst++ = *src++));
448 src = vbsn[boot_state].name;
449 if(have_cmdline) --dst;
450 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700451
452 src = verity_mode;
453 if(have_cmdline) --dst;
454 while ((*dst++ = *src++));
455 src = vbvm[device.verity_mode].name;
456 if(have_cmdline) -- dst;
457 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700458#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800459 src = usb_sn_cmdline;
460 if (have_cmdline) --dst;
461 have_cmdline = 1;
462 while ((*dst++ = *src++));
463 src = sn_buf;
464 if (have_cmdline) --dst;
465 have_cmdline = 1;
466 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800467 if (warm_boot) {
468 if (have_cmdline) --dst;
469 src = warmboot_cmdline;
470 while ((*dst++ = *src++));
471 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800472
Pavel Nedev5614d222013-06-17 18:01:02 +0300473 if (boot_into_recovery && gpt_exists) {
474 src = secondary_gpt_enable;
475 if (have_cmdline) --dst;
476 while ((*dst++ = *src++));
477 }
478
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200479 if (is_mdtp_activated) {
480 src = mdtp_activated_flag;
481 if (have_cmdline) --dst;
482 while ((*dst++ = *src++));
483 }
484
Pavel Nedev328ac822013-04-05 15:25:11 +0300485 if (boot_into_ffbm) {
486 src = androidboot_mode;
487 if (have_cmdline) --dst;
488 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700489 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300490 if (have_cmdline) --dst;
491 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800492 src = loglevel;
493 if (have_cmdline) --dst;
494 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800495 } else if (boot_reason_alarm) {
496 src = alarmboot_cmdline;
497 if (have_cmdline) --dst;
498 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300499 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700500 src = battchg_pause;
501 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800502 while ((*dst++ = *src++));
503 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800504
Shashank Mittalcd98d472011-08-02 14:29:24 -0700505 if(target_use_signed_kernel() && auth_kernel_img) {
506 src = auth_kernel;
507 if (have_cmdline) --dst;
508 while ((*dst++ = *src++));
509 }
510
Ajay Dudanid04110c2011-01-17 23:55:07 -0800511 switch(target_baseband())
512 {
513 case BASEBAND_APQ:
514 src = baseband_apq;
515 if (have_cmdline) --dst;
516 while ((*dst++ = *src++));
517 break;
518
519 case BASEBAND_MSM:
520 src = baseband_msm;
521 if (have_cmdline) --dst;
522 while ((*dst++ = *src++));
523 break;
524
525 case BASEBAND_CSFB:
526 src = baseband_csfb;
527 if (have_cmdline) --dst;
528 while ((*dst++ = *src++));
529 break;
530
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800531 case BASEBAND_SVLTE2A:
532 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800533 if (have_cmdline) --dst;
534 while ((*dst++ = *src++));
535 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700536
537 case BASEBAND_MDM:
538 src = baseband_mdm;
539 if (have_cmdline) --dst;
540 while ((*dst++ = *src++));
541 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700542
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800543 case BASEBAND_MDM2:
544 src = baseband_mdm2;
545 if (have_cmdline) --dst;
546 while ((*dst++ = *src++));
547 break;
548
Amol Jadi5c61a952012-05-04 17:05:35 -0700549 case BASEBAND_SGLTE:
550 src = baseband_sglte;
551 if (have_cmdline) --dst;
552 while ((*dst++ = *src++));
553 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530554
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800555 case BASEBAND_SGLTE2:
556 src = baseband_sglte2;
557 if (have_cmdline) --dst;
558 while ((*dst++ = *src++));
559 break;
560
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530561 case BASEBAND_DSDA:
562 src = baseband_dsda;
563 if (have_cmdline) --dst;
564 while ((*dst++ = *src++));
565 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800566
567 case BASEBAND_DSDA2:
568 src = baseband_dsda2;
569 if (have_cmdline) --dst;
570 while ((*dst++ = *src++));
571 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800572 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700573
574 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700575 src = display_panel_buf;
576 if (have_cmdline) --dst;
577 while ((*dst++ = *src++));
578 }
Joonwoo Park61112782013-10-02 19:50:39 -0700579
580 if (have_target_boot_params) {
581 if (have_cmdline) --dst;
582 src = target_boot_params;
583 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530584 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700585 }
Neeti Desaie245d492012-06-01 12:52:13 -0700586 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700587
588
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700589 if (boot_dev_buf)
590 free(boot_dev_buf);
591
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800592 if (cmdline_final)
593 dprintf(INFO, "cmdline: %s\n", cmdline_final);
594 else
595 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700596 return cmdline_final;
597}
598
599unsigned *atag_core(unsigned *ptr)
600{
601 /* CORE */
602 *ptr++ = 2;
603 *ptr++ = 0x54410001;
604
605 return ptr;
606
607}
608
609unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
610 unsigned ramdisk_size)
611{
612 if (ramdisk_size) {
613 *ptr++ = 4;
614 *ptr++ = 0x54420005;
615 *ptr++ = (unsigned)ramdisk;
616 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800617 }
618
Neeti Desaie245d492012-06-01 12:52:13 -0700619 return ptr;
620}
621
622unsigned *atag_ptable(unsigned **ptr_addr)
623{
624 int i;
625 struct ptable *ptable;
626
627 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700628 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
629 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700630 *(*ptr_addr)++ = 0x4d534d70;
631 for (i = 0; i < ptable->count; ++i)
632 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
633 }
634
635 return (*ptr_addr);
636}
637
638unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
639{
640 int cmdline_length = 0;
641 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700642 char *dest;
643
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800644 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700645 n = (cmdline_length + 4) & (~3);
646
647 *ptr++ = (n / 4) + 2;
648 *ptr++ = 0x54410009;
649 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800650 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700651 ptr += (n / 4);
652
653 return ptr;
654}
655
656unsigned *atag_end(unsigned *ptr)
657{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800658 /* END */
659 *ptr++ = 0;
660 *ptr++ = 0;
661
Neeti Desaie245d492012-06-01 12:52:13 -0700662 return ptr;
663}
664
665void generate_atags(unsigned *ptr, const char *cmdline,
666 void *ramdisk, unsigned ramdisk_size)
667{
668
669 ptr = atag_core(ptr);
670 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
671 ptr = target_atag_mem(ptr);
672
673 /* Skip NAND partition ATAGS for eMMC boot */
674 if (!target_is_emmc_boot()){
675 ptr = atag_ptable(&ptr);
676 }
677
678 ptr = atag_cmdline(ptr, cmdline);
679 ptr = atag_end(ptr);
680}
681
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700682typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700683void boot_linux(void *kernel, unsigned *tags,
684 const char *cmdline, unsigned machtype,
685 void *ramdisk, unsigned ramdisk_size)
686{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800687 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800688#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700689 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800690#endif
691
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700692 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800693 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800694 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800695
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530696 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700697
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800698 final_cmdline = update_cmdline((const char*)cmdline);
699
Neeti Desai17379b82012-06-04 18:42:53 -0700700#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800701 dprintf(INFO, "Updating device tree: start\n");
702
Neeti Desai17379b82012-06-04 18:42:53 -0700703 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530704 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700705 if(ret)
706 {
707 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
708 ASSERT(0);
709 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800710 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700711#else
Neeti Desaie245d492012-06-01 12:52:13 -0700712 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800713 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700714#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700715
Maria Yu52254c02014-07-04 16:14:54 +0800716 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700717
718#if VERIFIED_BOOT
719 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700720 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700721 {
722 dprintf(INFO, "Failed to write protect dev info\n");
723 ASSERT(0);
724 }
725#endif
726
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700727 /* Perform target specific cleanup */
728 target_uninit();
729
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800730 /* Turn off splash screen if enabled */
731#if DISPLAY_SPLASH_SCREEN
732 target_display_shutdown();
733#endif
734
735
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800736 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530737 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800738
739 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700740
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +0530741 /* Initialise wdog to catch early kernel crashes */
742#if WDOG_SUPPORT
743 msm_wdog_init();
744#endif
Amol Jadi4421e652011-06-16 15:00:48 -0700745 /* do any platform specific cleanup before kernel entry */
746 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700747
Brian Swetland9c4c0752009-01-25 16:23:50 -0800748 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700749
Amol Jadi504f9fe2012-08-16 13:56:48 -0700750#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800751 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700752#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700753 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800754
755 if (IS_ARM64(kptr))
756 /* Jump to a 64bit kernel */
757 scm_elexec_call((paddr_t)kernel, tags_phys);
758 else
759 /* Jump to a 32bit kernel */
760 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800761}
762
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700763/* Function to check if the memory address range falls within the aboot
764 * boundaries.
765 * start: Start of the memory region
766 * size: Size of the memory region
767 */
768int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
769{
770 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800771 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700772 return -1;
773
774 /* Check for memory overlap. */
775 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
776 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700777 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700778 return 0;
779 else
780 return -1;
781}
782
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800783#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800784
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800785BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800786#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800787BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800788#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800789
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700790static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
791{
792 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800793
794#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800795#if IMAGE_VERIF_ALGO_SHA1
796 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
797#else
798 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
799#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800800#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700801
802 /* Assume device is rooted at this time. */
803 device.is_tampered = 1;
804
805 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
806
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700807#if VERIFIED_BOOT
808 if(boot_into_recovery)
809 {
810 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530811 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700812 }
813 else
814 {
815 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530816 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700817 }
818 boot_verify_print_state();
819#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700820 ret = image_verify((unsigned char *)bootimg_addr,
821 (unsigned char *)(bootimg_addr + bootimg_size),
822 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800823 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700824#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700825 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
826
827 if (ret)
828 {
829 /* Authorized kernel */
830 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700831 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700832 }
833
Amit Blay4aa292f2015-04-28 21:55:59 +0300834#ifdef MDTP_SUPPORT
835 {
836 /* Verify MDTP lock.
837 * For boot & recovery partitions, use aboot's verification result.
838 */
839 mdtp_ext_partition_verification_t ext_partition;
840 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
841 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
842 ext_partition.page_size = 0; /* Not needed since already validated */
843 ext_partition.image_addr = 0; /* Not needed since already validated */
844 ext_partition.image_size = 0; /* Not needed since already validated */
845 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
846 mdtp_fwlock_verify_lock(&ext_partition);
847 }
848#endif /* MDTP_SUPPORT */
849
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700850#if USE_PCOM_SECBOOT
851 set_tamper_flag(device.is_tampered);
852#endif
853
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700854#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700855 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700856 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700857 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800858#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +0800859 display_bootverify_menu_thread(DISPLAY_MENU_RED);
lijuanga40d6302015-07-20 20:10:13 +0800860 wait_for_users_action();
861#else
862 dprintf(CRITICAL,
863 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
864 mdelay(5000);
865#endif
866
867 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700868 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800869#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +0800870 display_bootverify_menu_thread(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800871 wait_for_users_action();
872#else
873 dprintf(CRITICAL,
874 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
875 mdelay(5000);
876#endif
877 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700878 default:
lijuanga40d6302015-07-20 20:10:13 +0800879 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700880 }
881#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700882#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530883 if(device.is_tampered)
884 {
885 write_device_info_mmc(&device);
886 #ifdef TZ_TAMPER_FUSE
887 set_tamper_fuse_cmd();
888 #endif
889 #ifdef ASSERT_ON_TAMPER
890 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
891 ASSERT(0);
892 #endif
893 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700894#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700895}
896
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530897static bool check_format_bit()
898{
899 bool ret = false;
900 int index;
901 uint64_t offset;
902 struct boot_selection_info *in = NULL;
903 char *buf = NULL;
904
905 index = partition_get_index("bootselect");
906 if (index == INVALID_PTN)
907 {
908 dprintf(INFO, "Unable to locate /bootselect partition\n");
909 return ret;
910 }
911 offset = partition_get_offset(index);
912 if(!offset)
913 {
914 dprintf(INFO, "partition /bootselect doesn't exist\n");
915 return ret;
916 }
917 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
918 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530919 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530920 {
921 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
922 free(buf);
923 return ret;
924 }
925 in = (struct boot_selection_info *) buf;
926 if ((in->signature == BOOTSELECT_SIGNATURE) &&
927 (in->version == BOOTSELECT_VERSION)) {
928 if ((in->state_info & BOOTSELECT_FORMAT) &&
929 !(in->state_info & BOOTSELECT_FACTORY))
930 ret = true;
931 } else {
932 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
933 in->signature, in->version);
934 ASSERT(0);
935 }
936 free(buf);
937 return ret;
938}
939
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700940void boot_verifier_init()
941{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700942 uint32_t boot_state;
943 /* Check if device unlock */
944 if(device.is_unlocked)
945 {
946 boot_verify_send_event(DEV_UNLOCK);
947 boot_verify_print_state();
948 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
949 return;
950 }
951 else
952 {
953 boot_verify_send_event(BOOT_INIT);
954 }
955
956 /* Initialize keystore */
957 boot_state = boot_verify_keystore_init();
958 if(boot_state == YELLOW)
959 {
960 boot_verify_print_state();
961 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
962 }
963}
964
Shashank Mittal23b8f422010-04-16 19:27:21 -0700965int boot_linux_from_mmc(void)
966{
967 struct boot_img_hdr *hdr = (void*) buf;
968 struct boot_img_hdr *uhdr;
969 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700970 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700971 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700972 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700973
Shashank Mittalcd98d472011-08-02 14:29:24 -0700974 unsigned char *image_addr = 0;
975 unsigned kernel_actual;
976 unsigned ramdisk_actual;
977 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700978 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700979
Matthew Qin271927e2015-03-31 22:07:07 -0400980 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +0800981 unsigned int out_len = 0;
982 unsigned int out_avai_len = 0;
983 unsigned char *out_addr = NULL;
984 uint32_t dtb_offset = 0;
985 unsigned char *kernel_start_addr = NULL;
986 unsigned int kernel_size = 0;
987 int rc;
988
Neeti Desai465491e2012-07-31 12:53:35 -0700989#if DEVICE_TREE
990 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700991 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700992 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800993 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700994 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -0400995 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -0700996#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +0800997 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800998
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530999 if (check_format_bit())
1000 boot_into_recovery = 1;
1001
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001002 if (!boot_into_recovery) {
1003 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1004 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1005 if (rcode <= 0) {
1006 boot_into_ffbm = false;
1007 if (rcode < 0)
1008 dprintf(CRITICAL,"failed to get ffbm cookie");
1009 } else
1010 boot_into_ffbm = true;
1011 } else
1012 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001013 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1014 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1015 dprintf(INFO, "Unified boot method!\n");
1016 hdr = uhdr;
1017 goto unified_boot;
1018 }
Greg Griscod6250552011-06-29 14:40:23 -07001019 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001020 index = partition_get_index("boot");
1021 ptn = partition_get_offset(index);
1022 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001023 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1024 return -1;
1025 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001026 }
1027 else {
1028 index = partition_get_index("recovery");
1029 ptn = partition_get_offset(index);
1030 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001031 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1032 return -1;
1033 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001034 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001035 /* Set Lun for boot & recovery partitions */
1036 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001037
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301038 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001039 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1040 return -1;
1041 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001042
1043 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001044 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001045 return -1;
1046 }
1047
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001048 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301049
1050 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1051 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1052 return -1;
1053 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001054 page_size = hdr->page_size;
1055 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001056 }
1057
Matthew Qin49e51fa2015-02-09 10:40:45 +08001058 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1059 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001060
Matthew Qin49e51fa2015-02-09 10:40:45 +08001061 image_addr = (unsigned char *)target_get_scratch_address();
1062
1063#if DEVICE_TREE
1064 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1065 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1066#else
1067 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1068#endif
1069
1070#if VERIFIED_BOOT
1071 boot_verifier_init();
1072#endif
1073
1074 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
1075 {
1076 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1077 return -1;
1078 }
1079
Matthew Qinbb7923d2015-02-09 10:56:09 +08001080 /*
1081 * Update loading flow of bootimage to support compressed/uncompressed
1082 * bootimage on both 64bit and 32bit platform.
1083 * 1. Load bootimage from emmc partition onto DDR.
1084 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1085 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1086 * platform accordingly.
1087 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1088 */
1089
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001090 dprintf(INFO, "Loading (%s) image (%d): start\n",
1091 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001092 bs_set_timestamp(BS_KERNEL_LOAD_START);
1093
Matthew Qinbb7923d2015-02-09 10:56:09 +08001094 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001095 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1096 {
1097 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1098 return -1;
1099 }
1100
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001101 dprintf(INFO, "Loading (%s) image (%d): done\n",
1102 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1103
Matthew Qin49e51fa2015-02-09 10:40:45 +08001104 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1105
1106 /* Authenticate Kernel */
1107 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1108 (int) target_use_signed_kernel(),
1109 device.is_unlocked,
1110 device.is_tampered);
1111
1112 if(target_use_signed_kernel() && (!device.is_unlocked))
1113 {
1114 offset = imagesize_actual;
1115 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1116 {
1117 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1118 return -1;
1119 }
1120
1121 /* Read signature */
1122 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1123 {
1124 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1125 return -1;
1126 }
1127
1128 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
1129 } else {
1130 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1131 #ifdef TZ_SAVE_KERNEL_HASH
1132 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1133 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001134
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001135#if VERIFIED_BOOT
1136 if(boot_verify_get_state() == ORANGE)
1137 {
lijuanga40d6302015-07-20 20:10:13 +08001138#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +08001139 display_bootverify_menu_thread(DISPLAY_MENU_ORANGE);
lijuanga40d6302015-07-20 20:10:13 +08001140 wait_for_users_action();
1141#else
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001142 dprintf(CRITICAL,
lijuanga40d6302015-07-20 20:10:13 +08001143 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001144 mdelay(5000);
lijuanga40d6302015-07-20 20:10:13 +08001145#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001146 }
1147#endif
1148
Amit Blay4aa292f2015-04-28 21:55:59 +03001149#ifdef MDTP_SUPPORT
1150 {
1151 /* Verify MDTP lock.
1152 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1153 * since verification was skipped in aboot. The signature is not part of the loaded image.
1154 */
1155 mdtp_ext_partition_verification_t ext_partition;
1156 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1157 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1158 ext_partition.page_size = page_size;
1159 ext_partition.image_addr = (uint32)image_addr;
1160 ext_partition.image_size = imagesize_actual;
1161 ext_partition.sig_avail = FALSE;
1162 mdtp_fwlock_verify_lock(&ext_partition);
1163 }
1164#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001165 }
1166
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001167#if VERIFIED_BOOT
1168 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001169 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001170 ASSERT(0);
1171#endif
1172
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001173 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001174 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1175 * If not, continue booting.
1176 */
1177 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1178 {
1179 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1180 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001181 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001182 rc = decompress((unsigned char *)(image_addr + page_size),
1183 hdr->kernel_size, out_addr, out_avai_len,
1184 &dtb_offset, &out_len);
1185 if (rc)
1186 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001187 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001188 ASSERT(0);
1189 }
1190
Matthew Qin0b15b322015-05-19 05:20:54 -04001191 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001192 kptr = (struct kernel64_hdr *)out_addr;
1193 kernel_start_addr = out_addr;
1194 kernel_size = out_len;
1195 } else {
1196 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1197 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1198 kernel_size = hdr->kernel_size;
1199 }
1200
1201 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001202 * Update the kernel/ramdisk/tags address if the boot image header
1203 * has default values, these default values come from mkbootimg when
1204 * the boot image is flashed using fastboot flash:raw
1205 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001206 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001207
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001208 /* Get virtual addresses since the hdr saves physical addresses. */
1209 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1210 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1211 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001212
Matthew Qinbb7923d2015-02-09 10:56:09 +08001213 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001214 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001215 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001216 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1217 {
1218 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1219 return -1;
1220 }
1221
1222#ifndef DEVICE_TREE
1223 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1224 {
1225 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1226 return -1;
1227 }
1228#endif
1229
Matthew Qin49e51fa2015-02-09 10:40:45 +08001230 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001231 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001232 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001233
Matthew Qin49e51fa2015-02-09 10:40:45 +08001234 #if DEVICE_TREE
1235 if(hdr->dt_size) {
1236 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1237 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001238
Matthew Qin49e51fa2015-02-09 10:40:45 +08001239 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1240 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1241 return -1;
1242 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001243
Matthew Qin49e51fa2015-02-09 10:40:45 +08001244 /* Find index of device tree within device tree table */
1245 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1246 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1247 return -1;
1248 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001249
Matthew Qin271927e2015-03-31 22:07:07 -04001250 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1251 {
1252 unsigned int compressed_size = 0;
1253 out_addr += out_len;
1254 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001255 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001256 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1257 dt_entry.size, out_addr, out_avai_len,
1258 &compressed_size, &dtb_size);
1259 if (rc)
1260 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001261 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001262 ASSERT(0);
1263 }
1264
Matthew Qin0b15b322015-05-19 05:20:54 -04001265 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001266 best_match_dt_addr = out_addr;
1267 } else {
1268 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1269 dtb_size = dt_entry.size;
1270 }
1271
Matthew Qin49e51fa2015-02-09 10:40:45 +08001272 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001273 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001274 {
1275 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1276 return -1;
1277 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001278
Matthew Qin271927e2015-03-31 22:07:07 -04001279 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001280 } else {
1281 /* Validate the tags_addr */
1282 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001283 {
1284 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1285 return -1;
1286 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001287 /*
1288 * If appended dev tree is found, update the atags with
1289 * memory address to the DTB appended location on RAM.
1290 * Else update with the atags address in the kernel header
1291 */
1292 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001293 dtb = dev_tree_appended((void*)(image_addr + page_size),
1294 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001295 (void *)hdr->tags_addr);
1296 if (!dtb) {
1297 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001298 return -1;
1299 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001300 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001301 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001302
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001303 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1304 target_load_ssd_keystore();
1305
Shashank Mittal23b8f422010-04-16 19:27:21 -07001306unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001307
Dima Zavin77e41f32013-03-06 16:10:43 -08001308 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001309 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001310 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1311
1312 return 0;
1313}
1314
Dima Zavin214cc642009-01-26 11:16:21 -08001315int boot_linux_from_flash(void)
1316{
1317 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001318 struct ptentry *ptn;
1319 struct ptable *ptable;
1320 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001321
Shashank Mittalcd98d472011-08-02 14:29:24 -07001322 unsigned char *image_addr = 0;
1323 unsigned kernel_actual;
1324 unsigned ramdisk_actual;
1325 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001326 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001327
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001328#if DEVICE_TREE
1329 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001330 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001331 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001332 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001333#endif
1334
David Ng183a7422009-12-07 14:55:21 -08001335 if (target_is_emmc_boot()) {
1336 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1337 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1338 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1339 return -1;
1340 }
1341 goto continue_boot;
1342 }
1343
Dima Zavin214cc642009-01-26 11:16:21 -08001344 ptable = flash_get_ptable();
1345 if (ptable == NULL) {
1346 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1347 return -1;
1348 }
1349
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001350 if(!boot_into_recovery)
1351 {
1352 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001353
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001354 if (ptn == NULL) {
1355 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1356 return -1;
1357 }
1358 }
1359 else
1360 {
1361 ptn = ptable_find(ptable, "recovery");
1362 if (ptn == NULL) {
1363 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1364 return -1;
1365 }
Dima Zavin214cc642009-01-26 11:16:21 -08001366 }
1367
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001368 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001369 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1370 return -1;
1371 }
Dima Zavin214cc642009-01-26 11:16:21 -08001372
1373 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001374 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001375 return -1;
1376 }
1377
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001378 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001379 dprintf(CRITICAL, "ERROR: Invalid boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001380 return -1;
1381 }
1382
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001383 /*
1384 * Update the kernel/ramdisk/tags address if the boot image header
1385 * has default values, these default values come from mkbootimg when
1386 * the boot image is flashed using fastboot flash:raw
1387 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001388 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001389
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001390 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001391 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1392 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1393 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1394
1395 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1396 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1397
1398 /* Check if the addresses in the header are valid. */
1399 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1400 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1401 {
1402 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1403 return -1;
1404 }
1405
1406#ifndef DEVICE_TREE
1407 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1408 {
1409 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1410 return -1;
1411 }
1412#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001413
Shashank Mittalcd98d472011-08-02 14:29:24 -07001414 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001415 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001416 {
1417 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001418 offset = 0;
1419
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001420#if DEVICE_TREE
1421 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1422 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001423
1424 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1425 {
1426 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1427 return -1;
1428 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001429#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001430 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001431#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001432
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001433 dprintf(INFO, "Loading (%s) image (%d): start\n",
1434 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001435 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001436
Shashank Mittalcd98d472011-08-02 14:29:24 -07001437 /* Read image without signature */
1438 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1439 {
1440 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1441 return -1;
1442 }
Dima Zavin214cc642009-01-26 11:16:21 -08001443
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001444 dprintf(INFO, "Loading (%s) image (%d): done\n",
1445 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001446 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001447
Shashank Mittalcd98d472011-08-02 14:29:24 -07001448 offset = imagesize_actual;
1449 /* Read signature */
1450 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1451 {
1452 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001453 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001454 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001455
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301456 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001457
1458 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001459 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1460 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001461#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001462 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001463 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001464 {
1465 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1466 return -1;
1467 }
1468
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001469 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1470#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001471
1472 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001473 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001474 {
1475 write_device_info_flash(&device);
1476 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301477#if USE_PCOM_SECBOOT
1478 set_tamper_flag(device.is_tampered);
1479#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001480 }
1481 else
1482 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001483 offset = page_size;
1484
Amol Jadib6be5c12012-11-14 13:39:51 -08001485 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1486 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1487 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1488
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001489 dprintf(INFO, "Loading (%s) image (%d): start\n",
1490 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1491
Amol Jadi492d5a52013-03-15 16:12:34 -07001492 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001493
1494 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001495 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1496 return -1;
1497 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001498 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001499
Amol Jadib6be5c12012-11-14 13:39:51 -08001500 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001501 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1502 return -1;
1503 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001504 offset += ramdisk_actual;
1505
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001506 dprintf(INFO, "Loading (%s) image (%d): done\n",
1507 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1508
Amol Jadi492d5a52013-03-15 16:12:34 -07001509 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001510
1511 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001512 offset += second_actual;
1513 /* Second image loading not implemented. */
1514 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001515 }
1516
1517#if DEVICE_TREE
1518 if(hdr->dt_size != 0) {
1519
1520 /* Read the device tree table into buffer */
1521 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1522 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1523 return -1;
1524 }
1525
1526 table = (struct dt_table*) dt_buf;
1527
Deepa Dinamani19648b42013-09-05 17:05:55 -07001528 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001529 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1530 return -1;
1531 }
1532
Deepa Dinamani19648b42013-09-05 17:05:55 -07001533 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1534 if (!table)
1535 return -1;
1536
1537 /* Read the entire device tree table into buffer */
1538 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1539 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1540 return -1;
1541 }
1542
1543
Joel Kingaa335dc2013-06-03 16:11:08 -07001544 /* Find index of device tree within device tree table */
1545 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001546 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1547 return -1;
1548 }
1549
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001550 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001551 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001552 {
1553 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1554 return -1;
1555 }
1556
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001557 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001558 if(flash_read(ptn, offset + dt_entry.offset,
1559 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001560 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1561 return -1;
1562 }
1563 }
1564#endif
1565
Shashank Mittalcd98d472011-08-02 14:29:24 -07001566 }
David Ng183a7422009-12-07 14:55:21 -08001567continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001568
Dima Zavin214cc642009-01-26 11:16:21 -08001569 /* TODO: create/pass atags to kernel */
1570
Ajay Dudanie28a6072011-07-01 13:59:46 -07001571 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001572 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001573 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1574
1575 return 0;
1576}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001577
Shashank Mittal162244e2011-08-08 19:01:25 -07001578void write_device_info_mmc(device_info *dev)
1579{
Shashank Mittal162244e2011-08-08 19:01:25 -07001580 unsigned long long ptn = 0;
1581 unsigned long long size;
1582 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001583 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001584 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001585 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001586
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001587 if (devinfo_present)
1588 index = partition_get_index("devinfo");
1589 else
1590 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001591
Shashank Mittal162244e2011-08-08 19:01:25 -07001592 ptn = partition_get_offset(index);
1593 if(ptn == 0)
1594 {
1595 return;
1596 }
1597
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001598 lun = partition_get_lun(index);
1599 mmc_set_lun(lun);
1600
Shashank Mittal162244e2011-08-08 19:01:25 -07001601 size = partition_get_size(index);
1602
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001603 blocksize = mmc_get_device_blocksize();
1604
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001605 if (devinfo_present)
1606 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1607 else
1608 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1609 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001610 {
1611 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001612 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001613 }
1614}
1615
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001616void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001617{
Shashank Mittal162244e2011-08-08 19:01:25 -07001618 unsigned long long ptn = 0;
1619 unsigned long long size;
1620 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001621 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001622 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001623
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001624 if ((index = partition_get_index("devinfo")) < 0)
1625 {
1626 devinfo_present = false;
1627 index = partition_get_index("aboot");
1628 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001629
Shashank Mittal162244e2011-08-08 19:01:25 -07001630 ptn = partition_get_offset(index);
1631 if(ptn == 0)
1632 {
1633 return;
1634 }
1635
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001636 mmc_set_lun(partition_get_lun(index));
1637
Shashank Mittal162244e2011-08-08 19:01:25 -07001638 size = partition_get_size(index);
1639
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001640 blocksize = mmc_get_device_blocksize();
1641
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001642 if (devinfo_present)
1643 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1644 else
1645 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1646 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001647 {
1648 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001649 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001650 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001651}
1652
1653void write_device_info_flash(device_info *dev)
1654{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001655 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001656 struct ptentry *ptn;
1657 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001658 if(info == NULL)
1659 {
1660 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1661 ASSERT(0);
1662 }
1663 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001664 ptable = flash_get_ptable();
1665 if (ptable == NULL)
1666 {
1667 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1668 return;
1669 }
1670
1671 ptn = ptable_find(ptable, "devinfo");
1672 if (ptn == NULL)
1673 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001674 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001675 return;
1676 }
1677
1678 memcpy(info, dev, sizeof(device_info));
1679
1680 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1681 {
1682 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1683 return;
1684 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001685 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001686}
1687
vijay kumarc65876c2015-04-24 13:29:16 +05301688static int read_allow_oem_unlock(device_info *dev)
1689{
vijay kumarc65876c2015-04-24 13:29:16 +05301690 unsigned offset;
1691 int index;
1692 unsigned long long ptn;
1693 unsigned long long ptn_size;
1694 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001695 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301696
vijay kumarca2e6812015-07-08 20:28:25 +05301697 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301698 if (index == INVALID_PTN)
1699 {
vijay kumarca2e6812015-07-08 20:28:25 +05301700 index = partition_get_index(frp_ptns[1]);
1701 if (index == INVALID_PTN)
1702 {
1703 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1704 return -1;
1705 }
vijay kumarc65876c2015-04-24 13:29:16 +05301706 }
1707
1708 ptn = partition_get_offset(index);
1709 ptn_size = partition_get_size(index);
1710 offset = ptn_size - blocksize;
1711
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001712 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301713 {
1714 dprintf(CRITICAL, "Reading MMC failed\n");
1715 return -1;
1716 }
1717
1718 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1719 is_allow_unlock = buf[blocksize-1] & 0x01;
1720 return 0;
1721}
1722
1723static int write_allow_oem_unlock(bool allow_unlock)
1724{
vijay kumarc65876c2015-04-24 13:29:16 +05301725 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301726 int index;
1727 unsigned long long ptn;
1728 unsigned long long ptn_size;
1729 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001730 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301731
vijay kumarca2e6812015-07-08 20:28:25 +05301732 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301733 if (index == INVALID_PTN)
1734 {
vijay kumarca2e6812015-07-08 20:28:25 +05301735 index = partition_get_index(frp_ptns[1]);
1736 if (index == INVALID_PTN)
1737 {
1738 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1739 return -1;
1740 }
vijay kumarc65876c2015-04-24 13:29:16 +05301741 }
1742
1743 ptn = partition_get_offset(index);
1744 ptn_size = partition_get_size(index);
1745 offset = ptn_size - blocksize;
1746
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001747 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301748 {
1749 dprintf(CRITICAL, "Reading MMC failed\n");
1750 return -1;
1751 }
1752
1753 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1754 buf[blocksize-1] = allow_unlock;
1755 if (mmc_write(ptn + offset, blocksize, buf))
1756 {
1757 dprintf(CRITICAL, "Writing MMC failed\n");
1758 return -1;
1759 }
1760
1761 return 0;
1762}
1763
Shashank Mittal162244e2011-08-08 19:01:25 -07001764void read_device_info_flash(device_info *dev)
1765{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001766 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001767 struct ptentry *ptn;
1768 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001769 if(info == NULL)
1770 {
1771 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1772 ASSERT(0);
1773 }
1774 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001775 ptable = flash_get_ptable();
1776 if (ptable == NULL)
1777 {
1778 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1779 return;
1780 }
1781
1782 ptn = ptable_find(ptable, "devinfo");
1783 if (ptn == NULL)
1784 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001785 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001786 return;
1787 }
1788
1789 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1790 {
1791 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1792 return;
1793 }
1794
1795 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1796 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001797 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1798 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001799 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001800 write_device_info_flash(info);
1801 }
1802 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001803 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001804}
1805
1806void write_device_info(device_info *dev)
1807{
1808 if(target_is_emmc_boot())
1809 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001810 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1811 if(info == NULL)
1812 {
1813 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1814 ASSERT(0);
1815 }
1816 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001817 memcpy(info, dev, sizeof(struct device_info));
1818
1819#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001820 if (is_secure_boot_enable()) {
1821 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1822 ASSERT(0);
1823 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001824 else
1825 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001826#else
1827 write_device_info_mmc(info);
1828#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001829 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001830 }
1831 else
1832 {
1833 write_device_info_flash(dev);
1834 }
1835}
1836
1837void read_device_info(device_info *dev)
1838{
1839 if(target_is_emmc_boot())
1840 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001841 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1842 if(info == NULL)
1843 {
1844 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1845 ASSERT(0);
1846 }
1847 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001848
1849#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001850 if (is_secure_boot_enable()) {
1851 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1852 ASSERT(0);
1853 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001854 else
1855 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001856#else
1857 read_device_info_mmc(info);
1858#endif
1859
1860 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1861 {
1862 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08001863 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001864 info->is_unlocked = 0;
lijuang511a2b52015-08-14 20:50:51 +08001865 info->is_unlock_critical = 0;
1866 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001867 info->is_unlocked = 1;
lijuang511a2b52015-08-14 20:50:51 +08001868 info->is_unlock_critical = 1;
1869 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001870 info->is_tampered = 0;
1871 info->charger_screen_enabled = 0;
Sridhar Parasuram287e5062015-08-24 16:17:22 -07001872 info->verity_mode = 1; //enforcing by default
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001873 write_device_info(info);
1874 }
1875 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001876 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001877 }
1878 else
1879 {
1880 read_device_info_flash(dev);
1881 }
1882}
1883
1884void reset_device_info()
1885{
1886 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001887 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001888 write_device_info(&device);
1889}
1890
1891void set_device_root()
1892{
1893 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001894 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001895 write_device_info(&device);
1896}
1897
lijuang4ece1e72015-08-14 21:02:36 +08001898/* set device unlock value
1899 * Must check FRP before call this function
1900 * Need to wipe data when unlock status changed
1901 * type 0: oem unlock
1902 * type 1: unlock critical
1903 * status 0: unlock as false
1904 * status 1: lock as true
1905 */
1906void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08001907{
lijuang4ece1e72015-08-14 21:02:36 +08001908 if (type == UNLOCK)
1909 device.is_unlocked = status;
1910 else if (type == UNLOCK_CRITICAL)
1911 device.is_unlock_critical = status;
1912
1913 write_device_info(&device);
1914}
1915
1916static void set_device_unlock(int type, bool status)
1917{
1918 int is_unlocked = -1;
1919 char response[MAX_RSP_SIZE];
1920
1921 /* check device unlock status if it is as expected */
1922 if (type == UNLOCK)
1923 is_unlocked = device.is_unlocked;
1924 else if (type == UNLOCK_CRITICAL)
1925 is_unlocked = device.is_unlock_critical;
1926
1927 if (is_unlocked == status) {
1928 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
1929 fastboot_info(response);
1930 fastboot_okay("");
1931 return;
1932 }
1933
1934 /* status is true, it means to unlock device */
1935 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08001936 if(!is_allow_unlock) {
1937 fastboot_fail("oem unlock is not allowed");
1938 return;
1939 }
1940
lijuang4ece1e72015-08-14 21:02:36 +08001941#if FBCON_DISPLAY_MSG
1942 display_unlock_menu_thread(type);
1943 fastboot_okay("");
1944 return;
1945#else
1946 if (type == UNLOCK) {
1947 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
1948 return;
1949 }
1950#endif
lijuang21f12f52015-08-22 16:22:19 +08001951 }
lijuang4ece1e72015-08-14 21:02:36 +08001952
1953 set_device_unlock_value(type, status);
1954
1955 /* wipe data */
1956 struct recovery_message msg;
1957
1958 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
1959 write_misc(0, &msg, sizeof(msg));
1960
1961 fastboot_okay("");
1962 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08001963}
1964
lijuang511a2b52015-08-14 20:50:51 +08001965static bool critical_flash_allowed(const char * entry)
1966{
1967 uint32_t i = 0;
1968 if (entry == NULL)
1969 return false;
1970
1971 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
1972 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
1973 return true;
1974 }
1975 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04001976}
1977
1978#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07001979int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
1980{
1981 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07001982 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07001983 struct dt_table *table;
1984 struct dt_entry dt_entry;
1985 uint32_t dt_hdr_size;
1986 unsigned int compressed_size = 0;
1987 unsigned int dtb_size = 0;
1988 unsigned int out_avai_len = 0;
1989 unsigned char *out_addr = NULL;
1990 unsigned char *best_match_dt_addr = NULL;
1991 int rc;
1992
1993 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1994
1995 if(hdr->dt_size != 0) {
1996 /* add kernel offset */
1997 dt_image_offset += page_size;
1998 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001999 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002000
Deepa Dinamani19648b42013-09-05 17:05:55 -07002001 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002002 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2003 dt_image_offset += n;
2004
Joel Kingaa335dc2013-06-03 16:11:08 -07002005 /* add second offset */
2006 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002007 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2008 dt_image_offset += n;
2009 }
2010
Matthew Qin271927e2015-03-31 22:07:07 -04002011 /* offset now point to start of dt.img */
2012 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2013
2014 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2015 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002016 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002017 }
2018 /* Find index of device tree within device tree table */
2019 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2020 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2021 return -1;
Matthew Qin0b15b322015-05-19 05:20:54 -04002022 }
Matthew Qin271927e2015-03-31 22:07:07 -04002023
2024 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2025 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
Matthew Qin0b15b322015-05-19 05:20:54 -04002026 {
Matthew Qin271927e2015-03-31 22:07:07 -04002027 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2028 out_avai_len = target_get_max_flash_size() - scratch_offset;
2029 dprintf(INFO, "decompressing dtb: start\n");
2030 rc = decompress(best_match_dt_addr,
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002031 dt_entry.size, out_addr, out_avai_len,
Matthew Qin271927e2015-03-31 22:07:07 -04002032 &compressed_size, &dtb_size);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002033 if (rc)
2034 {
2035 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
2036 ASSERT(0);
2037 }
Amol Jadicb524072012-08-09 16:40:18 -07002038
Matthew Qin271927e2015-03-31 22:07:07 -04002039 dprintf(INFO, "decompressing dtb: done\n");
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002040 best_match_dt_addr = out_addr;
2041 } else {
Amol Jadicb524072012-08-09 16:40:18 -07002042 dtb_size = dt_entry.size;
2043 }
2044 /* Validate and Read device device tree in the "tags_add */
2045 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
2046 {
2047 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002048 return -1;
2049 }
Amit Blay8a510302015-08-17 09:20:01 +03002050
2051 /* Read device device tree in the "tags_add */
2052 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002053 } else
2054 return -1;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002055
2056 /* Everything looks fine. Return success. */
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002057 return 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002058}
2059#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002060
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002061void cmd_boot(const char *arg, void *data, unsigned sz)
2062{
Matthew Qinbb7923d2015-02-09 10:56:09 +08002063#ifdef MDTP_SUPPORT
2064 static bool is_mdtp_activated = 0;
2065#endif /* MDTP_SUPPORT */
2066 unsigned kernel_actual;
2067 unsigned ramdisk_actual;
2068 uint32_t image_actual;
Matthew Qin271927e2015-03-31 22:07:07 -04002069 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002070 uint32_t sig_actual = SIGNATURE_SIZE;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002071 struct boot_img_hdr *hdr = NULL;
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002072 struct kernel64_hdr *kptr = NULL;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002073 char *ptr = ((char*) data);
2074 int ret = 0;
2075 uint8_t dtb_copied = 0;
2076 unsigned int out_len = 0;
2077 unsigned int out_avai_len = 0;
2078 unsigned char *out_addr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002079 uint32_t dtb_offset = 0;
2080 unsigned char *kernel_start_addr = NULL;
2081 unsigned int kernel_size = 0;
2082 unsigned int scratch_offset = 0;
2083
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002084#if VERIFIED_BOOT
Brian Swetland9c4c0752009-01-25 16:23:50 -08002085 if(target_build_variant_user() && !device.is_unlocked)
2086 {
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002087 fastboot_fail("unlock device to use this command");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002088 return;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002089 }
2090#endif
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002091
2092 if (sz < sizeof(hdr)) {
2093 fastboot_fail("invalid bootimage header");
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002094 return;
2095 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002096
2097 hdr = (struct boot_img_hdr *)data;
2098
2099 /* ensure commandline is terminated */
2100 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2101
2102 if(target_is_emmc_boot() && hdr->page_size) {
2103 page_size = hdr->page_size;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002104 page_mask = page_size - 1;
2105 }
2106
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002107 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2108 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002109#if DEVICE_TREE
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002110 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2111#endif
2112
2113 image_actual = ADD_OF(page_size, kernel_actual);
2114 image_actual = ADD_OF(image_actual, ramdisk_actual);
2115 image_actual = ADD_OF(image_actual, dt_actual);
2116
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002117 if (target_use_signed_kernel() && (!device.is_unlocked))
2118 image_actual = ADD_OF(image_actual, sig_actual);
2119
2120 /* sz should have atleast raw boot image */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002121 if (image_actual > sz) {
Amit Blay4aa292f2015-04-28 21:55:59 +03002122 fastboot_fail("bootimage: incomplete or not signed");
2123 return;
2124 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002125
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002126 // Initialize boot state before trying to verify boot.img
2127#if VERIFIED_BOOT
2128 boot_verifier_init();
2129#endif
2130
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002131 /* Verify the boot image
2132 * device & page_size are initialized in aboot_init
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002133 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002134 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002135 /* Pass size excluding signature size, otherwise we would try to
2136 * access signature beyond its length
2137 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002138 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002139 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002140#ifdef MDTP_SUPPORT
2141 else
2142 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002143 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002144 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002145
2146 if (!is_mdtp_activated) {
2147 ext_partition.partition = MDTP_PARTITION_NONE;
2148 mdtp_fwlock_verify_lock(&ext_partition);
2149 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002150 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002151
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002152 mdtp_activated(&is_mdtp_activated);
2153 if(is_mdtp_activated){
2154 dprintf(CRITICAL, "fastboot boot command is not available.\n");
2155 return;
2156 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002157#endif /* MDTP_SUPPORT */
2158
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002159#if VERIFIED_BOOT
2160 // send root of trust
2161 if(!send_rot_command((uint32_t)device.is_unlocked))
2162 ASSERT(0);
2163#endif
2164
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002165 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002166 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2167 * If not, continue booting.
2168 */
2169 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2170 {
2171 out_addr = (unsigned char *)target_get_scratch_address();
2172 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2173 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002174 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002175 ret = decompress((unsigned char *)(ptr + page_size),
2176 hdr->kernel_size, out_addr, out_avai_len,
2177 &dtb_offset, &out_len);
2178 if (ret)
2179 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002180 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002181 ASSERT(0);
2182 }
2183
Matthew Qin0b15b322015-05-19 05:20:54 -04002184 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002185 kptr = (struct kernel64_hdr *)out_addr;
2186 kernel_start_addr = out_addr;
2187 kernel_size = out_len;
2188 } else {
2189 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2190 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2191 kernel_size = hdr->kernel_size;
2192 }
2193
2194 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002195 * Update the kernel/ramdisk/tags address if the boot image header
2196 * has default values, these default values come from mkbootimg when
2197 * the boot image is flashed using fastboot flash:raw
2198 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002199 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002200
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002201 /* Get virtual addresses since the hdr saves physical addresses. */
2202 hdr->kernel_addr = VA(hdr->kernel_addr);
2203 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2204 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002205
Matthew Qinbb7923d2015-02-09 10:56:09 +08002206 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002207 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002208 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002209 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2210 {
2211 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002212 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002213 }
2214
Amol Jadicb524072012-08-09 16:40:18 -07002215#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002216 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002217 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002218 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002219
2220 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002221#else
2222 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2223 {
2224 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002225 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002226 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002227#endif
2228
2229 /* Load ramdisk & kernel */
2230 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002231 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002232
2233#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002234 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2235 {
2236 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2237 return;
2238 }
2239
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002240 /*
2241 * If dtb is not found look for appended DTB in the kernel.
2242 * If appended dev tree is found, update the atags with
2243 * memory address to the DTB appended location on RAM.
2244 * Else update with the atags address in the kernel header
2245 */
2246 if (!dtb_copied) {
2247 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002248 dtb = dev_tree_appended((void*)(ptr + page_size),
2249 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002250 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002251 if (!dtb) {
2252 fastboot_fail("dtb not found");
2253 return;
2254 }
Amol Jadicb524072012-08-09 16:40:18 -07002255 }
2256#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002257
2258 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002259 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002260
Dima Zavin77e41f32013-03-06 16:10:43 -08002261 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002262 (const char*) hdr->cmdline, board_machtype(),
2263 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002264}
2265
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002266void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002267{
2268 struct ptentry *ptn;
2269 struct ptable *ptable;
2270
2271 ptable = flash_get_ptable();
2272 if (ptable == NULL) {
2273 fastboot_fail("partition table doesn't exist");
2274 return;
2275 }
2276
2277 ptn = ptable_find(ptable, arg);
2278 if (ptn == NULL) {
2279 fastboot_fail("unknown partition name");
2280 return;
2281 }
2282
2283 if (flash_erase(ptn)) {
2284 fastboot_fail("failed to erase partition");
2285 return;
2286 }
2287 fastboot_okay("");
2288}
2289
Bikas Gurungd48bd242010-09-04 19:54:32 -07002290
2291void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2292{
2293 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002294 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002295 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002296 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002297
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002298#if VERIFIED_BOOT
2299 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2300 {
2301 if(!device.is_unlocked)
2302 {
2303 fastboot_fail("unlock device to erase keystore");
2304 return;
2305 }
2306 }
2307#endif
2308
Kinson Chikf1a43512011-07-14 11:28:39 -07002309 index = partition_get_index(arg);
2310 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002311 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002312
Kinson Chikf1a43512011-07-14 11:28:39 -07002313 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002314 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002315 return;
2316 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002317
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002318 lun = partition_get_lun(index);
2319 mmc_set_lun(lun);
2320
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002321 if (platform_boot_dev_isemmc())
2322 {
2323 if (mmc_erase_card(ptn, size)) {
2324 fastboot_fail("failed to erase partition\n");
2325 return;
2326 }
2327 } else {
2328 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2329 size = partition_get_size(index);
2330 if (size > DEFAULT_ERASE_SIZE)
2331 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002332
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002333 /* Simple inefficient version of erase. Just writing
2334 0 in first several blocks */
2335 if (mmc_write(ptn , size, (unsigned int *)out)) {
2336 fastboot_fail("failed to erase partition");
2337 return;
2338 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002339 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002340#if VERIFIED_BOOT
2341 if(!(strncmp(arg, "userdata", 8)))
2342 if(send_delete_keys_to_tz())
2343 ASSERT(0);
2344#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002345 fastboot_okay("");
2346}
2347
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002348void cmd_erase(const char *arg, void *data, unsigned sz)
2349{
lijuang65c5a822015-08-29 16:35:36 +08002350#if CHECK_BAT_VOLTAGE
2351 if (!target_battery_soc_ok()) {
2352 fastboot_fail("Warning: battery's capacity is very low\n");
2353 return;
2354 }
2355#endif
2356
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002357#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002358 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002359 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002360 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002361 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002362 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002363 return;
2364 }
2365 }
2366#endif
2367
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002368 if(target_is_emmc_boot())
2369 cmd_erase_mmc(arg, data, sz);
2370 else
2371 cmd_erase_nand(arg, data, sz);
2372}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002373
Channagoud Kadabiad259832015-05-29 11:14:17 -07002374static uint32_t aboot_get_secret_key()
2375{
2376 /* 0 is invalid secret key, update this implementation to return
2377 * device specific unique secret key
2378 */
2379 return 0;
2380}
2381
Ajay Dudani5c761132011-04-07 20:19:04 -07002382void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002383{
2384 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002385 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002386 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002387 char *token = NULL;
2388 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002389 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002390 uint8_t lun = 0;
2391 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002392
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002393 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002394 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002395 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002396 if(token)
2397 {
2398 lun = atoi(token);
2399 mmc_set_lun(lun);
2400 lun_set = true;
2401 }
2402
Mao Jinlong226f33a2014-07-04 17:24:10 +08002403 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002404 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002405 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2406 {
2407 if (!aboot_frp_unlock(pname, data, sz))
2408 {
2409 fastboot_info("FRP unlock successful");
2410 fastboot_okay("");
2411 }
2412 else
2413 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2414
2415 return;
2416 }
2417
Mao Jinlong226f33a2014-07-04 17:24:10 +08002418 if (!strcmp(pname, "partition"))
2419 {
2420 dprintf(INFO, "Attempt to write partition image.\n");
2421 if (write_partition(sz, (unsigned char *) data)) {
2422 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002423 return;
2424 }
2425 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002426 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002427 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002428#if VERIFIED_BOOT
2429 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2430 {
2431 if(!device.is_unlocked)
2432 {
2433 fastboot_fail("unlock device to flash keystore");
2434 return;
2435 }
2436 if(!boot_verify_validate_keystore((unsigned char *)data))
2437 {
2438 fastboot_fail("image is not a keystore file");
2439 return;
2440 }
2441 }
2442#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002443 index = partition_get_index(pname);
2444 ptn = partition_get_offset(index);
2445 if(ptn == 0) {
2446 fastboot_fail("partition table doesn't exist");
2447 return;
2448 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002449
Mao Jinlong226f33a2014-07-04 17:24:10 +08002450 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2451 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2452 fastboot_fail("image is not a boot image");
2453 return;
2454 }
2455 }
2456
2457 if(!lun_set)
2458 {
2459 lun = partition_get_lun(index);
2460 mmc_set_lun(lun);
2461 }
2462
2463 size = partition_get_size(index);
2464 if (ROUND_TO_PAGE(sz,511) > size) {
2465 fastboot_fail("size too large");
2466 return;
2467 }
2468 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2469 fastboot_fail("flash write failure");
2470 return;
2471 }
Greg Grisco6e754772011-06-23 12:19:39 -07002472 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002473 }
2474 fastboot_okay("");
2475 return;
2476}
2477
Ajay Dudanide984792015-03-02 09:57:41 -08002478void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2479{
2480 int i, images;
2481 meta_header_t *meta_header;
2482 img_header_entry_t *img_header_entry;
2483
2484 meta_header = (meta_header_t*) data;
2485 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2486
2487 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2488
2489 for (i=0; i<images; i++) {
2490
2491 if((img_header_entry[i].ptn_name == NULL) ||
2492 (img_header_entry[i].start_offset == 0) ||
2493 (img_header_entry[i].size == 0))
2494 break;
2495
2496 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2497 (void *) data + img_header_entry[i].start_offset,
2498 img_header_entry[i].size);
2499 }
2500
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002501 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2502 {
2503 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2504 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2505 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2506 }
2507 else
2508 {
2509 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2510 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2511 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2512 }
2513
2514 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002515 fastboot_okay("");
2516 return;
2517}
2518
Ajay Dudani5c761132011-04-07 20:19:04 -07002519void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2520{
2521 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002522 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002523 uint32_t *fill_buf = NULL;
2524 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002525 sparse_header_t *sparse_header;
2526 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002527 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002528 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302529 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002530 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302531 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002532 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302533 /*End of the sparse image address*/
2534 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002535
Kinson Chikf1a43512011-07-14 11:28:39 -07002536 index = partition_get_index(arg);
2537 ptn = partition_get_offset(index);
2538 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002539 fastboot_fail("partition table doesn't exist");
2540 return;
2541 }
2542
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302543 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302544
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002545 lun = partition_get_lun(index);
2546 mmc_set_lun(lun);
2547
vijay kumar800255e2015-04-24 20:26:19 +05302548 if (sz < sizeof(sparse_header_t)) {
2549 fastboot_fail("size too low");
2550 return;
2551 }
2552
Ajay Dudani5c761132011-04-07 20:19:04 -07002553 /* Read and skip over sparse image header */
2554 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302555
vijay kumar1321f342015-03-27 12:13:42 +05302556 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002557 fastboot_fail("size too large");
2558 return;
2559 }
2560
vijay kumarde4fcf62015-04-23 13:05:49 +05302561 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302562
2563 if (data_end < (uint32_t)data) {
2564 fastboot_fail("buffer overreads occured due to invalid sparse header");
2565 return;
2566 }
2567
vijay kumarde4fcf62015-04-23 13:05:49 +05302568 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002569 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302570 fastboot_fail("sparse header size mismatch");
2571 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002572 }
2573
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002574 dprintf (SPEW, "=== Sparse Image Header ===\n");
2575 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2576 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2577 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2578 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2579 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2580 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2581 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2582 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002583
2584 /* Start processing chunks */
2585 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2586 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302587 /* Make sure the total image size does not exceed the partition size */
2588 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2589 fastboot_fail("size too large");
2590 return;
2591 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002592 /* Read and skip over chunk header */
2593 chunk_header = (chunk_header_t *) data;
2594 data += sizeof(chunk_header_t);
2595
vijay kumar800255e2015-04-24 20:26:19 +05302596 if (data_end < (uint32_t)data) {
2597 fastboot_fail("buffer overreads occured due to invalid sparse header");
2598 return;
2599 }
2600
Ajay Dudani5c761132011-04-07 20:19:04 -07002601 dprintf (SPEW, "=== Chunk Header ===\n");
2602 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2603 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2604 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2605
vijay kumar800255e2015-04-24 20:26:19 +05302606 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002607 {
vijay kumar800255e2015-04-24 20:26:19 +05302608 fastboot_fail("chunk header size mismatch");
2609 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002610 }
2611
wufeng.jiang813dc352015-06-02 23:02:46 -04002612 if (!sparse_header->blk_sz ){
2613 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302614 return;
2615 }
2616
wufeng.jiang813dc352015-06-02 23:02:46 -04002617 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2618
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302619 /* Make sure that the chunk size calculated from sparse image does not
2620 * exceed partition size
2621 */
2622 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2623 {
2624 fastboot_fail("Chunk data size exceeds partition size");
2625 return;
2626 }
2627
Ajay Dudani5c761132011-04-07 20:19:04 -07002628 switch (chunk_header->chunk_type)
2629 {
2630 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002631 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002632 chunk_data_sz))
2633 {
2634 fastboot_fail("Bogus chunk size for chunk type Raw");
2635 return;
2636 }
2637
vijay kumar800255e2015-04-24 20:26:19 +05302638 if (data_end < (uint32_t)data + chunk_data_sz) {
2639 fastboot_fail("buffer overreads occured due to invalid sparse header");
2640 return;
2641 }
2642
wufeng.jiang813dc352015-06-02 23:02:46 -04002643 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2644 otherwise it will return in the line above
2645 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002646 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002647 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002648 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002649 {
2650 fastboot_fail("flash write failure");
2651 return;
2652 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302653 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2654 fastboot_fail("Bogus size for RAW chunk type");
2655 return;
2656 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002657 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002658 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002659 break;
2660
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002661 case CHUNK_TYPE_FILL:
2662 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2663 sizeof(uint32_t)))
2664 {
2665 fastboot_fail("Bogus chunk size for chunk type FILL");
2666 return;
2667 }
2668
2669 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2670 if (!fill_buf)
2671 {
2672 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2673 return;
2674 }
2675
vijay kumar800255e2015-04-24 20:26:19 +05302676 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2677 fastboot_fail("buffer overreads occured due to invalid sparse header");
2678 return;
2679 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002680 fill_val = *(uint32_t *)data;
2681 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002682
2683 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2684 {
2685 fill_buf[i] = fill_val;
2686 }
2687
wufeng.jiang813dc352015-06-02 23:02:46 -04002688 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002689 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302690 /* Make sure that the data written to partition does not exceed partition size */
2691 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2692 {
2693 fastboot_fail("Chunk data size for fill type exceeds partition size");
2694 return;
2695 }
2696
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002697 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2698 sparse_header->blk_sz,
2699 fill_buf))
2700 {
2701 fastboot_fail("flash write failure");
2702 free(fill_buf);
2703 return;
2704 }
2705
2706 total_blocks++;
2707 }
2708
2709 free(fill_buf);
2710 break;
2711
Ajay Dudani5c761132011-04-07 20:19:04 -07002712 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302713 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2714 fastboot_fail("bogus size for chunk DONT CARE type");
2715 return;
2716 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002717 total_blocks += chunk_header->chunk_sz;
2718 break;
2719
Ajay Dudani5c761132011-04-07 20:19:04 -07002720 case CHUNK_TYPE_CRC:
2721 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2722 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002723 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002724 return;
2725 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302726 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2727 fastboot_fail("bogus size for chunk CRC type");
2728 return;
2729 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002730 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302731 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2732 fastboot_fail("integer overflow occured");
2733 return;
2734 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002735 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302736 if (data_end < (uint32_t)data) {
2737 fastboot_fail("buffer overreads occured due to invalid sparse header");
2738 return;
2739 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002740 break;
2741
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002742 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002743 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002744 fastboot_fail("Unknown chunk type");
2745 return;
2746 }
2747 }
2748
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002749 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2750 total_blocks, sparse_header->total_blks);
2751
2752 if(total_blocks != sparse_header->total_blks)
2753 {
2754 fastboot_fail("sparse image write failure");
2755 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002756
2757 fastboot_okay("");
2758 return;
2759}
2760
2761void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2762{
2763 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002764 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002765
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002766#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002767 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2768 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002769 int ret=0;
2770 uint32 major_version=0;
2771 uint32 minor_version=0;
2772
2773 ret = scm_svc_version(&major_version,&minor_version);
2774 if(!ret)
2775 {
2776 if(major_version >= 2)
2777 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002778 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002779 {
2780 ret = encrypt_scm((uint32 **) &data, &sz);
2781 if (ret != 0) {
2782 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2783 return;
2784 }
2785
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002786 /* Protect only for SSD */
2787 if (!strcmp(arg, "ssd")) {
2788 ret = scm_protect_keystore((uint32 *) data, sz);
2789 if (ret != 0) {
2790 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2791 return;
2792 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002793 }
2794 }
2795 else
2796 {
2797 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2798 if(ret != 0)
2799 {
2800 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2801 return;
2802 }
2803 }
2804 }
2805 else
2806 {
2807 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2808 magic_number[1] == DECRYPT_MAGIC_1)
2809 {
2810 ret = decrypt_scm((uint32 **) &data, &sz);
2811 if (ret != 0) {
2812 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2813 return;
2814 }
2815 }
2816 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2817 magic_number[1] == ENCRYPT_MAGIC_1)
2818 {
2819 ret = encrypt_scm((uint32 **) &data, &sz);
2820 if (ret != 0) {
2821 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2822 return;
2823 }
2824 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002825 }
2826 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002827 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002828 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002829 dprintf(CRITICAL,"INVALID SVC Version\n");
2830 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002831 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002832#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002833
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002834#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002835 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002836 {
lijuang511a2b52015-08-14 20:50:51 +08002837 /* if device is locked:
2838 * common partition will not allow to be flashed
2839 * critical partition will allow to flash image.
2840 */
2841 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
2842 fastboot_fail("Partition flashing is not allowed");
2843 return;
2844 }
2845
2846 /* if device critical is locked:
2847 * common partition will allow to be flashed
2848 * critical partition will not allow to flash image.
2849 */
2850 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
2851 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002852 return;
2853 }
2854 }
2855#endif
2856
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002857 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002858 meta_header = (meta_header_t *) data;
2859 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002860 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002861 else if (meta_header->magic == META_HEADER_MAGIC)
2862 cmd_flash_meta_img(arg, data, sz);
2863 else
2864 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002865
2866#if VERIFIED_BOOT
2867 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
2868 {
2869 // reset dm_verity mode to enforcing
2870 device.verity_mode = 1;
2871 write_device_info(&device);
2872 }
2873#endif
2874
Ajay Dudani5c761132011-04-07 20:19:04 -07002875 return;
2876}
2877
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002878void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2879{
2880 struct ptentry *sys_ptn;
2881 struct ptable *ptable;
2882
2883 ptable = flash_get_ptable();
2884 if (ptable == NULL) {
2885 fastboot_fail("partition table doesn't exist");
2886 return;
2887 }
2888
2889 sys_ptn = ptable_find(ptable, "system");
2890 if (sys_ptn == NULL) {
2891 fastboot_fail("system partition not found");
2892 return;
2893 }
2894
2895 sz = ROUND_TO_PAGE(sz, page_mask);
2896 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2897 fastboot_fail("update_ubi_vol failed");
2898 else
2899 fastboot_okay("");
2900}
2901
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002902void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002903{
2904 struct ptentry *ptn;
2905 struct ptable *ptable;
2906 unsigned extra = 0;
2907
2908 ptable = flash_get_ptable();
2909 if (ptable == NULL) {
2910 fastboot_fail("partition table doesn't exist");
2911 return;
2912 }
2913
2914 ptn = ptable_find(ptable, arg);
2915 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002916 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2917 arg);
2918 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002919 return;
2920 }
2921
2922 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2923 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2924 fastboot_fail("image is not a boot image");
2925 return;
2926 }
2927 }
2928
Amol Jadi5c61a952012-05-04 17:05:35 -07002929 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002930 || !strcmp(ptn->name, "userdata")
2931 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002932 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002933 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002934 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002935 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002936 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002937
2938 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002939 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2940 if (flash_ubi_img(ptn, data, sz)) {
2941 fastboot_fail("flash write failure");
2942 return;
2943 }
2944 } else {
2945 if (flash_write(ptn, extra, data, sz)) {
2946 fastboot_fail("flash write failure");
2947 return;
2948 }
Dima Zavin214cc642009-01-26 11:16:21 -08002949 }
2950 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2951 fastboot_okay("");
2952}
2953
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002954void cmd_flash(const char *arg, void *data, unsigned sz)
2955{
lijuang65c5a822015-08-29 16:35:36 +08002956#if CHECK_BAT_VOLTAGE
2957 if (!target_battery_soc_ok()) {
2958 fastboot_fail("Warning: battery's capacity is very low\n");
2959 return;
2960 }
2961#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002962 if(target_is_emmc_boot())
2963 cmd_flash_mmc(arg, data, sz);
2964 else
2965 cmd_flash_nand(arg, data, sz);
2966}
2967
Dima Zavin214cc642009-01-26 11:16:21 -08002968void cmd_continue(const char *arg, void *data, unsigned sz)
2969{
2970 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002971 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002972
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002973 if (target_is_emmc_boot())
2974 {
lijuanga40d6302015-07-20 20:10:13 +08002975#if FBCON_DISPLAY_MSG
2976 keys_detect_init();
2977#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002978 boot_linux_from_mmc();
2979 }
2980 else
2981 {
2982 boot_linux_from_flash();
2983 }
Dima Zavin214cc642009-01-26 11:16:21 -08002984}
2985
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002986void cmd_reboot(const char *arg, void *data, unsigned sz)
2987{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002988 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002989 fastboot_okay("");
2990 reboot_device(0);
2991}
2992
2993void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2994{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002995 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002996 fastboot_okay("");
2997 reboot_device(FASTBOOT_MODE);
2998}
2999
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003000void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3001{
3002 dprintf(INFO, "Enabling charger screen check\n");
3003 device.charger_screen_enabled = 1;
3004 write_device_info(&device);
3005 fastboot_okay("");
3006}
3007
3008void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3009{
3010 dprintf(INFO, "Disabling charger screen check\n");
3011 device.charger_screen_enabled = 0;
3012 write_device_info(&device);
3013 fastboot_okay("");
3014}
3015
lijuanga25d8bb2015-09-16 13:11:52 +08003016void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3017{
3018 char *p = NULL;
3019 const char *delim = " \t\n\r";
3020
3021 if (arg) {
3022 p = strtok((char *)arg, delim);
3023 if (p) {
3024 if (!strncmp(p, "0", 1)) {
3025 device.charger_screen_enabled = 0;
3026 } else if (!strncmp(p, "1", 1)) {
3027 device.charger_screen_enabled = 1;
3028 }
3029 }
3030 }
3031
3032 /* update charger_screen_enabled value for getvar
3033 * command
3034 */
3035 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3036 device.charger_screen_enabled);
3037
3038 write_device_info(&device);
3039 fastboot_okay("");
3040}
3041
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303042void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3043{
3044 dprintf(INFO, "Selecting display panel %s\n", arg);
3045 if (arg)
3046 strlcpy(device.display_panel, arg,
3047 sizeof(device.display_panel));
3048 write_device_info(&device);
3049 fastboot_okay("");
3050}
3051
Shashank Mittal162244e2011-08-08 19:01:25 -07003052void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3053{
lijuang4ece1e72015-08-14 21:02:36 +08003054 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303055}
3056
3057void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3058{
lijuang4ece1e72015-08-14 21:02:36 +08003059 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303060 if(!is_allow_unlock) {
3061 fastboot_fail("oem unlock is not allowed");
3062 return;
3063 }
3064
lijuang4ece1e72015-08-14 21:02:36 +08003065 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303066
lijuang4ece1e72015-08-14 21:02:36 +08003067 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303068 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003069
vijay kumarc65876c2015-04-24 13:29:16 +05303070 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3071 write_misc(0, &msg, sizeof(msg));
3072
3073 fastboot_okay("");
3074 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003075 }
3076 fastboot_okay("");
3077}
3078
Channagoud Kadabiad259832015-05-29 11:14:17 -07003079static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3080{
3081 int ret = 1;
3082 uint32_t secret_key;
3083 char seckey_buffer[MAX_RSP_SIZE];
3084
3085 secret_key = aboot_get_secret_key();
3086 if (secret_key)
3087 {
3088 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3089 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3090 {
3091 is_allow_unlock = true;
3092 write_allow_oem_unlock(is_allow_unlock);
3093 ret = 0;
3094 }
3095 }
3096 return ret;
3097}
3098
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003099void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3100{
lijuang4ece1e72015-08-14 21:02:36 +08003101 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003102}
3103
Shashank Mittala0032282011-08-26 14:50:11 -07003104void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3105{
lijuang511a2b52015-08-14 20:50:51 +08003106 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003107 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003108 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003109 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3110 fastboot_info(response);
lijuang511a2b52015-08-14 20:50:51 +08003111 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3112 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003113 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003114 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303115 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3116 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003117 fastboot_okay("");
3118}
3119
lijuang511a2b52015-08-14 20:50:51 +08003120void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3121{
3122 char response[MAX_RSP_SIZE];
3123 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3124 fastboot_info(response);
3125 fastboot_okay("");
3126}
3127
3128void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3129{
lijuang4ece1e72015-08-14 21:02:36 +08003130 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003131}
3132
3133void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3134{
lijuang4ece1e72015-08-14 21:02:36 +08003135 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003136}
3137
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003138void cmd_preflash(const char *arg, void *data, unsigned sz)
3139{
3140 fastboot_okay("");
3141}
3142
Mao Flynn7b379f32015-04-20 00:28:30 +08003143static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303144
Mao Flynn7b379f32015-04-20 00:28:30 +08003145int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303146{
Mao Flynn7b379f32015-04-20 00:28:30 +08003147 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303148 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003149 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303150 return -1;
3151 return 0;
3152}
3153
Mao Flynn7b379f32015-04-20 00:28:30 +08003154int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003155{
3156 struct ptentry *ptn;
3157 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003158 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003159 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003160
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303161 ptable = flash_get_ptable();
3162 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003163 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3164 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303165 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003166
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303167 ptn = ptable_find(ptable, "splash");
3168 if (ptn == NULL) {
3169 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003170 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303171 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003172 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303173 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003174 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303175 }
3176
Mao Flynn7b379f32015-04-20 00:28:30 +08003177 header = (struct logo_img_header *)logo_header;
3178 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303179 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003180 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303181 }
3182
3183 fb_display = fbcon_display();
3184 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003185 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3186 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3187
3188 /* if the logo is full-screen size, remove "fbcon_clear()" */
3189 if ((header->width != fb_display->width)
3190 || (header->height != fb_display->height))
3191 fbcon_clear();
3192
3193 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3194 (uint32_t *)base,
3195 (header->blocks * 512))) {
3196 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3197 return -1;
3198 }
3199 fbcon_extract_to_screen(header, base);
3200 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003201 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003202
3203 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3204 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3205 return -1;
3206 }
3207
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303208 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003209 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3210 (uint32_t *)base,
3211 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303212 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303213 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003214 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003215 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303216 }
3217
Mao Flynn7b379f32015-04-20 00:28:30 +08003218 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303219}
3220
Mao Flynn7b379f32015-04-20 00:28:30 +08003221int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303222{
3223 int index = INVALID_PTN;
3224 unsigned long long ptn = 0;
3225 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003226 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003227 uint32_t blocksize, realsize, readsize;
3228 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303229
3230 index = partition_get_index("splash");
3231 if (index == 0) {
3232 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003233 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303234 }
3235
3236 ptn = partition_get_offset(index);
3237 if (ptn == 0) {
3238 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003239 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303240 }
3241
Mao Flynn1893a7f2015-06-03 12:03:36 +08003242 mmc_set_lun(partition_get_lun(index));
3243
3244 blocksize = mmc_get_device_blocksize();
3245 if (blocksize == 0) {
3246 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3247 return -1;
3248 }
3249
3250 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003251 if (!fb_display)
3252 {
3253 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3254 return -1;
3255 }
3256
Mao Flynn1893a7f2015-06-03 12:03:36 +08003257 base = (uint8_t *) fb_display->base;
3258
3259 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303260 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003261 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303262 }
3263
Mao Flynn1893a7f2015-06-03 12:03:36 +08003264 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003265 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303266 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003267 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303268 }
3269
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303270 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003271 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3272 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003273
Mao Flynn1893a7f2015-06-03 12:03:36 +08003274 realsize = header->blocks * 512;
3275 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3276
3277 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003278 if ((header->width != fb_display->width)
3279 || (header->height != fb_display->height))
3280 fbcon_clear();
3281
Mao Flynn1893a7f2015-06-03 12:03:36 +08003282 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003283 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3284 return -1;
3285 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003286
Mao Flynn1893a7f2015-06-03 12:03:36 +08003287 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3288 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303289
Mao Flynn1893a7f2015-06-03 12:03:36 +08003290 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3291 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3292 return -1;
3293 }
3294
3295 realsize = header->width * header->height * fb_display->bpp / 8;
3296 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3297
3298 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3299 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3300 fbcon_clear();
3301 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3302 return -1;
3303 }
3304 } else {
3305 if (mmc_read(ptn + blocksize ,
3306 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3307 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3308 return -1;
3309 }
3310 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3311 }
3312 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303313 }
3314
Mao Flynn7b379f32015-04-20 00:28:30 +08003315 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303316}
3317
Mao Flynn7b379f32015-04-20 00:28:30 +08003318int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303319{
3320 if (target_is_emmc_boot()) {
3321 return splash_screen_mmc();
3322 } else {
3323 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003324 }
3325}
3326
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003327/* Get the size from partiton name */
3328static void get_partition_size(const char *arg, char *response)
3329{
3330 uint64_t ptn = 0;
3331 uint64_t size;
3332 int index = INVALID_PTN;
3333
3334 index = partition_get_index(arg);
3335
3336 if (index == INVALID_PTN)
3337 {
3338 dprintf(CRITICAL, "Invalid partition index\n");
3339 return;
3340 }
3341
3342 ptn = partition_get_offset(index);
3343
3344 if(!ptn)
3345 {
3346 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3347 return;
3348 }
3349
3350 size = partition_get_size(index);
3351
3352 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3353 return;
3354}
3355
3356/*
3357 * Publish the partition type & size info
3358 * fastboot getvar will publish the required information.
3359 * fastboot getvar partition_size:<partition_name>: partition size in hex
3360 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3361 */
3362static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3363{
3364 uint8_t i;
3365
3366 for (i = 0; i < num_parts; i++) {
3367 get_partition_size(info[i].part_name, info[i].size_response);
3368
3369 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3370 {
3371 dprintf(CRITICAL, "partition size name truncated\n");
3372 return;
3373 }
3374 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3375 {
3376 dprintf(CRITICAL, "partition type name truncated\n");
3377 return;
3378 }
3379
3380 /* publish partition size & type info */
3381 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3382 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3383 }
3384}
3385
lijuang4ece1e72015-08-14 21:02:36 +08003386void get_product_name(unsigned char *buf)
3387{
3388 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3389 return;
3390}
3391
3392void get_bootloader_version(unsigned char *buf)
3393{
3394 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3395 return;
3396}
3397
3398void get_baseband_version(unsigned char *buf)
3399{
3400 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3401 return;
3402}
3403
3404bool is_device_locked()
3405{
3406 return device.is_unlocked ? false:true;
3407}
3408
Amol Jadi5edf3552013-07-23 14:15:34 -07003409/* register commands and variables for fastboot */
3410void aboot_fastboot_register_commands(void)
3411{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003412 int i;
lijuangf16461c2015-08-03 17:09:34 +08003413 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003414
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003415 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003416 /* By default the enabled list is empty. */
3417 {"", NULL},
3418 /* move commands enclosed within the below ifndef to here
3419 * if they need to be enabled in user build.
3420 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003421#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003422 /* Register the following commands only for non-user builds */
3423 {"flash:", cmd_flash},
3424 {"erase:", cmd_erase},
3425 {"boot", cmd_boot},
3426 {"continue", cmd_continue},
3427 {"reboot", cmd_reboot},
3428 {"reboot-bootloader", cmd_reboot_bootloader},
3429 {"oem unlock", cmd_oem_unlock},
3430 {"oem unlock-go", cmd_oem_unlock_go},
3431 {"oem lock", cmd_oem_lock},
3432 {"flashing unlock", cmd_oem_unlock},
3433 {"flashing lock", cmd_oem_lock},
3434 {"flashing lock_critical", cmd_flashing_lock_critical},
3435 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3436 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3437 {"oem device-info", cmd_oem_devinfo},
3438 {"preflash", cmd_preflash},
3439 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3440 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003441 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003442 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003443#endif
lijuang511a2b52015-08-14 20:50:51 +08003444 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003445
3446 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3447 for (i = 1; i < fastboot_cmds_count; i++)
3448 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3449
Amol Jadi5edf3552013-07-23 14:15:34 -07003450 /* publish variables and their values */
3451 fastboot_publish("product", TARGET(BOARD));
3452 fastboot_publish("kernel", "lk");
3453 fastboot_publish("serialno", sn_buf);
3454
3455 /*
3456 * partition info is supported only for emmc partitions
3457 * Calling this for NAND prints some error messages which
3458 * is harmless but misleading. Avoid calling this for NAND
3459 * devices.
3460 */
3461 if (target_is_emmc_boot())
3462 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3463
3464 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003465 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3466 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003467 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003468 /* Is the charger screen check enabled */
3469 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3470 device.charger_screen_enabled);
3471 fastboot_publish("charger-screen-enabled",
3472 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003473 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303474 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3475 device.display_panel);
3476 fastboot_publish("display-panel",
3477 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003478 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3479 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003480 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3481 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3482 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3483 target_is_emmc_boot()? "eMMC":"UFS");
3484 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003485#if CHECK_BAT_VOLTAGE
3486 snprintf(battery_voltage, MAX_RSP_SIZE, "%d",
3487 target_get_battery_voltage());
3488 fastboot_publish("battery-voltage", (const char *) battery_voltage);
3489 fastboot_publish("battery-soc-ok", target_battery_soc_ok()? "yes":"no");
3490#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003491}
3492
Brian Swetland9c4c0752009-01-25 16:23:50 -08003493void aboot_init(const struct app_descriptor *app)
3494{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003495 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03003496 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003497
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003498 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003499 if (target_is_emmc_boot())
3500 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003501 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003502 page_mask = page_size - 1;
3503 }
3504 else
3505 {
3506 page_size = flash_page_size();
3507 page_mask = page_size - 1;
3508 }
3509
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003510 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3511
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003512 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303513 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003514
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003515 /* Display splash screen if enabled */
3516#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003517#if NO_ALARM_DISPLAY
3518 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003519#endif
lijuang99c02d82015-02-13 19:04:34 +08003520 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003521#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003522 /* Wait if the display shutdown is in progress */
3523 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003524 if (!pm_appsbl_display_init_done())
3525 target_display_init(device.display_panel);
3526 else
3527 display_image_on_screen();
3528#else
lijuang99c02d82015-02-13 19:04:34 +08003529 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003530#endif
lijuang99c02d82015-02-13 19:04:34 +08003531 dprintf(SPEW, "Display Init: Done\n");
3532#if NO_ALARM_DISPLAY
3533 }
3534#endif
3535#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003536
Greg Griscod6250552011-06-29 14:40:23 -07003537 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003538 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003539
Dhaval Patel223ec952013-07-18 14:49:44 -07003540 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3541
Matthew Qindefd5562014-07-11 18:02:40 +08003542 /*
3543 * Check power off reason if user force reset,
3544 * if yes phone will do normal boot.
3545 */
3546 if (is_user_force_reset())
3547 goto normal_boot;
3548
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003549 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003550 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003551 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003552 dprintf(ALWAYS,"dload mode key sequence detected\n");
3553 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003554 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003555 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003556 }
3557 else
3558 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303559 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003560 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003561 }
3562 boot_into_fastboot = true;
3563 }
3564 if (!boot_into_fastboot)
3565 {
3566 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3567 boot_into_recovery = 1;
3568 if (!boot_into_recovery &&
3569 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003570 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003571 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003572 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003573 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003574 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003575 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003576
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003577#if USE_PON_REBOOT_REG
3578 reboot_mode = check_hard_reboot_mode();
3579#else
Ajay Dudani77421292010-10-27 19:34:06 -07003580 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003581#endif
3582 if (reboot_mode == RECOVERY_MODE)
3583 {
Ajay Dudani77421292010-10-27 19:34:06 -07003584 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003585 }
3586 else if(reboot_mode == FASTBOOT_MODE)
3587 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003588 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003589 }
3590 else if(reboot_mode == ALARM_BOOT)
3591 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003592 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003593 }
3594 else if (reboot_mode == DM_VERITY_ENFORCING)
3595 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003596 device.verity_mode = 1;
3597 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003598 }
3599 else if (reboot_mode == DM_VERITY_LOGGING)
3600 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003601 device.verity_mode = 0;
3602 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003603 }
3604 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3605 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003606 if(send_delete_keys_to_tz())
3607 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003608 }
3609
Matthew Qindefd5562014-07-11 18:02:40 +08003610normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003611 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003612 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003613 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003614 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003615 if(emmc_recovery_init())
3616 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3617 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003618 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003619 if((device.is_unlocked) || (device.is_tampered))
3620 {
3621 #ifdef TZ_TAMPER_FUSE
3622 set_tamper_fuse_cmd();
3623 #endif
3624 #if USE_PCOM_SECBOOT
3625 set_tamper_flag(device.is_tampered);
3626 #endif
3627 }
Shashank Mittala0032282011-08-26 14:50:11 -07003628 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003629
Pavel Nedev5d91d412013-04-29 11:34:24 +03003630 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003631 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003632 else
3633 {
3634 recovery_init();
3635 #if USE_PCOM_SECBOOT
3636 if((device.is_unlocked) || (device.is_tampered))
3637 set_tamper_flag(device.is_tampered);
3638 #endif
3639 boot_linux_from_flash();
3640 }
3641 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3642 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003643 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003644
Amol Jadi5edf3552013-07-23 14:15:34 -07003645 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003646
Amol Jadi5edf3552013-07-23 14:15:34 -07003647 /* register aboot specific fastboot commands */
3648 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003649
Amol Jadi5edf3552013-07-23 14:15:34 -07003650 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003651 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003652
3653 /* initialize and start fastboot */
3654 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003655#if FBCON_DISPLAY_MSG
3656 display_fastboot_menu_thread();
3657#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003658}
3659
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003660uint32_t get_page_size()
3661{
3662 return page_size;
3663}
3664
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003665/*
3666 * Calculated and save hash (SHA256) for non-signed boot image.
3667 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003668 * @param image_addr - Boot image address
3669 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003670 *
3671 * @return int - 0 on success, negative value on failure.
3672 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003673static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003674{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003675 unsigned int digest[8];
3676#if IMAGE_VERIF_ALGO_SHA1
3677 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3678#else
3679 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3680#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003681
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003682 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003683 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003684
3685 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003686 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003687
3688 return 0;
3689}
3690
Brian Swetland9c4c0752009-01-25 16:23:50 -08003691APP_START(aboot)
3692 .init = aboot_init,
3693APP_END