blob: 07a8d76d5ec86f2eaefeb90da814a892a83e50dc [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08005 * Copyright (c) 2009-2015, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080060#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053061#include <platform/timer.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080062#if USE_RPMB_FOR_DEVINFO
63#include <rpmb.h>
64#endif
Dima Zavin214cc642009-01-26 11:16:21 -080065
Neeti Desai17379b82012-06-04 18:42:53 -070066#if DEVICE_TREE
67#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070068#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070069#endif
70
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053071#if WDOG_SUPPORT
72#include <wdog.h>
73#endif
74
Shashank Mittalcd98d472011-08-02 14:29:24 -070075#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080076#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080077#include "bootimg.h"
78#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070079#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080080#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070081#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070082#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070083#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070084#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020085#include "mdtp.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070086
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070087extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070088extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070089extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070090extern int get_target_boot_params(const char *cmdline, const char *part,
91 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070092
93void write_device_info_mmc(device_info *dev);
94void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -070095static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070096
Sridhar Parasurame94e8152014-10-24 14:06:03 -070097/* fastboot command function pointer */
98typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
99
100struct fastboot_cmd_desc {
101 char * name;
102 fastboot_cmd_fn cb;
103};
104
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700105#define EXPAND(NAME) #NAME
106#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700107
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800108#ifdef MEMBASE
109#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
110#else
David Ng183a7422009-12-07 14:55:21 -0800111#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800112#endif
113
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700114#ifndef MEMSIZE
115#define MEMSIZE 1024*1024
116#endif
117
118#define MAX_TAGS_SIZE 1024
119
lijuang72875802015-02-10 10:35:12 +0800120#define RECOVERY_HARD_RESET_MODE 0x01
121#define FASTBOOT_HARD_RESET_MODE 0x02
122#define RTC_HARD_RESET_MODE 0x03
123
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800124#define RECOVERY_MODE 0x77665502
125#define FASTBOOT_MODE 0x77665500
Matthew Qind886f3c2014-01-17 16:52:01 +0800126#define ALARM_BOOT 0x77665503
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800127
Kun Liang2f1601a2013-08-12 16:29:54 +0800128/* make 4096 as default size to ensure EFS,EXT4's erasing */
129#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800130#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800131
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700132#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700133#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800134
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800135#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
136
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700137#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
138
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800139#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700140static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700141#else
David Ng183a7422009-12-07 14:55:21 -0800142static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700143#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800144static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300145static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800146static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800147static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800148static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700149static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300150static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200151static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800152
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800153static const char *baseband_apq = " androidboot.baseband=apq";
154static const char *baseband_msm = " androidboot.baseband=msm";
155static const char *baseband_csfb = " androidboot.baseband=csfb";
156static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700157static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800158static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700159static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800160static const char *baseband_dsda = " androidboot.baseband=dsda";
161static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800162static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800163static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800164
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700165static unsigned page_size = 0;
166static unsigned page_mask = 0;
167static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
168static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700169static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800170static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700171static bool devinfo_present = true;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700172
Shashank Mittalcd98d472011-08-02 14:29:24 -0700173/* Assuming unauthorized kernel image by default */
174static int auth_kernel_img = 0;
175
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -0800176static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
vijay kumarc65876c2015-04-24 13:29:16 +0530177static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700178
Dima Zavin42168f22009-01-30 11:52:22 -0800179struct atag_ptbl_entry
180{
181 char name[16];
182 unsigned offset;
183 unsigned size;
184 unsigned flags;
185};
186
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700187/*
188 * Partition info, required to be published
189 * for fastboot
190 */
191struct getvar_partition_info {
192 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
193 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
194 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
195 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
196 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
197};
198
199/*
200 * Right now, we are publishing the info for only
201 * three partitions
202 */
203struct getvar_partition_info part_info[] =
204{
205 { "system" , "partition-size:", "partition-type:", "", "ext4" },
206 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
207 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
208};
209
210char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700211char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800212char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700213char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530214char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700215
Greg Griscod2471ef2011-07-14 13:00:42 -0700216extern int emmc_recovery_init(void);
217
Kinson Chik0b1c8162011-08-31 16:31:57 -0700218#if NO_KEYPAD_DRIVER
219extern int fastboot_trigger(void);
220#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700221
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800222static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700223{
224 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700225#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800226 if (is_arm64)
227 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
228 else
229 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700230 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
231 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700232#endif
233}
234
Dima Zavin42168f22009-01-30 11:52:22 -0800235static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
236{
237 struct atag_ptbl_entry atag_ptn;
238
239 memcpy(atag_ptn.name, ptn->name, 16);
240 atag_ptn.name[15] = '\0';
241 atag_ptn.offset = ptn->start;
242 atag_ptn.size = ptn->length;
243 atag_ptn.flags = ptn->flags;
244 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
245 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
246}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800247
Neeti Desaie245d492012-06-01 12:52:13 -0700248unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800249{
David Ng183a7422009-12-07 14:55:21 -0800250 int cmdline_len = 0;
251 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800252 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700253 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800254 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300255 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700256 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700257 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200258 bool is_mdtp_activated = 0;
259#ifdef MDTP_SUPPORT
260 mdtp_activated(&is_mdtp_activated);
261#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800262
Brian Swetland9c4c0752009-01-25 16:23:50 -0800263 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800264 cmdline_len = strlen(cmdline);
265 have_cmdline = 1;
266 }
267 if (target_is_emmc_boot()) {
268 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800269#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700270 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
271 ASSERT(boot_dev_buf);
272 platform_boot_dev_cmdline(boot_dev_buf);
273 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700274#endif
David Ng183a7422009-12-07 14:55:21 -0800275 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800276
277 cmdline_len += strlen(usb_sn_cmdline);
278 cmdline_len += strlen(sn_buf);
279
Pavel Nedev5614d222013-06-17 18:01:02 +0300280 if (boot_into_recovery && gpt_exists)
281 cmdline_len += strlen(secondary_gpt_enable);
282
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200283 if(is_mdtp_activated)
284 cmdline_len += strlen(mdtp_activated_flag);
285
Pavel Nedev328ac822013-04-05 15:25:11 +0300286 if (boot_into_ffbm) {
287 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700288 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800289 /* reduce kernel console messages to speed-up boot */
290 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800291 } else if (boot_reason_alarm) {
292 cmdline_len += strlen(alarmboot_cmdline);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700293 } else if (device.charger_screen_enabled &&
294 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700295 pause_at_bootup = 1;
296 cmdline_len += strlen(battchg_pause);
297 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800298
Shashank Mittalcd98d472011-08-02 14:29:24 -0700299 if(target_use_signed_kernel() && auth_kernel_img) {
300 cmdline_len += strlen(auth_kernel);
301 }
302
Joonwoo Park61112782013-10-02 19:50:39 -0700303 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
304 "system",
305 target_boot_params,
306 sizeof(target_boot_params)) == 0) {
307 have_target_boot_params = 1;
308 cmdline_len += strlen(target_boot_params);
309 }
310
Ajay Dudanid04110c2011-01-17 23:55:07 -0800311 /* Determine correct androidboot.baseband to use */
312 switch(target_baseband())
313 {
314 case BASEBAND_APQ:
315 cmdline_len += strlen(baseband_apq);
316 break;
317
318 case BASEBAND_MSM:
319 cmdline_len += strlen(baseband_msm);
320 break;
321
322 case BASEBAND_CSFB:
323 cmdline_len += strlen(baseband_csfb);
324 break;
325
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800326 case BASEBAND_SVLTE2A:
327 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800328 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700329
330 case BASEBAND_MDM:
331 cmdline_len += strlen(baseband_mdm);
332 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700333
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800334 case BASEBAND_MDM2:
335 cmdline_len += strlen(baseband_mdm2);
336 break;
337
Amol Jadi5c61a952012-05-04 17:05:35 -0700338 case BASEBAND_SGLTE:
339 cmdline_len += strlen(baseband_sglte);
340 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530341
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800342 case BASEBAND_SGLTE2:
343 cmdline_len += strlen(baseband_sglte2);
344 break;
345
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530346 case BASEBAND_DSDA:
347 cmdline_len += strlen(baseband_dsda);
348 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800349
350 case BASEBAND_DSDA2:
351 cmdline_len += strlen(baseband_dsda2);
352 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800353 }
354
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800355 if (cmdline) {
356 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
357 target_display_panel_node(device.display_panel,
358 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
359 strlen(display_panel_buf)) {
360 cmdline_len += strlen(display_panel_buf);
361 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700362 }
363
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800364 if (target_warm_boot()) {
365 warm_boot = true;
366 cmdline_len += strlen(warmboot_cmdline);
367 }
368
David Ng183a7422009-12-07 14:55:21 -0800369 if (cmdline_len > 0) {
370 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800371 unsigned char *dst;
372
373 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
374 ASSERT(cmdline_final != NULL);
375 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700376
Amol Jadi168b7712012-03-06 16:15:00 -0800377 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800378 if (have_cmdline) {
379 src = cmdline;
380 while ((*dst++ = *src++));
381 }
382 if (target_is_emmc_boot()) {
383 src = emmc_cmdline;
384 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700385 have_cmdline = 1;
386 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800387#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700388 src = boot_dev_buf;
389 if (have_cmdline) --dst;
390 while ((*dst++ = *src++));
391#endif
David Ngf773dde2010-07-26 19:55:08 -0700392 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800393
394 src = usb_sn_cmdline;
395 if (have_cmdline) --dst;
396 have_cmdline = 1;
397 while ((*dst++ = *src++));
398 src = sn_buf;
399 if (have_cmdline) --dst;
400 have_cmdline = 1;
401 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800402 if (warm_boot) {
403 if (have_cmdline) --dst;
404 src = warmboot_cmdline;
405 while ((*dst++ = *src++));
406 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800407
Pavel Nedev5614d222013-06-17 18:01:02 +0300408 if (boot_into_recovery && gpt_exists) {
409 src = secondary_gpt_enable;
410 if (have_cmdline) --dst;
411 while ((*dst++ = *src++));
412 }
413
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200414 if (is_mdtp_activated) {
415 src = mdtp_activated_flag;
416 if (have_cmdline) --dst;
417 while ((*dst++ = *src++));
418 }
419
Pavel Nedev328ac822013-04-05 15:25:11 +0300420 if (boot_into_ffbm) {
421 src = androidboot_mode;
422 if (have_cmdline) --dst;
423 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700424 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300425 if (have_cmdline) --dst;
426 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800427 src = loglevel;
428 if (have_cmdline) --dst;
429 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800430 } else if (boot_reason_alarm) {
431 src = alarmboot_cmdline;
432 if (have_cmdline) --dst;
433 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300434 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700435 src = battchg_pause;
436 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800437 while ((*dst++ = *src++));
438 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800439
Shashank Mittalcd98d472011-08-02 14:29:24 -0700440 if(target_use_signed_kernel() && auth_kernel_img) {
441 src = auth_kernel;
442 if (have_cmdline) --dst;
443 while ((*dst++ = *src++));
444 }
445
Ajay Dudanid04110c2011-01-17 23:55:07 -0800446 switch(target_baseband())
447 {
448 case BASEBAND_APQ:
449 src = baseband_apq;
450 if (have_cmdline) --dst;
451 while ((*dst++ = *src++));
452 break;
453
454 case BASEBAND_MSM:
455 src = baseband_msm;
456 if (have_cmdline) --dst;
457 while ((*dst++ = *src++));
458 break;
459
460 case BASEBAND_CSFB:
461 src = baseband_csfb;
462 if (have_cmdline) --dst;
463 while ((*dst++ = *src++));
464 break;
465
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800466 case BASEBAND_SVLTE2A:
467 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800468 if (have_cmdline) --dst;
469 while ((*dst++ = *src++));
470 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700471
472 case BASEBAND_MDM:
473 src = baseband_mdm;
474 if (have_cmdline) --dst;
475 while ((*dst++ = *src++));
476 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700477
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800478 case BASEBAND_MDM2:
479 src = baseband_mdm2;
480 if (have_cmdline) --dst;
481 while ((*dst++ = *src++));
482 break;
483
Amol Jadi5c61a952012-05-04 17:05:35 -0700484 case BASEBAND_SGLTE:
485 src = baseband_sglte;
486 if (have_cmdline) --dst;
487 while ((*dst++ = *src++));
488 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530489
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800490 case BASEBAND_SGLTE2:
491 src = baseband_sglte2;
492 if (have_cmdline) --dst;
493 while ((*dst++ = *src++));
494 break;
495
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530496 case BASEBAND_DSDA:
497 src = baseband_dsda;
498 if (have_cmdline) --dst;
499 while ((*dst++ = *src++));
500 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800501
502 case BASEBAND_DSDA2:
503 src = baseband_dsda2;
504 if (have_cmdline) --dst;
505 while ((*dst++ = *src++));
506 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800507 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700508
509 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700510 src = display_panel_buf;
511 if (have_cmdline) --dst;
512 while ((*dst++ = *src++));
513 }
Joonwoo Park61112782013-10-02 19:50:39 -0700514
515 if (have_target_boot_params) {
516 if (have_cmdline) --dst;
517 src = target_boot_params;
518 while ((*dst++ = *src++));
519 }
Neeti Desaie245d492012-06-01 12:52:13 -0700520 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700521
522
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700523 if (boot_dev_buf)
524 free(boot_dev_buf);
525
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800526 if (cmdline_final)
527 dprintf(INFO, "cmdline: %s\n", cmdline_final);
528 else
529 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700530 return cmdline_final;
531}
532
533unsigned *atag_core(unsigned *ptr)
534{
535 /* CORE */
536 *ptr++ = 2;
537 *ptr++ = 0x54410001;
538
539 return ptr;
540
541}
542
543unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
544 unsigned ramdisk_size)
545{
546 if (ramdisk_size) {
547 *ptr++ = 4;
548 *ptr++ = 0x54420005;
549 *ptr++ = (unsigned)ramdisk;
550 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800551 }
552
Neeti Desaie245d492012-06-01 12:52:13 -0700553 return ptr;
554}
555
556unsigned *atag_ptable(unsigned **ptr_addr)
557{
558 int i;
559 struct ptable *ptable;
560
561 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700562 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
563 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700564 *(*ptr_addr)++ = 0x4d534d70;
565 for (i = 0; i < ptable->count; ++i)
566 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
567 }
568
569 return (*ptr_addr);
570}
571
572unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
573{
574 int cmdline_length = 0;
575 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700576 char *dest;
577
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800578 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700579 n = (cmdline_length + 4) & (~3);
580
581 *ptr++ = (n / 4) + 2;
582 *ptr++ = 0x54410009;
583 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800584 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700585 ptr += (n / 4);
586
587 return ptr;
588}
589
590unsigned *atag_end(unsigned *ptr)
591{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800592 /* END */
593 *ptr++ = 0;
594 *ptr++ = 0;
595
Neeti Desaie245d492012-06-01 12:52:13 -0700596 return ptr;
597}
598
599void generate_atags(unsigned *ptr, const char *cmdline,
600 void *ramdisk, unsigned ramdisk_size)
601{
602
603 ptr = atag_core(ptr);
604 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
605 ptr = target_atag_mem(ptr);
606
607 /* Skip NAND partition ATAGS for eMMC boot */
608 if (!target_is_emmc_boot()){
609 ptr = atag_ptable(&ptr);
610 }
611
612 ptr = atag_cmdline(ptr, cmdline);
613 ptr = atag_end(ptr);
614}
615
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700616typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700617void boot_linux(void *kernel, unsigned *tags,
618 const char *cmdline, unsigned machtype,
619 void *ramdisk, unsigned ramdisk_size)
620{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800621 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800622#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700623 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800624#endif
625
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700626 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800627 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800628 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800629
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530630 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700631
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800632 final_cmdline = update_cmdline((const char*)cmdline);
633
Neeti Desai17379b82012-06-04 18:42:53 -0700634#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800635 dprintf(INFO, "Updating device tree: start\n");
636
Neeti Desai17379b82012-06-04 18:42:53 -0700637 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530638 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700639 if(ret)
640 {
641 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
642 ASSERT(0);
643 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800644 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700645#else
Neeti Desaie245d492012-06-01 12:52:13 -0700646 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800647 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700648#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700649
Maria Yu52254c02014-07-04 16:14:54 +0800650 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700651
652#if VERIFIED_BOOT
653 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700654 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700655 {
656 dprintf(INFO, "Failed to write protect dev info\n");
657 ASSERT(0);
658 }
659#endif
660
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700661 /* Perform target specific cleanup */
662 target_uninit();
663
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800664 /* Turn off splash screen if enabled */
665#if DISPLAY_SPLASH_SCREEN
666 target_display_shutdown();
667#endif
668
669
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800670 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530671 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800672
673 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700674
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +0530675 /* Initialise wdog to catch early kernel crashes */
676#if WDOG_SUPPORT
677 msm_wdog_init();
678#endif
Amol Jadi4421e652011-06-16 15:00:48 -0700679 /* do any platform specific cleanup before kernel entry */
680 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700681
Brian Swetland9c4c0752009-01-25 16:23:50 -0800682 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700683
Amol Jadi504f9fe2012-08-16 13:56:48 -0700684#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800685 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700686#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700687 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800688
689 if (IS_ARM64(kptr))
690 /* Jump to a 64bit kernel */
691 scm_elexec_call((paddr_t)kernel, tags_phys);
692 else
693 /* Jump to a 32bit kernel */
694 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800695}
696
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700697/* Function to check if the memory address range falls within the aboot
698 * boundaries.
699 * start: Start of the memory region
700 * size: Size of the memory region
701 */
702int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
703{
704 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800705 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700706 return -1;
707
708 /* Check for memory overlap. */
709 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
710 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700711 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700712 return 0;
713 else
714 return -1;
715}
716
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800717#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800718
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800719BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800720#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800721BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800722#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800723
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700724static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
725{
726 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800727
728#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800729#if IMAGE_VERIF_ALGO_SHA1
730 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
731#else
732 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
733#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800734#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700735
736 /* Assume device is rooted at this time. */
737 device.is_tampered = 1;
738
739 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
740
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700741#if VERIFIED_BOOT
742 if(boot_into_recovery)
743 {
744 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530745 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700746 }
747 else
748 {
749 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530750 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700751 }
752 boot_verify_print_state();
753#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700754 ret = image_verify((unsigned char *)bootimg_addr,
755 (unsigned char *)(bootimg_addr + bootimg_size),
756 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800757 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700758#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700759 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
760
761 if (ret)
762 {
763 /* Authorized kernel */
764 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700765 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700766 }
767
Amit Blay4aa292f2015-04-28 21:55:59 +0300768#ifdef MDTP_SUPPORT
769 {
770 /* Verify MDTP lock.
771 * For boot & recovery partitions, use aboot's verification result.
772 */
773 mdtp_ext_partition_verification_t ext_partition;
774 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
775 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
776 ext_partition.page_size = 0; /* Not needed since already validated */
777 ext_partition.image_addr = 0; /* Not needed since already validated */
778 ext_partition.image_size = 0; /* Not needed since already validated */
779 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
780 mdtp_fwlock_verify_lock(&ext_partition);
781 }
782#endif /* MDTP_SUPPORT */
783
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700784#if USE_PCOM_SECBOOT
785 set_tamper_flag(device.is_tampered);
786#endif
787
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700788#if VERIFIED_BOOT
789 if(boot_verify_get_state() == RED)
790 {
791 if(!boot_into_recovery)
792 {
793 dprintf(CRITICAL,
794 "Device verification failed. Rebooting into recovery.\n");
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +0530795 mdelay(1000);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700796 reboot_device(RECOVERY_MODE);
797 }
798 else
799 {
800 dprintf(CRITICAL,
801 "Recovery image verification failed. Asserting..\n");
802 ASSERT(0);
803 }
804 }
805#endif
Unnati Gandhi1be04752015-03-27 19:41:53 +0530806
807 if(device.is_tampered)
808 {
809 write_device_info_mmc(&device);
810 #ifdef TZ_TAMPER_FUSE
811 set_tamper_fuse_cmd();
812 #endif
813 #ifdef ASSERT_ON_TAMPER
814 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
815 ASSERT(0);
816 #endif
817 }
818
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700819}
820
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530821static bool check_format_bit()
822{
823 bool ret = false;
824 int index;
825 uint64_t offset;
826 struct boot_selection_info *in = NULL;
827 char *buf = NULL;
828
829 index = partition_get_index("bootselect");
830 if (index == INVALID_PTN)
831 {
832 dprintf(INFO, "Unable to locate /bootselect partition\n");
833 return ret;
834 }
835 offset = partition_get_offset(index);
836 if(!offset)
837 {
838 dprintf(INFO, "partition /bootselect doesn't exist\n");
839 return ret;
840 }
841 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
842 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530843 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530844 {
845 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
846 free(buf);
847 return ret;
848 }
849 in = (struct boot_selection_info *) buf;
850 if ((in->signature == BOOTSELECT_SIGNATURE) &&
851 (in->version == BOOTSELECT_VERSION)) {
852 if ((in->state_info & BOOTSELECT_FORMAT) &&
853 !(in->state_info & BOOTSELECT_FACTORY))
854 ret = true;
855 } else {
856 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
857 in->signature, in->version);
858 ASSERT(0);
859 }
860 free(buf);
861 return ret;
862}
863
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700864void boot_verifier_init()
865{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700866 uint32_t boot_state;
867 /* Check if device unlock */
868 if(device.is_unlocked)
869 {
870 boot_verify_send_event(DEV_UNLOCK);
871 boot_verify_print_state();
872 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
873 return;
874 }
875 else
876 {
877 boot_verify_send_event(BOOT_INIT);
878 }
879
880 /* Initialize keystore */
881 boot_state = boot_verify_keystore_init();
882 if(boot_state == YELLOW)
883 {
884 boot_verify_print_state();
885 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
886 }
887}
888
Shashank Mittal23b8f422010-04-16 19:27:21 -0700889int boot_linux_from_mmc(void)
890{
891 struct boot_img_hdr *hdr = (void*) buf;
892 struct boot_img_hdr *uhdr;
893 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700894 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700895 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700896 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700897
Shashank Mittalcd98d472011-08-02 14:29:24 -0700898 unsigned char *image_addr = 0;
899 unsigned kernel_actual;
900 unsigned ramdisk_actual;
901 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700902 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700903
Matthew Qin271927e2015-03-31 22:07:07 -0400904 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +0800905 unsigned int out_len = 0;
906 unsigned int out_avai_len = 0;
907 unsigned char *out_addr = NULL;
908 uint32_t dtb_offset = 0;
909 unsigned char *kernel_start_addr = NULL;
910 unsigned int kernel_size = 0;
911 int rc;
912
Neeti Desai465491e2012-07-31 12:53:35 -0700913#if DEVICE_TREE
914 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700915 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700916 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800917 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700918 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -0400919 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -0700920#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +0800921 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800922
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530923 if (check_format_bit())
924 boot_into_recovery = 1;
925
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700926 if (!boot_into_recovery) {
927 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
928 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
929 if (rcode <= 0) {
930 boot_into_ffbm = false;
931 if (rcode < 0)
932 dprintf(CRITICAL,"failed to get ffbm cookie");
933 } else
934 boot_into_ffbm = true;
935 } else
936 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700937 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
938 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
939 dprintf(INFO, "Unified boot method!\n");
940 hdr = uhdr;
941 goto unified_boot;
942 }
Greg Griscod6250552011-06-29 14:40:23 -0700943 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700944 index = partition_get_index("boot");
945 ptn = partition_get_offset(index);
946 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700947 dprintf(CRITICAL, "ERROR: No boot partition found\n");
948 return -1;
949 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700950 }
951 else {
952 index = partition_get_index("recovery");
953 ptn = partition_get_offset(index);
954 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700955 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
956 return -1;
957 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700958 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -0800959 /* Set Lun for boot & recovery partitions */
960 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -0700961
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530962 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700963 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
964 return -1;
965 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700966
967 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700968 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700969 return -1;
970 }
971
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700972 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530973
974 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
975 dprintf(CRITICAL, "ERROR: Invalid page size\n");
976 return -1;
977 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700978 page_size = hdr->page_size;
979 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700980 }
981
Matthew Qin49e51fa2015-02-09 10:40:45 +0800982 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
983 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800984
Matthew Qin49e51fa2015-02-09 10:40:45 +0800985 image_addr = (unsigned char *)target_get_scratch_address();
986
987#if DEVICE_TREE
988 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
989 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
990#else
991 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
992#endif
993
994#if VERIFIED_BOOT
995 boot_verifier_init();
996#endif
997
998 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
999 {
1000 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1001 return -1;
1002 }
1003
Matthew Qinbb7923d2015-02-09 10:56:09 +08001004 /*
1005 * Update loading flow of bootimage to support compressed/uncompressed
1006 * bootimage on both 64bit and 32bit platform.
1007 * 1. Load bootimage from emmc partition onto DDR.
1008 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1009 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1010 * platform accordingly.
1011 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1012 */
1013
Matthew Qin49e51fa2015-02-09 10:40:45 +08001014 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
1015 bs_set_timestamp(BS_KERNEL_LOAD_START);
1016
Matthew Qinbb7923d2015-02-09 10:56:09 +08001017 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001018 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1019 {
1020 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1021 return -1;
1022 }
1023
1024 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
1025 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1026
1027 /* Authenticate Kernel */
1028 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1029 (int) target_use_signed_kernel(),
1030 device.is_unlocked,
1031 device.is_tampered);
1032
1033 if(target_use_signed_kernel() && (!device.is_unlocked))
1034 {
1035 offset = imagesize_actual;
1036 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1037 {
1038 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1039 return -1;
1040 }
1041
1042 /* Read signature */
1043 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1044 {
1045 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1046 return -1;
1047 }
1048
1049 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
1050 } else {
1051 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1052 #ifdef TZ_SAVE_KERNEL_HASH
1053 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1054 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001055
1056#ifdef MDTP_SUPPORT
1057 {
1058 /* Verify MDTP lock.
1059 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1060 * since verification was skipped in aboot. The signature is not part of the loaded image.
1061 */
1062 mdtp_ext_partition_verification_t ext_partition;
1063 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1064 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1065 ext_partition.page_size = page_size;
1066 ext_partition.image_addr = (uint32)image_addr;
1067 ext_partition.image_size = imagesize_actual;
1068 ext_partition.sig_avail = FALSE;
1069 mdtp_fwlock_verify_lock(&ext_partition);
1070 }
1071#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001072 }
1073
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001074 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001075 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1076 * If not, continue booting.
1077 */
1078 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1079 {
1080 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1081 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001082 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001083 rc = decompress((unsigned char *)(image_addr + page_size),
1084 hdr->kernel_size, out_addr, out_avai_len,
1085 &dtb_offset, &out_len);
1086 if (rc)
1087 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001088 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001089 ASSERT(0);
1090 }
1091
Matthew Qin0b15b322015-05-19 05:20:54 -04001092 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001093 kptr = (struct kernel64_hdr *)out_addr;
1094 kernel_start_addr = out_addr;
1095 kernel_size = out_len;
1096 } else {
1097 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1098 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1099 kernel_size = hdr->kernel_size;
1100 }
1101
1102 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001103 * Update the kernel/ramdisk/tags address if the boot image header
1104 * has default values, these default values come from mkbootimg when
1105 * the boot image is flashed using fastboot flash:raw
1106 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001107 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001108
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001109 /* Get virtual addresses since the hdr saves physical addresses. */
1110 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1111 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1112 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001113
Matthew Qinbb7923d2015-02-09 10:56:09 +08001114 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001115 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001116 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001117 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1118 {
1119 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1120 return -1;
1121 }
1122
1123#ifndef DEVICE_TREE
1124 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1125 {
1126 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1127 return -1;
1128 }
1129#endif
1130
Matthew Qin49e51fa2015-02-09 10:40:45 +08001131 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001132 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001133 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001134
Matthew Qin49e51fa2015-02-09 10:40:45 +08001135 #if DEVICE_TREE
1136 if(hdr->dt_size) {
1137 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1138 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001139
Matthew Qin49e51fa2015-02-09 10:40:45 +08001140 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1141 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1142 return -1;
1143 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001144
Matthew Qin49e51fa2015-02-09 10:40:45 +08001145 /* Find index of device tree within device tree table */
1146 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1147 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1148 return -1;
1149 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001150
Matthew Qin271927e2015-03-31 22:07:07 -04001151 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1152 {
1153 unsigned int compressed_size = 0;
1154 out_addr += out_len;
1155 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001156 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001157 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1158 dt_entry.size, out_addr, out_avai_len,
1159 &compressed_size, &dtb_size);
1160 if (rc)
1161 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001162 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001163 ASSERT(0);
1164 }
1165
Matthew Qin0b15b322015-05-19 05:20:54 -04001166 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001167 best_match_dt_addr = out_addr;
1168 } else {
1169 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1170 dtb_size = dt_entry.size;
1171 }
1172
Matthew Qin49e51fa2015-02-09 10:40:45 +08001173 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001174 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001175 {
1176 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1177 return -1;
1178 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001179
Matthew Qin271927e2015-03-31 22:07:07 -04001180 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001181 } else {
1182 /* Validate the tags_addr */
1183 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001184 {
1185 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1186 return -1;
1187 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001188 /*
1189 * If appended dev tree is found, update the atags with
1190 * memory address to the DTB appended location on RAM.
1191 * Else update with the atags address in the kernel header
1192 */
1193 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001194 dtb = dev_tree_appended((void*)(image_addr + page_size),
1195 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001196 (void *)hdr->tags_addr);
1197 if (!dtb) {
1198 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001199 return -1;
1200 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001201 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001202 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001203
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001204 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1205 target_load_ssd_keystore();
1206
Shashank Mittal23b8f422010-04-16 19:27:21 -07001207unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001208
Dima Zavin77e41f32013-03-06 16:10:43 -08001209 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001210 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001211 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1212
1213 return 0;
1214}
1215
Dima Zavin214cc642009-01-26 11:16:21 -08001216int boot_linux_from_flash(void)
1217{
1218 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001219 struct ptentry *ptn;
1220 struct ptable *ptable;
1221 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001222
Shashank Mittalcd98d472011-08-02 14:29:24 -07001223 unsigned char *image_addr = 0;
1224 unsigned kernel_actual;
1225 unsigned ramdisk_actual;
1226 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001227 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001228
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001229#if DEVICE_TREE
1230 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001231 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001232 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001233 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001234#endif
1235
David Ng183a7422009-12-07 14:55:21 -08001236 if (target_is_emmc_boot()) {
1237 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1238 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1239 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1240 return -1;
1241 }
1242 goto continue_boot;
1243 }
1244
Dima Zavin214cc642009-01-26 11:16:21 -08001245 ptable = flash_get_ptable();
1246 if (ptable == NULL) {
1247 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1248 return -1;
1249 }
1250
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001251 if(!boot_into_recovery)
1252 {
1253 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001254
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001255 if (ptn == NULL) {
1256 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1257 return -1;
1258 }
1259 }
1260 else
1261 {
1262 ptn = ptable_find(ptable, "recovery");
1263 if (ptn == NULL) {
1264 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1265 return -1;
1266 }
Dima Zavin214cc642009-01-26 11:16:21 -08001267 }
1268
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001269 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001270 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1271 return -1;
1272 }
Dima Zavin214cc642009-01-26 11:16:21 -08001273
1274 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001275 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001276 return -1;
1277 }
1278
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001279 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001280 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 -08001281 return -1;
1282 }
1283
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001284 /*
1285 * Update the kernel/ramdisk/tags address if the boot image header
1286 * has default values, these default values come from mkbootimg when
1287 * the boot image is flashed using fastboot flash:raw
1288 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001289 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001290
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001291 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001292 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1293 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1294 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1295
1296 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1297 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1298
1299 /* Check if the addresses in the header are valid. */
1300 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1301 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1302 {
1303 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1304 return -1;
1305 }
1306
1307#ifndef DEVICE_TREE
1308 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1309 {
1310 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1311 return -1;
1312 }
1313#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001314
Shashank Mittalcd98d472011-08-02 14:29:24 -07001315 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001316 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001317 {
1318 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001319 offset = 0;
1320
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001321#if DEVICE_TREE
1322 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1323 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001324
1325 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1326 {
1327 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1328 return -1;
1329 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001330#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001331 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001332#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001333
Amol Jadib6be5c12012-11-14 13:39:51 -08001334 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001335 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001336
Shashank Mittalcd98d472011-08-02 14:29:24 -07001337 /* Read image without signature */
1338 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1339 {
1340 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1341 return -1;
1342 }
Dima Zavin214cc642009-01-26 11:16:21 -08001343
Amol Jadib6be5c12012-11-14 13:39:51 -08001344 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001345 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001346
Shashank Mittalcd98d472011-08-02 14:29:24 -07001347 offset = imagesize_actual;
1348 /* Read signature */
1349 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1350 {
1351 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001352 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001353 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001354
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301355 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001356
1357 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001358 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1359 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001360#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001361 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001362 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001363 {
1364 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1365 return -1;
1366 }
1367
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001368 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1369#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001370
1371 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001372 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001373 {
1374 write_device_info_flash(&device);
1375 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301376#if USE_PCOM_SECBOOT
1377 set_tamper_flag(device.is_tampered);
1378#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001379 }
1380 else
1381 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001382 offset = page_size;
1383
Amol Jadib6be5c12012-11-14 13:39:51 -08001384 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1385 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1386 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1387
1388 dprintf(INFO, "Loading boot image (%d): start\n",
1389 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001390 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001391
1392 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001393 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1394 return -1;
1395 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001396 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001397
Amol Jadib6be5c12012-11-14 13:39:51 -08001398 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001399 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1400 return -1;
1401 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001402 offset += ramdisk_actual;
1403
1404 dprintf(INFO, "Loading boot image (%d): done\n",
1405 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001406 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001407
1408 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001409 offset += second_actual;
1410 /* Second image loading not implemented. */
1411 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001412 }
1413
1414#if DEVICE_TREE
1415 if(hdr->dt_size != 0) {
1416
1417 /* Read the device tree table into buffer */
1418 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1419 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1420 return -1;
1421 }
1422
1423 table = (struct dt_table*) dt_buf;
1424
Deepa Dinamani19648b42013-09-05 17:05:55 -07001425 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001426 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1427 return -1;
1428 }
1429
Deepa Dinamani19648b42013-09-05 17:05:55 -07001430 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1431 if (!table)
1432 return -1;
1433
1434 /* Read the entire device tree table into buffer */
1435 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1436 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1437 return -1;
1438 }
1439
1440
Joel Kingaa335dc2013-06-03 16:11:08 -07001441 /* Find index of device tree within device tree table */
1442 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001443 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1444 return -1;
1445 }
1446
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001447 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001448 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001449 {
1450 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1451 return -1;
1452 }
1453
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001454 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001455 if(flash_read(ptn, offset + dt_entry.offset,
1456 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001457 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1458 return -1;
1459 }
1460 }
1461#endif
1462
Shashank Mittalcd98d472011-08-02 14:29:24 -07001463 }
David Ng183a7422009-12-07 14:55:21 -08001464continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001465
Dima Zavin214cc642009-01-26 11:16:21 -08001466 /* TODO: create/pass atags to kernel */
1467
Ajay Dudanie28a6072011-07-01 13:59:46 -07001468 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001469 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001470 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1471
1472 return 0;
1473}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001474
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001475BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001476void write_device_info_mmc(device_info *dev)
1477{
Shashank Mittal162244e2011-08-08 19:01:25 -07001478 unsigned long long ptn = 0;
1479 unsigned long long size;
1480 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001481 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001482 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001483 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001484
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001485 if (devinfo_present)
1486 index = partition_get_index("devinfo");
1487 else
1488 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001489
Shashank Mittal162244e2011-08-08 19:01:25 -07001490 ptn = partition_get_offset(index);
1491 if(ptn == 0)
1492 {
1493 return;
1494 }
1495
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001496 lun = partition_get_lun(index);
1497 mmc_set_lun(lun);
1498
Shashank Mittal162244e2011-08-08 19:01:25 -07001499 size = partition_get_size(index);
1500
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001501 blocksize = mmc_get_device_blocksize();
1502
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001503 if (devinfo_present)
1504 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1505 else
1506 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1507 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001508 {
1509 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1510 return;
1511 }
1512}
1513
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001514void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001515{
Shashank Mittal162244e2011-08-08 19:01:25 -07001516 unsigned long long ptn = 0;
1517 unsigned long long size;
1518 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001519 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001520 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001521
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001522 if ((index = partition_get_index("devinfo")) < 0)
1523 {
1524 devinfo_present = false;
1525 index = partition_get_index("aboot");
1526 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001527
Shashank Mittal162244e2011-08-08 19:01:25 -07001528 ptn = partition_get_offset(index);
1529 if(ptn == 0)
1530 {
1531 return;
1532 }
1533
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001534 mmc_set_lun(partition_get_lun(index));
1535
Shashank Mittal162244e2011-08-08 19:01:25 -07001536 size = partition_get_size(index);
1537
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001538 blocksize = mmc_get_device_blocksize();
1539
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001540 if (devinfo_present)
1541 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1542 else
1543 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1544 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001545 {
1546 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1547 return;
1548 }
1549
Shashank Mittal162244e2011-08-08 19:01:25 -07001550}
1551
1552void write_device_info_flash(device_info *dev)
1553{
1554 struct device_info *info = (void *) info_buf;
1555 struct ptentry *ptn;
1556 struct ptable *ptable;
1557
1558 ptable = flash_get_ptable();
1559 if (ptable == NULL)
1560 {
1561 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1562 return;
1563 }
1564
1565 ptn = ptable_find(ptable, "devinfo");
1566 if (ptn == NULL)
1567 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001568 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001569 return;
1570 }
1571
1572 memcpy(info, dev, sizeof(device_info));
1573
1574 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1575 {
1576 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1577 return;
1578 }
1579}
1580
vijay kumarc65876c2015-04-24 13:29:16 +05301581static int read_allow_oem_unlock(device_info *dev)
1582{
1583 const char *ptn_name = "frp";
1584 unsigned offset;
1585 int index;
1586 unsigned long long ptn;
1587 unsigned long long ptn_size;
1588 unsigned blocksize = mmc_get_device_blocksize();
1589 char buf[blocksize];
1590
1591 index = partition_get_index(ptn_name);
1592 if (index == INVALID_PTN)
1593 {
1594 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
1595 return -1;
1596 }
1597
1598 ptn = partition_get_offset(index);
1599 ptn_size = partition_get_size(index);
1600 offset = ptn_size - blocksize;
1601
1602 if (mmc_read(ptn + offset, (void *)buf, sizeof(buf)))
1603 {
1604 dprintf(CRITICAL, "Reading MMC failed\n");
1605 return -1;
1606 }
1607
1608 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1609 is_allow_unlock = buf[blocksize-1] & 0x01;
1610 return 0;
1611}
1612
1613static int write_allow_oem_unlock(bool allow_unlock)
1614{
1615 const char *ptn_name = "frp";
1616 unsigned offset;
1617
1618 int index;
1619 unsigned long long ptn;
1620 unsigned long long ptn_size;
1621 unsigned blocksize = mmc_get_device_blocksize();
1622 char buf[blocksize];
1623
1624 index = partition_get_index(ptn_name);
1625 if (index == INVALID_PTN)
1626 {
1627 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
1628 return -1;
1629 }
1630
1631 ptn = partition_get_offset(index);
1632 ptn_size = partition_get_size(index);
1633 offset = ptn_size - blocksize;
1634
1635 if (mmc_read(ptn + offset, (void *)buf, sizeof(buf)))
1636 {
1637 dprintf(CRITICAL, "Reading MMC failed\n");
1638 return -1;
1639 }
1640
1641 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1642 buf[blocksize-1] = allow_unlock;
1643 if (mmc_write(ptn + offset, blocksize, buf))
1644 {
1645 dprintf(CRITICAL, "Writing MMC failed\n");
1646 return -1;
1647 }
1648
1649 return 0;
1650}
1651
Shashank Mittal162244e2011-08-08 19:01:25 -07001652void read_device_info_flash(device_info *dev)
1653{
1654 struct device_info *info = (void*) info_buf;
1655 struct ptentry *ptn;
1656 struct ptable *ptable;
1657
1658 ptable = flash_get_ptable();
1659 if (ptable == NULL)
1660 {
1661 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1662 return;
1663 }
1664
1665 ptn = ptable_find(ptable, "devinfo");
1666 if (ptn == NULL)
1667 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001668 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001669 return;
1670 }
1671
1672 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1673 {
1674 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1675 return;
1676 }
1677
1678 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1679 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001680 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1681 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001682 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001683 write_device_info_flash(info);
1684 }
1685 memcpy(dev, info, sizeof(device_info));
1686}
1687
1688void write_device_info(device_info *dev)
1689{
1690 if(target_is_emmc_boot())
1691 {
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001692 struct device_info *info = (void*) info_buf;
1693 memcpy(info, dev, sizeof(struct device_info));
1694
1695#if USE_RPMB_FOR_DEVINFO
1696 if (is_secure_boot_enable())
1697 write_device_info_rpmb((void*) info, mmc_get_device_blocksize());
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001698 else
1699 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001700#else
1701 write_device_info_mmc(info);
1702#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001703 }
1704 else
1705 {
1706 write_device_info_flash(dev);
1707 }
1708}
1709
1710void read_device_info(device_info *dev)
1711{
1712 if(target_is_emmc_boot())
1713 {
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001714 struct device_info *info = (void*) info_buf;
1715
1716#if USE_RPMB_FOR_DEVINFO
1717 if (is_secure_boot_enable())
1718 read_device_info_rpmb((void*) info, mmc_get_device_blocksize());
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001719 else
1720 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001721#else
1722 read_device_info_mmc(info);
1723#endif
1724
1725 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1726 {
1727 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
Channagoud Kadabi35297672015-06-13 11:09:49 -07001728 info->is_unlocked = 0;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001729 info->is_verified = 0;
1730 info->is_tampered = 0;
Unnati Gandhia49daa12015-03-23 18:08:38 +05301731#if USER_BUILD_VARIANT
1732 info->charger_screen_enabled = 1;
1733#else
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001734 info->charger_screen_enabled = 0;
Unnati Gandhia49daa12015-03-23 18:08:38 +05301735#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001736 write_device_info(info);
1737 }
1738 memcpy(dev, info, sizeof(device_info));
Shashank Mittal162244e2011-08-08 19:01:25 -07001739 }
1740 else
1741 {
1742 read_device_info_flash(dev);
1743 }
1744}
1745
1746void reset_device_info()
1747{
1748 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001749 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001750 write_device_info(&device);
1751}
1752
1753void set_device_root()
1754{
1755 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001756 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001757 write_device_info(&device);
1758}
1759
Amol Jadicb524072012-08-09 16:40:18 -07001760#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001761int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
Amol Jadicb524072012-08-09 16:40:18 -07001762{
1763 uint32 dt_image_offset = 0;
1764 uint32_t n;
1765 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001766 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001767 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001768 unsigned int compressed_size = 0;
1769 unsigned int dtb_size = 0;
1770 unsigned int out_avai_len = 0;
1771 unsigned char *out_addr = NULL;
1772 unsigned char *best_match_dt_addr = NULL;
1773 int rc;
Amol Jadicb524072012-08-09 16:40:18 -07001774
1775 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1776
Amol Jadicb524072012-08-09 16:40:18 -07001777 if(hdr->dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001778 /* add kernel offset */
1779 dt_image_offset += page_size;
1780 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1781 dt_image_offset += n;
1782
1783 /* add ramdisk offset */
1784 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1785 dt_image_offset += n;
1786
1787 /* add second offset */
1788 if(hdr->second_size != 0) {
1789 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1790 dt_image_offset += n;
1791 }
1792
1793 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001794 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001795
Deepa Dinamani19648b42013-09-05 17:05:55 -07001796 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001797 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1798 return -1;
1799 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001800 /* Find index of device tree within device tree table */
1801 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001802 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1803 return -1;
1804 }
1805
Matthew Qin271927e2015-03-31 22:07:07 -04001806 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
1807 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
1808 {
1809 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
1810 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04001811 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001812 rc = decompress(best_match_dt_addr,
1813 dt_entry.size, out_addr, out_avai_len,
1814 &compressed_size, &dtb_size);
1815 if (rc)
1816 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001817 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001818 ASSERT(0);
1819 }
1820
Matthew Qin0b15b322015-05-19 05:20:54 -04001821 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001822 best_match_dt_addr = out_addr;
1823 } else {
1824 dtb_size = dt_entry.size;
1825 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001826 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001827 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001828 {
1829 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1830 return -1;
1831 }
1832
Amol Jadicb524072012-08-09 16:40:18 -07001833 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001834 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001835 } else
1836 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001837
1838 /* Everything looks fine. Return success. */
1839 return 0;
1840}
1841#endif
1842
Brian Swetland9c4c0752009-01-25 16:23:50 -08001843void cmd_boot(const char *arg, void *data, unsigned sz)
1844{
1845 unsigned kernel_actual;
1846 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001847 uint32_t image_actual;
1848 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001849 uint32_t sig_actual = SIGNATURE_SIZE;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001850 struct boot_img_hdr *hdr = NULL;
1851 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001852 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001853 int ret = 0;
1854 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001855 unsigned int out_len = 0;
1856 unsigned int out_avai_len = 0;
1857 unsigned char *out_addr = NULL;
1858 uint32_t dtb_offset = 0;
1859 unsigned char *kernel_start_addr = NULL;
1860 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04001861 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001862
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001863#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07001864 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001865 {
1866 fastboot_fail("unlock device to use this command");
1867 return;
1868 }
1869#endif
1870
Brian Swetland9c4c0752009-01-25 16:23:50 -08001871 if (sz < sizeof(hdr)) {
1872 fastboot_fail("invalid bootimage header");
1873 return;
1874 }
1875
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001876 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001877
1878 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001879 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001880
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001881 if(target_is_emmc_boot() && hdr->page_size) {
1882 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001883 page_mask = page_size - 1;
1884 }
1885
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001886 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1887 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001888#if DEVICE_TREE
1889 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1890#endif
1891
1892 image_actual = ADD_OF(page_size, kernel_actual);
1893 image_actual = ADD_OF(image_actual, ramdisk_actual);
1894 image_actual = ADD_OF(image_actual, dt_actual);
1895
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001896 if (target_use_signed_kernel() && (!device.is_unlocked))
1897 image_actual = ADD_OF(image_actual, sig_actual);
1898
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001899 /* sz should have atleast raw boot image */
1900 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001901 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001902 return;
1903 }
1904
1905 /* Verify the boot image
1906 * device & page_size are initialized in aboot_init
1907 */
1908 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001909 /* Pass size excluding signature size, otherwise we would try to
1910 * access signature beyond its length
1911 */
1912 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001913
Amit Blay4aa292f2015-04-28 21:55:59 +03001914#ifdef MDTP_SUPPORT
1915 else
1916 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03001917 /* fastboot boot is not allowed when MDTP is activated */
1918
Amit Blay4aa292f2015-04-28 21:55:59 +03001919 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 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03001928
1929 bool is_mdtp_activated = 0;
1930 mdtp_activated(&is_mdtp_activated);
1931 if(is_mdtp_activated){
1932 dprintf(CRITICAL, "fastboot boot command is not available.\n");
1933 return;
1934 }
Amit Blay4aa292f2015-04-28 21:55:59 +03001935#endif /* MDTP_SUPPORT */
1936
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001937 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001938 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1939 * If not, continue booting.
1940 */
1941 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
1942 {
1943 out_addr = (unsigned char *)target_get_scratch_address();
1944 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
1945 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001946 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001947 ret = decompress((unsigned char *)(ptr + page_size),
1948 hdr->kernel_size, out_addr, out_avai_len,
1949 &dtb_offset, &out_len);
1950 if (ret)
1951 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001952 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001953 ASSERT(0);
1954 }
1955
Matthew Qin0b15b322015-05-19 05:20:54 -04001956 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001957 kptr = (struct kernel64_hdr *)out_addr;
1958 kernel_start_addr = out_addr;
1959 kernel_size = out_len;
1960 } else {
1961 kptr = (struct kernel64_hdr*)((char *)data + page_size);
1962 kernel_start_addr = (unsigned char *)((char *)data + page_size);
1963 kernel_size = hdr->kernel_size;
1964 }
1965
1966 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001967 * Update the kernel/ramdisk/tags address if the boot image header
1968 * has default values, these default values come from mkbootimg when
1969 * the boot image is flashed using fastboot flash:raw
1970 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001971 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001972
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001973 /* Get virtual addresses since the hdr saves physical addresses. */
1974 hdr->kernel_addr = VA(hdr->kernel_addr);
1975 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1976 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001977
Matthew Qinbb7923d2015-02-09 10:56:09 +08001978 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001979 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001980 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001981 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1982 {
1983 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001984 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001985 }
1986
Amol Jadicb524072012-08-09 16:40:18 -07001987#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001988 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07001989 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04001990 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001991
1992 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001993#else
1994 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1995 {
1996 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001997 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001998 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001999#endif
2000
2001 /* Load ramdisk & kernel */
2002 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002003 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002004
2005#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002006 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2007 {
2008 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2009 return;
2010 }
2011
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002012 /*
2013 * If dtb is not found look for appended DTB in the kernel.
2014 * If appended dev tree is found, update the atags with
2015 * memory address to the DTB appended location on RAM.
2016 * Else update with the atags address in the kernel header
2017 */
2018 if (!dtb_copied) {
2019 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002020 dtb = dev_tree_appended((void*)(ptr + page_size),
2021 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002022 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002023 if (!dtb) {
2024 fastboot_fail("dtb not found");
2025 return;
2026 }
Amol Jadicb524072012-08-09 16:40:18 -07002027 }
2028#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002029
2030 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002031 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002032
Dima Zavin77e41f32013-03-06 16:10:43 -08002033 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002034 (const char*) hdr->cmdline, board_machtype(),
2035 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002036}
2037
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002038void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002039{
2040 struct ptentry *ptn;
2041 struct ptable *ptable;
2042
2043 ptable = flash_get_ptable();
2044 if (ptable == NULL) {
2045 fastboot_fail("partition table doesn't exist");
2046 return;
2047 }
2048
2049 ptn = ptable_find(ptable, arg);
2050 if (ptn == NULL) {
2051 fastboot_fail("unknown partition name");
2052 return;
2053 }
2054
2055 if (flash_erase(ptn)) {
2056 fastboot_fail("failed to erase partition");
2057 return;
2058 }
2059 fastboot_okay("");
2060}
2061
Bikas Gurungd48bd242010-09-04 19:54:32 -07002062
2063void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2064{
2065 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002066 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002067 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002068 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002069
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002070#if VERIFIED_BOOT
2071 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2072 {
2073 if(!device.is_unlocked)
2074 {
2075 fastboot_fail("unlock device to erase keystore");
2076 return;
2077 }
2078 }
2079#endif
2080
Kinson Chikf1a43512011-07-14 11:28:39 -07002081 index = partition_get_index(arg);
2082 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002083 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002084
Kinson Chikf1a43512011-07-14 11:28:39 -07002085 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002086 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002087 return;
2088 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002089
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002090 lun = partition_get_lun(index);
2091 mmc_set_lun(lun);
2092
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002093 if (platform_boot_dev_isemmc())
2094 {
2095 if (mmc_erase_card(ptn, size)) {
2096 fastboot_fail("failed to erase partition\n");
2097 return;
2098 }
2099 } else {
2100 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2101 size = partition_get_size(index);
2102 if (size > DEFAULT_ERASE_SIZE)
2103 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002104
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002105 /* Simple inefficient version of erase. Just writing
2106 0 in first several blocks */
2107 if (mmc_write(ptn , size, (unsigned int *)out)) {
2108 fastboot_fail("failed to erase partition");
2109 return;
2110 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002111 }
2112 fastboot_okay("");
2113}
2114
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002115void cmd_erase(const char *arg, void *data, unsigned sz)
2116{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002117#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002118 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002119 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002120 if(!device.is_unlocked && !device.is_verified)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002121 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002122 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002123 return;
2124 }
Channagoud Kadabi35297672015-06-13 11:09:49 -07002125 if(!device.is_unlocked && device.is_verified)
2126 {
2127 if(!boot_verify_flash_allowed(arg))
2128 {
2129 fastboot_fail("cannot flash this partition in verified state");
2130 return;
2131 }
2132 }
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002133 }
2134#endif
2135
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002136 if(target_is_emmc_boot())
2137 cmd_erase_mmc(arg, data, sz);
2138 else
2139 cmd_erase_nand(arg, data, sz);
2140}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002141
Ajay Dudani5c761132011-04-07 20:19:04 -07002142void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002143{
2144 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002145 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002146 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002147 char *token = NULL;
2148 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002149 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002150 uint8_t lun = 0;
2151 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002152
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002153 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002154 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002155 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002156 if(token)
2157 {
2158 lun = atoi(token);
2159 mmc_set_lun(lun);
2160 lun_set = true;
2161 }
2162
Mao Jinlong226f33a2014-07-04 17:24:10 +08002163 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002164 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002165 if (!strcmp(pname, "partition"))
2166 {
2167 dprintf(INFO, "Attempt to write partition image.\n");
2168 if (write_partition(sz, (unsigned char *) data)) {
2169 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002170 return;
2171 }
2172 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002173 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002174 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002175#if VERIFIED_BOOT
2176 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2177 {
2178 if(!device.is_unlocked)
2179 {
2180 fastboot_fail("unlock device to flash keystore");
2181 return;
2182 }
2183 if(!boot_verify_validate_keystore((unsigned char *)data))
2184 {
2185 fastboot_fail("image is not a keystore file");
2186 return;
2187 }
2188 }
2189#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002190 index = partition_get_index(pname);
2191 ptn = partition_get_offset(index);
2192 if(ptn == 0) {
2193 fastboot_fail("partition table doesn't exist");
2194 return;
2195 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002196
Mao Jinlong226f33a2014-07-04 17:24:10 +08002197 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2198 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2199 fastboot_fail("image is not a boot image");
2200 return;
2201 }
2202 }
2203
2204 if(!lun_set)
2205 {
2206 lun = partition_get_lun(index);
2207 mmc_set_lun(lun);
2208 }
2209
2210 size = partition_get_size(index);
2211 if (ROUND_TO_PAGE(sz,511) > size) {
2212 fastboot_fail("size too large");
2213 return;
2214 }
2215 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2216 fastboot_fail("flash write failure");
2217 return;
2218 }
Greg Grisco6e754772011-06-23 12:19:39 -07002219 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002220 }
2221 fastboot_okay("");
2222 return;
2223}
2224
Ajay Dudanide984792015-03-02 09:57:41 -08002225void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2226{
2227 int i, images;
2228 meta_header_t *meta_header;
2229 img_header_entry_t *img_header_entry;
2230
2231 meta_header = (meta_header_t*) data;
2232 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2233
2234 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2235
2236 for (i=0; i<images; i++) {
2237
2238 if((img_header_entry[i].ptn_name == NULL) ||
2239 (img_header_entry[i].start_offset == 0) ||
2240 (img_header_entry[i].size == 0))
2241 break;
2242
2243 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2244 (void *) data + img_header_entry[i].start_offset,
2245 img_header_entry[i].size);
2246 }
2247
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002248 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2249 {
2250 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2251 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2252 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2253 }
2254 else
2255 {
2256 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2257 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2258 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2259 }
2260
2261 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002262 fastboot_okay("");
2263 return;
2264}
2265
Ajay Dudani5c761132011-04-07 20:19:04 -07002266void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2267{
2268 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002269 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002270 uint32_t *fill_buf = NULL;
2271 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002272 sparse_header_t *sparse_header;
2273 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002274 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002275 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302276 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002277 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302278 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002279 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302280 /*End of the sparse image address*/
2281 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002282
Kinson Chikf1a43512011-07-14 11:28:39 -07002283 index = partition_get_index(arg);
2284 ptn = partition_get_offset(index);
2285 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002286 fastboot_fail("partition table doesn't exist");
2287 return;
2288 }
2289
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302290 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302291
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002292 lun = partition_get_lun(index);
2293 mmc_set_lun(lun);
2294
vijay kumar800255e2015-04-24 20:26:19 +05302295 if (sz < sizeof(sparse_header_t)) {
2296 fastboot_fail("size too low");
2297 return;
2298 }
2299
Ajay Dudani5c761132011-04-07 20:19:04 -07002300 /* Read and skip over sparse image header */
2301 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302302
vijay kumar1321f342015-03-27 12:13:42 +05302303 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002304 fastboot_fail("size too large");
2305 return;
2306 }
2307
vijay kumarde4fcf62015-04-23 13:05:49 +05302308 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302309
2310 if (data_end < (uint32_t)data) {
2311 fastboot_fail("buffer overreads occured due to invalid sparse header");
2312 return;
2313 }
2314
vijay kumarde4fcf62015-04-23 13:05:49 +05302315 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002316 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302317 fastboot_fail("sparse header size mismatch");
2318 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002319 }
2320
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002321 dprintf (SPEW, "=== Sparse Image Header ===\n");
2322 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2323 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2324 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2325 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2326 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2327 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2328 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2329 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002330
2331 /* Start processing chunks */
2332 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2333 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302334 /* Make sure the total image size does not exceed the partition size */
2335 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2336 fastboot_fail("size too large");
2337 return;
2338 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002339 /* Read and skip over chunk header */
2340 chunk_header = (chunk_header_t *) data;
2341 data += sizeof(chunk_header_t);
2342
vijay kumar800255e2015-04-24 20:26:19 +05302343 if (data_end < (uint32_t)data) {
2344 fastboot_fail("buffer overreads occured due to invalid sparse header");
2345 return;
2346 }
2347
Ajay Dudani5c761132011-04-07 20:19:04 -07002348 dprintf (SPEW, "=== Chunk Header ===\n");
2349 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2350 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2351 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2352
vijay kumar800255e2015-04-24 20:26:19 +05302353 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002354 {
vijay kumar800255e2015-04-24 20:26:19 +05302355 fastboot_fail("chunk header size mismatch");
2356 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002357 }
2358
wufeng.jiang813dc352015-06-02 23:02:46 -04002359 if (!sparse_header->blk_sz ){
2360 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302361 return;
2362 }
2363
wufeng.jiang813dc352015-06-02 23:02:46 -04002364 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2365
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302366 /* Make sure that the chunk size calculated from sparse image does not
2367 * exceed partition size
2368 */
2369 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2370 {
2371 fastboot_fail("Chunk data size exceeds partition size");
2372 return;
2373 }
2374
Ajay Dudani5c761132011-04-07 20:19:04 -07002375 switch (chunk_header->chunk_type)
2376 {
2377 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002378 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002379 chunk_data_sz))
2380 {
2381 fastboot_fail("Bogus chunk size for chunk type Raw");
2382 return;
2383 }
2384
vijay kumar800255e2015-04-24 20:26:19 +05302385 if (data_end < (uint32_t)data + chunk_data_sz) {
2386 fastboot_fail("buffer overreads occured due to invalid sparse header");
2387 return;
2388 }
2389
wufeng.jiang813dc352015-06-02 23:02:46 -04002390 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2391 otherwise it will return in the line above
2392 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002393 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002394 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002395 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002396 {
2397 fastboot_fail("flash write failure");
2398 return;
2399 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302400 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2401 fastboot_fail("Bogus size for RAW chunk type");
2402 return;
2403 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002404 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002405 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002406 break;
2407
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002408 case CHUNK_TYPE_FILL:
2409 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2410 sizeof(uint32_t)))
2411 {
2412 fastboot_fail("Bogus chunk size for chunk type FILL");
2413 return;
2414 }
2415
2416 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2417 if (!fill_buf)
2418 {
2419 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2420 return;
2421 }
2422
vijay kumar800255e2015-04-24 20:26:19 +05302423 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2424 fastboot_fail("buffer overreads occured due to invalid sparse header");
2425 return;
2426 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002427 fill_val = *(uint32_t *)data;
2428 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002429
2430 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2431 {
2432 fill_buf[i] = fill_val;
2433 }
2434
wufeng.jiang813dc352015-06-02 23:02:46 -04002435 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002436 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302437 /* Make sure that the data written to partition does not exceed partition size */
2438 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2439 {
2440 fastboot_fail("Chunk data size for fill type exceeds partition size");
2441 return;
2442 }
2443
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002444 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2445 sparse_header->blk_sz,
2446 fill_buf))
2447 {
2448 fastboot_fail("flash write failure");
2449 free(fill_buf);
2450 return;
2451 }
2452
2453 total_blocks++;
2454 }
2455
2456 free(fill_buf);
2457 break;
2458
Ajay Dudani5c761132011-04-07 20:19:04 -07002459 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302460 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2461 fastboot_fail("bogus size for chunk DONT CARE type");
2462 return;
2463 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002464 total_blocks += chunk_header->chunk_sz;
2465 break;
2466
Ajay Dudani5c761132011-04-07 20:19:04 -07002467 case CHUNK_TYPE_CRC:
2468 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2469 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002470 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002471 return;
2472 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302473 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2474 fastboot_fail("bogus size for chunk CRC type");
2475 return;
2476 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002477 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302478 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2479 fastboot_fail("integer overflow occured");
2480 return;
2481 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002482 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302483 if (data_end < (uint32_t)data) {
2484 fastboot_fail("buffer overreads occured due to invalid sparse header");
2485 return;
2486 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002487 break;
2488
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002489 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002490 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002491 fastboot_fail("Unknown chunk type");
2492 return;
2493 }
2494 }
2495
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002496 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2497 total_blocks, sparse_header->total_blks);
2498
2499 if(total_blocks != sparse_header->total_blks)
2500 {
2501 fastboot_fail("sparse image write failure");
2502 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002503
2504 fastboot_okay("");
2505 return;
2506}
2507
2508void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2509{
2510 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002511 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002512
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002513#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002514 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2515 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002516 int ret=0;
2517 uint32 major_version=0;
2518 uint32 minor_version=0;
2519
2520 ret = scm_svc_version(&major_version,&minor_version);
2521 if(!ret)
2522 {
2523 if(major_version >= 2)
2524 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002525 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002526 {
2527 ret = encrypt_scm((uint32 **) &data, &sz);
2528 if (ret != 0) {
2529 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2530 return;
2531 }
2532
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002533 /* Protect only for SSD */
2534 if (!strcmp(arg, "ssd")) {
2535 ret = scm_protect_keystore((uint32 *) data, sz);
2536 if (ret != 0) {
2537 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2538 return;
2539 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002540 }
2541 }
2542 else
2543 {
2544 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2545 if(ret != 0)
2546 {
2547 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2548 return;
2549 }
2550 }
2551 }
2552 else
2553 {
2554 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2555 magic_number[1] == DECRYPT_MAGIC_1)
2556 {
2557 ret = decrypt_scm((uint32 **) &data, &sz);
2558 if (ret != 0) {
2559 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2560 return;
2561 }
2562 }
2563 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2564 magic_number[1] == ENCRYPT_MAGIC_1)
2565 {
2566 ret = encrypt_scm((uint32 **) &data, &sz);
2567 if (ret != 0) {
2568 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2569 return;
2570 }
2571 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002572 }
2573 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002574 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002575 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002576 dprintf(CRITICAL,"INVALID SVC Version\n");
2577 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002578 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002579#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002580
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002581#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002582 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002583 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002584 if(!device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002585 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002586 fastboot_fail("device is locked. Cannot flash images");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002587 return;
2588 }
Channagoud Kadabi35297672015-06-13 11:09:49 -07002589 if(!device.is_unlocked && device.is_verified)
2590 {
2591 if(!boot_verify_flash_allowed(arg))
2592 {
2593 fastboot_fail("cannot flash this partition in verified state");
2594 return;
2595 }
2596 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002597 }
2598#endif
2599
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002600 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002601 meta_header = (meta_header_t *) data;
2602 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002603 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002604 else if (meta_header->magic == META_HEADER_MAGIC)
2605 cmd_flash_meta_img(arg, data, sz);
2606 else
2607 cmd_flash_mmc_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002608 return;
2609}
2610
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002611void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2612{
2613 struct ptentry *sys_ptn;
2614 struct ptable *ptable;
2615
2616 ptable = flash_get_ptable();
2617 if (ptable == NULL) {
2618 fastboot_fail("partition table doesn't exist");
2619 return;
2620 }
2621
2622 sys_ptn = ptable_find(ptable, "system");
2623 if (sys_ptn == NULL) {
2624 fastboot_fail("system partition not found");
2625 return;
2626 }
2627
2628 sz = ROUND_TO_PAGE(sz, page_mask);
2629 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2630 fastboot_fail("update_ubi_vol failed");
2631 else
2632 fastboot_okay("");
2633}
2634
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002635void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002636{
2637 struct ptentry *ptn;
2638 struct ptable *ptable;
2639 unsigned extra = 0;
2640
2641 ptable = flash_get_ptable();
2642 if (ptable == NULL) {
2643 fastboot_fail("partition table doesn't exist");
2644 return;
2645 }
2646
2647 ptn = ptable_find(ptable, arg);
2648 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002649 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2650 arg);
2651 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002652 return;
2653 }
2654
2655 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2656 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2657 fastboot_fail("image is not a boot image");
2658 return;
2659 }
2660 }
2661
Amol Jadi5c61a952012-05-04 17:05:35 -07002662 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002663 || !strcmp(ptn->name, "userdata")
2664 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002665 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002666 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002667 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002668 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002669 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002670
2671 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002672 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2673 if (flash_ubi_img(ptn, data, sz)) {
2674 fastboot_fail("flash write failure");
2675 return;
2676 }
2677 } else {
2678 if (flash_write(ptn, extra, data, sz)) {
2679 fastboot_fail("flash write failure");
2680 return;
2681 }
Dima Zavin214cc642009-01-26 11:16:21 -08002682 }
2683 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2684 fastboot_okay("");
2685}
2686
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002687void cmd_flash(const char *arg, void *data, unsigned sz)
2688{
2689 if(target_is_emmc_boot())
2690 cmd_flash_mmc(arg, data, sz);
2691 else
2692 cmd_flash_nand(arg, data, sz);
2693}
2694
Dima Zavin214cc642009-01-26 11:16:21 -08002695void cmd_continue(const char *arg, void *data, unsigned sz)
2696{
2697 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002698 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002699
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002700 if (target_is_emmc_boot())
2701 {
2702 boot_linux_from_mmc();
2703 }
2704 else
2705 {
2706 boot_linux_from_flash();
2707 }
Dima Zavin214cc642009-01-26 11:16:21 -08002708}
2709
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002710void cmd_reboot(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(0);
2715}
2716
2717void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2718{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002719 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002720 fastboot_okay("");
2721 reboot_device(FASTBOOT_MODE);
2722}
2723
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002724void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2725{
2726 dprintf(INFO, "Enabling charger screen check\n");
2727 device.charger_screen_enabled = 1;
2728 write_device_info(&device);
2729 fastboot_okay("");
2730}
2731
2732void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2733{
2734 dprintf(INFO, "Disabling charger screen check\n");
2735 device.charger_screen_enabled = 0;
2736 write_device_info(&device);
2737 fastboot_okay("");
2738}
2739
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302740void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2741{
2742 dprintf(INFO, "Selecting display panel %s\n", arg);
2743 if (arg)
2744 strlcpy(device.display_panel, arg,
2745 sizeof(device.display_panel));
2746 write_device_info(&device);
2747 fastboot_okay("");
2748}
2749
Shashank Mittal162244e2011-08-08 19:01:25 -07002750void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2751{
vijay kumarc65876c2015-04-24 13:29:16 +05302752 if(!is_allow_unlock) {
2753 fastboot_fail("oem unlock is not allowed");
2754 return;
2755 }
2756
2757 display_fbcon_message("Oem Unlock requested");
2758 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2759}
2760
2761void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
2762{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002763 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002764 {
vijay kumarc65876c2015-04-24 13:29:16 +05302765 if(!is_allow_unlock) {
2766 fastboot_fail("oem unlock is not allowed");
2767 return;
2768 }
2769
Shashank Mittal162244e2011-08-08 19:01:25 -07002770 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002771 device.is_verified = 0;
2772 write_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05302773
2774 struct recovery_message msg;
2775 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2776 write_misc(0, &msg, sizeof(msg));
2777
2778 fastboot_okay("");
2779 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002780 }
2781 fastboot_okay("");
2782}
2783
2784void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2785{
2786 /* TODO: Wipe user data */
2787 if(device.is_unlocked || device.is_verified)
2788 {
2789 device.is_unlocked = 0;
2790 device.is_verified = 0;
2791 write_device_info(&device);
2792 }
2793 fastboot_okay("");
2794}
2795
2796void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2797{
2798 /* TODO: Wipe user data */
2799 if(device.is_unlocked || !device.is_verified)
2800 {
2801 device.is_unlocked = 0;
2802 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002803 write_device_info(&device);
2804 }
2805 fastboot_okay("");
2806}
2807
Shashank Mittala0032282011-08-26 14:50:11 -07002808void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2809{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302810 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002811 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002812 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002813 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2814 fastboot_info(response);
2815 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002816 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302817 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2818 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002819 fastboot_okay("");
2820}
2821
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002822void cmd_preflash(const char *arg, void *data, unsigned sz)
2823{
2824 fastboot_okay("");
2825}
2826
Mao Flynn7b379f32015-04-20 00:28:30 +08002827static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302828
Mao Flynn7b379f32015-04-20 00:28:30 +08002829int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302830{
Mao Flynn7b379f32015-04-20 00:28:30 +08002831 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302832 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08002833 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302834 return -1;
2835 return 0;
2836}
2837
Mao Flynn7b379f32015-04-20 00:28:30 +08002838int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002839{
2840 struct ptentry *ptn;
2841 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08002842 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002843 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002844
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302845 ptable = flash_get_ptable();
2846 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002847 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2848 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302849 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002850
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302851 ptn = ptable_find(ptable, "splash");
2852 if (ptn == NULL) {
2853 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002854 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302855 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002856 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302857 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002858 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302859 }
2860
Mao Flynn7b379f32015-04-20 00:28:30 +08002861 header = (struct logo_img_header *)logo_header;
2862 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302863 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002864 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302865 }
2866
2867 fb_display = fbcon_display();
2868 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002869 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
2870 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
2871
2872 /* if the logo is full-screen size, remove "fbcon_clear()" */
2873 if ((header->width != fb_display->width)
2874 || (header->height != fb_display->height))
2875 fbcon_clear();
2876
2877 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
2878 (uint32_t *)base,
2879 (header->blocks * 512))) {
2880 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
2881 return -1;
2882 }
2883 fbcon_extract_to_screen(header, base);
2884 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002885 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002886
2887 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
2888 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
2889 return -1;
2890 }
2891
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302892 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08002893 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
2894 (uint32_t *)base,
2895 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302896 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302897 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002898 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002899 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302900 }
2901
Mao Flynn7b379f32015-04-20 00:28:30 +08002902 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302903}
2904
Mao Flynn7b379f32015-04-20 00:28:30 +08002905int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302906{
2907 int index = INVALID_PTN;
2908 unsigned long long ptn = 0;
2909 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08002910 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08002911 uint32_t blocksize, realsize, readsize;
2912 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302913
2914 index = partition_get_index("splash");
2915 if (index == 0) {
2916 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002917 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302918 }
2919
2920 ptn = partition_get_offset(index);
2921 if (ptn == 0) {
2922 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002923 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302924 }
2925
Mao Flynn1893a7f2015-06-03 12:03:36 +08002926 mmc_set_lun(partition_get_lun(index));
2927
2928 blocksize = mmc_get_device_blocksize();
2929 if (blocksize == 0) {
2930 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
2931 return -1;
2932 }
2933
2934 fb_display = fbcon_display();
2935 base = (uint8_t *) fb_display->base;
2936
2937 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302938 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002939 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302940 }
2941
Mao Flynn1893a7f2015-06-03 12:03:36 +08002942 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08002943 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302944 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002945 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302946 }
2947
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302948 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08002949 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
2950 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08002951
Mao Flynn1893a7f2015-06-03 12:03:36 +08002952 realsize = header->blocks * 512;
2953 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
2954
2955 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08002956 if ((header->width != fb_display->width)
2957 || (header->height != fb_display->height))
2958 fbcon_clear();
2959
Mao Flynn1893a7f2015-06-03 12:03:36 +08002960 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002961 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
2962 return -1;
2963 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002964
Mao Flynn1893a7f2015-06-03 12:03:36 +08002965 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
2966 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302967
Mao Flynn1893a7f2015-06-03 12:03:36 +08002968 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
2969 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
2970 return -1;
2971 }
2972
2973 realsize = header->width * header->height * fb_display->bpp / 8;
2974 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
2975
2976 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
2977 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
2978 fbcon_clear();
2979 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
2980 return -1;
2981 }
2982 } else {
2983 if (mmc_read(ptn + blocksize ,
2984 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
2985 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
2986 return -1;
2987 }
2988 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
2989 }
2990 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302991 }
2992
Mao Flynn7b379f32015-04-20 00:28:30 +08002993 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302994}
2995
Mao Flynn7b379f32015-04-20 00:28:30 +08002996int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302997{
2998 if (target_is_emmc_boot()) {
2999 return splash_screen_mmc();
3000 } else {
3001 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003002 }
3003}
3004
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003005/* Get the size from partiton name */
3006static void get_partition_size(const char *arg, char *response)
3007{
3008 uint64_t ptn = 0;
3009 uint64_t size;
3010 int index = INVALID_PTN;
3011
3012 index = partition_get_index(arg);
3013
3014 if (index == INVALID_PTN)
3015 {
3016 dprintf(CRITICAL, "Invalid partition index\n");
3017 return;
3018 }
3019
3020 ptn = partition_get_offset(index);
3021
3022 if(!ptn)
3023 {
3024 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3025 return;
3026 }
3027
3028 size = partition_get_size(index);
3029
3030 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3031 return;
3032}
3033
3034/*
3035 * Publish the partition type & size info
3036 * fastboot getvar will publish the required information.
3037 * fastboot getvar partition_size:<partition_name>: partition size in hex
3038 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3039 */
3040static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3041{
3042 uint8_t i;
3043
3044 for (i = 0; i < num_parts; i++) {
3045 get_partition_size(info[i].part_name, info[i].size_response);
3046
3047 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3048 {
3049 dprintf(CRITICAL, "partition size name truncated\n");
3050 return;
3051 }
3052 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3053 {
3054 dprintf(CRITICAL, "partition type name truncated\n");
3055 return;
3056 }
3057
3058 /* publish partition size & type info */
3059 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3060 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3061 }
3062}
3063
Amol Jadi5edf3552013-07-23 14:15:34 -07003064/* register commands and variables for fastboot */
3065void aboot_fastboot_register_commands(void)
3066{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003067 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07003068
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003069 struct fastboot_cmd_desc cmd_list[] = {
3070 /* By default the enabled list is empty. */
3071 {"", NULL},
3072 /* move commands enclosed within the below ifndef to here
3073 * if they need to be enabled in user build.
3074 */
3075#ifndef DISABLE_FASTBOOT_CMDS
3076 /* Register the following commands only for non-user builds */
3077 {"flash:", cmd_flash},
3078 {"erase:", cmd_erase},
3079 {"boot", cmd_boot},
3080 {"continue", cmd_continue},
3081 {"reboot", cmd_reboot},
3082 {"reboot-bootloader", cmd_reboot_bootloader},
3083 {"oem unlock", cmd_oem_unlock},
vijay kumarc65876c2015-04-24 13:29:16 +05303084 {"oem unlock-go", cmd_oem_unlock_go},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003085 {"oem lock", cmd_oem_lock},
3086 {"oem verified", cmd_oem_verified},
3087 {"oem device-info", cmd_oem_devinfo},
3088 {"preflash", cmd_preflash},
3089 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3090 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08003091 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003092#endif
3093 };
3094
3095 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3096 for (i = 1; i < fastboot_cmds_count; i++)
3097 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3098
Amol Jadi5edf3552013-07-23 14:15:34 -07003099 /* publish variables and their values */
3100 fastboot_publish("product", TARGET(BOARD));
3101 fastboot_publish("kernel", "lk");
3102 fastboot_publish("serialno", sn_buf);
3103
3104 /*
3105 * partition info is supported only for emmc partitions
3106 * Calling this for NAND prints some error messages which
3107 * is harmless but misleading. Avoid calling this for NAND
3108 * devices.
3109 */
3110 if (target_is_emmc_boot())
3111 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3112
3113 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003114 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3115 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003116 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003117 /* Is the charger screen check enabled */
3118 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3119 device.charger_screen_enabled);
3120 fastboot_publish("charger-screen-enabled",
3121 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303122 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3123 device.display_panel);
3124 fastboot_publish("display-panel",
3125 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003126 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3127 fastboot_publish("version-baseband", (const char *) device.radio_version);
Amol Jadi5edf3552013-07-23 14:15:34 -07003128}
3129
Brian Swetland9c4c0752009-01-25 16:23:50 -08003130void aboot_init(const struct app_descriptor *app)
3131{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003132 unsigned reboot_mode = 0;
lijuang72875802015-02-10 10:35:12 +08003133 unsigned hard_reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03003134 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003135
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003136 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003137 if (target_is_emmc_boot())
3138 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003139 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003140 page_mask = page_size - 1;
3141 }
3142 else
3143 {
3144 page_size = flash_page_size();
3145 page_mask = page_size - 1;
3146 }
3147
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003148 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3149
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003150 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303151 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003152
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003153 /* Display splash screen if enabled */
3154#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003155#if NO_ALARM_DISPLAY
3156 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003157#endif
lijuang99c02d82015-02-13 19:04:34 +08003158 dprintf(SPEW, "Display Init: Start\n");
3159 target_display_init(device.display_panel);
3160 dprintf(SPEW, "Display Init: Done\n");
3161#if NO_ALARM_DISPLAY
3162 }
3163#endif
3164#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003165
Greg Griscod6250552011-06-29 14:40:23 -07003166 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003167 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003168
Dhaval Patel223ec952013-07-18 14:49:44 -07003169 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3170
Matthew Qindefd5562014-07-11 18:02:40 +08003171 /*
3172 * Check power off reason if user force reset,
3173 * if yes phone will do normal boot.
3174 */
3175 if (is_user_force_reset())
3176 goto normal_boot;
3177
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003178 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003179 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003180 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003181 dprintf(ALWAYS,"dload mode key sequence detected\n");
3182 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003183 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003184 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003185 }
3186 else
3187 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303188 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003189 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003190 }
3191 boot_into_fastboot = true;
3192 }
3193 if (!boot_into_fastboot)
3194 {
3195 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3196 boot_into_recovery = 1;
3197 if (!boot_into_recovery &&
3198 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003199 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003200 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003201 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003202 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003203 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003204 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003205
Ajay Dudani77421292010-10-27 19:34:06 -07003206 reboot_mode = check_reboot_mode();
lijuang72875802015-02-10 10:35:12 +08003207 hard_reboot_mode = check_hard_reboot_mode();
3208 if (reboot_mode == RECOVERY_MODE ||
3209 hard_reboot_mode == RECOVERY_HARD_RESET_MODE) {
Ajay Dudani77421292010-10-27 19:34:06 -07003210 boot_into_recovery = 1;
lijuang72875802015-02-10 10:35:12 +08003211 } else if(reboot_mode == FASTBOOT_MODE ||
3212 hard_reboot_mode == FASTBOOT_HARD_RESET_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003213 boot_into_fastboot = true;
lijuang72875802015-02-10 10:35:12 +08003214 } else if(reboot_mode == ALARM_BOOT ||
3215 hard_reboot_mode == RTC_HARD_RESET_MODE) {
Matthew Qind886f3c2014-01-17 16:52:01 +08003216 boot_reason_alarm = true;
Ajay Dudani77421292010-10-27 19:34:06 -07003217 }
3218
Matthew Qindefd5562014-07-11 18:02:40 +08003219normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003220 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003221 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003222 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003223 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003224 if(emmc_recovery_init())
3225 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3226 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003227 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003228 if((device.is_unlocked) || (device.is_tampered))
3229 {
3230 #ifdef TZ_TAMPER_FUSE
3231 set_tamper_fuse_cmd();
3232 #endif
3233 #if USE_PCOM_SECBOOT
3234 set_tamper_flag(device.is_tampered);
3235 #endif
3236 }
Shashank Mittala0032282011-08-26 14:50:11 -07003237 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003238
Pavel Nedev5d91d412013-04-29 11:34:24 +03003239 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003240 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003241 else
3242 {
3243 recovery_init();
3244 #if USE_PCOM_SECBOOT
3245 if((device.is_unlocked) || (device.is_tampered))
3246 set_tamper_flag(device.is_tampered);
3247 #endif
3248 boot_linux_from_flash();
3249 }
3250 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3251 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003252 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003253
Amol Jadi5edf3552013-07-23 14:15:34 -07003254 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003255
Amol Jadi5edf3552013-07-23 14:15:34 -07003256 /* register aboot specific fastboot commands */
3257 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003258
Amol Jadi5edf3552013-07-23 14:15:34 -07003259 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003260 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003261
3262 /* initialize and start fastboot */
3263 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08003264}
3265
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003266uint32_t get_page_size()
3267{
3268 return page_size;
3269}
3270
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003271/*
3272 * Calculated and save hash (SHA256) for non-signed boot image.
3273 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003274 * @param image_addr - Boot image address
3275 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003276 *
3277 * @return int - 0 on success, negative value on failure.
3278 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003279static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003280{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003281 unsigned int digest[8];
3282#if IMAGE_VERIF_ALGO_SHA1
3283 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3284#else
3285 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3286#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003287
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003288 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003289 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003290
3291 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003292 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003293
3294 return 0;
3295}
3296
Brian Swetland9c4c0752009-01-25 16:23:50 -08003297APP_START(aboot)
3298 .init = aboot_init,
3299APP_END