blob: 553bca4dc72213a11393f0d6e5f550dfd2443a69 [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>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080061#if USE_RPMB_FOR_DEVINFO
62#include <rpmb.h>
63#endif
Dima Zavin214cc642009-01-26 11:16:21 -080064
Neeti Desai17379b82012-06-04 18:42:53 -070065#if DEVICE_TREE
66#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070067#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070068#endif
69
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053070#if WDOG_SUPPORT
71#include <wdog.h>
72#endif
73
Shashank Mittalcd98d472011-08-02 14:29:24 -070074#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080075#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080076#include "bootimg.h"
77#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070078#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080079#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070080#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070081#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070082#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070083#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020084#include "mdtp.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070085
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070086extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070087extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070088extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070089extern int get_target_boot_params(const char *cmdline, const char *part,
90 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070091
92void write_device_info_mmc(device_info *dev);
93void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -070094static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070095
Sridhar Parasurame94e8152014-10-24 14:06:03 -070096/* fastboot command function pointer */
97typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
98
99struct fastboot_cmd_desc {
100 char * name;
101 fastboot_cmd_fn cb;
102};
103
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700104#define EXPAND(NAME) #NAME
105#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700106
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800107#ifdef MEMBASE
108#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
109#else
David Ng183a7422009-12-07 14:55:21 -0800110#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800111#endif
112
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700113#ifndef MEMSIZE
114#define MEMSIZE 1024*1024
115#endif
116
117#define MAX_TAGS_SIZE 1024
118
lijuang72875802015-02-10 10:35:12 +0800119#define RECOVERY_HARD_RESET_MODE 0x01
120#define FASTBOOT_HARD_RESET_MODE 0x02
121#define RTC_HARD_RESET_MODE 0x03
122
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800123#define RECOVERY_MODE 0x77665502
124#define FASTBOOT_MODE 0x77665500
Matthew Qind886f3c2014-01-17 16:52:01 +0800125#define ALARM_BOOT 0x77665503
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800126
Kun Liang2f1601a2013-08-12 16:29:54 +0800127/* make 4096 as default size to ensure EFS,EXT4's erasing */
128#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800129#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800130
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700131#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700132#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800133
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800134#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
135
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700136#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
137
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800138#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700139static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700140#else
David Ng183a7422009-12-07 14:55:21 -0800141static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700142#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800143static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300144static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800145static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800146static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800147static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700148static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300149static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200150static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800151
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800152static const char *baseband_apq = " androidboot.baseband=apq";
153static const char *baseband_msm = " androidboot.baseband=msm";
154static const char *baseband_csfb = " androidboot.baseband=csfb";
155static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700156static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800157static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700158static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800159static const char *baseband_dsda = " androidboot.baseband=dsda";
160static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800161static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800162static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800163
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700164static unsigned page_size = 0;
165static unsigned page_mask = 0;
166static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
167static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700168static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800169static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700170static bool devinfo_present = true;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700171
Shashank Mittalcd98d472011-08-02 14:29:24 -0700172/* Assuming unauthorized kernel image by default */
173static int auth_kernel_img = 0;
174
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -0800175static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
vijay kumarc65876c2015-04-24 13:29:16 +0530176static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700177
Dima Zavin42168f22009-01-30 11:52:22 -0800178struct atag_ptbl_entry
179{
180 char name[16];
181 unsigned offset;
182 unsigned size;
183 unsigned flags;
184};
185
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700186/*
187 * Partition info, required to be published
188 * for fastboot
189 */
190struct getvar_partition_info {
191 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
192 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
193 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
194 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
195 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
196};
197
198/*
199 * Right now, we are publishing the info for only
200 * three partitions
201 */
202struct getvar_partition_info part_info[] =
203{
204 { "system" , "partition-size:", "partition-type:", "", "ext4" },
205 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
206 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
207};
208
209char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700210char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800211char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700212char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530213char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700214
Greg Griscod2471ef2011-07-14 13:00:42 -0700215extern int emmc_recovery_init(void);
216
Kinson Chik0b1c8162011-08-31 16:31:57 -0700217#if NO_KEYPAD_DRIVER
218extern int fastboot_trigger(void);
219#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700220
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800221static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700222{
223 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700224#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800225 if (is_arm64)
226 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
227 else
228 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700229 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
230 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700231#endif
232}
233
Dima Zavin42168f22009-01-30 11:52:22 -0800234static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
235{
236 struct atag_ptbl_entry atag_ptn;
237
238 memcpy(atag_ptn.name, ptn->name, 16);
239 atag_ptn.name[15] = '\0';
240 atag_ptn.offset = ptn->start;
241 atag_ptn.size = ptn->length;
242 atag_ptn.flags = ptn->flags;
243 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
244 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
245}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800246
Neeti Desaie245d492012-06-01 12:52:13 -0700247unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800248{
David Ng183a7422009-12-07 14:55:21 -0800249 int cmdline_len = 0;
250 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800251 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700252 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800253 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300254 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700255 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700256 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200257 bool is_mdtp_activated = 0;
258#ifdef MDTP_SUPPORT
259 mdtp_activated(&is_mdtp_activated);
260#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800261
Brian Swetland9c4c0752009-01-25 16:23:50 -0800262 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800263 cmdline_len = strlen(cmdline);
264 have_cmdline = 1;
265 }
266 if (target_is_emmc_boot()) {
267 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800268#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700269 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
270 ASSERT(boot_dev_buf);
271 platform_boot_dev_cmdline(boot_dev_buf);
272 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700273#endif
David Ng183a7422009-12-07 14:55:21 -0800274 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800275
276 cmdline_len += strlen(usb_sn_cmdline);
277 cmdline_len += strlen(sn_buf);
278
Pavel Nedev5614d222013-06-17 18:01:02 +0300279 if (boot_into_recovery && gpt_exists)
280 cmdline_len += strlen(secondary_gpt_enable);
281
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200282 if(is_mdtp_activated)
283 cmdline_len += strlen(mdtp_activated_flag);
284
Pavel Nedev328ac822013-04-05 15:25:11 +0300285 if (boot_into_ffbm) {
286 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700287 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800288 /* reduce kernel console messages to speed-up boot */
289 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800290 } else if (boot_reason_alarm) {
291 cmdline_len += strlen(alarmboot_cmdline);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700292 } else if (device.charger_screen_enabled &&
293 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700294 pause_at_bootup = 1;
295 cmdline_len += strlen(battchg_pause);
296 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800297
Shashank Mittalcd98d472011-08-02 14:29:24 -0700298 if(target_use_signed_kernel() && auth_kernel_img) {
299 cmdline_len += strlen(auth_kernel);
300 }
301
Joonwoo Park61112782013-10-02 19:50:39 -0700302 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
303 "system",
304 target_boot_params,
305 sizeof(target_boot_params)) == 0) {
306 have_target_boot_params = 1;
307 cmdline_len += strlen(target_boot_params);
308 }
309
Ajay Dudanid04110c2011-01-17 23:55:07 -0800310 /* Determine correct androidboot.baseband to use */
311 switch(target_baseband())
312 {
313 case BASEBAND_APQ:
314 cmdline_len += strlen(baseband_apq);
315 break;
316
317 case BASEBAND_MSM:
318 cmdline_len += strlen(baseband_msm);
319 break;
320
321 case BASEBAND_CSFB:
322 cmdline_len += strlen(baseband_csfb);
323 break;
324
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800325 case BASEBAND_SVLTE2A:
326 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800327 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700328
329 case BASEBAND_MDM:
330 cmdline_len += strlen(baseband_mdm);
331 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700332
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800333 case BASEBAND_MDM2:
334 cmdline_len += strlen(baseband_mdm2);
335 break;
336
Amol Jadi5c61a952012-05-04 17:05:35 -0700337 case BASEBAND_SGLTE:
338 cmdline_len += strlen(baseband_sglte);
339 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530340
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800341 case BASEBAND_SGLTE2:
342 cmdline_len += strlen(baseband_sglte2);
343 break;
344
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530345 case BASEBAND_DSDA:
346 cmdline_len += strlen(baseband_dsda);
347 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800348
349 case BASEBAND_DSDA2:
350 cmdline_len += strlen(baseband_dsda2);
351 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800352 }
353
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800354 if (cmdline) {
355 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
356 target_display_panel_node(device.display_panel,
357 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
358 strlen(display_panel_buf)) {
359 cmdline_len += strlen(display_panel_buf);
360 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700361 }
362
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800363 if (target_warm_boot()) {
364 warm_boot = true;
365 cmdline_len += strlen(warmboot_cmdline);
366 }
367
David Ng183a7422009-12-07 14:55:21 -0800368 if (cmdline_len > 0) {
369 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800370 unsigned char *dst;
371
372 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
373 ASSERT(cmdline_final != NULL);
374 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700375
Amol Jadi168b7712012-03-06 16:15:00 -0800376 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800377 if (have_cmdline) {
378 src = cmdline;
379 while ((*dst++ = *src++));
380 }
381 if (target_is_emmc_boot()) {
382 src = emmc_cmdline;
383 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700384 have_cmdline = 1;
385 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800386#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700387 src = boot_dev_buf;
388 if (have_cmdline) --dst;
389 while ((*dst++ = *src++));
390#endif
David Ngf773dde2010-07-26 19:55:08 -0700391 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800392
393 src = usb_sn_cmdline;
394 if (have_cmdline) --dst;
395 have_cmdline = 1;
396 while ((*dst++ = *src++));
397 src = sn_buf;
398 if (have_cmdline) --dst;
399 have_cmdline = 1;
400 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800401 if (warm_boot) {
402 if (have_cmdline) --dst;
403 src = warmboot_cmdline;
404 while ((*dst++ = *src++));
405 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800406
Pavel Nedev5614d222013-06-17 18:01:02 +0300407 if (boot_into_recovery && gpt_exists) {
408 src = secondary_gpt_enable;
409 if (have_cmdline) --dst;
410 while ((*dst++ = *src++));
411 }
412
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200413 if (is_mdtp_activated) {
414 src = mdtp_activated_flag;
415 if (have_cmdline) --dst;
416 while ((*dst++ = *src++));
417 }
418
Pavel Nedev328ac822013-04-05 15:25:11 +0300419 if (boot_into_ffbm) {
420 src = androidboot_mode;
421 if (have_cmdline) --dst;
422 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700423 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300424 if (have_cmdline) --dst;
425 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800426 src = loglevel;
427 if (have_cmdline) --dst;
428 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800429 } else if (boot_reason_alarm) {
430 src = alarmboot_cmdline;
431 if (have_cmdline) --dst;
432 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300433 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700434 src = battchg_pause;
435 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800436 while ((*dst++ = *src++));
437 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800438
Shashank Mittalcd98d472011-08-02 14:29:24 -0700439 if(target_use_signed_kernel() && auth_kernel_img) {
440 src = auth_kernel;
441 if (have_cmdline) --dst;
442 while ((*dst++ = *src++));
443 }
444
Ajay Dudanid04110c2011-01-17 23:55:07 -0800445 switch(target_baseband())
446 {
447 case BASEBAND_APQ:
448 src = baseband_apq;
449 if (have_cmdline) --dst;
450 while ((*dst++ = *src++));
451 break;
452
453 case BASEBAND_MSM:
454 src = baseband_msm;
455 if (have_cmdline) --dst;
456 while ((*dst++ = *src++));
457 break;
458
459 case BASEBAND_CSFB:
460 src = baseband_csfb;
461 if (have_cmdline) --dst;
462 while ((*dst++ = *src++));
463 break;
464
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800465 case BASEBAND_SVLTE2A:
466 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800467 if (have_cmdline) --dst;
468 while ((*dst++ = *src++));
469 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700470
471 case BASEBAND_MDM:
472 src = baseband_mdm;
473 if (have_cmdline) --dst;
474 while ((*dst++ = *src++));
475 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700476
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800477 case BASEBAND_MDM2:
478 src = baseband_mdm2;
479 if (have_cmdline) --dst;
480 while ((*dst++ = *src++));
481 break;
482
Amol Jadi5c61a952012-05-04 17:05:35 -0700483 case BASEBAND_SGLTE:
484 src = baseband_sglte;
485 if (have_cmdline) --dst;
486 while ((*dst++ = *src++));
487 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530488
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800489 case BASEBAND_SGLTE2:
490 src = baseband_sglte2;
491 if (have_cmdline) --dst;
492 while ((*dst++ = *src++));
493 break;
494
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530495 case BASEBAND_DSDA:
496 src = baseband_dsda;
497 if (have_cmdline) --dst;
498 while ((*dst++ = *src++));
499 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800500
501 case BASEBAND_DSDA2:
502 src = baseband_dsda2;
503 if (have_cmdline) --dst;
504 while ((*dst++ = *src++));
505 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800506 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700507
508 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700509 src = display_panel_buf;
510 if (have_cmdline) --dst;
511 while ((*dst++ = *src++));
512 }
Joonwoo Park61112782013-10-02 19:50:39 -0700513
514 if (have_target_boot_params) {
515 if (have_cmdline) --dst;
516 src = target_boot_params;
517 while ((*dst++ = *src++));
518 }
Neeti Desaie245d492012-06-01 12:52:13 -0700519 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700520
521
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700522 if (boot_dev_buf)
523 free(boot_dev_buf);
524
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800525 if (cmdline_final)
526 dprintf(INFO, "cmdline: %s\n", cmdline_final);
527 else
528 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700529 return cmdline_final;
530}
531
532unsigned *atag_core(unsigned *ptr)
533{
534 /* CORE */
535 *ptr++ = 2;
536 *ptr++ = 0x54410001;
537
538 return ptr;
539
540}
541
542unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
543 unsigned ramdisk_size)
544{
545 if (ramdisk_size) {
546 *ptr++ = 4;
547 *ptr++ = 0x54420005;
548 *ptr++ = (unsigned)ramdisk;
549 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800550 }
551
Neeti Desaie245d492012-06-01 12:52:13 -0700552 return ptr;
553}
554
555unsigned *atag_ptable(unsigned **ptr_addr)
556{
557 int i;
558 struct ptable *ptable;
559
560 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700561 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
562 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700563 *(*ptr_addr)++ = 0x4d534d70;
564 for (i = 0; i < ptable->count; ++i)
565 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
566 }
567
568 return (*ptr_addr);
569}
570
571unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
572{
573 int cmdline_length = 0;
574 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700575 char *dest;
576
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800577 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700578 n = (cmdline_length + 4) & (~3);
579
580 *ptr++ = (n / 4) + 2;
581 *ptr++ = 0x54410009;
582 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800583 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700584 ptr += (n / 4);
585
586 return ptr;
587}
588
589unsigned *atag_end(unsigned *ptr)
590{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800591 /* END */
592 *ptr++ = 0;
593 *ptr++ = 0;
594
Neeti Desaie245d492012-06-01 12:52:13 -0700595 return ptr;
596}
597
598void generate_atags(unsigned *ptr, const char *cmdline,
599 void *ramdisk, unsigned ramdisk_size)
600{
601
602 ptr = atag_core(ptr);
603 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
604 ptr = target_atag_mem(ptr);
605
606 /* Skip NAND partition ATAGS for eMMC boot */
607 if (!target_is_emmc_boot()){
608 ptr = atag_ptable(&ptr);
609 }
610
611 ptr = atag_cmdline(ptr, cmdline);
612 ptr = atag_end(ptr);
613}
614
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700615typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700616void boot_linux(void *kernel, unsigned *tags,
617 const char *cmdline, unsigned machtype,
618 void *ramdisk, unsigned ramdisk_size)
619{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800620 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800621#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700622 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800623#endif
624
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700625 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800626 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800627 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800628
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530629 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700630
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800631 final_cmdline = update_cmdline((const char*)cmdline);
632
Neeti Desai17379b82012-06-04 18:42:53 -0700633#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800634 dprintf(INFO, "Updating device tree: start\n");
635
Neeti Desai17379b82012-06-04 18:42:53 -0700636 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530637 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700638 if(ret)
639 {
640 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
641 ASSERT(0);
642 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800643 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700644#else
Neeti Desaie245d492012-06-01 12:52:13 -0700645 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800646 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700647#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700648
Maria Yu52254c02014-07-04 16:14:54 +0800649 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700650
651#if VERIFIED_BOOT
652 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700653 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700654 {
655 dprintf(INFO, "Failed to write protect dev info\n");
656 ASSERT(0);
657 }
658#endif
659
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700660 /* Perform target specific cleanup */
661 target_uninit();
662
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800663 /* Turn off splash screen if enabled */
664#if DISPLAY_SPLASH_SCREEN
665 target_display_shutdown();
666#endif
667
668
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800669 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530670 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800671
672 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700673
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +0530674 /* Initialise wdog to catch early kernel crashes */
675#if WDOG_SUPPORT
676 msm_wdog_init();
677#endif
Amol Jadi4421e652011-06-16 15:00:48 -0700678 /* do any platform specific cleanup before kernel entry */
679 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700680
Brian Swetland9c4c0752009-01-25 16:23:50 -0800681 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700682
Amol Jadi504f9fe2012-08-16 13:56:48 -0700683#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800684 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700685#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700686 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800687
688 if (IS_ARM64(kptr))
689 /* Jump to a 64bit kernel */
690 scm_elexec_call((paddr_t)kernel, tags_phys);
691 else
692 /* Jump to a 32bit kernel */
693 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800694}
695
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700696/* Function to check if the memory address range falls within the aboot
697 * boundaries.
698 * start: Start of the memory region
699 * size: Size of the memory region
700 */
701int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
702{
703 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800704 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700705 return -1;
706
707 /* Check for memory overlap. */
708 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
709 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700710 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700711 return 0;
712 else
713 return -1;
714}
715
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800716#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800717
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800718BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800719#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800720BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800721#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800722
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700723static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
724{
725 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800726
727#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800728#if IMAGE_VERIF_ALGO_SHA1
729 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
730#else
731 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
732#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800733#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700734
735 /* Assume device is rooted at this time. */
736 device.is_tampered = 1;
737
738 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
739
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700740#if VERIFIED_BOOT
741 if(boot_into_recovery)
742 {
743 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530744 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700745 }
746 else
747 {
748 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530749 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700750 }
751 boot_verify_print_state();
752#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700753 ret = image_verify((unsigned char *)bootimg_addr,
754 (unsigned char *)(bootimg_addr + bootimg_size),
755 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800756 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700757#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700758 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
759
760 if (ret)
761 {
762 /* Authorized kernel */
763 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700764 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700765 }
766
Amit Blay4aa292f2015-04-28 21:55:59 +0300767#ifdef MDTP_SUPPORT
768 {
769 /* Verify MDTP lock.
770 * For boot & recovery partitions, use aboot's verification result.
771 */
772 mdtp_ext_partition_verification_t ext_partition;
773 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
774 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
775 ext_partition.page_size = 0; /* Not needed since already validated */
776 ext_partition.image_addr = 0; /* Not needed since already validated */
777 ext_partition.image_size = 0; /* Not needed since already validated */
778 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
779 mdtp_fwlock_verify_lock(&ext_partition);
780 }
781#endif /* MDTP_SUPPORT */
782
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700783#if USE_PCOM_SECBOOT
784 set_tamper_flag(device.is_tampered);
785#endif
786
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700787#if VERIFIED_BOOT
788 if(boot_verify_get_state() == RED)
789 {
790 if(!boot_into_recovery)
791 {
792 dprintf(CRITICAL,
793 "Device verification failed. Rebooting into recovery.\n");
794 reboot_device(RECOVERY_MODE);
795 }
796 else
797 {
798 dprintf(CRITICAL,
799 "Recovery image verification failed. Asserting..\n");
800 ASSERT(0);
801 }
802 }
803#endif
Unnati Gandhi1be04752015-03-27 19:41:53 +0530804
805 if(device.is_tampered)
806 {
807 write_device_info_mmc(&device);
808 #ifdef TZ_TAMPER_FUSE
809 set_tamper_fuse_cmd();
810 #endif
811 #ifdef ASSERT_ON_TAMPER
812 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
813 ASSERT(0);
814 #endif
815 }
816
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700817}
818
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530819static bool check_format_bit()
820{
821 bool ret = false;
822 int index;
823 uint64_t offset;
824 struct boot_selection_info *in = NULL;
825 char *buf = NULL;
826
827 index = partition_get_index("bootselect");
828 if (index == INVALID_PTN)
829 {
830 dprintf(INFO, "Unable to locate /bootselect partition\n");
831 return ret;
832 }
833 offset = partition_get_offset(index);
834 if(!offset)
835 {
836 dprintf(INFO, "partition /bootselect doesn't exist\n");
837 return ret;
838 }
839 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
840 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530841 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530842 {
843 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
844 free(buf);
845 return ret;
846 }
847 in = (struct boot_selection_info *) buf;
848 if ((in->signature == BOOTSELECT_SIGNATURE) &&
849 (in->version == BOOTSELECT_VERSION)) {
850 if ((in->state_info & BOOTSELECT_FORMAT) &&
851 !(in->state_info & BOOTSELECT_FACTORY))
852 ret = true;
853 } else {
854 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
855 in->signature, in->version);
856 ASSERT(0);
857 }
858 free(buf);
859 return ret;
860}
861
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700862void boot_verifier_init()
863{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700864 uint32_t boot_state;
865 /* Check if device unlock */
866 if(device.is_unlocked)
867 {
868 boot_verify_send_event(DEV_UNLOCK);
869 boot_verify_print_state();
870 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
871 return;
872 }
873 else
874 {
875 boot_verify_send_event(BOOT_INIT);
876 }
877
878 /* Initialize keystore */
879 boot_state = boot_verify_keystore_init();
880 if(boot_state == YELLOW)
881 {
882 boot_verify_print_state();
883 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
884 }
885}
886
Shashank Mittal23b8f422010-04-16 19:27:21 -0700887int boot_linux_from_mmc(void)
888{
889 struct boot_img_hdr *hdr = (void*) buf;
890 struct boot_img_hdr *uhdr;
891 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700892 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700893 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700894 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700895
Shashank Mittalcd98d472011-08-02 14:29:24 -0700896 unsigned char *image_addr = 0;
897 unsigned kernel_actual;
898 unsigned ramdisk_actual;
899 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700900 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700901
Matthew Qin271927e2015-03-31 22:07:07 -0400902 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +0800903 unsigned int out_len = 0;
904 unsigned int out_avai_len = 0;
905 unsigned char *out_addr = NULL;
906 uint32_t dtb_offset = 0;
907 unsigned char *kernel_start_addr = NULL;
908 unsigned int kernel_size = 0;
909 int rc;
910
Neeti Desai465491e2012-07-31 12:53:35 -0700911#if DEVICE_TREE
912 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700913 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700914 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800915 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700916 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -0400917 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -0700918#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +0800919 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800920
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530921 if (check_format_bit())
922 boot_into_recovery = 1;
923
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700924 if (!boot_into_recovery) {
925 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
926 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
927 if (rcode <= 0) {
928 boot_into_ffbm = false;
929 if (rcode < 0)
930 dprintf(CRITICAL,"failed to get ffbm cookie");
931 } else
932 boot_into_ffbm = true;
933 } else
934 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700935 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
936 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
937 dprintf(INFO, "Unified boot method!\n");
938 hdr = uhdr;
939 goto unified_boot;
940 }
Greg Griscod6250552011-06-29 14:40:23 -0700941 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700942 index = partition_get_index("boot");
943 ptn = partition_get_offset(index);
944 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700945 dprintf(CRITICAL, "ERROR: No boot partition found\n");
946 return -1;
947 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700948 }
949 else {
950 index = partition_get_index("recovery");
951 ptn = partition_get_offset(index);
952 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700953 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
954 return -1;
955 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700956 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -0800957 /* Set Lun for boot & recovery partitions */
958 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -0700959
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530960 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700961 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
962 return -1;
963 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700964
965 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700966 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700967 return -1;
968 }
969
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700970 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530971
972 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
973 dprintf(CRITICAL, "ERROR: Invalid page size\n");
974 return -1;
975 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700976 page_size = hdr->page_size;
977 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700978 }
979
Matthew Qin49e51fa2015-02-09 10:40:45 +0800980 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
981 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800982
Matthew Qin49e51fa2015-02-09 10:40:45 +0800983 image_addr = (unsigned char *)target_get_scratch_address();
984
985#if DEVICE_TREE
986 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
987 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
988#else
989 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
990#endif
991
992#if VERIFIED_BOOT
993 boot_verifier_init();
994#endif
995
996 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
997 {
998 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
999 return -1;
1000 }
1001
Matthew Qinbb7923d2015-02-09 10:56:09 +08001002 /*
1003 * Update loading flow of bootimage to support compressed/uncompressed
1004 * bootimage on both 64bit and 32bit platform.
1005 * 1. Load bootimage from emmc partition onto DDR.
1006 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1007 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1008 * platform accordingly.
1009 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1010 */
1011
Matthew Qin49e51fa2015-02-09 10:40:45 +08001012 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
1013 bs_set_timestamp(BS_KERNEL_LOAD_START);
1014
Matthew Qinbb7923d2015-02-09 10:56:09 +08001015 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001016 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1017 {
1018 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1019 return -1;
1020 }
1021
1022 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
1023 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1024
1025 /* Authenticate Kernel */
1026 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1027 (int) target_use_signed_kernel(),
1028 device.is_unlocked,
1029 device.is_tampered);
1030
1031 if(target_use_signed_kernel() && (!device.is_unlocked))
1032 {
1033 offset = imagesize_actual;
1034 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1035 {
1036 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1037 return -1;
1038 }
1039
1040 /* Read signature */
1041 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1042 {
1043 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1044 return -1;
1045 }
1046
1047 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
1048 } else {
1049 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1050 #ifdef TZ_SAVE_KERNEL_HASH
1051 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1052 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001053
1054#ifdef MDTP_SUPPORT
1055 {
1056 /* Verify MDTP lock.
1057 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1058 * since verification was skipped in aboot. The signature is not part of the loaded image.
1059 */
1060 mdtp_ext_partition_verification_t ext_partition;
1061 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1062 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1063 ext_partition.page_size = page_size;
1064 ext_partition.image_addr = (uint32)image_addr;
1065 ext_partition.image_size = imagesize_actual;
1066 ext_partition.sig_avail = FALSE;
1067 mdtp_fwlock_verify_lock(&ext_partition);
1068 }
1069#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001070 }
1071
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001072 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001073 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1074 * If not, continue booting.
1075 */
1076 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1077 {
1078 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1079 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001080 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001081 rc = decompress((unsigned char *)(image_addr + page_size),
1082 hdr->kernel_size, out_addr, out_avai_len,
1083 &dtb_offset, &out_len);
1084 if (rc)
1085 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001086 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001087 ASSERT(0);
1088 }
1089
Matthew Qin0b15b322015-05-19 05:20:54 -04001090 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001091 kptr = (struct kernel64_hdr *)out_addr;
1092 kernel_start_addr = out_addr;
1093 kernel_size = out_len;
1094 } else {
1095 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1096 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1097 kernel_size = hdr->kernel_size;
1098 }
1099
1100 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001101 * Update the kernel/ramdisk/tags address if the boot image header
1102 * has default values, these default values come from mkbootimg when
1103 * the boot image is flashed using fastboot flash:raw
1104 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001105 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001106
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001107 /* Get virtual addresses since the hdr saves physical addresses. */
1108 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1109 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1110 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001111
Matthew Qinbb7923d2015-02-09 10:56:09 +08001112 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001113 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001114 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001115 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1116 {
1117 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1118 return -1;
1119 }
1120
1121#ifndef DEVICE_TREE
1122 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1123 {
1124 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1125 return -1;
1126 }
1127#endif
1128
Matthew Qin49e51fa2015-02-09 10:40:45 +08001129 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001130 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001131 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001132
Matthew Qin49e51fa2015-02-09 10:40:45 +08001133 #if DEVICE_TREE
1134 if(hdr->dt_size) {
1135 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1136 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001137
Matthew Qin49e51fa2015-02-09 10:40:45 +08001138 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1139 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1140 return -1;
1141 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001142
Matthew Qin49e51fa2015-02-09 10:40:45 +08001143 /* Find index of device tree within device tree table */
1144 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1145 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1146 return -1;
1147 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001148
Matthew Qin271927e2015-03-31 22:07:07 -04001149 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1150 {
1151 unsigned int compressed_size = 0;
1152 out_addr += out_len;
1153 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001154 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001155 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1156 dt_entry.size, out_addr, out_avai_len,
1157 &compressed_size, &dtb_size);
1158 if (rc)
1159 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001160 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001161 ASSERT(0);
1162 }
1163
Matthew Qin0b15b322015-05-19 05:20:54 -04001164 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001165 best_match_dt_addr = out_addr;
1166 } else {
1167 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1168 dtb_size = dt_entry.size;
1169 }
1170
Matthew Qin49e51fa2015-02-09 10:40:45 +08001171 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001172 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001173 {
1174 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1175 return -1;
1176 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001177
Matthew Qin271927e2015-03-31 22:07:07 -04001178 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001179 } else {
1180 /* Validate the tags_addr */
1181 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001182 {
1183 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1184 return -1;
1185 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001186 /*
1187 * If appended dev tree is found, update the atags with
1188 * memory address to the DTB appended location on RAM.
1189 * Else update with the atags address in the kernel header
1190 */
1191 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001192 dtb = dev_tree_appended((void*)(image_addr + page_size),
1193 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001194 (void *)hdr->tags_addr);
1195 if (!dtb) {
1196 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001197 return -1;
1198 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001199 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001200 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001201
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001202 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1203 target_load_ssd_keystore();
1204
Shashank Mittal23b8f422010-04-16 19:27:21 -07001205unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001206
Dima Zavin77e41f32013-03-06 16:10:43 -08001207 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001208 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001209 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1210
1211 return 0;
1212}
1213
Dima Zavin214cc642009-01-26 11:16:21 -08001214int boot_linux_from_flash(void)
1215{
1216 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001217 struct ptentry *ptn;
1218 struct ptable *ptable;
1219 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001220
Shashank Mittalcd98d472011-08-02 14:29:24 -07001221 unsigned char *image_addr = 0;
1222 unsigned kernel_actual;
1223 unsigned ramdisk_actual;
1224 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001225 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001226
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001227#if DEVICE_TREE
1228 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001229 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001230 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001231 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001232#endif
1233
David Ng183a7422009-12-07 14:55:21 -08001234 if (target_is_emmc_boot()) {
1235 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1236 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1237 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1238 return -1;
1239 }
1240 goto continue_boot;
1241 }
1242
Dima Zavin214cc642009-01-26 11:16:21 -08001243 ptable = flash_get_ptable();
1244 if (ptable == NULL) {
1245 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1246 return -1;
1247 }
1248
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001249 if(!boot_into_recovery)
1250 {
1251 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001252
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001253 if (ptn == NULL) {
1254 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1255 return -1;
1256 }
1257 }
1258 else
1259 {
1260 ptn = ptable_find(ptable, "recovery");
1261 if (ptn == NULL) {
1262 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1263 return -1;
1264 }
Dima Zavin214cc642009-01-26 11:16:21 -08001265 }
1266
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001267 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001268 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1269 return -1;
1270 }
Dima Zavin214cc642009-01-26 11:16:21 -08001271
1272 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001273 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001274 return -1;
1275 }
1276
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001277 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001278 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 -08001279 return -1;
1280 }
1281
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001282 /*
1283 * Update the kernel/ramdisk/tags address if the boot image header
1284 * has default values, these default values come from mkbootimg when
1285 * the boot image is flashed using fastboot flash:raw
1286 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001287 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001288
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001289 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001290 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1291 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1292 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1293
1294 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1295 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1296
1297 /* Check if the addresses in the header are valid. */
1298 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1299 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1300 {
1301 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1302 return -1;
1303 }
1304
1305#ifndef DEVICE_TREE
1306 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1307 {
1308 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1309 return -1;
1310 }
1311#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001312
Shashank Mittalcd98d472011-08-02 14:29:24 -07001313 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001314 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001315 {
1316 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001317 offset = 0;
1318
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001319#if DEVICE_TREE
1320 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1321 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001322
1323 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1324 {
1325 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1326 return -1;
1327 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001328#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001329 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001330#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001331
Amol Jadib6be5c12012-11-14 13:39:51 -08001332 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001333 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001334
Shashank Mittalcd98d472011-08-02 14:29:24 -07001335 /* Read image without signature */
1336 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1337 {
1338 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1339 return -1;
1340 }
Dima Zavin214cc642009-01-26 11:16:21 -08001341
Amol Jadib6be5c12012-11-14 13:39:51 -08001342 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001343 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001344
Shashank Mittalcd98d472011-08-02 14:29:24 -07001345 offset = imagesize_actual;
1346 /* Read signature */
1347 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1348 {
1349 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001350 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001351 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001352
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301353 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001354
1355 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001356 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1357 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001358#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001359 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001360 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001361 {
1362 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1363 return -1;
1364 }
1365
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001366 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1367#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001368
1369 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001370 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001371 {
1372 write_device_info_flash(&device);
1373 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301374#if USE_PCOM_SECBOOT
1375 set_tamper_flag(device.is_tampered);
1376#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001377 }
1378 else
1379 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001380 offset = page_size;
1381
Amol Jadib6be5c12012-11-14 13:39:51 -08001382 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1383 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1384 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1385
1386 dprintf(INFO, "Loading boot image (%d): start\n",
1387 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001388 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001389
1390 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001391 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1392 return -1;
1393 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001394 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001395
Amol Jadib6be5c12012-11-14 13:39:51 -08001396 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001397 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1398 return -1;
1399 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001400 offset += ramdisk_actual;
1401
1402 dprintf(INFO, "Loading boot image (%d): done\n",
1403 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001404 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001405
1406 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001407 offset += second_actual;
1408 /* Second image loading not implemented. */
1409 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001410 }
1411
1412#if DEVICE_TREE
1413 if(hdr->dt_size != 0) {
1414
1415 /* Read the device tree table into buffer */
1416 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1417 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1418 return -1;
1419 }
1420
1421 table = (struct dt_table*) dt_buf;
1422
Deepa Dinamani19648b42013-09-05 17:05:55 -07001423 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001424 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1425 return -1;
1426 }
1427
Deepa Dinamani19648b42013-09-05 17:05:55 -07001428 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1429 if (!table)
1430 return -1;
1431
1432 /* Read the entire device tree table into buffer */
1433 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1434 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1435 return -1;
1436 }
1437
1438
Joel Kingaa335dc2013-06-03 16:11:08 -07001439 /* Find index of device tree within device tree table */
1440 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001441 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1442 return -1;
1443 }
1444
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001445 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001446 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001447 {
1448 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1449 return -1;
1450 }
1451
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001452 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001453 if(flash_read(ptn, offset + dt_entry.offset,
1454 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001455 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1456 return -1;
1457 }
1458 }
1459#endif
1460
Shashank Mittalcd98d472011-08-02 14:29:24 -07001461 }
David Ng183a7422009-12-07 14:55:21 -08001462continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001463
Dima Zavin214cc642009-01-26 11:16:21 -08001464 /* TODO: create/pass atags to kernel */
1465
Ajay Dudanie28a6072011-07-01 13:59:46 -07001466 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001467 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001468 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1469
1470 return 0;
1471}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001472
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001473BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001474void write_device_info_mmc(device_info *dev)
1475{
Shashank Mittal162244e2011-08-08 19:01:25 -07001476 unsigned long long ptn = 0;
1477 unsigned long long size;
1478 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001479 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001480 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001481 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001482
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001483 if (devinfo_present)
1484 index = partition_get_index("devinfo");
1485 else
1486 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001487
Shashank Mittal162244e2011-08-08 19:01:25 -07001488 ptn = partition_get_offset(index);
1489 if(ptn == 0)
1490 {
1491 return;
1492 }
1493
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001494 lun = partition_get_lun(index);
1495 mmc_set_lun(lun);
1496
Shashank Mittal162244e2011-08-08 19:01:25 -07001497 size = partition_get_size(index);
1498
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001499 blocksize = mmc_get_device_blocksize();
1500
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001501 if (devinfo_present)
1502 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1503 else
1504 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1505 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001506 {
1507 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1508 return;
1509 }
1510}
1511
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001512void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001513{
Shashank Mittal162244e2011-08-08 19:01:25 -07001514 unsigned long long ptn = 0;
1515 unsigned long long size;
1516 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001517 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001518 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001519
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001520 if ((index = partition_get_index("devinfo")) < 0)
1521 {
1522 devinfo_present = false;
1523 index = partition_get_index("aboot");
1524 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001525
Shashank Mittal162244e2011-08-08 19:01:25 -07001526 ptn = partition_get_offset(index);
1527 if(ptn == 0)
1528 {
1529 return;
1530 }
1531
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001532 mmc_set_lun(partition_get_lun(index));
1533
Shashank Mittal162244e2011-08-08 19:01:25 -07001534 size = partition_get_size(index);
1535
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001536 blocksize = mmc_get_device_blocksize();
1537
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001538 if (devinfo_present)
1539 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1540 else
1541 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1542 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001543 {
1544 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1545 return;
1546 }
1547
Shashank Mittal162244e2011-08-08 19:01:25 -07001548}
1549
1550void write_device_info_flash(device_info *dev)
1551{
1552 struct device_info *info = (void *) info_buf;
1553 struct ptentry *ptn;
1554 struct ptable *ptable;
1555
1556 ptable = flash_get_ptable();
1557 if (ptable == NULL)
1558 {
1559 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1560 return;
1561 }
1562
1563 ptn = ptable_find(ptable, "devinfo");
1564 if (ptn == NULL)
1565 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001566 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001567 return;
1568 }
1569
1570 memcpy(info, dev, sizeof(device_info));
1571
1572 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1573 {
1574 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1575 return;
1576 }
1577}
1578
vijay kumarc65876c2015-04-24 13:29:16 +05301579static int read_allow_oem_unlock(device_info *dev)
1580{
1581 const char *ptn_name = "frp";
1582 unsigned offset;
1583 int index;
1584 unsigned long long ptn;
1585 unsigned long long ptn_size;
1586 unsigned blocksize = mmc_get_device_blocksize();
1587 char buf[blocksize];
1588
1589 index = partition_get_index(ptn_name);
1590 if (index == INVALID_PTN)
1591 {
1592 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
1593 return -1;
1594 }
1595
1596 ptn = partition_get_offset(index);
1597 ptn_size = partition_get_size(index);
1598 offset = ptn_size - blocksize;
1599
1600 if (mmc_read(ptn + offset, (void *)buf, sizeof(buf)))
1601 {
1602 dprintf(CRITICAL, "Reading MMC failed\n");
1603 return -1;
1604 }
1605
1606 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1607 is_allow_unlock = buf[blocksize-1] & 0x01;
1608 return 0;
1609}
1610
1611static int write_allow_oem_unlock(bool allow_unlock)
1612{
1613 const char *ptn_name = "frp";
1614 unsigned offset;
1615
1616 int index;
1617 unsigned long long ptn;
1618 unsigned long long ptn_size;
1619 unsigned blocksize = mmc_get_device_blocksize();
1620 char buf[blocksize];
1621
1622 index = partition_get_index(ptn_name);
1623 if (index == INVALID_PTN)
1624 {
1625 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
1626 return -1;
1627 }
1628
1629 ptn = partition_get_offset(index);
1630 ptn_size = partition_get_size(index);
1631 offset = ptn_size - blocksize;
1632
1633 if (mmc_read(ptn + offset, (void *)buf, sizeof(buf)))
1634 {
1635 dprintf(CRITICAL, "Reading MMC failed\n");
1636 return -1;
1637 }
1638
1639 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1640 buf[blocksize-1] = allow_unlock;
1641 if (mmc_write(ptn + offset, blocksize, buf))
1642 {
1643 dprintf(CRITICAL, "Writing MMC failed\n");
1644 return -1;
1645 }
1646
1647 return 0;
1648}
1649
Shashank Mittal162244e2011-08-08 19:01:25 -07001650void read_device_info_flash(device_info *dev)
1651{
1652 struct device_info *info = (void*) info_buf;
1653 struct ptentry *ptn;
1654 struct ptable *ptable;
1655
1656 ptable = flash_get_ptable();
1657 if (ptable == NULL)
1658 {
1659 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1660 return;
1661 }
1662
1663 ptn = ptable_find(ptable, "devinfo");
1664 if (ptn == NULL)
1665 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001666 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001667 return;
1668 }
1669
1670 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1671 {
1672 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1673 return;
1674 }
1675
1676 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1677 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001678 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1679 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001680 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001681 write_device_info_flash(info);
1682 }
1683 memcpy(dev, info, sizeof(device_info));
1684}
1685
1686void write_device_info(device_info *dev)
1687{
1688 if(target_is_emmc_boot())
1689 {
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001690 struct device_info *info = (void*) info_buf;
1691 memcpy(info, dev, sizeof(struct device_info));
1692
1693#if USE_RPMB_FOR_DEVINFO
1694 if (is_secure_boot_enable())
1695 write_device_info_rpmb((void*) info, mmc_get_device_blocksize());
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001696 else
1697 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001698#else
1699 write_device_info_mmc(info);
1700#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001701 }
1702 else
1703 {
1704 write_device_info_flash(dev);
1705 }
1706}
1707
1708void read_device_info(device_info *dev)
1709{
1710 if(target_is_emmc_boot())
1711 {
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001712 struct device_info *info = (void*) info_buf;
1713
1714#if USE_RPMB_FOR_DEVINFO
1715 if (is_secure_boot_enable())
1716 read_device_info_rpmb((void*) info, mmc_get_device_blocksize());
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001717 else
1718 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001719#else
1720 read_device_info_mmc(info);
1721#endif
1722
1723 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1724 {
1725 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
Channagoud Kadabi35297672015-06-13 11:09:49 -07001726 info->is_unlocked = 0;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001727 info->is_verified = 0;
1728 info->is_tampered = 0;
Unnati Gandhia49daa12015-03-23 18:08:38 +05301729#if USER_BUILD_VARIANT
1730 info->charger_screen_enabled = 1;
1731#else
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001732 info->charger_screen_enabled = 0;
Unnati Gandhia49daa12015-03-23 18:08:38 +05301733#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001734 write_device_info(info);
1735 }
1736 memcpy(dev, info, sizeof(device_info));
Shashank Mittal162244e2011-08-08 19:01:25 -07001737 }
1738 else
1739 {
1740 read_device_info_flash(dev);
1741 }
1742}
1743
1744void reset_device_info()
1745{
1746 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001747 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001748 write_device_info(&device);
1749}
1750
1751void set_device_root()
1752{
1753 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001754 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001755 write_device_info(&device);
1756}
1757
Amol Jadicb524072012-08-09 16:40:18 -07001758#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001759int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
Amol Jadicb524072012-08-09 16:40:18 -07001760{
1761 uint32 dt_image_offset = 0;
1762 uint32_t n;
1763 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001764 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001765 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001766 unsigned int compressed_size = 0;
1767 unsigned int dtb_size = 0;
1768 unsigned int out_avai_len = 0;
1769 unsigned char *out_addr = NULL;
1770 unsigned char *best_match_dt_addr = NULL;
1771 int rc;
Amol Jadicb524072012-08-09 16:40:18 -07001772
1773 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1774
Amol Jadicb524072012-08-09 16:40:18 -07001775 if(hdr->dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001776 /* add kernel offset */
1777 dt_image_offset += page_size;
1778 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1779 dt_image_offset += n;
1780
1781 /* add ramdisk offset */
1782 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1783 dt_image_offset += n;
1784
1785 /* add second offset */
1786 if(hdr->second_size != 0) {
1787 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1788 dt_image_offset += n;
1789 }
1790
1791 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001792 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001793
Deepa Dinamani19648b42013-09-05 17:05:55 -07001794 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001795 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1796 return -1;
1797 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001798 /* Find index of device tree within device tree table */
1799 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001800 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1801 return -1;
1802 }
1803
Matthew Qin271927e2015-03-31 22:07:07 -04001804 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
1805 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
1806 {
1807 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
1808 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04001809 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001810 rc = decompress(best_match_dt_addr,
1811 dt_entry.size, out_addr, out_avai_len,
1812 &compressed_size, &dtb_size);
1813 if (rc)
1814 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001815 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001816 ASSERT(0);
1817 }
1818
Matthew Qin0b15b322015-05-19 05:20:54 -04001819 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001820 best_match_dt_addr = out_addr;
1821 } else {
1822 dtb_size = dt_entry.size;
1823 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001824 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001825 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001826 {
1827 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1828 return -1;
1829 }
1830
Amol Jadicb524072012-08-09 16:40:18 -07001831 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001832 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001833 } else
1834 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001835
1836 /* Everything looks fine. Return success. */
1837 return 0;
1838}
1839#endif
1840
Brian Swetland9c4c0752009-01-25 16:23:50 -08001841void cmd_boot(const char *arg, void *data, unsigned sz)
1842{
1843 unsigned kernel_actual;
1844 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001845 uint32_t image_actual;
1846 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001847 uint32_t sig_actual = SIGNATURE_SIZE;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001848 struct boot_img_hdr *hdr = NULL;
1849 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001850 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001851 int ret = 0;
1852 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001853 unsigned int out_len = 0;
1854 unsigned int out_avai_len = 0;
1855 unsigned char *out_addr = NULL;
1856 uint32_t dtb_offset = 0;
1857 unsigned char *kernel_start_addr = NULL;
1858 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04001859 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001860
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001861#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07001862 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001863 {
1864 fastboot_fail("unlock device to use this command");
1865 return;
1866 }
1867#endif
1868
Brian Swetland9c4c0752009-01-25 16:23:50 -08001869 if (sz < sizeof(hdr)) {
1870 fastboot_fail("invalid bootimage header");
1871 return;
1872 }
1873
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001874 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001875
1876 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001877 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001878
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001879 if(target_is_emmc_boot() && hdr->page_size) {
1880 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001881 page_mask = page_size - 1;
1882 }
1883
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001884 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1885 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001886#if DEVICE_TREE
1887 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1888#endif
1889
1890 image_actual = ADD_OF(page_size, kernel_actual);
1891 image_actual = ADD_OF(image_actual, ramdisk_actual);
1892 image_actual = ADD_OF(image_actual, dt_actual);
1893
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001894 if (target_use_signed_kernel() && (!device.is_unlocked))
1895 image_actual = ADD_OF(image_actual, sig_actual);
1896
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001897 /* sz should have atleast raw boot image */
1898 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001899 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001900 return;
1901 }
1902
1903 /* Verify the boot image
1904 * device & page_size are initialized in aboot_init
1905 */
1906 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001907 /* Pass size excluding signature size, otherwise we would try to
1908 * access signature beyond its length
1909 */
1910 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001911
Amit Blay4aa292f2015-04-28 21:55:59 +03001912#ifdef MDTP_SUPPORT
1913 else
1914 {
1915 /* Verify MDTP lock before continue with boot process.
1916 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1917 * since verification was skipped in aboot. The signarue is already part of the loaded image.
1918 */
1919 mdtp_ext_partition_verification_t ext_partition;
1920 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1921 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1922 ext_partition.page_size = page_size;
1923 ext_partition.image_addr = (uint32_t)data;
1924 ext_partition.image_size = image_actual - sig_actual;
1925 ext_partition.sig_avail = TRUE;
1926 mdtp_fwlock_verify_lock(&ext_partition);
1927 }
1928#endif /* MDTP_SUPPORT */
1929
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001930 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001931 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1932 * If not, continue booting.
1933 */
1934 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
1935 {
1936 out_addr = (unsigned char *)target_get_scratch_address();
1937 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
1938 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001939 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001940 ret = decompress((unsigned char *)(ptr + page_size),
1941 hdr->kernel_size, out_addr, out_avai_len,
1942 &dtb_offset, &out_len);
1943 if (ret)
1944 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001945 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001946 ASSERT(0);
1947 }
1948
Matthew Qin0b15b322015-05-19 05:20:54 -04001949 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001950 kptr = (struct kernel64_hdr *)out_addr;
1951 kernel_start_addr = out_addr;
1952 kernel_size = out_len;
1953 } else {
1954 kptr = (struct kernel64_hdr*)((char *)data + page_size);
1955 kernel_start_addr = (unsigned char *)((char *)data + page_size);
1956 kernel_size = hdr->kernel_size;
1957 }
1958
1959 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001960 * Update the kernel/ramdisk/tags address if the boot image header
1961 * has default values, these default values come from mkbootimg when
1962 * the boot image is flashed using fastboot flash:raw
1963 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001964 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001965
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001966 /* Get virtual addresses since the hdr saves physical addresses. */
1967 hdr->kernel_addr = VA(hdr->kernel_addr);
1968 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1969 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001970
Matthew Qinbb7923d2015-02-09 10:56:09 +08001971 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001972 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001973 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001974 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1975 {
1976 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001977 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001978 }
1979
Amol Jadicb524072012-08-09 16:40:18 -07001980#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001981 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07001982 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04001983 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001984
1985 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001986#else
1987 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1988 {
1989 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001990 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001991 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001992#endif
1993
1994 /* Load ramdisk & kernel */
1995 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08001996 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001997
1998#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08001999 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2000 {
2001 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2002 return;
2003 }
2004
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002005 /*
2006 * If dtb is not found look for appended DTB in the kernel.
2007 * If appended dev tree is found, update the atags with
2008 * memory address to the DTB appended location on RAM.
2009 * Else update with the atags address in the kernel header
2010 */
2011 if (!dtb_copied) {
2012 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002013 dtb = dev_tree_appended((void*)(ptr + page_size),
2014 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002015 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002016 if (!dtb) {
2017 fastboot_fail("dtb not found");
2018 return;
2019 }
Amol Jadicb524072012-08-09 16:40:18 -07002020 }
2021#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002022
2023 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002024 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002025
Dima Zavin77e41f32013-03-06 16:10:43 -08002026 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002027 (const char*) hdr->cmdline, board_machtype(),
2028 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002029}
2030
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002031void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002032{
2033 struct ptentry *ptn;
2034 struct ptable *ptable;
2035
2036 ptable = flash_get_ptable();
2037 if (ptable == NULL) {
2038 fastboot_fail("partition table doesn't exist");
2039 return;
2040 }
2041
2042 ptn = ptable_find(ptable, arg);
2043 if (ptn == NULL) {
2044 fastboot_fail("unknown partition name");
2045 return;
2046 }
2047
2048 if (flash_erase(ptn)) {
2049 fastboot_fail("failed to erase partition");
2050 return;
2051 }
2052 fastboot_okay("");
2053}
2054
Bikas Gurungd48bd242010-09-04 19:54:32 -07002055
2056void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2057{
2058 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002059 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002060 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002061 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002062
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002063#if VERIFIED_BOOT
2064 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2065 {
2066 if(!device.is_unlocked)
2067 {
2068 fastboot_fail("unlock device to erase keystore");
2069 return;
2070 }
2071 }
2072#endif
2073
Kinson Chikf1a43512011-07-14 11:28:39 -07002074 index = partition_get_index(arg);
2075 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002076 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002077
Kinson Chikf1a43512011-07-14 11:28:39 -07002078 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002079 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002080 return;
2081 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002082
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002083 lun = partition_get_lun(index);
2084 mmc_set_lun(lun);
2085
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002086 if (platform_boot_dev_isemmc())
2087 {
2088 if (mmc_erase_card(ptn, size)) {
2089 fastboot_fail("failed to erase partition\n");
2090 return;
2091 }
2092 } else {
2093 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2094 size = partition_get_size(index);
2095 if (size > DEFAULT_ERASE_SIZE)
2096 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002097
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002098 /* Simple inefficient version of erase. Just writing
2099 0 in first several blocks */
2100 if (mmc_write(ptn , size, (unsigned int *)out)) {
2101 fastboot_fail("failed to erase partition");
2102 return;
2103 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002104 }
2105 fastboot_okay("");
2106}
2107
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002108void cmd_erase(const char *arg, void *data, unsigned sz)
2109{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002110#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002111 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002112 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002113 if(!device.is_unlocked && !device.is_verified)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002114 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002115 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002116 return;
2117 }
Channagoud Kadabi35297672015-06-13 11:09:49 -07002118 if(!device.is_unlocked && device.is_verified)
2119 {
2120 if(!boot_verify_flash_allowed(arg))
2121 {
2122 fastboot_fail("cannot flash this partition in verified state");
2123 return;
2124 }
2125 }
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002126 }
2127#endif
2128
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002129 if(target_is_emmc_boot())
2130 cmd_erase_mmc(arg, data, sz);
2131 else
2132 cmd_erase_nand(arg, data, sz);
2133}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002134
Ajay Dudani5c761132011-04-07 20:19:04 -07002135void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002136{
2137 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002138 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002139 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002140 char *token = NULL;
2141 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002142 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002143 uint8_t lun = 0;
2144 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002145
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002146 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002147 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002148 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002149 if(token)
2150 {
2151 lun = atoi(token);
2152 mmc_set_lun(lun);
2153 lun_set = true;
2154 }
2155
Mao Jinlong226f33a2014-07-04 17:24:10 +08002156 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002157 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002158 if (!strcmp(pname, "partition"))
2159 {
2160 dprintf(INFO, "Attempt to write partition image.\n");
2161 if (write_partition(sz, (unsigned char *) data)) {
2162 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002163 return;
2164 }
2165 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002166 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002167 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002168#if VERIFIED_BOOT
2169 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2170 {
2171 if(!device.is_unlocked)
2172 {
2173 fastboot_fail("unlock device to flash keystore");
2174 return;
2175 }
2176 if(!boot_verify_validate_keystore((unsigned char *)data))
2177 {
2178 fastboot_fail("image is not a keystore file");
2179 return;
2180 }
2181 }
2182#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002183 index = partition_get_index(pname);
2184 ptn = partition_get_offset(index);
2185 if(ptn == 0) {
2186 fastboot_fail("partition table doesn't exist");
2187 return;
2188 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002189
Mao Jinlong226f33a2014-07-04 17:24:10 +08002190 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2191 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2192 fastboot_fail("image is not a boot image");
2193 return;
2194 }
2195 }
2196
2197 if(!lun_set)
2198 {
2199 lun = partition_get_lun(index);
2200 mmc_set_lun(lun);
2201 }
2202
2203 size = partition_get_size(index);
2204 if (ROUND_TO_PAGE(sz,511) > size) {
2205 fastboot_fail("size too large");
2206 return;
2207 }
2208 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2209 fastboot_fail("flash write failure");
2210 return;
2211 }
Greg Grisco6e754772011-06-23 12:19:39 -07002212 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002213 }
2214 fastboot_okay("");
2215 return;
2216}
2217
Ajay Dudanide984792015-03-02 09:57:41 -08002218void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2219{
2220 int i, images;
2221 meta_header_t *meta_header;
2222 img_header_entry_t *img_header_entry;
2223
2224 meta_header = (meta_header_t*) data;
2225 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2226
2227 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2228
2229 for (i=0; i<images; i++) {
2230
2231 if((img_header_entry[i].ptn_name == NULL) ||
2232 (img_header_entry[i].start_offset == 0) ||
2233 (img_header_entry[i].size == 0))
2234 break;
2235
2236 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2237 (void *) data + img_header_entry[i].start_offset,
2238 img_header_entry[i].size);
2239 }
2240
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002241 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2242 {
2243 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2244 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2245 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2246 }
2247 else
2248 {
2249 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2250 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2251 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2252 }
2253
2254 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002255 fastboot_okay("");
2256 return;
2257}
2258
Ajay Dudani5c761132011-04-07 20:19:04 -07002259void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2260{
2261 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002262 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002263 uint32_t *fill_buf = NULL;
2264 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002265 sparse_header_t *sparse_header;
2266 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002267 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002268 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302269 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002270 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302271 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002272 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302273 /*End of the sparse image address*/
2274 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002275
Kinson Chikf1a43512011-07-14 11:28:39 -07002276 index = partition_get_index(arg);
2277 ptn = partition_get_offset(index);
2278 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002279 fastboot_fail("partition table doesn't exist");
2280 return;
2281 }
2282
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302283 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302284
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002285 lun = partition_get_lun(index);
2286 mmc_set_lun(lun);
2287
vijay kumar800255e2015-04-24 20:26:19 +05302288 if (sz < sizeof(sparse_header_t)) {
2289 fastboot_fail("size too low");
2290 return;
2291 }
2292
Ajay Dudani5c761132011-04-07 20:19:04 -07002293 /* Read and skip over sparse image header */
2294 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302295
vijay kumar1321f342015-03-27 12:13:42 +05302296 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002297 fastboot_fail("size too large");
2298 return;
2299 }
2300
vijay kumarde4fcf62015-04-23 13:05:49 +05302301 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302302
2303 if (data_end < (uint32_t)data) {
2304 fastboot_fail("buffer overreads occured due to invalid sparse header");
2305 return;
2306 }
2307
vijay kumarde4fcf62015-04-23 13:05:49 +05302308 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002309 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302310 fastboot_fail("sparse header size mismatch");
2311 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002312 }
2313
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002314 dprintf (SPEW, "=== Sparse Image Header ===\n");
2315 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2316 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2317 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2318 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2319 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2320 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2321 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2322 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002323
2324 /* Start processing chunks */
2325 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2326 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302327 /* Make sure the total image size does not exceed the partition size */
2328 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2329 fastboot_fail("size too large");
2330 return;
2331 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002332 /* Read and skip over chunk header */
2333 chunk_header = (chunk_header_t *) data;
2334 data += sizeof(chunk_header_t);
2335
vijay kumar800255e2015-04-24 20:26:19 +05302336 if (data_end < (uint32_t)data) {
2337 fastboot_fail("buffer overreads occured due to invalid sparse header");
2338 return;
2339 }
2340
Ajay Dudani5c761132011-04-07 20:19:04 -07002341 dprintf (SPEW, "=== Chunk Header ===\n");
2342 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2343 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2344 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2345
vijay kumar800255e2015-04-24 20:26:19 +05302346 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002347 {
vijay kumar800255e2015-04-24 20:26:19 +05302348 fastboot_fail("chunk header size mismatch");
2349 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002350 }
2351
wufeng.jiang813dc352015-06-02 23:02:46 -04002352 if (!sparse_header->blk_sz ){
2353 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302354 return;
2355 }
2356
wufeng.jiang813dc352015-06-02 23:02:46 -04002357 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2358
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302359 /* Make sure that the chunk size calculated from sparse image does not
2360 * exceed partition size
2361 */
2362 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2363 {
2364 fastboot_fail("Chunk data size exceeds partition size");
2365 return;
2366 }
2367
Ajay Dudani5c761132011-04-07 20:19:04 -07002368 switch (chunk_header->chunk_type)
2369 {
2370 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002371 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002372 chunk_data_sz))
2373 {
2374 fastboot_fail("Bogus chunk size for chunk type Raw");
2375 return;
2376 }
2377
vijay kumar800255e2015-04-24 20:26:19 +05302378 if (data_end < (uint32_t)data + chunk_data_sz) {
2379 fastboot_fail("buffer overreads occured due to invalid sparse header");
2380 return;
2381 }
2382
wufeng.jiang813dc352015-06-02 23:02:46 -04002383 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2384 otherwise it will return in the line above
2385 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002386 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002387 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002388 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002389 {
2390 fastboot_fail("flash write failure");
2391 return;
2392 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302393 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2394 fastboot_fail("Bogus size for RAW chunk type");
2395 return;
2396 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002397 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002398 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002399 break;
2400
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002401 case CHUNK_TYPE_FILL:
2402 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2403 sizeof(uint32_t)))
2404 {
2405 fastboot_fail("Bogus chunk size for chunk type FILL");
2406 return;
2407 }
2408
2409 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2410 if (!fill_buf)
2411 {
2412 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2413 return;
2414 }
2415
vijay kumar800255e2015-04-24 20:26:19 +05302416 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2417 fastboot_fail("buffer overreads occured due to invalid sparse header");
2418 return;
2419 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002420 fill_val = *(uint32_t *)data;
2421 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002422
2423 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2424 {
2425 fill_buf[i] = fill_val;
2426 }
2427
wufeng.jiang813dc352015-06-02 23:02:46 -04002428 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002429 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302430 /* Make sure that the data written to partition does not exceed partition size */
2431 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2432 {
2433 fastboot_fail("Chunk data size for fill type exceeds partition size");
2434 return;
2435 }
2436
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002437 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2438 sparse_header->blk_sz,
2439 fill_buf))
2440 {
2441 fastboot_fail("flash write failure");
2442 free(fill_buf);
2443 return;
2444 }
2445
2446 total_blocks++;
2447 }
2448
2449 free(fill_buf);
2450 break;
2451
Ajay Dudani5c761132011-04-07 20:19:04 -07002452 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302453 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2454 fastboot_fail("bogus size for chunk DONT CARE type");
2455 return;
2456 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002457 total_blocks += chunk_header->chunk_sz;
2458 break;
2459
Ajay Dudani5c761132011-04-07 20:19:04 -07002460 case CHUNK_TYPE_CRC:
2461 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2462 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002463 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002464 return;
2465 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302466 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2467 fastboot_fail("bogus size for chunk CRC type");
2468 return;
2469 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002470 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302471 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2472 fastboot_fail("integer overflow occured");
2473 return;
2474 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002475 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302476 if (data_end < (uint32_t)data) {
2477 fastboot_fail("buffer overreads occured due to invalid sparse header");
2478 return;
2479 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002480 break;
2481
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002482 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002483 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002484 fastboot_fail("Unknown chunk type");
2485 return;
2486 }
2487 }
2488
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002489 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2490 total_blocks, sparse_header->total_blks);
2491
2492 if(total_blocks != sparse_header->total_blks)
2493 {
2494 fastboot_fail("sparse image write failure");
2495 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002496
2497 fastboot_okay("");
2498 return;
2499}
2500
2501void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2502{
2503 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002504 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002505
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002506#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002507 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2508 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002509 int ret=0;
2510 uint32 major_version=0;
2511 uint32 minor_version=0;
2512
2513 ret = scm_svc_version(&major_version,&minor_version);
2514 if(!ret)
2515 {
2516 if(major_version >= 2)
2517 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002518 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002519 {
2520 ret = encrypt_scm((uint32 **) &data, &sz);
2521 if (ret != 0) {
2522 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2523 return;
2524 }
2525
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002526 /* Protect only for SSD */
2527 if (!strcmp(arg, "ssd")) {
2528 ret = scm_protect_keystore((uint32 *) data, sz);
2529 if (ret != 0) {
2530 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2531 return;
2532 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002533 }
2534 }
2535 else
2536 {
2537 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2538 if(ret != 0)
2539 {
2540 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2541 return;
2542 }
2543 }
2544 }
2545 else
2546 {
2547 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2548 magic_number[1] == DECRYPT_MAGIC_1)
2549 {
2550 ret = decrypt_scm((uint32 **) &data, &sz);
2551 if (ret != 0) {
2552 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2553 return;
2554 }
2555 }
2556 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2557 magic_number[1] == ENCRYPT_MAGIC_1)
2558 {
2559 ret = encrypt_scm((uint32 **) &data, &sz);
2560 if (ret != 0) {
2561 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2562 return;
2563 }
2564 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002565 }
2566 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002567 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002568 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002569 dprintf(CRITICAL,"INVALID SVC Version\n");
2570 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002571 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002572#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002573
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002574#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002575 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002576 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002577 if(!device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002578 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002579 fastboot_fail("device is locked. Cannot flash images");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002580 return;
2581 }
Channagoud Kadabi35297672015-06-13 11:09:49 -07002582 if(!device.is_unlocked && device.is_verified)
2583 {
2584 if(!boot_verify_flash_allowed(arg))
2585 {
2586 fastboot_fail("cannot flash this partition in verified state");
2587 return;
2588 }
2589 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002590 }
2591#endif
2592
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002593 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002594 meta_header = (meta_header_t *) data;
2595 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002596 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002597 else if (meta_header->magic == META_HEADER_MAGIC)
2598 cmd_flash_meta_img(arg, data, sz);
2599 else
2600 cmd_flash_mmc_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002601 return;
2602}
2603
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002604void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2605{
2606 struct ptentry *sys_ptn;
2607 struct ptable *ptable;
2608
2609 ptable = flash_get_ptable();
2610 if (ptable == NULL) {
2611 fastboot_fail("partition table doesn't exist");
2612 return;
2613 }
2614
2615 sys_ptn = ptable_find(ptable, "system");
2616 if (sys_ptn == NULL) {
2617 fastboot_fail("system partition not found");
2618 return;
2619 }
2620
2621 sz = ROUND_TO_PAGE(sz, page_mask);
2622 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2623 fastboot_fail("update_ubi_vol failed");
2624 else
2625 fastboot_okay("");
2626}
2627
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002628void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002629{
2630 struct ptentry *ptn;
2631 struct ptable *ptable;
2632 unsigned extra = 0;
2633
2634 ptable = flash_get_ptable();
2635 if (ptable == NULL) {
2636 fastboot_fail("partition table doesn't exist");
2637 return;
2638 }
2639
2640 ptn = ptable_find(ptable, arg);
2641 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002642 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2643 arg);
2644 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002645 return;
2646 }
2647
2648 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2649 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2650 fastboot_fail("image is not a boot image");
2651 return;
2652 }
2653 }
2654
Amol Jadi5c61a952012-05-04 17:05:35 -07002655 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002656 || !strcmp(ptn->name, "userdata")
2657 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002658 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002659 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002660 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002661 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002662 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002663
2664 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002665 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2666 if (flash_ubi_img(ptn, data, sz)) {
2667 fastboot_fail("flash write failure");
2668 return;
2669 }
2670 } else {
2671 if (flash_write(ptn, extra, data, sz)) {
2672 fastboot_fail("flash write failure");
2673 return;
2674 }
Dima Zavin214cc642009-01-26 11:16:21 -08002675 }
2676 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2677 fastboot_okay("");
2678}
2679
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002680void cmd_flash(const char *arg, void *data, unsigned sz)
2681{
2682 if(target_is_emmc_boot())
2683 cmd_flash_mmc(arg, data, sz);
2684 else
2685 cmd_flash_nand(arg, data, sz);
2686}
2687
Dima Zavin214cc642009-01-26 11:16:21 -08002688void cmd_continue(const char *arg, void *data, unsigned sz)
2689{
2690 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002691 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002692
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002693 if (target_is_emmc_boot())
2694 {
2695 boot_linux_from_mmc();
2696 }
2697 else
2698 {
2699 boot_linux_from_flash();
2700 }
Dima Zavin214cc642009-01-26 11:16:21 -08002701}
2702
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002703void cmd_reboot(const char *arg, void *data, unsigned sz)
2704{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002705 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002706 fastboot_okay("");
2707 reboot_device(0);
2708}
2709
2710void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2711{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002712 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002713 fastboot_okay("");
2714 reboot_device(FASTBOOT_MODE);
2715}
2716
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002717void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2718{
2719 dprintf(INFO, "Enabling charger screen check\n");
2720 device.charger_screen_enabled = 1;
2721 write_device_info(&device);
2722 fastboot_okay("");
2723}
2724
2725void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2726{
2727 dprintf(INFO, "Disabling charger screen check\n");
2728 device.charger_screen_enabled = 0;
2729 write_device_info(&device);
2730 fastboot_okay("");
2731}
2732
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302733void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2734{
2735 dprintf(INFO, "Selecting display panel %s\n", arg);
2736 if (arg)
2737 strlcpy(device.display_panel, arg,
2738 sizeof(device.display_panel));
2739 write_device_info(&device);
2740 fastboot_okay("");
2741}
2742
Shashank Mittal162244e2011-08-08 19:01:25 -07002743void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2744{
vijay kumarc65876c2015-04-24 13:29:16 +05302745 if(!is_allow_unlock) {
2746 fastboot_fail("oem unlock is not allowed");
2747 return;
2748 }
2749
2750 display_fbcon_message("Oem Unlock requested");
2751 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2752}
2753
2754void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
2755{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002756 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002757 {
vijay kumarc65876c2015-04-24 13:29:16 +05302758 if(!is_allow_unlock) {
2759 fastboot_fail("oem unlock is not allowed");
2760 return;
2761 }
2762
Shashank Mittal162244e2011-08-08 19:01:25 -07002763 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002764 device.is_verified = 0;
2765 write_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05302766
2767 struct recovery_message msg;
2768 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2769 write_misc(0, &msg, sizeof(msg));
2770
2771 fastboot_okay("");
2772 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002773 }
2774 fastboot_okay("");
2775}
2776
2777void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2778{
2779 /* TODO: Wipe user data */
2780 if(device.is_unlocked || device.is_verified)
2781 {
2782 device.is_unlocked = 0;
2783 device.is_verified = 0;
2784 write_device_info(&device);
2785 }
2786 fastboot_okay("");
2787}
2788
2789void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2790{
2791 /* TODO: Wipe user data */
2792 if(device.is_unlocked || !device.is_verified)
2793 {
2794 device.is_unlocked = 0;
2795 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002796 write_device_info(&device);
2797 }
2798 fastboot_okay("");
2799}
2800
Shashank Mittala0032282011-08-26 14:50:11 -07002801void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2802{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302803 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002804 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002805 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002806 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2807 fastboot_info(response);
2808 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002809 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302810 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2811 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002812 fastboot_okay("");
2813}
2814
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002815void cmd_preflash(const char *arg, void *data, unsigned sz)
2816{
2817 fastboot_okay("");
2818}
2819
Mao Flynn7b379f32015-04-20 00:28:30 +08002820static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302821
Mao Flynn7b379f32015-04-20 00:28:30 +08002822int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302823{
Mao Flynn7b379f32015-04-20 00:28:30 +08002824 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302825 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08002826 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302827 return -1;
2828 return 0;
2829}
2830
Mao Flynn7b379f32015-04-20 00:28:30 +08002831int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002832{
2833 struct ptentry *ptn;
2834 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08002835 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002836 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002837
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302838 ptable = flash_get_ptable();
2839 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002840 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2841 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302842 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002843
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302844 ptn = ptable_find(ptable, "splash");
2845 if (ptn == NULL) {
2846 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002847 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302848 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002849 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302850 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002851 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302852 }
2853
Mao Flynn7b379f32015-04-20 00:28:30 +08002854 header = (struct logo_img_header *)logo_header;
2855 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302856 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002857 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302858 }
2859
2860 fb_display = fbcon_display();
2861 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002862 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
2863 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
2864
2865 /* if the logo is full-screen size, remove "fbcon_clear()" */
2866 if ((header->width != fb_display->width)
2867 || (header->height != fb_display->height))
2868 fbcon_clear();
2869
2870 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
2871 (uint32_t *)base,
2872 (header->blocks * 512))) {
2873 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
2874 return -1;
2875 }
2876 fbcon_extract_to_screen(header, base);
2877 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002878 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002879
2880 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
2881 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
2882 return -1;
2883 }
2884
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302885 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08002886 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
2887 (uint32_t *)base,
2888 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302889 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302890 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002891 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002892 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302893 }
2894
Mao Flynn7b379f32015-04-20 00:28:30 +08002895 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302896}
2897
Mao Flynn7b379f32015-04-20 00:28:30 +08002898int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302899{
2900 int index = INVALID_PTN;
2901 unsigned long long ptn = 0;
2902 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08002903 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08002904 uint32_t blocksize, realsize, readsize;
2905 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302906
2907 index = partition_get_index("splash");
2908 if (index == 0) {
2909 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002910 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302911 }
2912
2913 ptn = partition_get_offset(index);
2914 if (ptn == 0) {
2915 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002916 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302917 }
2918
Mao Flynn1893a7f2015-06-03 12:03:36 +08002919 mmc_set_lun(partition_get_lun(index));
2920
2921 blocksize = mmc_get_device_blocksize();
2922 if (blocksize == 0) {
2923 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
2924 return -1;
2925 }
2926
2927 fb_display = fbcon_display();
2928 base = (uint8_t *) fb_display->base;
2929
2930 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302931 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002932 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302933 }
2934
Mao Flynn1893a7f2015-06-03 12:03:36 +08002935 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08002936 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302937 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002938 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302939 }
2940
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302941 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08002942 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
2943 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08002944
Mao Flynn1893a7f2015-06-03 12:03:36 +08002945 realsize = header->blocks * 512;
2946 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
2947
2948 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08002949 if ((header->width != fb_display->width)
2950 || (header->height != fb_display->height))
2951 fbcon_clear();
2952
Mao Flynn1893a7f2015-06-03 12:03:36 +08002953 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002954 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
2955 return -1;
2956 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002957
Mao Flynn1893a7f2015-06-03 12:03:36 +08002958 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
2959 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302960
Mao Flynn1893a7f2015-06-03 12:03:36 +08002961 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
2962 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
2963 return -1;
2964 }
2965
2966 realsize = header->width * header->height * fb_display->bpp / 8;
2967 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
2968
2969 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
2970 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
2971 fbcon_clear();
2972 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
2973 return -1;
2974 }
2975 } else {
2976 if (mmc_read(ptn + blocksize ,
2977 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
2978 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
2979 return -1;
2980 }
2981 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
2982 }
2983 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302984 }
2985
Mao Flynn7b379f32015-04-20 00:28:30 +08002986 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302987}
2988
Mao Flynn7b379f32015-04-20 00:28:30 +08002989int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302990{
2991 if (target_is_emmc_boot()) {
2992 return splash_screen_mmc();
2993 } else {
2994 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002995 }
2996}
2997
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002998/* Get the size from partiton name */
2999static void get_partition_size(const char *arg, char *response)
3000{
3001 uint64_t ptn = 0;
3002 uint64_t size;
3003 int index = INVALID_PTN;
3004
3005 index = partition_get_index(arg);
3006
3007 if (index == INVALID_PTN)
3008 {
3009 dprintf(CRITICAL, "Invalid partition index\n");
3010 return;
3011 }
3012
3013 ptn = partition_get_offset(index);
3014
3015 if(!ptn)
3016 {
3017 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3018 return;
3019 }
3020
3021 size = partition_get_size(index);
3022
3023 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3024 return;
3025}
3026
3027/*
3028 * Publish the partition type & size info
3029 * fastboot getvar will publish the required information.
3030 * fastboot getvar partition_size:<partition_name>: partition size in hex
3031 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3032 */
3033static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3034{
3035 uint8_t i;
3036
3037 for (i = 0; i < num_parts; i++) {
3038 get_partition_size(info[i].part_name, info[i].size_response);
3039
3040 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3041 {
3042 dprintf(CRITICAL, "partition size name truncated\n");
3043 return;
3044 }
3045 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3046 {
3047 dprintf(CRITICAL, "partition type name truncated\n");
3048 return;
3049 }
3050
3051 /* publish partition size & type info */
3052 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3053 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3054 }
3055}
3056
Amol Jadi5edf3552013-07-23 14:15:34 -07003057/* register commands and variables for fastboot */
3058void aboot_fastboot_register_commands(void)
3059{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003060 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07003061
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003062 struct fastboot_cmd_desc cmd_list[] = {
3063 /* By default the enabled list is empty. */
3064 {"", NULL},
3065 /* move commands enclosed within the below ifndef to here
3066 * if they need to be enabled in user build.
3067 */
3068#ifndef DISABLE_FASTBOOT_CMDS
3069 /* Register the following commands only for non-user builds */
3070 {"flash:", cmd_flash},
3071 {"erase:", cmd_erase},
3072 {"boot", cmd_boot},
3073 {"continue", cmd_continue},
3074 {"reboot", cmd_reboot},
3075 {"reboot-bootloader", cmd_reboot_bootloader},
3076 {"oem unlock", cmd_oem_unlock},
vijay kumarc65876c2015-04-24 13:29:16 +05303077 {"oem unlock-go", cmd_oem_unlock_go},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003078 {"oem lock", cmd_oem_lock},
3079 {"oem verified", cmd_oem_verified},
3080 {"oem device-info", cmd_oem_devinfo},
3081 {"preflash", cmd_preflash},
3082 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3083 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08003084 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003085#endif
3086 };
3087
3088 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3089 for (i = 1; i < fastboot_cmds_count; i++)
3090 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3091
Amol Jadi5edf3552013-07-23 14:15:34 -07003092 /* publish variables and their values */
3093 fastboot_publish("product", TARGET(BOARD));
3094 fastboot_publish("kernel", "lk");
3095 fastboot_publish("serialno", sn_buf);
3096
3097 /*
3098 * partition info is supported only for emmc partitions
3099 * Calling this for NAND prints some error messages which
3100 * is harmless but misleading. Avoid calling this for NAND
3101 * devices.
3102 */
3103 if (target_is_emmc_boot())
3104 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3105
3106 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003107 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3108 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003109 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003110 /* Is the charger screen check enabled */
3111 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3112 device.charger_screen_enabled);
3113 fastboot_publish("charger-screen-enabled",
3114 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303115 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3116 device.display_panel);
3117 fastboot_publish("display-panel",
3118 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003119 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3120 fastboot_publish("version-baseband", (const char *) device.radio_version);
Amol Jadi5edf3552013-07-23 14:15:34 -07003121}
3122
Brian Swetland9c4c0752009-01-25 16:23:50 -08003123void aboot_init(const struct app_descriptor *app)
3124{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003125 unsigned reboot_mode = 0;
lijuang72875802015-02-10 10:35:12 +08003126 unsigned hard_reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03003127 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003128
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003129 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003130 if (target_is_emmc_boot())
3131 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003132 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003133 page_mask = page_size - 1;
3134 }
3135 else
3136 {
3137 page_size = flash_page_size();
3138 page_mask = page_size - 1;
3139 }
3140
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003141 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3142
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003143 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303144 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003145
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003146 /* Display splash screen if enabled */
3147#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003148#if NO_ALARM_DISPLAY
3149 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003150#endif
lijuang99c02d82015-02-13 19:04:34 +08003151 dprintf(SPEW, "Display Init: Start\n");
3152 target_display_init(device.display_panel);
3153 dprintf(SPEW, "Display Init: Done\n");
3154#if NO_ALARM_DISPLAY
3155 }
3156#endif
3157#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003158
Greg Griscod6250552011-06-29 14:40:23 -07003159 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003160 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003161
Dhaval Patel223ec952013-07-18 14:49:44 -07003162 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3163
Matthew Qindefd5562014-07-11 18:02:40 +08003164 /*
3165 * Check power off reason if user force reset,
3166 * if yes phone will do normal boot.
3167 */
3168 if (is_user_force_reset())
3169 goto normal_boot;
3170
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003171 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003172 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003173 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003174 dprintf(ALWAYS,"dload mode key sequence detected\n");
3175 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003176 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003177 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003178 }
3179 else
3180 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303181 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003182 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003183 }
3184 boot_into_fastboot = true;
3185 }
3186 if (!boot_into_fastboot)
3187 {
3188 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3189 boot_into_recovery = 1;
3190 if (!boot_into_recovery &&
3191 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003192 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003193 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003194 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003195 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003196 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003197 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003198
Ajay Dudani77421292010-10-27 19:34:06 -07003199 reboot_mode = check_reboot_mode();
lijuang72875802015-02-10 10:35:12 +08003200 hard_reboot_mode = check_hard_reboot_mode();
3201 if (reboot_mode == RECOVERY_MODE ||
3202 hard_reboot_mode == RECOVERY_HARD_RESET_MODE) {
Ajay Dudani77421292010-10-27 19:34:06 -07003203 boot_into_recovery = 1;
lijuang72875802015-02-10 10:35:12 +08003204 } else if(reboot_mode == FASTBOOT_MODE ||
3205 hard_reboot_mode == FASTBOOT_HARD_RESET_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003206 boot_into_fastboot = true;
lijuang72875802015-02-10 10:35:12 +08003207 } else if(reboot_mode == ALARM_BOOT ||
3208 hard_reboot_mode == RTC_HARD_RESET_MODE) {
Matthew Qind886f3c2014-01-17 16:52:01 +08003209 boot_reason_alarm = true;
Ajay Dudani77421292010-10-27 19:34:06 -07003210 }
3211
Matthew Qindefd5562014-07-11 18:02:40 +08003212normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003213 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003214 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003215 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003216 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003217 if(emmc_recovery_init())
3218 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3219 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003220 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003221 if((device.is_unlocked) || (device.is_tampered))
3222 {
3223 #ifdef TZ_TAMPER_FUSE
3224 set_tamper_fuse_cmd();
3225 #endif
3226 #if USE_PCOM_SECBOOT
3227 set_tamper_flag(device.is_tampered);
3228 #endif
3229 }
Shashank Mittala0032282011-08-26 14:50:11 -07003230 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003231
Pavel Nedev5d91d412013-04-29 11:34:24 +03003232 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003233 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003234 else
3235 {
3236 recovery_init();
3237 #if USE_PCOM_SECBOOT
3238 if((device.is_unlocked) || (device.is_tampered))
3239 set_tamper_flag(device.is_tampered);
3240 #endif
3241 boot_linux_from_flash();
3242 }
3243 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3244 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003245 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003246
Amol Jadi5edf3552013-07-23 14:15:34 -07003247 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003248
Amol Jadi5edf3552013-07-23 14:15:34 -07003249 /* register aboot specific fastboot commands */
3250 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003251
Amol Jadi5edf3552013-07-23 14:15:34 -07003252 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003253 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003254
3255 /* initialize and start fastboot */
3256 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08003257}
3258
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003259uint32_t get_page_size()
3260{
3261 return page_size;
3262}
3263
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003264/*
3265 * Calculated and save hash (SHA256) for non-signed boot image.
3266 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003267 * @param image_addr - Boot image address
3268 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003269 *
3270 * @return int - 0 on success, negative value on failure.
3271 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003272static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003273{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003274 unsigned int digest[8];
3275#if IMAGE_VERIF_ALGO_SHA1
3276 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3277#else
3278 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3279#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003280
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003281 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003282 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003283
3284 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003285 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003286
3287 return 0;
3288}
3289
Brian Swetland9c4c0752009-01-25 16:23:50 -08003290APP_START(aboot)
3291 .init = aboot_init,
3292APP_END