blob: 44b0858c02485d4c9d79d05dfc83e2ee15bc89aa [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>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070095#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070096
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070097extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070098extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070099extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700100extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530101 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700102
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700103void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700104void write_device_info_mmc(device_info *dev);
105void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700106static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700107static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700108
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700109/* fastboot command function pointer */
110typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
111
112struct fastboot_cmd_desc {
113 char * name;
114 fastboot_cmd_fn cb;
115};
116
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700117#define EXPAND(NAME) #NAME
118#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700119
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800120#ifdef MEMBASE
121#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
122#else
David Ng183a7422009-12-07 14:55:21 -0800123#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800124#endif
125
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700126#ifndef MEMSIZE
127#define MEMSIZE 1024*1024
128#endif
129
130#define MAX_TAGS_SIZE 1024
131
Kun Liang2f1601a2013-08-12 16:29:54 +0800132/* make 4096 as default size to ensure EFS,EXT4's erasing */
133#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700134#define MAX_PANEL_BUF_SIZE 196
Kun Liang2f1601a2013-08-12 16:29:54 +0800135
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700136#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700137#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800138
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800139#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
140
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700141#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
142
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800143#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700144static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700145#else
David Ng183a7422009-12-07 14:55:21 -0800146static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700147#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800148static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300149static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800150static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800151static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800152static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700153static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300154static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200155static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800156
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800157static const char *baseband_apq = " androidboot.baseband=apq";
158static const char *baseband_msm = " androidboot.baseband=msm";
159static const char *baseband_csfb = " androidboot.baseband=csfb";
160static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700161static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800162static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700163static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800164static const char *baseband_dsda = " androidboot.baseband=dsda";
165static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800166static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800167static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800168
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700169#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700170static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700171static const char *verified_state= " androidboot.verifiedbootstate=";
172
173//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700174
175struct verified_boot_verity_mode vbvm[] =
176{
177 {false, "logging"},
178 {true, "enforcing"},
179};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700180struct verified_boot_state_name vbsn[] =
181{
182 {GREEN, "green"},
183 {ORANGE, "orange"},
184 {YELLOW,"yellow"},
185 {RED,"red" },
186};
187#endif
188
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700189static unsigned page_size = 0;
190static unsigned page_mask = 0;
191static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
192static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530193static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800194static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700195static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700196bool boot_into_fastboot = false;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700197
Shashank Mittalcd98d472011-08-02 14:29:24 -0700198/* Assuming unauthorized kernel image by default */
199static int auth_kernel_img = 0;
200
lijuang511a2b52015-08-14 20:50:51 +0800201static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
vijay kumarc65876c2015-04-24 13:29:16 +0530202static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700203
vijay kumarca2e6812015-07-08 20:28:25 +0530204static char frp_ptns[2][8] = {"config","frp"};
205
lijuang511a2b52015-08-14 20:50:51 +0800206static const char *critical_flash_allowed_ptn[] = {
207 "aboot",
208 "rpm",
209 "tz",
210 "sbl",
211 "sdi",
212 "sbl1",
213 "xbl",
214 "hyp",
215 "pmic",
216 "bootloader",
217 "devinfo",
218 "partition"};
219
Dima Zavin42168f22009-01-30 11:52:22 -0800220struct atag_ptbl_entry
221{
222 char name[16];
223 unsigned offset;
224 unsigned size;
225 unsigned flags;
226};
227
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700228/*
229 * Partition info, required to be published
230 * for fastboot
231 */
232struct getvar_partition_info {
233 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
234 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
235 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
236 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
237 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
238};
239
240/*
241 * Right now, we are publishing the info for only
242 * three partitions
243 */
244struct getvar_partition_info part_info[] =
245{
246 { "system" , "partition-size:", "partition-type:", "", "ext4" },
247 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
248 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
249};
250
251char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700252char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800253char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700254char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530255char panel_display_mode[MAX_RSP_SIZE];
lijuang65c5a822015-08-29 16:35:36 +0800256char battery_voltage[MAX_RSP_SIZE];
lijuangf16461c2015-08-03 17:09:34 +0800257char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700258
Greg Griscod2471ef2011-07-14 13:00:42 -0700259extern int emmc_recovery_init(void);
260
Kinson Chik0b1c8162011-08-31 16:31:57 -0700261#if NO_KEYPAD_DRIVER
262extern int fastboot_trigger(void);
263#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700264
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800265static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700266{
267 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700268#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800269 if (is_arm64)
270 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
271 else
272 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700273 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
274 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700275#endif
276}
277
Dima Zavin42168f22009-01-30 11:52:22 -0800278static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
279{
280 struct atag_ptbl_entry atag_ptn;
281
282 memcpy(atag_ptn.name, ptn->name, 16);
283 atag_ptn.name[15] = '\0';
284 atag_ptn.offset = ptn->start;
285 atag_ptn.size = ptn->length;
286 atag_ptn.flags = ptn->flags;
287 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
288 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
289}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800290
Neeti Desaie245d492012-06-01 12:52:13 -0700291unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800292{
David Ng183a7422009-12-07 14:55:21 -0800293 int cmdline_len = 0;
294 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800295 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700296 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800297 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300298 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700299 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700300 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200301 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700302#if VERIFIED_BOOT
303 uint32_t boot_state = boot_verify_get_state();
304#endif
305
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200306#ifdef MDTP_SUPPORT
307 mdtp_activated(&is_mdtp_activated);
308#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800309
Brian Swetland9c4c0752009-01-25 16:23:50 -0800310 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800311 cmdline_len = strlen(cmdline);
312 have_cmdline = 1;
313 }
314 if (target_is_emmc_boot()) {
315 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800316#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700317 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
318 ASSERT(boot_dev_buf);
319 platform_boot_dev_cmdline(boot_dev_buf);
320 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700321#endif
David Ng183a7422009-12-07 14:55:21 -0800322 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800323
324 cmdline_len += strlen(usb_sn_cmdline);
325 cmdline_len += strlen(sn_buf);
326
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700327#if VERIFIED_BOOT
328 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700329 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
330 {
331 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
332 ASSERT(0);
333 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700334 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700335#endif
336
Pavel Nedev5614d222013-06-17 18:01:02 +0300337 if (boot_into_recovery && gpt_exists)
338 cmdline_len += strlen(secondary_gpt_enable);
339
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200340 if(is_mdtp_activated)
341 cmdline_len += strlen(mdtp_activated_flag);
342
Pavel Nedev328ac822013-04-05 15:25:11 +0300343 if (boot_into_ffbm) {
344 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700345 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800346 /* reduce kernel console messages to speed-up boot */
347 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800348 } else if (boot_reason_alarm) {
349 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800350 } else if ((target_build_variant_user() || device.charger_screen_enabled)
351 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700352 pause_at_bootup = 1;
353 cmdline_len += strlen(battchg_pause);
354 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800355
Shashank Mittalcd98d472011-08-02 14:29:24 -0700356 if(target_use_signed_kernel() && auth_kernel_img) {
357 cmdline_len += strlen(auth_kernel);
358 }
359
Joonwoo Park61112782013-10-02 19:50:39 -0700360 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
361 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530362 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700363 have_target_boot_params = 1;
364 cmdline_len += strlen(target_boot_params);
365 }
366
Ajay Dudanid04110c2011-01-17 23:55:07 -0800367 /* Determine correct androidboot.baseband to use */
368 switch(target_baseband())
369 {
370 case BASEBAND_APQ:
371 cmdline_len += strlen(baseband_apq);
372 break;
373
374 case BASEBAND_MSM:
375 cmdline_len += strlen(baseband_msm);
376 break;
377
378 case BASEBAND_CSFB:
379 cmdline_len += strlen(baseband_csfb);
380 break;
381
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800382 case BASEBAND_SVLTE2A:
383 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800384 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700385
386 case BASEBAND_MDM:
387 cmdline_len += strlen(baseband_mdm);
388 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700389
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800390 case BASEBAND_MDM2:
391 cmdline_len += strlen(baseband_mdm2);
392 break;
393
Amol Jadi5c61a952012-05-04 17:05:35 -0700394 case BASEBAND_SGLTE:
395 cmdline_len += strlen(baseband_sglte);
396 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530397
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800398 case BASEBAND_SGLTE2:
399 cmdline_len += strlen(baseband_sglte2);
400 break;
401
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530402 case BASEBAND_DSDA:
403 cmdline_len += strlen(baseband_dsda);
404 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800405
406 case BASEBAND_DSDA2:
407 cmdline_len += strlen(baseband_dsda2);
408 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800409 }
410
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800411 if (cmdline) {
412 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530413 target_display_panel_node(display_panel_buf,
414 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800415 strlen(display_panel_buf)) {
416 cmdline_len += strlen(display_panel_buf);
417 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700418 }
419
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800420 if (target_warm_boot()) {
421 warm_boot = true;
422 cmdline_len += strlen(warmboot_cmdline);
423 }
424
David Ng183a7422009-12-07 14:55:21 -0800425 if (cmdline_len > 0) {
426 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800427 unsigned char *dst;
428
429 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
430 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530431 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800432 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700433
Amol Jadi168b7712012-03-06 16:15:00 -0800434 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800435 if (have_cmdline) {
436 src = cmdline;
437 while ((*dst++ = *src++));
438 }
439 if (target_is_emmc_boot()) {
440 src = emmc_cmdline;
441 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700442 have_cmdline = 1;
443 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800444#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700445 src = boot_dev_buf;
446 if (have_cmdline) --dst;
447 while ((*dst++ = *src++));
448#endif
David Ngf773dde2010-07-26 19:55:08 -0700449 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800450
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700451#if VERIFIED_BOOT
452 src = verified_state;
453 if(have_cmdline) --dst;
454 have_cmdline = 1;
455 while ((*dst++ = *src++));
456 src = vbsn[boot_state].name;
457 if(have_cmdline) --dst;
458 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700459
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700460 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
461 {
462 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
463 ASSERT(0);
464 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700465 src = verity_mode;
466 if(have_cmdline) --dst;
467 while ((*dst++ = *src++));
468 src = vbvm[device.verity_mode].name;
469 if(have_cmdline) -- dst;
470 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700471#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800472 src = usb_sn_cmdline;
473 if (have_cmdline) --dst;
474 have_cmdline = 1;
475 while ((*dst++ = *src++));
476 src = sn_buf;
477 if (have_cmdline) --dst;
478 have_cmdline = 1;
479 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800480 if (warm_boot) {
481 if (have_cmdline) --dst;
482 src = warmboot_cmdline;
483 while ((*dst++ = *src++));
484 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800485
Pavel Nedev5614d222013-06-17 18:01:02 +0300486 if (boot_into_recovery && gpt_exists) {
487 src = secondary_gpt_enable;
488 if (have_cmdline) --dst;
489 while ((*dst++ = *src++));
490 }
491
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200492 if (is_mdtp_activated) {
493 src = mdtp_activated_flag;
494 if (have_cmdline) --dst;
495 while ((*dst++ = *src++));
496 }
497
Pavel Nedev328ac822013-04-05 15:25:11 +0300498 if (boot_into_ffbm) {
499 src = androidboot_mode;
500 if (have_cmdline) --dst;
501 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700502 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300503 if (have_cmdline) --dst;
504 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800505 src = loglevel;
506 if (have_cmdline) --dst;
507 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800508 } else if (boot_reason_alarm) {
509 src = alarmboot_cmdline;
510 if (have_cmdline) --dst;
511 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300512 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700513 src = battchg_pause;
514 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800515 while ((*dst++ = *src++));
516 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800517
Shashank Mittalcd98d472011-08-02 14:29:24 -0700518 if(target_use_signed_kernel() && auth_kernel_img) {
519 src = auth_kernel;
520 if (have_cmdline) --dst;
521 while ((*dst++ = *src++));
522 }
523
Ajay Dudanid04110c2011-01-17 23:55:07 -0800524 switch(target_baseband())
525 {
526 case BASEBAND_APQ:
527 src = baseband_apq;
528 if (have_cmdline) --dst;
529 while ((*dst++ = *src++));
530 break;
531
532 case BASEBAND_MSM:
533 src = baseband_msm;
534 if (have_cmdline) --dst;
535 while ((*dst++ = *src++));
536 break;
537
538 case BASEBAND_CSFB:
539 src = baseband_csfb;
540 if (have_cmdline) --dst;
541 while ((*dst++ = *src++));
542 break;
543
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800544 case BASEBAND_SVLTE2A:
545 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800546 if (have_cmdline) --dst;
547 while ((*dst++ = *src++));
548 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700549
550 case BASEBAND_MDM:
551 src = baseband_mdm;
552 if (have_cmdline) --dst;
553 while ((*dst++ = *src++));
554 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700555
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800556 case BASEBAND_MDM2:
557 src = baseband_mdm2;
558 if (have_cmdline) --dst;
559 while ((*dst++ = *src++));
560 break;
561
Amol Jadi5c61a952012-05-04 17:05:35 -0700562 case BASEBAND_SGLTE:
563 src = baseband_sglte;
564 if (have_cmdline) --dst;
565 while ((*dst++ = *src++));
566 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530567
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800568 case BASEBAND_SGLTE2:
569 src = baseband_sglte2;
570 if (have_cmdline) --dst;
571 while ((*dst++ = *src++));
572 break;
573
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530574 case BASEBAND_DSDA:
575 src = baseband_dsda;
576 if (have_cmdline) --dst;
577 while ((*dst++ = *src++));
578 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800579
580 case BASEBAND_DSDA2:
581 src = baseband_dsda2;
582 if (have_cmdline) --dst;
583 while ((*dst++ = *src++));
584 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800585 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700586
587 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700588 src = display_panel_buf;
589 if (have_cmdline) --dst;
590 while ((*dst++ = *src++));
591 }
Joonwoo Park61112782013-10-02 19:50:39 -0700592
593 if (have_target_boot_params) {
594 if (have_cmdline) --dst;
595 src = target_boot_params;
596 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530597 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700598 }
Neeti Desaie245d492012-06-01 12:52:13 -0700599 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700600
601
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700602 if (boot_dev_buf)
603 free(boot_dev_buf);
604
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800605 if (cmdline_final)
606 dprintf(INFO, "cmdline: %s\n", cmdline_final);
607 else
608 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700609 return cmdline_final;
610}
611
612unsigned *atag_core(unsigned *ptr)
613{
614 /* CORE */
615 *ptr++ = 2;
616 *ptr++ = 0x54410001;
617
618 return ptr;
619
620}
621
622unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
623 unsigned ramdisk_size)
624{
625 if (ramdisk_size) {
626 *ptr++ = 4;
627 *ptr++ = 0x54420005;
628 *ptr++ = (unsigned)ramdisk;
629 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800630 }
631
Neeti Desaie245d492012-06-01 12:52:13 -0700632 return ptr;
633}
634
635unsigned *atag_ptable(unsigned **ptr_addr)
636{
637 int i;
638 struct ptable *ptable;
639
640 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700641 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
642 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700643 *(*ptr_addr)++ = 0x4d534d70;
644 for (i = 0; i < ptable->count; ++i)
645 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
646 }
647
648 return (*ptr_addr);
649}
650
651unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
652{
653 int cmdline_length = 0;
654 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700655 char *dest;
656
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800657 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700658 n = (cmdline_length + 4) & (~3);
659
660 *ptr++ = (n / 4) + 2;
661 *ptr++ = 0x54410009;
662 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800663 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700664 ptr += (n / 4);
665
666 return ptr;
667}
668
669unsigned *atag_end(unsigned *ptr)
670{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800671 /* END */
672 *ptr++ = 0;
673 *ptr++ = 0;
674
Neeti Desaie245d492012-06-01 12:52:13 -0700675 return ptr;
676}
677
678void generate_atags(unsigned *ptr, const char *cmdline,
679 void *ramdisk, unsigned ramdisk_size)
680{
681
682 ptr = atag_core(ptr);
683 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
684 ptr = target_atag_mem(ptr);
685
686 /* Skip NAND partition ATAGS for eMMC boot */
687 if (!target_is_emmc_boot()){
688 ptr = atag_ptable(&ptr);
689 }
690
691 ptr = atag_cmdline(ptr, cmdline);
692 ptr = atag_end(ptr);
693}
694
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700695typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700696void boot_linux(void *kernel, unsigned *tags,
697 const char *cmdline, unsigned machtype,
698 void *ramdisk, unsigned ramdisk_size)
699{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800700 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800701#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700702 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800703#endif
704
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700705 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800706 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800707 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800708
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530709 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700710
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800711 final_cmdline = update_cmdline((const char*)cmdline);
712
Neeti Desai17379b82012-06-04 18:42:53 -0700713#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800714 dprintf(INFO, "Updating device tree: start\n");
715
Neeti Desai17379b82012-06-04 18:42:53 -0700716 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530717 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700718 if(ret)
719 {
720 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
721 ASSERT(0);
722 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800723 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700724#else
Neeti Desaie245d492012-06-01 12:52:13 -0700725 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800726 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700727#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700728
Maria Yu52254c02014-07-04 16:14:54 +0800729 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700730
731#if VERIFIED_BOOT
732 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700733 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700734 {
735 dprintf(INFO, "Failed to write protect dev info\n");
736 ASSERT(0);
737 }
738#endif
739
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800740 /* Turn off splash screen if enabled */
741#if DISPLAY_SPLASH_SCREEN
742 target_display_shutdown();
743#endif
744
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700745 /* Perform target specific cleanup */
746 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800747
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800748 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530749 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800750
751 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700752
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +0530753 /* Initialise wdog to catch early kernel crashes */
754#if WDOG_SUPPORT
755 msm_wdog_init();
756#endif
Amol Jadi4421e652011-06-16 15:00:48 -0700757 /* do any platform specific cleanup before kernel entry */
758 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700759
Brian Swetland9c4c0752009-01-25 16:23:50 -0800760 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700761
Amol Jadi504f9fe2012-08-16 13:56:48 -0700762#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800763 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700764#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700765 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800766
767 if (IS_ARM64(kptr))
768 /* Jump to a 64bit kernel */
769 scm_elexec_call((paddr_t)kernel, tags_phys);
770 else
771 /* Jump to a 32bit kernel */
772 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800773}
774
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700775/* Function to check if the memory address range falls within the aboot
776 * boundaries.
777 * start: Start of the memory region
778 * size: Size of the memory region
779 */
780int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
781{
782 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800783 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700784 return -1;
785
786 /* Check for memory overlap. */
787 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
788 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700789 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700790 return 0;
791 else
792 return -1;
793}
794
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800795#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800796
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800797BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800798#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800799BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800800#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800801
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700802static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
803{
804 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800805
806#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800807#if IMAGE_VERIF_ALGO_SHA1
808 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
809#else
810 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
811#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800812#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700813
814 /* Assume device is rooted at this time. */
815 device.is_tampered = 1;
816
817 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
818
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700819#if VERIFIED_BOOT
820 if(boot_into_recovery)
821 {
822 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530823 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700824 }
825 else
826 {
827 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530828 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700829 }
830 boot_verify_print_state();
831#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700832 ret = image_verify((unsigned char *)bootimg_addr,
833 (unsigned char *)(bootimg_addr + bootimg_size),
834 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800835 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700836#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700837 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
838
839 if (ret)
840 {
841 /* Authorized kernel */
842 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700843 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700844 }
845
Amit Blay4aa292f2015-04-28 21:55:59 +0300846#ifdef MDTP_SUPPORT
847 {
848 /* Verify MDTP lock.
849 * For boot & recovery partitions, use aboot's verification result.
850 */
851 mdtp_ext_partition_verification_t ext_partition;
852 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
853 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
854 ext_partition.page_size = 0; /* Not needed since already validated */
855 ext_partition.image_addr = 0; /* Not needed since already validated */
856 ext_partition.image_size = 0; /* Not needed since already validated */
857 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
858 mdtp_fwlock_verify_lock(&ext_partition);
859 }
860#endif /* MDTP_SUPPORT */
861
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700862#if USE_PCOM_SECBOOT
863 set_tamper_flag(device.is_tampered);
864#endif
865
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700866#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700867 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700868 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700869 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800870#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +0800871 display_bootverify_menu_thread(DISPLAY_MENU_RED);
lijuanga40d6302015-07-20 20:10:13 +0800872 wait_for_users_action();
873#else
874 dprintf(CRITICAL,
875 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
876 mdelay(5000);
877#endif
878
879 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700880 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800881#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +0800882 display_bootverify_menu_thread(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800883 wait_for_users_action();
884#else
885 dprintf(CRITICAL,
886 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
887 mdelay(5000);
888#endif
889 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700890 default:
lijuanga40d6302015-07-20 20:10:13 +0800891 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700892 }
893#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700894#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530895 if(device.is_tampered)
896 {
897 write_device_info_mmc(&device);
898 #ifdef TZ_TAMPER_FUSE
899 set_tamper_fuse_cmd();
900 #endif
901 #ifdef ASSERT_ON_TAMPER
902 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
903 ASSERT(0);
904 #endif
905 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700906#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700907}
908
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530909static bool check_format_bit()
910{
911 bool ret = false;
912 int index;
913 uint64_t offset;
914 struct boot_selection_info *in = NULL;
915 char *buf = NULL;
916
917 index = partition_get_index("bootselect");
918 if (index == INVALID_PTN)
919 {
920 dprintf(INFO, "Unable to locate /bootselect partition\n");
921 return ret;
922 }
923 offset = partition_get_offset(index);
924 if(!offset)
925 {
926 dprintf(INFO, "partition /bootselect doesn't exist\n");
927 return ret;
928 }
929 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
930 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530931 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530932 {
933 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
934 free(buf);
935 return ret;
936 }
937 in = (struct boot_selection_info *) buf;
938 if ((in->signature == BOOTSELECT_SIGNATURE) &&
939 (in->version == BOOTSELECT_VERSION)) {
940 if ((in->state_info & BOOTSELECT_FORMAT) &&
941 !(in->state_info & BOOTSELECT_FACTORY))
942 ret = true;
943 } else {
944 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
945 in->signature, in->version);
946 ASSERT(0);
947 }
948 free(buf);
949 return ret;
950}
951
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700952void boot_verifier_init()
953{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700954 uint32_t boot_state;
955 /* Check if device unlock */
956 if(device.is_unlocked)
957 {
958 boot_verify_send_event(DEV_UNLOCK);
959 boot_verify_print_state();
960 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
961 return;
962 }
963 else
964 {
965 boot_verify_send_event(BOOT_INIT);
966 }
967
968 /* Initialize keystore */
969 boot_state = boot_verify_keystore_init();
970 if(boot_state == YELLOW)
971 {
972 boot_verify_print_state();
973 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
974 }
975}
976
Shashank Mittal23b8f422010-04-16 19:27:21 -0700977int boot_linux_from_mmc(void)
978{
979 struct boot_img_hdr *hdr = (void*) buf;
980 struct boot_img_hdr *uhdr;
981 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700982 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700983 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700984 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700985
Shashank Mittalcd98d472011-08-02 14:29:24 -0700986 unsigned char *image_addr = 0;
987 unsigned kernel_actual;
988 unsigned ramdisk_actual;
989 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700990 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700991
Matthew Qin271927e2015-03-31 22:07:07 -0400992 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +0800993 unsigned int out_len = 0;
994 unsigned int out_avai_len = 0;
995 unsigned char *out_addr = NULL;
996 uint32_t dtb_offset = 0;
997 unsigned char *kernel_start_addr = NULL;
998 unsigned int kernel_size = 0;
999 int rc;
1000
Neeti Desai465491e2012-07-31 12:53:35 -07001001#if DEVICE_TREE
1002 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001003 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001004 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001005 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001006 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001007 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001008#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001009 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001010
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301011 if (check_format_bit())
1012 boot_into_recovery = 1;
1013
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001014 if (!boot_into_recovery) {
1015 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1016 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1017 if (rcode <= 0) {
1018 boot_into_ffbm = false;
1019 if (rcode < 0)
1020 dprintf(CRITICAL,"failed to get ffbm cookie");
1021 } else
1022 boot_into_ffbm = true;
1023 } else
1024 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001025 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1026 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1027 dprintf(INFO, "Unified boot method!\n");
1028 hdr = uhdr;
1029 goto unified_boot;
1030 }
Greg Griscod6250552011-06-29 14:40:23 -07001031 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001032 index = partition_get_index("boot");
1033 ptn = partition_get_offset(index);
1034 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001035 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1036 return -1;
1037 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001038 }
1039 else {
1040 index = partition_get_index("recovery");
1041 ptn = partition_get_offset(index);
1042 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001043 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1044 return -1;
1045 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001046 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001047 /* Set Lun for boot & recovery partitions */
1048 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001049
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301050 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001051 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1052 return -1;
1053 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001054
1055 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001056 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001057 return -1;
1058 }
1059
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001060 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301061
1062 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1063 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1064 return -1;
1065 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001066 page_size = hdr->page_size;
1067 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001068 }
1069
vijay kumar287bb542015-09-29 13:01:52 +05301070 /* ensure commandline is terminated */
1071 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1072
Matthew Qin49e51fa2015-02-09 10:40:45 +08001073 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1074 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001075
Matthew Qin49e51fa2015-02-09 10:40:45 +08001076 image_addr = (unsigned char *)target_get_scratch_address();
1077
1078#if DEVICE_TREE
1079 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1080 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1081#else
1082 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1083#endif
1084
1085#if VERIFIED_BOOT
1086 boot_verifier_init();
1087#endif
1088
1089 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
1090 {
1091 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1092 return -1;
1093 }
1094
Matthew Qinbb7923d2015-02-09 10:56:09 +08001095 /*
1096 * Update loading flow of bootimage to support compressed/uncompressed
1097 * bootimage on both 64bit and 32bit platform.
1098 * 1. Load bootimage from emmc partition onto DDR.
1099 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1100 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1101 * platform accordingly.
1102 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1103 */
1104
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001105 dprintf(INFO, "Loading (%s) image (%d): start\n",
1106 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001107 bs_set_timestamp(BS_KERNEL_LOAD_START);
1108
Matthew Qinbb7923d2015-02-09 10:56:09 +08001109 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001110 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1111 {
1112 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1113 return -1;
1114 }
1115
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001116 dprintf(INFO, "Loading (%s) image (%d): done\n",
1117 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1118
Matthew Qin49e51fa2015-02-09 10:40:45 +08001119 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1120
1121 /* Authenticate Kernel */
1122 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1123 (int) target_use_signed_kernel(),
1124 device.is_unlocked,
1125 device.is_tampered);
1126
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001127 /* Change the condition a little bit to include the test framework support.
1128 * We would never reach this point if device is in fastboot mode, even if we did
1129 * that means we are in test mode, so execute kernel authentication part for the
1130 * tests */
1131 if((target_use_signed_kernel() && (!device.is_unlocked)) || boot_into_fastboot)
Matthew Qin49e51fa2015-02-09 10:40:45 +08001132 {
1133 offset = imagesize_actual;
1134 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1135 {
1136 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1137 return -1;
1138 }
1139
1140 /* Read signature */
1141 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1142 {
1143 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1144 return -1;
1145 }
1146
1147 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001148 /* The purpose of our test is done here */
1149 if (boot_into_fastboot && auth_kernel_img)
1150 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001151 } else {
1152 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1153 #ifdef TZ_SAVE_KERNEL_HASH
1154 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1155 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001156
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001157#if VERIFIED_BOOT
1158 if(boot_verify_get_state() == ORANGE)
1159 {
lijuanga40d6302015-07-20 20:10:13 +08001160#if FBCON_DISPLAY_MSG
lijuang4ece1e72015-08-14 21:02:36 +08001161 display_bootverify_menu_thread(DISPLAY_MENU_ORANGE);
lijuanga40d6302015-07-20 20:10:13 +08001162 wait_for_users_action();
1163#else
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001164 dprintf(CRITICAL,
lijuanga40d6302015-07-20 20:10:13 +08001165 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001166 mdelay(5000);
lijuanga40d6302015-07-20 20:10:13 +08001167#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001168 }
1169#endif
1170
Amit Blay4aa292f2015-04-28 21:55:59 +03001171#ifdef MDTP_SUPPORT
1172 {
1173 /* Verify MDTP lock.
1174 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1175 * since verification was skipped in aboot. The signature is not part of the loaded image.
1176 */
1177 mdtp_ext_partition_verification_t ext_partition;
1178 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1179 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1180 ext_partition.page_size = page_size;
1181 ext_partition.image_addr = (uint32)image_addr;
1182 ext_partition.image_size = imagesize_actual;
1183 ext_partition.sig_avail = FALSE;
1184 mdtp_fwlock_verify_lock(&ext_partition);
1185 }
1186#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001187 }
1188
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001189#if VERIFIED_BOOT
1190 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001191 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001192 ASSERT(0);
1193#endif
1194
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001195 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001196 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1197 * If not, continue booting.
1198 */
1199 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1200 {
1201 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1202 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001203 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001204 rc = decompress((unsigned char *)(image_addr + page_size),
1205 hdr->kernel_size, out_addr, out_avai_len,
1206 &dtb_offset, &out_len);
1207 if (rc)
1208 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001209 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001210 ASSERT(0);
1211 }
1212
Matthew Qin0b15b322015-05-19 05:20:54 -04001213 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001214 kptr = (struct kernel64_hdr *)out_addr;
1215 kernel_start_addr = out_addr;
1216 kernel_size = out_len;
1217 } else {
1218 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1219 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1220 kernel_size = hdr->kernel_size;
1221 }
1222
1223 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001224 * Update the kernel/ramdisk/tags address if the boot image header
1225 * has default values, these default values come from mkbootimg when
1226 * the boot image is flashed using fastboot flash:raw
1227 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001228 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001229
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001230 /* Get virtual addresses since the hdr saves physical addresses. */
1231 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1232 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1233 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001234
Matthew Qinbb7923d2015-02-09 10:56:09 +08001235 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001236 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001237 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001238 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1239 {
1240 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1241 return -1;
1242 }
1243
1244#ifndef DEVICE_TREE
1245 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1246 {
1247 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1248 return -1;
1249 }
1250#endif
1251
Matthew Qin49e51fa2015-02-09 10:40:45 +08001252 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001253 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001254 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001255
Matthew Qin49e51fa2015-02-09 10:40:45 +08001256 #if DEVICE_TREE
1257 if(hdr->dt_size) {
1258 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1259 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001260
Matthew Qin49e51fa2015-02-09 10:40:45 +08001261 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1262 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1263 return -1;
1264 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001265
Matthew Qin49e51fa2015-02-09 10:40:45 +08001266 /* Find index of device tree within device tree table */
1267 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1268 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1269 return -1;
1270 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001271
Matthew Qin271927e2015-03-31 22:07:07 -04001272 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1273 {
1274 unsigned int compressed_size = 0;
1275 out_addr += out_len;
1276 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001277 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001278 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1279 dt_entry.size, out_addr, out_avai_len,
1280 &compressed_size, &dtb_size);
1281 if (rc)
1282 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001283 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001284 ASSERT(0);
1285 }
1286
Matthew Qin0b15b322015-05-19 05:20:54 -04001287 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001288 best_match_dt_addr = out_addr;
1289 } else {
1290 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1291 dtb_size = dt_entry.size;
1292 }
1293
Matthew Qin49e51fa2015-02-09 10:40:45 +08001294 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001295 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001296 {
1297 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1298 return -1;
1299 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001300
Matthew Qin271927e2015-03-31 22:07:07 -04001301 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001302 } else {
1303 /* Validate the tags_addr */
1304 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001305 {
1306 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1307 return -1;
1308 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001309 /*
1310 * If appended dev tree is found, update the atags with
1311 * memory address to the DTB appended location on RAM.
1312 * Else update with the atags address in the kernel header
1313 */
1314 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001315 dtb = dev_tree_appended((void*)(image_addr + page_size),
1316 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001317 (void *)hdr->tags_addr);
1318 if (!dtb) {
1319 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001320 return -1;
1321 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001322 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001323 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001324
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001325 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1326 target_load_ssd_keystore();
1327
Shashank Mittal23b8f422010-04-16 19:27:21 -07001328unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001329
Dima Zavin77e41f32013-03-06 16:10:43 -08001330 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001331 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001332 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1333
1334 return 0;
1335}
1336
Dima Zavin214cc642009-01-26 11:16:21 -08001337int boot_linux_from_flash(void)
1338{
1339 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001340 struct ptentry *ptn;
1341 struct ptable *ptable;
1342 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001343
Shashank Mittalcd98d472011-08-02 14:29:24 -07001344 unsigned char *image_addr = 0;
1345 unsigned kernel_actual;
1346 unsigned ramdisk_actual;
1347 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001348 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001349
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001350#if DEVICE_TREE
1351 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001352 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001353 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001354 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001355#endif
1356
David Ng183a7422009-12-07 14:55:21 -08001357 if (target_is_emmc_boot()) {
1358 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1359 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1360 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1361 return -1;
1362 }
1363 goto continue_boot;
1364 }
1365
Dima Zavin214cc642009-01-26 11:16:21 -08001366 ptable = flash_get_ptable();
1367 if (ptable == NULL) {
1368 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1369 return -1;
1370 }
1371
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001372 if(!boot_into_recovery)
1373 {
1374 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001375
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001376 if (ptn == NULL) {
1377 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1378 return -1;
1379 }
1380 }
1381 else
1382 {
1383 ptn = ptable_find(ptable, "recovery");
1384 if (ptn == NULL) {
1385 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1386 return -1;
1387 }
Dima Zavin214cc642009-01-26 11:16:21 -08001388 }
1389
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001390 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001391 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1392 return -1;
1393 }
Dima Zavin214cc642009-01-26 11:16:21 -08001394
1395 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001396 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001397 return -1;
1398 }
1399
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001400 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001401 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 -08001402 return -1;
1403 }
1404
vijay kumar287bb542015-09-29 13:01:52 +05301405 /* ensure commandline is terminated */
1406 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1407
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001408 /*
1409 * Update the kernel/ramdisk/tags address if the boot image header
1410 * has default values, these default values come from mkbootimg when
1411 * the boot image is flashed using fastboot flash:raw
1412 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001413 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001414
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001415 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001416 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1417 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1418 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1419
1420 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1421 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1422
1423 /* Check if the addresses in the header are valid. */
1424 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1425 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1426 {
1427 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1428 return -1;
1429 }
1430
1431#ifndef DEVICE_TREE
1432 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1433 {
1434 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1435 return -1;
1436 }
1437#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001438
Shashank Mittalcd98d472011-08-02 14:29:24 -07001439 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001440 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001441 {
1442 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001443 offset = 0;
1444
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001445#if DEVICE_TREE
1446 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1447 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001448
1449 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1450 {
1451 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1452 return -1;
1453 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001454#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001455 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001456#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001457
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001458 dprintf(INFO, "Loading (%s) image (%d): start\n",
1459 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001460 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001461
Shashank Mittalcd98d472011-08-02 14:29:24 -07001462 /* Read image without signature */
1463 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1464 {
1465 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1466 return -1;
1467 }
Dima Zavin214cc642009-01-26 11:16:21 -08001468
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001469 dprintf(INFO, "Loading (%s) image (%d): done\n",
1470 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001471 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001472
Shashank Mittalcd98d472011-08-02 14:29:24 -07001473 offset = imagesize_actual;
1474 /* Read signature */
1475 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1476 {
1477 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001478 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001479 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001480
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301481 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001482
1483 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001484 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1485 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001486#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001487 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001488 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001489 {
1490 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1491 return -1;
1492 }
1493
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001494 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1495#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001496
1497 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001498 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001499 {
1500 write_device_info_flash(&device);
1501 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301502#if USE_PCOM_SECBOOT
1503 set_tamper_flag(device.is_tampered);
1504#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001505 }
1506 else
1507 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001508 offset = page_size;
1509
Amol Jadib6be5c12012-11-14 13:39:51 -08001510 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1511 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1512 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1513
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001514 dprintf(INFO, "Loading (%s) image (%d): start\n",
1515 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1516
Amol Jadi492d5a52013-03-15 16:12:34 -07001517 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001518
1519 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001520 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1521 return -1;
1522 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001523 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001524
Amol Jadib6be5c12012-11-14 13:39:51 -08001525 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001526 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1527 return -1;
1528 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001529 offset += ramdisk_actual;
1530
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001531 dprintf(INFO, "Loading (%s) image (%d): done\n",
1532 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1533
Amol Jadi492d5a52013-03-15 16:12:34 -07001534 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001535
1536 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001537 offset += second_actual;
1538 /* Second image loading not implemented. */
1539 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001540 }
1541
1542#if DEVICE_TREE
1543 if(hdr->dt_size != 0) {
1544
1545 /* Read the device tree table into buffer */
1546 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1547 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1548 return -1;
1549 }
1550
1551 table = (struct dt_table*) dt_buf;
1552
Deepa Dinamani19648b42013-09-05 17:05:55 -07001553 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001554 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1555 return -1;
1556 }
1557
Deepa Dinamani19648b42013-09-05 17:05:55 -07001558 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1559 if (!table)
1560 return -1;
1561
1562 /* Read the entire device tree table into buffer */
1563 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1564 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1565 return -1;
1566 }
1567
1568
Joel Kingaa335dc2013-06-03 16:11:08 -07001569 /* Find index of device tree within device tree table */
1570 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001571 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1572 return -1;
1573 }
1574
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001575 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001576 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001577 {
1578 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1579 return -1;
1580 }
1581
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001582 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001583 if(flash_read(ptn, offset + dt_entry.offset,
1584 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001585 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1586 return -1;
1587 }
1588 }
1589#endif
1590
Shashank Mittalcd98d472011-08-02 14:29:24 -07001591 }
David Ng183a7422009-12-07 14:55:21 -08001592continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001593
Dima Zavin214cc642009-01-26 11:16:21 -08001594 /* TODO: create/pass atags to kernel */
1595
Ajay Dudanie28a6072011-07-01 13:59:46 -07001596 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001597 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001598 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1599
1600 return 0;
1601}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001602
Shashank Mittal162244e2011-08-08 19:01:25 -07001603void write_device_info_mmc(device_info *dev)
1604{
Shashank Mittal162244e2011-08-08 19:01:25 -07001605 unsigned long long ptn = 0;
1606 unsigned long long size;
1607 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001608 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001609 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001610 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001611
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001612 if (devinfo_present)
1613 index = partition_get_index("devinfo");
1614 else
1615 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001616
Shashank Mittal162244e2011-08-08 19:01:25 -07001617 ptn = partition_get_offset(index);
1618 if(ptn == 0)
1619 {
1620 return;
1621 }
1622
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001623 lun = partition_get_lun(index);
1624 mmc_set_lun(lun);
1625
Shashank Mittal162244e2011-08-08 19:01:25 -07001626 size = partition_get_size(index);
1627
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001628 blocksize = mmc_get_device_blocksize();
1629
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001630 if (devinfo_present)
1631 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1632 else
1633 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1634 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001635 {
1636 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001637 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001638 }
1639}
1640
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001641void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001642{
Shashank Mittal162244e2011-08-08 19:01:25 -07001643 unsigned long long ptn = 0;
1644 unsigned long long size;
1645 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001646 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001647 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001648
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001649 if ((index = partition_get_index("devinfo")) < 0)
1650 {
1651 devinfo_present = false;
1652 index = partition_get_index("aboot");
1653 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001654
Shashank Mittal162244e2011-08-08 19:01:25 -07001655 ptn = partition_get_offset(index);
1656 if(ptn == 0)
1657 {
1658 return;
1659 }
1660
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001661 mmc_set_lun(partition_get_lun(index));
1662
Shashank Mittal162244e2011-08-08 19:01:25 -07001663 size = partition_get_size(index);
1664
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001665 blocksize = mmc_get_device_blocksize();
1666
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001667 if (devinfo_present)
1668 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1669 else
1670 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1671 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001672 {
1673 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001674 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001675 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001676}
1677
1678void write_device_info_flash(device_info *dev)
1679{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001680 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001681 struct ptentry *ptn;
1682 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001683 if(info == NULL)
1684 {
1685 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1686 ASSERT(0);
1687 }
1688 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001689 ptable = flash_get_ptable();
1690 if (ptable == NULL)
1691 {
1692 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1693 return;
1694 }
1695
1696 ptn = ptable_find(ptable, "devinfo");
1697 if (ptn == NULL)
1698 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001699 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001700 return;
1701 }
1702
1703 memcpy(info, dev, sizeof(device_info));
1704
1705 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1706 {
1707 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1708 return;
1709 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001710 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001711}
1712
vijay kumarc65876c2015-04-24 13:29:16 +05301713static int read_allow_oem_unlock(device_info *dev)
1714{
vijay kumarc65876c2015-04-24 13:29:16 +05301715 unsigned offset;
1716 int index;
1717 unsigned long long ptn;
1718 unsigned long long ptn_size;
1719 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001720 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301721
vijay kumarca2e6812015-07-08 20:28:25 +05301722 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301723 if (index == INVALID_PTN)
1724 {
vijay kumarca2e6812015-07-08 20:28:25 +05301725 index = partition_get_index(frp_ptns[1]);
1726 if (index == INVALID_PTN)
1727 {
1728 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1729 return -1;
1730 }
vijay kumarc65876c2015-04-24 13:29:16 +05301731 }
1732
1733 ptn = partition_get_offset(index);
1734 ptn_size = partition_get_size(index);
1735 offset = ptn_size - blocksize;
1736
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001737 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301738 {
1739 dprintf(CRITICAL, "Reading MMC failed\n");
1740 return -1;
1741 }
1742
1743 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1744 is_allow_unlock = buf[blocksize-1] & 0x01;
1745 return 0;
1746}
1747
1748static int write_allow_oem_unlock(bool allow_unlock)
1749{
vijay kumarc65876c2015-04-24 13:29:16 +05301750 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301751 int index;
1752 unsigned long long ptn;
1753 unsigned long long ptn_size;
1754 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001755 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301756
vijay kumarca2e6812015-07-08 20:28:25 +05301757 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301758 if (index == INVALID_PTN)
1759 {
vijay kumarca2e6812015-07-08 20:28:25 +05301760 index = partition_get_index(frp_ptns[1]);
1761 if (index == INVALID_PTN)
1762 {
1763 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1764 return -1;
1765 }
vijay kumarc65876c2015-04-24 13:29:16 +05301766 }
1767
1768 ptn = partition_get_offset(index);
1769 ptn_size = partition_get_size(index);
1770 offset = ptn_size - blocksize;
1771
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001772 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301773 {
1774 dprintf(CRITICAL, "Reading MMC failed\n");
1775 return -1;
1776 }
1777
1778 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1779 buf[blocksize-1] = allow_unlock;
1780 if (mmc_write(ptn + offset, blocksize, buf))
1781 {
1782 dprintf(CRITICAL, "Writing MMC failed\n");
1783 return -1;
1784 }
1785
1786 return 0;
1787}
1788
Shashank Mittal162244e2011-08-08 19:01:25 -07001789void read_device_info_flash(device_info *dev)
1790{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001791 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001792 struct ptentry *ptn;
1793 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001794 if(info == NULL)
1795 {
1796 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1797 ASSERT(0);
1798 }
1799 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001800 ptable = flash_get_ptable();
1801 if (ptable == NULL)
1802 {
1803 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1804 return;
1805 }
1806
1807 ptn = ptable_find(ptable, "devinfo");
1808 if (ptn == NULL)
1809 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001810 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001811 return;
1812 }
1813
1814 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1815 {
1816 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1817 return;
1818 }
1819
1820 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1821 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001822 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1823 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001824 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001825 write_device_info_flash(info);
1826 }
1827 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001828 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001829}
1830
1831void write_device_info(device_info *dev)
1832{
1833 if(target_is_emmc_boot())
1834 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001835 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1836 if(info == NULL)
1837 {
1838 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1839 ASSERT(0);
1840 }
1841 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001842 memcpy(info, dev, sizeof(struct device_info));
1843
1844#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001845 if (is_secure_boot_enable()) {
1846 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1847 ASSERT(0);
1848 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001849 else
1850 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001851#else
1852 write_device_info_mmc(info);
1853#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001854 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001855 }
1856 else
1857 {
1858 write_device_info_flash(dev);
1859 }
1860}
1861
1862void read_device_info(device_info *dev)
1863{
1864 if(target_is_emmc_boot())
1865 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001866 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1867 if(info == NULL)
1868 {
1869 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1870 ASSERT(0);
1871 }
1872 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001873
1874#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001875 if (is_secure_boot_enable()) {
1876 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1877 ASSERT(0);
1878 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001879 else
1880 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001881#else
1882 read_device_info_mmc(info);
1883#endif
1884
1885 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1886 {
1887 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08001888 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001889 info->is_unlocked = 0;
lijuang511a2b52015-08-14 20:50:51 +08001890 info->is_unlock_critical = 0;
1891 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001892 info->is_unlocked = 1;
lijuang511a2b52015-08-14 20:50:51 +08001893 info->is_unlock_critical = 1;
1894 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001895 info->is_tampered = 0;
1896 info->charger_screen_enabled = 0;
Sridhar Parasuram287e5062015-08-24 16:17:22 -07001897 info->verity_mode = 1; //enforcing by default
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001898 write_device_info(info);
1899 }
1900 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001901 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001902 }
1903 else
1904 {
1905 read_device_info_flash(dev);
1906 }
1907}
1908
1909void reset_device_info()
1910{
1911 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001912 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001913 write_device_info(&device);
1914}
1915
1916void set_device_root()
1917{
1918 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001919 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001920 write_device_info(&device);
1921}
1922
lijuang4ece1e72015-08-14 21:02:36 +08001923/* set device unlock value
1924 * Must check FRP before call this function
1925 * Need to wipe data when unlock status changed
1926 * type 0: oem unlock
1927 * type 1: unlock critical
1928 * status 0: unlock as false
1929 * status 1: lock as true
1930 */
1931void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08001932{
lijuang4ece1e72015-08-14 21:02:36 +08001933 if (type == UNLOCK)
1934 device.is_unlocked = status;
1935 else if (type == UNLOCK_CRITICAL)
1936 device.is_unlock_critical = status;
1937
1938 write_device_info(&device);
1939}
1940
1941static void set_device_unlock(int type, bool status)
1942{
1943 int is_unlocked = -1;
1944 char response[MAX_RSP_SIZE];
1945
1946 /* check device unlock status if it is as expected */
1947 if (type == UNLOCK)
1948 is_unlocked = device.is_unlocked;
1949 else if (type == UNLOCK_CRITICAL)
1950 is_unlocked = device.is_unlock_critical;
1951
1952 if (is_unlocked == status) {
1953 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
1954 fastboot_info(response);
1955 fastboot_okay("");
1956 return;
1957 }
1958
1959 /* status is true, it means to unlock device */
1960 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08001961 if(!is_allow_unlock) {
1962 fastboot_fail("oem unlock is not allowed");
1963 return;
1964 }
1965
lijuang4ece1e72015-08-14 21:02:36 +08001966#if FBCON_DISPLAY_MSG
1967 display_unlock_menu_thread(type);
1968 fastboot_okay("");
1969 return;
1970#else
1971 if (type == UNLOCK) {
1972 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
1973 return;
1974 }
1975#endif
lijuang21f12f52015-08-22 16:22:19 +08001976 }
lijuang4ece1e72015-08-14 21:02:36 +08001977
1978 set_device_unlock_value(type, status);
1979
1980 /* wipe data */
1981 struct recovery_message msg;
1982
1983 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
1984 write_misc(0, &msg, sizeof(msg));
1985
1986 fastboot_okay("");
1987 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08001988}
1989
lijuang511a2b52015-08-14 20:50:51 +08001990static bool critical_flash_allowed(const char * entry)
1991{
1992 uint32_t i = 0;
1993 if (entry == NULL)
1994 return false;
1995
1996 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
1997 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
1998 return true;
1999 }
2000 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002001}
2002
2003#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002004int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2005{
2006 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002007 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002008 struct dt_table *table;
2009 struct dt_entry dt_entry;
2010 uint32_t dt_hdr_size;
2011 unsigned int compressed_size = 0;
2012 unsigned int dtb_size = 0;
2013 unsigned int out_avai_len = 0;
2014 unsigned char *out_addr = NULL;
2015 unsigned char *best_match_dt_addr = NULL;
2016 int rc;
2017
2018 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2019
2020 if(hdr->dt_size != 0) {
2021 /* add kernel offset */
2022 dt_image_offset += page_size;
2023 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002024 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002025
Deepa Dinamani19648b42013-09-05 17:05:55 -07002026 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002027 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2028 dt_image_offset += n;
2029
Joel Kingaa335dc2013-06-03 16:11:08 -07002030 /* add second offset */
2031 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002032 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2033 dt_image_offset += n;
2034 }
2035
Matthew Qin271927e2015-03-31 22:07:07 -04002036 /* offset now point to start of dt.img */
2037 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2038
2039 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2040 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002041 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002042 }
2043 /* Find index of device tree within device tree table */
2044 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2045 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2046 return -1;
Matthew Qin0b15b322015-05-19 05:20:54 -04002047 }
Matthew Qin271927e2015-03-31 22:07:07 -04002048
2049 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2050 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
Matthew Qin0b15b322015-05-19 05:20:54 -04002051 {
Matthew Qin271927e2015-03-31 22:07:07 -04002052 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2053 out_avai_len = target_get_max_flash_size() - scratch_offset;
2054 dprintf(INFO, "decompressing dtb: start\n");
2055 rc = decompress(best_match_dt_addr,
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002056 dt_entry.size, out_addr, out_avai_len,
Matthew Qin271927e2015-03-31 22:07:07 -04002057 &compressed_size, &dtb_size);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002058 if (rc)
2059 {
2060 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
2061 ASSERT(0);
2062 }
Amol Jadicb524072012-08-09 16:40:18 -07002063
Matthew Qin271927e2015-03-31 22:07:07 -04002064 dprintf(INFO, "decompressing dtb: done\n");
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002065 best_match_dt_addr = out_addr;
2066 } else {
Amol Jadicb524072012-08-09 16:40:18 -07002067 dtb_size = dt_entry.size;
2068 }
2069 /* Validate and Read device device tree in the "tags_add */
2070 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
2071 {
2072 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002073 return -1;
2074 }
Amit Blay8a510302015-08-17 09:20:01 +03002075
2076 /* Read device device tree in the "tags_add */
2077 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002078 } else
2079 return -1;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002080
2081 /* Everything looks fine. Return success. */
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002082 return 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002083}
2084#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002085
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002086void cmd_boot(const char *arg, void *data, unsigned sz)
2087{
Matthew Qinbb7923d2015-02-09 10:56:09 +08002088#ifdef MDTP_SUPPORT
2089 static bool is_mdtp_activated = 0;
2090#endif /* MDTP_SUPPORT */
2091 unsigned kernel_actual;
2092 unsigned ramdisk_actual;
2093 uint32_t image_actual;
Matthew Qin271927e2015-03-31 22:07:07 -04002094 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002095 uint32_t sig_actual = SIGNATURE_SIZE;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002096 struct boot_img_hdr *hdr = NULL;
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002097 struct kernel64_hdr *kptr = NULL;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002098 char *ptr = ((char*) data);
2099 int ret = 0;
2100 uint8_t dtb_copied = 0;
2101 unsigned int out_len = 0;
2102 unsigned int out_avai_len = 0;
2103 unsigned char *out_addr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002104 uint32_t dtb_offset = 0;
2105 unsigned char *kernel_start_addr = NULL;
2106 unsigned int kernel_size = 0;
2107 unsigned int scratch_offset = 0;
2108
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002109#if VERIFIED_BOOT
Brian Swetland9c4c0752009-01-25 16:23:50 -08002110 if(target_build_variant_user() && !device.is_unlocked)
2111 {
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002112 fastboot_fail("unlock device to use this command");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002113 return;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002114 }
2115#endif
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002116
2117 if (sz < sizeof(hdr)) {
2118 fastboot_fail("invalid bootimage header");
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002119 return;
2120 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002121
2122 hdr = (struct boot_img_hdr *)data;
2123
2124 /* ensure commandline is terminated */
2125 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2126
2127 if(target_is_emmc_boot() && hdr->page_size) {
2128 page_size = hdr->page_size;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002129 page_mask = page_size - 1;
2130 }
2131
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002132 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2133 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002134#if DEVICE_TREE
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002135 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2136#endif
2137
2138 image_actual = ADD_OF(page_size, kernel_actual);
2139 image_actual = ADD_OF(image_actual, ramdisk_actual);
2140 image_actual = ADD_OF(image_actual, dt_actual);
2141
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002142 if (target_use_signed_kernel() && (!device.is_unlocked))
2143 image_actual = ADD_OF(image_actual, sig_actual);
2144
2145 /* sz should have atleast raw boot image */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002146 if (image_actual > sz) {
Amit Blay4aa292f2015-04-28 21:55:59 +03002147 fastboot_fail("bootimage: incomplete or not signed");
2148 return;
2149 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002150
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002151 // Initialize boot state before trying to verify boot.img
2152#if VERIFIED_BOOT
2153 boot_verifier_init();
2154#endif
2155
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002156 /* Verify the boot image
2157 * device & page_size are initialized in aboot_init
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002158 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002159 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002160 /* Pass size excluding signature size, otherwise we would try to
2161 * access signature beyond its length
2162 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002163 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002164 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002165#ifdef MDTP_SUPPORT
2166 else
2167 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002168 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002169 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002170
2171 if (!is_mdtp_activated) {
2172 ext_partition.partition = MDTP_PARTITION_NONE;
2173 mdtp_fwlock_verify_lock(&ext_partition);
2174 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002175 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002176
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002177 mdtp_activated(&is_mdtp_activated);
2178 if(is_mdtp_activated){
2179 dprintf(CRITICAL, "fastboot boot command is not available.\n");
2180 return;
2181 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002182#endif /* MDTP_SUPPORT */
2183
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002184#if VERIFIED_BOOT
2185 // send root of trust
2186 if(!send_rot_command((uint32_t)device.is_unlocked))
2187 ASSERT(0);
2188#endif
2189
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002190 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002191 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2192 * If not, continue booting.
2193 */
2194 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2195 {
2196 out_addr = (unsigned char *)target_get_scratch_address();
2197 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2198 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002199 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002200 ret = decompress((unsigned char *)(ptr + page_size),
2201 hdr->kernel_size, out_addr, out_avai_len,
2202 &dtb_offset, &out_len);
2203 if (ret)
2204 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002205 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002206 ASSERT(0);
2207 }
2208
Matthew Qin0b15b322015-05-19 05:20:54 -04002209 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002210 kptr = (struct kernel64_hdr *)out_addr;
2211 kernel_start_addr = out_addr;
2212 kernel_size = out_len;
2213 } else {
2214 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2215 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2216 kernel_size = hdr->kernel_size;
2217 }
2218
2219 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002220 * Update the kernel/ramdisk/tags address if the boot image header
2221 * has default values, these default values come from mkbootimg when
2222 * the boot image is flashed using fastboot flash:raw
2223 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002224 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002225
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002226 /* Get virtual addresses since the hdr saves physical addresses. */
2227 hdr->kernel_addr = VA(hdr->kernel_addr);
2228 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2229 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002230
Matthew Qinbb7923d2015-02-09 10:56:09 +08002231 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002232 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002233 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002234 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2235 {
2236 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002237 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002238 }
2239
Amol Jadicb524072012-08-09 16:40:18 -07002240#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002241 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002242 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002243 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002244
2245 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002246#else
2247 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2248 {
2249 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002250 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002251 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002252#endif
2253
2254 /* Load ramdisk & kernel */
2255 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002256 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002257
2258#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002259 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2260 {
2261 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2262 return;
2263 }
2264
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002265 /*
2266 * If dtb is not found look for appended DTB in the kernel.
2267 * If appended dev tree is found, update the atags with
2268 * memory address to the DTB appended location on RAM.
2269 * Else update with the atags address in the kernel header
2270 */
2271 if (!dtb_copied) {
2272 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002273 dtb = dev_tree_appended((void*)(ptr + page_size),
2274 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002275 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002276 if (!dtb) {
2277 fastboot_fail("dtb not found");
2278 return;
2279 }
Amol Jadicb524072012-08-09 16:40:18 -07002280 }
2281#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002282
2283 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002284 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002285
Dima Zavin77e41f32013-03-06 16:10:43 -08002286 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002287 (const char*) hdr->cmdline, board_machtype(),
2288 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002289}
2290
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002291void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002292{
2293 struct ptentry *ptn;
2294 struct ptable *ptable;
2295
2296 ptable = flash_get_ptable();
2297 if (ptable == NULL) {
2298 fastboot_fail("partition table doesn't exist");
2299 return;
2300 }
2301
2302 ptn = ptable_find(ptable, arg);
2303 if (ptn == NULL) {
2304 fastboot_fail("unknown partition name");
2305 return;
2306 }
2307
2308 if (flash_erase(ptn)) {
2309 fastboot_fail("failed to erase partition");
2310 return;
2311 }
2312 fastboot_okay("");
2313}
2314
Bikas Gurungd48bd242010-09-04 19:54:32 -07002315
2316void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2317{
2318 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002319 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002320 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002321 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002322
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002323#if VERIFIED_BOOT
2324 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2325 {
2326 if(!device.is_unlocked)
2327 {
2328 fastboot_fail("unlock device to erase keystore");
2329 return;
2330 }
2331 }
2332#endif
2333
Kinson Chikf1a43512011-07-14 11:28:39 -07002334 index = partition_get_index(arg);
2335 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002336 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002337
Kinson Chikf1a43512011-07-14 11:28:39 -07002338 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002339 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002340 return;
2341 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002342
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002343 lun = partition_get_lun(index);
2344 mmc_set_lun(lun);
2345
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002346 if (platform_boot_dev_isemmc())
2347 {
2348 if (mmc_erase_card(ptn, size)) {
2349 fastboot_fail("failed to erase partition\n");
2350 return;
2351 }
2352 } else {
2353 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2354 size = partition_get_size(index);
2355 if (size > DEFAULT_ERASE_SIZE)
2356 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002357
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002358 /* Simple inefficient version of erase. Just writing
2359 0 in first several blocks */
2360 if (mmc_write(ptn , size, (unsigned int *)out)) {
2361 fastboot_fail("failed to erase partition");
2362 return;
2363 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002364 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002365#if VERIFIED_BOOT
2366 if(!(strncmp(arg, "userdata", 8)))
2367 if(send_delete_keys_to_tz())
2368 ASSERT(0);
2369#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002370 fastboot_okay("");
2371}
2372
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002373void cmd_erase(const char *arg, void *data, unsigned sz)
2374{
lijuang65c5a822015-08-29 16:35:36 +08002375#if CHECK_BAT_VOLTAGE
2376 if (!target_battery_soc_ok()) {
2377 fastboot_fail("Warning: battery's capacity is very low\n");
2378 return;
2379 }
2380#endif
2381
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002382#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002383 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002384 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002385 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002386 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002387 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002388 return;
2389 }
2390 }
2391#endif
2392
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002393 if(target_is_emmc_boot())
2394 cmd_erase_mmc(arg, data, sz);
2395 else
2396 cmd_erase_nand(arg, data, sz);
2397}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002398
Channagoud Kadabiad259832015-05-29 11:14:17 -07002399static uint32_t aboot_get_secret_key()
2400{
2401 /* 0 is invalid secret key, update this implementation to return
2402 * device specific unique secret key
2403 */
2404 return 0;
2405}
2406
Ajay Dudani5c761132011-04-07 20:19:04 -07002407void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002408{
2409 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002410 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002411 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002412 char *token = NULL;
2413 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002414 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002415 uint8_t lun = 0;
2416 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002417
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002418 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002419 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002420 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002421 if(token)
2422 {
2423 lun = atoi(token);
2424 mmc_set_lun(lun);
2425 lun_set = true;
2426 }
2427
Mao Jinlong226f33a2014-07-04 17:24:10 +08002428 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002429 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002430 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2431 {
2432 if (!aboot_frp_unlock(pname, data, sz))
2433 {
2434 fastboot_info("FRP unlock successful");
2435 fastboot_okay("");
2436 }
2437 else
2438 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2439
2440 return;
2441 }
2442
Mao Jinlong226f33a2014-07-04 17:24:10 +08002443 if (!strcmp(pname, "partition"))
2444 {
2445 dprintf(INFO, "Attempt to write partition image.\n");
2446 if (write_partition(sz, (unsigned char *) data)) {
2447 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002448 return;
2449 }
2450 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002451 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002452 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002453#if VERIFIED_BOOT
2454 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2455 {
2456 if(!device.is_unlocked)
2457 {
2458 fastboot_fail("unlock device to flash keystore");
2459 return;
2460 }
2461 if(!boot_verify_validate_keystore((unsigned char *)data))
2462 {
2463 fastboot_fail("image is not a keystore file");
2464 return;
2465 }
2466 }
2467#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002468 index = partition_get_index(pname);
2469 ptn = partition_get_offset(index);
2470 if(ptn == 0) {
2471 fastboot_fail("partition table doesn't exist");
2472 return;
2473 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002474
Mao Jinlong226f33a2014-07-04 17:24:10 +08002475 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2476 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2477 fastboot_fail("image is not a boot image");
2478 return;
2479 }
2480 }
2481
2482 if(!lun_set)
2483 {
2484 lun = partition_get_lun(index);
2485 mmc_set_lun(lun);
2486 }
2487
2488 size = partition_get_size(index);
2489 if (ROUND_TO_PAGE(sz,511) > size) {
2490 fastboot_fail("size too large");
2491 return;
2492 }
2493 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2494 fastboot_fail("flash write failure");
2495 return;
2496 }
Greg Grisco6e754772011-06-23 12:19:39 -07002497 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002498 }
2499 fastboot_okay("");
2500 return;
2501}
2502
Ajay Dudanide984792015-03-02 09:57:41 -08002503void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2504{
2505 int i, images;
2506 meta_header_t *meta_header;
2507 img_header_entry_t *img_header_entry;
2508
2509 meta_header = (meta_header_t*) data;
2510 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2511
2512 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2513
2514 for (i=0; i<images; i++) {
2515
2516 if((img_header_entry[i].ptn_name == NULL) ||
2517 (img_header_entry[i].start_offset == 0) ||
2518 (img_header_entry[i].size == 0))
2519 break;
2520
2521 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2522 (void *) data + img_header_entry[i].start_offset,
2523 img_header_entry[i].size);
2524 }
2525
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002526 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2527 {
2528 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2529 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2530 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2531 }
2532 else
2533 {
2534 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2535 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2536 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2537 }
2538
2539 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002540 fastboot_okay("");
2541 return;
2542}
2543
Ajay Dudani5c761132011-04-07 20:19:04 -07002544void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2545{
2546 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002547 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002548 uint32_t *fill_buf = NULL;
2549 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002550 sparse_header_t *sparse_header;
2551 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002552 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002553 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302554 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002555 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302556 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002557 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302558 /*End of the sparse image address*/
2559 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002560
Kinson Chikf1a43512011-07-14 11:28:39 -07002561 index = partition_get_index(arg);
2562 ptn = partition_get_offset(index);
2563 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002564 fastboot_fail("partition table doesn't exist");
2565 return;
2566 }
2567
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302568 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302569
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002570 lun = partition_get_lun(index);
2571 mmc_set_lun(lun);
2572
vijay kumar800255e2015-04-24 20:26:19 +05302573 if (sz < sizeof(sparse_header_t)) {
2574 fastboot_fail("size too low");
2575 return;
2576 }
2577
Ajay Dudani5c761132011-04-07 20:19:04 -07002578 /* Read and skip over sparse image header */
2579 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302580
vijay kumar1321f342015-03-27 12:13:42 +05302581 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002582 fastboot_fail("size too large");
2583 return;
2584 }
2585
vijay kumarde4fcf62015-04-23 13:05:49 +05302586 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302587
2588 if (data_end < (uint32_t)data) {
2589 fastboot_fail("buffer overreads occured due to invalid sparse header");
2590 return;
2591 }
2592
vijay kumarde4fcf62015-04-23 13:05:49 +05302593 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002594 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302595 fastboot_fail("sparse header size mismatch");
2596 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002597 }
2598
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002599 dprintf (SPEW, "=== Sparse Image Header ===\n");
2600 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2601 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2602 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2603 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2604 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2605 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2606 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2607 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002608
2609 /* Start processing chunks */
2610 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2611 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302612 /* Make sure the total image size does not exceed the partition size */
2613 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2614 fastboot_fail("size too large");
2615 return;
2616 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002617 /* Read and skip over chunk header */
2618 chunk_header = (chunk_header_t *) data;
2619 data += sizeof(chunk_header_t);
2620
vijay kumar800255e2015-04-24 20:26:19 +05302621 if (data_end < (uint32_t)data) {
2622 fastboot_fail("buffer overreads occured due to invalid sparse header");
2623 return;
2624 }
2625
Ajay Dudani5c761132011-04-07 20:19:04 -07002626 dprintf (SPEW, "=== Chunk Header ===\n");
2627 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2628 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2629 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2630
vijay kumar800255e2015-04-24 20:26:19 +05302631 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002632 {
vijay kumar800255e2015-04-24 20:26:19 +05302633 fastboot_fail("chunk header size mismatch");
2634 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002635 }
2636
wufeng.jiang813dc352015-06-02 23:02:46 -04002637 if (!sparse_header->blk_sz ){
2638 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302639 return;
2640 }
2641
wufeng.jiang813dc352015-06-02 23:02:46 -04002642 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2643
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302644 /* Make sure that the chunk size calculated from sparse image does not
2645 * exceed partition size
2646 */
2647 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2648 {
2649 fastboot_fail("Chunk data size exceeds partition size");
2650 return;
2651 }
2652
Ajay Dudani5c761132011-04-07 20:19:04 -07002653 switch (chunk_header->chunk_type)
2654 {
2655 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002656 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002657 chunk_data_sz))
2658 {
2659 fastboot_fail("Bogus chunk size for chunk type Raw");
2660 return;
2661 }
2662
vijay kumar800255e2015-04-24 20:26:19 +05302663 if (data_end < (uint32_t)data + chunk_data_sz) {
2664 fastboot_fail("buffer overreads occured due to invalid sparse header");
2665 return;
2666 }
2667
wufeng.jiang813dc352015-06-02 23:02:46 -04002668 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2669 otherwise it will return in the line above
2670 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002671 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002672 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002673 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002674 {
2675 fastboot_fail("flash write failure");
2676 return;
2677 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302678 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2679 fastboot_fail("Bogus size for RAW chunk type");
2680 return;
2681 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002682 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002683 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002684 break;
2685
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002686 case CHUNK_TYPE_FILL:
2687 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2688 sizeof(uint32_t)))
2689 {
2690 fastboot_fail("Bogus chunk size for chunk type FILL");
2691 return;
2692 }
2693
2694 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2695 if (!fill_buf)
2696 {
2697 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2698 return;
2699 }
2700
vijay kumar800255e2015-04-24 20:26:19 +05302701 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2702 fastboot_fail("buffer overreads occured due to invalid sparse header");
2703 return;
2704 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002705 fill_val = *(uint32_t *)data;
2706 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002707
2708 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2709 {
2710 fill_buf[i] = fill_val;
2711 }
2712
wufeng.jiang813dc352015-06-02 23:02:46 -04002713 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002714 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302715 /* Make sure that the data written to partition does not exceed partition size */
2716 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2717 {
2718 fastboot_fail("Chunk data size for fill type exceeds partition size");
2719 return;
2720 }
2721
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002722 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2723 sparse_header->blk_sz,
2724 fill_buf))
2725 {
2726 fastboot_fail("flash write failure");
2727 free(fill_buf);
2728 return;
2729 }
2730
2731 total_blocks++;
2732 }
2733
2734 free(fill_buf);
2735 break;
2736
Ajay Dudani5c761132011-04-07 20:19:04 -07002737 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302738 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2739 fastboot_fail("bogus size for chunk DONT CARE type");
2740 return;
2741 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002742 total_blocks += chunk_header->chunk_sz;
2743 break;
2744
Ajay Dudani5c761132011-04-07 20:19:04 -07002745 case CHUNK_TYPE_CRC:
2746 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2747 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002748 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002749 return;
2750 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302751 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2752 fastboot_fail("bogus size for chunk CRC type");
2753 return;
2754 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002755 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302756 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2757 fastboot_fail("integer overflow occured");
2758 return;
2759 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002760 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302761 if (data_end < (uint32_t)data) {
2762 fastboot_fail("buffer overreads occured due to invalid sparse header");
2763 return;
2764 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002765 break;
2766
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002767 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002768 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002769 fastboot_fail("Unknown chunk type");
2770 return;
2771 }
2772 }
2773
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002774 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2775 total_blocks, sparse_header->total_blks);
2776
2777 if(total_blocks != sparse_header->total_blks)
2778 {
2779 fastboot_fail("sparse image write failure");
2780 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002781
2782 fastboot_okay("");
2783 return;
2784}
2785
2786void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2787{
2788 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002789 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002790
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002791#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002792 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2793 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002794 int ret=0;
2795 uint32 major_version=0;
2796 uint32 minor_version=0;
2797
2798 ret = scm_svc_version(&major_version,&minor_version);
2799 if(!ret)
2800 {
2801 if(major_version >= 2)
2802 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002803 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002804 {
2805 ret = encrypt_scm((uint32 **) &data, &sz);
2806 if (ret != 0) {
2807 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2808 return;
2809 }
2810
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002811 /* Protect only for SSD */
2812 if (!strcmp(arg, "ssd")) {
2813 ret = scm_protect_keystore((uint32 *) data, sz);
2814 if (ret != 0) {
2815 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2816 return;
2817 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002818 }
2819 }
2820 else
2821 {
2822 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2823 if(ret != 0)
2824 {
2825 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2826 return;
2827 }
2828 }
2829 }
2830 else
2831 {
2832 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2833 magic_number[1] == DECRYPT_MAGIC_1)
2834 {
2835 ret = decrypt_scm((uint32 **) &data, &sz);
2836 if (ret != 0) {
2837 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2838 return;
2839 }
2840 }
2841 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2842 magic_number[1] == ENCRYPT_MAGIC_1)
2843 {
2844 ret = encrypt_scm((uint32 **) &data, &sz);
2845 if (ret != 0) {
2846 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2847 return;
2848 }
2849 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002850 }
2851 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002852 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002853 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002854 dprintf(CRITICAL,"INVALID SVC Version\n");
2855 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002856 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002857#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002858
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002859#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002860 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002861 {
lijuang511a2b52015-08-14 20:50:51 +08002862 /* if device is locked:
2863 * common partition will not allow to be flashed
2864 * critical partition will allow to flash image.
2865 */
2866 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
2867 fastboot_fail("Partition flashing is not allowed");
2868 return;
2869 }
2870
2871 /* if device critical is locked:
2872 * common partition will allow to be flashed
2873 * critical partition will not allow to flash image.
2874 */
2875 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
2876 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002877 return;
2878 }
2879 }
2880#endif
2881
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002882 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002883 meta_header = (meta_header_t *) data;
2884 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002885 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002886 else if (meta_header->magic == META_HEADER_MAGIC)
2887 cmd_flash_meta_img(arg, data, sz);
2888 else
2889 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002890
2891#if VERIFIED_BOOT
2892 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
2893 {
2894 // reset dm_verity mode to enforcing
2895 device.verity_mode = 1;
2896 write_device_info(&device);
2897 }
2898#endif
2899
Ajay Dudani5c761132011-04-07 20:19:04 -07002900 return;
2901}
2902
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002903void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2904{
2905 struct ptentry *sys_ptn;
2906 struct ptable *ptable;
2907
2908 ptable = flash_get_ptable();
2909 if (ptable == NULL) {
2910 fastboot_fail("partition table doesn't exist");
2911 return;
2912 }
2913
2914 sys_ptn = ptable_find(ptable, "system");
2915 if (sys_ptn == NULL) {
2916 fastboot_fail("system partition not found");
2917 return;
2918 }
2919
2920 sz = ROUND_TO_PAGE(sz, page_mask);
2921 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2922 fastboot_fail("update_ubi_vol failed");
2923 else
2924 fastboot_okay("");
2925}
2926
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002927void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002928{
2929 struct ptentry *ptn;
2930 struct ptable *ptable;
2931 unsigned extra = 0;
2932
2933 ptable = flash_get_ptable();
2934 if (ptable == NULL) {
2935 fastboot_fail("partition table doesn't exist");
2936 return;
2937 }
2938
2939 ptn = ptable_find(ptable, arg);
2940 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002941 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2942 arg);
2943 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002944 return;
2945 }
2946
2947 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2948 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2949 fastboot_fail("image is not a boot image");
2950 return;
2951 }
2952 }
2953
Amol Jadi5c61a952012-05-04 17:05:35 -07002954 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002955 || !strcmp(ptn->name, "userdata")
2956 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002957 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002958 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002959 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002960 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002961 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002962
2963 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002964 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2965 if (flash_ubi_img(ptn, data, sz)) {
2966 fastboot_fail("flash write failure");
2967 return;
2968 }
2969 } else {
2970 if (flash_write(ptn, extra, data, sz)) {
2971 fastboot_fail("flash write failure");
2972 return;
2973 }
Dima Zavin214cc642009-01-26 11:16:21 -08002974 }
2975 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2976 fastboot_okay("");
2977}
2978
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002979void cmd_flash(const char *arg, void *data, unsigned sz)
2980{
lijuang65c5a822015-08-29 16:35:36 +08002981#if CHECK_BAT_VOLTAGE
2982 if (!target_battery_soc_ok()) {
2983 fastboot_fail("Warning: battery's capacity is very low\n");
2984 return;
2985 }
2986#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002987 if(target_is_emmc_boot())
2988 cmd_flash_mmc(arg, data, sz);
2989 else
2990 cmd_flash_nand(arg, data, sz);
2991}
2992
Dima Zavin214cc642009-01-26 11:16:21 -08002993void cmd_continue(const char *arg, void *data, unsigned sz)
2994{
2995 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002996 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002997
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002998 if (target_is_emmc_boot())
2999 {
lijuanga40d6302015-07-20 20:10:13 +08003000#if FBCON_DISPLAY_MSG
3001 keys_detect_init();
3002#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003003 boot_linux_from_mmc();
3004 }
3005 else
3006 {
3007 boot_linux_from_flash();
3008 }
Dima Zavin214cc642009-01-26 11:16:21 -08003009}
3010
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003011void cmd_reboot(const char *arg, void *data, unsigned sz)
3012{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003013 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003014 fastboot_okay("");
3015 reboot_device(0);
3016}
3017
3018void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3019{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003020 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003021 fastboot_okay("");
3022 reboot_device(FASTBOOT_MODE);
3023}
3024
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003025void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3026{
3027 dprintf(INFO, "Enabling charger screen check\n");
3028 device.charger_screen_enabled = 1;
3029 write_device_info(&device);
3030 fastboot_okay("");
3031}
3032
3033void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3034{
3035 dprintf(INFO, "Disabling charger screen check\n");
3036 device.charger_screen_enabled = 0;
3037 write_device_info(&device);
3038 fastboot_okay("");
3039}
3040
lijuanga25d8bb2015-09-16 13:11:52 +08003041void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3042{
3043 char *p = NULL;
3044 const char *delim = " \t\n\r";
3045
3046 if (arg) {
3047 p = strtok((char *)arg, delim);
3048 if (p) {
3049 if (!strncmp(p, "0", 1)) {
3050 device.charger_screen_enabled = 0;
3051 } else if (!strncmp(p, "1", 1)) {
3052 device.charger_screen_enabled = 1;
3053 }
3054 }
3055 }
3056
3057 /* update charger_screen_enabled value for getvar
3058 * command
3059 */
3060 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3061 device.charger_screen_enabled);
3062
3063 write_device_info(&device);
3064 fastboot_okay("");
3065}
3066
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303067void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3068{
3069 dprintf(INFO, "Selecting display panel %s\n", arg);
3070 if (arg)
3071 strlcpy(device.display_panel, arg,
3072 sizeof(device.display_panel));
3073 write_device_info(&device);
3074 fastboot_okay("");
3075}
3076
Shashank Mittal162244e2011-08-08 19:01:25 -07003077void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3078{
lijuang4ece1e72015-08-14 21:02:36 +08003079 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303080}
3081
3082void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3083{
lijuang4ece1e72015-08-14 21:02:36 +08003084 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303085 if(!is_allow_unlock) {
3086 fastboot_fail("oem unlock is not allowed");
3087 return;
3088 }
3089
lijuang4ece1e72015-08-14 21:02:36 +08003090 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303091
lijuang4ece1e72015-08-14 21:02:36 +08003092 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303093 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003094
vijay kumarc65876c2015-04-24 13:29:16 +05303095 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3096 write_misc(0, &msg, sizeof(msg));
3097
3098 fastboot_okay("");
3099 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003100 }
3101 fastboot_okay("");
3102}
3103
Channagoud Kadabiad259832015-05-29 11:14:17 -07003104static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3105{
3106 int ret = 1;
3107 uint32_t secret_key;
3108 char seckey_buffer[MAX_RSP_SIZE];
3109
3110 secret_key = aboot_get_secret_key();
3111 if (secret_key)
3112 {
3113 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3114 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3115 {
3116 is_allow_unlock = true;
3117 write_allow_oem_unlock(is_allow_unlock);
3118 ret = 0;
3119 }
3120 }
3121 return ret;
3122}
3123
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003124void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3125{
lijuang4ece1e72015-08-14 21:02:36 +08003126 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003127}
3128
Shashank Mittala0032282011-08-26 14:50:11 -07003129void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3130{
lijuang511a2b52015-08-14 20:50:51 +08003131 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003132 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003133 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003134 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3135 fastboot_info(response);
lijuang511a2b52015-08-14 20:50:51 +08003136 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3137 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003138 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003139 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303140 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3141 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003142 fastboot_okay("");
3143}
3144
lijuang511a2b52015-08-14 20:50:51 +08003145void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3146{
3147 char response[MAX_RSP_SIZE];
3148 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3149 fastboot_info(response);
3150 fastboot_okay("");
3151}
3152
3153void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3154{
lijuang4ece1e72015-08-14 21:02:36 +08003155 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003156}
3157
3158void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3159{
lijuang4ece1e72015-08-14 21:02:36 +08003160 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003161}
3162
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003163void cmd_preflash(const char *arg, void *data, unsigned sz)
3164{
3165 fastboot_okay("");
3166}
3167
Mao Flynn7b379f32015-04-20 00:28:30 +08003168static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303169
Mao Flynn7b379f32015-04-20 00:28:30 +08003170int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303171{
Mao Flynn7b379f32015-04-20 00:28:30 +08003172 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303173 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003174 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303175 return -1;
3176 return 0;
3177}
3178
Mao Flynn7b379f32015-04-20 00:28:30 +08003179int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003180{
3181 struct ptentry *ptn;
3182 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003183 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003184 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003185
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303186 ptable = flash_get_ptable();
3187 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003188 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3189 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303190 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003191
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303192 ptn = ptable_find(ptable, "splash");
3193 if (ptn == NULL) {
3194 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003195 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303196 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003197 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303198 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003199 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303200 }
3201
Mao Flynn7b379f32015-04-20 00:28:30 +08003202 header = (struct logo_img_header *)logo_header;
3203 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303204 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003205 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303206 }
3207
3208 fb_display = fbcon_display();
3209 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003210 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3211 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3212
3213 /* if the logo is full-screen size, remove "fbcon_clear()" */
3214 if ((header->width != fb_display->width)
3215 || (header->height != fb_display->height))
3216 fbcon_clear();
3217
3218 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3219 (uint32_t *)base,
3220 (header->blocks * 512))) {
3221 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3222 return -1;
3223 }
3224 fbcon_extract_to_screen(header, base);
3225 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003226 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003227
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003228 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3229 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003230 return -1;
3231 }
3232
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303233 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003234 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3235 (uint32_t *)base,
3236 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303237 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303238 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003239 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003240 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303241 }
3242
Mao Flynn7b379f32015-04-20 00:28:30 +08003243 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303244}
3245
Mao Flynn7b379f32015-04-20 00:28:30 +08003246int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303247{
3248 int index = INVALID_PTN;
3249 unsigned long long ptn = 0;
3250 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003251 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003252 uint32_t blocksize, realsize, readsize;
3253 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303254
3255 index = partition_get_index("splash");
3256 if (index == 0) {
3257 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003258 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303259 }
3260
3261 ptn = partition_get_offset(index);
3262 if (ptn == 0) {
3263 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003264 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303265 }
3266
Mao Flynn1893a7f2015-06-03 12:03:36 +08003267 mmc_set_lun(partition_get_lun(index));
3268
3269 blocksize = mmc_get_device_blocksize();
3270 if (blocksize == 0) {
3271 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3272 return -1;
3273 }
3274
3275 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003276 if (!fb_display)
3277 {
3278 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3279 return -1;
3280 }
3281
Mao Flynn1893a7f2015-06-03 12:03:36 +08003282 base = (uint8_t *) fb_display->base;
3283
3284 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303285 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003286 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303287 }
3288
Mao Flynn1893a7f2015-06-03 12:03:36 +08003289 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003290 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303291 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003292 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303293 }
3294
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303295 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003296 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3297 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003298
Mao Flynn1893a7f2015-06-03 12:03:36 +08003299 realsize = header->blocks * 512;
3300 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3301
3302 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003303 if ((header->width != fb_display->width)
3304 || (header->height != fb_display->height))
3305 fbcon_clear();
3306
Mao Flynn1893a7f2015-06-03 12:03:36 +08003307 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003308 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3309 return -1;
3310 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003311
Mao Flynn1893a7f2015-06-03 12:03:36 +08003312 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3313 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303314
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003315 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3316 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003317 return -1;
3318 }
3319
3320 realsize = header->width * header->height * fb_display->bpp / 8;
3321 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3322
3323 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3324 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3325 fbcon_clear();
3326 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3327 return -1;
3328 }
3329 } else {
3330 if (mmc_read(ptn + blocksize ,
3331 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3332 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3333 return -1;
3334 }
3335 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3336 }
3337 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303338 }
3339
Mao Flynn7b379f32015-04-20 00:28:30 +08003340 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303341}
3342
Mao Flynn7b379f32015-04-20 00:28:30 +08003343int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303344{
3345 if (target_is_emmc_boot()) {
3346 return splash_screen_mmc();
3347 } else {
3348 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003349 }
3350}
3351
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003352/* Get the size from partiton name */
3353static void get_partition_size(const char *arg, char *response)
3354{
3355 uint64_t ptn = 0;
3356 uint64_t size;
3357 int index = INVALID_PTN;
3358
3359 index = partition_get_index(arg);
3360
3361 if (index == INVALID_PTN)
3362 {
3363 dprintf(CRITICAL, "Invalid partition index\n");
3364 return;
3365 }
3366
3367 ptn = partition_get_offset(index);
3368
3369 if(!ptn)
3370 {
3371 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3372 return;
3373 }
3374
3375 size = partition_get_size(index);
3376
3377 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3378 return;
3379}
3380
3381/*
3382 * Publish the partition type & size info
3383 * fastboot getvar will publish the required information.
3384 * fastboot getvar partition_size:<partition_name>: partition size in hex
3385 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3386 */
3387static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3388{
3389 uint8_t i;
3390
3391 for (i = 0; i < num_parts; i++) {
3392 get_partition_size(info[i].part_name, info[i].size_response);
3393
3394 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3395 {
3396 dprintf(CRITICAL, "partition size name truncated\n");
3397 return;
3398 }
3399 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3400 {
3401 dprintf(CRITICAL, "partition type name truncated\n");
3402 return;
3403 }
3404
3405 /* publish partition size & type info */
3406 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3407 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3408 }
3409}
3410
lijuang4ece1e72015-08-14 21:02:36 +08003411void get_product_name(unsigned char *buf)
3412{
3413 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3414 return;
3415}
3416
3417void get_bootloader_version(unsigned char *buf)
3418{
3419 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3420 return;
3421}
3422
3423void get_baseband_version(unsigned char *buf)
3424{
3425 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3426 return;
3427}
3428
3429bool is_device_locked()
3430{
3431 return device.is_unlocked ? false:true;
3432}
3433
Amol Jadi5edf3552013-07-23 14:15:34 -07003434/* register commands and variables for fastboot */
3435void aboot_fastboot_register_commands(void)
3436{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003437 int i;
lijuangf16461c2015-08-03 17:09:34 +08003438 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003439
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003440 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003441 /* By default the enabled list is empty. */
3442 {"", NULL},
3443 /* move commands enclosed within the below ifndef to here
3444 * if they need to be enabled in user build.
3445 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003446#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003447 /* Register the following commands only for non-user builds */
3448 {"flash:", cmd_flash},
3449 {"erase:", cmd_erase},
3450 {"boot", cmd_boot},
3451 {"continue", cmd_continue},
3452 {"reboot", cmd_reboot},
3453 {"reboot-bootloader", cmd_reboot_bootloader},
3454 {"oem unlock", cmd_oem_unlock},
3455 {"oem unlock-go", cmd_oem_unlock_go},
3456 {"oem lock", cmd_oem_lock},
3457 {"flashing unlock", cmd_oem_unlock},
3458 {"flashing lock", cmd_oem_lock},
3459 {"flashing lock_critical", cmd_flashing_lock_critical},
3460 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3461 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3462 {"oem device-info", cmd_oem_devinfo},
3463 {"preflash", cmd_preflash},
3464 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3465 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003466 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003467 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003468#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003469 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003470#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003471#endif
lijuang511a2b52015-08-14 20:50:51 +08003472 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003473
3474 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3475 for (i = 1; i < fastboot_cmds_count; i++)
3476 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3477
Amol Jadi5edf3552013-07-23 14:15:34 -07003478 /* publish variables and their values */
3479 fastboot_publish("product", TARGET(BOARD));
3480 fastboot_publish("kernel", "lk");
3481 fastboot_publish("serialno", sn_buf);
3482
3483 /*
3484 * partition info is supported only for emmc partitions
3485 * Calling this for NAND prints some error messages which
3486 * is harmless but misleading. Avoid calling this for NAND
3487 * devices.
3488 */
3489 if (target_is_emmc_boot())
3490 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3491
3492 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003493 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3494 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003495 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003496 /* Is the charger screen check enabled */
3497 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3498 device.charger_screen_enabled);
3499 fastboot_publish("charger-screen-enabled",
3500 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003501 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303502 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3503 device.display_panel);
3504 fastboot_publish("display-panel",
3505 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003506 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3507 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003508 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3509 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3510 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3511 target_is_emmc_boot()? "eMMC":"UFS");
3512 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003513#if CHECK_BAT_VOLTAGE
3514 snprintf(battery_voltage, MAX_RSP_SIZE, "%d",
3515 target_get_battery_voltage());
3516 fastboot_publish("battery-voltage", (const char *) battery_voltage);
3517 fastboot_publish("battery-soc-ok", target_battery_soc_ok()? "yes":"no");
3518#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003519}
3520
Brian Swetland9c4c0752009-01-25 16:23:50 -08003521void aboot_init(const struct app_descriptor *app)
3522{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003523 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003524
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003525 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003526 if (target_is_emmc_boot())
3527 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003528 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003529 page_mask = page_size - 1;
3530 }
3531 else
3532 {
3533 page_size = flash_page_size();
3534 page_mask = page_size - 1;
3535 }
3536
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003537 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3538
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003539 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303540 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003541
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003542 /* Display splash screen if enabled */
3543#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003544#if NO_ALARM_DISPLAY
3545 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003546#endif
lijuang99c02d82015-02-13 19:04:34 +08003547 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003548#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003549 /* Wait if the display shutdown is in progress */
3550 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003551 if (!pm_appsbl_display_init_done())
3552 target_display_init(device.display_panel);
3553 else
3554 display_image_on_screen();
3555#else
lijuang99c02d82015-02-13 19:04:34 +08003556 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003557#endif
lijuang99c02d82015-02-13 19:04:34 +08003558 dprintf(SPEW, "Display Init: Done\n");
3559#if NO_ALARM_DISPLAY
3560 }
3561#endif
3562#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003563
Greg Griscod6250552011-06-29 14:40:23 -07003564 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003565 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003566
Dhaval Patel223ec952013-07-18 14:49:44 -07003567 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3568
Matthew Qindefd5562014-07-11 18:02:40 +08003569 /*
3570 * Check power off reason if user force reset,
3571 * if yes phone will do normal boot.
3572 */
3573 if (is_user_force_reset())
3574 goto normal_boot;
3575
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003576 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003577 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003578 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003579 dprintf(ALWAYS,"dload mode key sequence detected\n");
3580 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003581 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003582 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003583 }
3584 else
3585 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303586 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003587 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003588 }
3589 boot_into_fastboot = true;
3590 }
3591 if (!boot_into_fastboot)
3592 {
3593 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3594 boot_into_recovery = 1;
3595 if (!boot_into_recovery &&
3596 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003597 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003598 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003599 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003600 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003601 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003602 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003603
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003604#if USE_PON_REBOOT_REG
3605 reboot_mode = check_hard_reboot_mode();
3606#else
Ajay Dudani77421292010-10-27 19:34:06 -07003607 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003608#endif
3609 if (reboot_mode == RECOVERY_MODE)
3610 {
Ajay Dudani77421292010-10-27 19:34:06 -07003611 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003612 }
3613 else if(reboot_mode == FASTBOOT_MODE)
3614 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003615 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003616 }
3617 else if(reboot_mode == ALARM_BOOT)
3618 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003619 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003620 }
3621 else if (reboot_mode == DM_VERITY_ENFORCING)
3622 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003623 device.verity_mode = 1;
3624 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003625 }
3626 else if (reboot_mode == DM_VERITY_LOGGING)
3627 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003628 device.verity_mode = 0;
3629 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003630 }
3631 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3632 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003633 if(send_delete_keys_to_tz())
3634 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003635 }
3636
Matthew Qindefd5562014-07-11 18:02:40 +08003637normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003638 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003639 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003640 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003641 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003642 if(emmc_recovery_init())
3643 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3644 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003645 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003646 if((device.is_unlocked) || (device.is_tampered))
3647 {
3648 #ifdef TZ_TAMPER_FUSE
3649 set_tamper_fuse_cmd();
3650 #endif
3651 #if USE_PCOM_SECBOOT
3652 set_tamper_flag(device.is_tampered);
3653 #endif
3654 }
Shashank Mittala0032282011-08-26 14:50:11 -07003655 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003656
Pavel Nedev5d91d412013-04-29 11:34:24 +03003657 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003658 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003659 else
3660 {
3661 recovery_init();
3662 #if USE_PCOM_SECBOOT
3663 if((device.is_unlocked) || (device.is_tampered))
3664 set_tamper_flag(device.is_tampered);
3665 #endif
3666 boot_linux_from_flash();
3667 }
3668 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3669 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003670 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003671
Amol Jadi5edf3552013-07-23 14:15:34 -07003672 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003673
Amol Jadi5edf3552013-07-23 14:15:34 -07003674 /* register aboot specific fastboot commands */
3675 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003676
Amol Jadi5edf3552013-07-23 14:15:34 -07003677 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003678 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003679
3680 /* initialize and start fastboot */
3681 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003682#if FBCON_DISPLAY_MSG
3683 display_fastboot_menu_thread();
3684#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003685}
3686
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003687uint32_t get_page_size()
3688{
3689 return page_size;
3690}
3691
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003692/*
3693 * Calculated and save hash (SHA256) for non-signed boot image.
3694 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003695 * @param image_addr - Boot image address
3696 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003697 *
3698 * @return int - 0 on success, negative value on failure.
3699 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003700static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003701{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003702 unsigned int digest[8];
3703#if IMAGE_VERIF_ALGO_SHA1
3704 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3705#else
3706 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3707#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003708
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003709 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003710 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003711
3712 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003713 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003714
3715 return 0;
3716}
3717
Brian Swetland9c4c0752009-01-25 16:23:50 -08003718APP_START(aboot)
3719 .init = aboot_init,
3720APP_END