blob: 91a8c06041d83d3ba81898982b9d696cfbf879a6 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08005 * Copyright (c) 2009-2015, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Dima Zavin214cc642009-01-26 11:16:21 -080060
Neeti Desai17379b82012-06-04 18:42:53 -070061#if DEVICE_TREE
62#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070063#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070064#endif
65
Shashank Mittalcd98d472011-08-02 14:29:24 -070066#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080067#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080068#include "bootimg.h"
69#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070070#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070071#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070072#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070073#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070074#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020075#include "mdtp.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070076
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070077extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070078extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070079extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070080extern int get_target_boot_params(const char *cmdline, const char *part,
81 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070082
83void write_device_info_mmc(device_info *dev);
84void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -070085static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070086
Sridhar Parasurame94e8152014-10-24 14:06:03 -070087/* fastboot command function pointer */
88typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
89
90struct fastboot_cmd_desc {
91 char * name;
92 fastboot_cmd_fn cb;
93};
94
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070095#define EXPAND(NAME) #NAME
96#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070097
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080098#ifdef MEMBASE
99#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
100#else
David Ng183a7422009-12-07 14:55:21 -0800101#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800102#endif
103
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700104#ifndef MEMSIZE
105#define MEMSIZE 1024*1024
106#endif
107
108#define MAX_TAGS_SIZE 1024
109
lijuang72875802015-02-10 10:35:12 +0800110#define RECOVERY_HARD_RESET_MODE 0x01
111#define FASTBOOT_HARD_RESET_MODE 0x02
112#define RTC_HARD_RESET_MODE 0x03
113
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800114#define RECOVERY_MODE 0x77665502
115#define FASTBOOT_MODE 0x77665500
Matthew Qind886f3c2014-01-17 16:52:01 +0800116#define ALARM_BOOT 0x77665503
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800117
Kun Liang2f1601a2013-08-12 16:29:54 +0800118/* make 4096 as default size to ensure EFS,EXT4's erasing */
119#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800120#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800121
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700122#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700123#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800124
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800125#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
126
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700127#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
128
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800129#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700130static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700131#else
David Ng183a7422009-12-07 14:55:21 -0800132static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700133#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800134static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300135static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800136static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800137static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800138static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700139static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300140static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800141
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800142static const char *baseband_apq = " androidboot.baseband=apq";
143static const char *baseband_msm = " androidboot.baseband=msm";
144static const char *baseband_csfb = " androidboot.baseband=csfb";
145static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700146static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800147static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700148static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800149static const char *baseband_dsda = " androidboot.baseband=dsda";
150static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800151static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800152static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800153
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700154static unsigned page_size = 0;
155static unsigned page_mask = 0;
156static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
157static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700158static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800159static bool boot_reason_alarm;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700160
Shashank Mittalcd98d472011-08-02 14:29:24 -0700161/* Assuming unauthorized kernel image by default */
162static int auth_kernel_img = 0;
163
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530164static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}};
Shashank Mittal162244e2011-08-08 19:01:25 -0700165
Dima Zavin42168f22009-01-30 11:52:22 -0800166struct atag_ptbl_entry
167{
168 char name[16];
169 unsigned offset;
170 unsigned size;
171 unsigned flags;
172};
173
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700174/*
175 * Partition info, required to be published
176 * for fastboot
177 */
178struct getvar_partition_info {
179 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
180 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
181 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
182 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
183 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
184};
185
186/*
187 * Right now, we are publishing the info for only
188 * three partitions
189 */
190struct getvar_partition_info part_info[] =
191{
192 { "system" , "partition-size:", "partition-type:", "", "ext4" },
193 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
194 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
195};
196
197char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700198char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800199char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700200char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530201char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700202
Greg Griscod2471ef2011-07-14 13:00:42 -0700203extern int emmc_recovery_init(void);
204
Kinson Chik0b1c8162011-08-31 16:31:57 -0700205#if NO_KEYPAD_DRIVER
206extern int fastboot_trigger(void);
207#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700208
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800209static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700210{
211 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700212#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800213 if (is_arm64)
214 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
215 else
216 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700217 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
218 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700219#endif
220}
221
Dima Zavin42168f22009-01-30 11:52:22 -0800222static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
223{
224 struct atag_ptbl_entry atag_ptn;
225
226 memcpy(atag_ptn.name, ptn->name, 16);
227 atag_ptn.name[15] = '\0';
228 atag_ptn.offset = ptn->start;
229 atag_ptn.size = ptn->length;
230 atag_ptn.flags = ptn->flags;
231 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
232 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
233}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800234
Neeti Desaie245d492012-06-01 12:52:13 -0700235unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800236{
David Ng183a7422009-12-07 14:55:21 -0800237 int cmdline_len = 0;
238 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800239 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700240 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800241 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300242 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700243 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700244 char *boot_dev_buf = NULL;
Dima Zavin42168f22009-01-30 11:52:22 -0800245
Brian Swetland9c4c0752009-01-25 16:23:50 -0800246 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800247 cmdline_len = strlen(cmdline);
248 have_cmdline = 1;
249 }
250 if (target_is_emmc_boot()) {
251 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800252#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700253 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
254 ASSERT(boot_dev_buf);
255 platform_boot_dev_cmdline(boot_dev_buf);
256 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700257#endif
David Ng183a7422009-12-07 14:55:21 -0800258 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800259
260 cmdline_len += strlen(usb_sn_cmdline);
261 cmdline_len += strlen(sn_buf);
262
Pavel Nedev5614d222013-06-17 18:01:02 +0300263 if (boot_into_recovery && gpt_exists)
264 cmdline_len += strlen(secondary_gpt_enable);
265
Pavel Nedev328ac822013-04-05 15:25:11 +0300266 if (boot_into_ffbm) {
267 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700268 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800269 /* reduce kernel console messages to speed-up boot */
270 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800271 } else if (boot_reason_alarm) {
272 cmdline_len += strlen(alarmboot_cmdline);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700273 } else if (device.charger_screen_enabled &&
274 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700275 pause_at_bootup = 1;
276 cmdline_len += strlen(battchg_pause);
277 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800278
Shashank Mittalcd98d472011-08-02 14:29:24 -0700279 if(target_use_signed_kernel() && auth_kernel_img) {
280 cmdline_len += strlen(auth_kernel);
281 }
282
Joonwoo Park61112782013-10-02 19:50:39 -0700283 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
284 "system",
285 target_boot_params,
286 sizeof(target_boot_params)) == 0) {
287 have_target_boot_params = 1;
288 cmdline_len += strlen(target_boot_params);
289 }
290
Ajay Dudanid04110c2011-01-17 23:55:07 -0800291 /* Determine correct androidboot.baseband to use */
292 switch(target_baseband())
293 {
294 case BASEBAND_APQ:
295 cmdline_len += strlen(baseband_apq);
296 break;
297
298 case BASEBAND_MSM:
299 cmdline_len += strlen(baseband_msm);
300 break;
301
302 case BASEBAND_CSFB:
303 cmdline_len += strlen(baseband_csfb);
304 break;
305
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800306 case BASEBAND_SVLTE2A:
307 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800308 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700309
310 case BASEBAND_MDM:
311 cmdline_len += strlen(baseband_mdm);
312 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700313
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800314 case BASEBAND_MDM2:
315 cmdline_len += strlen(baseband_mdm2);
316 break;
317
Amol Jadi5c61a952012-05-04 17:05:35 -0700318 case BASEBAND_SGLTE:
319 cmdline_len += strlen(baseband_sglte);
320 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530321
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800322 case BASEBAND_SGLTE2:
323 cmdline_len += strlen(baseband_sglte2);
324 break;
325
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530326 case BASEBAND_DSDA:
327 cmdline_len += strlen(baseband_dsda);
328 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800329
330 case BASEBAND_DSDA2:
331 cmdline_len += strlen(baseband_dsda2);
332 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800333 }
334
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800335 if (cmdline) {
336 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
337 target_display_panel_node(device.display_panel,
338 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
339 strlen(display_panel_buf)) {
340 cmdline_len += strlen(display_panel_buf);
341 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700342 }
343
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800344 if (target_warm_boot()) {
345 warm_boot = true;
346 cmdline_len += strlen(warmboot_cmdline);
347 }
348
David Ng183a7422009-12-07 14:55:21 -0800349 if (cmdline_len > 0) {
350 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800351 unsigned char *dst;
352
353 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
354 ASSERT(cmdline_final != NULL);
355 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700356
Amol Jadi168b7712012-03-06 16:15:00 -0800357 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800358 if (have_cmdline) {
359 src = cmdline;
360 while ((*dst++ = *src++));
361 }
362 if (target_is_emmc_boot()) {
363 src = emmc_cmdline;
364 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700365 have_cmdline = 1;
366 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800367#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700368 src = boot_dev_buf;
369 if (have_cmdline) --dst;
370 while ((*dst++ = *src++));
371#endif
David Ngf773dde2010-07-26 19:55:08 -0700372 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800373
374 src = usb_sn_cmdline;
375 if (have_cmdline) --dst;
376 have_cmdline = 1;
377 while ((*dst++ = *src++));
378 src = sn_buf;
379 if (have_cmdline) --dst;
380 have_cmdline = 1;
381 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800382 if (warm_boot) {
383 if (have_cmdline) --dst;
384 src = warmboot_cmdline;
385 while ((*dst++ = *src++));
386 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800387
Pavel Nedev5614d222013-06-17 18:01:02 +0300388 if (boot_into_recovery && gpt_exists) {
389 src = secondary_gpt_enable;
390 if (have_cmdline) --dst;
391 while ((*dst++ = *src++));
392 }
393
Pavel Nedev328ac822013-04-05 15:25:11 +0300394 if (boot_into_ffbm) {
395 src = androidboot_mode;
396 if (have_cmdline) --dst;
397 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700398 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300399 if (have_cmdline) --dst;
400 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800401 src = loglevel;
402 if (have_cmdline) --dst;
403 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800404 } else if (boot_reason_alarm) {
405 src = alarmboot_cmdline;
406 if (have_cmdline) --dst;
407 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300408 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700409 src = battchg_pause;
410 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800411 while ((*dst++ = *src++));
412 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800413
Shashank Mittalcd98d472011-08-02 14:29:24 -0700414 if(target_use_signed_kernel() && auth_kernel_img) {
415 src = auth_kernel;
416 if (have_cmdline) --dst;
417 while ((*dst++ = *src++));
418 }
419
Ajay Dudanid04110c2011-01-17 23:55:07 -0800420 switch(target_baseband())
421 {
422 case BASEBAND_APQ:
423 src = baseband_apq;
424 if (have_cmdline) --dst;
425 while ((*dst++ = *src++));
426 break;
427
428 case BASEBAND_MSM:
429 src = baseband_msm;
430 if (have_cmdline) --dst;
431 while ((*dst++ = *src++));
432 break;
433
434 case BASEBAND_CSFB:
435 src = baseband_csfb;
436 if (have_cmdline) --dst;
437 while ((*dst++ = *src++));
438 break;
439
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800440 case BASEBAND_SVLTE2A:
441 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800442 if (have_cmdline) --dst;
443 while ((*dst++ = *src++));
444 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700445
446 case BASEBAND_MDM:
447 src = baseband_mdm;
448 if (have_cmdline) --dst;
449 while ((*dst++ = *src++));
450 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700451
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800452 case BASEBAND_MDM2:
453 src = baseband_mdm2;
454 if (have_cmdline) --dst;
455 while ((*dst++ = *src++));
456 break;
457
Amol Jadi5c61a952012-05-04 17:05:35 -0700458 case BASEBAND_SGLTE:
459 src = baseband_sglte;
460 if (have_cmdline) --dst;
461 while ((*dst++ = *src++));
462 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530463
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800464 case BASEBAND_SGLTE2:
465 src = baseband_sglte2;
466 if (have_cmdline) --dst;
467 while ((*dst++ = *src++));
468 break;
469
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530470 case BASEBAND_DSDA:
471 src = baseband_dsda;
472 if (have_cmdline) --dst;
473 while ((*dst++ = *src++));
474 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800475
476 case BASEBAND_DSDA2:
477 src = baseband_dsda2;
478 if (have_cmdline) --dst;
479 while ((*dst++ = *src++));
480 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800481 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700482
483 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700484 src = display_panel_buf;
485 if (have_cmdline) --dst;
486 while ((*dst++ = *src++));
487 }
Joonwoo Park61112782013-10-02 19:50:39 -0700488
489 if (have_target_boot_params) {
490 if (have_cmdline) --dst;
491 src = target_boot_params;
492 while ((*dst++ = *src++));
493 }
Neeti Desaie245d492012-06-01 12:52:13 -0700494 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700495
496
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700497 if (boot_dev_buf)
498 free(boot_dev_buf);
499
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800500 if (cmdline_final)
501 dprintf(INFO, "cmdline: %s\n", cmdline_final);
502 else
503 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700504 return cmdline_final;
505}
506
507unsigned *atag_core(unsigned *ptr)
508{
509 /* CORE */
510 *ptr++ = 2;
511 *ptr++ = 0x54410001;
512
513 return ptr;
514
515}
516
517unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
518 unsigned ramdisk_size)
519{
520 if (ramdisk_size) {
521 *ptr++ = 4;
522 *ptr++ = 0x54420005;
523 *ptr++ = (unsigned)ramdisk;
524 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800525 }
526
Neeti Desaie245d492012-06-01 12:52:13 -0700527 return ptr;
528}
529
530unsigned *atag_ptable(unsigned **ptr_addr)
531{
532 int i;
533 struct ptable *ptable;
534
535 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700536 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
537 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700538 *(*ptr_addr)++ = 0x4d534d70;
539 for (i = 0; i < ptable->count; ++i)
540 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
541 }
542
543 return (*ptr_addr);
544}
545
546unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
547{
548 int cmdline_length = 0;
549 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700550 char *dest;
551
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800552 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700553 n = (cmdline_length + 4) & (~3);
554
555 *ptr++ = (n / 4) + 2;
556 *ptr++ = 0x54410009;
557 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800558 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700559 ptr += (n / 4);
560
561 return ptr;
562}
563
564unsigned *atag_end(unsigned *ptr)
565{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800566 /* END */
567 *ptr++ = 0;
568 *ptr++ = 0;
569
Neeti Desaie245d492012-06-01 12:52:13 -0700570 return ptr;
571}
572
573void generate_atags(unsigned *ptr, const char *cmdline,
574 void *ramdisk, unsigned ramdisk_size)
575{
576
577 ptr = atag_core(ptr);
578 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
579 ptr = target_atag_mem(ptr);
580
581 /* Skip NAND partition ATAGS for eMMC boot */
582 if (!target_is_emmc_boot()){
583 ptr = atag_ptable(&ptr);
584 }
585
586 ptr = atag_cmdline(ptr, cmdline);
587 ptr = atag_end(ptr);
588}
589
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700590typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700591void boot_linux(void *kernel, unsigned *tags,
592 const char *cmdline, unsigned machtype,
593 void *ramdisk, unsigned ramdisk_size)
594{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800595 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800596#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700597 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800598#endif
599
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700600 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800601 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800602 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800603
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530604 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700605
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800606 final_cmdline = update_cmdline((const char*)cmdline);
607
Neeti Desai17379b82012-06-04 18:42:53 -0700608#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800609 dprintf(INFO, "Updating device tree: start\n");
610
Neeti Desai17379b82012-06-04 18:42:53 -0700611 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530612 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700613 if(ret)
614 {
615 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
616 ASSERT(0);
617 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800618 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700619#else
Neeti Desaie245d492012-06-01 12:52:13 -0700620 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800621 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700622#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700623
Maria Yu52254c02014-07-04 16:14:54 +0800624 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700625
626#if VERIFIED_BOOT
627 /* Write protect the device info */
Channagoud Kadabibc4f15f2015-02-25 12:09:51 -0800628 if (target_build_variant_user() && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700629 {
630 dprintf(INFO, "Failed to write protect dev info\n");
631 ASSERT(0);
632 }
633#endif
634
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700635 /* Perform target specific cleanup */
636 target_uninit();
637
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800638 /* Turn off splash screen if enabled */
639#if DISPLAY_SPLASH_SCREEN
640 target_display_shutdown();
641#endif
642
643
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800644 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530645 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800646
647 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700648
Amol Jadi4421e652011-06-16 15:00:48 -0700649 /* do any platform specific cleanup before kernel entry */
650 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700651
Brian Swetland9c4c0752009-01-25 16:23:50 -0800652 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700653
Amol Jadi504f9fe2012-08-16 13:56:48 -0700654#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800655 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700656#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700657 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800658
659 if (IS_ARM64(kptr))
660 /* Jump to a 64bit kernel */
661 scm_elexec_call((paddr_t)kernel, tags_phys);
662 else
663 /* Jump to a 32bit kernel */
664 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800665}
666
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700667/* Function to check if the memory address range falls within the aboot
668 * boundaries.
669 * start: Start of the memory region
670 * size: Size of the memory region
671 */
672int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
673{
674 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800675 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700676 return -1;
677
678 /* Check for memory overlap. */
679 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
680 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700681 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700682 return 0;
683 else
684 return -1;
685}
686
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800687#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800688
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800689BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800690#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800691BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800692#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800693
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700694static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
695{
696 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800697
698#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800699#if IMAGE_VERIF_ALGO_SHA1
700 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
701#else
702 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
703#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800704#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700705
706 /* Assume device is rooted at this time. */
707 device.is_tampered = 1;
708
709 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
710
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700711#if VERIFIED_BOOT
712 if(boot_into_recovery)
713 {
714 ret = boot_verify_image((unsigned char *)bootimg_addr,
715 bootimg_size, "recovery");
716 }
717 else
718 {
719 ret = boot_verify_image((unsigned char *)bootimg_addr,
720 bootimg_size, "boot");
721 }
722 boot_verify_print_state();
723#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700724 ret = image_verify((unsigned char *)bootimg_addr,
725 (unsigned char *)(bootimg_addr + bootimg_size),
726 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800727 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700728#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700729 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
730
731 if (ret)
732 {
733 /* Authorized kernel */
734 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700735 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700736 }
737
738#if USE_PCOM_SECBOOT
739 set_tamper_flag(device.is_tampered);
740#endif
741
742 if(device.is_tampered)
743 {
744 write_device_info_mmc(&device);
745 #ifdef TZ_TAMPER_FUSE
746 set_tamper_fuse_cmd();
747 #endif
748 #ifdef ASSERT_ON_TAMPER
749 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
750 ASSERT(0);
751 #endif
752 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700753
754#if VERIFIED_BOOT
755 if(boot_verify_get_state() == RED)
756 {
757 if(!boot_into_recovery)
758 {
759 dprintf(CRITICAL,
760 "Device verification failed. Rebooting into recovery.\n");
761 reboot_device(RECOVERY_MODE);
762 }
763 else
764 {
765 dprintf(CRITICAL,
766 "Recovery image verification failed. Asserting..\n");
767 ASSERT(0);
768 }
769 }
770#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700771}
772
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530773static bool check_format_bit()
774{
775 bool ret = false;
776 int index;
777 uint64_t offset;
778 struct boot_selection_info *in = NULL;
779 char *buf = NULL;
780
781 index = partition_get_index("bootselect");
782 if (index == INVALID_PTN)
783 {
784 dprintf(INFO, "Unable to locate /bootselect partition\n");
785 return ret;
786 }
787 offset = partition_get_offset(index);
788 if(!offset)
789 {
790 dprintf(INFO, "partition /bootselect doesn't exist\n");
791 return ret;
792 }
793 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
794 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530795 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530796 {
797 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
798 free(buf);
799 return ret;
800 }
801 in = (struct boot_selection_info *) buf;
802 if ((in->signature == BOOTSELECT_SIGNATURE) &&
803 (in->version == BOOTSELECT_VERSION)) {
804 if ((in->state_info & BOOTSELECT_FORMAT) &&
805 !(in->state_info & BOOTSELECT_FACTORY))
806 ret = true;
807 } else {
808 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
809 in->signature, in->version);
810 ASSERT(0);
811 }
812 free(buf);
813 return ret;
814}
815
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700816void boot_verifier_init()
817{
818
819 uint32_t boot_state;
820 /* Check if device unlock */
821 if(device.is_unlocked)
822 {
823 boot_verify_send_event(DEV_UNLOCK);
824 boot_verify_print_state();
825 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
826 return;
827 }
828 else
829 {
830 boot_verify_send_event(BOOT_INIT);
831 }
832
833 /* Initialize keystore */
834 boot_state = boot_verify_keystore_init();
835 if(boot_state == YELLOW)
836 {
837 boot_verify_print_state();
838 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
839 }
840}
841
Shashank Mittal23b8f422010-04-16 19:27:21 -0700842int boot_linux_from_mmc(void)
843{
844 struct boot_img_hdr *hdr = (void*) buf;
845 struct boot_img_hdr *uhdr;
846 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700847 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700848 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700849 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700850
Shashank Mittalcd98d472011-08-02 14:29:24 -0700851 unsigned char *image_addr = 0;
852 unsigned kernel_actual;
853 unsigned ramdisk_actual;
854 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700855 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700856
857#if DEVICE_TREE
858 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700859 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700860 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800861 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700862 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700863#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800864 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
865 struct kernel64_hdr *kptr = (void*) kbuf;
866
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530867 if (check_format_bit())
868 boot_into_recovery = 1;
869
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700870 if (!boot_into_recovery) {
871 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
872 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
873 if (rcode <= 0) {
874 boot_into_ffbm = false;
875 if (rcode < 0)
876 dprintf(CRITICAL,"failed to get ffbm cookie");
877 } else
878 boot_into_ffbm = true;
879 } else
880 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700881 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
882 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
883 dprintf(INFO, "Unified boot method!\n");
884 hdr = uhdr;
885 goto unified_boot;
886 }
Greg Griscod6250552011-06-29 14:40:23 -0700887 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700888 index = partition_get_index("boot");
889 ptn = partition_get_offset(index);
890 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700891 dprintf(CRITICAL, "ERROR: No boot partition found\n");
892 return -1;
893 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700894 }
895 else {
896 index = partition_get_index("recovery");
897 ptn = partition_get_offset(index);
898 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700899 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
900 return -1;
901 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700902 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -0800903 /* Set Lun for boot & recovery partitions */
904 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -0700905
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530906 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700907 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
908 return -1;
909 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700910
911 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700912 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700913 return -1;
914 }
915
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700916 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530917
918 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
919 dprintf(CRITICAL, "ERROR: Invalid page size\n");
920 return -1;
921 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700922 page_size = hdr->page_size;
923 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700924 }
925
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800926 /* Read the next page to get kernel Image header
927 * which lives in the second page for arm64 targets.
928 */
929
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530930 if (mmc_read(ptn + page_size, (uint32_t *) kbuf, page_size)) {
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800931 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
932 return -1;
933 }
934
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700935 /*
936 * Update the kernel/ramdisk/tags address if the boot image header
937 * has default values, these default values come from mkbootimg when
938 * the boot image is flashed using fastboot flash:raw
939 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800940 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700941
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700942 /* Get virtual addresses since the hdr saves physical addresses. */
943 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
944 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
945 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700946
947 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
948 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
949
950 /* Check if the addresses in the header are valid. */
951 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
952 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
953 {
954 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
955 return -1;
956 }
957
958#ifndef DEVICE_TREE
959 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
960 {
961 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
962 return -1;
963 }
964#endif
965
Shashank Mittalcd98d472011-08-02 14:29:24 -0700966 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300967 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
968 (int) target_use_signed_kernel(),
969 device.is_unlocked,
970 device.is_tampered);
971
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700972#if VERIFIED_BOOT
973 boot_verifier_init();
974#endif
975
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700976 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800977 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700978 offset = 0;
979
Shashank Mittalcd98d472011-08-02 14:29:24 -0700980 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700981
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800982#if DEVICE_TREE
983 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
984 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700985
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700986 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700987 {
988 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
989 return -1;
990 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800991#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530992 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700993
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700994#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700995
Amol Jadib6be5c12012-11-14 13:39:51 -0800996 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700997 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800998
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530999 if (check_aboot_addr_range_overlap((uint32_t)image_addr, imagesize_actual))
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -07001000 {
1001 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1002 return -1;
1003 }
1004
Shashank Mittalcd98d472011-08-02 14:29:24 -07001005 /* Read image without signature */
1006 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1007 {
1008 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1009 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -08001010 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001011
Amol Jadib6be5c12012-11-14 13:39:51 -08001012 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001013 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001014
Shashank Mittalcd98d472011-08-02 14:29:24 -07001015 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -07001016
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301017 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -07001018 {
1019 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1020 return -1;
1021 }
1022
Shashank Mittalcd98d472011-08-02 14:29:24 -07001023 /* Read signature */
1024 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1025 {
1026 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001027 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001028 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001029
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301030 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001031
Neeti Desai465491e2012-07-31 12:53:35 -07001032 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -07001033 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1034 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -07001035
Neeti Desai465491e2012-07-31 12:53:35 -07001036 #if DEVICE_TREE
1037 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001038 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -07001039 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001040
Deepa Dinamani19648b42013-09-05 17:05:55 -07001041 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001042 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1043 return -1;
1044 }
Deepa Dinamani19648b42013-09-05 17:05:55 -07001045
Neeti Desai465491e2012-07-31 12:53:35 -07001046 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -07001047 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001048 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
1049 return -1;
1050 }
1051
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001052 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001053 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001054 {
1055 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1056 return -1;
1057 }
1058
Joel Kingaa335dc2013-06-03 16:11:08 -07001059 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001060 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -08001061 /*
1062 * If appended dev tree is found, update the atags with
1063 * memory address to the DTB appended location on RAM.
1064 * Else update with the atags address in the kernel header
1065 */
1066 void *dtb;
1067 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001068 hdr->kernel_size,
1069 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001070 if (!dtb) {
1071 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1072 return -1;
1073 }
Neeti Desai465491e2012-07-31 12:53:35 -07001074 }
1075 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001076 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001077 else
1078 {
Amol Jadib6be5c12012-11-14 13:39:51 -08001079 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001080
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001081 image_addr = (unsigned char *)target_get_scratch_address();
1082#if DEVICE_TREE
1083 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1084 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1085
1086 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
1087 {
1088 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1089 return -1;
1090 }
1091#else
1092 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1093
1094#endif
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001095 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001096 {
1097 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1098 return -1;
1099 }
1100
Amol Jadib6be5c12012-11-14 13:39:51 -08001101 dprintf(INFO, "Loading boot image (%d): start\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001102 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001103 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001104
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001105 offset = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001106
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001107 /* Load the entire boot image */
1108 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual)) {
1109 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
Shashank Mittalcd98d472011-08-02 14:29:24 -07001110 return -1;
1111 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001112
1113 dprintf(INFO, "Loading boot image (%d): done\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001114 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001115 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -07001116
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001117 #ifdef TZ_SAVE_KERNEL_HASH
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001118 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001119 #endif /* TZ_SAVE_KERNEL_HASH */
1120
1121 /* Move kernel, ramdisk and device tree to correct address */
1122 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1123 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Neeti Desai465491e2012-07-31 12:53:35 -07001124
1125 #if DEVICE_TREE
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001126 if(hdr->dt_size) {
1127 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1128 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001129
Deepa Dinamani19648b42013-09-05 17:05:55 -07001130 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001131 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1132 return -1;
1133 }
1134
Joel Kingaa335dc2013-06-03 16:11:08 -07001135 /* Find index of device tree within device tree table */
1136 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001137 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1138 return -1;
1139 }
1140
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001141 /* Validate and Read device device tree in the tags_addr */
Joel Kingaa335dc2013-06-03 16:11:08 -07001142 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001143 {
1144 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1145 return -1;
1146 }
1147
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001148 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001149 } else {
Aparna Mallavarapu33127372014-06-03 22:15:54 +05301150 /* Validate the tags_addr */
1151 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1152 {
1153 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1154 return -1;
1155 }
Dima Zavin77e41f32013-03-06 16:10:43 -08001156 /*
1157 * If appended dev tree is found, update the atags with
1158 * memory address to the DTB appended location on RAM.
1159 * Else update with the atags address in the kernel header
1160 */
1161 void *dtb;
1162 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001163 kernel_actual,
1164 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001165 if (!dtb) {
1166 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1167 return -1;
1168 }
Neeti Desai465491e2012-07-31 12:53:35 -07001169 }
1170 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001171 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001172
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001173 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1174 target_load_ssd_keystore();
1175
Shashank Mittal23b8f422010-04-16 19:27:21 -07001176unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001177
Dima Zavin77e41f32013-03-06 16:10:43 -08001178 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001179 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001180 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1181
1182 return 0;
1183}
1184
Dima Zavin214cc642009-01-26 11:16:21 -08001185int boot_linux_from_flash(void)
1186{
1187 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001188 struct ptentry *ptn;
1189 struct ptable *ptable;
1190 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001191
Shashank Mittalcd98d472011-08-02 14:29:24 -07001192 unsigned char *image_addr = 0;
1193 unsigned kernel_actual;
1194 unsigned ramdisk_actual;
1195 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001196 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001197
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001198#if DEVICE_TREE
1199 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001200 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001201 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001202 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001203#endif
1204
David Ng183a7422009-12-07 14:55:21 -08001205 if (target_is_emmc_boot()) {
1206 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1207 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1208 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1209 return -1;
1210 }
1211 goto continue_boot;
1212 }
1213
Dima Zavin214cc642009-01-26 11:16:21 -08001214 ptable = flash_get_ptable();
1215 if (ptable == NULL) {
1216 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1217 return -1;
1218 }
1219
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001220 if(!boot_into_recovery)
1221 {
1222 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001223
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001224 if (ptn == NULL) {
1225 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1226 return -1;
1227 }
1228 }
1229 else
1230 {
1231 ptn = ptable_find(ptable, "recovery");
1232 if (ptn == NULL) {
1233 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1234 return -1;
1235 }
Dima Zavin214cc642009-01-26 11:16:21 -08001236 }
1237
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001238 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001239 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1240 return -1;
1241 }
Dima Zavin214cc642009-01-26 11:16:21 -08001242
1243 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001244 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001245 return -1;
1246 }
1247
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001248 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001249 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 -08001250 return -1;
1251 }
1252
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001253 /*
1254 * Update the kernel/ramdisk/tags address if the boot image header
1255 * has default values, these default values come from mkbootimg when
1256 * the boot image is flashed using fastboot flash:raw
1257 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001258 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001259
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001260 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001261 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1262 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1263 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1264
1265 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1266 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1267
1268 /* Check if the addresses in the header are valid. */
1269 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1270 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1271 {
1272 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1273 return -1;
1274 }
1275
1276#ifndef DEVICE_TREE
1277 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1278 {
1279 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1280 return -1;
1281 }
1282#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001283
Shashank Mittalcd98d472011-08-02 14:29:24 -07001284 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001285 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001286 {
1287 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001288 offset = 0;
1289
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001290#if DEVICE_TREE
1291 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1292 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001293
1294 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1295 {
1296 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1297 return -1;
1298 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001299#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001300 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001301#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001302
Amol Jadib6be5c12012-11-14 13:39:51 -08001303 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001304 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001305
Shashank Mittalcd98d472011-08-02 14:29:24 -07001306 /* Read image without signature */
1307 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1308 {
1309 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1310 return -1;
1311 }
Dima Zavin214cc642009-01-26 11:16:21 -08001312
Amol Jadib6be5c12012-11-14 13:39:51 -08001313 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001314 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001315
Shashank Mittalcd98d472011-08-02 14:29:24 -07001316 offset = imagesize_actual;
1317 /* Read signature */
1318 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1319 {
1320 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001321 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001322 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001323
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301324 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001325
1326 /* Move kernel and ramdisk to correct address */
1327 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1328 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001329#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001330 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001331 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001332 {
1333 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1334 return -1;
1335 }
1336
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001337 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1338#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001339
1340 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001341 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001342 {
1343 write_device_info_flash(&device);
1344 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301345#if USE_PCOM_SECBOOT
1346 set_tamper_flag(device.is_tampered);
1347#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001348 }
1349 else
1350 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001351 offset = page_size;
1352
Amol Jadib6be5c12012-11-14 13:39:51 -08001353 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1354 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1355 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1356
1357 dprintf(INFO, "Loading boot image (%d): start\n",
1358 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001359 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001360
1361 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001362 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1363 return -1;
1364 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001365 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001366
Amol Jadib6be5c12012-11-14 13:39:51 -08001367 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001368 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1369 return -1;
1370 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001371 offset += ramdisk_actual;
1372
1373 dprintf(INFO, "Loading boot image (%d): done\n",
1374 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001375 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001376
1377 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001378 offset += second_actual;
1379 /* Second image loading not implemented. */
1380 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001381 }
1382
1383#if DEVICE_TREE
1384 if(hdr->dt_size != 0) {
1385
1386 /* Read the device tree table into buffer */
1387 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1388 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1389 return -1;
1390 }
1391
1392 table = (struct dt_table*) dt_buf;
1393
Deepa Dinamani19648b42013-09-05 17:05:55 -07001394 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001395 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1396 return -1;
1397 }
1398
Deepa Dinamani19648b42013-09-05 17:05:55 -07001399 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1400 if (!table)
1401 return -1;
1402
1403 /* Read the entire device tree table into buffer */
1404 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1405 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1406 return -1;
1407 }
1408
1409
Joel Kingaa335dc2013-06-03 16:11:08 -07001410 /* Find index of device tree within device tree table */
1411 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001412 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1413 return -1;
1414 }
1415
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001416 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001417 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001418 {
1419 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1420 return -1;
1421 }
1422
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001423 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001424 if(flash_read(ptn, offset + dt_entry.offset,
1425 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001426 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1427 return -1;
1428 }
1429 }
1430#endif
1431
Shashank Mittalcd98d472011-08-02 14:29:24 -07001432 }
David Ng183a7422009-12-07 14:55:21 -08001433continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001434
Dima Zavin214cc642009-01-26 11:16:21 -08001435 /* TODO: create/pass atags to kernel */
1436
Ajay Dudanie28a6072011-07-01 13:59:46 -07001437 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001438 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001439 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1440
1441 return 0;
1442}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001443
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001444BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001445void write_device_info_mmc(device_info *dev)
1446{
1447 struct device_info *info = (void*) info_buf;
1448 unsigned long long ptn = 0;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001449#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001450 unsigned long long size;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001451#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001452 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001453 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001454 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001455
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001456#if VERIFIED_BOOT
1457 index = partition_get_index("devinfo");
1458#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001459 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001460#endif
1461
Shashank Mittal162244e2011-08-08 19:01:25 -07001462 ptn = partition_get_offset(index);
1463 if(ptn == 0)
1464 {
1465 return;
1466 }
1467
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001468 lun = partition_get_lun(index);
1469 mmc_set_lun(lun);
1470
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001471#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001472 size = partition_get_size(index);
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001473#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001474
1475 memcpy(info, dev, sizeof(device_info));
1476
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001477 blocksize = mmc_get_device_blocksize();
1478
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001479#if VERIFIED_BOOT
1480 if(mmc_write(ptn, blocksize, (void *)info_buf))
1481#else
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001482 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001483#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001484 {
1485 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1486 return;
1487 }
1488}
1489
1490void read_device_info_mmc(device_info *dev)
1491{
1492 struct device_info *info = (void*) info_buf;
1493 unsigned long long ptn = 0;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001494#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001495 unsigned long long size;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001496#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001497 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001498 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001499
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001500#if VERIFIED_BOOT
1501 index = partition_get_index("devinfo");
1502#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001503 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001504#endif
1505
Shashank Mittal162244e2011-08-08 19:01:25 -07001506 ptn = partition_get_offset(index);
1507 if(ptn == 0)
1508 {
1509 return;
1510 }
1511
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001512 mmc_set_lun(partition_get_lun(index));
1513
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001514#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001515 size = partition_get_size(index);
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001516#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001517
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001518 blocksize = mmc_get_device_blocksize();
1519
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001520#if VERIFIED_BOOT
1521 if(mmc_read(ptn, (void *)info_buf, blocksize))
1522#else
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001523 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001524#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001525 {
1526 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1527 return;
1528 }
1529
1530 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1531 {
1532 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
Shashank Mittal7e033062014-09-25 19:58:07 -07001533#if DEFAULT_UNLOCK
1534 info->is_unlocked = 1;
1535#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001536 info->is_unlocked = 0;
Shashank Mittal7e033062014-09-25 19:58:07 -07001537#endif
1538 info->is_verified = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001539 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001540 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001541
1542 write_device_info_mmc(info);
1543 }
1544 memcpy(dev, info, sizeof(device_info));
1545}
1546
1547void write_device_info_flash(device_info *dev)
1548{
1549 struct device_info *info = (void *) info_buf;
1550 struct ptentry *ptn;
1551 struct ptable *ptable;
1552
1553 ptable = flash_get_ptable();
1554 if (ptable == NULL)
1555 {
1556 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1557 return;
1558 }
1559
1560 ptn = ptable_find(ptable, "devinfo");
1561 if (ptn == NULL)
1562 {
1563 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1564 return;
1565 }
1566
1567 memcpy(info, dev, sizeof(device_info));
1568
1569 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1570 {
1571 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1572 return;
1573 }
1574}
1575
1576void read_device_info_flash(device_info *dev)
1577{
1578 struct device_info *info = (void*) info_buf;
1579 struct ptentry *ptn;
1580 struct ptable *ptable;
1581
1582 ptable = flash_get_ptable();
1583 if (ptable == NULL)
1584 {
1585 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1586 return;
1587 }
1588
1589 ptn = ptable_find(ptable, "devinfo");
1590 if (ptn == NULL)
1591 {
1592 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1593 return;
1594 }
1595
1596 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1597 {
1598 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1599 return;
1600 }
1601
1602 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1603 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001604 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1605 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001606 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001607 write_device_info_flash(info);
1608 }
1609 memcpy(dev, info, sizeof(device_info));
1610}
1611
1612void write_device_info(device_info *dev)
1613{
1614 if(target_is_emmc_boot())
1615 {
1616 write_device_info_mmc(dev);
1617 }
1618 else
1619 {
1620 write_device_info_flash(dev);
1621 }
1622}
1623
1624void read_device_info(device_info *dev)
1625{
1626 if(target_is_emmc_boot())
1627 {
1628 read_device_info_mmc(dev);
1629 }
1630 else
1631 {
1632 read_device_info_flash(dev);
1633 }
1634}
1635
1636void reset_device_info()
1637{
1638 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001639 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001640 write_device_info(&device);
1641}
1642
1643void set_device_root()
1644{
1645 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001646 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001647 write_device_info(&device);
1648}
1649
Amol Jadicb524072012-08-09 16:40:18 -07001650#if DEVICE_TREE
1651int copy_dtb(uint8_t *boot_image_start)
1652{
1653 uint32 dt_image_offset = 0;
1654 uint32_t n;
1655 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001656 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001657 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001658
1659 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1660
Amol Jadicb524072012-08-09 16:40:18 -07001661 if(hdr->dt_size != 0) {
1662
1663 /* add kernel offset */
1664 dt_image_offset += page_size;
1665 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1666 dt_image_offset += n;
1667
1668 /* add ramdisk offset */
1669 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1670 dt_image_offset += n;
1671
1672 /* add second offset */
1673 if(hdr->second_size != 0) {
1674 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1675 dt_image_offset += n;
1676 }
1677
1678 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001679 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001680
Deepa Dinamani19648b42013-09-05 17:05:55 -07001681 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001682 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1683 return -1;
1684 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001685 /* Find index of device tree within device tree table */
1686 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001687 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1688 return -1;
1689 }
1690
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001691 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001692 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001693 {
1694 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1695 return -1;
1696 }
1697
Amol Jadicb524072012-08-09 16:40:18 -07001698 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001699 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001700 boot_image_start + dt_image_offset + dt_entry.offset,
1701 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001702 } else
1703 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001704
1705 /* Everything looks fine. Return success. */
1706 return 0;
1707}
1708#endif
1709
Brian Swetland9c4c0752009-01-25 16:23:50 -08001710void cmd_boot(const char *arg, void *data, unsigned sz)
1711{
1712 unsigned kernel_actual;
1713 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001714 uint32_t image_actual;
1715 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001716 uint32_t sig_actual = SIGNATURE_SIZE;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001717 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001718 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001719 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001720 int ret = 0;
1721 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001722
Amit Blay6281ebc2015-01-11 14:44:08 +02001723#ifdef MDTP_SUPPORT
1724 /* Go through Firmware Lock verification before continue with boot process */
1725 mdtp_fwlock_verify_lock();
1726 fbcon_clear();
1727#endif /* MDTP_SUPPORT */
1728
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001729#if VERIFIED_BOOT
1730 if(!device.is_unlocked)
1731 {
1732 fastboot_fail("unlock device to use this command");
1733 return;
1734 }
1735#endif
1736
Brian Swetland9c4c0752009-01-25 16:23:50 -08001737 if (sz < sizeof(hdr)) {
1738 fastboot_fail("invalid bootimage header");
1739 return;
1740 }
1741
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001742 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001743
1744 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001745 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001746
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001747 if(target_is_emmc_boot() && hdr->page_size) {
1748 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001749 page_mask = page_size - 1;
1750 }
1751
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001752 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1753 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001754#if DEVICE_TREE
1755 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1756#endif
1757
1758 image_actual = ADD_OF(page_size, kernel_actual);
1759 image_actual = ADD_OF(image_actual, ramdisk_actual);
1760 image_actual = ADD_OF(image_actual, dt_actual);
1761
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001762 if (target_use_signed_kernel() && (!device.is_unlocked))
1763 image_actual = ADD_OF(image_actual, sig_actual);
1764
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001765 /* sz should have atleast raw boot image */
1766 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001767 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001768 return;
1769 }
1770
1771 /* Verify the boot image
1772 * device & page_size are initialized in aboot_init
1773 */
1774 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001775 /* Pass size excluding signature size, otherwise we would try to
1776 * access signature beyond its length
1777 */
1778 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001779
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001780 /*
1781 * Update the kernel/ramdisk/tags address if the boot image header
1782 * has default values, these default values come from mkbootimg when
1783 * the boot image is flashed using fastboot flash:raw
1784 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001785 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1786 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001787
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001788 /* Get virtual addresses since the hdr saves physical addresses. */
1789 hdr->kernel_addr = VA(hdr->kernel_addr);
1790 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1791 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001792
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001793 /* Check if the addresses in the header are valid. */
1794 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1795 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1796 {
1797 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001798 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001799 }
1800
Amol Jadicb524072012-08-09 16:40:18 -07001801#if DEVICE_TREE
1802 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001803 ret = copy_dtb(data);
1804
1805 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001806#else
1807 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1808 {
1809 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001810 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001811 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001812#endif
1813
1814 /* Load ramdisk & kernel */
1815 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1816 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1817
1818#if DEVICE_TREE
1819 /*
1820 * If dtb is not found look for appended DTB in the kernel.
1821 * If appended dev tree is found, update the atags with
1822 * memory address to the DTB appended location on RAM.
1823 * Else update with the atags address in the kernel header
1824 */
1825 if (!dtb_copied) {
1826 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001827 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1828 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001829 if (!dtb) {
1830 fastboot_fail("dtb not found");
1831 return;
1832 }
Amol Jadicb524072012-08-09 16:40:18 -07001833 }
1834#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001835
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001836#ifndef DEVICE_TREE
1837 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1838 {
1839 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001840 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001841 }
1842#endif
1843
Brian Swetland9c4c0752009-01-25 16:23:50 -08001844 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001845 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001846
Dima Zavin77e41f32013-03-06 16:10:43 -08001847 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001848 (const char*) hdr->cmdline, board_machtype(),
1849 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001850}
1851
Sridhar Parasurame94e8152014-10-24 14:06:03 -07001852void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08001853{
1854 struct ptentry *ptn;
1855 struct ptable *ptable;
1856
1857 ptable = flash_get_ptable();
1858 if (ptable == NULL) {
1859 fastboot_fail("partition table doesn't exist");
1860 return;
1861 }
1862
1863 ptn = ptable_find(ptable, arg);
1864 if (ptn == NULL) {
1865 fastboot_fail("unknown partition name");
1866 return;
1867 }
1868
1869 if (flash_erase(ptn)) {
1870 fastboot_fail("failed to erase partition");
1871 return;
1872 }
1873 fastboot_okay("");
1874}
1875
Bikas Gurungd48bd242010-09-04 19:54:32 -07001876
1877void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1878{
1879 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001880 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001881 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001882 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001883
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001884#if VERIFIED_BOOT
1885 if(!strcmp(arg, KEYSTORE_PTN_NAME))
1886 {
1887 if(!device.is_unlocked)
1888 {
1889 fastboot_fail("unlock device to erase keystore");
1890 return;
1891 }
1892 }
1893#endif
1894
Kinson Chikf1a43512011-07-14 11:28:39 -07001895 index = partition_get_index(arg);
1896 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001897 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001898
Kinson Chikf1a43512011-07-14 11:28:39 -07001899 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001900 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001901 return;
1902 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001903
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001904 lun = partition_get_lun(index);
1905 mmc_set_lun(lun);
1906
Sridhar Parasuramd7957122015-02-27 11:33:40 -08001907 if (platform_boot_dev_isemmc())
1908 {
1909 if (mmc_erase_card(ptn, size)) {
1910 fastboot_fail("failed to erase partition\n");
1911 return;
1912 }
1913 } else {
1914 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
1915 size = partition_get_size(index);
1916 if (size > DEFAULT_ERASE_SIZE)
1917 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08001918
Sridhar Parasuramd7957122015-02-27 11:33:40 -08001919 /* Simple inefficient version of erase. Just writing
1920 0 in first several blocks */
1921 if (mmc_write(ptn , size, (unsigned int *)out)) {
1922 fastboot_fail("failed to erase partition");
1923 return;
1924 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07001925 }
1926 fastboot_okay("");
1927}
1928
Sridhar Parasurame94e8152014-10-24 14:06:03 -07001929void cmd_erase(const char *arg, void *data, unsigned sz)
1930{
1931 if(target_is_emmc_boot())
1932 cmd_erase_mmc(arg, data, sz);
1933 else
1934 cmd_erase_nand(arg, data, sz);
1935}
Bikas Gurungd48bd242010-09-04 19:54:32 -07001936
Ajay Dudani5c761132011-04-07 20:19:04 -07001937void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001938{
1939 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001940 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001941 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001942 char *token = NULL;
1943 char *pname = NULL;
1944 uint8_t lun = 0;
1945 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001946
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301947 token = strtok((char *)arg, ":");
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001948 pname = token;
1949 token = strtok(NULL, ":");
1950 if(token)
1951 {
1952 lun = atoi(token);
1953 mmc_set_lun(lun);
1954 lun_set = true;
1955 }
1956
Mao Jinlong226f33a2014-07-04 17:24:10 +08001957 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07001958 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08001959 if (!strcmp(pname, "partition"))
1960 {
1961 dprintf(INFO, "Attempt to write partition image.\n");
1962 if (write_partition(sz, (unsigned char *) data)) {
1963 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07001964 return;
1965 }
1966 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08001967 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001968 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001969#if VERIFIED_BOOT
1970 if(!strcmp(pname, KEYSTORE_PTN_NAME))
1971 {
1972 if(!device.is_unlocked)
1973 {
1974 fastboot_fail("unlock device to flash keystore");
1975 return;
1976 }
1977 if(!boot_verify_validate_keystore((unsigned char *)data))
1978 {
1979 fastboot_fail("image is not a keystore file");
1980 return;
1981 }
1982 }
1983#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08001984 index = partition_get_index(pname);
1985 ptn = partition_get_offset(index);
1986 if(ptn == 0) {
1987 fastboot_fail("partition table doesn't exist");
1988 return;
1989 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001990
Mao Jinlong226f33a2014-07-04 17:24:10 +08001991 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
1992 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1993 fastboot_fail("image is not a boot image");
1994 return;
1995 }
1996 }
1997
1998 if(!lun_set)
1999 {
2000 lun = partition_get_lun(index);
2001 mmc_set_lun(lun);
2002 }
2003
2004 size = partition_get_size(index);
2005 if (ROUND_TO_PAGE(sz,511) > size) {
2006 fastboot_fail("size too large");
2007 return;
2008 }
2009 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2010 fastboot_fail("flash write failure");
2011 return;
2012 }
Greg Grisco6e754772011-06-23 12:19:39 -07002013 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002014 }
2015 fastboot_okay("");
2016 return;
2017}
2018
Ajay Dudani5c761132011-04-07 20:19:04 -07002019void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2020{
2021 unsigned int chunk;
2022 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002023 uint32_t *fill_buf = NULL;
2024 uint32_t fill_val;
2025 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002026 sparse_header_t *sparse_header;
2027 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002028 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002029 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302030 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002031 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302032 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002033 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002034
Kinson Chikf1a43512011-07-14 11:28:39 -07002035 index = partition_get_index(arg);
2036 ptn = partition_get_offset(index);
2037 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002038 fastboot_fail("partition table doesn't exist");
2039 return;
2040 }
2041
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302042 size = partition_get_size(index);
2043 if (ROUND_TO_PAGE(sz,511) > size) {
2044 fastboot_fail("size too large");
2045 return;
2046 }
2047
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002048 lun = partition_get_lun(index);
2049 mmc_set_lun(lun);
2050
Ajay Dudani5c761132011-04-07 20:19:04 -07002051 /* Read and skip over sparse image header */
2052 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08002053 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
2054 fastboot_fail("size too large");
2055 return;
2056 }
2057
Ajay Dudani5c761132011-04-07 20:19:04 -07002058 data += sparse_header->file_hdr_sz;
2059 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
2060 {
2061 /* Skip the remaining bytes in a header that is longer than
2062 * we expected.
2063 */
2064 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
2065 }
2066
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002067 dprintf (SPEW, "=== Sparse Image Header ===\n");
2068 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2069 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2070 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2071 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2072 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2073 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2074 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2075 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002076
2077 /* Start processing chunks */
2078 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2079 {
2080 /* Read and skip over chunk header */
2081 chunk_header = (chunk_header_t *) data;
2082 data += sizeof(chunk_header_t);
2083
2084 dprintf (SPEW, "=== Chunk Header ===\n");
2085 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2086 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2087 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2088
2089 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
2090 {
2091 /* Skip the remaining bytes in a header that is longer than
2092 * we expected.
2093 */
2094 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
2095 }
2096
2097 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
2098 switch (chunk_header->chunk_type)
2099 {
2100 case CHUNK_TYPE_RAW:
2101 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2102 chunk_data_sz))
2103 {
2104 fastboot_fail("Bogus chunk size for chunk type Raw");
2105 return;
2106 }
2107
Ajay Dudaniab18f022011-05-12 14:39:22 -07002108 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2109 chunk_data_sz,
2110 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002111 {
2112 fastboot_fail("flash write failure");
2113 return;
2114 }
2115 total_blocks += chunk_header->chunk_sz;
2116 data += chunk_data_sz;
2117 break;
2118
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002119 case CHUNK_TYPE_FILL:
2120 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2121 sizeof(uint32_t)))
2122 {
2123 fastboot_fail("Bogus chunk size for chunk type FILL");
2124 return;
2125 }
2126
2127 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2128 if (!fill_buf)
2129 {
2130 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2131 return;
2132 }
2133
2134 fill_val = *(uint32_t *)data;
2135 data = (char *) data + sizeof(uint32_t);
2136 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
2137
2138 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2139 {
2140 fill_buf[i] = fill_val;
2141 }
2142
2143 for (i = 0; i < chunk_blk_cnt; i++)
2144 {
2145 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2146 sparse_header->blk_sz,
2147 fill_buf))
2148 {
2149 fastboot_fail("flash write failure");
2150 free(fill_buf);
2151 return;
2152 }
2153
2154 total_blocks++;
2155 }
2156
2157 free(fill_buf);
2158 break;
2159
Ajay Dudani5c761132011-04-07 20:19:04 -07002160 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002161 total_blocks += chunk_header->chunk_sz;
2162 break;
2163
Ajay Dudani5c761132011-04-07 20:19:04 -07002164 case CHUNK_TYPE_CRC:
2165 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2166 {
2167 fastboot_fail("Bogus chunk size for chunk type Dont Care");
2168 return;
2169 }
2170 total_blocks += chunk_header->chunk_sz;
2171 data += chunk_data_sz;
2172 break;
2173
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002174 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002175 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002176 fastboot_fail("Unknown chunk type");
2177 return;
2178 }
2179 }
2180
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002181 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2182 total_blocks, sparse_header->total_blks);
2183
2184 if(total_blocks != sparse_header->total_blks)
2185 {
2186 fastboot_fail("sparse image write failure");
2187 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002188
2189 fastboot_okay("");
2190 return;
2191}
2192
2193void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2194{
2195 sparse_header_t *sparse_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002196
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002197#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002198 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2199 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002200 int ret=0;
2201 uint32 major_version=0;
2202 uint32 minor_version=0;
2203
2204 ret = scm_svc_version(&major_version,&minor_version);
2205 if(!ret)
2206 {
2207 if(major_version >= 2)
2208 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002209 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002210 {
2211 ret = encrypt_scm((uint32 **) &data, &sz);
2212 if (ret != 0) {
2213 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2214 return;
2215 }
2216
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002217 /* Protect only for SSD */
2218 if (!strcmp(arg, "ssd")) {
2219 ret = scm_protect_keystore((uint32 *) data, sz);
2220 if (ret != 0) {
2221 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2222 return;
2223 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002224 }
2225 }
2226 else
2227 {
2228 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2229 if(ret != 0)
2230 {
2231 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2232 return;
2233 }
2234 }
2235 }
2236 else
2237 {
2238 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2239 magic_number[1] == DECRYPT_MAGIC_1)
2240 {
2241 ret = decrypt_scm((uint32 **) &data, &sz);
2242 if (ret != 0) {
2243 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2244 return;
2245 }
2246 }
2247 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2248 magic_number[1] == ENCRYPT_MAGIC_1)
2249 {
2250 ret = encrypt_scm((uint32 **) &data, &sz);
2251 if (ret != 0) {
2252 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2253 return;
2254 }
2255 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002256 }
2257 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002258 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002259 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002260 dprintf(CRITICAL,"INVALID SVC Version\n");
2261 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002262 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002263#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002264
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002265#if VERIFIED_BOOT
2266 if(!device.is_unlocked && !device.is_verified)
2267 {
2268 fastboot_fail("device is locked. Cannot flash images");
2269 return;
2270 }
2271 if(!device.is_unlocked && device.is_verified)
2272 {
2273 if(!boot_verify_flash_allowed(arg))
2274 {
2275 fastboot_fail("cannot flash this partition in verified state");
2276 return;
2277 }
2278 }
2279#endif
2280
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002281 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002282 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
2283 cmd_flash_mmc_img(arg, data, sz);
2284 else
2285 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002286 return;
2287}
2288
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002289void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002290{
2291 struct ptentry *ptn;
2292 struct ptable *ptable;
2293 unsigned extra = 0;
2294
2295 ptable = flash_get_ptable();
2296 if (ptable == NULL) {
2297 fastboot_fail("partition table doesn't exist");
2298 return;
2299 }
2300
2301 ptn = ptable_find(ptable, arg);
2302 if (ptn == NULL) {
2303 fastboot_fail("unknown partition name");
2304 return;
2305 }
2306
2307 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2308 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2309 fastboot_fail("image is not a boot image");
2310 return;
2311 }
2312 }
2313
Amol Jadi5c61a952012-05-04 17:05:35 -07002314 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002315 || !strcmp(ptn->name, "userdata")
2316 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002317 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002318 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002319 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002320 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002321 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002322
2323 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002324 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2325 if (flash_ubi_img(ptn, data, sz)) {
2326 fastboot_fail("flash write failure");
2327 return;
2328 }
2329 } else {
2330 if (flash_write(ptn, extra, data, sz)) {
2331 fastboot_fail("flash write failure");
2332 return;
2333 }
Dima Zavin214cc642009-01-26 11:16:21 -08002334 }
2335 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2336 fastboot_okay("");
2337}
2338
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002339void cmd_flash(const char *arg, void *data, unsigned sz)
2340{
2341 if(target_is_emmc_boot())
2342 cmd_flash_mmc(arg, data, sz);
2343 else
2344 cmd_flash_nand(arg, data, sz);
2345}
2346
Dima Zavin214cc642009-01-26 11:16:21 -08002347void cmd_continue(const char *arg, void *data, unsigned sz)
2348{
2349 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002350 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002351
Amit Blay6281ebc2015-01-11 14:44:08 +02002352#ifdef MDTP_SUPPORT
2353 /* Go through Firmware Lock verification before continue with boot process */
2354 mdtp_fwlock_verify_lock();
2355 fbcon_clear();
2356#endif /* MDTP_SUPPORT */
2357
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002358 if (target_is_emmc_boot())
2359 {
2360 boot_linux_from_mmc();
2361 }
2362 else
2363 {
2364 boot_linux_from_flash();
2365 }
Dima Zavin214cc642009-01-26 11:16:21 -08002366}
2367
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002368void cmd_reboot(const char *arg, void *data, unsigned sz)
2369{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002370 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002371 fastboot_okay("");
2372 reboot_device(0);
2373}
2374
2375void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2376{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002377 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002378 fastboot_okay("");
2379 reboot_device(FASTBOOT_MODE);
2380}
2381
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002382void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2383{
2384 dprintf(INFO, "Enabling charger screen check\n");
2385 device.charger_screen_enabled = 1;
2386 write_device_info(&device);
2387 fastboot_okay("");
2388}
2389
2390void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2391{
2392 dprintf(INFO, "Disabling charger screen check\n");
2393 device.charger_screen_enabled = 0;
2394 write_device_info(&device);
2395 fastboot_okay("");
2396}
2397
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302398void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2399{
2400 dprintf(INFO, "Selecting display panel %s\n", arg);
2401 if (arg)
2402 strlcpy(device.display_panel, arg,
2403 sizeof(device.display_panel));
2404 write_device_info(&device);
2405 fastboot_okay("");
2406}
2407
Shashank Mittal162244e2011-08-08 19:01:25 -07002408void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2409{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002410 /* TODO: Wipe user data */
2411 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002412 {
2413 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002414 device.is_verified = 0;
2415 write_device_info(&device);
2416 }
2417 fastboot_okay("");
2418}
2419
2420void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2421{
2422 /* TODO: Wipe user data */
2423 if(device.is_unlocked || device.is_verified)
2424 {
2425 device.is_unlocked = 0;
2426 device.is_verified = 0;
2427 write_device_info(&device);
2428 }
2429 fastboot_okay("");
2430}
2431
2432void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2433{
2434 /* TODO: Wipe user data */
2435 if(device.is_unlocked || !device.is_verified)
2436 {
2437 device.is_unlocked = 0;
2438 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002439 write_device_info(&device);
2440 }
2441 fastboot_okay("");
2442}
2443
Shashank Mittala0032282011-08-26 14:50:11 -07002444void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2445{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302446 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002447 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002448 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002449 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2450 fastboot_info(response);
2451 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002452 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302453 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2454 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002455 fastboot_okay("");
2456}
2457
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002458void cmd_preflash(const char *arg, void *data, unsigned sz)
2459{
2460 fastboot_okay("");
2461}
2462
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302463struct fbimage* splash_screen_flash();
2464
2465int splash_screen_check_header(struct fbimage *logo)
2466{
2467 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2468 return -1;
2469 if (logo->header.width == 0 || logo->header.height == 0)
2470 return -1;
2471 return 0;
2472}
2473
2474struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002475{
2476 struct ptentry *ptn;
2477 struct ptable *ptable;
2478 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002479 struct fbimage *logo = NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002480
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302481
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002482 logo = (struct fbimage *) malloc(ROUNDUP(page_size, sizeof(struct fbimage)));
2483 ASSERT(logo);
2484
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302485 ptable = flash_get_ptable();
2486 if (ptable == NULL) {
2487 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002488 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302489 }
2490 ptn = ptable_find(ptable, "splash");
2491 if (ptn == NULL) {
2492 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002493 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302494 }
2495
2496 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2497 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002498 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302499 }
2500
2501 if (splash_screen_check_header(logo)) {
2502 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002503 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302504 }
2505
2506 fb_display = fbcon_display();
2507 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302508 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2509 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back to default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002510 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002511 }
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302512 uint8_t *base = (uint8_t *) fb_display->base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302513 if (flash_read(ptn + sizeof(logo->header), 0,
2514 base,
2515 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2516 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302517 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002518 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002519 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302520 logo->image = base;
2521 }
2522
2523 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002524
2525err:
2526 free(logo);
2527 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302528}
2529
2530struct fbimage* splash_screen_mmc()
2531{
2532 int index = INVALID_PTN;
2533 unsigned long long ptn = 0;
2534 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002535 struct fbimage *logo = NULL;
Channagoud Kadabida117392014-11-24 11:01:12 -08002536 uint32_t blocksize;
2537 uint32_t readsize;
2538 uint32_t ptn_size;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302539
2540 index = partition_get_index("splash");
2541 if (index == 0) {
2542 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2543 return NULL;
2544 }
2545
2546 ptn = partition_get_offset(index);
2547 if (ptn == 0) {
2548 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2549 return NULL;
2550 }
2551
Channagoud Kadabida117392014-11-24 11:01:12 -08002552 ptn_size = partition_get_size(index);
2553 blocksize = mmc_get_device_blocksize();
2554 readsize = ROUNDUP(sizeof(logo->header), blocksize);
2555
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002556 logo = (struct fbimage *)memalign(CACHE_LINE, ROUNDUP(readsize, CACHE_LINE));
2557 ASSERT(logo);
2558
Channagoud Kadabida117392014-11-24 11:01:12 -08002559 if (mmc_read(ptn, (uint32_t *) logo, readsize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302560 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002561 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302562 }
2563
2564 if (splash_screen_check_header(logo)) {
2565 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002566 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302567 }
2568
2569 fb_display = fbcon_display();
2570 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302571 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2572 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002573 goto err;
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302574 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302575 uint8_t *base = (uint8_t *) fb_display->base;
Channagoud Kadabida117392014-11-24 11:01:12 -08002576 readsize = ROUNDUP((logo->header.width * logo->header.height * fb_display->bpp/8), blocksize);
2577
2578 if (readsize > ptn_size)
2579 {
2580 dprintf(CRITICAL, "@%d:Invalid logo header readsize:%u exceeds ptn_size:%u\n", __LINE__, readsize,ptn_size);
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002581 goto err;
Channagoud Kadabida117392014-11-24 11:01:12 -08002582 }
2583
2584 if (mmc_read(ptn + sizeof(logo->header),(uint32_t *)base, readsize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302585 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302586 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002587 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302588 }
2589
2590 logo->image = base;
2591 }
2592
2593 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002594
2595err:
2596 free(logo);
2597 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302598}
2599
2600
2601struct fbimage* fetch_image_from_partition()
2602{
2603 if (target_is_emmc_boot()) {
2604 return splash_screen_mmc();
2605 } else {
2606 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002607 }
2608}
2609
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002610/* Get the size from partiton name */
2611static void get_partition_size(const char *arg, char *response)
2612{
2613 uint64_t ptn = 0;
2614 uint64_t size;
2615 int index = INVALID_PTN;
2616
2617 index = partition_get_index(arg);
2618
2619 if (index == INVALID_PTN)
2620 {
2621 dprintf(CRITICAL, "Invalid partition index\n");
2622 return;
2623 }
2624
2625 ptn = partition_get_offset(index);
2626
2627 if(!ptn)
2628 {
2629 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2630 return;
2631 }
2632
2633 size = partition_get_size(index);
2634
2635 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2636 return;
2637}
2638
2639/*
2640 * Publish the partition type & size info
2641 * fastboot getvar will publish the required information.
2642 * fastboot getvar partition_size:<partition_name>: partition size in hex
2643 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2644 */
2645static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2646{
2647 uint8_t i;
2648
2649 for (i = 0; i < num_parts; i++) {
2650 get_partition_size(info[i].part_name, info[i].size_response);
2651
2652 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2653 {
2654 dprintf(CRITICAL, "partition size name truncated\n");
2655 return;
2656 }
2657 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2658 {
2659 dprintf(CRITICAL, "partition type name truncated\n");
2660 return;
2661 }
2662
2663 /* publish partition size & type info */
2664 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2665 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2666 }
2667}
2668
Amol Jadi5edf3552013-07-23 14:15:34 -07002669/* register commands and variables for fastboot */
2670void aboot_fastboot_register_commands(void)
2671{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002672 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07002673
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002674 struct fastboot_cmd_desc cmd_list[] = {
2675 /* By default the enabled list is empty. */
2676 {"", NULL},
2677 /* move commands enclosed within the below ifndef to here
2678 * if they need to be enabled in user build.
2679 */
2680#ifndef DISABLE_FASTBOOT_CMDS
2681 /* Register the following commands only for non-user builds */
2682 {"flash:", cmd_flash},
2683 {"erase:", cmd_erase},
2684 {"boot", cmd_boot},
2685 {"continue", cmd_continue},
2686 {"reboot", cmd_reboot},
2687 {"reboot-bootloader", cmd_reboot_bootloader},
2688 {"oem unlock", cmd_oem_unlock},
2689 {"oem lock", cmd_oem_lock},
2690 {"oem verified", cmd_oem_verified},
2691 {"oem device-info", cmd_oem_devinfo},
2692 {"preflash", cmd_preflash},
2693 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
2694 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08002695 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002696#endif
2697 };
2698
2699 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
2700 for (i = 1; i < fastboot_cmds_count; i++)
2701 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
2702
Amol Jadi5edf3552013-07-23 14:15:34 -07002703 /* publish variables and their values */
2704 fastboot_publish("product", TARGET(BOARD));
2705 fastboot_publish("kernel", "lk");
2706 fastboot_publish("serialno", sn_buf);
2707
2708 /*
2709 * partition info is supported only for emmc partitions
2710 * Calling this for NAND prints some error messages which
2711 * is harmless but misleading. Avoid calling this for NAND
2712 * devices.
2713 */
2714 if (target_is_emmc_boot())
2715 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2716
2717 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002718 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2719 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002720 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002721 /* Is the charger screen check enabled */
2722 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2723 device.charger_screen_enabled);
2724 fastboot_publish("charger-screen-enabled",
2725 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302726 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2727 device.display_panel);
2728 fastboot_publish("display-panel",
2729 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002730}
2731
Brian Swetland9c4c0752009-01-25 16:23:50 -08002732void aboot_init(const struct app_descriptor *app)
2733{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002734 unsigned reboot_mode = 0;
lijuang72875802015-02-10 10:35:12 +08002735 unsigned hard_reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002736 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002737
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002738 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002739 if (target_is_emmc_boot())
2740 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002741 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002742 page_mask = page_size - 1;
2743 }
2744 else
2745 {
2746 page_size = flash_page_size();
2747 page_mask = page_size - 1;
2748 }
2749
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002750 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2751
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002752 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002753
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002754 /* Display splash screen if enabled */
2755#if DISPLAY_SPLASH_SCREEN
2756 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302757 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002758 dprintf(SPEW, "Display Init: Done\n");
2759#endif
2760
2761
Greg Griscod6250552011-06-29 14:40:23 -07002762 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002763 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002764
Dhaval Patel223ec952013-07-18 14:49:44 -07002765 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2766
Matthew Qindefd5562014-07-11 18:02:40 +08002767 /*
2768 * Check power off reason if user force reset,
2769 * if yes phone will do normal boot.
2770 */
2771 if (is_user_force_reset())
2772 goto normal_boot;
2773
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002774 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002775 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002776 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002777 dprintf(ALWAYS,"dload mode key sequence detected\n");
2778 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002779 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002780 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002781 }
2782 else
2783 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302784 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002785 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002786 }
2787 boot_into_fastboot = true;
2788 }
2789 if (!boot_into_fastboot)
2790 {
2791 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2792 boot_into_recovery = 1;
2793 if (!boot_into_recovery &&
2794 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002795 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002796 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002797 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002798 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002799 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002800 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002801
Ajay Dudani77421292010-10-27 19:34:06 -07002802 reboot_mode = check_reboot_mode();
lijuang72875802015-02-10 10:35:12 +08002803 hard_reboot_mode = check_hard_reboot_mode();
2804 if (reboot_mode == RECOVERY_MODE ||
2805 hard_reboot_mode == RECOVERY_HARD_RESET_MODE) {
Ajay Dudani77421292010-10-27 19:34:06 -07002806 boot_into_recovery = 1;
lijuang72875802015-02-10 10:35:12 +08002807 } else if(reboot_mode == FASTBOOT_MODE ||
2808 hard_reboot_mode == FASTBOOT_HARD_RESET_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002809 boot_into_fastboot = true;
lijuang72875802015-02-10 10:35:12 +08002810 } else if(reboot_mode == ALARM_BOOT ||
2811 hard_reboot_mode == RTC_HARD_RESET_MODE) {
Matthew Qind886f3c2014-01-17 16:52:01 +08002812 boot_reason_alarm = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002813 }
2814
Matthew Qindefd5562014-07-11 18:02:40 +08002815normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03002816 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002817 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002818 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002819 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002820 if(emmc_recovery_init())
2821 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2822 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002823 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002824 if((device.is_unlocked) || (device.is_tampered))
2825 {
2826 #ifdef TZ_TAMPER_FUSE
2827 set_tamper_fuse_cmd();
2828 #endif
2829 #if USE_PCOM_SECBOOT
2830 set_tamper_flag(device.is_tampered);
2831 #endif
2832 }
Shashank Mittala0032282011-08-26 14:50:11 -07002833 }
Amit Blay6281ebc2015-01-11 14:44:08 +02002834
2835#ifdef MDTP_SUPPORT
2836 /* Go through Firmware Lock verification before continue with boot process */
2837 mdtp_fwlock_verify_lock();
2838 fbcon_clear();
2839#endif /* MDTP_SUPPORT */
2840
Pavel Nedev5d91d412013-04-29 11:34:24 +03002841 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002842 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002843 else
2844 {
2845 recovery_init();
2846 #if USE_PCOM_SECBOOT
2847 if((device.is_unlocked) || (device.is_tampered))
2848 set_tamper_flag(device.is_tampered);
2849 #endif
2850 boot_linux_from_flash();
2851 }
2852 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2853 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002854 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002855
Amol Jadi5edf3552013-07-23 14:15:34 -07002856 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002857
Amol Jadi5edf3552013-07-23 14:15:34 -07002858 /* register aboot specific fastboot commands */
2859 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002860
Amol Jadi5edf3552013-07-23 14:15:34 -07002861 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002862 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002863
2864 /* initialize and start fastboot */
2865 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002866}
2867
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002868uint32_t get_page_size()
2869{
2870 return page_size;
2871}
2872
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002873/*
2874 * Calculated and save hash (SHA256) for non-signed boot image.
2875 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002876 * @param image_addr - Boot image address
2877 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002878 *
2879 * @return int - 0 on success, negative value on failure.
2880 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002881static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002882{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002883 unsigned int digest[8];
2884#if IMAGE_VERIF_ALGO_SHA1
2885 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
2886#else
2887 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
2888#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002889
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002890 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002891 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002892
2893 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002894 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002895
2896 return 0;
2897}
2898
Brian Swetland9c4c0752009-01-25 16:23:50 -08002899APP_START(aboot)
2900 .init = aboot_init,
2901APP_END