blob: 097cf8bcc3691e437cc99089080b52a28308bc1a [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Kuogee Hsieh55e92c52013-12-20 08:34:10 -08005 * Copyright (c) 2009-2014, 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>
43#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080044#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080045#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080046#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070047#include <target.h>
48#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070049#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070050#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070051#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070052#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070053#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030054#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070055#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070056#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070057#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053058#include <image_verify.h>
Dima Zavin214cc642009-01-26 11:16:21 -080059
Neeti Desai17379b82012-06-04 18:42:53 -070060#if DEVICE_TREE
61#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070062#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070063#endif
64
Shashank Mittalcd98d472011-08-02 14:29:24 -070065#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080066#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080067#include "bootimg.h"
68#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070069#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070070#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070071#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070072#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070073#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020074#include "mdtp.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070075
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070076extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070077extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070078extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070079extern int get_target_boot_params(const char *cmdline, const char *part,
80 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070081
82void write_device_info_mmc(device_info *dev);
83void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -070084static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070085
Sridhar Parasurame94e8152014-10-24 14:06:03 -070086/* fastboot command function pointer */
87typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
88
89struct fastboot_cmd_desc {
90 char * name;
91 fastboot_cmd_fn cb;
92};
93
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070094#define EXPAND(NAME) #NAME
95#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070096
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080097#ifdef MEMBASE
98#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
99#else
David Ng183a7422009-12-07 14:55:21 -0800100#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800101#endif
102
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700103#ifndef MEMSIZE
104#define MEMSIZE 1024*1024
105#endif
106
107#define MAX_TAGS_SIZE 1024
108
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800109#define RECOVERY_MODE 0x77665502
110#define FASTBOOT_MODE 0x77665500
Matthew Qind886f3c2014-01-17 16:52:01 +0800111#define ALARM_BOOT 0x77665503
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800112
Kun Liang2f1601a2013-08-12 16:29:54 +0800113/* make 4096 as default size to ensure EFS,EXT4's erasing */
114#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800115#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800116
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800117#define UBI_MAGIC "UBI#"
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700118#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800119#define UBI_MAGIC_SIZE 0x04
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700120#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800121
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800122#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
123
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700124#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
125
Smita Ghoshb4555da2014-09-18 14:14:56 -0700126#if UFS_SUPPORT || USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700127static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700128#else
David Ng183a7422009-12-07 14:55:21 -0800129static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700130#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800131static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300132static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800133static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800134static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800135static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700136static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300137static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800138
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800139static const char *baseband_apq = " androidboot.baseband=apq";
140static const char *baseband_msm = " androidboot.baseband=msm";
141static const char *baseband_csfb = " androidboot.baseband=csfb";
142static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700143static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800144static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700145static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800146static const char *baseband_dsda = " androidboot.baseband=dsda";
147static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800148static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800149static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800150
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700151static unsigned page_size = 0;
152static unsigned page_mask = 0;
153static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
154static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700155static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800156static bool boot_reason_alarm;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700157
Shashank Mittalcd98d472011-08-02 14:29:24 -0700158/* Assuming unauthorized kernel image by default */
159static int auth_kernel_img = 0;
160
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530161static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}};
Shashank Mittal162244e2011-08-08 19:01:25 -0700162
Dima Zavin42168f22009-01-30 11:52:22 -0800163struct atag_ptbl_entry
164{
165 char name[16];
166 unsigned offset;
167 unsigned size;
168 unsigned flags;
169};
170
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700171/*
172 * Partition info, required to be published
173 * for fastboot
174 */
175struct getvar_partition_info {
176 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
177 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
178 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
179 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
180 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
181};
182
183/*
184 * Right now, we are publishing the info for only
185 * three partitions
186 */
187struct getvar_partition_info part_info[] =
188{
189 { "system" , "partition-size:", "partition-type:", "", "ext4" },
190 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
191 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
192};
193
194char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700195char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800196char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700197char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530198char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700199
Greg Griscod2471ef2011-07-14 13:00:42 -0700200extern int emmc_recovery_init(void);
201
Kinson Chik0b1c8162011-08-31 16:31:57 -0700202#if NO_KEYPAD_DRIVER
203extern int fastboot_trigger(void);
204#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700205
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800206static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700207{
208 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700209#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800210 if (is_arm64)
211 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
212 else
213 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700214 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
215 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700216#endif
217}
218
Dima Zavin42168f22009-01-30 11:52:22 -0800219static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
220{
221 struct atag_ptbl_entry atag_ptn;
222
223 memcpy(atag_ptn.name, ptn->name, 16);
224 atag_ptn.name[15] = '\0';
225 atag_ptn.offset = ptn->start;
226 atag_ptn.size = ptn->length;
227 atag_ptn.flags = ptn->flags;
228 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
229 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
230}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800231
Neeti Desaie245d492012-06-01 12:52:13 -0700232unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800233{
David Ng183a7422009-12-07 14:55:21 -0800234 int cmdline_len = 0;
235 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800236 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700237 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800238 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300239 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700240 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700241 char *boot_dev_buf = NULL;
Dima Zavin42168f22009-01-30 11:52:22 -0800242
Brian Swetland9c4c0752009-01-25 16:23:50 -0800243 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800244 cmdline_len = strlen(cmdline);
245 have_cmdline = 1;
246 }
247 if (target_is_emmc_boot()) {
248 cmdline_len += strlen(emmc_cmdline);
Smita Ghoshb4555da2014-09-18 14:14:56 -0700249#if UFS_SUPPORT || USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700250 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
251 ASSERT(boot_dev_buf);
252 platform_boot_dev_cmdline(boot_dev_buf);
253 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700254#endif
David Ng183a7422009-12-07 14:55:21 -0800255 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800256
257 cmdline_len += strlen(usb_sn_cmdline);
258 cmdline_len += strlen(sn_buf);
259
Pavel Nedev5614d222013-06-17 18:01:02 +0300260 if (boot_into_recovery && gpt_exists)
261 cmdline_len += strlen(secondary_gpt_enable);
262
Pavel Nedev328ac822013-04-05 15:25:11 +0300263 if (boot_into_ffbm) {
264 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700265 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800266 /* reduce kernel console messages to speed-up boot */
267 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800268 } else if (boot_reason_alarm) {
269 cmdline_len += strlen(alarmboot_cmdline);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700270 } else if (device.charger_screen_enabled &&
271 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700272 pause_at_bootup = 1;
273 cmdline_len += strlen(battchg_pause);
274 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800275
Shashank Mittalcd98d472011-08-02 14:29:24 -0700276 if(target_use_signed_kernel() && auth_kernel_img) {
277 cmdline_len += strlen(auth_kernel);
278 }
279
Joonwoo Park61112782013-10-02 19:50:39 -0700280 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
281 "system",
282 target_boot_params,
283 sizeof(target_boot_params)) == 0) {
284 have_target_boot_params = 1;
285 cmdline_len += strlen(target_boot_params);
286 }
287
Ajay Dudanid04110c2011-01-17 23:55:07 -0800288 /* Determine correct androidboot.baseband to use */
289 switch(target_baseband())
290 {
291 case BASEBAND_APQ:
292 cmdline_len += strlen(baseband_apq);
293 break;
294
295 case BASEBAND_MSM:
296 cmdline_len += strlen(baseband_msm);
297 break;
298
299 case BASEBAND_CSFB:
300 cmdline_len += strlen(baseband_csfb);
301 break;
302
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800303 case BASEBAND_SVLTE2A:
304 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800305 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700306
307 case BASEBAND_MDM:
308 cmdline_len += strlen(baseband_mdm);
309 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700310
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800311 case BASEBAND_MDM2:
312 cmdline_len += strlen(baseband_mdm2);
313 break;
314
Amol Jadi5c61a952012-05-04 17:05:35 -0700315 case BASEBAND_SGLTE:
316 cmdline_len += strlen(baseband_sglte);
317 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530318
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800319 case BASEBAND_SGLTE2:
320 cmdline_len += strlen(baseband_sglte2);
321 break;
322
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530323 case BASEBAND_DSDA:
324 cmdline_len += strlen(baseband_dsda);
325 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800326
327 case BASEBAND_DSDA2:
328 cmdline_len += strlen(baseband_dsda2);
329 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800330 }
331
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800332 if (cmdline) {
333 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
334 target_display_panel_node(device.display_panel,
335 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
336 strlen(display_panel_buf)) {
337 cmdline_len += strlen(display_panel_buf);
338 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700339 }
340
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800341 if (target_warm_boot()) {
342 warm_boot = true;
343 cmdline_len += strlen(warmboot_cmdline);
344 }
345
David Ng183a7422009-12-07 14:55:21 -0800346 if (cmdline_len > 0) {
347 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800348 unsigned char *dst;
349
350 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
351 ASSERT(cmdline_final != NULL);
352 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700353
Amol Jadi168b7712012-03-06 16:15:00 -0800354 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800355 if (have_cmdline) {
356 src = cmdline;
357 while ((*dst++ = *src++));
358 }
359 if (target_is_emmc_boot()) {
360 src = emmc_cmdline;
361 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700362 have_cmdline = 1;
363 while ((*dst++ = *src++));
Smita Ghoshb4555da2014-09-18 14:14:56 -0700364#if UFS_SUPPORT || USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700365 src = boot_dev_buf;
366 if (have_cmdline) --dst;
367 while ((*dst++ = *src++));
368#endif
David Ngf773dde2010-07-26 19:55:08 -0700369 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800370
371 src = usb_sn_cmdline;
372 if (have_cmdline) --dst;
373 have_cmdline = 1;
374 while ((*dst++ = *src++));
375 src = sn_buf;
376 if (have_cmdline) --dst;
377 have_cmdline = 1;
378 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800379 if (warm_boot) {
380 if (have_cmdline) --dst;
381 src = warmboot_cmdline;
382 while ((*dst++ = *src++));
383 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800384
Pavel Nedev5614d222013-06-17 18:01:02 +0300385 if (boot_into_recovery && gpt_exists) {
386 src = secondary_gpt_enable;
387 if (have_cmdline) --dst;
388 while ((*dst++ = *src++));
389 }
390
Pavel Nedev328ac822013-04-05 15:25:11 +0300391 if (boot_into_ffbm) {
392 src = androidboot_mode;
393 if (have_cmdline) --dst;
394 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700395 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300396 if (have_cmdline) --dst;
397 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800398 src = loglevel;
399 if (have_cmdline) --dst;
400 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800401 } else if (boot_reason_alarm) {
402 src = alarmboot_cmdline;
403 if (have_cmdline) --dst;
404 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300405 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700406 src = battchg_pause;
407 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800408 while ((*dst++ = *src++));
409 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800410
Shashank Mittalcd98d472011-08-02 14:29:24 -0700411 if(target_use_signed_kernel() && auth_kernel_img) {
412 src = auth_kernel;
413 if (have_cmdline) --dst;
414 while ((*dst++ = *src++));
415 }
416
Ajay Dudanid04110c2011-01-17 23:55:07 -0800417 switch(target_baseband())
418 {
419 case BASEBAND_APQ:
420 src = baseband_apq;
421 if (have_cmdline) --dst;
422 while ((*dst++ = *src++));
423 break;
424
425 case BASEBAND_MSM:
426 src = baseband_msm;
427 if (have_cmdline) --dst;
428 while ((*dst++ = *src++));
429 break;
430
431 case BASEBAND_CSFB:
432 src = baseband_csfb;
433 if (have_cmdline) --dst;
434 while ((*dst++ = *src++));
435 break;
436
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800437 case BASEBAND_SVLTE2A:
438 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800439 if (have_cmdline) --dst;
440 while ((*dst++ = *src++));
441 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700442
443 case BASEBAND_MDM:
444 src = baseband_mdm;
445 if (have_cmdline) --dst;
446 while ((*dst++ = *src++));
447 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700448
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800449 case BASEBAND_MDM2:
450 src = baseband_mdm2;
451 if (have_cmdline) --dst;
452 while ((*dst++ = *src++));
453 break;
454
Amol Jadi5c61a952012-05-04 17:05:35 -0700455 case BASEBAND_SGLTE:
456 src = baseband_sglte;
457 if (have_cmdline) --dst;
458 while ((*dst++ = *src++));
459 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530460
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800461 case BASEBAND_SGLTE2:
462 src = baseband_sglte2;
463 if (have_cmdline) --dst;
464 while ((*dst++ = *src++));
465 break;
466
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530467 case BASEBAND_DSDA:
468 src = baseband_dsda;
469 if (have_cmdline) --dst;
470 while ((*dst++ = *src++));
471 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800472
473 case BASEBAND_DSDA2:
474 src = baseband_dsda2;
475 if (have_cmdline) --dst;
476 while ((*dst++ = *src++));
477 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800478 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700479
480 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700481 src = display_panel_buf;
482 if (have_cmdline) --dst;
483 while ((*dst++ = *src++));
484 }
Joonwoo Park61112782013-10-02 19:50:39 -0700485
486 if (have_target_boot_params) {
487 if (have_cmdline) --dst;
488 src = target_boot_params;
489 while ((*dst++ = *src++));
490 }
Neeti Desaie245d492012-06-01 12:52:13 -0700491 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700492
493
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700494 if (boot_dev_buf)
495 free(boot_dev_buf);
496
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800497 if (cmdline_final)
498 dprintf(INFO, "cmdline: %s\n", cmdline_final);
499 else
500 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700501 return cmdline_final;
502}
503
504unsigned *atag_core(unsigned *ptr)
505{
506 /* CORE */
507 *ptr++ = 2;
508 *ptr++ = 0x54410001;
509
510 return ptr;
511
512}
513
514unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
515 unsigned ramdisk_size)
516{
517 if (ramdisk_size) {
518 *ptr++ = 4;
519 *ptr++ = 0x54420005;
520 *ptr++ = (unsigned)ramdisk;
521 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800522 }
523
Neeti Desaie245d492012-06-01 12:52:13 -0700524 return ptr;
525}
526
527unsigned *atag_ptable(unsigned **ptr_addr)
528{
529 int i;
530 struct ptable *ptable;
531
532 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700533 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
534 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700535 *(*ptr_addr)++ = 0x4d534d70;
536 for (i = 0; i < ptable->count; ++i)
537 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
538 }
539
540 return (*ptr_addr);
541}
542
543unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
544{
545 int cmdline_length = 0;
546 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700547 char *dest;
548
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800549 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700550 n = (cmdline_length + 4) & (~3);
551
552 *ptr++ = (n / 4) + 2;
553 *ptr++ = 0x54410009;
554 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800555 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700556 ptr += (n / 4);
557
558 return ptr;
559}
560
561unsigned *atag_end(unsigned *ptr)
562{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800563 /* END */
564 *ptr++ = 0;
565 *ptr++ = 0;
566
Neeti Desaie245d492012-06-01 12:52:13 -0700567 return ptr;
568}
569
570void generate_atags(unsigned *ptr, const char *cmdline,
571 void *ramdisk, unsigned ramdisk_size)
572{
573
574 ptr = atag_core(ptr);
575 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
576 ptr = target_atag_mem(ptr);
577
578 /* Skip NAND partition ATAGS for eMMC boot */
579 if (!target_is_emmc_boot()){
580 ptr = atag_ptable(&ptr);
581 }
582
583 ptr = atag_cmdline(ptr, cmdline);
584 ptr = atag_end(ptr);
585}
586
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700587typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700588void boot_linux(void *kernel, unsigned *tags,
589 const char *cmdline, unsigned machtype,
590 void *ramdisk, unsigned ramdisk_size)
591{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800592 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800593#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700594 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800595#endif
596
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700597 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800598 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800599 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800600
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530601 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700602
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800603 final_cmdline = update_cmdline((const char*)cmdline);
604
Neeti Desai17379b82012-06-04 18:42:53 -0700605#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800606 dprintf(INFO, "Updating device tree: start\n");
607
Neeti Desai17379b82012-06-04 18:42:53 -0700608 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530609 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700610 if(ret)
611 {
612 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
613 ASSERT(0);
614 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800615 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700616#else
Neeti Desaie245d492012-06-01 12:52:13 -0700617 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800618 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700619#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700620
Maria Yu52254c02014-07-04 16:14:54 +0800621 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700622
623#if VERIFIED_BOOT
624 /* Write protect the device info */
625 if (mmc_write_protect("devinfo", 1))
626 {
627 dprintf(INFO, "Failed to write protect dev info\n");
628 ASSERT(0);
629 }
630#endif
631
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700632 /* Perform target specific cleanup */
633 target_uninit();
634
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800635 /* Turn off splash screen if enabled */
636#if DISPLAY_SPLASH_SCREEN
637 target_display_shutdown();
638#endif
639
640
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800641 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530642 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800643
644 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700645
Amol Jadi4421e652011-06-16 15:00:48 -0700646 /* do any platform specific cleanup before kernel entry */
647 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700648
Brian Swetland9c4c0752009-01-25 16:23:50 -0800649 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700650
Amol Jadi504f9fe2012-08-16 13:56:48 -0700651#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800652 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700653#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700654 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800655
656 if (IS_ARM64(kptr))
657 /* Jump to a 64bit kernel */
658 scm_elexec_call((paddr_t)kernel, tags_phys);
659 else
660 /* Jump to a 32bit kernel */
661 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800662}
663
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700664/* Function to check if the memory address range falls within the aboot
665 * boundaries.
666 * start: Start of the memory region
667 * size: Size of the memory region
668 */
669int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
670{
671 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800672 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700673 return -1;
674
675 /* Check for memory overlap. */
676 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
677 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700678 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700679 return 0;
680 else
681 return -1;
682}
683
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800684#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800685
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800686BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800687#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800688BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800689#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800690
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700691static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
692{
693 int ret;
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800694#if IMAGE_VERIF_ALGO_SHA1
695 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
696#else
697 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
698#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700699
700 /* Assume device is rooted at this time. */
701 device.is_tampered = 1;
702
703 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
704
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700705#if VERIFIED_BOOT
706 if(boot_into_recovery)
707 {
708 ret = boot_verify_image((unsigned char *)bootimg_addr,
709 bootimg_size, "recovery");
710 }
711 else
712 {
713 ret = boot_verify_image((unsigned char *)bootimg_addr,
714 bootimg_size, "boot");
715 }
716 boot_verify_print_state();
717#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700718 ret = image_verify((unsigned char *)bootimg_addr,
719 (unsigned char *)(bootimg_addr + bootimg_size),
720 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800721 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700722#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700723 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
724
725 if (ret)
726 {
727 /* Authorized kernel */
728 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700729 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700730 }
731
732#if USE_PCOM_SECBOOT
733 set_tamper_flag(device.is_tampered);
734#endif
735
736 if(device.is_tampered)
737 {
738 write_device_info_mmc(&device);
739 #ifdef TZ_TAMPER_FUSE
740 set_tamper_fuse_cmd();
741 #endif
742 #ifdef ASSERT_ON_TAMPER
743 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
744 ASSERT(0);
745 #endif
746 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700747
748#if VERIFIED_BOOT
749 if(boot_verify_get_state() == RED)
750 {
751 if(!boot_into_recovery)
752 {
753 dprintf(CRITICAL,
754 "Device verification failed. Rebooting into recovery.\n");
755 reboot_device(RECOVERY_MODE);
756 }
757 else
758 {
759 dprintf(CRITICAL,
760 "Recovery image verification failed. Asserting..\n");
761 ASSERT(0);
762 }
763 }
764#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700765}
766
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530767static bool check_format_bit()
768{
769 bool ret = false;
770 int index;
771 uint64_t offset;
772 struct boot_selection_info *in = NULL;
773 char *buf = NULL;
774
775 index = partition_get_index("bootselect");
776 if (index == INVALID_PTN)
777 {
778 dprintf(INFO, "Unable to locate /bootselect partition\n");
779 return ret;
780 }
781 offset = partition_get_offset(index);
782 if(!offset)
783 {
784 dprintf(INFO, "partition /bootselect doesn't exist\n");
785 return ret;
786 }
787 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
788 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530789 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530790 {
791 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
792 free(buf);
793 return ret;
794 }
795 in = (struct boot_selection_info *) buf;
796 if ((in->signature == BOOTSELECT_SIGNATURE) &&
797 (in->version == BOOTSELECT_VERSION)) {
798 if ((in->state_info & BOOTSELECT_FORMAT) &&
799 !(in->state_info & BOOTSELECT_FACTORY))
800 ret = true;
801 } else {
802 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
803 in->signature, in->version);
804 ASSERT(0);
805 }
806 free(buf);
807 return ret;
808}
809
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700810void boot_verifier_init()
811{
812
813 uint32_t boot_state;
814 /* Check if device unlock */
815 if(device.is_unlocked)
816 {
817 boot_verify_send_event(DEV_UNLOCK);
818 boot_verify_print_state();
819 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
820 return;
821 }
822 else
823 {
824 boot_verify_send_event(BOOT_INIT);
825 }
826
827 /* Initialize keystore */
828 boot_state = boot_verify_keystore_init();
829 if(boot_state == YELLOW)
830 {
831 boot_verify_print_state();
832 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
833 }
834}
835
Shashank Mittal23b8f422010-04-16 19:27:21 -0700836int boot_linux_from_mmc(void)
837{
838 struct boot_img_hdr *hdr = (void*) buf;
839 struct boot_img_hdr *uhdr;
840 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700841 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700842 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700843 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700844
Shashank Mittalcd98d472011-08-02 14:29:24 -0700845 unsigned char *image_addr = 0;
846 unsigned kernel_actual;
847 unsigned ramdisk_actual;
848 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700849 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700850
851#if DEVICE_TREE
852 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700853 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700854 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800855 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700856 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700857#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800858 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
859 struct kernel64_hdr *kptr = (void*) kbuf;
860
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530861 if (check_format_bit())
862 boot_into_recovery = 1;
863
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700864 if (!boot_into_recovery) {
865 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
866 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
867 if (rcode <= 0) {
868 boot_into_ffbm = false;
869 if (rcode < 0)
870 dprintf(CRITICAL,"failed to get ffbm cookie");
871 } else
872 boot_into_ffbm = true;
873 } else
874 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700875 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
876 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
877 dprintf(INFO, "Unified boot method!\n");
878 hdr = uhdr;
879 goto unified_boot;
880 }
Greg Griscod6250552011-06-29 14:40:23 -0700881 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700882 index = partition_get_index("boot");
883 ptn = partition_get_offset(index);
884 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700885 dprintf(CRITICAL, "ERROR: No boot partition found\n");
886 return -1;
887 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700888 }
889 else {
890 index = partition_get_index("recovery");
891 ptn = partition_get_offset(index);
892 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700893 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
894 return -1;
895 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700896 }
897
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530898 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700899 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
900 return -1;
901 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700902
903 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700904 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700905 return -1;
906 }
907
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700908 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530909
910 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
911 dprintf(CRITICAL, "ERROR: Invalid page size\n");
912 return -1;
913 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700914 page_size = hdr->page_size;
915 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700916 }
917
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800918 /* Read the next page to get kernel Image header
919 * which lives in the second page for arm64 targets.
920 */
921
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530922 if (mmc_read(ptn + page_size, (uint32_t *) kbuf, page_size)) {
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800923 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
924 return -1;
925 }
926
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700927 /*
928 * Update the kernel/ramdisk/tags address if the boot image header
929 * has default values, these default values come from mkbootimg when
930 * the boot image is flashed using fastboot flash:raw
931 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800932 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700933
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700934 /* Get virtual addresses since the hdr saves physical addresses. */
935 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
936 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
937 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700938
939 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
940 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
941
942 /* Check if the addresses in the header are valid. */
943 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
944 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
945 {
946 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
947 return -1;
948 }
949
950#ifndef DEVICE_TREE
951 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
952 {
953 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
954 return -1;
955 }
956#endif
957
Shashank Mittalcd98d472011-08-02 14:29:24 -0700958 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300959 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
960 (int) target_use_signed_kernel(),
961 device.is_unlocked,
962 device.is_tampered);
963
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700964#if VERIFIED_BOOT
965 boot_verifier_init();
966#endif
967
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700968 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800969 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700970 offset = 0;
971
Shashank Mittalcd98d472011-08-02 14:29:24 -0700972 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700973
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800974#if DEVICE_TREE
975 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
976 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700977
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700978 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700979 {
980 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
981 return -1;
982 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800983#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530984 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700985
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700986#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700987
Amol Jadib6be5c12012-11-14 13:39:51 -0800988 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700989 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800990
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530991 if (check_aboot_addr_range_overlap((uint32_t)image_addr, imagesize_actual))
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700992 {
993 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
994 return -1;
995 }
996
Shashank Mittalcd98d472011-08-02 14:29:24 -0700997 /* Read image without signature */
998 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
999 {
1000 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1001 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -08001002 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001003
Amol Jadib6be5c12012-11-14 13:39:51 -08001004 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001005 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001006
Shashank Mittalcd98d472011-08-02 14:29:24 -07001007 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -07001008
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301009 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -07001010 {
1011 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1012 return -1;
1013 }
1014
Shashank Mittalcd98d472011-08-02 14:29:24 -07001015 /* Read signature */
1016 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1017 {
1018 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001019 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001020 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001021
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301022 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001023
Neeti Desai465491e2012-07-31 12:53:35 -07001024 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -07001025 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1026 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -07001027
Neeti Desai465491e2012-07-31 12:53:35 -07001028 #if DEVICE_TREE
1029 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001030 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -07001031 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001032
Deepa Dinamani19648b42013-09-05 17:05:55 -07001033 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001034 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1035 return -1;
1036 }
Deepa Dinamani19648b42013-09-05 17:05:55 -07001037
Neeti Desai465491e2012-07-31 12:53:35 -07001038 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -07001039 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001040 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
1041 return -1;
1042 }
1043
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001044 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001045 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001046 {
1047 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1048 return -1;
1049 }
1050
Joel Kingaa335dc2013-06-03 16:11:08 -07001051 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001052 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -08001053 /*
1054 * If appended dev tree is found, update the atags with
1055 * memory address to the DTB appended location on RAM.
1056 * Else update with the atags address in the kernel header
1057 */
1058 void *dtb;
1059 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001060 hdr->kernel_size,
1061 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001062 if (!dtb) {
1063 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1064 return -1;
1065 }
Neeti Desai465491e2012-07-31 12:53:35 -07001066 }
1067 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001068 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001069 else
1070 {
Amol Jadib6be5c12012-11-14 13:39:51 -08001071 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001072
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001073 image_addr = (unsigned char *)target_get_scratch_address();
1074#if DEVICE_TREE
1075 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1076 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1077
1078 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
1079 {
1080 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1081 return -1;
1082 }
1083#else
1084 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1085
1086#endif
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001087 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001088 {
1089 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1090 return -1;
1091 }
1092
Amol Jadib6be5c12012-11-14 13:39:51 -08001093 dprintf(INFO, "Loading boot image (%d): start\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001094 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001095 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001096
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001097 offset = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001098
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001099 /* Load the entire boot image */
1100 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual)) {
1101 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
Shashank Mittalcd98d472011-08-02 14:29:24 -07001102 return -1;
1103 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001104
1105 dprintf(INFO, "Loading boot image (%d): done\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001106 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001107 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -07001108
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001109 #ifdef TZ_SAVE_KERNEL_HASH
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001110 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001111 #endif /* TZ_SAVE_KERNEL_HASH */
1112
1113 /* Move kernel, ramdisk and device tree to correct address */
1114 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1115 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Neeti Desai465491e2012-07-31 12:53:35 -07001116
1117 #if DEVICE_TREE
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001118 if(hdr->dt_size) {
1119 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1120 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001121
Deepa Dinamani19648b42013-09-05 17:05:55 -07001122 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001123 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1124 return -1;
1125 }
1126
Joel Kingaa335dc2013-06-03 16:11:08 -07001127 /* Find index of device tree within device tree table */
1128 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001129 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1130 return -1;
1131 }
1132
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001133 /* Validate and Read device device tree in the tags_addr */
Joel Kingaa335dc2013-06-03 16:11:08 -07001134 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001135 {
1136 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1137 return -1;
1138 }
1139
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001140 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001141 } else {
Aparna Mallavarapu33127372014-06-03 22:15:54 +05301142 /* Validate the tags_addr */
1143 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1144 {
1145 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1146 return -1;
1147 }
Dima Zavin77e41f32013-03-06 16:10:43 -08001148 /*
1149 * If appended dev tree is found, update the atags with
1150 * memory address to the DTB appended location on RAM.
1151 * Else update with the atags address in the kernel header
1152 */
1153 void *dtb;
1154 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001155 kernel_actual,
1156 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001157 if (!dtb) {
1158 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1159 return -1;
1160 }
Neeti Desai465491e2012-07-31 12:53:35 -07001161 }
1162 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001163 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001164
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001165 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1166 target_load_ssd_keystore();
1167
Shashank Mittal23b8f422010-04-16 19:27:21 -07001168unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001169
Dima Zavin77e41f32013-03-06 16:10:43 -08001170 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001171 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001172 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1173
1174 return 0;
1175}
1176
Dima Zavin214cc642009-01-26 11:16:21 -08001177int boot_linux_from_flash(void)
1178{
1179 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001180 struct ptentry *ptn;
1181 struct ptable *ptable;
1182 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001183
Shashank Mittalcd98d472011-08-02 14:29:24 -07001184 unsigned char *image_addr = 0;
1185 unsigned kernel_actual;
1186 unsigned ramdisk_actual;
1187 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001188 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001189
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001190#if DEVICE_TREE
1191 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001192 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001193 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001194 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001195#endif
1196
David Ng183a7422009-12-07 14:55:21 -08001197 if (target_is_emmc_boot()) {
1198 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1199 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1200 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1201 return -1;
1202 }
1203 goto continue_boot;
1204 }
1205
Dima Zavin214cc642009-01-26 11:16:21 -08001206 ptable = flash_get_ptable();
1207 if (ptable == NULL) {
1208 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1209 return -1;
1210 }
1211
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001212 if(!boot_into_recovery)
1213 {
1214 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001215
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001216 if (ptn == NULL) {
1217 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1218 return -1;
1219 }
1220 }
1221 else
1222 {
1223 ptn = ptable_find(ptable, "recovery");
1224 if (ptn == NULL) {
1225 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1226 return -1;
1227 }
Dima Zavin214cc642009-01-26 11:16:21 -08001228 }
1229
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001230 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001231 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1232 return -1;
1233 }
Dima Zavin214cc642009-01-26 11:16:21 -08001234
1235 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001236 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001237 return -1;
1238 }
1239
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001240 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001241 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 -08001242 return -1;
1243 }
1244
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001245 /*
1246 * Update the kernel/ramdisk/tags address if the boot image header
1247 * has default values, these default values come from mkbootimg when
1248 * the boot image is flashed using fastboot flash:raw
1249 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001250 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001251
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001252 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001253 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1254 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1255 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1256
1257 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1258 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1259
1260 /* Check if the addresses in the header are valid. */
1261 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1262 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1263 {
1264 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1265 return -1;
1266 }
1267
1268#ifndef DEVICE_TREE
1269 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1270 {
1271 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1272 return -1;
1273 }
1274#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001275
Shashank Mittalcd98d472011-08-02 14:29:24 -07001276 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001277 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001278 {
1279 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001280 offset = 0;
1281
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001282#if DEVICE_TREE
1283 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1284 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001285
1286 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1287 {
1288 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1289 return -1;
1290 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001291#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001292 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001293#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001294
Amol Jadib6be5c12012-11-14 13:39:51 -08001295 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001296 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001297
Shashank Mittalcd98d472011-08-02 14:29:24 -07001298 /* Read image without signature */
1299 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1300 {
1301 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1302 return -1;
1303 }
Dima Zavin214cc642009-01-26 11:16:21 -08001304
Amol Jadib6be5c12012-11-14 13:39:51 -08001305 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001306 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001307
Shashank Mittalcd98d472011-08-02 14:29:24 -07001308 offset = imagesize_actual;
1309 /* Read signature */
1310 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1311 {
1312 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001313 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001314 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001315
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301316 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001317
1318 /* Move kernel and ramdisk to correct address */
1319 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1320 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001321#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001322 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001323 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001324 {
1325 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1326 return -1;
1327 }
1328
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001329 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1330#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001331
1332 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001333 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001334 {
1335 write_device_info_flash(&device);
1336 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301337#if USE_PCOM_SECBOOT
1338 set_tamper_flag(device.is_tampered);
1339#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001340 }
1341 else
1342 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001343 offset = page_size;
1344
Amol Jadib6be5c12012-11-14 13:39:51 -08001345 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1346 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1347 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1348
1349 dprintf(INFO, "Loading boot image (%d): start\n",
1350 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001351 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001352
1353 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001354 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1355 return -1;
1356 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001357 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001358
Amol Jadib6be5c12012-11-14 13:39:51 -08001359 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001360 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1361 return -1;
1362 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001363 offset += ramdisk_actual;
1364
1365 dprintf(INFO, "Loading boot image (%d): done\n",
1366 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001367 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001368
1369 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001370 offset += second_actual;
1371 /* Second image loading not implemented. */
1372 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001373 }
1374
1375#if DEVICE_TREE
1376 if(hdr->dt_size != 0) {
1377
1378 /* Read the device tree table into buffer */
1379 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1380 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1381 return -1;
1382 }
1383
1384 table = (struct dt_table*) dt_buf;
1385
Deepa Dinamani19648b42013-09-05 17:05:55 -07001386 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001387 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1388 return -1;
1389 }
1390
Deepa Dinamani19648b42013-09-05 17:05:55 -07001391 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1392 if (!table)
1393 return -1;
1394
1395 /* Read the entire device tree table into buffer */
1396 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1397 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1398 return -1;
1399 }
1400
1401
Joel Kingaa335dc2013-06-03 16:11:08 -07001402 /* Find index of device tree within device tree table */
1403 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001404 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1405 return -1;
1406 }
1407
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001408 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001409 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001410 {
1411 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1412 return -1;
1413 }
1414
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001415 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001416 if(flash_read(ptn, offset + dt_entry.offset,
1417 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001418 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1419 return -1;
1420 }
1421 }
1422#endif
1423
Shashank Mittalcd98d472011-08-02 14:29:24 -07001424 }
David Ng183a7422009-12-07 14:55:21 -08001425continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001426
Dima Zavin214cc642009-01-26 11:16:21 -08001427 /* TODO: create/pass atags to kernel */
1428
Ajay Dudanie28a6072011-07-01 13:59:46 -07001429 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001430 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001431 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1432
1433 return 0;
1434}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001435
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001436BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001437void write_device_info_mmc(device_info *dev)
1438{
1439 struct device_info *info = (void*) info_buf;
1440 unsigned long long ptn = 0;
1441 unsigned long long size;
1442 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001443 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001444 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001445
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001446#if VERIFIED_BOOT
1447 index = partition_get_index("devinfo");
1448#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001449 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001450#endif
1451
Shashank Mittal162244e2011-08-08 19:01:25 -07001452 ptn = partition_get_offset(index);
1453 if(ptn == 0)
1454 {
1455 return;
1456 }
1457
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001458 lun = partition_get_lun(index);
1459 mmc_set_lun(lun);
1460
Shashank Mittal162244e2011-08-08 19:01:25 -07001461 size = partition_get_size(index);
1462
1463 memcpy(info, dev, sizeof(device_info));
1464
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001465 blocksize = mmc_get_device_blocksize();
1466
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001467#if VERIFIED_BOOT
1468 if(mmc_write(ptn, blocksize, (void *)info_buf))
1469#else
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001470 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001471#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001472 {
1473 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1474 return;
1475 }
1476}
1477
1478void read_device_info_mmc(device_info *dev)
1479{
1480 struct device_info *info = (void*) info_buf;
1481 unsigned long long ptn = 0;
1482 unsigned long long size;
1483 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001484 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001485
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001486#if VERIFIED_BOOT
1487 index = partition_get_index("devinfo");
1488#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001489 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001490#endif
1491
Shashank Mittal162244e2011-08-08 19:01:25 -07001492 ptn = partition_get_offset(index);
1493 if(ptn == 0)
1494 {
1495 return;
1496 }
1497
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001498 mmc_set_lun(partition_get_lun(index));
1499
Shashank Mittal162244e2011-08-08 19:01:25 -07001500 size = partition_get_size(index);
1501
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001502 blocksize = mmc_get_device_blocksize();
1503
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001504#if VERIFIED_BOOT
1505 if(mmc_read(ptn, (void *)info_buf, blocksize))
1506#else
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001507 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001508#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001509 {
1510 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1511 return;
1512 }
1513
1514 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1515 {
1516 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
Shashank Mittal7e033062014-09-25 19:58:07 -07001517#if DEFAULT_UNLOCK
1518 info->is_unlocked = 1;
1519#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001520 info->is_unlocked = 0;
Shashank Mittal7e033062014-09-25 19:58:07 -07001521#endif
1522 info->is_verified = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001523 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001524 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001525
1526 write_device_info_mmc(info);
1527 }
1528 memcpy(dev, info, sizeof(device_info));
1529}
1530
1531void write_device_info_flash(device_info *dev)
1532{
1533 struct device_info *info = (void *) info_buf;
1534 struct ptentry *ptn;
1535 struct ptable *ptable;
1536
1537 ptable = flash_get_ptable();
1538 if (ptable == NULL)
1539 {
1540 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1541 return;
1542 }
1543
1544 ptn = ptable_find(ptable, "devinfo");
1545 if (ptn == NULL)
1546 {
1547 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1548 return;
1549 }
1550
1551 memcpy(info, dev, sizeof(device_info));
1552
1553 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1554 {
1555 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1556 return;
1557 }
1558}
1559
1560void read_device_info_flash(device_info *dev)
1561{
1562 struct device_info *info = (void*) info_buf;
1563 struct ptentry *ptn;
1564 struct ptable *ptable;
1565
1566 ptable = flash_get_ptable();
1567 if (ptable == NULL)
1568 {
1569 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1570 return;
1571 }
1572
1573 ptn = ptable_find(ptable, "devinfo");
1574 if (ptn == NULL)
1575 {
1576 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1577 return;
1578 }
1579
1580 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1581 {
1582 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1583 return;
1584 }
1585
1586 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1587 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001588 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1589 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001590 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001591 write_device_info_flash(info);
1592 }
1593 memcpy(dev, info, sizeof(device_info));
1594}
1595
1596void write_device_info(device_info *dev)
1597{
1598 if(target_is_emmc_boot())
1599 {
1600 write_device_info_mmc(dev);
1601 }
1602 else
1603 {
1604 write_device_info_flash(dev);
1605 }
1606}
1607
1608void read_device_info(device_info *dev)
1609{
1610 if(target_is_emmc_boot())
1611 {
1612 read_device_info_mmc(dev);
1613 }
1614 else
1615 {
1616 read_device_info_flash(dev);
1617 }
1618}
1619
1620void reset_device_info()
1621{
1622 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001623 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001624 write_device_info(&device);
1625}
1626
1627void set_device_root()
1628{
1629 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001630 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001631 write_device_info(&device);
1632}
1633
Amol Jadicb524072012-08-09 16:40:18 -07001634#if DEVICE_TREE
1635int copy_dtb(uint8_t *boot_image_start)
1636{
1637 uint32 dt_image_offset = 0;
1638 uint32_t n;
1639 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001640 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001641 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001642
1643 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1644
Amol Jadicb524072012-08-09 16:40:18 -07001645 if(hdr->dt_size != 0) {
1646
1647 /* add kernel offset */
1648 dt_image_offset += page_size;
1649 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1650 dt_image_offset += n;
1651
1652 /* add ramdisk offset */
1653 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1654 dt_image_offset += n;
1655
1656 /* add second offset */
1657 if(hdr->second_size != 0) {
1658 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1659 dt_image_offset += n;
1660 }
1661
1662 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001663 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001664
Deepa Dinamani19648b42013-09-05 17:05:55 -07001665 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001666 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1667 return -1;
1668 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001669 /* Find index of device tree within device tree table */
1670 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001671 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1672 return -1;
1673 }
1674
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001675 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001676 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001677 {
1678 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1679 return -1;
1680 }
1681
Amol Jadicb524072012-08-09 16:40:18 -07001682 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001683 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001684 boot_image_start + dt_image_offset + dt_entry.offset,
1685 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001686 } else
1687 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001688
1689 /* Everything looks fine. Return success. */
1690 return 0;
1691}
1692#endif
1693
Brian Swetland9c4c0752009-01-25 16:23:50 -08001694void cmd_boot(const char *arg, void *data, unsigned sz)
1695{
1696 unsigned kernel_actual;
1697 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001698 uint32_t image_actual;
1699 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001700 uint32_t sig_actual = SIGNATURE_SIZE;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001701 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001702 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001703 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001704 int ret = 0;
1705 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001706
Amit Blay6281ebc2015-01-11 14:44:08 +02001707#ifdef MDTP_SUPPORT
1708 /* Go through Firmware Lock verification before continue with boot process */
1709 mdtp_fwlock_verify_lock();
1710 fbcon_clear();
1711#endif /* MDTP_SUPPORT */
1712
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001713#if VERIFIED_BOOT
1714 if(!device.is_unlocked)
1715 {
1716 fastboot_fail("unlock device to use this command");
1717 return;
1718 }
1719#endif
1720
Brian Swetland9c4c0752009-01-25 16:23:50 -08001721 if (sz < sizeof(hdr)) {
1722 fastboot_fail("invalid bootimage header");
1723 return;
1724 }
1725
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001726 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001727
1728 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001729 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001730
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001731 if(target_is_emmc_boot() && hdr->page_size) {
1732 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001733 page_mask = page_size - 1;
1734 }
1735
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001736 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1737 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001738#if DEVICE_TREE
1739 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1740#endif
1741
1742 image_actual = ADD_OF(page_size, kernel_actual);
1743 image_actual = ADD_OF(image_actual, ramdisk_actual);
1744 image_actual = ADD_OF(image_actual, dt_actual);
1745
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001746 if (target_use_signed_kernel() && (!device.is_unlocked))
1747 image_actual = ADD_OF(image_actual, sig_actual);
1748
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001749 /* sz should have atleast raw boot image */
1750 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001751 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001752 return;
1753 }
1754
1755 /* Verify the boot image
1756 * device & page_size are initialized in aboot_init
1757 */
1758 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001759 /* Pass size excluding signature size, otherwise we would try to
1760 * access signature beyond its length
1761 */
1762 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001763
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001764 /*
1765 * Update the kernel/ramdisk/tags address if the boot image header
1766 * has default values, these default values come from mkbootimg when
1767 * the boot image is flashed using fastboot flash:raw
1768 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001769 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1770 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001771
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001772 /* Get virtual addresses since the hdr saves physical addresses. */
1773 hdr->kernel_addr = VA(hdr->kernel_addr);
1774 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1775 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001776
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001777 /* Check if the addresses in the header are valid. */
1778 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1779 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1780 {
1781 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001782 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001783 }
1784
Amol Jadicb524072012-08-09 16:40:18 -07001785#if DEVICE_TREE
1786 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001787 ret = copy_dtb(data);
1788
1789 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001790#else
1791 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1792 {
1793 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001794 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001795 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001796#endif
1797
1798 /* Load ramdisk & kernel */
1799 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1800 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1801
1802#if DEVICE_TREE
1803 /*
1804 * If dtb is not found look for appended DTB in the kernel.
1805 * If appended dev tree is found, update the atags with
1806 * memory address to the DTB appended location on RAM.
1807 * Else update with the atags address in the kernel header
1808 */
1809 if (!dtb_copied) {
1810 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001811 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1812 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001813 if (!dtb) {
1814 fastboot_fail("dtb not found");
1815 return;
1816 }
Amol Jadicb524072012-08-09 16:40:18 -07001817 }
1818#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001819
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001820#ifndef DEVICE_TREE
1821 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1822 {
1823 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001824 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001825 }
1826#endif
1827
Brian Swetland9c4c0752009-01-25 16:23:50 -08001828 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001829 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001830
Dima Zavin77e41f32013-03-06 16:10:43 -08001831 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001832 (const char*) hdr->cmdline, board_machtype(),
1833 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001834}
1835
Sridhar Parasurame94e8152014-10-24 14:06:03 -07001836void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08001837{
1838 struct ptentry *ptn;
1839 struct ptable *ptable;
1840
1841 ptable = flash_get_ptable();
1842 if (ptable == NULL) {
1843 fastboot_fail("partition table doesn't exist");
1844 return;
1845 }
1846
1847 ptn = ptable_find(ptable, arg);
1848 if (ptn == NULL) {
1849 fastboot_fail("unknown partition name");
1850 return;
1851 }
1852
1853 if (flash_erase(ptn)) {
1854 fastboot_fail("failed to erase partition");
1855 return;
1856 }
1857 fastboot_okay("");
1858}
1859
Bikas Gurungd48bd242010-09-04 19:54:32 -07001860
1861void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1862{
1863 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001864 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001865 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001866 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001867
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001868#if VERIFIED_BOOT
1869 if(!strcmp(arg, KEYSTORE_PTN_NAME))
1870 {
1871 if(!device.is_unlocked)
1872 {
1873 fastboot_fail("unlock device to erase keystore");
1874 return;
1875 }
1876 }
1877#endif
1878
Kinson Chikf1a43512011-07-14 11:28:39 -07001879 index = partition_get_index(arg);
1880 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001881 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001882
Kinson Chikf1a43512011-07-14 11:28:39 -07001883 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001884 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001885 return;
1886 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001887
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001888 lun = partition_get_lun(index);
1889 mmc_set_lun(lun);
1890
Oliver Wangcee448d2013-10-22 18:40:13 +08001891#if MMC_SDHCI_SUPPORT
1892 if (mmc_erase_card(ptn, size)) {
1893 fastboot_fail("failed to erase partition\n");
1894 return;
1895 }
1896#else
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301897 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Kun Liang2f1601a2013-08-12 16:29:54 +08001898 size = partition_get_size(index);
1899 if (size > DEFAULT_ERASE_SIZE)
1900 size = DEFAULT_ERASE_SIZE;
1901
neetidb4b24d62012-01-20 12:13:09 -08001902 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001903 0 in first several blocks */
1904 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001905 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001906 return;
1907 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001908#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001909 fastboot_okay("");
1910}
1911
Sridhar Parasurame94e8152014-10-24 14:06:03 -07001912void cmd_erase(const char *arg, void *data, unsigned sz)
1913{
1914 if(target_is_emmc_boot())
1915 cmd_erase_mmc(arg, data, sz);
1916 else
1917 cmd_erase_nand(arg, data, sz);
1918}
Bikas Gurungd48bd242010-09-04 19:54:32 -07001919
Ajay Dudani5c761132011-04-07 20:19:04 -07001920void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001921{
1922 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001923 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001924 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001925 char *token = NULL;
1926 char *pname = NULL;
1927 uint8_t lun = 0;
1928 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001929
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301930 token = strtok((char *)arg, ":");
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001931 pname = token;
1932 token = strtok(NULL, ":");
1933 if(token)
1934 {
1935 lun = atoi(token);
1936 mmc_set_lun(lun);
1937 lun_set = true;
1938 }
1939
Mao Jinlong226f33a2014-07-04 17:24:10 +08001940 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07001941 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08001942 if (!strcmp(pname, "partition"))
1943 {
1944 dprintf(INFO, "Attempt to write partition image.\n");
1945 if (write_partition(sz, (unsigned char *) data)) {
1946 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07001947 return;
1948 }
1949 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08001950 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001951 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001952#if VERIFIED_BOOT
1953 if(!strcmp(pname, KEYSTORE_PTN_NAME))
1954 {
1955 if(!device.is_unlocked)
1956 {
1957 fastboot_fail("unlock device to flash keystore");
1958 return;
1959 }
1960 if(!boot_verify_validate_keystore((unsigned char *)data))
1961 {
1962 fastboot_fail("image is not a keystore file");
1963 return;
1964 }
1965 }
1966#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08001967 index = partition_get_index(pname);
1968 ptn = partition_get_offset(index);
1969 if(ptn == 0) {
1970 fastboot_fail("partition table doesn't exist");
1971 return;
1972 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001973
Mao Jinlong226f33a2014-07-04 17:24:10 +08001974 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
1975 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1976 fastboot_fail("image is not a boot image");
1977 return;
1978 }
1979 }
1980
1981 if(!lun_set)
1982 {
1983 lun = partition_get_lun(index);
1984 mmc_set_lun(lun);
1985 }
1986
1987 size = partition_get_size(index);
1988 if (ROUND_TO_PAGE(sz,511) > size) {
1989 fastboot_fail("size too large");
1990 return;
1991 }
1992 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1993 fastboot_fail("flash write failure");
1994 return;
1995 }
Greg Grisco6e754772011-06-23 12:19:39 -07001996 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001997 }
1998 fastboot_okay("");
1999 return;
2000}
2001
Ajay Dudani5c761132011-04-07 20:19:04 -07002002void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2003{
2004 unsigned int chunk;
2005 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002006 uint32_t *fill_buf = NULL;
2007 uint32_t fill_val;
2008 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002009 sparse_header_t *sparse_header;
2010 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002011 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002012 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302013 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002014 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302015 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002016 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002017
Kinson Chikf1a43512011-07-14 11:28:39 -07002018 index = partition_get_index(arg);
2019 ptn = partition_get_offset(index);
2020 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002021 fastboot_fail("partition table doesn't exist");
2022 return;
2023 }
2024
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302025 size = partition_get_size(index);
2026 if (ROUND_TO_PAGE(sz,511) > size) {
2027 fastboot_fail("size too large");
2028 return;
2029 }
2030
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002031 lun = partition_get_lun(index);
2032 mmc_set_lun(lun);
2033
Ajay Dudani5c761132011-04-07 20:19:04 -07002034 /* Read and skip over sparse image header */
2035 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08002036 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
2037 fastboot_fail("size too large");
2038 return;
2039 }
2040
Ajay Dudani5c761132011-04-07 20:19:04 -07002041 data += sparse_header->file_hdr_sz;
2042 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
2043 {
2044 /* Skip the remaining bytes in a header that is longer than
2045 * we expected.
2046 */
2047 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
2048 }
2049
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002050 dprintf (SPEW, "=== Sparse Image Header ===\n");
2051 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2052 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2053 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2054 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2055 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2056 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2057 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2058 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002059
2060 /* Start processing chunks */
2061 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2062 {
2063 /* Read and skip over chunk header */
2064 chunk_header = (chunk_header_t *) data;
2065 data += sizeof(chunk_header_t);
2066
2067 dprintf (SPEW, "=== Chunk Header ===\n");
2068 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2069 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2070 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2071
2072 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
2073 {
2074 /* Skip the remaining bytes in a header that is longer than
2075 * we expected.
2076 */
2077 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
2078 }
2079
2080 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
2081 switch (chunk_header->chunk_type)
2082 {
2083 case CHUNK_TYPE_RAW:
2084 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2085 chunk_data_sz))
2086 {
2087 fastboot_fail("Bogus chunk size for chunk type Raw");
2088 return;
2089 }
2090
Ajay Dudaniab18f022011-05-12 14:39:22 -07002091 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2092 chunk_data_sz,
2093 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002094 {
2095 fastboot_fail("flash write failure");
2096 return;
2097 }
2098 total_blocks += chunk_header->chunk_sz;
2099 data += chunk_data_sz;
2100 break;
2101
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002102 case CHUNK_TYPE_FILL:
2103 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2104 sizeof(uint32_t)))
2105 {
2106 fastboot_fail("Bogus chunk size for chunk type FILL");
2107 return;
2108 }
2109
2110 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2111 if (!fill_buf)
2112 {
2113 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2114 return;
2115 }
2116
2117 fill_val = *(uint32_t *)data;
2118 data = (char *) data + sizeof(uint32_t);
2119 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
2120
2121 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2122 {
2123 fill_buf[i] = fill_val;
2124 }
2125
2126 for (i = 0; i < chunk_blk_cnt; i++)
2127 {
2128 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2129 sparse_header->blk_sz,
2130 fill_buf))
2131 {
2132 fastboot_fail("flash write failure");
2133 free(fill_buf);
2134 return;
2135 }
2136
2137 total_blocks++;
2138 }
2139
2140 free(fill_buf);
2141 break;
2142
Ajay Dudani5c761132011-04-07 20:19:04 -07002143 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002144 total_blocks += chunk_header->chunk_sz;
2145 break;
2146
Ajay Dudani5c761132011-04-07 20:19:04 -07002147 case CHUNK_TYPE_CRC:
2148 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2149 {
2150 fastboot_fail("Bogus chunk size for chunk type Dont Care");
2151 return;
2152 }
2153 total_blocks += chunk_header->chunk_sz;
2154 data += chunk_data_sz;
2155 break;
2156
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002157 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002158 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002159 fastboot_fail("Unknown chunk type");
2160 return;
2161 }
2162 }
2163
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002164 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2165 total_blocks, sparse_header->total_blks);
2166
2167 if(total_blocks != sparse_header->total_blks)
2168 {
2169 fastboot_fail("sparse image write failure");
2170 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002171
2172 fastboot_okay("");
2173 return;
2174}
2175
2176void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2177{
2178 sparse_header_t *sparse_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002179
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002180#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002181 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2182 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002183 int ret=0;
2184 uint32 major_version=0;
2185 uint32 minor_version=0;
2186
2187 ret = scm_svc_version(&major_version,&minor_version);
2188 if(!ret)
2189 {
2190 if(major_version >= 2)
2191 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002192 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002193 {
2194 ret = encrypt_scm((uint32 **) &data, &sz);
2195 if (ret != 0) {
2196 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2197 return;
2198 }
2199
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002200 /* Protect only for SSD */
2201 if (!strcmp(arg, "ssd")) {
2202 ret = scm_protect_keystore((uint32 *) data, sz);
2203 if (ret != 0) {
2204 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2205 return;
2206 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002207 }
2208 }
2209 else
2210 {
2211 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2212 if(ret != 0)
2213 {
2214 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2215 return;
2216 }
2217 }
2218 }
2219 else
2220 {
2221 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2222 magic_number[1] == DECRYPT_MAGIC_1)
2223 {
2224 ret = decrypt_scm((uint32 **) &data, &sz);
2225 if (ret != 0) {
2226 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2227 return;
2228 }
2229 }
2230 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2231 magic_number[1] == ENCRYPT_MAGIC_1)
2232 {
2233 ret = encrypt_scm((uint32 **) &data, &sz);
2234 if (ret != 0) {
2235 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2236 return;
2237 }
2238 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002239 }
2240 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002241 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002242 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002243 dprintf(CRITICAL,"INVALID SVC Version\n");
2244 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002245 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002246#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002247
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002248#if VERIFIED_BOOT
2249 if(!device.is_unlocked && !device.is_verified)
2250 {
2251 fastboot_fail("device is locked. Cannot flash images");
2252 return;
2253 }
2254 if(!device.is_unlocked && device.is_verified)
2255 {
2256 if(!boot_verify_flash_allowed(arg))
2257 {
2258 fastboot_fail("cannot flash this partition in verified state");
2259 return;
2260 }
2261 }
2262#endif
2263
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002264 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002265 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
2266 cmd_flash_mmc_img(arg, data, sz);
2267 else
2268 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002269 return;
2270}
2271
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002272void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002273{
2274 struct ptentry *ptn;
2275 struct ptable *ptable;
2276 unsigned extra = 0;
2277
2278 ptable = flash_get_ptable();
2279 if (ptable == NULL) {
2280 fastboot_fail("partition table doesn't exist");
2281 return;
2282 }
2283
2284 ptn = ptable_find(ptable, arg);
2285 if (ptn == NULL) {
2286 fastboot_fail("unknown partition name");
2287 return;
2288 }
2289
2290 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2291 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2292 fastboot_fail("image is not a boot image");
2293 return;
2294 }
2295 }
2296
Amol Jadi5c61a952012-05-04 17:05:35 -07002297 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002298 || !strcmp(ptn->name, "userdata")
2299 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002300 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002301 || !strcmp(ptn->name, "modem"))
2302 {
2303 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07002304 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002305 else
2306 extra = 0;
2307 }
2308 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002309 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002310
2311 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2312 if (flash_write(ptn, extra, data, sz)) {
2313 fastboot_fail("flash write failure");
2314 return;
2315 }
2316 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2317 fastboot_okay("");
2318}
2319
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002320void cmd_flash(const char *arg, void *data, unsigned sz)
2321{
2322 if(target_is_emmc_boot())
2323 cmd_flash_mmc(arg, data, sz);
2324 else
2325 cmd_flash_nand(arg, data, sz);
2326}
2327
Dima Zavin214cc642009-01-26 11:16:21 -08002328void cmd_continue(const char *arg, void *data, unsigned sz)
2329{
2330 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002331 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002332
Amit Blay6281ebc2015-01-11 14:44:08 +02002333#ifdef MDTP_SUPPORT
2334 /* Go through Firmware Lock verification before continue with boot process */
2335 mdtp_fwlock_verify_lock();
2336 fbcon_clear();
2337#endif /* MDTP_SUPPORT */
2338
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002339 if (target_is_emmc_boot())
2340 {
2341 boot_linux_from_mmc();
2342 }
2343 else
2344 {
2345 boot_linux_from_flash();
2346 }
Dima Zavin214cc642009-01-26 11:16:21 -08002347}
2348
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002349void cmd_reboot(const char *arg, void *data, unsigned sz)
2350{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002351 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002352 fastboot_okay("");
2353 reboot_device(0);
2354}
2355
2356void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2357{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002358 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002359 fastboot_okay("");
2360 reboot_device(FASTBOOT_MODE);
2361}
2362
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002363void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2364{
2365 dprintf(INFO, "Enabling charger screen check\n");
2366 device.charger_screen_enabled = 1;
2367 write_device_info(&device);
2368 fastboot_okay("");
2369}
2370
2371void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2372{
2373 dprintf(INFO, "Disabling charger screen check\n");
2374 device.charger_screen_enabled = 0;
2375 write_device_info(&device);
2376 fastboot_okay("");
2377}
2378
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302379void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2380{
2381 dprintf(INFO, "Selecting display panel %s\n", arg);
2382 if (arg)
2383 strlcpy(device.display_panel, arg,
2384 sizeof(device.display_panel));
2385 write_device_info(&device);
2386 fastboot_okay("");
2387}
2388
Shashank Mittal162244e2011-08-08 19:01:25 -07002389void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2390{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002391 /* TODO: Wipe user data */
2392 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002393 {
2394 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002395 device.is_verified = 0;
2396 write_device_info(&device);
2397 }
2398 fastboot_okay("");
2399}
2400
2401void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2402{
2403 /* TODO: Wipe user data */
2404 if(device.is_unlocked || device.is_verified)
2405 {
2406 device.is_unlocked = 0;
2407 device.is_verified = 0;
2408 write_device_info(&device);
2409 }
2410 fastboot_okay("");
2411}
2412
2413void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2414{
2415 /* TODO: Wipe user data */
2416 if(device.is_unlocked || !device.is_verified)
2417 {
2418 device.is_unlocked = 0;
2419 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002420 write_device_info(&device);
2421 }
2422 fastboot_okay("");
2423}
2424
Shashank Mittala0032282011-08-26 14:50:11 -07002425void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2426{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302427 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002428 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002429 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002430 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2431 fastboot_info(response);
2432 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002433 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302434 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2435 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002436 fastboot_okay("");
2437}
2438
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002439void cmd_preflash(const char *arg, void *data, unsigned sz)
2440{
2441 fastboot_okay("");
2442}
2443
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302444struct fbimage* splash_screen_flash();
2445
2446int splash_screen_check_header(struct fbimage *logo)
2447{
2448 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2449 return -1;
2450 if (logo->header.width == 0 || logo->header.height == 0)
2451 return -1;
2452 return 0;
2453}
2454
2455struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002456{
2457 struct ptentry *ptn;
2458 struct ptable *ptable;
2459 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002460 struct fbimage *logo = NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002461
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302462
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002463 logo = (struct fbimage *) malloc(ROUNDUP(page_size, sizeof(struct fbimage)));
2464 ASSERT(logo);
2465
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302466 ptable = flash_get_ptable();
2467 if (ptable == NULL) {
2468 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002469 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302470 }
2471 ptn = ptable_find(ptable, "splash");
2472 if (ptn == NULL) {
2473 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002474 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302475 }
2476
2477 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2478 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002479 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302480 }
2481
2482 if (splash_screen_check_header(logo)) {
2483 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002484 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302485 }
2486
2487 fb_display = fbcon_display();
2488 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302489 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2490 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back to default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002491 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002492 }
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302493 uint8_t *base = (uint8_t *) fb_display->base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302494 if (flash_read(ptn + sizeof(logo->header), 0,
2495 base,
2496 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2497 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302498 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002499 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002500 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302501 logo->image = base;
2502 }
2503
2504 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002505
2506err:
2507 free(logo);
2508 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302509}
2510
2511struct fbimage* splash_screen_mmc()
2512{
2513 int index = INVALID_PTN;
2514 unsigned long long ptn = 0;
2515 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002516 struct fbimage *logo = NULL;
Channagoud Kadabida117392014-11-24 11:01:12 -08002517 uint32_t blocksize;
2518 uint32_t readsize;
2519 uint32_t ptn_size;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302520
2521 index = partition_get_index("splash");
2522 if (index == 0) {
2523 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2524 return NULL;
2525 }
2526
2527 ptn = partition_get_offset(index);
2528 if (ptn == 0) {
2529 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2530 return NULL;
2531 }
2532
Channagoud Kadabida117392014-11-24 11:01:12 -08002533 ptn_size = partition_get_size(index);
2534 blocksize = mmc_get_device_blocksize();
2535 readsize = ROUNDUP(sizeof(logo->header), blocksize);
2536
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002537 logo = (struct fbimage *)memalign(CACHE_LINE, ROUNDUP(readsize, CACHE_LINE));
2538 ASSERT(logo);
2539
Channagoud Kadabida117392014-11-24 11:01:12 -08002540 if (mmc_read(ptn, (uint32_t *) logo, readsize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302541 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002542 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302543 }
2544
2545 if (splash_screen_check_header(logo)) {
2546 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002547 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302548 }
2549
2550 fb_display = fbcon_display();
2551 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302552 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2553 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002554 goto err;
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302555 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302556 uint8_t *base = (uint8_t *) fb_display->base;
Channagoud Kadabida117392014-11-24 11:01:12 -08002557 readsize = ROUNDUP((logo->header.width * logo->header.height * fb_display->bpp/8), blocksize);
2558
2559 if (readsize > ptn_size)
2560 {
2561 dprintf(CRITICAL, "@%d:Invalid logo header readsize:%u exceeds ptn_size:%u\n", __LINE__, readsize,ptn_size);
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002562 goto err;
Channagoud Kadabida117392014-11-24 11:01:12 -08002563 }
2564
2565 if (mmc_read(ptn + sizeof(logo->header),(uint32_t *)base, readsize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302566 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302567 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002568 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302569 }
2570
2571 logo->image = base;
2572 }
2573
2574 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002575
2576err:
2577 free(logo);
2578 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302579}
2580
2581
2582struct fbimage* fetch_image_from_partition()
2583{
2584 if (target_is_emmc_boot()) {
2585 return splash_screen_mmc();
2586 } else {
2587 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002588 }
2589}
2590
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002591/* Get the size from partiton name */
2592static void get_partition_size(const char *arg, char *response)
2593{
2594 uint64_t ptn = 0;
2595 uint64_t size;
2596 int index = INVALID_PTN;
2597
2598 index = partition_get_index(arg);
2599
2600 if (index == INVALID_PTN)
2601 {
2602 dprintf(CRITICAL, "Invalid partition index\n");
2603 return;
2604 }
2605
2606 ptn = partition_get_offset(index);
2607
2608 if(!ptn)
2609 {
2610 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2611 return;
2612 }
2613
2614 size = partition_get_size(index);
2615
2616 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2617 return;
2618}
2619
2620/*
2621 * Publish the partition type & size info
2622 * fastboot getvar will publish the required information.
2623 * fastboot getvar partition_size:<partition_name>: partition size in hex
2624 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2625 */
2626static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2627{
2628 uint8_t i;
2629
2630 for (i = 0; i < num_parts; i++) {
2631 get_partition_size(info[i].part_name, info[i].size_response);
2632
2633 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2634 {
2635 dprintf(CRITICAL, "partition size name truncated\n");
2636 return;
2637 }
2638 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2639 {
2640 dprintf(CRITICAL, "partition type name truncated\n");
2641 return;
2642 }
2643
2644 /* publish partition size & type info */
2645 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2646 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2647 }
2648}
2649
Amol Jadi5edf3552013-07-23 14:15:34 -07002650/* register commands and variables for fastboot */
2651void aboot_fastboot_register_commands(void)
2652{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002653 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07002654
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002655 struct fastboot_cmd_desc cmd_list[] = {
2656 /* By default the enabled list is empty. */
2657 {"", NULL},
2658 /* move commands enclosed within the below ifndef to here
2659 * if they need to be enabled in user build.
2660 */
2661#ifndef DISABLE_FASTBOOT_CMDS
2662 /* Register the following commands only for non-user builds */
2663 {"flash:", cmd_flash},
2664 {"erase:", cmd_erase},
2665 {"boot", cmd_boot},
2666 {"continue", cmd_continue},
2667 {"reboot", cmd_reboot},
2668 {"reboot-bootloader", cmd_reboot_bootloader},
2669 {"oem unlock", cmd_oem_unlock},
2670 {"oem lock", cmd_oem_lock},
2671 {"oem verified", cmd_oem_verified},
2672 {"oem device-info", cmd_oem_devinfo},
2673 {"preflash", cmd_preflash},
2674 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
2675 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08002676 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002677#endif
2678 };
2679
2680 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
2681 for (i = 1; i < fastboot_cmds_count; i++)
2682 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
2683
Amol Jadi5edf3552013-07-23 14:15:34 -07002684 /* publish variables and their values */
2685 fastboot_publish("product", TARGET(BOARD));
2686 fastboot_publish("kernel", "lk");
2687 fastboot_publish("serialno", sn_buf);
2688
2689 /*
2690 * partition info is supported only for emmc partitions
2691 * Calling this for NAND prints some error messages which
2692 * is harmless but misleading. Avoid calling this for NAND
2693 * devices.
2694 */
2695 if (target_is_emmc_boot())
2696 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2697
2698 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002699 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2700 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002701 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002702 /* Is the charger screen check enabled */
2703 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2704 device.charger_screen_enabled);
2705 fastboot_publish("charger-screen-enabled",
2706 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302707 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2708 device.display_panel);
2709 fastboot_publish("display-panel",
2710 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002711}
2712
Brian Swetland9c4c0752009-01-25 16:23:50 -08002713void aboot_init(const struct app_descriptor *app)
2714{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002715 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002716 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002717
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002718 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002719 if (target_is_emmc_boot())
2720 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002721 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002722 page_mask = page_size - 1;
2723 }
2724 else
2725 {
2726 page_size = flash_page_size();
2727 page_mask = page_size - 1;
2728 }
2729
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002730 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2731
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002732 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002733
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002734 /* Display splash screen if enabled */
2735#if DISPLAY_SPLASH_SCREEN
2736 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302737 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002738 dprintf(SPEW, "Display Init: Done\n");
2739#endif
2740
2741
Greg Griscod6250552011-06-29 14:40:23 -07002742 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002743 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002744
Dhaval Patel223ec952013-07-18 14:49:44 -07002745 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2746
Matthew Qindefd5562014-07-11 18:02:40 +08002747 /*
2748 * Check power off reason if user force reset,
2749 * if yes phone will do normal boot.
2750 */
2751 if (is_user_force_reset())
2752 goto normal_boot;
2753
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002754 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002755 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002756 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002757 dprintf(ALWAYS,"dload mode key sequence detected\n");
2758 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002759 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002760 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002761 }
2762 else
2763 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302764 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002765 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002766 }
2767 boot_into_fastboot = true;
2768 }
2769 if (!boot_into_fastboot)
2770 {
2771 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2772 boot_into_recovery = 1;
2773 if (!boot_into_recovery &&
2774 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002775 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002776 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002777 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002778 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002779 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002780 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002781
Ajay Dudani77421292010-10-27 19:34:06 -07002782 reboot_mode = check_reboot_mode();
2783 if (reboot_mode == RECOVERY_MODE) {
2784 boot_into_recovery = 1;
2785 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002786 boot_into_fastboot = true;
Matthew Qind886f3c2014-01-17 16:52:01 +08002787 } else if(reboot_mode == ALARM_BOOT) {
2788 boot_reason_alarm = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002789 }
2790
Matthew Qindefd5562014-07-11 18:02:40 +08002791normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03002792 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002793 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002794 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002795 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002796 if(emmc_recovery_init())
2797 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2798 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002799 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002800 if((device.is_unlocked) || (device.is_tampered))
2801 {
2802 #ifdef TZ_TAMPER_FUSE
2803 set_tamper_fuse_cmd();
2804 #endif
2805 #if USE_PCOM_SECBOOT
2806 set_tamper_flag(device.is_tampered);
2807 #endif
2808 }
Shashank Mittala0032282011-08-26 14:50:11 -07002809 }
Amit Blay6281ebc2015-01-11 14:44:08 +02002810
2811#ifdef MDTP_SUPPORT
2812 /* Go through Firmware Lock verification before continue with boot process */
2813 mdtp_fwlock_verify_lock();
2814 fbcon_clear();
2815#endif /* MDTP_SUPPORT */
2816
Pavel Nedev5d91d412013-04-29 11:34:24 +03002817 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002818 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002819 else
2820 {
2821 recovery_init();
2822 #if USE_PCOM_SECBOOT
2823 if((device.is_unlocked) || (device.is_tampered))
2824 set_tamper_flag(device.is_tampered);
2825 #endif
2826 boot_linux_from_flash();
2827 }
2828 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2829 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002830 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002831
Amol Jadi5edf3552013-07-23 14:15:34 -07002832 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002833
Amol Jadi5edf3552013-07-23 14:15:34 -07002834 /* register aboot specific fastboot commands */
2835 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002836
Amol Jadi5edf3552013-07-23 14:15:34 -07002837 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002838 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002839
2840 /* initialize and start fastboot */
2841 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002842}
2843
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002844uint32_t get_page_size()
2845{
2846 return page_size;
2847}
2848
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002849/*
2850 * Calculated and save hash (SHA256) for non-signed boot image.
2851 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002852 * @param image_addr - Boot image address
2853 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002854 *
2855 * @return int - 0 on success, negative value on failure.
2856 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002857static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002858{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002859 unsigned int digest[8];
2860#if IMAGE_VERIF_ALGO_SHA1
2861 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
2862#else
2863 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
2864#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002865
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002866 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002867 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002868
2869 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002870 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002871
2872 return 0;
2873}
2874
Brian Swetland9c4c0752009-01-25 16:23:50 -08002875APP_START(aboot)
2876 .init = aboot_init,
2877APP_END