blob: 736a1f4b7ddda8704dde52a6cd081478b1368dd0 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08005 * Copyright (c) 2009-2015, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080060#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053061#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080062#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080063#if USE_RPMB_FOR_DEVINFO
64#include <rpmb.h>
65#endif
Dima Zavin214cc642009-01-26 11:16:21 -080066
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070067#if ENABLE_WBC
68#include <pm_app_smbchg.h>
69#endif
70
Neeti Desai17379b82012-06-04 18:42:53 -070071#if DEVICE_TREE
72#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070073#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070074#endif
75
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053076#if WDOG_SUPPORT
77#include <wdog.h>
78#endif
79
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070080#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070081#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080082#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080083#include "bootimg.h"
84#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070085#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080086#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070087#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070088#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070089#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020091#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070092#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080093#include <menu_keys_detect.h>
94#include <display_menu.h>
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070095
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070096extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070097extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070098extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070099extern int get_target_boot_params(const char *cmdline, const char *part,
100 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700101
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700102void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700103void write_device_info_mmc(device_info *dev);
104void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700105static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700106
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700107/* fastboot command function pointer */
108typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
109
110struct fastboot_cmd_desc {
111 char * name;
112 fastboot_cmd_fn cb;
113};
114
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700115#define EXPAND(NAME) #NAME
116#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700117
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800118#ifdef MEMBASE
119#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
120#else
David Ng183a7422009-12-07 14:55:21 -0800121#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800122#endif
123
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700124#ifndef MEMSIZE
125#define MEMSIZE 1024*1024
126#endif
127
128#define MAX_TAGS_SIZE 1024
129
Kun Liang2f1601a2013-08-12 16:29:54 +0800130/* make 4096 as default size to ensure EFS,EXT4's erasing */
131#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700132#define MAX_PANEL_BUF_SIZE 196
Kun Liang2f1601a2013-08-12 16:29:54 +0800133
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700134#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700135#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800136
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800137#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
138
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700139#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
140
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800141#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700142static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700143#else
David Ng183a7422009-12-07 14:55:21 -0800144static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700145#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800146static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300147static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800148static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800149static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800150static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700151static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300152static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200153static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800154
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800155static const char *baseband_apq = " androidboot.baseband=apq";
156static const char *baseband_msm = " androidboot.baseband=msm";
157static const char *baseband_csfb = " androidboot.baseband=csfb";
158static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700159static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800160static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700161static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800162static const char *baseband_dsda = " androidboot.baseband=dsda";
163static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800164static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800165static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800166
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700167#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700168static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700169static const char *verified_state= " androidboot.verifiedbootstate=";
170
171//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700172
173struct verified_boot_verity_mode vbvm[] =
174{
175 {false, "logging"},
176 {true, "enforcing"},
177};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700178struct verified_boot_state_name vbsn[] =
179{
180 {GREEN, "green"},
181 {ORANGE, "orange"},
182 {YELLOW,"yellow"},
183 {RED,"red" },
184};
185#endif
186
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700187static unsigned page_size = 0;
188static unsigned page_mask = 0;
189static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
190static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700191static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800192static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700193static bool devinfo_present = true;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700194
Shashank Mittalcd98d472011-08-02 14:29:24 -0700195/* Assuming unauthorized kernel image by default */
196static int auth_kernel_img = 0;
197
lijuang511a2b52015-08-14 20:50:51 +0800198static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
vijay kumarc65876c2015-04-24 13:29:16 +0530199static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700200
vijay kumarca2e6812015-07-08 20:28:25 +0530201static char frp_ptns[2][8] = {"config","frp"};
202
lijuang511a2b52015-08-14 20:50:51 +0800203static const char *critical_flash_allowed_ptn[] = {
204 "aboot",
205 "rpm",
206 "tz",
207 "sbl",
208 "sdi",
209 "sbl1",
210 "xbl",
211 "hyp",
212 "pmic",
213 "bootloader",
214 "devinfo",
215 "partition"};
216
Dima Zavin42168f22009-01-30 11:52:22 -0800217struct atag_ptbl_entry
218{
219 char name[16];
220 unsigned offset;
221 unsigned size;
222 unsigned flags;
223};
224
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700225/*
226 * Partition info, required to be published
227 * for fastboot
228 */
229struct getvar_partition_info {
230 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
231 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
232 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
233 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
234 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
235};
236
237/*
238 * Right now, we are publishing the info for only
239 * three partitions
240 */
241struct getvar_partition_info part_info[] =
242{
243 { "system" , "partition-size:", "partition-type:", "", "ext4" },
244 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
245 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
246};
247
248char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700249char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800250char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700251char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530252char panel_display_mode[MAX_RSP_SIZE];
lijuang65c5a822015-08-29 16:35:36 +0800253char battery_voltage[MAX_RSP_SIZE];
lijuangf16461c2015-08-03 17:09:34 +0800254char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700255
Greg Griscod2471ef2011-07-14 13:00:42 -0700256extern int emmc_recovery_init(void);
257
Kinson Chik0b1c8162011-08-31 16:31:57 -0700258#if NO_KEYPAD_DRIVER
259extern int fastboot_trigger(void);
260#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700261
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800262static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700263{
264 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700265#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800266 if (is_arm64)
267 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
268 else
269 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700270 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
271 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700272#endif
273}
274
Dima Zavin42168f22009-01-30 11:52:22 -0800275static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
276{
277 struct atag_ptbl_entry atag_ptn;
278
279 memcpy(atag_ptn.name, ptn->name, 16);
280 atag_ptn.name[15] = '\0';
281 atag_ptn.offset = ptn->start;
282 atag_ptn.size = ptn->length;
283 atag_ptn.flags = ptn->flags;
284 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
285 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
286}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800287
Neeti Desaie245d492012-06-01 12:52:13 -0700288unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800289{
David Ng183a7422009-12-07 14:55:21 -0800290 int cmdline_len = 0;
291 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800292 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700293 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800294 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300295 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700296 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700297 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200298 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700299#if VERIFIED_BOOT
300 uint32_t boot_state = boot_verify_get_state();
301#endif
302
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200303#ifdef MDTP_SUPPORT
304 mdtp_activated(&is_mdtp_activated);
305#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800306
Brian Swetland9c4c0752009-01-25 16:23:50 -0800307 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800308 cmdline_len = strlen(cmdline);
309 have_cmdline = 1;
310 }
311 if (target_is_emmc_boot()) {
312 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800313#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700314 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
315 ASSERT(boot_dev_buf);
316 platform_boot_dev_cmdline(boot_dev_buf);
317 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700318#endif
David Ng183a7422009-12-07 14:55:21 -0800319 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800320
321 cmdline_len += strlen(usb_sn_cmdline);
322 cmdline_len += strlen(sn_buf);
323
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700324#if VERIFIED_BOOT
325 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700326 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700327#endif
328
Pavel Nedev5614d222013-06-17 18:01:02 +0300329 if (boot_into_recovery && gpt_exists)
330 cmdline_len += strlen(secondary_gpt_enable);
331
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200332 if(is_mdtp_activated)
333 cmdline_len += strlen(mdtp_activated_flag);
334
Pavel Nedev328ac822013-04-05 15:25:11 +0300335 if (boot_into_ffbm) {
336 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700337 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800338 /* reduce kernel console messages to speed-up boot */
339 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800340 } else if (boot_reason_alarm) {
341 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800342 } else if ((target_build_variant_user() || device.charger_screen_enabled)
343 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700344 pause_at_bootup = 1;
345 cmdline_len += strlen(battchg_pause);
346 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800347
Shashank Mittalcd98d472011-08-02 14:29:24 -0700348 if(target_use_signed_kernel() && auth_kernel_img) {
349 cmdline_len += strlen(auth_kernel);
350 }
351
Joonwoo Park61112782013-10-02 19:50:39 -0700352 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
353 "system",
354 target_boot_params,
355 sizeof(target_boot_params)) == 0) {
356 have_target_boot_params = 1;
357 cmdline_len += strlen(target_boot_params);
358 }
359
Ajay Dudanid04110c2011-01-17 23:55:07 -0800360 /* Determine correct androidboot.baseband to use */
361 switch(target_baseband())
362 {
363 case BASEBAND_APQ:
364 cmdline_len += strlen(baseband_apq);
365 break;
366
367 case BASEBAND_MSM:
368 cmdline_len += strlen(baseband_msm);
369 break;
370
371 case BASEBAND_CSFB:
372 cmdline_len += strlen(baseband_csfb);
373 break;
374
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800375 case BASEBAND_SVLTE2A:
376 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800377 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700378
379 case BASEBAND_MDM:
380 cmdline_len += strlen(baseband_mdm);
381 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700382
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800383 case BASEBAND_MDM2:
384 cmdline_len += strlen(baseband_mdm2);
385 break;
386
Amol Jadi5c61a952012-05-04 17:05:35 -0700387 case BASEBAND_SGLTE:
388 cmdline_len += strlen(baseband_sglte);
389 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530390
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800391 case BASEBAND_SGLTE2:
392 cmdline_len += strlen(baseband_sglte2);
393 break;
394
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530395 case BASEBAND_DSDA:
396 cmdline_len += strlen(baseband_dsda);
397 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800398
399 case BASEBAND_DSDA2:
400 cmdline_len += strlen(baseband_dsda2);
401 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800402 }
403
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800404 if (cmdline) {
405 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530406 target_display_panel_node(display_panel_buf,
407 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800408 strlen(display_panel_buf)) {
409 cmdline_len += strlen(display_panel_buf);
410 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700411 }
412
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800413 if (target_warm_boot()) {
414 warm_boot = true;
415 cmdline_len += strlen(warmboot_cmdline);
416 }
417
David Ng183a7422009-12-07 14:55:21 -0800418 if (cmdline_len > 0) {
419 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800420 unsigned char *dst;
421
422 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
423 ASSERT(cmdline_final != NULL);
424 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700425
Amol Jadi168b7712012-03-06 16:15:00 -0800426 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800427 if (have_cmdline) {
428 src = cmdline;
429 while ((*dst++ = *src++));
430 }
431 if (target_is_emmc_boot()) {
432 src = emmc_cmdline;
433 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700434 have_cmdline = 1;
435 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800436#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700437 src = boot_dev_buf;
438 if (have_cmdline) --dst;
439 while ((*dst++ = *src++));
440#endif
David Ngf773dde2010-07-26 19:55:08 -0700441 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800442
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700443#if VERIFIED_BOOT
444 src = verified_state;
445 if(have_cmdline) --dst;
446 have_cmdline = 1;
447 while ((*dst++ = *src++));
448 src = vbsn[boot_state].name;
449 if(have_cmdline) --dst;
450 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700451
452 src = verity_mode;
453 if(have_cmdline) --dst;
454 while ((*dst++ = *src++));
455 src = vbvm[device.verity_mode].name;
456 if(have_cmdline) -- dst;
457 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700458#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800459 src = usb_sn_cmdline;
460 if (have_cmdline) --dst;
461 have_cmdline = 1;
462 while ((*dst++ = *src++));
463 src = sn_buf;
464 if (have_cmdline) --dst;
465 have_cmdline = 1;
466 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800467 if (warm_boot) {
468 if (have_cmdline) --dst;
469 src = warmboot_cmdline;
470 while ((*dst++ = *src++));
471 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800472
Pavel Nedev5614d222013-06-17 18:01:02 +0300473 if (boot_into_recovery && gpt_exists) {
474 src = secondary_gpt_enable;
475 if (have_cmdline) --dst;
476 while ((*dst++ = *src++));
477 }
478
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200479 if (is_mdtp_activated) {
480 src = mdtp_activated_flag;
481 if (have_cmdline) --dst;
482 while ((*dst++ = *src++));
483 }
484
Pavel Nedev328ac822013-04-05 15:25:11 +0300485 if (boot_into_ffbm) {
486 src = androidboot_mode;
487 if (have_cmdline) --dst;
488 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700489 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300490 if (have_cmdline) --dst;
491 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800492 src = loglevel;
493 if (have_cmdline) --dst;
494 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800495 } else if (boot_reason_alarm) {
496 src = alarmboot_cmdline;
497 if (have_cmdline) --dst;
498 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300499 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700500 src = battchg_pause;
501 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800502 while ((*dst++ = *src++));
503 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800504
Shashank Mittalcd98d472011-08-02 14:29:24 -0700505 if(target_use_signed_kernel() && auth_kernel_img) {
506 src = auth_kernel;
507 if (have_cmdline) --dst;
508 while ((*dst++ = *src++));
509 }
510
Ajay Dudanid04110c2011-01-17 23:55:07 -0800511 switch(target_baseband())
512 {
513 case BASEBAND_APQ:
514 src = baseband_apq;
515 if (have_cmdline) --dst;
516 while ((*dst++ = *src++));
517 break;
518
519 case BASEBAND_MSM:
520 src = baseband_msm;
521 if (have_cmdline) --dst;
522 while ((*dst++ = *src++));
523 break;
524
525 case BASEBAND_CSFB:
526 src = baseband_csfb;
527 if (have_cmdline) --dst;
528 while ((*dst++ = *src++));
529 break;
530
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800531 case BASEBAND_SVLTE2A:
532 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800533 if (have_cmdline) --dst;
534 while ((*dst++ = *src++));
535 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700536
537 case BASEBAND_MDM:
538 src = baseband_mdm;
539 if (have_cmdline) --dst;
540 while ((*dst++ = *src++));
541 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700542
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800543 case BASEBAND_MDM2:
544 src = baseband_mdm2;
545 if (have_cmdline) --dst;
546 while ((*dst++ = *src++));
547 break;
548
Amol Jadi5c61a952012-05-04 17:05:35 -0700549 case BASEBAND_SGLTE:
550 src = baseband_sglte;
551 if (have_cmdline) --dst;
552 while ((*dst++ = *src++));
553 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530554
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800555 case BASEBAND_SGLTE2:
556 src = baseband_sglte2;
557 if (have_cmdline) --dst;
558 while ((*dst++ = *src++));
559 break;
560
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530561 case BASEBAND_DSDA:
562 src = baseband_dsda;
563 if (have_cmdline) --dst;
564 while ((*dst++ = *src++));
565 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800566
567 case BASEBAND_DSDA2:
568 src = baseband_dsda2;
569 if (have_cmdline) --dst;
570 while ((*dst++ = *src++));
571 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800572 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700573
574 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700575 src = display_panel_buf;
576 if (have_cmdline) --dst;
577 while ((*dst++ = *src++));
578 }
Joonwoo Park61112782013-10-02 19:50:39 -0700579
580 if (have_target_boot_params) {
581 if (have_cmdline) --dst;
582 src = target_boot_params;
583 while ((*dst++ = *src++));
584 }
Neeti Desaie245d492012-06-01 12:52:13 -0700585 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700586
587
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700588 if (boot_dev_buf)
589 free(boot_dev_buf);
590
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800591 if (cmdline_final)
592 dprintf(INFO, "cmdline: %s\n", cmdline_final);
593 else
594 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700595 return cmdline_final;
596}
597
598unsigned *atag_core(unsigned *ptr)
599{
600 /* CORE */
601 *ptr++ = 2;
602 *ptr++ = 0x54410001;
603
604 return ptr;
605
606}
607
608unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
609 unsigned ramdisk_size)
610{
611 if (ramdisk_size) {
612 *ptr++ = 4;
613 *ptr++ = 0x54420005;
614 *ptr++ = (unsigned)ramdisk;
615 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800616 }
617
Neeti Desaie245d492012-06-01 12:52:13 -0700618 return ptr;
619}
620
621unsigned *atag_ptable(unsigned **ptr_addr)
622{
623 int i;
624 struct ptable *ptable;
625
626 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700627 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
628 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700629 *(*ptr_addr)++ = 0x4d534d70;
630 for (i = 0; i < ptable->count; ++i)
631 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
632 }
633
634 return (*ptr_addr);
635}
636
637unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
638{
639 int cmdline_length = 0;
640 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700641 char *dest;
642
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800643 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700644 n = (cmdline_length + 4) & (~3);
645
646 *ptr++ = (n / 4) + 2;
647 *ptr++ = 0x54410009;
648 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800649 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700650 ptr += (n / 4);
651
652 return ptr;
653}
654
655unsigned *atag_end(unsigned *ptr)
656{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800657 /* END */
658 *ptr++ = 0;
659 *ptr++ = 0;
660
Neeti Desaie245d492012-06-01 12:52:13 -0700661 return ptr;
662}
663
664void generate_atags(unsigned *ptr, const char *cmdline,
665 void *ramdisk, unsigned ramdisk_size)
666{
667
668 ptr = atag_core(ptr);
669 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
670 ptr = target_atag_mem(ptr);
671
672 /* Skip NAND partition ATAGS for eMMC boot */
673 if (!target_is_emmc_boot()){
674 ptr = atag_ptable(&ptr);
675 }
676
677 ptr = atag_cmdline(ptr, cmdline);
678 ptr = atag_end(ptr);
679}
680
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700681typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700682void boot_linux(void *kernel, unsigned *tags,
683 const char *cmdline, unsigned machtype,
684 void *ramdisk, unsigned ramdisk_size)
685{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800686 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800687#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700688 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800689#endif
690
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700691 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800692 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800693 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800694
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530695 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700696
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800697 final_cmdline = update_cmdline((const char*)cmdline);
698
Neeti Desai17379b82012-06-04 18:42:53 -0700699#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800700 dprintf(INFO, "Updating device tree: start\n");
701
Neeti Desai17379b82012-06-04 18:42:53 -0700702 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530703 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700704 if(ret)
705 {
706 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
707 ASSERT(0);
708 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800709 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700710#else
Neeti Desaie245d492012-06-01 12:52:13 -0700711 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800712 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700713#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700714
Maria Yu52254c02014-07-04 16:14:54 +0800715 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700716
717#if VERIFIED_BOOT
718 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700719 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700720 {
721 dprintf(INFO, "Failed to write protect dev info\n");
722 ASSERT(0);
723 }
724#endif
725
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700726 /* Perform target specific cleanup */
727 target_uninit();
728
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800729 /* Turn off splash screen if enabled */
730#if DISPLAY_SPLASH_SCREEN
731 target_display_shutdown();
732#endif
733
734
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800735 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530736 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800737
738 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700739
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +0530740 /* Initialise wdog to catch early kernel crashes */
741#if WDOG_SUPPORT
742 msm_wdog_init();
743#endif
Amol Jadi4421e652011-06-16 15:00:48 -0700744 /* do any platform specific cleanup before kernel entry */
745 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700746
Brian Swetland9c4c0752009-01-25 16:23:50 -0800747 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700748
Amol Jadi504f9fe2012-08-16 13:56:48 -0700749#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800750 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700751#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700752 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800753
754 if (IS_ARM64(kptr))
755 /* Jump to a 64bit kernel */
756 scm_elexec_call((paddr_t)kernel, tags_phys);
757 else
758 /* Jump to a 32bit kernel */
759 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800760}
761
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700762/* Function to check if the memory address range falls within the aboot
763 * boundaries.
764 * start: Start of the memory region
765 * size: Size of the memory region
766 */
767int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
768{
769 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800770 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700771 return -1;
772
773 /* Check for memory overlap. */
774 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
775 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700776 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700777 return 0;
778 else
779 return -1;
780}
781
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800782#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800783
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800784BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800785#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800786BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800787#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800788
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700789static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
790{
791 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800792
793#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800794#if IMAGE_VERIF_ALGO_SHA1
795 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
796#else
797 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
798#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800799#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700800
801 /* Assume device is rooted at this time. */
802 device.is_tampered = 1;
803
804 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
805
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700806#if VERIFIED_BOOT
807 if(boot_into_recovery)
808 {
809 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530810 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700811 }
812 else
813 {
814 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530815 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700816 }
817 boot_verify_print_state();
818#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700819 ret = image_verify((unsigned char *)bootimg_addr,
820 (unsigned char *)(bootimg_addr + bootimg_size),
821 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800822 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700823#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700824 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
825
826 if (ret)
827 {
828 /* Authorized kernel */
829 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700830 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700831 }
832
Amit Blay4aa292f2015-04-28 21:55:59 +0300833#ifdef MDTP_SUPPORT
834 {
835 /* Verify MDTP lock.
836 * For boot & recovery partitions, use aboot's verification result.
837 */
838 mdtp_ext_partition_verification_t ext_partition;
839 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
840 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
841 ext_partition.page_size = 0; /* Not needed since already validated */
842 ext_partition.image_addr = 0; /* Not needed since already validated */
843 ext_partition.image_size = 0; /* Not needed since already validated */
844 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
845 mdtp_fwlock_verify_lock(&ext_partition);
846 }
847#endif /* MDTP_SUPPORT */
848
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700849#if USE_PCOM_SECBOOT
850 set_tamper_flag(device.is_tampered);
851#endif
852
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700853#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700854 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700855 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700856 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800857#if FBCON_DISPLAY_MSG
858 display_menu_thread(DISPLAY_THREAD_BOOT_STATE);
859 wait_for_users_action();
860#else
861 dprintf(CRITICAL,
862 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
863 mdelay(5000);
864#endif
865
866 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700867 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800868#if FBCON_DISPLAY_MSG
869 display_menu_thread(DISPLAY_THREAD_BOOT_STATE);
870 wait_for_users_action();
871#else
872 dprintf(CRITICAL,
873 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
874 mdelay(5000);
875#endif
876 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700877 default:
lijuanga40d6302015-07-20 20:10:13 +0800878 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700879 }
880#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700881#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530882 if(device.is_tampered)
883 {
884 write_device_info_mmc(&device);
885 #ifdef TZ_TAMPER_FUSE
886 set_tamper_fuse_cmd();
887 #endif
888 #ifdef ASSERT_ON_TAMPER
889 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
890 ASSERT(0);
891 #endif
892 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700893#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700894}
895
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530896static bool check_format_bit()
897{
898 bool ret = false;
899 int index;
900 uint64_t offset;
901 struct boot_selection_info *in = NULL;
902 char *buf = NULL;
903
904 index = partition_get_index("bootselect");
905 if (index == INVALID_PTN)
906 {
907 dprintf(INFO, "Unable to locate /bootselect partition\n");
908 return ret;
909 }
910 offset = partition_get_offset(index);
911 if(!offset)
912 {
913 dprintf(INFO, "partition /bootselect doesn't exist\n");
914 return ret;
915 }
916 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
917 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530918 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530919 {
920 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
921 free(buf);
922 return ret;
923 }
924 in = (struct boot_selection_info *) buf;
925 if ((in->signature == BOOTSELECT_SIGNATURE) &&
926 (in->version == BOOTSELECT_VERSION)) {
927 if ((in->state_info & BOOTSELECT_FORMAT) &&
928 !(in->state_info & BOOTSELECT_FACTORY))
929 ret = true;
930 } else {
931 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
932 in->signature, in->version);
933 ASSERT(0);
934 }
935 free(buf);
936 return ret;
937}
938
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700939void boot_verifier_init()
940{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700941 uint32_t boot_state;
942 /* Check if device unlock */
943 if(device.is_unlocked)
944 {
945 boot_verify_send_event(DEV_UNLOCK);
946 boot_verify_print_state();
947 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
948 return;
949 }
950 else
951 {
952 boot_verify_send_event(BOOT_INIT);
953 }
954
955 /* Initialize keystore */
956 boot_state = boot_verify_keystore_init();
957 if(boot_state == YELLOW)
958 {
959 boot_verify_print_state();
960 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
961 }
962}
963
Shashank Mittal23b8f422010-04-16 19:27:21 -0700964int boot_linux_from_mmc(void)
965{
966 struct boot_img_hdr *hdr = (void*) buf;
967 struct boot_img_hdr *uhdr;
968 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700969 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700970 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700971 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700972
Shashank Mittalcd98d472011-08-02 14:29:24 -0700973 unsigned char *image_addr = 0;
974 unsigned kernel_actual;
975 unsigned ramdisk_actual;
976 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700977 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700978
Matthew Qin271927e2015-03-31 22:07:07 -0400979 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +0800980 unsigned int out_len = 0;
981 unsigned int out_avai_len = 0;
982 unsigned char *out_addr = NULL;
983 uint32_t dtb_offset = 0;
984 unsigned char *kernel_start_addr = NULL;
985 unsigned int kernel_size = 0;
986 int rc;
987
Neeti Desai465491e2012-07-31 12:53:35 -0700988#if DEVICE_TREE
989 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700990 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700991 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800992 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700993 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -0400994 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -0700995#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +0800996 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800997
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530998 if (check_format_bit())
999 boot_into_recovery = 1;
1000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001001 if (!boot_into_recovery) {
1002 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1003 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1004 if (rcode <= 0) {
1005 boot_into_ffbm = false;
1006 if (rcode < 0)
1007 dprintf(CRITICAL,"failed to get ffbm cookie");
1008 } else
1009 boot_into_ffbm = true;
1010 } else
1011 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001012 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1013 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1014 dprintf(INFO, "Unified boot method!\n");
1015 hdr = uhdr;
1016 goto unified_boot;
1017 }
Greg Griscod6250552011-06-29 14:40:23 -07001018 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001019 index = partition_get_index("boot");
1020 ptn = partition_get_offset(index);
1021 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001022 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1023 return -1;
1024 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001025 }
1026 else {
1027 index = partition_get_index("recovery");
1028 ptn = partition_get_offset(index);
1029 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001030 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1031 return -1;
1032 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001033 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001034 /* Set Lun for boot & recovery partitions */
1035 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001036
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301037 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001038 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1039 return -1;
1040 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001041
1042 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001043 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001044 return -1;
1045 }
1046
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001047 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301048
1049 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1050 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1051 return -1;
1052 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001053 page_size = hdr->page_size;
1054 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001055 }
1056
Matthew Qin49e51fa2015-02-09 10:40:45 +08001057 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1058 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001059
Matthew Qin49e51fa2015-02-09 10:40:45 +08001060 image_addr = (unsigned char *)target_get_scratch_address();
1061
1062#if DEVICE_TREE
1063 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1064 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1065#else
1066 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1067#endif
1068
1069#if VERIFIED_BOOT
1070 boot_verifier_init();
1071#endif
1072
1073 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
1074 {
1075 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1076 return -1;
1077 }
1078
Matthew Qinbb7923d2015-02-09 10:56:09 +08001079 /*
1080 * Update loading flow of bootimage to support compressed/uncompressed
1081 * bootimage on both 64bit and 32bit platform.
1082 * 1. Load bootimage from emmc partition onto DDR.
1083 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1084 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1085 * platform accordingly.
1086 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1087 */
1088
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001089 dprintf(INFO, "Loading (%s) image (%d): start\n",
1090 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001091 bs_set_timestamp(BS_KERNEL_LOAD_START);
1092
Matthew Qinbb7923d2015-02-09 10:56:09 +08001093 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001094 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1095 {
1096 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1097 return -1;
1098 }
1099
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001100 dprintf(INFO, "Loading (%s) image (%d): done\n",
1101 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1102
Matthew Qin49e51fa2015-02-09 10:40:45 +08001103 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1104
1105 /* Authenticate Kernel */
1106 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1107 (int) target_use_signed_kernel(),
1108 device.is_unlocked,
1109 device.is_tampered);
1110
1111 if(target_use_signed_kernel() && (!device.is_unlocked))
1112 {
1113 offset = imagesize_actual;
1114 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1115 {
1116 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1117 return -1;
1118 }
1119
1120 /* Read signature */
1121 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1122 {
1123 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1124 return -1;
1125 }
1126
1127 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
1128 } else {
1129 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1130 #ifdef TZ_SAVE_KERNEL_HASH
1131 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1132 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001133
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001134#if VERIFIED_BOOT
1135 if(boot_verify_get_state() == ORANGE)
1136 {
lijuanga40d6302015-07-20 20:10:13 +08001137#if FBCON_DISPLAY_MSG
1138 display_menu_thread(DISPLAY_THREAD_BOOT_STATE);
1139 wait_for_users_action();
1140#else
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001141 dprintf(CRITICAL,
lijuanga40d6302015-07-20 20:10:13 +08001142 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001143 mdelay(5000);
lijuanga40d6302015-07-20 20:10:13 +08001144#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001145 }
1146#endif
1147
Amit Blay4aa292f2015-04-28 21:55:59 +03001148#ifdef MDTP_SUPPORT
1149 {
1150 /* Verify MDTP lock.
1151 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1152 * since verification was skipped in aboot. The signature is not part of the loaded image.
1153 */
1154 mdtp_ext_partition_verification_t ext_partition;
1155 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1156 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1157 ext_partition.page_size = page_size;
1158 ext_partition.image_addr = (uint32)image_addr;
1159 ext_partition.image_size = imagesize_actual;
1160 ext_partition.sig_avail = FALSE;
1161 mdtp_fwlock_verify_lock(&ext_partition);
1162 }
1163#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001164 }
1165
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001166#if VERIFIED_BOOT
1167 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001168 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001169 ASSERT(0);
1170#endif
1171
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001172 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001173 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1174 * If not, continue booting.
1175 */
1176 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1177 {
1178 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1179 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001180 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001181 rc = decompress((unsigned char *)(image_addr + page_size),
1182 hdr->kernel_size, out_addr, out_avai_len,
1183 &dtb_offset, &out_len);
1184 if (rc)
1185 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001186 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001187 ASSERT(0);
1188 }
1189
Matthew Qin0b15b322015-05-19 05:20:54 -04001190 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001191 kptr = (struct kernel64_hdr *)out_addr;
1192 kernel_start_addr = out_addr;
1193 kernel_size = out_len;
1194 } else {
1195 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1196 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1197 kernel_size = hdr->kernel_size;
1198 }
1199
1200 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001201 * Update the kernel/ramdisk/tags address if the boot image header
1202 * has default values, these default values come from mkbootimg when
1203 * the boot image is flashed using fastboot flash:raw
1204 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001205 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001206
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001207 /* Get virtual addresses since the hdr saves physical addresses. */
1208 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1209 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1210 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001211
Matthew Qinbb7923d2015-02-09 10:56:09 +08001212 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001213 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001214 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001215 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1216 {
1217 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1218 return -1;
1219 }
1220
1221#ifndef DEVICE_TREE
1222 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1223 {
1224 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1225 return -1;
1226 }
1227#endif
1228
Matthew Qin49e51fa2015-02-09 10:40:45 +08001229 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001230 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001231 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001232
Matthew Qin49e51fa2015-02-09 10:40:45 +08001233 #if DEVICE_TREE
1234 if(hdr->dt_size) {
1235 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1236 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001237
Matthew Qin49e51fa2015-02-09 10:40:45 +08001238 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1239 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1240 return -1;
1241 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001242
Matthew Qin49e51fa2015-02-09 10:40:45 +08001243 /* Find index of device tree within device tree table */
1244 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1245 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1246 return -1;
1247 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001248
Matthew Qin271927e2015-03-31 22:07:07 -04001249 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1250 {
1251 unsigned int compressed_size = 0;
1252 out_addr += out_len;
1253 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001254 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001255 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1256 dt_entry.size, out_addr, out_avai_len,
1257 &compressed_size, &dtb_size);
1258 if (rc)
1259 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001260 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001261 ASSERT(0);
1262 }
1263
Matthew Qin0b15b322015-05-19 05:20:54 -04001264 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001265 best_match_dt_addr = out_addr;
1266 } else {
1267 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1268 dtb_size = dt_entry.size;
1269 }
1270
Matthew Qin49e51fa2015-02-09 10:40:45 +08001271 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001272 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001273 {
1274 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1275 return -1;
1276 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001277
Matthew Qin271927e2015-03-31 22:07:07 -04001278 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001279 } else {
1280 /* Validate the tags_addr */
1281 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001282 {
1283 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1284 return -1;
1285 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001286 /*
1287 * If appended dev tree is found, update the atags with
1288 * memory address to the DTB appended location on RAM.
1289 * Else update with the atags address in the kernel header
1290 */
1291 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001292 dtb = dev_tree_appended((void*)(image_addr + page_size),
1293 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001294 (void *)hdr->tags_addr);
1295 if (!dtb) {
1296 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001297 return -1;
1298 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001299 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001300 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001301
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001302 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1303 target_load_ssd_keystore();
1304
Shashank Mittal23b8f422010-04-16 19:27:21 -07001305unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001306
Dima Zavin77e41f32013-03-06 16:10:43 -08001307 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001308 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001309 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1310
1311 return 0;
1312}
1313
Dima Zavin214cc642009-01-26 11:16:21 -08001314int boot_linux_from_flash(void)
1315{
1316 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001317 struct ptentry *ptn;
1318 struct ptable *ptable;
1319 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001320
Shashank Mittalcd98d472011-08-02 14:29:24 -07001321 unsigned char *image_addr = 0;
1322 unsigned kernel_actual;
1323 unsigned ramdisk_actual;
1324 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001325 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001326
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001327#if DEVICE_TREE
1328 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001329 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001330 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001331 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001332#endif
1333
David Ng183a7422009-12-07 14:55:21 -08001334 if (target_is_emmc_boot()) {
1335 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1336 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1337 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1338 return -1;
1339 }
1340 goto continue_boot;
1341 }
1342
Dima Zavin214cc642009-01-26 11:16:21 -08001343 ptable = flash_get_ptable();
1344 if (ptable == NULL) {
1345 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1346 return -1;
1347 }
1348
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001349 if(!boot_into_recovery)
1350 {
1351 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001352
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001353 if (ptn == NULL) {
1354 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1355 return -1;
1356 }
1357 }
1358 else
1359 {
1360 ptn = ptable_find(ptable, "recovery");
1361 if (ptn == NULL) {
1362 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1363 return -1;
1364 }
Dima Zavin214cc642009-01-26 11:16:21 -08001365 }
1366
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001367 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001368 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1369 return -1;
1370 }
Dima Zavin214cc642009-01-26 11:16:21 -08001371
1372 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001373 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001374 return -1;
1375 }
1376
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001377 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001378 dprintf(CRITICAL, "ERROR: Invalid boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001379 return -1;
1380 }
1381
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001382 /*
1383 * Update the kernel/ramdisk/tags address if the boot image header
1384 * has default values, these default values come from mkbootimg when
1385 * the boot image is flashed using fastboot flash:raw
1386 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001387 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001388
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001389 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001390 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1391 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1392 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1393
1394 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1395 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1396
1397 /* Check if the addresses in the header are valid. */
1398 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1399 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1400 {
1401 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1402 return -1;
1403 }
1404
1405#ifndef DEVICE_TREE
1406 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1407 {
1408 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1409 return -1;
1410 }
1411#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001412
Shashank Mittalcd98d472011-08-02 14:29:24 -07001413 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001414 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001415 {
1416 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001417 offset = 0;
1418
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001419#if DEVICE_TREE
1420 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1421 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001422
1423 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1424 {
1425 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1426 return -1;
1427 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001428#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001429 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001430#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001431
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001432 dprintf(INFO, "Loading (%s) image (%d): start\n",
1433 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001434 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001435
Shashank Mittalcd98d472011-08-02 14:29:24 -07001436 /* Read image without signature */
1437 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1438 {
1439 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1440 return -1;
1441 }
Dima Zavin214cc642009-01-26 11:16:21 -08001442
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001443 dprintf(INFO, "Loading (%s) image (%d): done\n",
1444 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001445 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001446
Shashank Mittalcd98d472011-08-02 14:29:24 -07001447 offset = imagesize_actual;
1448 /* Read signature */
1449 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1450 {
1451 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001452 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001453 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001454
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301455 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001456
1457 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001458 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1459 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001460#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001461 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001462 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001463 {
1464 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1465 return -1;
1466 }
1467
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001468 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1469#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001470
1471 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001472 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001473 {
1474 write_device_info_flash(&device);
1475 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301476#if USE_PCOM_SECBOOT
1477 set_tamper_flag(device.is_tampered);
1478#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001479 }
1480 else
1481 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001482 offset = page_size;
1483
Amol Jadib6be5c12012-11-14 13:39:51 -08001484 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1485 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1486 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1487
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001488 dprintf(INFO, "Loading (%s) image (%d): start\n",
1489 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1490
Amol Jadi492d5a52013-03-15 16:12:34 -07001491 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001492
1493 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001494 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1495 return -1;
1496 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001497 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001498
Amol Jadib6be5c12012-11-14 13:39:51 -08001499 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001500 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1501 return -1;
1502 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001503 offset += ramdisk_actual;
1504
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001505 dprintf(INFO, "Loading (%s) image (%d): done\n",
1506 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1507
Amol Jadi492d5a52013-03-15 16:12:34 -07001508 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001509
1510 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001511 offset += second_actual;
1512 /* Second image loading not implemented. */
1513 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001514 }
1515
1516#if DEVICE_TREE
1517 if(hdr->dt_size != 0) {
1518
1519 /* Read the device tree table into buffer */
1520 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1521 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1522 return -1;
1523 }
1524
1525 table = (struct dt_table*) dt_buf;
1526
Deepa Dinamani19648b42013-09-05 17:05:55 -07001527 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001528 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1529 return -1;
1530 }
1531
Deepa Dinamani19648b42013-09-05 17:05:55 -07001532 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1533 if (!table)
1534 return -1;
1535
1536 /* Read the entire device tree table into buffer */
1537 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1538 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1539 return -1;
1540 }
1541
1542
Joel Kingaa335dc2013-06-03 16:11:08 -07001543 /* Find index of device tree within device tree table */
1544 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001545 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1546 return -1;
1547 }
1548
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001549 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001550 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001551 {
1552 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1553 return -1;
1554 }
1555
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001556 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001557 if(flash_read(ptn, offset + dt_entry.offset,
1558 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001559 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1560 return -1;
1561 }
1562 }
1563#endif
1564
Shashank Mittalcd98d472011-08-02 14:29:24 -07001565 }
David Ng183a7422009-12-07 14:55:21 -08001566continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001567
Dima Zavin214cc642009-01-26 11:16:21 -08001568 /* TODO: create/pass atags to kernel */
1569
Ajay Dudanie28a6072011-07-01 13:59:46 -07001570 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001571 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001572 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1573
1574 return 0;
1575}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001576
Shashank Mittal162244e2011-08-08 19:01:25 -07001577void write_device_info_mmc(device_info *dev)
1578{
Shashank Mittal162244e2011-08-08 19:01:25 -07001579 unsigned long long ptn = 0;
1580 unsigned long long size;
1581 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001582 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001583 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001584 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001585
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001586 if (devinfo_present)
1587 index = partition_get_index("devinfo");
1588 else
1589 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001590
Shashank Mittal162244e2011-08-08 19:01:25 -07001591 ptn = partition_get_offset(index);
1592 if(ptn == 0)
1593 {
1594 return;
1595 }
1596
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001597 lun = partition_get_lun(index);
1598 mmc_set_lun(lun);
1599
Shashank Mittal162244e2011-08-08 19:01:25 -07001600 size = partition_get_size(index);
1601
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001602 blocksize = mmc_get_device_blocksize();
1603
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001604 if (devinfo_present)
1605 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1606 else
1607 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1608 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001609 {
1610 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001611 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001612 }
1613}
1614
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001615void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001616{
Shashank Mittal162244e2011-08-08 19:01:25 -07001617 unsigned long long ptn = 0;
1618 unsigned long long size;
1619 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001620 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001621 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001622
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001623 if ((index = partition_get_index("devinfo")) < 0)
1624 {
1625 devinfo_present = false;
1626 index = partition_get_index("aboot");
1627 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001628
Shashank Mittal162244e2011-08-08 19:01:25 -07001629 ptn = partition_get_offset(index);
1630 if(ptn == 0)
1631 {
1632 return;
1633 }
1634
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001635 mmc_set_lun(partition_get_lun(index));
1636
Shashank Mittal162244e2011-08-08 19:01:25 -07001637 size = partition_get_size(index);
1638
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001639 blocksize = mmc_get_device_blocksize();
1640
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001641 if (devinfo_present)
1642 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1643 else
1644 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1645 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001646 {
1647 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001648 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001649 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001650}
1651
1652void write_device_info_flash(device_info *dev)
1653{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001654 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001655 struct ptentry *ptn;
1656 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001657 if(info == NULL)
1658 {
1659 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1660 ASSERT(0);
1661 }
1662 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001663 ptable = flash_get_ptable();
1664 if (ptable == NULL)
1665 {
1666 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1667 return;
1668 }
1669
1670 ptn = ptable_find(ptable, "devinfo");
1671 if (ptn == NULL)
1672 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001673 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001674 return;
1675 }
1676
1677 memcpy(info, dev, sizeof(device_info));
1678
1679 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1680 {
1681 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1682 return;
1683 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001684 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001685}
1686
vijay kumarc65876c2015-04-24 13:29:16 +05301687static int read_allow_oem_unlock(device_info *dev)
1688{
vijay kumarc65876c2015-04-24 13:29:16 +05301689 unsigned offset;
1690 int index;
1691 unsigned long long ptn;
1692 unsigned long long ptn_size;
1693 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001694 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301695
vijay kumarca2e6812015-07-08 20:28:25 +05301696 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301697 if (index == INVALID_PTN)
1698 {
vijay kumarca2e6812015-07-08 20:28:25 +05301699 index = partition_get_index(frp_ptns[1]);
1700 if (index == INVALID_PTN)
1701 {
1702 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1703 return -1;
1704 }
vijay kumarc65876c2015-04-24 13:29:16 +05301705 }
1706
1707 ptn = partition_get_offset(index);
1708 ptn_size = partition_get_size(index);
1709 offset = ptn_size - blocksize;
1710
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001711 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301712 {
1713 dprintf(CRITICAL, "Reading MMC failed\n");
1714 return -1;
1715 }
1716
1717 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1718 is_allow_unlock = buf[blocksize-1] & 0x01;
1719 return 0;
1720}
1721
1722static int write_allow_oem_unlock(bool allow_unlock)
1723{
vijay kumarc65876c2015-04-24 13:29:16 +05301724 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301725 int index;
1726 unsigned long long ptn;
1727 unsigned long long ptn_size;
1728 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001729 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301730
vijay kumarca2e6812015-07-08 20:28:25 +05301731 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301732 if (index == INVALID_PTN)
1733 {
vijay kumarca2e6812015-07-08 20:28:25 +05301734 index = partition_get_index(frp_ptns[1]);
1735 if (index == INVALID_PTN)
1736 {
1737 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1738 return -1;
1739 }
vijay kumarc65876c2015-04-24 13:29:16 +05301740 }
1741
1742 ptn = partition_get_offset(index);
1743 ptn_size = partition_get_size(index);
1744 offset = ptn_size - blocksize;
1745
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001746 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301747 {
1748 dprintf(CRITICAL, "Reading MMC failed\n");
1749 return -1;
1750 }
1751
1752 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1753 buf[blocksize-1] = allow_unlock;
1754 if (mmc_write(ptn + offset, blocksize, buf))
1755 {
1756 dprintf(CRITICAL, "Writing MMC failed\n");
1757 return -1;
1758 }
1759
1760 return 0;
1761}
1762
Shashank Mittal162244e2011-08-08 19:01:25 -07001763void read_device_info_flash(device_info *dev)
1764{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001765 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001766 struct ptentry *ptn;
1767 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001768 if(info == NULL)
1769 {
1770 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1771 ASSERT(0);
1772 }
1773 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001774 ptable = flash_get_ptable();
1775 if (ptable == NULL)
1776 {
1777 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1778 return;
1779 }
1780
1781 ptn = ptable_find(ptable, "devinfo");
1782 if (ptn == NULL)
1783 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001784 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001785 return;
1786 }
1787
1788 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1789 {
1790 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1791 return;
1792 }
1793
1794 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1795 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001796 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1797 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001798 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001799 write_device_info_flash(info);
1800 }
1801 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001802 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001803}
1804
1805void write_device_info(device_info *dev)
1806{
1807 if(target_is_emmc_boot())
1808 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001809 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1810 if(info == NULL)
1811 {
1812 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1813 ASSERT(0);
1814 }
1815 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001816 memcpy(info, dev, sizeof(struct device_info));
1817
1818#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001819 if (is_secure_boot_enable()) {
1820 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1821 ASSERT(0);
1822 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001823 else
1824 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001825#else
1826 write_device_info_mmc(info);
1827#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001828 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001829 }
1830 else
1831 {
1832 write_device_info_flash(dev);
1833 }
1834}
1835
1836void read_device_info(device_info *dev)
1837{
1838 if(target_is_emmc_boot())
1839 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001840 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1841 if(info == NULL)
1842 {
1843 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1844 ASSERT(0);
1845 }
1846 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001847
1848#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001849 if (is_secure_boot_enable()) {
1850 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1851 ASSERT(0);
1852 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001853 else
1854 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001855#else
1856 read_device_info_mmc(info);
1857#endif
1858
1859 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1860 {
1861 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08001862 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001863 info->is_unlocked = 0;
lijuang511a2b52015-08-14 20:50:51 +08001864 info->is_unlock_critical = 0;
1865 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001866 info->is_unlocked = 1;
lijuang511a2b52015-08-14 20:50:51 +08001867 info->is_unlock_critical = 1;
1868 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001869 info->is_tampered = 0;
1870 info->charger_screen_enabled = 0;
Sridhar Parasuram287e5062015-08-24 16:17:22 -07001871 info->verity_mode = 1; //enforcing by default
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001872 write_device_info(info);
1873 }
1874 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001875 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001876 }
1877 else
1878 {
1879 read_device_info_flash(dev);
1880 }
1881}
1882
1883void reset_device_info()
1884{
1885 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001886 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001887 write_device_info(&device);
1888}
1889
1890void set_device_root()
1891{
1892 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001893 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001894 write_device_info(&device);
1895}
1896
lijuang21f12f52015-08-22 16:22:19 +08001897void set_oem_unlock()
1898{
1899 if(!device.is_unlocked) {
1900 if(!is_allow_unlock) {
1901 fastboot_fail("oem unlock is not allowed");
1902 return;
1903 }
1904
1905 device.is_unlocked = 1;
1906 write_device_info(&device);
1907 }
1908}
1909
lijuang511a2b52015-08-14 20:50:51 +08001910static bool critical_flash_allowed(const char * entry)
1911{
1912 uint32_t i = 0;
1913 if (entry == NULL)
1914 return false;
1915
1916 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
1917 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
1918 return true;
1919 }
1920 return false;
1921}
1922
Amol Jadicb524072012-08-09 16:40:18 -07001923#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001924int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
Amol Jadicb524072012-08-09 16:40:18 -07001925{
1926 uint32 dt_image_offset = 0;
1927 uint32_t n;
1928 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001929 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001930 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001931 unsigned int compressed_size = 0;
1932 unsigned int dtb_size = 0;
1933 unsigned int out_avai_len = 0;
1934 unsigned char *out_addr = NULL;
1935 unsigned char *best_match_dt_addr = NULL;
1936 int rc;
Amol Jadicb524072012-08-09 16:40:18 -07001937
1938 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1939
Amol Jadicb524072012-08-09 16:40:18 -07001940 if(hdr->dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001941 /* add kernel offset */
1942 dt_image_offset += page_size;
1943 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1944 dt_image_offset += n;
1945
1946 /* add ramdisk offset */
1947 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1948 dt_image_offset += n;
1949
1950 /* add second offset */
1951 if(hdr->second_size != 0) {
1952 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1953 dt_image_offset += n;
1954 }
1955
1956 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001957 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001958
Deepa Dinamani19648b42013-09-05 17:05:55 -07001959 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001960 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1961 return -1;
1962 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001963 /* Find index of device tree within device tree table */
1964 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001965 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1966 return -1;
1967 }
1968
Matthew Qin271927e2015-03-31 22:07:07 -04001969 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
1970 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
1971 {
1972 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
1973 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04001974 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001975 rc = decompress(best_match_dt_addr,
1976 dt_entry.size, out_addr, out_avai_len,
1977 &compressed_size, &dtb_size);
1978 if (rc)
1979 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001980 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001981 ASSERT(0);
1982 }
1983
Matthew Qin0b15b322015-05-19 05:20:54 -04001984 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001985 best_match_dt_addr = out_addr;
1986 } else {
1987 dtb_size = dt_entry.size;
1988 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001989 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001990 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001991 {
1992 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1993 return -1;
1994 }
1995
Amol Jadicb524072012-08-09 16:40:18 -07001996 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001997 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001998 } else
1999 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002000
2001 /* Everything looks fine. Return success. */
2002 return 0;
2003}
2004#endif
2005
Brian Swetland9c4c0752009-01-25 16:23:50 -08002006void cmd_boot(const char *arg, void *data, unsigned sz)
2007{
Amit Blay8a510302015-08-17 09:20:01 +03002008#ifdef MDTP_SUPPORT
2009 static bool is_mdtp_activated = 0;
2010#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002011 unsigned kernel_actual;
2012 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002013 uint32_t image_actual;
2014 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002015 uint32_t sig_actual = SIGNATURE_SIZE;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002016 struct boot_img_hdr *hdr = NULL;
2017 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002018 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002019 int ret = 0;
2020 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002021 unsigned int out_len = 0;
2022 unsigned int out_avai_len = 0;
2023 unsigned char *out_addr = NULL;
2024 uint32_t dtb_offset = 0;
2025 unsigned char *kernel_start_addr = NULL;
2026 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002027 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002028
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002029#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002030 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002031 {
2032 fastboot_fail("unlock device to use this command");
2033 return;
2034 }
2035#endif
2036
Brian Swetland9c4c0752009-01-25 16:23:50 -08002037 if (sz < sizeof(hdr)) {
2038 fastboot_fail("invalid bootimage header");
2039 return;
2040 }
2041
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002042 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002043
2044 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002045 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002046
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002047 if(target_is_emmc_boot() && hdr->page_size) {
2048 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002049 page_mask = page_size - 1;
2050 }
2051
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002052 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2053 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002054#if DEVICE_TREE
2055 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2056#endif
2057
2058 image_actual = ADD_OF(page_size, kernel_actual);
2059 image_actual = ADD_OF(image_actual, ramdisk_actual);
2060 image_actual = ADD_OF(image_actual, dt_actual);
2061
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002062 if (target_use_signed_kernel() && (!device.is_unlocked))
2063 image_actual = ADD_OF(image_actual, sig_actual);
2064
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002065 /* sz should have atleast raw boot image */
2066 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002067 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002068 return;
2069 }
2070
2071 /* Verify the boot image
2072 * device & page_size are initialized in aboot_init
2073 */
2074 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002075 /* Pass size excluding signature size, otherwise we would try to
2076 * access signature beyond its length
2077 */
2078 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002079
Amit Blay4aa292f2015-04-28 21:55:59 +03002080#ifdef MDTP_SUPPORT
2081 else
2082 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002083 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002084 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002085
2086 if (!is_mdtp_activated) {
2087 ext_partition.partition = MDTP_PARTITION_NONE;
2088 mdtp_fwlock_verify_lock(&ext_partition);
2089 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002090 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002091
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002092 mdtp_activated(&is_mdtp_activated);
2093 if(is_mdtp_activated){
2094 dprintf(CRITICAL, "fastboot boot command is not available.\n");
2095 return;
2096 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002097#endif /* MDTP_SUPPORT */
2098
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002099 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002100 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2101 * If not, continue booting.
2102 */
2103 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2104 {
2105 out_addr = (unsigned char *)target_get_scratch_address();
2106 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2107 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002108 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002109 ret = decompress((unsigned char *)(ptr + page_size),
2110 hdr->kernel_size, out_addr, out_avai_len,
2111 &dtb_offset, &out_len);
2112 if (ret)
2113 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002114 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002115 ASSERT(0);
2116 }
2117
Matthew Qin0b15b322015-05-19 05:20:54 -04002118 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002119 kptr = (struct kernel64_hdr *)out_addr;
2120 kernel_start_addr = out_addr;
2121 kernel_size = out_len;
2122 } else {
2123 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2124 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2125 kernel_size = hdr->kernel_size;
2126 }
2127
2128 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002129 * Update the kernel/ramdisk/tags address if the boot image header
2130 * has default values, these default values come from mkbootimg when
2131 * the boot image is flashed using fastboot flash:raw
2132 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002133 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002134
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002135 /* Get virtual addresses since the hdr saves physical addresses. */
2136 hdr->kernel_addr = VA(hdr->kernel_addr);
2137 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2138 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002139
Matthew Qinbb7923d2015-02-09 10:56:09 +08002140 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002141 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002142 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002143 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2144 {
2145 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002146 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002147 }
2148
Amol Jadicb524072012-08-09 16:40:18 -07002149#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002150 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002151 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002152 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002153
2154 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002155#else
2156 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2157 {
2158 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002159 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002160 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002161#endif
2162
2163 /* Load ramdisk & kernel */
2164 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002165 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002166
2167#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002168 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2169 {
2170 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2171 return;
2172 }
2173
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002174 /*
2175 * If dtb is not found look for appended DTB in the kernel.
2176 * If appended dev tree is found, update the atags with
2177 * memory address to the DTB appended location on RAM.
2178 * Else update with the atags address in the kernel header
2179 */
2180 if (!dtb_copied) {
2181 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002182 dtb = dev_tree_appended((void*)(ptr + page_size),
2183 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002184 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002185 if (!dtb) {
2186 fastboot_fail("dtb not found");
2187 return;
2188 }
Amol Jadicb524072012-08-09 16:40:18 -07002189 }
2190#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002191
2192 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002193 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002194
Dima Zavin77e41f32013-03-06 16:10:43 -08002195 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002196 (const char*) hdr->cmdline, board_machtype(),
2197 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002198}
2199
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002200void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002201{
2202 struct ptentry *ptn;
2203 struct ptable *ptable;
2204
2205 ptable = flash_get_ptable();
2206 if (ptable == NULL) {
2207 fastboot_fail("partition table doesn't exist");
2208 return;
2209 }
2210
2211 ptn = ptable_find(ptable, arg);
2212 if (ptn == NULL) {
2213 fastboot_fail("unknown partition name");
2214 return;
2215 }
2216
2217 if (flash_erase(ptn)) {
2218 fastboot_fail("failed to erase partition");
2219 return;
2220 }
2221 fastboot_okay("");
2222}
2223
Bikas Gurungd48bd242010-09-04 19:54:32 -07002224
2225void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2226{
2227 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002228 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002229 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002230 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002231
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002232#if VERIFIED_BOOT
2233 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2234 {
2235 if(!device.is_unlocked)
2236 {
2237 fastboot_fail("unlock device to erase keystore");
2238 return;
2239 }
2240 }
2241#endif
2242
Kinson Chikf1a43512011-07-14 11:28:39 -07002243 index = partition_get_index(arg);
2244 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002245 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002246
Kinson Chikf1a43512011-07-14 11:28:39 -07002247 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002248 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002249 return;
2250 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002251
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002252 lun = partition_get_lun(index);
2253 mmc_set_lun(lun);
2254
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002255 if (platform_boot_dev_isemmc())
2256 {
2257 if (mmc_erase_card(ptn, size)) {
2258 fastboot_fail("failed to erase partition\n");
2259 return;
2260 }
2261 } else {
2262 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2263 size = partition_get_size(index);
2264 if (size > DEFAULT_ERASE_SIZE)
2265 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002266
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002267 /* Simple inefficient version of erase. Just writing
2268 0 in first several blocks */
2269 if (mmc_write(ptn , size, (unsigned int *)out)) {
2270 fastboot_fail("failed to erase partition");
2271 return;
2272 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002273 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002274#if VERIFIED_BOOT
2275 if(!(strncmp(arg, "userdata", 8)))
2276 if(send_delete_keys_to_tz())
2277 ASSERT(0);
2278#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002279 fastboot_okay("");
2280}
2281
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002282void cmd_erase(const char *arg, void *data, unsigned sz)
2283{
lijuang65c5a822015-08-29 16:35:36 +08002284#if CHECK_BAT_VOLTAGE
2285 if (!target_battery_soc_ok()) {
2286 fastboot_fail("Warning: battery's capacity is very low\n");
2287 return;
2288 }
2289#endif
2290
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002291#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002292 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002293 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002294 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002295 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002296 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002297 return;
2298 }
2299 }
2300#endif
2301
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002302 if(target_is_emmc_boot())
2303 cmd_erase_mmc(arg, data, sz);
2304 else
2305 cmd_erase_nand(arg, data, sz);
2306}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002307
Ajay Dudani5c761132011-04-07 20:19:04 -07002308void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002309{
2310 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002311 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002312 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002313 char *token = NULL;
2314 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002315 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002316 uint8_t lun = 0;
2317 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002318
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002319 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002320 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002321 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002322 if(token)
2323 {
2324 lun = atoi(token);
2325 mmc_set_lun(lun);
2326 lun_set = true;
2327 }
2328
Mao Jinlong226f33a2014-07-04 17:24:10 +08002329 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002330 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002331 if (!strcmp(pname, "partition"))
2332 {
2333 dprintf(INFO, "Attempt to write partition image.\n");
2334 if (write_partition(sz, (unsigned char *) data)) {
2335 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002336 return;
2337 }
2338 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002339 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002340 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002341#if VERIFIED_BOOT
2342 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2343 {
2344 if(!device.is_unlocked)
2345 {
2346 fastboot_fail("unlock device to flash keystore");
2347 return;
2348 }
2349 if(!boot_verify_validate_keystore((unsigned char *)data))
2350 {
2351 fastboot_fail("image is not a keystore file");
2352 return;
2353 }
2354 }
2355#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002356 index = partition_get_index(pname);
2357 ptn = partition_get_offset(index);
2358 if(ptn == 0) {
2359 fastboot_fail("partition table doesn't exist");
2360 return;
2361 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002362
Mao Jinlong226f33a2014-07-04 17:24:10 +08002363 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2364 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2365 fastboot_fail("image is not a boot image");
2366 return;
2367 }
2368 }
2369
2370 if(!lun_set)
2371 {
2372 lun = partition_get_lun(index);
2373 mmc_set_lun(lun);
2374 }
2375
2376 size = partition_get_size(index);
2377 if (ROUND_TO_PAGE(sz,511) > size) {
2378 fastboot_fail("size too large");
2379 return;
2380 }
2381 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2382 fastboot_fail("flash write failure");
2383 return;
2384 }
Greg Grisco6e754772011-06-23 12:19:39 -07002385 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002386 }
2387 fastboot_okay("");
2388 return;
2389}
2390
Ajay Dudanide984792015-03-02 09:57:41 -08002391void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2392{
2393 int i, images;
2394 meta_header_t *meta_header;
2395 img_header_entry_t *img_header_entry;
2396
2397 meta_header = (meta_header_t*) data;
2398 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2399
2400 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2401
2402 for (i=0; i<images; i++) {
2403
2404 if((img_header_entry[i].ptn_name == NULL) ||
2405 (img_header_entry[i].start_offset == 0) ||
2406 (img_header_entry[i].size == 0))
2407 break;
2408
2409 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2410 (void *) data + img_header_entry[i].start_offset,
2411 img_header_entry[i].size);
2412 }
2413
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002414 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2415 {
2416 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2417 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2418 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2419 }
2420 else
2421 {
2422 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2423 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2424 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2425 }
2426
2427 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002428 fastboot_okay("");
2429 return;
2430}
2431
Ajay Dudani5c761132011-04-07 20:19:04 -07002432void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2433{
2434 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002435 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002436 uint32_t *fill_buf = NULL;
2437 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002438 sparse_header_t *sparse_header;
2439 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002440 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002441 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302442 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002443 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302444 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002445 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302446 /*End of the sparse image address*/
2447 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002448
Kinson Chikf1a43512011-07-14 11:28:39 -07002449 index = partition_get_index(arg);
2450 ptn = partition_get_offset(index);
2451 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002452 fastboot_fail("partition table doesn't exist");
2453 return;
2454 }
2455
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302456 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302457
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002458 lun = partition_get_lun(index);
2459 mmc_set_lun(lun);
2460
vijay kumar800255e2015-04-24 20:26:19 +05302461 if (sz < sizeof(sparse_header_t)) {
2462 fastboot_fail("size too low");
2463 return;
2464 }
2465
Ajay Dudani5c761132011-04-07 20:19:04 -07002466 /* Read and skip over sparse image header */
2467 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302468
vijay kumar1321f342015-03-27 12:13:42 +05302469 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002470 fastboot_fail("size too large");
2471 return;
2472 }
2473
vijay kumarde4fcf62015-04-23 13:05:49 +05302474 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302475
2476 if (data_end < (uint32_t)data) {
2477 fastboot_fail("buffer overreads occured due to invalid sparse header");
2478 return;
2479 }
2480
vijay kumarde4fcf62015-04-23 13:05:49 +05302481 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002482 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302483 fastboot_fail("sparse header size mismatch");
2484 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002485 }
2486
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002487 dprintf (SPEW, "=== Sparse Image Header ===\n");
2488 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2489 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2490 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2491 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2492 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2493 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2494 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2495 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002496
2497 /* Start processing chunks */
2498 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2499 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302500 /* Make sure the total image size does not exceed the partition size */
2501 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2502 fastboot_fail("size too large");
2503 return;
2504 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002505 /* Read and skip over chunk header */
2506 chunk_header = (chunk_header_t *) data;
2507 data += sizeof(chunk_header_t);
2508
vijay kumar800255e2015-04-24 20:26:19 +05302509 if (data_end < (uint32_t)data) {
2510 fastboot_fail("buffer overreads occured due to invalid sparse header");
2511 return;
2512 }
2513
Ajay Dudani5c761132011-04-07 20:19:04 -07002514 dprintf (SPEW, "=== Chunk Header ===\n");
2515 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2516 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2517 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2518
vijay kumar800255e2015-04-24 20:26:19 +05302519 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002520 {
vijay kumar800255e2015-04-24 20:26:19 +05302521 fastboot_fail("chunk header size mismatch");
2522 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002523 }
2524
wufeng.jiang813dc352015-06-02 23:02:46 -04002525 if (!sparse_header->blk_sz ){
2526 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302527 return;
2528 }
2529
wufeng.jiang813dc352015-06-02 23:02:46 -04002530 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2531
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302532 /* Make sure that the chunk size calculated from sparse image does not
2533 * exceed partition size
2534 */
2535 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2536 {
2537 fastboot_fail("Chunk data size exceeds partition size");
2538 return;
2539 }
2540
Ajay Dudani5c761132011-04-07 20:19:04 -07002541 switch (chunk_header->chunk_type)
2542 {
2543 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002544 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002545 chunk_data_sz))
2546 {
2547 fastboot_fail("Bogus chunk size for chunk type Raw");
2548 return;
2549 }
2550
vijay kumar800255e2015-04-24 20:26:19 +05302551 if (data_end < (uint32_t)data + chunk_data_sz) {
2552 fastboot_fail("buffer overreads occured due to invalid sparse header");
2553 return;
2554 }
2555
wufeng.jiang813dc352015-06-02 23:02:46 -04002556 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2557 otherwise it will return in the line above
2558 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002559 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002560 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002561 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002562 {
2563 fastboot_fail("flash write failure");
2564 return;
2565 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302566 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2567 fastboot_fail("Bogus size for RAW chunk type");
2568 return;
2569 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002570 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002571 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002572 break;
2573
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002574 case CHUNK_TYPE_FILL:
2575 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2576 sizeof(uint32_t)))
2577 {
2578 fastboot_fail("Bogus chunk size for chunk type FILL");
2579 return;
2580 }
2581
2582 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2583 if (!fill_buf)
2584 {
2585 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2586 return;
2587 }
2588
vijay kumar800255e2015-04-24 20:26:19 +05302589 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2590 fastboot_fail("buffer overreads occured due to invalid sparse header");
2591 return;
2592 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002593 fill_val = *(uint32_t *)data;
2594 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002595
2596 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2597 {
2598 fill_buf[i] = fill_val;
2599 }
2600
wufeng.jiang813dc352015-06-02 23:02:46 -04002601 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002602 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302603 /* Make sure that the data written to partition does not exceed partition size */
2604 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2605 {
2606 fastboot_fail("Chunk data size for fill type exceeds partition size");
2607 return;
2608 }
2609
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002610 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2611 sparse_header->blk_sz,
2612 fill_buf))
2613 {
2614 fastboot_fail("flash write failure");
2615 free(fill_buf);
2616 return;
2617 }
2618
2619 total_blocks++;
2620 }
2621
2622 free(fill_buf);
2623 break;
2624
Ajay Dudani5c761132011-04-07 20:19:04 -07002625 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302626 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2627 fastboot_fail("bogus size for chunk DONT CARE type");
2628 return;
2629 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002630 total_blocks += chunk_header->chunk_sz;
2631 break;
2632
Ajay Dudani5c761132011-04-07 20:19:04 -07002633 case CHUNK_TYPE_CRC:
2634 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2635 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002636 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002637 return;
2638 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302639 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2640 fastboot_fail("bogus size for chunk CRC type");
2641 return;
2642 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002643 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302644 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2645 fastboot_fail("integer overflow occured");
2646 return;
2647 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002648 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302649 if (data_end < (uint32_t)data) {
2650 fastboot_fail("buffer overreads occured due to invalid sparse header");
2651 return;
2652 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002653 break;
2654
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002655 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002656 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002657 fastboot_fail("Unknown chunk type");
2658 return;
2659 }
2660 }
2661
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002662 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2663 total_blocks, sparse_header->total_blks);
2664
2665 if(total_blocks != sparse_header->total_blks)
2666 {
2667 fastboot_fail("sparse image write failure");
2668 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002669
2670 fastboot_okay("");
2671 return;
2672}
2673
2674void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2675{
2676 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002677 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002678
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002679#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002680 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2681 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002682 int ret=0;
2683 uint32 major_version=0;
2684 uint32 minor_version=0;
2685
2686 ret = scm_svc_version(&major_version,&minor_version);
2687 if(!ret)
2688 {
2689 if(major_version >= 2)
2690 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002691 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002692 {
2693 ret = encrypt_scm((uint32 **) &data, &sz);
2694 if (ret != 0) {
2695 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2696 return;
2697 }
2698
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002699 /* Protect only for SSD */
2700 if (!strcmp(arg, "ssd")) {
2701 ret = scm_protect_keystore((uint32 *) data, sz);
2702 if (ret != 0) {
2703 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2704 return;
2705 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002706 }
2707 }
2708 else
2709 {
2710 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2711 if(ret != 0)
2712 {
2713 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2714 return;
2715 }
2716 }
2717 }
2718 else
2719 {
2720 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2721 magic_number[1] == DECRYPT_MAGIC_1)
2722 {
2723 ret = decrypt_scm((uint32 **) &data, &sz);
2724 if (ret != 0) {
2725 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2726 return;
2727 }
2728 }
2729 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2730 magic_number[1] == ENCRYPT_MAGIC_1)
2731 {
2732 ret = encrypt_scm((uint32 **) &data, &sz);
2733 if (ret != 0) {
2734 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2735 return;
2736 }
2737 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002738 }
2739 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002740 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002741 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002742 dprintf(CRITICAL,"INVALID SVC Version\n");
2743 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002744 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002745#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002746
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002747#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002748 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002749 {
lijuang511a2b52015-08-14 20:50:51 +08002750 /* if device is locked:
2751 * common partition will not allow to be flashed
2752 * critical partition will allow to flash image.
2753 */
2754 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
2755 fastboot_fail("Partition flashing is not allowed");
2756 return;
2757 }
2758
2759 /* if device critical is locked:
2760 * common partition will allow to be flashed
2761 * critical partition will not allow to flash image.
2762 */
2763 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
2764 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002765 return;
2766 }
2767 }
2768#endif
2769
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002770 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002771 meta_header = (meta_header_t *) data;
2772 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002773 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002774 else if (meta_header->magic == META_HEADER_MAGIC)
2775 cmd_flash_meta_img(arg, data, sz);
2776 else
2777 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002778
2779#if VERIFIED_BOOT
2780 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
2781 {
2782 // reset dm_verity mode to enforcing
2783 device.verity_mode = 1;
2784 write_device_info(&device);
2785 }
2786#endif
2787
Ajay Dudani5c761132011-04-07 20:19:04 -07002788 return;
2789}
2790
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002791void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2792{
2793 struct ptentry *sys_ptn;
2794 struct ptable *ptable;
2795
2796 ptable = flash_get_ptable();
2797 if (ptable == NULL) {
2798 fastboot_fail("partition table doesn't exist");
2799 return;
2800 }
2801
2802 sys_ptn = ptable_find(ptable, "system");
2803 if (sys_ptn == NULL) {
2804 fastboot_fail("system partition not found");
2805 return;
2806 }
2807
2808 sz = ROUND_TO_PAGE(sz, page_mask);
2809 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2810 fastboot_fail("update_ubi_vol failed");
2811 else
2812 fastboot_okay("");
2813}
2814
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002815void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002816{
2817 struct ptentry *ptn;
2818 struct ptable *ptable;
2819 unsigned extra = 0;
2820
2821 ptable = flash_get_ptable();
2822 if (ptable == NULL) {
2823 fastboot_fail("partition table doesn't exist");
2824 return;
2825 }
2826
2827 ptn = ptable_find(ptable, arg);
2828 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002829 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2830 arg);
2831 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002832 return;
2833 }
2834
2835 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2836 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2837 fastboot_fail("image is not a boot image");
2838 return;
2839 }
2840 }
2841
Amol Jadi5c61a952012-05-04 17:05:35 -07002842 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002843 || !strcmp(ptn->name, "userdata")
2844 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002845 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002846 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002847 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002848 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002849 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002850
2851 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002852 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2853 if (flash_ubi_img(ptn, data, sz)) {
2854 fastboot_fail("flash write failure");
2855 return;
2856 }
2857 } else {
2858 if (flash_write(ptn, extra, data, sz)) {
2859 fastboot_fail("flash write failure");
2860 return;
2861 }
Dima Zavin214cc642009-01-26 11:16:21 -08002862 }
2863 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2864 fastboot_okay("");
2865}
2866
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002867void cmd_flash(const char *arg, void *data, unsigned sz)
2868{
lijuang65c5a822015-08-29 16:35:36 +08002869#if CHECK_BAT_VOLTAGE
2870 if (!target_battery_soc_ok()) {
2871 fastboot_fail("Warning: battery's capacity is very low\n");
2872 return;
2873 }
2874#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002875 if(target_is_emmc_boot())
2876 cmd_flash_mmc(arg, data, sz);
2877 else
2878 cmd_flash_nand(arg, data, sz);
2879}
2880
Dima Zavin214cc642009-01-26 11:16:21 -08002881void cmd_continue(const char *arg, void *data, unsigned sz)
2882{
2883 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002884 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002885
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002886 if (target_is_emmc_boot())
2887 {
lijuanga40d6302015-07-20 20:10:13 +08002888#if FBCON_DISPLAY_MSG
2889 keys_detect_init();
2890#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002891 boot_linux_from_mmc();
2892 }
2893 else
2894 {
2895 boot_linux_from_flash();
2896 }
Dima Zavin214cc642009-01-26 11:16:21 -08002897}
2898
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002899void cmd_reboot(const char *arg, void *data, unsigned sz)
2900{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002901 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002902 fastboot_okay("");
2903 reboot_device(0);
2904}
2905
2906void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2907{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002908 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002909 fastboot_okay("");
2910 reboot_device(FASTBOOT_MODE);
2911}
2912
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002913void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2914{
2915 dprintf(INFO, "Enabling charger screen check\n");
2916 device.charger_screen_enabled = 1;
2917 write_device_info(&device);
2918 fastboot_okay("");
2919}
2920
2921void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2922{
2923 dprintf(INFO, "Disabling charger screen check\n");
2924 device.charger_screen_enabled = 0;
2925 write_device_info(&device);
2926 fastboot_okay("");
2927}
2928
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302929void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2930{
2931 dprintf(INFO, "Selecting display panel %s\n", arg);
2932 if (arg)
2933 strlcpy(device.display_panel, arg,
2934 sizeof(device.display_panel));
2935 write_device_info(&device);
2936 fastboot_okay("");
2937}
2938
Shashank Mittal162244e2011-08-08 19:01:25 -07002939void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2940{
vijay kumarc65876c2015-04-24 13:29:16 +05302941 if(!is_allow_unlock) {
2942 fastboot_fail("oem unlock is not allowed");
2943 return;
2944 }
2945
lijuanga40d6302015-07-20 20:10:13 +08002946#if FBCON_DISPLAY_MSG
2947 if(!device.is_unlocked)
2948 display_menu_thread(DISPLAY_THREAD_UNLOCK);
2949 else
2950 fastboot_info("Device already unlocked!");
2951 fastboot_okay("");
2952#else
vijay kumarc65876c2015-04-24 13:29:16 +05302953 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
lijuanga40d6302015-07-20 20:10:13 +08002954#endif
vijay kumarc65876c2015-04-24 13:29:16 +05302955}
2956
2957void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
2958{
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002959 if(!device.is_unlocked)
Shashank Mittal162244e2011-08-08 19:01:25 -07002960 {
vijay kumarc65876c2015-04-24 13:29:16 +05302961 if(!is_allow_unlock) {
2962 fastboot_fail("oem unlock is not allowed");
2963 return;
2964 }
2965
Shashank Mittal162244e2011-08-08 19:01:25 -07002966 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002967 write_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05302968
2969 struct recovery_message msg;
2970 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2971 write_misc(0, &msg, sizeof(msg));
2972
2973 fastboot_okay("");
2974 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002975 }
2976 fastboot_okay("");
2977}
2978
2979void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2980{
Sridhar Parasuram8b792422015-07-05 11:38:13 -07002981 struct recovery_message msg;
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002982 if(device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002983 {
2984 device.is_unlocked = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002985 write_device_info(&device);
Sridhar Parasuram8b792422015-07-05 11:38:13 -07002986 // upon oem lock, reboot to recovery to wipe user data
2987 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2988 write_misc(0, &msg, sizeof(msg));
2989 fastboot_okay("");
2990 reboot_device(RECOVERY_MODE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002991 }
2992 fastboot_okay("");
2993}
2994
Shashank Mittala0032282011-08-26 14:50:11 -07002995void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2996{
lijuang511a2b52015-08-14 20:50:51 +08002997 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002998 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002999 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003000 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3001 fastboot_info(response);
lijuang511a2b52015-08-14 20:50:51 +08003002 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3003 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003004 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003005 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303006 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3007 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003008 fastboot_okay("");
3009}
3010
lijuang511a2b52015-08-14 20:50:51 +08003011void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3012{
3013 char response[MAX_RSP_SIZE];
3014 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3015 fastboot_info(response);
3016 fastboot_okay("");
3017}
3018
3019void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3020{
3021 if(device.is_unlock_critical) {
3022 device.is_unlock_critical = 0;
3023 write_device_info(&device);
3024 }
3025 fastboot_okay("");
3026}
3027
3028void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3029{
3030 if(!device.is_unlock_critical)
3031 {
3032 if(!is_allow_unlock) {
3033 fastboot_fail("oem unlock is not allowed");
3034 return;
3035 }
3036
3037 device.is_unlock_critical = 1;
3038 write_device_info(&device);
3039
3040 struct recovery_message msg;
3041 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3042 write_misc(0, &msg, sizeof(msg));
3043
3044 fastboot_okay("");
3045 reboot_device(RECOVERY_MODE);
3046 }
3047 fastboot_okay("");
3048
3049}
3050
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003051void cmd_preflash(const char *arg, void *data, unsigned sz)
3052{
3053 fastboot_okay("");
3054}
3055
Mao Flynn7b379f32015-04-20 00:28:30 +08003056static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303057
Mao Flynn7b379f32015-04-20 00:28:30 +08003058int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303059{
Mao Flynn7b379f32015-04-20 00:28:30 +08003060 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303061 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003062 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303063 return -1;
3064 return 0;
3065}
3066
Mao Flynn7b379f32015-04-20 00:28:30 +08003067int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003068{
3069 struct ptentry *ptn;
3070 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003071 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003072 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003073
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303074 ptable = flash_get_ptable();
3075 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003076 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3077 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303078 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003079
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303080 ptn = ptable_find(ptable, "splash");
3081 if (ptn == NULL) {
3082 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003083 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303084 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003085 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303086 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003087 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303088 }
3089
Mao Flynn7b379f32015-04-20 00:28:30 +08003090 header = (struct logo_img_header *)logo_header;
3091 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303092 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003093 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303094 }
3095
3096 fb_display = fbcon_display();
3097 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003098 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3099 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3100
3101 /* if the logo is full-screen size, remove "fbcon_clear()" */
3102 if ((header->width != fb_display->width)
3103 || (header->height != fb_display->height))
3104 fbcon_clear();
3105
3106 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3107 (uint32_t *)base,
3108 (header->blocks * 512))) {
3109 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3110 return -1;
3111 }
3112 fbcon_extract_to_screen(header, base);
3113 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003114 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003115
3116 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3117 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3118 return -1;
3119 }
3120
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303121 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003122 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3123 (uint32_t *)base,
3124 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303125 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303126 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003127 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003128 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303129 }
3130
Mao Flynn7b379f32015-04-20 00:28:30 +08003131 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303132}
3133
Mao Flynn7b379f32015-04-20 00:28:30 +08003134int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303135{
3136 int index = INVALID_PTN;
3137 unsigned long long ptn = 0;
3138 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003139 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003140 uint32_t blocksize, realsize, readsize;
3141 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303142
3143 index = partition_get_index("splash");
3144 if (index == 0) {
3145 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003146 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303147 }
3148
3149 ptn = partition_get_offset(index);
3150 if (ptn == 0) {
3151 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003152 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303153 }
3154
Mao Flynn1893a7f2015-06-03 12:03:36 +08003155 mmc_set_lun(partition_get_lun(index));
3156
3157 blocksize = mmc_get_device_blocksize();
3158 if (blocksize == 0) {
3159 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3160 return -1;
3161 }
3162
3163 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003164 if (!fb_display)
3165 {
3166 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3167 return -1;
3168 }
3169
Mao Flynn1893a7f2015-06-03 12:03:36 +08003170 base = (uint8_t *) fb_display->base;
3171
3172 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303173 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003174 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303175 }
3176
Mao Flynn1893a7f2015-06-03 12:03:36 +08003177 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003178 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303179 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003180 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303181 }
3182
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303183 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003184 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3185 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003186
Mao Flynn1893a7f2015-06-03 12:03:36 +08003187 realsize = header->blocks * 512;
3188 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3189
3190 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003191 if ((header->width != fb_display->width)
3192 || (header->height != fb_display->height))
3193 fbcon_clear();
3194
Mao Flynn1893a7f2015-06-03 12:03:36 +08003195 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003196 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3197 return -1;
3198 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003199
Mao Flynn1893a7f2015-06-03 12:03:36 +08003200 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3201 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303202
Mao Flynn1893a7f2015-06-03 12:03:36 +08003203 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
3208 realsize = header->width * header->height * fb_display->bpp / 8;
3209 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3210
3211 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3212 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3213 fbcon_clear();
3214 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3215 return -1;
3216 }
3217 } else {
3218 if (mmc_read(ptn + blocksize ,
3219 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3220 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3221 return -1;
3222 }
3223 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3224 }
3225 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303226 }
3227
Mao Flynn7b379f32015-04-20 00:28:30 +08003228 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303229}
3230
Mao Flynn7b379f32015-04-20 00:28:30 +08003231int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303232{
3233 if (target_is_emmc_boot()) {
3234 return splash_screen_mmc();
3235 } else {
3236 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003237 }
3238}
3239
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003240/* Get the size from partiton name */
3241static void get_partition_size(const char *arg, char *response)
3242{
3243 uint64_t ptn = 0;
3244 uint64_t size;
3245 int index = INVALID_PTN;
3246
3247 index = partition_get_index(arg);
3248
3249 if (index == INVALID_PTN)
3250 {
3251 dprintf(CRITICAL, "Invalid partition index\n");
3252 return;
3253 }
3254
3255 ptn = partition_get_offset(index);
3256
3257 if(!ptn)
3258 {
3259 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3260 return;
3261 }
3262
3263 size = partition_get_size(index);
3264
3265 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3266 return;
3267}
3268
3269/*
3270 * Publish the partition type & size info
3271 * fastboot getvar will publish the required information.
3272 * fastboot getvar partition_size:<partition_name>: partition size in hex
3273 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3274 */
3275static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3276{
3277 uint8_t i;
3278
3279 for (i = 0; i < num_parts; i++) {
3280 get_partition_size(info[i].part_name, info[i].size_response);
3281
3282 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3283 {
3284 dprintf(CRITICAL, "partition size name truncated\n");
3285 return;
3286 }
3287 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3288 {
3289 dprintf(CRITICAL, "partition type name truncated\n");
3290 return;
3291 }
3292
3293 /* publish partition size & type info */
3294 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3295 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3296 }
3297}
3298
Amol Jadi5edf3552013-07-23 14:15:34 -07003299/* register commands and variables for fastboot */
3300void aboot_fastboot_register_commands(void)
3301{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003302 int i;
lijuangf16461c2015-08-03 17:09:34 +08003303 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003304
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003305 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003306 /* By default the enabled list is empty. */
3307 {"", NULL},
3308 /* move commands enclosed within the below ifndef to here
3309 * if they need to be enabled in user build.
3310 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003311#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003312 /* Register the following commands only for non-user builds */
3313 {"flash:", cmd_flash},
3314 {"erase:", cmd_erase},
3315 {"boot", cmd_boot},
3316 {"continue", cmd_continue},
3317 {"reboot", cmd_reboot},
3318 {"reboot-bootloader", cmd_reboot_bootloader},
3319 {"oem unlock", cmd_oem_unlock},
3320 {"oem unlock-go", cmd_oem_unlock_go},
3321 {"oem lock", cmd_oem_lock},
3322 {"flashing unlock", cmd_oem_unlock},
3323 {"flashing lock", cmd_oem_lock},
3324 {"flashing lock_critical", cmd_flashing_lock_critical},
3325 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3326 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3327 {"oem device-info", cmd_oem_devinfo},
3328 {"preflash", cmd_preflash},
3329 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3330 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
3331 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003332#endif
lijuang511a2b52015-08-14 20:50:51 +08003333 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003334
3335 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3336 for (i = 1; i < fastboot_cmds_count; i++)
3337 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3338
Amol Jadi5edf3552013-07-23 14:15:34 -07003339 /* publish variables and their values */
3340 fastboot_publish("product", TARGET(BOARD));
3341 fastboot_publish("kernel", "lk");
3342 fastboot_publish("serialno", sn_buf);
3343
3344 /*
3345 * partition info is supported only for emmc partitions
3346 * Calling this for NAND prints some error messages which
3347 * is harmless but misleading. Avoid calling this for NAND
3348 * devices.
3349 */
3350 if (target_is_emmc_boot())
3351 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3352
3353 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003354 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3355 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003356 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003357 /* Is the charger screen check enabled */
3358 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3359 device.charger_screen_enabled);
3360 fastboot_publish("charger-screen-enabled",
3361 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303362 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3363 device.display_panel);
3364 fastboot_publish("display-panel",
3365 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003366 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3367 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003368 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3369 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3370 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3371 target_is_emmc_boot()? "eMMC":"UFS");
3372 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003373#if CHECK_BAT_VOLTAGE
3374 snprintf(battery_voltage, MAX_RSP_SIZE, "%d",
3375 target_get_battery_voltage());
3376 fastboot_publish("battery-voltage", (const char *) battery_voltage);
3377 fastboot_publish("battery-soc-ok", target_battery_soc_ok()? "yes":"no");
3378#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003379}
3380
Brian Swetland9c4c0752009-01-25 16:23:50 -08003381void aboot_init(const struct app_descriptor *app)
3382{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003383 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03003384 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003385
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003386 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003387 if (target_is_emmc_boot())
3388 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003389 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003390 page_mask = page_size - 1;
3391 }
3392 else
3393 {
3394 page_size = flash_page_size();
3395 page_mask = page_size - 1;
3396 }
3397
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003398 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3399
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003400 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303401 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003402
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003403 /* Display splash screen if enabled */
3404#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003405#if NO_ALARM_DISPLAY
3406 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003407#endif
lijuang99c02d82015-02-13 19:04:34 +08003408 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003409#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003410 /* Wait if the display shutdown is in progress */
3411 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003412 if (!pm_appsbl_display_init_done())
3413 target_display_init(device.display_panel);
3414 else
3415 display_image_on_screen();
3416#else
lijuang99c02d82015-02-13 19:04:34 +08003417 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003418#endif
lijuang99c02d82015-02-13 19:04:34 +08003419 dprintf(SPEW, "Display Init: Done\n");
3420#if NO_ALARM_DISPLAY
3421 }
3422#endif
3423#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003424
Greg Griscod6250552011-06-29 14:40:23 -07003425 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003426 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003427
Dhaval Patel223ec952013-07-18 14:49:44 -07003428 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3429
Matthew Qindefd5562014-07-11 18:02:40 +08003430 /*
3431 * Check power off reason if user force reset,
3432 * if yes phone will do normal boot.
3433 */
3434 if (is_user_force_reset())
3435 goto normal_boot;
3436
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003437 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003438 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003439 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003440 dprintf(ALWAYS,"dload mode key sequence detected\n");
3441 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003442 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003443 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003444 }
3445 else
3446 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303447 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003448 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003449 }
3450 boot_into_fastboot = true;
3451 }
3452 if (!boot_into_fastboot)
3453 {
3454 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3455 boot_into_recovery = 1;
3456 if (!boot_into_recovery &&
3457 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003458 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003459 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003460 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003461 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003462 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003463 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003464
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003465#if USE_PON_REBOOT_REG
3466 reboot_mode = check_hard_reboot_mode();
3467#else
Ajay Dudani77421292010-10-27 19:34:06 -07003468 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003469#endif
3470 if (reboot_mode == RECOVERY_MODE)
3471 {
Ajay Dudani77421292010-10-27 19:34:06 -07003472 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003473 }
3474 else if(reboot_mode == FASTBOOT_MODE)
3475 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003476 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003477 }
3478 else if(reboot_mode == ALARM_BOOT)
3479 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003480 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003481 }
3482 else if (reboot_mode == DM_VERITY_ENFORCING)
3483 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003484 device.verity_mode = 1;
3485 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003486 }
3487 else if (reboot_mode == DM_VERITY_LOGGING)
3488 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003489 device.verity_mode = 0;
3490 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003491 }
3492 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3493 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003494 if(send_delete_keys_to_tz())
3495 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003496 }
3497
Matthew Qindefd5562014-07-11 18:02:40 +08003498normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003499 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003500 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003501 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003502 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003503 if(emmc_recovery_init())
3504 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3505 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003506 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003507 if((device.is_unlocked) || (device.is_tampered))
3508 {
3509 #ifdef TZ_TAMPER_FUSE
3510 set_tamper_fuse_cmd();
3511 #endif
3512 #if USE_PCOM_SECBOOT
3513 set_tamper_flag(device.is_tampered);
3514 #endif
3515 }
Shashank Mittala0032282011-08-26 14:50:11 -07003516 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003517
Pavel Nedev5d91d412013-04-29 11:34:24 +03003518 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003519 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003520 else
3521 {
3522 recovery_init();
3523 #if USE_PCOM_SECBOOT
3524 if((device.is_unlocked) || (device.is_tampered))
3525 set_tamper_flag(device.is_tampered);
3526 #endif
3527 boot_linux_from_flash();
3528 }
3529 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3530 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003531 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003532
Amol Jadi5edf3552013-07-23 14:15:34 -07003533 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003534
Amol Jadi5edf3552013-07-23 14:15:34 -07003535 /* register aboot specific fastboot commands */
3536 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003537
Amol Jadi5edf3552013-07-23 14:15:34 -07003538 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003539 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003540
3541 /* initialize and start fastboot */
3542 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08003543}
3544
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003545uint32_t get_page_size()
3546{
3547 return page_size;
3548}
3549
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003550/*
3551 * Calculated and save hash (SHA256) for non-signed boot image.
3552 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003553 * @param image_addr - Boot image address
3554 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003555 *
3556 * @return int - 0 on success, negative value on failure.
3557 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003558static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003559{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003560 unsigned int digest[8];
3561#if IMAGE_VERIF_ALGO_SHA1
3562 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3563#else
3564 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3565#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003566
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003567 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003568 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003569
3570 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003571 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003572
3573 return 0;
3574}
3575
Brian Swetland9c4c0752009-01-25 16:23:50 -08003576APP_START(aboot)
3577 .init = aboot_init,
3578APP_END