blob: 472f7812d225522ed831cc86c03865d850a0103a [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08005 * Copyright (c) 2009-2015, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080060#include <decompress.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080061#if USE_RPMB_FOR_DEVINFO
62#include <rpmb.h>
63#endif
Dima Zavin214cc642009-01-26 11:16:21 -080064
Neeti Desai17379b82012-06-04 18:42:53 -070065#if DEVICE_TREE
66#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070067#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070068#endif
69
Shashank Mittalcd98d472011-08-02 14:29:24 -070070#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080071#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080072#include "bootimg.h"
73#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070074#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080075#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070076#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070077#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070078#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070079#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020080#include "mdtp.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070081
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070082extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070083extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070084extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070085extern int get_target_boot_params(const char *cmdline, const char *part,
86 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070087
88void write_device_info_mmc(device_info *dev);
89void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -070090static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070091
Sridhar Parasurame94e8152014-10-24 14:06:03 -070092/* fastboot command function pointer */
93typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
94
95struct fastboot_cmd_desc {
96 char * name;
97 fastboot_cmd_fn cb;
98};
99
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700100#define EXPAND(NAME) #NAME
101#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700102
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800103#ifdef MEMBASE
104#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
105#else
David Ng183a7422009-12-07 14:55:21 -0800106#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800107#endif
108
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700109#ifndef MEMSIZE
110#define MEMSIZE 1024*1024
111#endif
112
113#define MAX_TAGS_SIZE 1024
114
lijuang72875802015-02-10 10:35:12 +0800115#define RECOVERY_HARD_RESET_MODE 0x01
116#define FASTBOOT_HARD_RESET_MODE 0x02
117#define RTC_HARD_RESET_MODE 0x03
118
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800119#define RECOVERY_MODE 0x77665502
120#define FASTBOOT_MODE 0x77665500
Matthew Qind886f3c2014-01-17 16:52:01 +0800121#define ALARM_BOOT 0x77665503
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800122
Kun Liang2f1601a2013-08-12 16:29:54 +0800123/* make 4096 as default size to ensure EFS,EXT4's erasing */
124#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800125#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800126
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700127#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700128#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800129
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800130#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
131
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700132#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
133
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800134#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700135static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700136#else
David Ng183a7422009-12-07 14:55:21 -0800137static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700138#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800139static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300140static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800141static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800142static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800143static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700144static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300145static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200146static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800147
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800148static const char *baseband_apq = " androidboot.baseband=apq";
149static const char *baseband_msm = " androidboot.baseband=msm";
150static const char *baseband_csfb = " androidboot.baseband=csfb";
151static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700152static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800153static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700154static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800155static const char *baseband_dsda = " androidboot.baseband=dsda";
156static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800157static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800158static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800159
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700160static unsigned page_size = 0;
161static unsigned page_mask = 0;
162static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
163static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700164static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800165static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700166static bool devinfo_present = true;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700167
Shashank Mittalcd98d472011-08-02 14:29:24 -0700168/* Assuming unauthorized kernel image by default */
169static int auth_kernel_img = 0;
170
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -0800171static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
vijay kumarc65876c2015-04-24 13:29:16 +0530172static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700173
Dima Zavin42168f22009-01-30 11:52:22 -0800174struct atag_ptbl_entry
175{
176 char name[16];
177 unsigned offset;
178 unsigned size;
179 unsigned flags;
180};
181
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700182/*
183 * Partition info, required to be published
184 * for fastboot
185 */
186struct getvar_partition_info {
187 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
188 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
189 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
190 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
191 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
192};
193
194/*
195 * Right now, we are publishing the info for only
196 * three partitions
197 */
198struct getvar_partition_info part_info[] =
199{
200 { "system" , "partition-size:", "partition-type:", "", "ext4" },
201 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
202 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
203};
204
205char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700206char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800207char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700208char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530209char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700210
Greg Griscod2471ef2011-07-14 13:00:42 -0700211extern int emmc_recovery_init(void);
212
Kinson Chik0b1c8162011-08-31 16:31:57 -0700213#if NO_KEYPAD_DRIVER
214extern int fastboot_trigger(void);
215#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700216
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800217static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700218{
219 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700220#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800221 if (is_arm64)
222 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
223 else
224 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700225 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
226 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700227#endif
228}
229
Dima Zavin42168f22009-01-30 11:52:22 -0800230static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
231{
232 struct atag_ptbl_entry atag_ptn;
233
234 memcpy(atag_ptn.name, ptn->name, 16);
235 atag_ptn.name[15] = '\0';
236 atag_ptn.offset = ptn->start;
237 atag_ptn.size = ptn->length;
238 atag_ptn.flags = ptn->flags;
239 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
240 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
241}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800242
Neeti Desaie245d492012-06-01 12:52:13 -0700243unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800244{
David Ng183a7422009-12-07 14:55:21 -0800245 int cmdline_len = 0;
246 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800247 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700248 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800249 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300250 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700251 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700252 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200253 bool is_mdtp_activated = 0;
254#ifdef MDTP_SUPPORT
255 mdtp_activated(&is_mdtp_activated);
256#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800257
Brian Swetland9c4c0752009-01-25 16:23:50 -0800258 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800259 cmdline_len = strlen(cmdline);
260 have_cmdline = 1;
261 }
262 if (target_is_emmc_boot()) {
263 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800264#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700265 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
266 ASSERT(boot_dev_buf);
267 platform_boot_dev_cmdline(boot_dev_buf);
268 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700269#endif
David Ng183a7422009-12-07 14:55:21 -0800270 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800271
272 cmdline_len += strlen(usb_sn_cmdline);
273 cmdline_len += strlen(sn_buf);
274
Pavel Nedev5614d222013-06-17 18:01:02 +0300275 if (boot_into_recovery && gpt_exists)
276 cmdline_len += strlen(secondary_gpt_enable);
277
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200278 if(is_mdtp_activated)
279 cmdline_len += strlen(mdtp_activated_flag);
280
Pavel Nedev328ac822013-04-05 15:25:11 +0300281 if (boot_into_ffbm) {
282 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700283 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800284 /* reduce kernel console messages to speed-up boot */
285 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800286 } else if (boot_reason_alarm) {
287 cmdline_len += strlen(alarmboot_cmdline);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700288 } else if (device.charger_screen_enabled &&
289 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700290 pause_at_bootup = 1;
291 cmdline_len += strlen(battchg_pause);
292 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800293
Shashank Mittalcd98d472011-08-02 14:29:24 -0700294 if(target_use_signed_kernel() && auth_kernel_img) {
295 cmdline_len += strlen(auth_kernel);
296 }
297
Joonwoo Park61112782013-10-02 19:50:39 -0700298 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
299 "system",
300 target_boot_params,
301 sizeof(target_boot_params)) == 0) {
302 have_target_boot_params = 1;
303 cmdline_len += strlen(target_boot_params);
304 }
305
Ajay Dudanid04110c2011-01-17 23:55:07 -0800306 /* Determine correct androidboot.baseband to use */
307 switch(target_baseband())
308 {
309 case BASEBAND_APQ:
310 cmdline_len += strlen(baseband_apq);
311 break;
312
313 case BASEBAND_MSM:
314 cmdline_len += strlen(baseband_msm);
315 break;
316
317 case BASEBAND_CSFB:
318 cmdline_len += strlen(baseband_csfb);
319 break;
320
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800321 case BASEBAND_SVLTE2A:
322 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800323 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700324
325 case BASEBAND_MDM:
326 cmdline_len += strlen(baseband_mdm);
327 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700328
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800329 case BASEBAND_MDM2:
330 cmdline_len += strlen(baseband_mdm2);
331 break;
332
Amol Jadi5c61a952012-05-04 17:05:35 -0700333 case BASEBAND_SGLTE:
334 cmdline_len += strlen(baseband_sglte);
335 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530336
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800337 case BASEBAND_SGLTE2:
338 cmdline_len += strlen(baseband_sglte2);
339 break;
340
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530341 case BASEBAND_DSDA:
342 cmdline_len += strlen(baseband_dsda);
343 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800344
345 case BASEBAND_DSDA2:
346 cmdline_len += strlen(baseband_dsda2);
347 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800348 }
349
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800350 if (cmdline) {
351 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
352 target_display_panel_node(device.display_panel,
353 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
354 strlen(display_panel_buf)) {
355 cmdline_len += strlen(display_panel_buf);
356 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700357 }
358
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800359 if (target_warm_boot()) {
360 warm_boot = true;
361 cmdline_len += strlen(warmboot_cmdline);
362 }
363
David Ng183a7422009-12-07 14:55:21 -0800364 if (cmdline_len > 0) {
365 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800366 unsigned char *dst;
367
368 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
369 ASSERT(cmdline_final != NULL);
370 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700371
Amol Jadi168b7712012-03-06 16:15:00 -0800372 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800373 if (have_cmdline) {
374 src = cmdline;
375 while ((*dst++ = *src++));
376 }
377 if (target_is_emmc_boot()) {
378 src = emmc_cmdline;
379 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700380 have_cmdline = 1;
381 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800382#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700383 src = boot_dev_buf;
384 if (have_cmdline) --dst;
385 while ((*dst++ = *src++));
386#endif
David Ngf773dde2010-07-26 19:55:08 -0700387 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800388
389 src = usb_sn_cmdline;
390 if (have_cmdline) --dst;
391 have_cmdline = 1;
392 while ((*dst++ = *src++));
393 src = sn_buf;
394 if (have_cmdline) --dst;
395 have_cmdline = 1;
396 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800397 if (warm_boot) {
398 if (have_cmdline) --dst;
399 src = warmboot_cmdline;
400 while ((*dst++ = *src++));
401 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800402
Pavel Nedev5614d222013-06-17 18:01:02 +0300403 if (boot_into_recovery && gpt_exists) {
404 src = secondary_gpt_enable;
405 if (have_cmdline) --dst;
406 while ((*dst++ = *src++));
407 }
408
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200409 if (is_mdtp_activated) {
410 src = mdtp_activated_flag;
411 if (have_cmdline) --dst;
412 while ((*dst++ = *src++));
413 }
414
Pavel Nedev328ac822013-04-05 15:25:11 +0300415 if (boot_into_ffbm) {
416 src = androidboot_mode;
417 if (have_cmdline) --dst;
418 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700419 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300420 if (have_cmdline) --dst;
421 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800422 src = loglevel;
423 if (have_cmdline) --dst;
424 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800425 } else if (boot_reason_alarm) {
426 src = alarmboot_cmdline;
427 if (have_cmdline) --dst;
428 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300429 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700430 src = battchg_pause;
431 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800432 while ((*dst++ = *src++));
433 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800434
Shashank Mittalcd98d472011-08-02 14:29:24 -0700435 if(target_use_signed_kernel() && auth_kernel_img) {
436 src = auth_kernel;
437 if (have_cmdline) --dst;
438 while ((*dst++ = *src++));
439 }
440
Ajay Dudanid04110c2011-01-17 23:55:07 -0800441 switch(target_baseband())
442 {
443 case BASEBAND_APQ:
444 src = baseband_apq;
445 if (have_cmdline) --dst;
446 while ((*dst++ = *src++));
447 break;
448
449 case BASEBAND_MSM:
450 src = baseband_msm;
451 if (have_cmdline) --dst;
452 while ((*dst++ = *src++));
453 break;
454
455 case BASEBAND_CSFB:
456 src = baseband_csfb;
457 if (have_cmdline) --dst;
458 while ((*dst++ = *src++));
459 break;
460
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800461 case BASEBAND_SVLTE2A:
462 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800463 if (have_cmdline) --dst;
464 while ((*dst++ = *src++));
465 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700466
467 case BASEBAND_MDM:
468 src = baseband_mdm;
469 if (have_cmdline) --dst;
470 while ((*dst++ = *src++));
471 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700472
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800473 case BASEBAND_MDM2:
474 src = baseband_mdm2;
475 if (have_cmdline) --dst;
476 while ((*dst++ = *src++));
477 break;
478
Amol Jadi5c61a952012-05-04 17:05:35 -0700479 case BASEBAND_SGLTE:
480 src = baseband_sglte;
481 if (have_cmdline) --dst;
482 while ((*dst++ = *src++));
483 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530484
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800485 case BASEBAND_SGLTE2:
486 src = baseband_sglte2;
487 if (have_cmdline) --dst;
488 while ((*dst++ = *src++));
489 break;
490
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530491 case BASEBAND_DSDA:
492 src = baseband_dsda;
493 if (have_cmdline) --dst;
494 while ((*dst++ = *src++));
495 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800496
497 case BASEBAND_DSDA2:
498 src = baseband_dsda2;
499 if (have_cmdline) --dst;
500 while ((*dst++ = *src++));
501 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800502 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700503
504 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700505 src = display_panel_buf;
506 if (have_cmdline) --dst;
507 while ((*dst++ = *src++));
508 }
Joonwoo Park61112782013-10-02 19:50:39 -0700509
510 if (have_target_boot_params) {
511 if (have_cmdline) --dst;
512 src = target_boot_params;
513 while ((*dst++ = *src++));
514 }
Neeti Desaie245d492012-06-01 12:52:13 -0700515 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700516
517
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700518 if (boot_dev_buf)
519 free(boot_dev_buf);
520
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800521 if (cmdline_final)
522 dprintf(INFO, "cmdline: %s\n", cmdline_final);
523 else
524 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700525 return cmdline_final;
526}
527
528unsigned *atag_core(unsigned *ptr)
529{
530 /* CORE */
531 *ptr++ = 2;
532 *ptr++ = 0x54410001;
533
534 return ptr;
535
536}
537
538unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
539 unsigned ramdisk_size)
540{
541 if (ramdisk_size) {
542 *ptr++ = 4;
543 *ptr++ = 0x54420005;
544 *ptr++ = (unsigned)ramdisk;
545 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800546 }
547
Neeti Desaie245d492012-06-01 12:52:13 -0700548 return ptr;
549}
550
551unsigned *atag_ptable(unsigned **ptr_addr)
552{
553 int i;
554 struct ptable *ptable;
555
556 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700557 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
558 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700559 *(*ptr_addr)++ = 0x4d534d70;
560 for (i = 0; i < ptable->count; ++i)
561 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
562 }
563
564 return (*ptr_addr);
565}
566
567unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
568{
569 int cmdline_length = 0;
570 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700571 char *dest;
572
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800573 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700574 n = (cmdline_length + 4) & (~3);
575
576 *ptr++ = (n / 4) + 2;
577 *ptr++ = 0x54410009;
578 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800579 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700580 ptr += (n / 4);
581
582 return ptr;
583}
584
585unsigned *atag_end(unsigned *ptr)
586{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800587 /* END */
588 *ptr++ = 0;
589 *ptr++ = 0;
590
Neeti Desaie245d492012-06-01 12:52:13 -0700591 return ptr;
592}
593
594void generate_atags(unsigned *ptr, const char *cmdline,
595 void *ramdisk, unsigned ramdisk_size)
596{
597
598 ptr = atag_core(ptr);
599 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
600 ptr = target_atag_mem(ptr);
601
602 /* Skip NAND partition ATAGS for eMMC boot */
603 if (!target_is_emmc_boot()){
604 ptr = atag_ptable(&ptr);
605 }
606
607 ptr = atag_cmdline(ptr, cmdline);
608 ptr = atag_end(ptr);
609}
610
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700611typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700612void boot_linux(void *kernel, unsigned *tags,
613 const char *cmdline, unsigned machtype,
614 void *ramdisk, unsigned ramdisk_size)
615{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800616 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800617#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700618 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800619#endif
620
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700621 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800622 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800623 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800624
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530625 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700626
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800627 final_cmdline = update_cmdline((const char*)cmdline);
628
Neeti Desai17379b82012-06-04 18:42:53 -0700629#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800630 dprintf(INFO, "Updating device tree: start\n");
631
Neeti Desai17379b82012-06-04 18:42:53 -0700632 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530633 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700634 if(ret)
635 {
636 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
637 ASSERT(0);
638 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800639 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700640#else
Neeti Desaie245d492012-06-01 12:52:13 -0700641 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800642 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700643#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700644
Maria Yu52254c02014-07-04 16:14:54 +0800645 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700646
647#if VERIFIED_BOOT
648 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700649 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700650 {
651 dprintf(INFO, "Failed to write protect dev info\n");
652 ASSERT(0);
653 }
654#endif
655
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700656 /* Perform target specific cleanup */
657 target_uninit();
658
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800659 /* Turn off splash screen if enabled */
660#if DISPLAY_SPLASH_SCREEN
661 target_display_shutdown();
662#endif
663
664
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800665 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530666 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800667
668 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700669
Amol Jadi4421e652011-06-16 15:00:48 -0700670 /* do any platform specific cleanup before kernel entry */
671 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700672
Brian Swetland9c4c0752009-01-25 16:23:50 -0800673 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700674
Amol Jadi504f9fe2012-08-16 13:56:48 -0700675#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800676 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700677#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700678 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800679
680 if (IS_ARM64(kptr))
681 /* Jump to a 64bit kernel */
682 scm_elexec_call((paddr_t)kernel, tags_phys);
683 else
684 /* Jump to a 32bit kernel */
685 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800686}
687
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700688/* Function to check if the memory address range falls within the aboot
689 * boundaries.
690 * start: Start of the memory region
691 * size: Size of the memory region
692 */
693int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
694{
695 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800696 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700697 return -1;
698
699 /* Check for memory overlap. */
700 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
701 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700702 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700703 return 0;
704 else
705 return -1;
706}
707
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800708#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800709
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800710BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800711#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800712BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800713#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800714
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700715static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
716{
717 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800718
719#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800720#if IMAGE_VERIF_ALGO_SHA1
721 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
722#else
723 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
724#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800725#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700726
727 /* Assume device is rooted at this time. */
728 device.is_tampered = 1;
729
730 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
731
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700732#if VERIFIED_BOOT
733 if(boot_into_recovery)
734 {
735 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530736 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700737 }
738 else
739 {
740 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530741 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700742 }
743 boot_verify_print_state();
744#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700745 ret = image_verify((unsigned char *)bootimg_addr,
746 (unsigned char *)(bootimg_addr + bootimg_size),
747 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800748 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700749#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700750 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
751
752 if (ret)
753 {
754 /* Authorized kernel */
755 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700756 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700757 }
758
Amit Blay4aa292f2015-04-28 21:55:59 +0300759#ifdef MDTP_SUPPORT
760 {
761 /* Verify MDTP lock.
762 * For boot & recovery partitions, use aboot's verification result.
763 */
764 mdtp_ext_partition_verification_t ext_partition;
765 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
766 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
767 ext_partition.page_size = 0; /* Not needed since already validated */
768 ext_partition.image_addr = 0; /* Not needed since already validated */
769 ext_partition.image_size = 0; /* Not needed since already validated */
770 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
771 mdtp_fwlock_verify_lock(&ext_partition);
772 }
773#endif /* MDTP_SUPPORT */
774
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700775#if USE_PCOM_SECBOOT
776 set_tamper_flag(device.is_tampered);
777#endif
778
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700779#if VERIFIED_BOOT
780 if(boot_verify_get_state() == RED)
781 {
782 if(!boot_into_recovery)
783 {
784 dprintf(CRITICAL,
785 "Device verification failed. Rebooting into recovery.\n");
786 reboot_device(RECOVERY_MODE);
787 }
788 else
789 {
790 dprintf(CRITICAL,
791 "Recovery image verification failed. Asserting..\n");
792 ASSERT(0);
793 }
794 }
795#endif
Unnati Gandhi1be04752015-03-27 19:41:53 +0530796
797 if(device.is_tampered)
798 {
799 write_device_info_mmc(&device);
800 #ifdef TZ_TAMPER_FUSE
801 set_tamper_fuse_cmd();
802 #endif
803 #ifdef ASSERT_ON_TAMPER
804 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
805 ASSERT(0);
806 #endif
807 }
808
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700809}
810
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530811static bool check_format_bit()
812{
813 bool ret = false;
814 int index;
815 uint64_t offset;
816 struct boot_selection_info *in = NULL;
817 char *buf = NULL;
818
819 index = partition_get_index("bootselect");
820 if (index == INVALID_PTN)
821 {
822 dprintf(INFO, "Unable to locate /bootselect partition\n");
823 return ret;
824 }
825 offset = partition_get_offset(index);
826 if(!offset)
827 {
828 dprintf(INFO, "partition /bootselect doesn't exist\n");
829 return ret;
830 }
831 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
832 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530833 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530834 {
835 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
836 free(buf);
837 return ret;
838 }
839 in = (struct boot_selection_info *) buf;
840 if ((in->signature == BOOTSELECT_SIGNATURE) &&
841 (in->version == BOOTSELECT_VERSION)) {
842 if ((in->state_info & BOOTSELECT_FORMAT) &&
843 !(in->state_info & BOOTSELECT_FACTORY))
844 ret = true;
845 } else {
846 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
847 in->signature, in->version);
848 ASSERT(0);
849 }
850 free(buf);
851 return ret;
852}
853
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700854void boot_verifier_init()
855{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700856 uint32_t boot_state;
857 /* Check if device unlock */
858 if(device.is_unlocked)
859 {
860 boot_verify_send_event(DEV_UNLOCK);
861 boot_verify_print_state();
862 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
863 return;
864 }
865 else
866 {
867 boot_verify_send_event(BOOT_INIT);
868 }
869
870 /* Initialize keystore */
871 boot_state = boot_verify_keystore_init();
872 if(boot_state == YELLOW)
873 {
874 boot_verify_print_state();
875 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
876 }
877}
878
Shashank Mittal23b8f422010-04-16 19:27:21 -0700879int boot_linux_from_mmc(void)
880{
881 struct boot_img_hdr *hdr = (void*) buf;
882 struct boot_img_hdr *uhdr;
883 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700884 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700885 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700886 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700887
Shashank Mittalcd98d472011-08-02 14:29:24 -0700888 unsigned char *image_addr = 0;
889 unsigned kernel_actual;
890 unsigned ramdisk_actual;
891 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700892 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700893
Matthew Qin271927e2015-03-31 22:07:07 -0400894 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +0800895 unsigned int out_len = 0;
896 unsigned int out_avai_len = 0;
897 unsigned char *out_addr = NULL;
898 uint32_t dtb_offset = 0;
899 unsigned char *kernel_start_addr = NULL;
900 unsigned int kernel_size = 0;
901 int rc;
902
Neeti Desai465491e2012-07-31 12:53:35 -0700903#if DEVICE_TREE
904 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700905 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700906 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800907 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700908 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -0400909 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -0700910#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +0800911 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800912
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530913 if (check_format_bit())
914 boot_into_recovery = 1;
915
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700916 if (!boot_into_recovery) {
917 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
918 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
919 if (rcode <= 0) {
920 boot_into_ffbm = false;
921 if (rcode < 0)
922 dprintf(CRITICAL,"failed to get ffbm cookie");
923 } else
924 boot_into_ffbm = true;
925 } else
926 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700927 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
928 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
929 dprintf(INFO, "Unified boot method!\n");
930 hdr = uhdr;
931 goto unified_boot;
932 }
Greg Griscod6250552011-06-29 14:40:23 -0700933 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700934 index = partition_get_index("boot");
935 ptn = partition_get_offset(index);
936 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700937 dprintf(CRITICAL, "ERROR: No boot partition found\n");
938 return -1;
939 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700940 }
941 else {
942 index = partition_get_index("recovery");
943 ptn = partition_get_offset(index);
944 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700945 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
946 return -1;
947 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700948 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -0800949 /* Set Lun for boot & recovery partitions */
950 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -0700951
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530952 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700953 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
954 return -1;
955 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700956
957 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700958 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700959 return -1;
960 }
961
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700962 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530963
964 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
965 dprintf(CRITICAL, "ERROR: Invalid page size\n");
966 return -1;
967 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700968 page_size = hdr->page_size;
969 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700970 }
971
Matthew Qin49e51fa2015-02-09 10:40:45 +0800972 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
973 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800974
Matthew Qin49e51fa2015-02-09 10:40:45 +0800975 image_addr = (unsigned char *)target_get_scratch_address();
976
977#if DEVICE_TREE
978 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
979 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
980#else
981 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
982#endif
983
984#if VERIFIED_BOOT
985 boot_verifier_init();
986#endif
987
988 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
989 {
990 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
991 return -1;
992 }
993
Matthew Qinbb7923d2015-02-09 10:56:09 +0800994 /*
995 * Update loading flow of bootimage to support compressed/uncompressed
996 * bootimage on both 64bit and 32bit platform.
997 * 1. Load bootimage from emmc partition onto DDR.
998 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
999 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1000 * platform accordingly.
1001 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1002 */
1003
Matthew Qin49e51fa2015-02-09 10:40:45 +08001004 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
1005 bs_set_timestamp(BS_KERNEL_LOAD_START);
1006
Matthew Qinbb7923d2015-02-09 10:56:09 +08001007 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001008 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1009 {
1010 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1011 return -1;
1012 }
1013
1014 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
1015 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1016
1017 /* Authenticate Kernel */
1018 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1019 (int) target_use_signed_kernel(),
1020 device.is_unlocked,
1021 device.is_tampered);
1022
1023 if(target_use_signed_kernel() && (!device.is_unlocked))
1024 {
1025 offset = imagesize_actual;
1026 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1027 {
1028 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1029 return -1;
1030 }
1031
1032 /* Read signature */
1033 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1034 {
1035 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1036 return -1;
1037 }
1038
1039 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
1040 } else {
1041 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1042 #ifdef TZ_SAVE_KERNEL_HASH
1043 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1044 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001045
1046#ifdef MDTP_SUPPORT
1047 {
1048 /* Verify MDTP lock.
1049 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1050 * since verification was skipped in aboot. The signature is not part of the loaded image.
1051 */
1052 mdtp_ext_partition_verification_t ext_partition;
1053 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1054 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1055 ext_partition.page_size = page_size;
1056 ext_partition.image_addr = (uint32)image_addr;
1057 ext_partition.image_size = imagesize_actual;
1058 ext_partition.sig_avail = FALSE;
1059 mdtp_fwlock_verify_lock(&ext_partition);
1060 }
1061#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001062 }
1063
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001064 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001065 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1066 * If not, continue booting.
1067 */
1068 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1069 {
1070 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1071 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001072 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001073 rc = decompress((unsigned char *)(image_addr + page_size),
1074 hdr->kernel_size, out_addr, out_avai_len,
1075 &dtb_offset, &out_len);
1076 if (rc)
1077 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001078 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001079 ASSERT(0);
1080 }
1081
Matthew Qin0b15b322015-05-19 05:20:54 -04001082 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001083 kptr = (struct kernel64_hdr *)out_addr;
1084 kernel_start_addr = out_addr;
1085 kernel_size = out_len;
1086 } else {
1087 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1088 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1089 kernel_size = hdr->kernel_size;
1090 }
1091
1092 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001093 * Update the kernel/ramdisk/tags address if the boot image header
1094 * has default values, these default values come from mkbootimg when
1095 * the boot image is flashed using fastboot flash:raw
1096 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001097 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001098
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001099 /* Get virtual addresses since the hdr saves physical addresses. */
1100 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1101 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1102 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001103
Matthew Qinbb7923d2015-02-09 10:56:09 +08001104 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001105 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001106 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001107 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1108 {
1109 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1110 return -1;
1111 }
1112
1113#ifndef DEVICE_TREE
1114 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1115 {
1116 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1117 return -1;
1118 }
1119#endif
1120
Matthew Qin49e51fa2015-02-09 10:40:45 +08001121 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001122 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001123 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001124
Matthew Qin49e51fa2015-02-09 10:40:45 +08001125 #if DEVICE_TREE
1126 if(hdr->dt_size) {
1127 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1128 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001129
Matthew Qin49e51fa2015-02-09 10:40:45 +08001130 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1131 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1132 return -1;
1133 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001134
Matthew Qin49e51fa2015-02-09 10:40:45 +08001135 /* Find index of device tree within device tree table */
1136 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1137 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1138 return -1;
1139 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001140
Matthew Qin271927e2015-03-31 22:07:07 -04001141 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1142 {
1143 unsigned int compressed_size = 0;
1144 out_addr += out_len;
1145 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001146 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001147 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1148 dt_entry.size, out_addr, out_avai_len,
1149 &compressed_size, &dtb_size);
1150 if (rc)
1151 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001152 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001153 ASSERT(0);
1154 }
1155
Matthew Qin0b15b322015-05-19 05:20:54 -04001156 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001157 best_match_dt_addr = out_addr;
1158 } else {
1159 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1160 dtb_size = dt_entry.size;
1161 }
1162
Matthew Qin49e51fa2015-02-09 10:40:45 +08001163 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001164 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001165 {
1166 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1167 return -1;
1168 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001169
Matthew Qin271927e2015-03-31 22:07:07 -04001170 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001171 } else {
1172 /* Validate the tags_addr */
1173 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001174 {
1175 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1176 return -1;
1177 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001178 /*
1179 * If appended dev tree is found, update the atags with
1180 * memory address to the DTB appended location on RAM.
1181 * Else update with the atags address in the kernel header
1182 */
1183 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001184 dtb = dev_tree_appended((void*)(image_addr + page_size),
1185 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001186 (void *)hdr->tags_addr);
1187 if (!dtb) {
1188 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001189 return -1;
1190 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001191 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001192 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001193
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001194 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1195 target_load_ssd_keystore();
1196
Shashank Mittal23b8f422010-04-16 19:27:21 -07001197unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001198
Dima Zavin77e41f32013-03-06 16:10:43 -08001199 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001200 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001201 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1202
1203 return 0;
1204}
1205
Dima Zavin214cc642009-01-26 11:16:21 -08001206int boot_linux_from_flash(void)
1207{
1208 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001209 struct ptentry *ptn;
1210 struct ptable *ptable;
1211 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001212
Shashank Mittalcd98d472011-08-02 14:29:24 -07001213 unsigned char *image_addr = 0;
1214 unsigned kernel_actual;
1215 unsigned ramdisk_actual;
1216 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001217 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001218
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001219#if DEVICE_TREE
1220 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001221 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001222 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001223 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001224#endif
1225
David Ng183a7422009-12-07 14:55:21 -08001226 if (target_is_emmc_boot()) {
1227 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1228 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1229 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1230 return -1;
1231 }
1232 goto continue_boot;
1233 }
1234
Dima Zavin214cc642009-01-26 11:16:21 -08001235 ptable = flash_get_ptable();
1236 if (ptable == NULL) {
1237 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1238 return -1;
1239 }
1240
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001241 if(!boot_into_recovery)
1242 {
1243 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001244
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001245 if (ptn == NULL) {
1246 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1247 return -1;
1248 }
1249 }
1250 else
1251 {
1252 ptn = ptable_find(ptable, "recovery");
1253 if (ptn == NULL) {
1254 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1255 return -1;
1256 }
Dima Zavin214cc642009-01-26 11:16:21 -08001257 }
1258
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001259 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001260 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1261 return -1;
1262 }
Dima Zavin214cc642009-01-26 11:16:21 -08001263
1264 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001265 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001266 return -1;
1267 }
1268
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001269 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001270 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 -08001271 return -1;
1272 }
1273
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001274 /*
1275 * Update the kernel/ramdisk/tags address if the boot image header
1276 * has default values, these default values come from mkbootimg when
1277 * the boot image is flashed using fastboot flash:raw
1278 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001279 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001280
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001281 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001282 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1283 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1284 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1285
1286 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1287 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1288
1289 /* Check if the addresses in the header are valid. */
1290 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1291 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1292 {
1293 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1294 return -1;
1295 }
1296
1297#ifndef DEVICE_TREE
1298 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1299 {
1300 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1301 return -1;
1302 }
1303#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001304
Shashank Mittalcd98d472011-08-02 14:29:24 -07001305 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001306 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001307 {
1308 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001309 offset = 0;
1310
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001311#if DEVICE_TREE
1312 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1313 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001314
1315 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1316 {
1317 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1318 return -1;
1319 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001320#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001321 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001322#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001323
Amol Jadib6be5c12012-11-14 13:39:51 -08001324 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001325 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001326
Shashank Mittalcd98d472011-08-02 14:29:24 -07001327 /* Read image without signature */
1328 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1329 {
1330 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1331 return -1;
1332 }
Dima Zavin214cc642009-01-26 11:16:21 -08001333
Amol Jadib6be5c12012-11-14 13:39:51 -08001334 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001335 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001336
Shashank Mittalcd98d472011-08-02 14:29:24 -07001337 offset = imagesize_actual;
1338 /* Read signature */
1339 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1340 {
1341 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001342 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001343 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001344
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301345 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001346
1347 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001348 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1349 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001350#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001351 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001352 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001353 {
1354 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1355 return -1;
1356 }
1357
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001358 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1359#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001360
1361 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001362 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001363 {
1364 write_device_info_flash(&device);
1365 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301366#if USE_PCOM_SECBOOT
1367 set_tamper_flag(device.is_tampered);
1368#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001369 }
1370 else
1371 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001372 offset = page_size;
1373
Amol Jadib6be5c12012-11-14 13:39:51 -08001374 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1375 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1376 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1377
1378 dprintf(INFO, "Loading boot image (%d): start\n",
1379 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001380 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001381
1382 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001383 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1384 return -1;
1385 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001386 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001387
Amol Jadib6be5c12012-11-14 13:39:51 -08001388 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001389 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1390 return -1;
1391 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001392 offset += ramdisk_actual;
1393
1394 dprintf(INFO, "Loading boot image (%d): done\n",
1395 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001396 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001397
1398 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001399 offset += second_actual;
1400 /* Second image loading not implemented. */
1401 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001402 }
1403
1404#if DEVICE_TREE
1405 if(hdr->dt_size != 0) {
1406
1407 /* Read the device tree table into buffer */
1408 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1409 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1410 return -1;
1411 }
1412
1413 table = (struct dt_table*) dt_buf;
1414
Deepa Dinamani19648b42013-09-05 17:05:55 -07001415 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001416 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1417 return -1;
1418 }
1419
Deepa Dinamani19648b42013-09-05 17:05:55 -07001420 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1421 if (!table)
1422 return -1;
1423
1424 /* Read the entire device tree table into buffer */
1425 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1426 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1427 return -1;
1428 }
1429
1430
Joel Kingaa335dc2013-06-03 16:11:08 -07001431 /* Find index of device tree within device tree table */
1432 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001433 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1434 return -1;
1435 }
1436
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001437 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001438 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001439 {
1440 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1441 return -1;
1442 }
1443
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001444 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001445 if(flash_read(ptn, offset + dt_entry.offset,
1446 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001447 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1448 return -1;
1449 }
1450 }
1451#endif
1452
Shashank Mittalcd98d472011-08-02 14:29:24 -07001453 }
David Ng183a7422009-12-07 14:55:21 -08001454continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001455
Dima Zavin214cc642009-01-26 11:16:21 -08001456 /* TODO: create/pass atags to kernel */
1457
Ajay Dudanie28a6072011-07-01 13:59:46 -07001458 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001459 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001460 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1461
1462 return 0;
1463}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001464
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001465BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001466void write_device_info_mmc(device_info *dev)
1467{
Shashank Mittal162244e2011-08-08 19:01:25 -07001468 unsigned long long ptn = 0;
1469 unsigned long long size;
1470 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001471 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001472 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001473 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001474
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001475 if (devinfo_present)
1476 index = partition_get_index("devinfo");
1477 else
1478 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001479
Shashank Mittal162244e2011-08-08 19:01:25 -07001480 ptn = partition_get_offset(index);
1481 if(ptn == 0)
1482 {
1483 return;
1484 }
1485
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001486 lun = partition_get_lun(index);
1487 mmc_set_lun(lun);
1488
Shashank Mittal162244e2011-08-08 19:01:25 -07001489 size = partition_get_size(index);
1490
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001491 blocksize = mmc_get_device_blocksize();
1492
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001493 if (devinfo_present)
1494 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1495 else
1496 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1497 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001498 {
1499 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1500 return;
1501 }
1502}
1503
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001504void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001505{
Shashank Mittal162244e2011-08-08 19:01:25 -07001506 unsigned long long ptn = 0;
1507 unsigned long long size;
1508 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001509 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001510 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001511
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001512 if ((index = partition_get_index("devinfo")) < 0)
1513 {
1514 devinfo_present = false;
1515 index = partition_get_index("aboot");
1516 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001517
Shashank Mittal162244e2011-08-08 19:01:25 -07001518 ptn = partition_get_offset(index);
1519 if(ptn == 0)
1520 {
1521 return;
1522 }
1523
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001524 mmc_set_lun(partition_get_lun(index));
1525
Shashank Mittal162244e2011-08-08 19:01:25 -07001526 size = partition_get_size(index);
1527
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001528 blocksize = mmc_get_device_blocksize();
1529
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001530 if (devinfo_present)
1531 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1532 else
1533 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1534 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001535 {
1536 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1537 return;
1538 }
1539
Shashank Mittal162244e2011-08-08 19:01:25 -07001540}
1541
1542void write_device_info_flash(device_info *dev)
1543{
1544 struct device_info *info = (void *) info_buf;
1545 struct ptentry *ptn;
1546 struct ptable *ptable;
1547
1548 ptable = flash_get_ptable();
1549 if (ptable == NULL)
1550 {
1551 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1552 return;
1553 }
1554
1555 ptn = ptable_find(ptable, "devinfo");
1556 if (ptn == NULL)
1557 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001558 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001559 return;
1560 }
1561
1562 memcpy(info, dev, sizeof(device_info));
1563
1564 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1565 {
1566 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1567 return;
1568 }
1569}
1570
vijay kumarc65876c2015-04-24 13:29:16 +05301571static int read_allow_oem_unlock(device_info *dev)
1572{
1573 const char *ptn_name = "frp";
1574 unsigned offset;
1575 int index;
1576 unsigned long long ptn;
1577 unsigned long long ptn_size;
1578 unsigned blocksize = mmc_get_device_blocksize();
1579 char buf[blocksize];
1580
1581 index = partition_get_index(ptn_name);
1582 if (index == INVALID_PTN)
1583 {
1584 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
1585 return -1;
1586 }
1587
1588 ptn = partition_get_offset(index);
1589 ptn_size = partition_get_size(index);
1590 offset = ptn_size - blocksize;
1591
1592 if (mmc_read(ptn + offset, (void *)buf, sizeof(buf)))
1593 {
1594 dprintf(CRITICAL, "Reading MMC failed\n");
1595 return -1;
1596 }
1597
1598 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1599 is_allow_unlock = buf[blocksize-1] & 0x01;
1600 return 0;
1601}
1602
1603static int write_allow_oem_unlock(bool allow_unlock)
1604{
1605 const char *ptn_name = "frp";
1606 unsigned offset;
1607
1608 int index;
1609 unsigned long long ptn;
1610 unsigned long long ptn_size;
1611 unsigned blocksize = mmc_get_device_blocksize();
1612 char buf[blocksize];
1613
1614 index = partition_get_index(ptn_name);
1615 if (index == INVALID_PTN)
1616 {
1617 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
1618 return -1;
1619 }
1620
1621 ptn = partition_get_offset(index);
1622 ptn_size = partition_get_size(index);
1623 offset = ptn_size - blocksize;
1624
1625 if (mmc_read(ptn + offset, (void *)buf, sizeof(buf)))
1626 {
1627 dprintf(CRITICAL, "Reading MMC failed\n");
1628 return -1;
1629 }
1630
1631 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1632 buf[blocksize-1] = allow_unlock;
1633 if (mmc_write(ptn + offset, blocksize, buf))
1634 {
1635 dprintf(CRITICAL, "Writing MMC failed\n");
1636 return -1;
1637 }
1638
1639 return 0;
1640}
1641
Shashank Mittal162244e2011-08-08 19:01:25 -07001642void read_device_info_flash(device_info *dev)
1643{
1644 struct device_info *info = (void*) info_buf;
1645 struct ptentry *ptn;
1646 struct ptable *ptable;
1647
1648 ptable = flash_get_ptable();
1649 if (ptable == NULL)
1650 {
1651 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1652 return;
1653 }
1654
1655 ptn = ptable_find(ptable, "devinfo");
1656 if (ptn == NULL)
1657 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001658 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001659 return;
1660 }
1661
1662 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1663 {
1664 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1665 return;
1666 }
1667
1668 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1669 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001670 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1671 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001672 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001673 write_device_info_flash(info);
1674 }
1675 memcpy(dev, info, sizeof(device_info));
1676}
1677
1678void write_device_info(device_info *dev)
1679{
1680 if(target_is_emmc_boot())
1681 {
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001682 struct device_info *info = (void*) info_buf;
1683 memcpy(info, dev, sizeof(struct device_info));
1684
1685#if USE_RPMB_FOR_DEVINFO
1686 if (is_secure_boot_enable())
1687 write_device_info_rpmb((void*) info, mmc_get_device_blocksize());
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001688 else
1689 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001690#else
1691 write_device_info_mmc(info);
1692#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001693 }
1694 else
1695 {
1696 write_device_info_flash(dev);
1697 }
1698}
1699
1700void read_device_info(device_info *dev)
1701{
1702 if(target_is_emmc_boot())
1703 {
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001704 struct device_info *info = (void*) info_buf;
1705
1706#if USE_RPMB_FOR_DEVINFO
1707 if (is_secure_boot_enable())
1708 read_device_info_rpmb((void*) info, mmc_get_device_blocksize());
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001709 else
1710 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001711#else
1712 read_device_info_mmc(info);
1713#endif
1714
1715 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1716 {
1717 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1718 if (is_secure_boot_enable())
1719 info->is_unlocked = 0;
1720 else
1721 info->is_unlocked = 1;
1722 info->is_verified = 0;
1723 info->is_tampered = 0;
Unnati Gandhia49daa12015-03-23 18:08:38 +05301724#if USER_BUILD_VARIANT
1725 info->charger_screen_enabled = 1;
1726#else
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001727 info->charger_screen_enabled = 0;
Unnati Gandhia49daa12015-03-23 18:08:38 +05301728#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001729 write_device_info(info);
1730 }
1731 memcpy(dev, info, sizeof(device_info));
Shashank Mittal162244e2011-08-08 19:01:25 -07001732 }
1733 else
1734 {
1735 read_device_info_flash(dev);
1736 }
1737}
1738
1739void reset_device_info()
1740{
1741 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001742 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001743 write_device_info(&device);
1744}
1745
1746void set_device_root()
1747{
1748 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001749 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001750 write_device_info(&device);
1751}
1752
Amol Jadicb524072012-08-09 16:40:18 -07001753#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001754int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
Amol Jadicb524072012-08-09 16:40:18 -07001755{
1756 uint32 dt_image_offset = 0;
1757 uint32_t n;
1758 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001759 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001760 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001761 unsigned int compressed_size = 0;
1762 unsigned int dtb_size = 0;
1763 unsigned int out_avai_len = 0;
1764 unsigned char *out_addr = NULL;
1765 unsigned char *best_match_dt_addr = NULL;
1766 int rc;
Amol Jadicb524072012-08-09 16:40:18 -07001767
1768 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1769
Amol Jadicb524072012-08-09 16:40:18 -07001770 if(hdr->dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001771 /* add kernel offset */
1772 dt_image_offset += page_size;
1773 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1774 dt_image_offset += n;
1775
1776 /* add ramdisk offset */
1777 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1778 dt_image_offset += n;
1779
1780 /* add second offset */
1781 if(hdr->second_size != 0) {
1782 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1783 dt_image_offset += n;
1784 }
1785
1786 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001787 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001788
Deepa Dinamani19648b42013-09-05 17:05:55 -07001789 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001790 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1791 return -1;
1792 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001793 /* Find index of device tree within device tree table */
1794 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001795 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1796 return -1;
1797 }
1798
Matthew Qin271927e2015-03-31 22:07:07 -04001799 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
1800 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
1801 {
1802 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
1803 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04001804 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001805 rc = decompress(best_match_dt_addr,
1806 dt_entry.size, out_addr, out_avai_len,
1807 &compressed_size, &dtb_size);
1808 if (rc)
1809 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001810 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001811 ASSERT(0);
1812 }
1813
Matthew Qin0b15b322015-05-19 05:20:54 -04001814 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001815 best_match_dt_addr = out_addr;
1816 } else {
1817 dtb_size = dt_entry.size;
1818 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001819 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001820 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001821 {
1822 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1823 return -1;
1824 }
1825
Amol Jadicb524072012-08-09 16:40:18 -07001826 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001827 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001828 } else
1829 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001830
1831 /* Everything looks fine. Return success. */
1832 return 0;
1833}
1834#endif
1835
Brian Swetland9c4c0752009-01-25 16:23:50 -08001836void cmd_boot(const char *arg, void *data, unsigned sz)
1837{
1838 unsigned kernel_actual;
1839 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001840 uint32_t image_actual;
1841 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001842 uint32_t sig_actual = SIGNATURE_SIZE;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001843 struct boot_img_hdr *hdr = NULL;
1844 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001845 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001846 int ret = 0;
1847 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001848 unsigned int out_len = 0;
1849 unsigned int out_avai_len = 0;
1850 unsigned char *out_addr = NULL;
1851 uint32_t dtb_offset = 0;
1852 unsigned char *kernel_start_addr = NULL;
1853 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04001854 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001855
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001856#if VERIFIED_BOOT
1857 if(!device.is_unlocked)
1858 {
1859 fastboot_fail("unlock device to use this command");
1860 return;
1861 }
1862#endif
1863
Brian Swetland9c4c0752009-01-25 16:23:50 -08001864 if (sz < sizeof(hdr)) {
1865 fastboot_fail("invalid bootimage header");
1866 return;
1867 }
1868
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001869 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001870
1871 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001872 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001873
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001874 if(target_is_emmc_boot() && hdr->page_size) {
1875 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001876 page_mask = page_size - 1;
1877 }
1878
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001879 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1880 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001881#if DEVICE_TREE
1882 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1883#endif
1884
1885 image_actual = ADD_OF(page_size, kernel_actual);
1886 image_actual = ADD_OF(image_actual, ramdisk_actual);
1887 image_actual = ADD_OF(image_actual, dt_actual);
1888
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001889 if (target_use_signed_kernel() && (!device.is_unlocked))
1890 image_actual = ADD_OF(image_actual, sig_actual);
1891
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001892 /* sz should have atleast raw boot image */
1893 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001894 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001895 return;
1896 }
1897
1898 /* Verify the boot image
1899 * device & page_size are initialized in aboot_init
1900 */
1901 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001902 /* Pass size excluding signature size, otherwise we would try to
1903 * access signature beyond its length
1904 */
1905 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001906
Amit Blay4aa292f2015-04-28 21:55:59 +03001907#ifdef MDTP_SUPPORT
1908 else
1909 {
1910 /* Verify MDTP lock before continue with boot process.
1911 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1912 * since verification was skipped in aboot. The signarue is already part of the loaded image.
1913 */
1914 mdtp_ext_partition_verification_t ext_partition;
1915 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1916 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1917 ext_partition.page_size = page_size;
1918 ext_partition.image_addr = (uint32_t)data;
1919 ext_partition.image_size = image_actual - sig_actual;
1920 ext_partition.sig_avail = TRUE;
1921 mdtp_fwlock_verify_lock(&ext_partition);
1922 }
1923#endif /* MDTP_SUPPORT */
1924
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001925 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001926 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1927 * If not, continue booting.
1928 */
1929 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
1930 {
1931 out_addr = (unsigned char *)target_get_scratch_address();
1932 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
1933 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001934 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001935 ret = decompress((unsigned char *)(ptr + page_size),
1936 hdr->kernel_size, out_addr, out_avai_len,
1937 &dtb_offset, &out_len);
1938 if (ret)
1939 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001940 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001941 ASSERT(0);
1942 }
1943
Matthew Qin0b15b322015-05-19 05:20:54 -04001944 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001945 kptr = (struct kernel64_hdr *)out_addr;
1946 kernel_start_addr = out_addr;
1947 kernel_size = out_len;
1948 } else {
1949 kptr = (struct kernel64_hdr*)((char *)data + page_size);
1950 kernel_start_addr = (unsigned char *)((char *)data + page_size);
1951 kernel_size = hdr->kernel_size;
1952 }
1953
1954 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001955 * Update the kernel/ramdisk/tags address if the boot image header
1956 * has default values, these default values come from mkbootimg when
1957 * the boot image is flashed using fastboot flash:raw
1958 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001959 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001960
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001961 /* Get virtual addresses since the hdr saves physical addresses. */
1962 hdr->kernel_addr = VA(hdr->kernel_addr);
1963 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1964 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001965
Matthew Qinbb7923d2015-02-09 10:56:09 +08001966 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001967 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001968 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001969 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1970 {
1971 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001972 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001973 }
1974
Amol Jadicb524072012-08-09 16:40:18 -07001975#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001976 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07001977 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04001978 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001979
1980 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001981#else
1982 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1983 {
1984 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001985 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001986 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001987#endif
1988
1989 /* Load ramdisk & kernel */
1990 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08001991 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001992
1993#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08001994 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1995 {
1996 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1997 return;
1998 }
1999
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002000 /*
2001 * If dtb is not found look for appended DTB in the kernel.
2002 * If appended dev tree is found, update the atags with
2003 * memory address to the DTB appended location on RAM.
2004 * Else update with the atags address in the kernel header
2005 */
2006 if (!dtb_copied) {
2007 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002008 dtb = dev_tree_appended((void*)(ptr + page_size),
2009 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002010 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002011 if (!dtb) {
2012 fastboot_fail("dtb not found");
2013 return;
2014 }
Amol Jadicb524072012-08-09 16:40:18 -07002015 }
2016#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002017
2018 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002019 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002020
Dima Zavin77e41f32013-03-06 16:10:43 -08002021 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002022 (const char*) hdr->cmdline, board_machtype(),
2023 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002024}
2025
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002026void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002027{
2028 struct ptentry *ptn;
2029 struct ptable *ptable;
2030
2031 ptable = flash_get_ptable();
2032 if (ptable == NULL) {
2033 fastboot_fail("partition table doesn't exist");
2034 return;
2035 }
2036
2037 ptn = ptable_find(ptable, arg);
2038 if (ptn == NULL) {
2039 fastboot_fail("unknown partition name");
2040 return;
2041 }
2042
2043 if (flash_erase(ptn)) {
2044 fastboot_fail("failed to erase partition");
2045 return;
2046 }
2047 fastboot_okay("");
2048}
2049
Bikas Gurungd48bd242010-09-04 19:54:32 -07002050
2051void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2052{
2053 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002054 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002055 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002056 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002057
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002058#if VERIFIED_BOOT
2059 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2060 {
2061 if(!device.is_unlocked)
2062 {
2063 fastboot_fail("unlock device to erase keystore");
2064 return;
2065 }
2066 }
2067#endif
2068
Kinson Chikf1a43512011-07-14 11:28:39 -07002069 index = partition_get_index(arg);
2070 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002071 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002072
Kinson Chikf1a43512011-07-14 11:28:39 -07002073 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002074 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002075 return;
2076 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002077
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002078 lun = partition_get_lun(index);
2079 mmc_set_lun(lun);
2080
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002081 if (platform_boot_dev_isemmc())
2082 {
2083 if (mmc_erase_card(ptn, size)) {
2084 fastboot_fail("failed to erase partition\n");
2085 return;
2086 }
2087 } else {
2088 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2089 size = partition_get_size(index);
2090 if (size > DEFAULT_ERASE_SIZE)
2091 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002092
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002093 /* Simple inefficient version of erase. Just writing
2094 0 in first several blocks */
2095 if (mmc_write(ptn , size, (unsigned int *)out)) {
2096 fastboot_fail("failed to erase partition");
2097 return;
2098 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002099 }
2100 fastboot_okay("");
2101}
2102
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002103void cmd_erase(const char *arg, void *data, unsigned sz)
2104{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002105#if VERIFIED_BOOT
2106 if(!device.is_unlocked && !device.is_verified)
2107 {
2108 fastboot_fail("device is locked. Cannot erase");
2109 return;
2110 }
2111 if(!device.is_unlocked && device.is_verified)
2112 {
2113 if(!boot_verify_flash_allowed(arg))
2114 {
2115 fastboot_fail("cannot flash this partition in verified state");
2116 return;
2117 }
2118 }
2119#endif
2120
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002121 if(target_is_emmc_boot())
2122 cmd_erase_mmc(arg, data, sz);
2123 else
2124 cmd_erase_nand(arg, data, sz);
2125}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002126
Ajay Dudani5c761132011-04-07 20:19:04 -07002127void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002128{
2129 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002130 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002131 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002132 char *token = NULL;
2133 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002134 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002135 uint8_t lun = 0;
2136 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002137
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002138 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002139 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002140 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002141 if(token)
2142 {
2143 lun = atoi(token);
2144 mmc_set_lun(lun);
2145 lun_set = true;
2146 }
2147
Mao Jinlong226f33a2014-07-04 17:24:10 +08002148 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002149 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002150 if (!strcmp(pname, "partition"))
2151 {
2152 dprintf(INFO, "Attempt to write partition image.\n");
2153 if (write_partition(sz, (unsigned char *) data)) {
2154 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002155 return;
2156 }
2157 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002158 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002159 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002160#if VERIFIED_BOOT
2161 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2162 {
2163 if(!device.is_unlocked)
2164 {
2165 fastboot_fail("unlock device to flash keystore");
2166 return;
2167 }
2168 if(!boot_verify_validate_keystore((unsigned char *)data))
2169 {
2170 fastboot_fail("image is not a keystore file");
2171 return;
2172 }
2173 }
2174#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002175 index = partition_get_index(pname);
2176 ptn = partition_get_offset(index);
2177 if(ptn == 0) {
2178 fastboot_fail("partition table doesn't exist");
2179 return;
2180 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002181
Mao Jinlong226f33a2014-07-04 17:24:10 +08002182 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2183 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2184 fastboot_fail("image is not a boot image");
2185 return;
2186 }
2187 }
2188
2189 if(!lun_set)
2190 {
2191 lun = partition_get_lun(index);
2192 mmc_set_lun(lun);
2193 }
2194
2195 size = partition_get_size(index);
2196 if (ROUND_TO_PAGE(sz,511) > size) {
2197 fastboot_fail("size too large");
2198 return;
2199 }
2200 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2201 fastboot_fail("flash write failure");
2202 return;
2203 }
Greg Grisco6e754772011-06-23 12:19:39 -07002204 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002205 }
2206 fastboot_okay("");
2207 return;
2208}
2209
Ajay Dudanide984792015-03-02 09:57:41 -08002210void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2211{
2212 int i, images;
2213 meta_header_t *meta_header;
2214 img_header_entry_t *img_header_entry;
2215
2216 meta_header = (meta_header_t*) data;
2217 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2218
2219 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2220
2221 for (i=0; i<images; i++) {
2222
2223 if((img_header_entry[i].ptn_name == NULL) ||
2224 (img_header_entry[i].start_offset == 0) ||
2225 (img_header_entry[i].size == 0))
2226 break;
2227
2228 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2229 (void *) data + img_header_entry[i].start_offset,
2230 img_header_entry[i].size);
2231 }
2232
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002233 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2234 {
2235 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2236 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2237 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2238 }
2239 else
2240 {
2241 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2242 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2243 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2244 }
2245
2246 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002247 fastboot_okay("");
2248 return;
2249}
2250
Ajay Dudani5c761132011-04-07 20:19:04 -07002251void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2252{
2253 unsigned int chunk;
2254 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002255 uint32_t *fill_buf = NULL;
2256 uint32_t fill_val;
2257 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002258 sparse_header_t *sparse_header;
2259 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002260 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002261 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302262 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002263 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302264 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002265 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002266
Kinson Chikf1a43512011-07-14 11:28:39 -07002267 index = partition_get_index(arg);
2268 ptn = partition_get_offset(index);
2269 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002270 fastboot_fail("partition table doesn't exist");
2271 return;
2272 }
2273
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302274 size = partition_get_size(index);
2275 if (ROUND_TO_PAGE(sz,511) > size) {
2276 fastboot_fail("size too large");
2277 return;
2278 }
2279
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002280 lun = partition_get_lun(index);
2281 mmc_set_lun(lun);
2282
Ajay Dudani5c761132011-04-07 20:19:04 -07002283 /* Read and skip over sparse image header */
2284 sparse_header = (sparse_header_t *) data;
vijay kumar1321f342015-03-27 12:13:42 +05302285 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002286 fastboot_fail("size too large");
2287 return;
2288 }
2289
Ajay Dudani5c761132011-04-07 20:19:04 -07002290 data += sparse_header->file_hdr_sz;
2291 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
2292 {
2293 /* Skip the remaining bytes in a header that is longer than
2294 * we expected.
2295 */
2296 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
2297 }
2298
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002299 dprintf (SPEW, "=== Sparse Image Header ===\n");
2300 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2301 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2302 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2303 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2304 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2305 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2306 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2307 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002308
2309 /* Start processing chunks */
2310 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2311 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302312 /* Make sure the total image size does not exceed the partition size */
2313 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2314 fastboot_fail("size too large");
2315 return;
2316 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002317 /* Read and skip over chunk header */
2318 chunk_header = (chunk_header_t *) data;
2319 data += sizeof(chunk_header_t);
2320
2321 dprintf (SPEW, "=== Chunk Header ===\n");
2322 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2323 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2324 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2325
2326 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
2327 {
2328 /* Skip the remaining bytes in a header that is longer than
2329 * we expected.
2330 */
2331 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
2332 }
2333
2334 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302335
2336 /* Make sure multiplication does not overflow uint32 size */
2337 if (sparse_header->blk_sz && (chunk_header->chunk_sz != chunk_data_sz / sparse_header->blk_sz))
2338 {
2339 fastboot_fail("Bogus size sparse and chunk header");
2340 return;
2341 }
2342
2343 /* Make sure that the chunk size calculated from sparse image does not
2344 * exceed partition size
2345 */
2346 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2347 {
2348 fastboot_fail("Chunk data size exceeds partition size");
2349 return;
2350 }
2351
Ajay Dudani5c761132011-04-07 20:19:04 -07002352 switch (chunk_header->chunk_type)
2353 {
2354 case CHUNK_TYPE_RAW:
2355 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2356 chunk_data_sz))
2357 {
2358 fastboot_fail("Bogus chunk size for chunk type Raw");
2359 return;
2360 }
2361
Ajay Dudaniab18f022011-05-12 14:39:22 -07002362 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2363 chunk_data_sz,
2364 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002365 {
2366 fastboot_fail("flash write failure");
2367 return;
2368 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302369 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2370 fastboot_fail("Bogus size for RAW chunk type");
2371 return;
2372 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002373 total_blocks += chunk_header->chunk_sz;
2374 data += chunk_data_sz;
2375 break;
2376
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002377 case CHUNK_TYPE_FILL:
2378 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2379 sizeof(uint32_t)))
2380 {
2381 fastboot_fail("Bogus chunk size for chunk type FILL");
2382 return;
2383 }
2384
2385 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2386 if (!fill_buf)
2387 {
2388 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2389 return;
2390 }
2391
2392 fill_val = *(uint32_t *)data;
2393 data = (char *) data + sizeof(uint32_t);
2394 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
2395
2396 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2397 {
2398 fill_buf[i] = fill_val;
2399 }
2400
2401 for (i = 0; i < chunk_blk_cnt; i++)
2402 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302403 /* Make sure that the data written to partition does not exceed partition size */
2404 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2405 {
2406 fastboot_fail("Chunk data size for fill type exceeds partition size");
2407 return;
2408 }
2409
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002410 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2411 sparse_header->blk_sz,
2412 fill_buf))
2413 {
2414 fastboot_fail("flash write failure");
2415 free(fill_buf);
2416 return;
2417 }
2418
2419 total_blocks++;
2420 }
2421
2422 free(fill_buf);
2423 break;
2424
Ajay Dudani5c761132011-04-07 20:19:04 -07002425 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302426 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2427 fastboot_fail("bogus size for chunk DONT CARE type");
2428 return;
2429 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002430 total_blocks += chunk_header->chunk_sz;
2431 break;
2432
Ajay Dudani5c761132011-04-07 20:19:04 -07002433 case CHUNK_TYPE_CRC:
2434 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2435 {
2436 fastboot_fail("Bogus chunk size for chunk type Dont Care");
2437 return;
2438 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302439 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2440 fastboot_fail("bogus size for chunk CRC type");
2441 return;
2442 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002443 total_blocks += chunk_header->chunk_sz;
2444 data += chunk_data_sz;
2445 break;
2446
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002447 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002448 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002449 fastboot_fail("Unknown chunk type");
2450 return;
2451 }
2452 }
2453
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002454 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2455 total_blocks, sparse_header->total_blks);
2456
2457 if(total_blocks != sparse_header->total_blks)
2458 {
2459 fastboot_fail("sparse image write failure");
2460 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002461
2462 fastboot_okay("");
2463 return;
2464}
2465
2466void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2467{
2468 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002469 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002470
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002471#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002472 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2473 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002474 int ret=0;
2475 uint32 major_version=0;
2476 uint32 minor_version=0;
2477
2478 ret = scm_svc_version(&major_version,&minor_version);
2479 if(!ret)
2480 {
2481 if(major_version >= 2)
2482 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002483 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002484 {
2485 ret = encrypt_scm((uint32 **) &data, &sz);
2486 if (ret != 0) {
2487 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2488 return;
2489 }
2490
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002491 /* Protect only for SSD */
2492 if (!strcmp(arg, "ssd")) {
2493 ret = scm_protect_keystore((uint32 *) data, sz);
2494 if (ret != 0) {
2495 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2496 return;
2497 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002498 }
2499 }
2500 else
2501 {
2502 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2503 if(ret != 0)
2504 {
2505 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2506 return;
2507 }
2508 }
2509 }
2510 else
2511 {
2512 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2513 magic_number[1] == DECRYPT_MAGIC_1)
2514 {
2515 ret = decrypt_scm((uint32 **) &data, &sz);
2516 if (ret != 0) {
2517 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2518 return;
2519 }
2520 }
2521 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2522 magic_number[1] == ENCRYPT_MAGIC_1)
2523 {
2524 ret = encrypt_scm((uint32 **) &data, &sz);
2525 if (ret != 0) {
2526 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2527 return;
2528 }
2529 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002530 }
2531 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002532 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002533 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002534 dprintf(CRITICAL,"INVALID SVC Version\n");
2535 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002536 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002537#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002538
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002539#if VERIFIED_BOOT
vijay kumarc65876c2015-04-24 13:29:16 +05302540 if(!device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002541 {
2542 fastboot_fail("device is locked. Cannot flash images");
2543 return;
2544 }
2545 if(!device.is_unlocked && device.is_verified)
2546 {
2547 if(!boot_verify_flash_allowed(arg))
2548 {
2549 fastboot_fail("cannot flash this partition in verified state");
2550 return;
2551 }
2552 }
2553#endif
2554
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002555 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002556 meta_header = (meta_header_t *) data;
2557 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002558 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002559 else if (meta_header->magic == META_HEADER_MAGIC)
2560 cmd_flash_meta_img(arg, data, sz);
2561 else
2562 cmd_flash_mmc_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002563 return;
2564}
2565
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002566void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2567{
2568 struct ptentry *sys_ptn;
2569 struct ptable *ptable;
2570
2571 ptable = flash_get_ptable();
2572 if (ptable == NULL) {
2573 fastboot_fail("partition table doesn't exist");
2574 return;
2575 }
2576
2577 sys_ptn = ptable_find(ptable, "system");
2578 if (sys_ptn == NULL) {
2579 fastboot_fail("system partition not found");
2580 return;
2581 }
2582
2583 sz = ROUND_TO_PAGE(sz, page_mask);
2584 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2585 fastboot_fail("update_ubi_vol failed");
2586 else
2587 fastboot_okay("");
2588}
2589
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002590void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002591{
2592 struct ptentry *ptn;
2593 struct ptable *ptable;
2594 unsigned extra = 0;
2595
2596 ptable = flash_get_ptable();
2597 if (ptable == NULL) {
2598 fastboot_fail("partition table doesn't exist");
2599 return;
2600 }
2601
2602 ptn = ptable_find(ptable, arg);
2603 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002604 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2605 arg);
2606 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002607 return;
2608 }
2609
2610 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2611 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2612 fastboot_fail("image is not a boot image");
2613 return;
2614 }
2615 }
2616
Amol Jadi5c61a952012-05-04 17:05:35 -07002617 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002618 || !strcmp(ptn->name, "userdata")
2619 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002620 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002621 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002622 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002623 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002624 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002625
2626 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002627 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2628 if (flash_ubi_img(ptn, data, sz)) {
2629 fastboot_fail("flash write failure");
2630 return;
2631 }
2632 } else {
2633 if (flash_write(ptn, extra, data, sz)) {
2634 fastboot_fail("flash write failure");
2635 return;
2636 }
Dima Zavin214cc642009-01-26 11:16:21 -08002637 }
2638 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2639 fastboot_okay("");
2640}
2641
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002642void cmd_flash(const char *arg, void *data, unsigned sz)
2643{
2644 if(target_is_emmc_boot())
2645 cmd_flash_mmc(arg, data, sz);
2646 else
2647 cmd_flash_nand(arg, data, sz);
2648}
2649
Dima Zavin214cc642009-01-26 11:16:21 -08002650void cmd_continue(const char *arg, void *data, unsigned sz)
2651{
2652 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002653 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002654
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002655 if (target_is_emmc_boot())
2656 {
2657 boot_linux_from_mmc();
2658 }
2659 else
2660 {
2661 boot_linux_from_flash();
2662 }
Dima Zavin214cc642009-01-26 11:16:21 -08002663}
2664
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002665void cmd_reboot(const char *arg, void *data, unsigned sz)
2666{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002667 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002668 fastboot_okay("");
2669 reboot_device(0);
2670}
2671
2672void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2673{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002674 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002675 fastboot_okay("");
2676 reboot_device(FASTBOOT_MODE);
2677}
2678
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002679void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2680{
2681 dprintf(INFO, "Enabling charger screen check\n");
2682 device.charger_screen_enabled = 1;
2683 write_device_info(&device);
2684 fastboot_okay("");
2685}
2686
2687void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2688{
2689 dprintf(INFO, "Disabling charger screen check\n");
2690 device.charger_screen_enabled = 0;
2691 write_device_info(&device);
2692 fastboot_okay("");
2693}
2694
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302695void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2696{
2697 dprintf(INFO, "Selecting display panel %s\n", arg);
2698 if (arg)
2699 strlcpy(device.display_panel, arg,
2700 sizeof(device.display_panel));
2701 write_device_info(&device);
2702 fastboot_okay("");
2703}
2704
Shashank Mittal162244e2011-08-08 19:01:25 -07002705void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2706{
vijay kumarc65876c2015-04-24 13:29:16 +05302707 if(!is_allow_unlock) {
2708 fastboot_fail("oem unlock is not allowed");
2709 return;
2710 }
2711
2712 display_fbcon_message("Oem Unlock requested");
2713 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2714}
2715
2716void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
2717{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002718 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002719 {
vijay kumarc65876c2015-04-24 13:29:16 +05302720 if(!is_allow_unlock) {
2721 fastboot_fail("oem unlock is not allowed");
2722 return;
2723 }
2724
Shashank Mittal162244e2011-08-08 19:01:25 -07002725 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002726 device.is_verified = 0;
2727 write_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05302728
2729 struct recovery_message msg;
2730 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2731 write_misc(0, &msg, sizeof(msg));
2732
2733 fastboot_okay("");
2734 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002735 }
2736 fastboot_okay("");
2737}
2738
2739void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2740{
2741 /* TODO: Wipe user data */
2742 if(device.is_unlocked || device.is_verified)
2743 {
2744 device.is_unlocked = 0;
2745 device.is_verified = 0;
2746 write_device_info(&device);
2747 }
2748 fastboot_okay("");
2749}
2750
2751void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2752{
2753 /* TODO: Wipe user data */
2754 if(device.is_unlocked || !device.is_verified)
2755 {
2756 device.is_unlocked = 0;
2757 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002758 write_device_info(&device);
2759 }
2760 fastboot_okay("");
2761}
2762
Shashank Mittala0032282011-08-26 14:50:11 -07002763void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2764{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302765 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002766 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002767 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002768 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2769 fastboot_info(response);
2770 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002771 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302772 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2773 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002774 fastboot_okay("");
2775}
2776
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002777void cmd_preflash(const char *arg, void *data, unsigned sz)
2778{
2779 fastboot_okay("");
2780}
2781
Mao Flynn7b379f32015-04-20 00:28:30 +08002782static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302783
Mao Flynn7b379f32015-04-20 00:28:30 +08002784int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302785{
Mao Flynn7b379f32015-04-20 00:28:30 +08002786 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302787 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08002788 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302789 return -1;
2790 return 0;
2791}
2792
Mao Flynn7b379f32015-04-20 00:28:30 +08002793int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002794{
2795 struct ptentry *ptn;
2796 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08002797 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002798 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002799
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302800 ptable = flash_get_ptable();
2801 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002802 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2803 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302804 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002805
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302806 ptn = ptable_find(ptable, "splash");
2807 if (ptn == NULL) {
2808 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002809 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302810 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002811 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302812 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002813 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302814 }
2815
Mao Flynn7b379f32015-04-20 00:28:30 +08002816 header = (struct logo_img_header *)logo_header;
2817 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302818 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002819 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302820 }
2821
2822 fb_display = fbcon_display();
2823 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002824 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
2825 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
2826
2827 /* if the logo is full-screen size, remove "fbcon_clear()" */
2828 if ((header->width != fb_display->width)
2829 || (header->height != fb_display->height))
2830 fbcon_clear();
2831
2832 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
2833 (uint32_t *)base,
2834 (header->blocks * 512))) {
2835 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
2836 return -1;
2837 }
2838 fbcon_extract_to_screen(header, base);
2839 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002840 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002841
2842 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
2843 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
2844 return -1;
2845 }
2846
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302847 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08002848 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
2849 (uint32_t *)base,
2850 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302851 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302852 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002853 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002854 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302855 }
2856
Mao Flynn7b379f32015-04-20 00:28:30 +08002857 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302858}
2859
Mao Flynn7b379f32015-04-20 00:28:30 +08002860int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302861{
2862 int index = INVALID_PTN;
2863 unsigned long long ptn = 0;
2864 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08002865 struct logo_img_header *header;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302866
2867 index = partition_get_index("splash");
2868 if (index == 0) {
2869 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002870 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302871 }
2872
2873 ptn = partition_get_offset(index);
2874 if (ptn == 0) {
2875 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002876 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302877 }
2878
Mao Flynn7b379f32015-04-20 00:28:30 +08002879 if (mmc_read(ptn, (uint32_t *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302880 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002881 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302882 }
2883
Mao Flynn7b379f32015-04-20 00:28:30 +08002884 header = (struct logo_img_header *)logo_header;
2885 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302886 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002887 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302888 }
2889
2890 fb_display = fbcon_display();
2891 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002892 /* 1 RLE24 compressed data */
2893 if (header->type && (header->blocks != 0)) {
2894 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
2895
2896 /* if the logo is full-screen size, remove "fbcon_clear()" */
2897 if ((header->width != fb_display->width)
2898 || (header->height != fb_display->height))
2899 fbcon_clear();
2900
2901 if (mmc_read(ptn + LOGO_IMG_HEADER_SIZE,
2902 (uint32_t *)base,
2903 (header->blocks * 512))) {
2904 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
2905 return -1;
2906 }
2907 fbcon_extract_to_screen(header, base);
2908 return 0;
2909 }
2910
2911 /* 2 Raw BGR data */
2912 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302913 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002914 return -1;
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302915 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302916 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08002917 if (mmc_read(ptn + LOGO_IMG_HEADER_SIZE,
2918 (uint32_t *)base,
2919 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302920 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302921 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002922 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302923 }
2924
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302925 }
2926
Mao Flynn7b379f32015-04-20 00:28:30 +08002927 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302928}
2929
Mao Flynn7b379f32015-04-20 00:28:30 +08002930int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302931{
2932 if (target_is_emmc_boot()) {
2933 return splash_screen_mmc();
2934 } else {
2935 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002936 }
2937}
2938
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002939/* Get the size from partiton name */
2940static void get_partition_size(const char *arg, char *response)
2941{
2942 uint64_t ptn = 0;
2943 uint64_t size;
2944 int index = INVALID_PTN;
2945
2946 index = partition_get_index(arg);
2947
2948 if (index == INVALID_PTN)
2949 {
2950 dprintf(CRITICAL, "Invalid partition index\n");
2951 return;
2952 }
2953
2954 ptn = partition_get_offset(index);
2955
2956 if(!ptn)
2957 {
2958 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2959 return;
2960 }
2961
2962 size = partition_get_size(index);
2963
2964 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2965 return;
2966}
2967
2968/*
2969 * Publish the partition type & size info
2970 * fastboot getvar will publish the required information.
2971 * fastboot getvar partition_size:<partition_name>: partition size in hex
2972 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2973 */
2974static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2975{
2976 uint8_t i;
2977
2978 for (i = 0; i < num_parts; i++) {
2979 get_partition_size(info[i].part_name, info[i].size_response);
2980
2981 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2982 {
2983 dprintf(CRITICAL, "partition size name truncated\n");
2984 return;
2985 }
2986 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2987 {
2988 dprintf(CRITICAL, "partition type name truncated\n");
2989 return;
2990 }
2991
2992 /* publish partition size & type info */
2993 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2994 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2995 }
2996}
2997
Amol Jadi5edf3552013-07-23 14:15:34 -07002998/* register commands and variables for fastboot */
2999void aboot_fastboot_register_commands(void)
3000{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003001 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07003002
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003003 struct fastboot_cmd_desc cmd_list[] = {
3004 /* By default the enabled list is empty. */
3005 {"", NULL},
3006 /* move commands enclosed within the below ifndef to here
3007 * if they need to be enabled in user build.
3008 */
3009#ifndef DISABLE_FASTBOOT_CMDS
3010 /* Register the following commands only for non-user builds */
3011 {"flash:", cmd_flash},
3012 {"erase:", cmd_erase},
3013 {"boot", cmd_boot},
3014 {"continue", cmd_continue},
3015 {"reboot", cmd_reboot},
3016 {"reboot-bootloader", cmd_reboot_bootloader},
3017 {"oem unlock", cmd_oem_unlock},
vijay kumarc65876c2015-04-24 13:29:16 +05303018 {"oem unlock-go", cmd_oem_unlock_go},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003019 {"oem lock", cmd_oem_lock},
3020 {"oem verified", cmd_oem_verified},
3021 {"oem device-info", cmd_oem_devinfo},
3022 {"preflash", cmd_preflash},
3023 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3024 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08003025 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003026#endif
3027 };
3028
3029 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3030 for (i = 1; i < fastboot_cmds_count; i++)
3031 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3032
Amol Jadi5edf3552013-07-23 14:15:34 -07003033 /* publish variables and their values */
3034 fastboot_publish("product", TARGET(BOARD));
3035 fastboot_publish("kernel", "lk");
3036 fastboot_publish("serialno", sn_buf);
3037
3038 /*
3039 * partition info is supported only for emmc partitions
3040 * Calling this for NAND prints some error messages which
3041 * is harmless but misleading. Avoid calling this for NAND
3042 * devices.
3043 */
3044 if (target_is_emmc_boot())
3045 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3046
3047 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003048 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3049 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003050 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003051 /* Is the charger screen check enabled */
3052 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3053 device.charger_screen_enabled);
3054 fastboot_publish("charger-screen-enabled",
3055 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303056 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3057 device.display_panel);
3058 fastboot_publish("display-panel",
3059 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003060 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3061 fastboot_publish("version-baseband", (const char *) device.radio_version);
Amol Jadi5edf3552013-07-23 14:15:34 -07003062}
3063
Brian Swetland9c4c0752009-01-25 16:23:50 -08003064void aboot_init(const struct app_descriptor *app)
3065{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003066 unsigned reboot_mode = 0;
lijuang72875802015-02-10 10:35:12 +08003067 unsigned hard_reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03003068 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003069
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003070 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003071 if (target_is_emmc_boot())
3072 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003073 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003074 page_mask = page_size - 1;
3075 }
3076 else
3077 {
3078 page_size = flash_page_size();
3079 page_mask = page_size - 1;
3080 }
3081
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003082 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3083
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003084 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303085 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003086
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003087 /* Display splash screen if enabled */
3088#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003089#if NO_ALARM_DISPLAY
3090 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003091#endif
lijuang99c02d82015-02-13 19:04:34 +08003092 dprintf(SPEW, "Display Init: Start\n");
3093 target_display_init(device.display_panel);
3094 dprintf(SPEW, "Display Init: Done\n");
3095#if NO_ALARM_DISPLAY
3096 }
3097#endif
3098#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003099
Greg Griscod6250552011-06-29 14:40:23 -07003100 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003101 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003102
Dhaval Patel223ec952013-07-18 14:49:44 -07003103 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3104
Matthew Qindefd5562014-07-11 18:02:40 +08003105 /*
3106 * Check power off reason if user force reset,
3107 * if yes phone will do normal boot.
3108 */
3109 if (is_user_force_reset())
3110 goto normal_boot;
3111
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003112 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003113 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003114 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003115 dprintf(ALWAYS,"dload mode key sequence detected\n");
3116 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003117 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003118 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003119 }
3120 else
3121 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303122 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003123 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003124 }
3125 boot_into_fastboot = true;
3126 }
3127 if (!boot_into_fastboot)
3128 {
3129 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3130 boot_into_recovery = 1;
3131 if (!boot_into_recovery &&
3132 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003133 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003134 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003135 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003136 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003137 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003138 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003139
Ajay Dudani77421292010-10-27 19:34:06 -07003140 reboot_mode = check_reboot_mode();
lijuang72875802015-02-10 10:35:12 +08003141 hard_reboot_mode = check_hard_reboot_mode();
3142 if (reboot_mode == RECOVERY_MODE ||
3143 hard_reboot_mode == RECOVERY_HARD_RESET_MODE) {
Ajay Dudani77421292010-10-27 19:34:06 -07003144 boot_into_recovery = 1;
lijuang72875802015-02-10 10:35:12 +08003145 } else if(reboot_mode == FASTBOOT_MODE ||
3146 hard_reboot_mode == FASTBOOT_HARD_RESET_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003147 boot_into_fastboot = true;
lijuang72875802015-02-10 10:35:12 +08003148 } else if(reboot_mode == ALARM_BOOT ||
3149 hard_reboot_mode == RTC_HARD_RESET_MODE) {
Matthew Qind886f3c2014-01-17 16:52:01 +08003150 boot_reason_alarm = true;
Ajay Dudani77421292010-10-27 19:34:06 -07003151 }
3152
Matthew Qindefd5562014-07-11 18:02:40 +08003153normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003154 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003155 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003156 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003157 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003158 if(emmc_recovery_init())
3159 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3160 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003161 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003162 if((device.is_unlocked) || (device.is_tampered))
3163 {
3164 #ifdef TZ_TAMPER_FUSE
3165 set_tamper_fuse_cmd();
3166 #endif
3167 #if USE_PCOM_SECBOOT
3168 set_tamper_flag(device.is_tampered);
3169 #endif
3170 }
Shashank Mittala0032282011-08-26 14:50:11 -07003171 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003172
Pavel Nedev5d91d412013-04-29 11:34:24 +03003173 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003174 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003175 else
3176 {
3177 recovery_init();
3178 #if USE_PCOM_SECBOOT
3179 if((device.is_unlocked) || (device.is_tampered))
3180 set_tamper_flag(device.is_tampered);
3181 #endif
3182 boot_linux_from_flash();
3183 }
3184 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3185 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003186 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003187
Amol Jadi5edf3552013-07-23 14:15:34 -07003188 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003189
Amol Jadi5edf3552013-07-23 14:15:34 -07003190 /* register aboot specific fastboot commands */
3191 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003192
Amol Jadi5edf3552013-07-23 14:15:34 -07003193 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003194 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003195
3196 /* initialize and start fastboot */
3197 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08003198}
3199
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003200uint32_t get_page_size()
3201{
3202 return page_size;
3203}
3204
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003205/*
3206 * Calculated and save hash (SHA256) for non-signed boot image.
3207 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003208 * @param image_addr - Boot image address
3209 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003210 *
3211 * @return int - 0 on success, negative value on failure.
3212 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003213static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003214{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003215 unsigned int digest[8];
3216#if IMAGE_VERIF_ALGO_SHA1
3217 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3218#else
3219 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3220#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003221
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003222 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003223 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003224
3225 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003226 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003227
3228 return 0;
3229}
3230
Brian Swetland9c4c0752009-01-25 16:23:50 -08003231APP_START(aboot)
3232 .init = aboot_init,
3233APP_END