blob: c12ebcf04190f1c5bbf552b7de8de6438da2019a [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08005 * Copyright (c) 2009-2015, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
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;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800694
695#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800696#if IMAGE_VERIF_ALGO_SHA1
697 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
698#else
699 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
700#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800701#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700702
703 /* Assume device is rooted at this time. */
704 device.is_tampered = 1;
705
706 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
707
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700708#if VERIFIED_BOOT
709 if(boot_into_recovery)
710 {
711 ret = boot_verify_image((unsigned char *)bootimg_addr,
712 bootimg_size, "recovery");
713 }
714 else
715 {
716 ret = boot_verify_image((unsigned char *)bootimg_addr,
717 bootimg_size, "boot");
718 }
719 boot_verify_print_state();
720#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700721 ret = image_verify((unsigned char *)bootimg_addr,
722 (unsigned char *)(bootimg_addr + bootimg_size),
723 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800724 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700725#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700726 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
727
728 if (ret)
729 {
730 /* Authorized kernel */
731 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700732 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700733 }
734
735#if USE_PCOM_SECBOOT
736 set_tamper_flag(device.is_tampered);
737#endif
738
739 if(device.is_tampered)
740 {
741 write_device_info_mmc(&device);
742 #ifdef TZ_TAMPER_FUSE
743 set_tamper_fuse_cmd();
744 #endif
745 #ifdef ASSERT_ON_TAMPER
746 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
747 ASSERT(0);
748 #endif
749 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700750
751#if VERIFIED_BOOT
752 if(boot_verify_get_state() == RED)
753 {
754 if(!boot_into_recovery)
755 {
756 dprintf(CRITICAL,
757 "Device verification failed. Rebooting into recovery.\n");
758 reboot_device(RECOVERY_MODE);
759 }
760 else
761 {
762 dprintf(CRITICAL,
763 "Recovery image verification failed. Asserting..\n");
764 ASSERT(0);
765 }
766 }
767#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700768}
769
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530770static bool check_format_bit()
771{
772 bool ret = false;
773 int index;
774 uint64_t offset;
775 struct boot_selection_info *in = NULL;
776 char *buf = NULL;
777
778 index = partition_get_index("bootselect");
779 if (index == INVALID_PTN)
780 {
781 dprintf(INFO, "Unable to locate /bootselect partition\n");
782 return ret;
783 }
784 offset = partition_get_offset(index);
785 if(!offset)
786 {
787 dprintf(INFO, "partition /bootselect doesn't exist\n");
788 return ret;
789 }
790 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
791 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530792 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530793 {
794 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
795 free(buf);
796 return ret;
797 }
798 in = (struct boot_selection_info *) buf;
799 if ((in->signature == BOOTSELECT_SIGNATURE) &&
800 (in->version == BOOTSELECT_VERSION)) {
801 if ((in->state_info & BOOTSELECT_FORMAT) &&
802 !(in->state_info & BOOTSELECT_FACTORY))
803 ret = true;
804 } else {
805 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
806 in->signature, in->version);
807 ASSERT(0);
808 }
809 free(buf);
810 return ret;
811}
812
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700813void boot_verifier_init()
814{
815
816 uint32_t boot_state;
817 /* Check if device unlock */
818 if(device.is_unlocked)
819 {
820 boot_verify_send_event(DEV_UNLOCK);
821 boot_verify_print_state();
822 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
823 return;
824 }
825 else
826 {
827 boot_verify_send_event(BOOT_INIT);
828 }
829
830 /* Initialize keystore */
831 boot_state = boot_verify_keystore_init();
832 if(boot_state == YELLOW)
833 {
834 boot_verify_print_state();
835 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
836 }
837}
838
Shashank Mittal23b8f422010-04-16 19:27:21 -0700839int boot_linux_from_mmc(void)
840{
841 struct boot_img_hdr *hdr = (void*) buf;
842 struct boot_img_hdr *uhdr;
843 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700844 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700845 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700846 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700847
Shashank Mittalcd98d472011-08-02 14:29:24 -0700848 unsigned char *image_addr = 0;
849 unsigned kernel_actual;
850 unsigned ramdisk_actual;
851 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700852 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700853
854#if DEVICE_TREE
855 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700856 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700857 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800858 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700859 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700860#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800861 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
862 struct kernel64_hdr *kptr = (void*) kbuf;
863
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530864 if (check_format_bit())
865 boot_into_recovery = 1;
866
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700867 if (!boot_into_recovery) {
868 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
869 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
870 if (rcode <= 0) {
871 boot_into_ffbm = false;
872 if (rcode < 0)
873 dprintf(CRITICAL,"failed to get ffbm cookie");
874 } else
875 boot_into_ffbm = true;
876 } else
877 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700878 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
879 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
880 dprintf(INFO, "Unified boot method!\n");
881 hdr = uhdr;
882 goto unified_boot;
883 }
Greg Griscod6250552011-06-29 14:40:23 -0700884 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700885 index = partition_get_index("boot");
886 ptn = partition_get_offset(index);
887 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700888 dprintf(CRITICAL, "ERROR: No boot partition found\n");
889 return -1;
890 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700891 }
892 else {
893 index = partition_get_index("recovery");
894 ptn = partition_get_offset(index);
895 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700896 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
897 return -1;
898 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700899 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -0800900 /* Set Lun for boot & recovery partitions */
901 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -0700902
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530903 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700904 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
905 return -1;
906 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700907
908 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700909 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700910 return -1;
911 }
912
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700913 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530914
915 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
916 dprintf(CRITICAL, "ERROR: Invalid page size\n");
917 return -1;
918 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700919 page_size = hdr->page_size;
920 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700921 }
922
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800923 /* Read the next page to get kernel Image header
924 * which lives in the second page for arm64 targets.
925 */
926
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530927 if (mmc_read(ptn + page_size, (uint32_t *) kbuf, page_size)) {
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800928 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
929 return -1;
930 }
931
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700932 /*
933 * Update the kernel/ramdisk/tags address if the boot image header
934 * has default values, these default values come from mkbootimg when
935 * the boot image is flashed using fastboot flash:raw
936 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800937 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700938
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700939 /* Get virtual addresses since the hdr saves physical addresses. */
940 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
941 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
942 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700943
944 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
945 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
946
947 /* Check if the addresses in the header are valid. */
948 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
949 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
950 {
951 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
952 return -1;
953 }
954
955#ifndef DEVICE_TREE
956 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
957 {
958 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
959 return -1;
960 }
961#endif
962
Shashank Mittalcd98d472011-08-02 14:29:24 -0700963 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300964 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
965 (int) target_use_signed_kernel(),
966 device.is_unlocked,
967 device.is_tampered);
968
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700969#if VERIFIED_BOOT
970 boot_verifier_init();
971#endif
972
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700973 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800974 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700975 offset = 0;
976
Shashank Mittalcd98d472011-08-02 14:29:24 -0700977 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700978
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800979#if DEVICE_TREE
980 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
981 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700982
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700983 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700984 {
985 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
986 return -1;
987 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800988#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530989 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700990
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700991#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700992
Amol Jadib6be5c12012-11-14 13:39:51 -0800993 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700994 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800995
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530996 if (check_aboot_addr_range_overlap((uint32_t)image_addr, imagesize_actual))
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700997 {
998 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
999 return -1;
1000 }
1001
Shashank Mittalcd98d472011-08-02 14:29:24 -07001002 /* Read image without signature */
1003 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1004 {
1005 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1006 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -08001007 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001008
Amol Jadib6be5c12012-11-14 13:39:51 -08001009 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001010 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001011
Shashank Mittalcd98d472011-08-02 14:29:24 -07001012 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -07001013
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301014 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -07001015 {
1016 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1017 return -1;
1018 }
1019
Shashank Mittalcd98d472011-08-02 14:29:24 -07001020 /* Read signature */
1021 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1022 {
1023 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001024 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001025 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001026
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301027 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001028
Neeti Desai465491e2012-07-31 12:53:35 -07001029 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -07001030 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1031 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -07001032
Neeti Desai465491e2012-07-31 12:53:35 -07001033 #if DEVICE_TREE
1034 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001035 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -07001036 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001037
Deepa Dinamani19648b42013-09-05 17:05:55 -07001038 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001039 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1040 return -1;
1041 }
Deepa Dinamani19648b42013-09-05 17:05:55 -07001042
Neeti Desai465491e2012-07-31 12:53:35 -07001043 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -07001044 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001045 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
1046 return -1;
1047 }
1048
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001049 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001050 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001051 {
1052 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1053 return -1;
1054 }
1055
Joel Kingaa335dc2013-06-03 16:11:08 -07001056 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001057 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -08001058 /*
1059 * If appended dev tree is found, update the atags with
1060 * memory address to the DTB appended location on RAM.
1061 * Else update with the atags address in the kernel header
1062 */
1063 void *dtb;
1064 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001065 hdr->kernel_size,
1066 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001067 if (!dtb) {
1068 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1069 return -1;
1070 }
Neeti Desai465491e2012-07-31 12:53:35 -07001071 }
1072 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001073 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001074 else
1075 {
Amol Jadib6be5c12012-11-14 13:39:51 -08001076 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001077
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001078 image_addr = (unsigned char *)target_get_scratch_address();
1079#if DEVICE_TREE
1080 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1081 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1082
1083 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
1084 {
1085 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1086 return -1;
1087 }
1088#else
1089 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1090
1091#endif
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001092 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001093 {
1094 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1095 return -1;
1096 }
1097
Amol Jadib6be5c12012-11-14 13:39:51 -08001098 dprintf(INFO, "Loading boot image (%d): start\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001099 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001100 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001101
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001102 offset = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001103
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001104 /* Load the entire boot image */
1105 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual)) {
1106 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
Shashank Mittalcd98d472011-08-02 14:29:24 -07001107 return -1;
1108 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001109
1110 dprintf(INFO, "Loading boot image (%d): done\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001111 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001112 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -07001113
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001114 #ifdef TZ_SAVE_KERNEL_HASH
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001115 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001116 #endif /* TZ_SAVE_KERNEL_HASH */
1117
1118 /* Move kernel, ramdisk and device tree to correct address */
1119 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1120 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Neeti Desai465491e2012-07-31 12:53:35 -07001121
1122 #if DEVICE_TREE
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001123 if(hdr->dt_size) {
1124 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1125 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001126
Deepa Dinamani19648b42013-09-05 17:05:55 -07001127 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001128 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1129 return -1;
1130 }
1131
Joel Kingaa335dc2013-06-03 16:11:08 -07001132 /* Find index of device tree within device tree table */
1133 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001134 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1135 return -1;
1136 }
1137
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001138 /* Validate and Read device device tree in the tags_addr */
Joel Kingaa335dc2013-06-03 16:11:08 -07001139 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001140 {
1141 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1142 return -1;
1143 }
1144
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001145 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001146 } else {
Aparna Mallavarapu33127372014-06-03 22:15:54 +05301147 /* Validate the tags_addr */
1148 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1149 {
1150 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1151 return -1;
1152 }
Dima Zavin77e41f32013-03-06 16:10:43 -08001153 /*
1154 * If appended dev tree is found, update the atags with
1155 * memory address to the DTB appended location on RAM.
1156 * Else update with the atags address in the kernel header
1157 */
1158 void *dtb;
1159 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001160 kernel_actual,
1161 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001162 if (!dtb) {
1163 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1164 return -1;
1165 }
Neeti Desai465491e2012-07-31 12:53:35 -07001166 }
1167 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001168 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001169
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001170 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1171 target_load_ssd_keystore();
1172
Shashank Mittal23b8f422010-04-16 19:27:21 -07001173unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001174
Dima Zavin77e41f32013-03-06 16:10:43 -08001175 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001176 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001177 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1178
1179 return 0;
1180}
1181
Dima Zavin214cc642009-01-26 11:16:21 -08001182int boot_linux_from_flash(void)
1183{
1184 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001185 struct ptentry *ptn;
1186 struct ptable *ptable;
1187 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001188
Shashank Mittalcd98d472011-08-02 14:29:24 -07001189 unsigned char *image_addr = 0;
1190 unsigned kernel_actual;
1191 unsigned ramdisk_actual;
1192 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001193 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001194
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001195#if DEVICE_TREE
1196 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001197 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001198 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001199 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001200#endif
1201
David Ng183a7422009-12-07 14:55:21 -08001202 if (target_is_emmc_boot()) {
1203 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1204 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1205 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1206 return -1;
1207 }
1208 goto continue_boot;
1209 }
1210
Dima Zavin214cc642009-01-26 11:16:21 -08001211 ptable = flash_get_ptable();
1212 if (ptable == NULL) {
1213 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1214 return -1;
1215 }
1216
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001217 if(!boot_into_recovery)
1218 {
1219 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001220
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001221 if (ptn == NULL) {
1222 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1223 return -1;
1224 }
1225 }
1226 else
1227 {
1228 ptn = ptable_find(ptable, "recovery");
1229 if (ptn == NULL) {
1230 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1231 return -1;
1232 }
Dima Zavin214cc642009-01-26 11:16:21 -08001233 }
1234
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001235 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001236 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1237 return -1;
1238 }
Dima Zavin214cc642009-01-26 11:16:21 -08001239
1240 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001241 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001242 return -1;
1243 }
1244
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001245 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001246 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 -08001247 return -1;
1248 }
1249
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001250 /*
1251 * Update the kernel/ramdisk/tags address if the boot image header
1252 * has default values, these default values come from mkbootimg when
1253 * the boot image is flashed using fastboot flash:raw
1254 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001255 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001256
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001257 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001258 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1259 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1260 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1261
1262 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1263 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1264
1265 /* Check if the addresses in the header are valid. */
1266 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1267 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1268 {
1269 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1270 return -1;
1271 }
1272
1273#ifndef DEVICE_TREE
1274 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1275 {
1276 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1277 return -1;
1278 }
1279#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001280
Shashank Mittalcd98d472011-08-02 14:29:24 -07001281 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001282 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001283 {
1284 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001285 offset = 0;
1286
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001287#if DEVICE_TREE
1288 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1289 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001290
1291 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1292 {
1293 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1294 return -1;
1295 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001296#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001297 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001298#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001299
Amol Jadib6be5c12012-11-14 13:39:51 -08001300 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001301 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001302
Shashank Mittalcd98d472011-08-02 14:29:24 -07001303 /* Read image without signature */
1304 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1305 {
1306 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1307 return -1;
1308 }
Dima Zavin214cc642009-01-26 11:16:21 -08001309
Amol Jadib6be5c12012-11-14 13:39:51 -08001310 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001311 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001312
Shashank Mittalcd98d472011-08-02 14:29:24 -07001313 offset = imagesize_actual;
1314 /* Read signature */
1315 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1316 {
1317 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001318 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001319 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001320
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301321 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001322
1323 /* Move kernel and ramdisk to correct address */
1324 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1325 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001326#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001327 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001328 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001329 {
1330 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1331 return -1;
1332 }
1333
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001334 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1335#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001336
1337 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001338 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001339 {
1340 write_device_info_flash(&device);
1341 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301342#if USE_PCOM_SECBOOT
1343 set_tamper_flag(device.is_tampered);
1344#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001345 }
1346 else
1347 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001348 offset = page_size;
1349
Amol Jadib6be5c12012-11-14 13:39:51 -08001350 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1351 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1352 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1353
1354 dprintf(INFO, "Loading boot image (%d): start\n",
1355 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001356 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001357
1358 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001359 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1360 return -1;
1361 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001362 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001363
Amol Jadib6be5c12012-11-14 13:39:51 -08001364 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001365 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1366 return -1;
1367 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001368 offset += ramdisk_actual;
1369
1370 dprintf(INFO, "Loading boot image (%d): done\n",
1371 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001372 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001373
1374 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001375 offset += second_actual;
1376 /* Second image loading not implemented. */
1377 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001378 }
1379
1380#if DEVICE_TREE
1381 if(hdr->dt_size != 0) {
1382
1383 /* Read the device tree table into buffer */
1384 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1385 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1386 return -1;
1387 }
1388
1389 table = (struct dt_table*) dt_buf;
1390
Deepa Dinamani19648b42013-09-05 17:05:55 -07001391 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001392 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1393 return -1;
1394 }
1395
Deepa Dinamani19648b42013-09-05 17:05:55 -07001396 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1397 if (!table)
1398 return -1;
1399
1400 /* Read the entire device tree table into buffer */
1401 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1402 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1403 return -1;
1404 }
1405
1406
Joel Kingaa335dc2013-06-03 16:11:08 -07001407 /* Find index of device tree within device tree table */
1408 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001409 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1410 return -1;
1411 }
1412
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001413 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001414 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001415 {
1416 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1417 return -1;
1418 }
1419
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001420 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001421 if(flash_read(ptn, offset + dt_entry.offset,
1422 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001423 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1424 return -1;
1425 }
1426 }
1427#endif
1428
Shashank Mittalcd98d472011-08-02 14:29:24 -07001429 }
David Ng183a7422009-12-07 14:55:21 -08001430continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001431
Dima Zavin214cc642009-01-26 11:16:21 -08001432 /* TODO: create/pass atags to kernel */
1433
Ajay Dudanie28a6072011-07-01 13:59:46 -07001434 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001435 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001436 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1437
1438 return 0;
1439}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001440
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001441BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001442void write_device_info_mmc(device_info *dev)
1443{
1444 struct device_info *info = (void*) info_buf;
1445 unsigned long long ptn = 0;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001446#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001447 unsigned long long size;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001448#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001449 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001450 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001451 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001452
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001453#if VERIFIED_BOOT
1454 index = partition_get_index("devinfo");
1455#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001456 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001457#endif
1458
Shashank Mittal162244e2011-08-08 19:01:25 -07001459 ptn = partition_get_offset(index);
1460 if(ptn == 0)
1461 {
1462 return;
1463 }
1464
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001465 lun = partition_get_lun(index);
1466 mmc_set_lun(lun);
1467
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001468#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001469 size = partition_get_size(index);
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001470#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001471
1472 memcpy(info, dev, sizeof(device_info));
1473
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001474 blocksize = mmc_get_device_blocksize();
1475
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001476#if VERIFIED_BOOT
1477 if(mmc_write(ptn, blocksize, (void *)info_buf))
1478#else
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001479 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001480#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001481 {
1482 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1483 return;
1484 }
1485}
1486
1487void read_device_info_mmc(device_info *dev)
1488{
1489 struct device_info *info = (void*) info_buf;
1490 unsigned long long ptn = 0;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001491#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001492 unsigned long long size;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001493#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001494 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001495 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001496
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001497#if VERIFIED_BOOT
1498 index = partition_get_index("devinfo");
1499#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001500 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001501#endif
1502
Shashank Mittal162244e2011-08-08 19:01:25 -07001503 ptn = partition_get_offset(index);
1504 if(ptn == 0)
1505 {
1506 return;
1507 }
1508
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001509 mmc_set_lun(partition_get_lun(index));
1510
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001511#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001512 size = partition_get_size(index);
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001513#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001514
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001515 blocksize = mmc_get_device_blocksize();
1516
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001517#if VERIFIED_BOOT
1518 if(mmc_read(ptn, (void *)info_buf, blocksize))
1519#else
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001520 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001521#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001522 {
1523 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1524 return;
1525 }
1526
1527 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1528 {
1529 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
Shashank Mittal7e033062014-09-25 19:58:07 -07001530#if DEFAULT_UNLOCK
1531 info->is_unlocked = 1;
1532#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001533 info->is_unlocked = 0;
Shashank Mittal7e033062014-09-25 19:58:07 -07001534#endif
1535 info->is_verified = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001536 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001537 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001538
1539 write_device_info_mmc(info);
1540 }
1541 memcpy(dev, info, sizeof(device_info));
1542}
1543
1544void write_device_info_flash(device_info *dev)
1545{
1546 struct device_info *info = (void *) info_buf;
1547 struct ptentry *ptn;
1548 struct ptable *ptable;
1549
1550 ptable = flash_get_ptable();
1551 if (ptable == NULL)
1552 {
1553 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1554 return;
1555 }
1556
1557 ptn = ptable_find(ptable, "devinfo");
1558 if (ptn == NULL)
1559 {
1560 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1561 return;
1562 }
1563
1564 memcpy(info, dev, sizeof(device_info));
1565
1566 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1567 {
1568 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1569 return;
1570 }
1571}
1572
1573void read_device_info_flash(device_info *dev)
1574{
1575 struct device_info *info = (void*) info_buf;
1576 struct ptentry *ptn;
1577 struct ptable *ptable;
1578
1579 ptable = flash_get_ptable();
1580 if (ptable == NULL)
1581 {
1582 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1583 return;
1584 }
1585
1586 ptn = ptable_find(ptable, "devinfo");
1587 if (ptn == NULL)
1588 {
1589 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1590 return;
1591 }
1592
1593 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1594 {
1595 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1596 return;
1597 }
1598
1599 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1600 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001601 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1602 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001603 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001604 write_device_info_flash(info);
1605 }
1606 memcpy(dev, info, sizeof(device_info));
1607}
1608
1609void write_device_info(device_info *dev)
1610{
1611 if(target_is_emmc_boot())
1612 {
1613 write_device_info_mmc(dev);
1614 }
1615 else
1616 {
1617 write_device_info_flash(dev);
1618 }
1619}
1620
1621void read_device_info(device_info *dev)
1622{
1623 if(target_is_emmc_boot())
1624 {
1625 read_device_info_mmc(dev);
1626 }
1627 else
1628 {
1629 read_device_info_flash(dev);
1630 }
1631}
1632
1633void reset_device_info()
1634{
1635 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001636 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001637 write_device_info(&device);
1638}
1639
1640void set_device_root()
1641{
1642 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001643 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001644 write_device_info(&device);
1645}
1646
Amol Jadicb524072012-08-09 16:40:18 -07001647#if DEVICE_TREE
1648int copy_dtb(uint8_t *boot_image_start)
1649{
1650 uint32 dt_image_offset = 0;
1651 uint32_t n;
1652 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001653 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001654 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001655
1656 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1657
Amol Jadicb524072012-08-09 16:40:18 -07001658 if(hdr->dt_size != 0) {
1659
1660 /* add kernel offset */
1661 dt_image_offset += page_size;
1662 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1663 dt_image_offset += n;
1664
1665 /* add ramdisk offset */
1666 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1667 dt_image_offset += n;
1668
1669 /* add second offset */
1670 if(hdr->second_size != 0) {
1671 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1672 dt_image_offset += n;
1673 }
1674
1675 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001676 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001677
Deepa Dinamani19648b42013-09-05 17:05:55 -07001678 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001679 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1680 return -1;
1681 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001682 /* Find index of device tree within device tree table */
1683 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001684 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1685 return -1;
1686 }
1687
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001688 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001689 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001690 {
1691 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1692 return -1;
1693 }
1694
Amol Jadicb524072012-08-09 16:40:18 -07001695 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001696 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001697 boot_image_start + dt_image_offset + dt_entry.offset,
1698 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001699 } else
1700 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001701
1702 /* Everything looks fine. Return success. */
1703 return 0;
1704}
1705#endif
1706
Brian Swetland9c4c0752009-01-25 16:23:50 -08001707void cmd_boot(const char *arg, void *data, unsigned sz)
1708{
1709 unsigned kernel_actual;
1710 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001711 uint32_t image_actual;
1712 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001713 uint32_t sig_actual = SIGNATURE_SIZE;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001714 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001715 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001716 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001717 int ret = 0;
1718 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001719
Amit Blay6281ebc2015-01-11 14:44:08 +02001720#ifdef MDTP_SUPPORT
1721 /* Go through Firmware Lock verification before continue with boot process */
1722 mdtp_fwlock_verify_lock();
1723 fbcon_clear();
1724#endif /* MDTP_SUPPORT */
1725
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001726#if VERIFIED_BOOT
1727 if(!device.is_unlocked)
1728 {
1729 fastboot_fail("unlock device to use this command");
1730 return;
1731 }
1732#endif
1733
Brian Swetland9c4c0752009-01-25 16:23:50 -08001734 if (sz < sizeof(hdr)) {
1735 fastboot_fail("invalid bootimage header");
1736 return;
1737 }
1738
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001739 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001740
1741 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001742 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001743
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001744 if(target_is_emmc_boot() && hdr->page_size) {
1745 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001746 page_mask = page_size - 1;
1747 }
1748
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001749 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1750 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001751#if DEVICE_TREE
1752 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1753#endif
1754
1755 image_actual = ADD_OF(page_size, kernel_actual);
1756 image_actual = ADD_OF(image_actual, ramdisk_actual);
1757 image_actual = ADD_OF(image_actual, dt_actual);
1758
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001759 if (target_use_signed_kernel() && (!device.is_unlocked))
1760 image_actual = ADD_OF(image_actual, sig_actual);
1761
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001762 /* sz should have atleast raw boot image */
1763 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001764 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001765 return;
1766 }
1767
1768 /* Verify the boot image
1769 * device & page_size are initialized in aboot_init
1770 */
1771 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001772 /* Pass size excluding signature size, otherwise we would try to
1773 * access signature beyond its length
1774 */
1775 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001776
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001777 /*
1778 * Update the kernel/ramdisk/tags address if the boot image header
1779 * has default values, these default values come from mkbootimg when
1780 * the boot image is flashed using fastboot flash:raw
1781 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001782 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1783 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001784
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001785 /* Get virtual addresses since the hdr saves physical addresses. */
1786 hdr->kernel_addr = VA(hdr->kernel_addr);
1787 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1788 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001789
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001790 /* Check if the addresses in the header are valid. */
1791 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1792 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1793 {
1794 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001795 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001796 }
1797
Amol Jadicb524072012-08-09 16:40:18 -07001798#if DEVICE_TREE
1799 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001800 ret = copy_dtb(data);
1801
1802 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001803#else
1804 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1805 {
1806 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001807 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001808 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001809#endif
1810
1811 /* Load ramdisk & kernel */
1812 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1813 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1814
1815#if DEVICE_TREE
1816 /*
1817 * If dtb is not found look for appended DTB in the kernel.
1818 * If appended dev tree is found, update the atags with
1819 * memory address to the DTB appended location on RAM.
1820 * Else update with the atags address in the kernel header
1821 */
1822 if (!dtb_copied) {
1823 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001824 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1825 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001826 if (!dtb) {
1827 fastboot_fail("dtb not found");
1828 return;
1829 }
Amol Jadicb524072012-08-09 16:40:18 -07001830 }
1831#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001832
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001833#ifndef DEVICE_TREE
1834 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1835 {
1836 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001837 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001838 }
1839#endif
1840
Brian Swetland9c4c0752009-01-25 16:23:50 -08001841 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001842 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001843
Dima Zavin77e41f32013-03-06 16:10:43 -08001844 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001845 (const char*) hdr->cmdline, board_machtype(),
1846 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001847}
1848
Sridhar Parasurame94e8152014-10-24 14:06:03 -07001849void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08001850{
1851 struct ptentry *ptn;
1852 struct ptable *ptable;
1853
1854 ptable = flash_get_ptable();
1855 if (ptable == NULL) {
1856 fastboot_fail("partition table doesn't exist");
1857 return;
1858 }
1859
1860 ptn = ptable_find(ptable, arg);
1861 if (ptn == NULL) {
1862 fastboot_fail("unknown partition name");
1863 return;
1864 }
1865
1866 if (flash_erase(ptn)) {
1867 fastboot_fail("failed to erase partition");
1868 return;
1869 }
1870 fastboot_okay("");
1871}
1872
Bikas Gurungd48bd242010-09-04 19:54:32 -07001873
1874void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1875{
1876 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001877 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001878 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001879 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001880
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001881#if VERIFIED_BOOT
1882 if(!strcmp(arg, KEYSTORE_PTN_NAME))
1883 {
1884 if(!device.is_unlocked)
1885 {
1886 fastboot_fail("unlock device to erase keystore");
1887 return;
1888 }
1889 }
1890#endif
1891
Kinson Chikf1a43512011-07-14 11:28:39 -07001892 index = partition_get_index(arg);
1893 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001894 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001895
Kinson Chikf1a43512011-07-14 11:28:39 -07001896 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001897 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001898 return;
1899 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001900
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001901 lun = partition_get_lun(index);
1902 mmc_set_lun(lun);
1903
Oliver Wangcee448d2013-10-22 18:40:13 +08001904#if MMC_SDHCI_SUPPORT
1905 if (mmc_erase_card(ptn, size)) {
1906 fastboot_fail("failed to erase partition\n");
1907 return;
1908 }
1909#else
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301910 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Kun Liang2f1601a2013-08-12 16:29:54 +08001911 size = partition_get_size(index);
1912 if (size > DEFAULT_ERASE_SIZE)
1913 size = DEFAULT_ERASE_SIZE;
1914
neetidb4b24d62012-01-20 12:13:09 -08001915 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001916 0 in first several blocks */
1917 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001918 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001919 return;
1920 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001921#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001922 fastboot_okay("");
1923}
1924
Sridhar Parasurame94e8152014-10-24 14:06:03 -07001925void cmd_erase(const char *arg, void *data, unsigned sz)
1926{
1927 if(target_is_emmc_boot())
1928 cmd_erase_mmc(arg, data, sz);
1929 else
1930 cmd_erase_nand(arg, data, sz);
1931}
Bikas Gurungd48bd242010-09-04 19:54:32 -07001932
Ajay Dudani5c761132011-04-07 20:19:04 -07001933void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001934{
1935 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001936 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001937 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001938 char *token = NULL;
1939 char *pname = NULL;
1940 uint8_t lun = 0;
1941 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001942
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301943 token = strtok((char *)arg, ":");
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001944 pname = token;
1945 token = strtok(NULL, ":");
1946 if(token)
1947 {
1948 lun = atoi(token);
1949 mmc_set_lun(lun);
1950 lun_set = true;
1951 }
1952
Mao Jinlong226f33a2014-07-04 17:24:10 +08001953 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07001954 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08001955 if (!strcmp(pname, "partition"))
1956 {
1957 dprintf(INFO, "Attempt to write partition image.\n");
1958 if (write_partition(sz, (unsigned char *) data)) {
1959 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07001960 return;
1961 }
1962 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08001963 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001964 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001965#if VERIFIED_BOOT
1966 if(!strcmp(pname, KEYSTORE_PTN_NAME))
1967 {
1968 if(!device.is_unlocked)
1969 {
1970 fastboot_fail("unlock device to flash keystore");
1971 return;
1972 }
1973 if(!boot_verify_validate_keystore((unsigned char *)data))
1974 {
1975 fastboot_fail("image is not a keystore file");
1976 return;
1977 }
1978 }
1979#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08001980 index = partition_get_index(pname);
1981 ptn = partition_get_offset(index);
1982 if(ptn == 0) {
1983 fastboot_fail("partition table doesn't exist");
1984 return;
1985 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001986
Mao Jinlong226f33a2014-07-04 17:24:10 +08001987 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
1988 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1989 fastboot_fail("image is not a boot image");
1990 return;
1991 }
1992 }
1993
1994 if(!lun_set)
1995 {
1996 lun = partition_get_lun(index);
1997 mmc_set_lun(lun);
1998 }
1999
2000 size = partition_get_size(index);
2001 if (ROUND_TO_PAGE(sz,511) > size) {
2002 fastboot_fail("size too large");
2003 return;
2004 }
2005 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2006 fastboot_fail("flash write failure");
2007 return;
2008 }
Greg Grisco6e754772011-06-23 12:19:39 -07002009 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002010 }
2011 fastboot_okay("");
2012 return;
2013}
2014
Ajay Dudani5c761132011-04-07 20:19:04 -07002015void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2016{
2017 unsigned int chunk;
2018 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002019 uint32_t *fill_buf = NULL;
2020 uint32_t fill_val;
2021 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002022 sparse_header_t *sparse_header;
2023 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002024 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002025 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302026 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002027 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302028 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002029 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002030
Kinson Chikf1a43512011-07-14 11:28:39 -07002031 index = partition_get_index(arg);
2032 ptn = partition_get_offset(index);
2033 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002034 fastboot_fail("partition table doesn't exist");
2035 return;
2036 }
2037
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302038 size = partition_get_size(index);
2039 if (ROUND_TO_PAGE(sz,511) > size) {
2040 fastboot_fail("size too large");
2041 return;
2042 }
2043
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002044 lun = partition_get_lun(index);
2045 mmc_set_lun(lun);
2046
Ajay Dudani5c761132011-04-07 20:19:04 -07002047 /* Read and skip over sparse image header */
2048 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08002049 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
2050 fastboot_fail("size too large");
2051 return;
2052 }
2053
Ajay Dudani5c761132011-04-07 20:19:04 -07002054 data += sparse_header->file_hdr_sz;
2055 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
2056 {
2057 /* Skip the remaining bytes in a header that is longer than
2058 * we expected.
2059 */
2060 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
2061 }
2062
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002063 dprintf (SPEW, "=== Sparse Image Header ===\n");
2064 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2065 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2066 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2067 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2068 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2069 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2070 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2071 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002072
2073 /* Start processing chunks */
2074 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2075 {
2076 /* Read and skip over chunk header */
2077 chunk_header = (chunk_header_t *) data;
2078 data += sizeof(chunk_header_t);
2079
2080 dprintf (SPEW, "=== Chunk Header ===\n");
2081 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2082 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2083 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2084
2085 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
2086 {
2087 /* Skip the remaining bytes in a header that is longer than
2088 * we expected.
2089 */
2090 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
2091 }
2092
2093 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
2094 switch (chunk_header->chunk_type)
2095 {
2096 case CHUNK_TYPE_RAW:
2097 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2098 chunk_data_sz))
2099 {
2100 fastboot_fail("Bogus chunk size for chunk type Raw");
2101 return;
2102 }
2103
Ajay Dudaniab18f022011-05-12 14:39:22 -07002104 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2105 chunk_data_sz,
2106 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002107 {
2108 fastboot_fail("flash write failure");
2109 return;
2110 }
2111 total_blocks += chunk_header->chunk_sz;
2112 data += chunk_data_sz;
2113 break;
2114
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002115 case CHUNK_TYPE_FILL:
2116 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2117 sizeof(uint32_t)))
2118 {
2119 fastboot_fail("Bogus chunk size for chunk type FILL");
2120 return;
2121 }
2122
2123 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2124 if (!fill_buf)
2125 {
2126 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2127 return;
2128 }
2129
2130 fill_val = *(uint32_t *)data;
2131 data = (char *) data + sizeof(uint32_t);
2132 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
2133
2134 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2135 {
2136 fill_buf[i] = fill_val;
2137 }
2138
2139 for (i = 0; i < chunk_blk_cnt; i++)
2140 {
2141 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2142 sparse_header->blk_sz,
2143 fill_buf))
2144 {
2145 fastboot_fail("flash write failure");
2146 free(fill_buf);
2147 return;
2148 }
2149
2150 total_blocks++;
2151 }
2152
2153 free(fill_buf);
2154 break;
2155
Ajay Dudani5c761132011-04-07 20:19:04 -07002156 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002157 total_blocks += chunk_header->chunk_sz;
2158 break;
2159
Ajay Dudani5c761132011-04-07 20:19:04 -07002160 case CHUNK_TYPE_CRC:
2161 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2162 {
2163 fastboot_fail("Bogus chunk size for chunk type Dont Care");
2164 return;
2165 }
2166 total_blocks += chunk_header->chunk_sz;
2167 data += chunk_data_sz;
2168 break;
2169
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002170 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002171 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002172 fastboot_fail("Unknown chunk type");
2173 return;
2174 }
2175 }
2176
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002177 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2178 total_blocks, sparse_header->total_blks);
2179
2180 if(total_blocks != sparse_header->total_blks)
2181 {
2182 fastboot_fail("sparse image write failure");
2183 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002184
2185 fastboot_okay("");
2186 return;
2187}
2188
2189void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2190{
2191 sparse_header_t *sparse_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002192
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002193#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002194 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2195 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002196 int ret=0;
2197 uint32 major_version=0;
2198 uint32 minor_version=0;
2199
2200 ret = scm_svc_version(&major_version,&minor_version);
2201 if(!ret)
2202 {
2203 if(major_version >= 2)
2204 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002205 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002206 {
2207 ret = encrypt_scm((uint32 **) &data, &sz);
2208 if (ret != 0) {
2209 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2210 return;
2211 }
2212
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002213 /* Protect only for SSD */
2214 if (!strcmp(arg, "ssd")) {
2215 ret = scm_protect_keystore((uint32 *) data, sz);
2216 if (ret != 0) {
2217 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2218 return;
2219 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002220 }
2221 }
2222 else
2223 {
2224 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2225 if(ret != 0)
2226 {
2227 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2228 return;
2229 }
2230 }
2231 }
2232 else
2233 {
2234 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2235 magic_number[1] == DECRYPT_MAGIC_1)
2236 {
2237 ret = decrypt_scm((uint32 **) &data, &sz);
2238 if (ret != 0) {
2239 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2240 return;
2241 }
2242 }
2243 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2244 magic_number[1] == ENCRYPT_MAGIC_1)
2245 {
2246 ret = encrypt_scm((uint32 **) &data, &sz);
2247 if (ret != 0) {
2248 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2249 return;
2250 }
2251 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002252 }
2253 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002254 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002255 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002256 dprintf(CRITICAL,"INVALID SVC Version\n");
2257 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002258 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002259#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002260
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002261#if VERIFIED_BOOT
2262 if(!device.is_unlocked && !device.is_verified)
2263 {
2264 fastboot_fail("device is locked. Cannot flash images");
2265 return;
2266 }
2267 if(!device.is_unlocked && device.is_verified)
2268 {
2269 if(!boot_verify_flash_allowed(arg))
2270 {
2271 fastboot_fail("cannot flash this partition in verified state");
2272 return;
2273 }
2274 }
2275#endif
2276
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002277 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002278 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
2279 cmd_flash_mmc_img(arg, data, sz);
2280 else
2281 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002282 return;
2283}
2284
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002285void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002286{
2287 struct ptentry *ptn;
2288 struct ptable *ptable;
2289 unsigned extra = 0;
2290
2291 ptable = flash_get_ptable();
2292 if (ptable == NULL) {
2293 fastboot_fail("partition table doesn't exist");
2294 return;
2295 }
2296
2297 ptn = ptable_find(ptable, arg);
2298 if (ptn == NULL) {
2299 fastboot_fail("unknown partition name");
2300 return;
2301 }
2302
2303 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2304 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2305 fastboot_fail("image is not a boot image");
2306 return;
2307 }
2308 }
2309
Amol Jadi5c61a952012-05-04 17:05:35 -07002310 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002311 || !strcmp(ptn->name, "userdata")
2312 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002313 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002314 || !strcmp(ptn->name, "modem"))
2315 {
2316 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07002317 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002318 else
2319 extra = 0;
2320 }
2321 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002322 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002323
2324 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2325 if (flash_write(ptn, extra, data, sz)) {
2326 fastboot_fail("flash write failure");
2327 return;
2328 }
2329 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2330 fastboot_okay("");
2331}
2332
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002333void cmd_flash(const char *arg, void *data, unsigned sz)
2334{
2335 if(target_is_emmc_boot())
2336 cmd_flash_mmc(arg, data, sz);
2337 else
2338 cmd_flash_nand(arg, data, sz);
2339}
2340
Dima Zavin214cc642009-01-26 11:16:21 -08002341void cmd_continue(const char *arg, void *data, unsigned sz)
2342{
2343 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002344 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002345
Amit Blay6281ebc2015-01-11 14:44:08 +02002346#ifdef MDTP_SUPPORT
2347 /* Go through Firmware Lock verification before continue with boot process */
2348 mdtp_fwlock_verify_lock();
2349 fbcon_clear();
2350#endif /* MDTP_SUPPORT */
2351
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002352 if (target_is_emmc_boot())
2353 {
2354 boot_linux_from_mmc();
2355 }
2356 else
2357 {
2358 boot_linux_from_flash();
2359 }
Dima Zavin214cc642009-01-26 11:16:21 -08002360}
2361
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002362void cmd_reboot(const char *arg, void *data, unsigned sz)
2363{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002364 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002365 fastboot_okay("");
2366 reboot_device(0);
2367}
2368
2369void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2370{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002371 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002372 fastboot_okay("");
2373 reboot_device(FASTBOOT_MODE);
2374}
2375
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002376void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2377{
2378 dprintf(INFO, "Enabling charger screen check\n");
2379 device.charger_screen_enabled = 1;
2380 write_device_info(&device);
2381 fastboot_okay("");
2382}
2383
2384void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2385{
2386 dprintf(INFO, "Disabling charger screen check\n");
2387 device.charger_screen_enabled = 0;
2388 write_device_info(&device);
2389 fastboot_okay("");
2390}
2391
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302392void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2393{
2394 dprintf(INFO, "Selecting display panel %s\n", arg);
2395 if (arg)
2396 strlcpy(device.display_panel, arg,
2397 sizeof(device.display_panel));
2398 write_device_info(&device);
2399 fastboot_okay("");
2400}
2401
Shashank Mittal162244e2011-08-08 19:01:25 -07002402void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2403{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002404 /* TODO: Wipe user data */
2405 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002406 {
2407 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002408 device.is_verified = 0;
2409 write_device_info(&device);
2410 }
2411 fastboot_okay("");
2412}
2413
2414void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2415{
2416 /* TODO: Wipe user data */
2417 if(device.is_unlocked || device.is_verified)
2418 {
2419 device.is_unlocked = 0;
2420 device.is_verified = 0;
2421 write_device_info(&device);
2422 }
2423 fastboot_okay("");
2424}
2425
2426void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2427{
2428 /* TODO: Wipe user data */
2429 if(device.is_unlocked || !device.is_verified)
2430 {
2431 device.is_unlocked = 0;
2432 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002433 write_device_info(&device);
2434 }
2435 fastboot_okay("");
2436}
2437
Shashank Mittala0032282011-08-26 14:50:11 -07002438void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2439{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302440 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002441 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002442 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002443 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2444 fastboot_info(response);
2445 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002446 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302447 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2448 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002449 fastboot_okay("");
2450}
2451
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002452void cmd_preflash(const char *arg, void *data, unsigned sz)
2453{
2454 fastboot_okay("");
2455}
2456
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302457struct fbimage* splash_screen_flash();
2458
2459int splash_screen_check_header(struct fbimage *logo)
2460{
2461 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2462 return -1;
2463 if (logo->header.width == 0 || logo->header.height == 0)
2464 return -1;
2465 return 0;
2466}
2467
2468struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002469{
2470 struct ptentry *ptn;
2471 struct ptable *ptable;
2472 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002473 struct fbimage *logo = NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002474
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302475
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002476 logo = (struct fbimage *) malloc(ROUNDUP(page_size, sizeof(struct fbimage)));
2477 ASSERT(logo);
2478
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302479 ptable = flash_get_ptable();
2480 if (ptable == NULL) {
2481 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002482 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302483 }
2484 ptn = ptable_find(ptable, "splash");
2485 if (ptn == NULL) {
2486 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002487 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302488 }
2489
2490 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2491 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002492 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302493 }
2494
2495 if (splash_screen_check_header(logo)) {
2496 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002497 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302498 }
2499
2500 fb_display = fbcon_display();
2501 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302502 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2503 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back to default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002504 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002505 }
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302506 uint8_t *base = (uint8_t *) fb_display->base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302507 if (flash_read(ptn + sizeof(logo->header), 0,
2508 base,
2509 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2510 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302511 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002512 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002513 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302514 logo->image = base;
2515 }
2516
2517 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002518
2519err:
2520 free(logo);
2521 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302522}
2523
2524struct fbimage* splash_screen_mmc()
2525{
2526 int index = INVALID_PTN;
2527 unsigned long long ptn = 0;
2528 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002529 struct fbimage *logo = NULL;
Channagoud Kadabida117392014-11-24 11:01:12 -08002530 uint32_t blocksize;
2531 uint32_t readsize;
2532 uint32_t ptn_size;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302533
2534 index = partition_get_index("splash");
2535 if (index == 0) {
2536 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2537 return NULL;
2538 }
2539
2540 ptn = partition_get_offset(index);
2541 if (ptn == 0) {
2542 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2543 return NULL;
2544 }
2545
Channagoud Kadabida117392014-11-24 11:01:12 -08002546 ptn_size = partition_get_size(index);
2547 blocksize = mmc_get_device_blocksize();
2548 readsize = ROUNDUP(sizeof(logo->header), blocksize);
2549
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002550 logo = (struct fbimage *)memalign(CACHE_LINE, ROUNDUP(readsize, CACHE_LINE));
2551 ASSERT(logo);
2552
Channagoud Kadabida117392014-11-24 11:01:12 -08002553 if (mmc_read(ptn, (uint32_t *) logo, readsize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302554 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002555 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302556 }
2557
2558 if (splash_screen_check_header(logo)) {
2559 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002560 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302561 }
2562
2563 fb_display = fbcon_display();
2564 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302565 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2566 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002567 goto err;
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302568 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302569 uint8_t *base = (uint8_t *) fb_display->base;
Channagoud Kadabida117392014-11-24 11:01:12 -08002570 readsize = ROUNDUP((logo->header.width * logo->header.height * fb_display->bpp/8), blocksize);
2571
2572 if (readsize > ptn_size)
2573 {
2574 dprintf(CRITICAL, "@%d:Invalid logo header readsize:%u exceeds ptn_size:%u\n", __LINE__, readsize,ptn_size);
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002575 goto err;
Channagoud Kadabida117392014-11-24 11:01:12 -08002576 }
2577
2578 if (mmc_read(ptn + sizeof(logo->header),(uint32_t *)base, readsize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302579 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302580 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002581 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302582 }
2583
2584 logo->image = base;
2585 }
2586
2587 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002588
2589err:
2590 free(logo);
2591 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302592}
2593
2594
2595struct fbimage* fetch_image_from_partition()
2596{
2597 if (target_is_emmc_boot()) {
2598 return splash_screen_mmc();
2599 } else {
2600 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002601 }
2602}
2603
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002604/* Get the size from partiton name */
2605static void get_partition_size(const char *arg, char *response)
2606{
2607 uint64_t ptn = 0;
2608 uint64_t size;
2609 int index = INVALID_PTN;
2610
2611 index = partition_get_index(arg);
2612
2613 if (index == INVALID_PTN)
2614 {
2615 dprintf(CRITICAL, "Invalid partition index\n");
2616 return;
2617 }
2618
2619 ptn = partition_get_offset(index);
2620
2621 if(!ptn)
2622 {
2623 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2624 return;
2625 }
2626
2627 size = partition_get_size(index);
2628
2629 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2630 return;
2631}
2632
2633/*
2634 * Publish the partition type & size info
2635 * fastboot getvar will publish the required information.
2636 * fastboot getvar partition_size:<partition_name>: partition size in hex
2637 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2638 */
2639static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2640{
2641 uint8_t i;
2642
2643 for (i = 0; i < num_parts; i++) {
2644 get_partition_size(info[i].part_name, info[i].size_response);
2645
2646 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2647 {
2648 dprintf(CRITICAL, "partition size name truncated\n");
2649 return;
2650 }
2651 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2652 {
2653 dprintf(CRITICAL, "partition type name truncated\n");
2654 return;
2655 }
2656
2657 /* publish partition size & type info */
2658 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2659 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2660 }
2661}
2662
Amol Jadi5edf3552013-07-23 14:15:34 -07002663/* register commands and variables for fastboot */
2664void aboot_fastboot_register_commands(void)
2665{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002666 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07002667
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002668 struct fastboot_cmd_desc cmd_list[] = {
2669 /* By default the enabled list is empty. */
2670 {"", NULL},
2671 /* move commands enclosed within the below ifndef to here
2672 * if they need to be enabled in user build.
2673 */
2674#ifndef DISABLE_FASTBOOT_CMDS
2675 /* Register the following commands only for non-user builds */
2676 {"flash:", cmd_flash},
2677 {"erase:", cmd_erase},
2678 {"boot", cmd_boot},
2679 {"continue", cmd_continue},
2680 {"reboot", cmd_reboot},
2681 {"reboot-bootloader", cmd_reboot_bootloader},
2682 {"oem unlock", cmd_oem_unlock},
2683 {"oem lock", cmd_oem_lock},
2684 {"oem verified", cmd_oem_verified},
2685 {"oem device-info", cmd_oem_devinfo},
2686 {"preflash", cmd_preflash},
2687 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
2688 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08002689 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002690#endif
2691 };
2692
2693 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
2694 for (i = 1; i < fastboot_cmds_count; i++)
2695 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
2696
Amol Jadi5edf3552013-07-23 14:15:34 -07002697 /* publish variables and their values */
2698 fastboot_publish("product", TARGET(BOARD));
2699 fastboot_publish("kernel", "lk");
2700 fastboot_publish("serialno", sn_buf);
2701
2702 /*
2703 * partition info is supported only for emmc partitions
2704 * Calling this for NAND prints some error messages which
2705 * is harmless but misleading. Avoid calling this for NAND
2706 * devices.
2707 */
2708 if (target_is_emmc_boot())
2709 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2710
2711 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002712 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2713 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002714 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002715 /* Is the charger screen check enabled */
2716 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2717 device.charger_screen_enabled);
2718 fastboot_publish("charger-screen-enabled",
2719 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302720 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2721 device.display_panel);
2722 fastboot_publish("display-panel",
2723 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002724}
2725
Brian Swetland9c4c0752009-01-25 16:23:50 -08002726void aboot_init(const struct app_descriptor *app)
2727{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002728 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002729 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002730
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002731 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002732 if (target_is_emmc_boot())
2733 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002734 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002735 page_mask = page_size - 1;
2736 }
2737 else
2738 {
2739 page_size = flash_page_size();
2740 page_mask = page_size - 1;
2741 }
2742
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002743 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2744
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002745 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002746
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002747 /* Display splash screen if enabled */
2748#if DISPLAY_SPLASH_SCREEN
2749 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302750 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002751 dprintf(SPEW, "Display Init: Done\n");
2752#endif
2753
2754
Greg Griscod6250552011-06-29 14:40:23 -07002755 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002756 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002757
Dhaval Patel223ec952013-07-18 14:49:44 -07002758 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2759
Matthew Qindefd5562014-07-11 18:02:40 +08002760 /*
2761 * Check power off reason if user force reset,
2762 * if yes phone will do normal boot.
2763 */
2764 if (is_user_force_reset())
2765 goto normal_boot;
2766
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002767 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002768 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002769 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002770 dprintf(ALWAYS,"dload mode key sequence detected\n");
2771 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002772 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002773 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002774 }
2775 else
2776 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302777 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002778 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002779 }
2780 boot_into_fastboot = true;
2781 }
2782 if (!boot_into_fastboot)
2783 {
2784 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2785 boot_into_recovery = 1;
2786 if (!boot_into_recovery &&
2787 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002788 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002789 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002790 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002791 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002792 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002793 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002794
Ajay Dudani77421292010-10-27 19:34:06 -07002795 reboot_mode = check_reboot_mode();
2796 if (reboot_mode == RECOVERY_MODE) {
2797 boot_into_recovery = 1;
2798 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002799 boot_into_fastboot = true;
Matthew Qind886f3c2014-01-17 16:52:01 +08002800 } else if(reboot_mode == ALARM_BOOT) {
2801 boot_reason_alarm = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002802 }
2803
Matthew Qindefd5562014-07-11 18:02:40 +08002804normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03002805 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002806 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002807 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002808 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002809 if(emmc_recovery_init())
2810 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2811 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002812 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002813 if((device.is_unlocked) || (device.is_tampered))
2814 {
2815 #ifdef TZ_TAMPER_FUSE
2816 set_tamper_fuse_cmd();
2817 #endif
2818 #if USE_PCOM_SECBOOT
2819 set_tamper_flag(device.is_tampered);
2820 #endif
2821 }
Shashank Mittala0032282011-08-26 14:50:11 -07002822 }
Amit Blay6281ebc2015-01-11 14:44:08 +02002823
2824#ifdef MDTP_SUPPORT
2825 /* Go through Firmware Lock verification before continue with boot process */
2826 mdtp_fwlock_verify_lock();
2827 fbcon_clear();
2828#endif /* MDTP_SUPPORT */
2829
Pavel Nedev5d91d412013-04-29 11:34:24 +03002830 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002831 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002832 else
2833 {
2834 recovery_init();
2835 #if USE_PCOM_SECBOOT
2836 if((device.is_unlocked) || (device.is_tampered))
2837 set_tamper_flag(device.is_tampered);
2838 #endif
2839 boot_linux_from_flash();
2840 }
2841 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2842 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002843 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002844
Amol Jadi5edf3552013-07-23 14:15:34 -07002845 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002846
Amol Jadi5edf3552013-07-23 14:15:34 -07002847 /* register aboot specific fastboot commands */
2848 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002849
Amol Jadi5edf3552013-07-23 14:15:34 -07002850 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002851 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002852
2853 /* initialize and start fastboot */
2854 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002855}
2856
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002857uint32_t get_page_size()
2858{
2859 return page_size;
2860}
2861
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002862/*
2863 * Calculated and save hash (SHA256) for non-signed boot image.
2864 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002865 * @param image_addr - Boot image address
2866 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002867 *
2868 * @return int - 0 on success, negative value on failure.
2869 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002870static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002871{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002872 unsigned int digest[8];
2873#if IMAGE_VERIF_ALGO_SHA1
2874 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
2875#else
2876 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
2877#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002878
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002879 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002880 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002881
2882 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002883 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002884
2885 return 0;
2886}
2887
Brian Swetland9c4c0752009-01-25 16:23:50 -08002888APP_START(aboot)
2889 .init = aboot_init,
2890APP_END