blob: 35373d8cd7d510069ca20e43518e7144fffe1c60 [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"
Ajay Dudanide984792015-03-02 09:57:41 -080071#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070072#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070073#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070074#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070075#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020076#include "mdtp.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070077
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070078extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070079extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070080extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070081extern int get_target_boot_params(const char *cmdline, const char *part,
82 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070083
84void write_device_info_mmc(device_info *dev);
85void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -070086static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070087
Sridhar Parasurame94e8152014-10-24 14:06:03 -070088/* fastboot command function pointer */
89typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
90
91struct fastboot_cmd_desc {
92 char * name;
93 fastboot_cmd_fn cb;
94};
95
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070096#define EXPAND(NAME) #NAME
97#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070098
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080099#ifdef MEMBASE
100#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
101#else
David Ng183a7422009-12-07 14:55:21 -0800102#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800103#endif
104
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700105#ifndef MEMSIZE
106#define MEMSIZE 1024*1024
107#endif
108
109#define MAX_TAGS_SIZE 1024
110
lijuang72875802015-02-10 10:35:12 +0800111#define RECOVERY_HARD_RESET_MODE 0x01
112#define FASTBOOT_HARD_RESET_MODE 0x02
113#define RTC_HARD_RESET_MODE 0x03
114
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800115#define RECOVERY_MODE 0x77665502
116#define FASTBOOT_MODE 0x77665500
Matthew Qind886f3c2014-01-17 16:52:01 +0800117#define ALARM_BOOT 0x77665503
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800118
Kun Liang2f1601a2013-08-12 16:29:54 +0800119/* make 4096 as default size to ensure EFS,EXT4's erasing */
120#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800121#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800122
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700123#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700124#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800125
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800126#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
127
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700128#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
129
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800130#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700131static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700132#else
David Ng183a7422009-12-07 14:55:21 -0800133static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700134#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800135static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300136static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800137static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800138static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800139static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700140static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300141static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800142
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800143static const char *baseband_apq = " androidboot.baseband=apq";
144static const char *baseband_msm = " androidboot.baseband=msm";
145static const char *baseband_csfb = " androidboot.baseband=csfb";
146static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700147static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800148static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700149static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800150static const char *baseband_dsda = " androidboot.baseband=dsda";
151static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800152static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800153static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800154
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700155static unsigned page_size = 0;
156static unsigned page_mask = 0;
157static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
158static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700159static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800160static bool boot_reason_alarm;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700161
Shashank Mittalcd98d472011-08-02 14:29:24 -0700162/* Assuming unauthorized kernel image by default */
163static int auth_kernel_img = 0;
164
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530165static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}};
Shashank Mittal162244e2011-08-08 19:01:25 -0700166
Dima Zavin42168f22009-01-30 11:52:22 -0800167struct atag_ptbl_entry
168{
169 char name[16];
170 unsigned offset;
171 unsigned size;
172 unsigned flags;
173};
174
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700175/*
176 * Partition info, required to be published
177 * for fastboot
178 */
179struct getvar_partition_info {
180 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
181 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
182 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
183 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
184 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
185};
186
187/*
188 * Right now, we are publishing the info for only
189 * three partitions
190 */
191struct getvar_partition_info part_info[] =
192{
193 { "system" , "partition-size:", "partition-type:", "", "ext4" },
194 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
195 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
196};
197
198char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700199char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800200char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700201char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530202char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700203
Greg Griscod2471ef2011-07-14 13:00:42 -0700204extern int emmc_recovery_init(void);
205
Kinson Chik0b1c8162011-08-31 16:31:57 -0700206#if NO_KEYPAD_DRIVER
207extern int fastboot_trigger(void);
208#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700209
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800210static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700211{
212 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700213#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800214 if (is_arm64)
215 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
216 else
217 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700218 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
219 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700220#endif
221}
222
Dima Zavin42168f22009-01-30 11:52:22 -0800223static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
224{
225 struct atag_ptbl_entry atag_ptn;
226
227 memcpy(atag_ptn.name, ptn->name, 16);
228 atag_ptn.name[15] = '\0';
229 atag_ptn.offset = ptn->start;
230 atag_ptn.size = ptn->length;
231 atag_ptn.flags = ptn->flags;
232 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
233 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
234}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800235
Neeti Desaie245d492012-06-01 12:52:13 -0700236unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800237{
David Ng183a7422009-12-07 14:55:21 -0800238 int cmdline_len = 0;
239 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800240 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700241 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800242 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300243 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700244 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700245 char *boot_dev_buf = NULL;
Dima Zavin42168f22009-01-30 11:52:22 -0800246
Brian Swetland9c4c0752009-01-25 16:23:50 -0800247 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800248 cmdline_len = strlen(cmdline);
249 have_cmdline = 1;
250 }
251 if (target_is_emmc_boot()) {
252 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800253#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700254 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
255 ASSERT(boot_dev_buf);
256 platform_boot_dev_cmdline(boot_dev_buf);
257 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700258#endif
David Ng183a7422009-12-07 14:55:21 -0800259 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800260
261 cmdline_len += strlen(usb_sn_cmdline);
262 cmdline_len += strlen(sn_buf);
263
Pavel Nedev5614d222013-06-17 18:01:02 +0300264 if (boot_into_recovery && gpt_exists)
265 cmdline_len += strlen(secondary_gpt_enable);
266
Pavel Nedev328ac822013-04-05 15:25:11 +0300267 if (boot_into_ffbm) {
268 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700269 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800270 /* reduce kernel console messages to speed-up boot */
271 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800272 } else if (boot_reason_alarm) {
273 cmdline_len += strlen(alarmboot_cmdline);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700274 } else if (device.charger_screen_enabled &&
275 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700276 pause_at_bootup = 1;
277 cmdline_len += strlen(battchg_pause);
278 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800279
Shashank Mittalcd98d472011-08-02 14:29:24 -0700280 if(target_use_signed_kernel() && auth_kernel_img) {
281 cmdline_len += strlen(auth_kernel);
282 }
283
Joonwoo Park61112782013-10-02 19:50:39 -0700284 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
285 "system",
286 target_boot_params,
287 sizeof(target_boot_params)) == 0) {
288 have_target_boot_params = 1;
289 cmdline_len += strlen(target_boot_params);
290 }
291
Ajay Dudanid04110c2011-01-17 23:55:07 -0800292 /* Determine correct androidboot.baseband to use */
293 switch(target_baseband())
294 {
295 case BASEBAND_APQ:
296 cmdline_len += strlen(baseband_apq);
297 break;
298
299 case BASEBAND_MSM:
300 cmdline_len += strlen(baseband_msm);
301 break;
302
303 case BASEBAND_CSFB:
304 cmdline_len += strlen(baseband_csfb);
305 break;
306
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800307 case BASEBAND_SVLTE2A:
308 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800309 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700310
311 case BASEBAND_MDM:
312 cmdline_len += strlen(baseband_mdm);
313 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700314
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800315 case BASEBAND_MDM2:
316 cmdline_len += strlen(baseband_mdm2);
317 break;
318
Amol Jadi5c61a952012-05-04 17:05:35 -0700319 case BASEBAND_SGLTE:
320 cmdline_len += strlen(baseband_sglte);
321 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530322
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800323 case BASEBAND_SGLTE2:
324 cmdline_len += strlen(baseband_sglte2);
325 break;
326
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530327 case BASEBAND_DSDA:
328 cmdline_len += strlen(baseband_dsda);
329 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800330
331 case BASEBAND_DSDA2:
332 cmdline_len += strlen(baseband_dsda2);
333 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800334 }
335
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800336 if (cmdline) {
337 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
338 target_display_panel_node(device.display_panel,
339 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
340 strlen(display_panel_buf)) {
341 cmdline_len += strlen(display_panel_buf);
342 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700343 }
344
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800345 if (target_warm_boot()) {
346 warm_boot = true;
347 cmdline_len += strlen(warmboot_cmdline);
348 }
349
David Ng183a7422009-12-07 14:55:21 -0800350 if (cmdline_len > 0) {
351 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800352 unsigned char *dst;
353
354 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
355 ASSERT(cmdline_final != NULL);
356 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700357
Amol Jadi168b7712012-03-06 16:15:00 -0800358 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800359 if (have_cmdline) {
360 src = cmdline;
361 while ((*dst++ = *src++));
362 }
363 if (target_is_emmc_boot()) {
364 src = emmc_cmdline;
365 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700366 have_cmdline = 1;
367 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800368#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700369 src = boot_dev_buf;
370 if (have_cmdline) --dst;
371 while ((*dst++ = *src++));
372#endif
David Ngf773dde2010-07-26 19:55:08 -0700373 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800374
375 src = usb_sn_cmdline;
376 if (have_cmdline) --dst;
377 have_cmdline = 1;
378 while ((*dst++ = *src++));
379 src = sn_buf;
380 if (have_cmdline) --dst;
381 have_cmdline = 1;
382 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800383 if (warm_boot) {
384 if (have_cmdline) --dst;
385 src = warmboot_cmdline;
386 while ((*dst++ = *src++));
387 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800388
Pavel Nedev5614d222013-06-17 18:01:02 +0300389 if (boot_into_recovery && gpt_exists) {
390 src = secondary_gpt_enable;
391 if (have_cmdline) --dst;
392 while ((*dst++ = *src++));
393 }
394
Pavel Nedev328ac822013-04-05 15:25:11 +0300395 if (boot_into_ffbm) {
396 src = androidboot_mode;
397 if (have_cmdline) --dst;
398 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700399 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300400 if (have_cmdline) --dst;
401 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800402 src = loglevel;
403 if (have_cmdline) --dst;
404 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800405 } else if (boot_reason_alarm) {
406 src = alarmboot_cmdline;
407 if (have_cmdline) --dst;
408 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300409 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700410 src = battchg_pause;
411 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800412 while ((*dst++ = *src++));
413 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800414
Shashank Mittalcd98d472011-08-02 14:29:24 -0700415 if(target_use_signed_kernel() && auth_kernel_img) {
416 src = auth_kernel;
417 if (have_cmdline) --dst;
418 while ((*dst++ = *src++));
419 }
420
Ajay Dudanid04110c2011-01-17 23:55:07 -0800421 switch(target_baseband())
422 {
423 case BASEBAND_APQ:
424 src = baseband_apq;
425 if (have_cmdline) --dst;
426 while ((*dst++ = *src++));
427 break;
428
429 case BASEBAND_MSM:
430 src = baseband_msm;
431 if (have_cmdline) --dst;
432 while ((*dst++ = *src++));
433 break;
434
435 case BASEBAND_CSFB:
436 src = baseband_csfb;
437 if (have_cmdline) --dst;
438 while ((*dst++ = *src++));
439 break;
440
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800441 case BASEBAND_SVLTE2A:
442 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800443 if (have_cmdline) --dst;
444 while ((*dst++ = *src++));
445 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700446
447 case BASEBAND_MDM:
448 src = baseband_mdm;
449 if (have_cmdline) --dst;
450 while ((*dst++ = *src++));
451 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700452
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800453 case BASEBAND_MDM2:
454 src = baseband_mdm2;
455 if (have_cmdline) --dst;
456 while ((*dst++ = *src++));
457 break;
458
Amol Jadi5c61a952012-05-04 17:05:35 -0700459 case BASEBAND_SGLTE:
460 src = baseband_sglte;
461 if (have_cmdline) --dst;
462 while ((*dst++ = *src++));
463 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530464
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800465 case BASEBAND_SGLTE2:
466 src = baseband_sglte2;
467 if (have_cmdline) --dst;
468 while ((*dst++ = *src++));
469 break;
470
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530471 case BASEBAND_DSDA:
472 src = baseband_dsda;
473 if (have_cmdline) --dst;
474 while ((*dst++ = *src++));
475 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800476
477 case BASEBAND_DSDA2:
478 src = baseband_dsda2;
479 if (have_cmdline) --dst;
480 while ((*dst++ = *src++));
481 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800482 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700483
484 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700485 src = display_panel_buf;
486 if (have_cmdline) --dst;
487 while ((*dst++ = *src++));
488 }
Joonwoo Park61112782013-10-02 19:50:39 -0700489
490 if (have_target_boot_params) {
491 if (have_cmdline) --dst;
492 src = target_boot_params;
493 while ((*dst++ = *src++));
494 }
Neeti Desaie245d492012-06-01 12:52:13 -0700495 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700496
497
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700498 if (boot_dev_buf)
499 free(boot_dev_buf);
500
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800501 if (cmdline_final)
502 dprintf(INFO, "cmdline: %s\n", cmdline_final);
503 else
504 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700505 return cmdline_final;
506}
507
508unsigned *atag_core(unsigned *ptr)
509{
510 /* CORE */
511 *ptr++ = 2;
512 *ptr++ = 0x54410001;
513
514 return ptr;
515
516}
517
518unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
519 unsigned ramdisk_size)
520{
521 if (ramdisk_size) {
522 *ptr++ = 4;
523 *ptr++ = 0x54420005;
524 *ptr++ = (unsigned)ramdisk;
525 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800526 }
527
Neeti Desaie245d492012-06-01 12:52:13 -0700528 return ptr;
529}
530
531unsigned *atag_ptable(unsigned **ptr_addr)
532{
533 int i;
534 struct ptable *ptable;
535
536 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700537 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
538 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700539 *(*ptr_addr)++ = 0x4d534d70;
540 for (i = 0; i < ptable->count; ++i)
541 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
542 }
543
544 return (*ptr_addr);
545}
546
547unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
548{
549 int cmdline_length = 0;
550 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700551 char *dest;
552
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800553 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700554 n = (cmdline_length + 4) & (~3);
555
556 *ptr++ = (n / 4) + 2;
557 *ptr++ = 0x54410009;
558 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800559 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700560 ptr += (n / 4);
561
562 return ptr;
563}
564
565unsigned *atag_end(unsigned *ptr)
566{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800567 /* END */
568 *ptr++ = 0;
569 *ptr++ = 0;
570
Neeti Desaie245d492012-06-01 12:52:13 -0700571 return ptr;
572}
573
574void generate_atags(unsigned *ptr, const char *cmdline,
575 void *ramdisk, unsigned ramdisk_size)
576{
577
578 ptr = atag_core(ptr);
579 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
580 ptr = target_atag_mem(ptr);
581
582 /* Skip NAND partition ATAGS for eMMC boot */
583 if (!target_is_emmc_boot()){
584 ptr = atag_ptable(&ptr);
585 }
586
587 ptr = atag_cmdline(ptr, cmdline);
588 ptr = atag_end(ptr);
589}
590
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700591typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700592void boot_linux(void *kernel, unsigned *tags,
593 const char *cmdline, unsigned machtype,
594 void *ramdisk, unsigned ramdisk_size)
595{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800596 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800597#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700598 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800599#endif
600
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700601 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800602 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800603 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800604
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530605 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700606
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800607 final_cmdline = update_cmdline((const char*)cmdline);
608
Neeti Desai17379b82012-06-04 18:42:53 -0700609#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800610 dprintf(INFO, "Updating device tree: start\n");
611
Neeti Desai17379b82012-06-04 18:42:53 -0700612 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530613 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700614 if(ret)
615 {
616 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
617 ASSERT(0);
618 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800619 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700620#else
Neeti Desaie245d492012-06-01 12:52:13 -0700621 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800622 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700623#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700624
Maria Yu52254c02014-07-04 16:14:54 +0800625 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700626
627#if VERIFIED_BOOT
628 /* Write protect the device info */
Channagoud Kadabibc4f15f2015-02-25 12:09:51 -0800629 if (target_build_variant_user() && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700630 {
631 dprintf(INFO, "Failed to write protect dev info\n");
632 ASSERT(0);
633 }
634#endif
635
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700636 /* Perform target specific cleanup */
637 target_uninit();
638
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800639 /* Turn off splash screen if enabled */
640#if DISPLAY_SPLASH_SCREEN
641 target_display_shutdown();
642#endif
643
644
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800645 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530646 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800647
648 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700649
Amol Jadi4421e652011-06-16 15:00:48 -0700650 /* do any platform specific cleanup before kernel entry */
651 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700652
Brian Swetland9c4c0752009-01-25 16:23:50 -0800653 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700654
Amol Jadi504f9fe2012-08-16 13:56:48 -0700655#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800656 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700657#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700658 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800659
660 if (IS_ARM64(kptr))
661 /* Jump to a 64bit kernel */
662 scm_elexec_call((paddr_t)kernel, tags_phys);
663 else
664 /* Jump to a 32bit kernel */
665 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800666}
667
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700668/* Function to check if the memory address range falls within the aboot
669 * boundaries.
670 * start: Start of the memory region
671 * size: Size of the memory region
672 */
673int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
674{
675 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800676 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700677 return -1;
678
679 /* Check for memory overlap. */
680 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
681 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700682 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700683 return 0;
684 else
685 return -1;
686}
687
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800688#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800689
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800690BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800691#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800692BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800693#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800694
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700695static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
696{
697 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800698
699#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800700#if IMAGE_VERIF_ALGO_SHA1
701 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
702#else
703 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
704#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800705#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700706
707 /* Assume device is rooted at this time. */
708 device.is_tampered = 1;
709
710 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
711
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700712#if VERIFIED_BOOT
713 if(boot_into_recovery)
714 {
715 ret = boot_verify_image((unsigned char *)bootimg_addr,
716 bootimg_size, "recovery");
717 }
718 else
719 {
720 ret = boot_verify_image((unsigned char *)bootimg_addr,
721 bootimg_size, "boot");
722 }
723 boot_verify_print_state();
724#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700725 ret = image_verify((unsigned char *)bootimg_addr,
726 (unsigned char *)(bootimg_addr + bootimg_size),
727 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800728 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700729#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700730 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
731
732 if (ret)
733 {
734 /* Authorized kernel */
735 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700736 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700737 }
738
739#if USE_PCOM_SECBOOT
740 set_tamper_flag(device.is_tampered);
741#endif
742
743 if(device.is_tampered)
744 {
745 write_device_info_mmc(&device);
746 #ifdef TZ_TAMPER_FUSE
747 set_tamper_fuse_cmd();
748 #endif
749 #ifdef ASSERT_ON_TAMPER
750 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
751 ASSERT(0);
752 #endif
753 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700754
755#if VERIFIED_BOOT
756 if(boot_verify_get_state() == RED)
757 {
758 if(!boot_into_recovery)
759 {
760 dprintf(CRITICAL,
761 "Device verification failed. Rebooting into recovery.\n");
762 reboot_device(RECOVERY_MODE);
763 }
764 else
765 {
766 dprintf(CRITICAL,
767 "Recovery image verification failed. Asserting..\n");
768 ASSERT(0);
769 }
770 }
771#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700772}
773
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530774static bool check_format_bit()
775{
776 bool ret = false;
777 int index;
778 uint64_t offset;
779 struct boot_selection_info *in = NULL;
780 char *buf = NULL;
781
782 index = partition_get_index("bootselect");
783 if (index == INVALID_PTN)
784 {
785 dprintf(INFO, "Unable to locate /bootselect partition\n");
786 return ret;
787 }
788 offset = partition_get_offset(index);
789 if(!offset)
790 {
791 dprintf(INFO, "partition /bootselect doesn't exist\n");
792 return ret;
793 }
794 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
795 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530796 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530797 {
798 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
799 free(buf);
800 return ret;
801 }
802 in = (struct boot_selection_info *) buf;
803 if ((in->signature == BOOTSELECT_SIGNATURE) &&
804 (in->version == BOOTSELECT_VERSION)) {
805 if ((in->state_info & BOOTSELECT_FORMAT) &&
806 !(in->state_info & BOOTSELECT_FACTORY))
807 ret = true;
808 } else {
809 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
810 in->signature, in->version);
811 ASSERT(0);
812 }
813 free(buf);
814 return ret;
815}
816
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700817void boot_verifier_init()
818{
819
820 uint32_t boot_state;
821 /* Check if device unlock */
822 if(device.is_unlocked)
823 {
824 boot_verify_send_event(DEV_UNLOCK);
825 boot_verify_print_state();
826 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
827 return;
828 }
829 else
830 {
831 boot_verify_send_event(BOOT_INIT);
832 }
833
834 /* Initialize keystore */
835 boot_state = boot_verify_keystore_init();
836 if(boot_state == YELLOW)
837 {
838 boot_verify_print_state();
839 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
840 }
841}
842
Shashank Mittal23b8f422010-04-16 19:27:21 -0700843int boot_linux_from_mmc(void)
844{
845 struct boot_img_hdr *hdr = (void*) buf;
846 struct boot_img_hdr *uhdr;
847 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700848 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700849 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700850 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700851
Shashank Mittalcd98d472011-08-02 14:29:24 -0700852 unsigned char *image_addr = 0;
853 unsigned kernel_actual;
854 unsigned ramdisk_actual;
855 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700856 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700857
858#if DEVICE_TREE
859 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700860 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700861 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800862 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700863 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700864#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800865 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
866 struct kernel64_hdr *kptr = (void*) kbuf;
867
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530868 if (check_format_bit())
869 boot_into_recovery = 1;
870
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700871 if (!boot_into_recovery) {
872 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
873 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
874 if (rcode <= 0) {
875 boot_into_ffbm = false;
876 if (rcode < 0)
877 dprintf(CRITICAL,"failed to get ffbm cookie");
878 } else
879 boot_into_ffbm = true;
880 } else
881 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700882 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
883 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
884 dprintf(INFO, "Unified boot method!\n");
885 hdr = uhdr;
886 goto unified_boot;
887 }
Greg Griscod6250552011-06-29 14:40:23 -0700888 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700889 index = partition_get_index("boot");
890 ptn = partition_get_offset(index);
891 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700892 dprintf(CRITICAL, "ERROR: No boot partition found\n");
893 return -1;
894 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700895 }
896 else {
897 index = partition_get_index("recovery");
898 ptn = partition_get_offset(index);
899 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700900 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
901 return -1;
902 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700903 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -0800904 /* Set Lun for boot & recovery partitions */
905 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -0700906
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530907 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700908 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
909 return -1;
910 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700911
912 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700913 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700914 return -1;
915 }
916
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700917 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530918
919 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
920 dprintf(CRITICAL, "ERROR: Invalid page size\n");
921 return -1;
922 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700923 page_size = hdr->page_size;
924 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700925 }
926
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800927 /* Read the next page to get kernel Image header
928 * which lives in the second page for arm64 targets.
929 */
930
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530931 if (mmc_read(ptn + page_size, (uint32_t *) kbuf, page_size)) {
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800932 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
933 return -1;
934 }
935
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700936 /*
937 * Update the kernel/ramdisk/tags address if the boot image header
938 * has default values, these default values come from mkbootimg when
939 * the boot image is flashed using fastboot flash:raw
940 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800941 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700942
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700943 /* Get virtual addresses since the hdr saves physical addresses. */
944 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
945 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
946 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700947
948 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
949 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
950
951 /* Check if the addresses in the header are valid. */
952 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
953 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
954 {
955 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
956 return -1;
957 }
958
959#ifndef DEVICE_TREE
960 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
961 {
962 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
963 return -1;
964 }
965#endif
966
Shashank Mittalcd98d472011-08-02 14:29:24 -0700967 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300968 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
969 (int) target_use_signed_kernel(),
970 device.is_unlocked,
971 device.is_tampered);
972
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700973#if VERIFIED_BOOT
974 boot_verifier_init();
975#endif
976
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700977 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800978 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700979 offset = 0;
980
Shashank Mittalcd98d472011-08-02 14:29:24 -0700981 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700982
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800983#if DEVICE_TREE
984 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
985 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700986
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700987 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700988 {
989 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
990 return -1;
991 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800992#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530993 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700994
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700995#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700996
Amol Jadib6be5c12012-11-14 13:39:51 -0800997 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700998 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800999
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301000 if (check_aboot_addr_range_overlap((uint32_t)image_addr, imagesize_actual))
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -07001001 {
1002 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1003 return -1;
1004 }
1005
Shashank Mittalcd98d472011-08-02 14:29:24 -07001006 /* Read image without signature */
1007 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1008 {
1009 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1010 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -08001011 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001012
Amol Jadib6be5c12012-11-14 13:39:51 -08001013 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001014 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001015
Shashank Mittalcd98d472011-08-02 14:29:24 -07001016 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -07001017
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301018 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -07001019 {
1020 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1021 return -1;
1022 }
1023
Shashank Mittalcd98d472011-08-02 14:29:24 -07001024 /* Read signature */
1025 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1026 {
1027 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001028 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001029 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001030
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301031 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001032
Neeti Desai465491e2012-07-31 12:53:35 -07001033 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -07001034 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1035 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -07001036
Neeti Desai465491e2012-07-31 12:53:35 -07001037 #if DEVICE_TREE
1038 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001039 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -07001040 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001041
Deepa Dinamani19648b42013-09-05 17:05:55 -07001042 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001043 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1044 return -1;
1045 }
Deepa Dinamani19648b42013-09-05 17:05:55 -07001046
Neeti Desai465491e2012-07-31 12:53:35 -07001047 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -07001048 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001049 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
1050 return -1;
1051 }
1052
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001053 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001054 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001055 {
1056 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1057 return -1;
1058 }
1059
Joel Kingaa335dc2013-06-03 16:11:08 -07001060 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001061 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -08001062 /*
1063 * If appended dev tree is found, update the atags with
1064 * memory address to the DTB appended location on RAM.
1065 * Else update with the atags address in the kernel header
1066 */
1067 void *dtb;
1068 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001069 hdr->kernel_size,
1070 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001071 if (!dtb) {
1072 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1073 return -1;
1074 }
Neeti Desai465491e2012-07-31 12:53:35 -07001075 }
1076 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001077 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001078 else
1079 {
Amol Jadib6be5c12012-11-14 13:39:51 -08001080 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001081
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001082 image_addr = (unsigned char *)target_get_scratch_address();
1083#if DEVICE_TREE
1084 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1085 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1086
1087 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
1088 {
1089 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1090 return -1;
1091 }
1092#else
1093 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1094
1095#endif
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001096 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001097 {
1098 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1099 return -1;
1100 }
1101
Amol Jadib6be5c12012-11-14 13:39:51 -08001102 dprintf(INFO, "Loading boot image (%d): start\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001103 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001104 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001105
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001106 offset = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001107
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001108 /* Load the entire boot image */
1109 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual)) {
1110 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
Shashank Mittalcd98d472011-08-02 14:29:24 -07001111 return -1;
1112 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001113
1114 dprintf(INFO, "Loading boot image (%d): done\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001115 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001116 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -07001117
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001118 #ifdef TZ_SAVE_KERNEL_HASH
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001119 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001120 #endif /* TZ_SAVE_KERNEL_HASH */
1121
1122 /* Move kernel, ramdisk and device tree to correct address */
1123 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1124 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Neeti Desai465491e2012-07-31 12:53:35 -07001125
1126 #if DEVICE_TREE
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001127 if(hdr->dt_size) {
1128 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1129 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001130
Deepa Dinamani19648b42013-09-05 17:05:55 -07001131 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001132 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1133 return -1;
1134 }
1135
Joel Kingaa335dc2013-06-03 16:11:08 -07001136 /* Find index of device tree within device tree table */
1137 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001138 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1139 return -1;
1140 }
1141
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001142 /* Validate and Read device device tree in the tags_addr */
Joel Kingaa335dc2013-06-03 16:11:08 -07001143 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001144 {
1145 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1146 return -1;
1147 }
1148
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001149 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001150 } else {
Aparna Mallavarapu33127372014-06-03 22:15:54 +05301151 /* Validate the tags_addr */
1152 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1153 {
1154 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1155 return -1;
1156 }
Dima Zavin77e41f32013-03-06 16:10:43 -08001157 /*
1158 * If appended dev tree is found, update the atags with
1159 * memory address to the DTB appended location on RAM.
1160 * Else update with the atags address in the kernel header
1161 */
1162 void *dtb;
1163 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001164 kernel_actual,
1165 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001166 if (!dtb) {
1167 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1168 return -1;
1169 }
Neeti Desai465491e2012-07-31 12:53:35 -07001170 }
1171 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001172 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001173
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001174 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1175 target_load_ssd_keystore();
1176
Shashank Mittal23b8f422010-04-16 19:27:21 -07001177unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001178
Dima Zavin77e41f32013-03-06 16:10:43 -08001179 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001180 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001181 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1182
1183 return 0;
1184}
1185
Dima Zavin214cc642009-01-26 11:16:21 -08001186int boot_linux_from_flash(void)
1187{
1188 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001189 struct ptentry *ptn;
1190 struct ptable *ptable;
1191 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001192
Shashank Mittalcd98d472011-08-02 14:29:24 -07001193 unsigned char *image_addr = 0;
1194 unsigned kernel_actual;
1195 unsigned ramdisk_actual;
1196 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001197 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001198
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001199#if DEVICE_TREE
1200 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001201 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001202 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001203 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001204#endif
1205
David Ng183a7422009-12-07 14:55:21 -08001206 if (target_is_emmc_boot()) {
1207 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1208 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1209 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1210 return -1;
1211 }
1212 goto continue_boot;
1213 }
1214
Dima Zavin214cc642009-01-26 11:16:21 -08001215 ptable = flash_get_ptable();
1216 if (ptable == NULL) {
1217 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1218 return -1;
1219 }
1220
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001221 if(!boot_into_recovery)
1222 {
1223 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001224
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001225 if (ptn == NULL) {
1226 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1227 return -1;
1228 }
1229 }
1230 else
1231 {
1232 ptn = ptable_find(ptable, "recovery");
1233 if (ptn == NULL) {
1234 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1235 return -1;
1236 }
Dima Zavin214cc642009-01-26 11:16:21 -08001237 }
1238
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001239 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001240 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1241 return -1;
1242 }
Dima Zavin214cc642009-01-26 11:16:21 -08001243
1244 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001245 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001246 return -1;
1247 }
1248
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001249 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001250 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 -08001251 return -1;
1252 }
1253
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001254 /*
1255 * Update the kernel/ramdisk/tags address if the boot image header
1256 * has default values, these default values come from mkbootimg when
1257 * the boot image is flashed using fastboot flash:raw
1258 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001259 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001260
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001261 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001262 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1263 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1264 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1265
1266 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1267 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1268
1269 /* Check if the addresses in the header are valid. */
1270 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1271 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1272 {
1273 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1274 return -1;
1275 }
1276
1277#ifndef DEVICE_TREE
1278 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1279 {
1280 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1281 return -1;
1282 }
1283#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001284
Shashank Mittalcd98d472011-08-02 14:29:24 -07001285 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001286 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001287 {
1288 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001289 offset = 0;
1290
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001291#if DEVICE_TREE
1292 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1293 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001294
1295 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1296 {
1297 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1298 return -1;
1299 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001300#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001301 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001302#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001303
Amol Jadib6be5c12012-11-14 13:39:51 -08001304 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001305 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001306
Shashank Mittalcd98d472011-08-02 14:29:24 -07001307 /* Read image without signature */
1308 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1309 {
1310 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1311 return -1;
1312 }
Dima Zavin214cc642009-01-26 11:16:21 -08001313
Amol Jadib6be5c12012-11-14 13:39:51 -08001314 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001315 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001316
Shashank Mittalcd98d472011-08-02 14:29:24 -07001317 offset = imagesize_actual;
1318 /* Read signature */
1319 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1320 {
1321 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001322 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001323 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001324
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301325 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001326
1327 /* Move kernel and ramdisk to correct address */
1328 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1329 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001330#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001331 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001332 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001333 {
1334 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1335 return -1;
1336 }
1337
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001338 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1339#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001340
1341 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001342 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001343 {
1344 write_device_info_flash(&device);
1345 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301346#if USE_PCOM_SECBOOT
1347 set_tamper_flag(device.is_tampered);
1348#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001349 }
1350 else
1351 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001352 offset = page_size;
1353
Amol Jadib6be5c12012-11-14 13:39:51 -08001354 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1355 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1356 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1357
1358 dprintf(INFO, "Loading boot image (%d): start\n",
1359 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001360 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001361
1362 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001363 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1364 return -1;
1365 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001366 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001367
Amol Jadib6be5c12012-11-14 13:39:51 -08001368 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001369 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1370 return -1;
1371 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001372 offset += ramdisk_actual;
1373
1374 dprintf(INFO, "Loading boot image (%d): done\n",
1375 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001376 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001377
1378 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001379 offset += second_actual;
1380 /* Second image loading not implemented. */
1381 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001382 }
1383
1384#if DEVICE_TREE
1385 if(hdr->dt_size != 0) {
1386
1387 /* Read the device tree table into buffer */
1388 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1389 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1390 return -1;
1391 }
1392
1393 table = (struct dt_table*) dt_buf;
1394
Deepa Dinamani19648b42013-09-05 17:05:55 -07001395 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001396 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1397 return -1;
1398 }
1399
Deepa Dinamani19648b42013-09-05 17:05:55 -07001400 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1401 if (!table)
1402 return -1;
1403
1404 /* Read the entire device tree table into buffer */
1405 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1406 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1407 return -1;
1408 }
1409
1410
Joel Kingaa335dc2013-06-03 16:11:08 -07001411 /* Find index of device tree within device tree table */
1412 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001413 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1414 return -1;
1415 }
1416
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001417 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001418 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001419 {
1420 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1421 return -1;
1422 }
1423
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001424 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001425 if(flash_read(ptn, offset + dt_entry.offset,
1426 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001427 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1428 return -1;
1429 }
1430 }
1431#endif
1432
Shashank Mittalcd98d472011-08-02 14:29:24 -07001433 }
David Ng183a7422009-12-07 14:55:21 -08001434continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001435
Dima Zavin214cc642009-01-26 11:16:21 -08001436 /* TODO: create/pass atags to kernel */
1437
Ajay Dudanie28a6072011-07-01 13:59:46 -07001438 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001439 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001440 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1441
1442 return 0;
1443}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001444
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001445BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001446void write_device_info_mmc(device_info *dev)
1447{
1448 struct device_info *info = (void*) info_buf;
1449 unsigned long long ptn = 0;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001450#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001451 unsigned long long size;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001452#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001453 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001454 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001455 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001456
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001457#if VERIFIED_BOOT
1458 index = partition_get_index("devinfo");
1459#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001460 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001461#endif
1462
Shashank Mittal162244e2011-08-08 19:01:25 -07001463 ptn = partition_get_offset(index);
1464 if(ptn == 0)
1465 {
1466 return;
1467 }
1468
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001469 lun = partition_get_lun(index);
1470 mmc_set_lun(lun);
1471
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001472#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001473 size = partition_get_size(index);
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001474#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001475
1476 memcpy(info, dev, sizeof(device_info));
1477
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001478 blocksize = mmc_get_device_blocksize();
1479
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001480#if VERIFIED_BOOT
1481 if(mmc_write(ptn, blocksize, (void *)info_buf))
1482#else
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001483 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001484#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001485 {
1486 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1487 return;
1488 }
1489}
1490
1491void read_device_info_mmc(device_info *dev)
1492{
1493 struct device_info *info = (void*) info_buf;
1494 unsigned long long ptn = 0;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001495#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001496 unsigned long long size;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001497#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001498 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001499 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001500
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001501#if VERIFIED_BOOT
1502 index = partition_get_index("devinfo");
1503#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001504 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001505#endif
1506
Shashank Mittal162244e2011-08-08 19:01:25 -07001507 ptn = partition_get_offset(index);
1508 if(ptn == 0)
1509 {
1510 return;
1511 }
1512
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001513 mmc_set_lun(partition_get_lun(index));
1514
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001515#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001516 size = partition_get_size(index);
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001517#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001518
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001519 blocksize = mmc_get_device_blocksize();
1520
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001521#if VERIFIED_BOOT
1522 if(mmc_read(ptn, (void *)info_buf, blocksize))
1523#else
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001524 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001525#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001526 {
1527 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1528 return;
1529 }
1530
1531 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1532 {
1533 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
Shashank Mittal7e033062014-09-25 19:58:07 -07001534#if DEFAULT_UNLOCK
1535 info->is_unlocked = 1;
1536#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001537 info->is_unlocked = 0;
Shashank Mittal7e033062014-09-25 19:58:07 -07001538#endif
1539 info->is_verified = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001540 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001541 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001542
1543 write_device_info_mmc(info);
1544 }
1545 memcpy(dev, info, sizeof(device_info));
1546}
1547
1548void write_device_info_flash(device_info *dev)
1549{
1550 struct device_info *info = (void *) info_buf;
1551 struct ptentry *ptn;
1552 struct ptable *ptable;
1553
1554 ptable = flash_get_ptable();
1555 if (ptable == NULL)
1556 {
1557 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1558 return;
1559 }
1560
1561 ptn = ptable_find(ptable, "devinfo");
1562 if (ptn == NULL)
1563 {
1564 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1565 return;
1566 }
1567
1568 memcpy(info, dev, sizeof(device_info));
1569
1570 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1571 {
1572 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1573 return;
1574 }
1575}
1576
1577void read_device_info_flash(device_info *dev)
1578{
1579 struct device_info *info = (void*) info_buf;
1580 struct ptentry *ptn;
1581 struct ptable *ptable;
1582
1583 ptable = flash_get_ptable();
1584 if (ptable == NULL)
1585 {
1586 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1587 return;
1588 }
1589
1590 ptn = ptable_find(ptable, "devinfo");
1591 if (ptn == NULL)
1592 {
1593 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1594 return;
1595 }
1596
1597 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1598 {
1599 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1600 return;
1601 }
1602
1603 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1604 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001605 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1606 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001607 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001608 write_device_info_flash(info);
1609 }
1610 memcpy(dev, info, sizeof(device_info));
1611}
1612
1613void write_device_info(device_info *dev)
1614{
1615 if(target_is_emmc_boot())
1616 {
1617 write_device_info_mmc(dev);
1618 }
1619 else
1620 {
1621 write_device_info_flash(dev);
1622 }
1623}
1624
1625void read_device_info(device_info *dev)
1626{
1627 if(target_is_emmc_boot())
1628 {
1629 read_device_info_mmc(dev);
1630 }
1631 else
1632 {
1633 read_device_info_flash(dev);
1634 }
1635}
1636
1637void reset_device_info()
1638{
1639 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001640 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001641 write_device_info(&device);
1642}
1643
1644void set_device_root()
1645{
1646 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001647 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001648 write_device_info(&device);
1649}
1650
Amol Jadicb524072012-08-09 16:40:18 -07001651#if DEVICE_TREE
1652int copy_dtb(uint8_t *boot_image_start)
1653{
1654 uint32 dt_image_offset = 0;
1655 uint32_t n;
1656 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001657 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001658 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001659
1660 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1661
Amol Jadicb524072012-08-09 16:40:18 -07001662 if(hdr->dt_size != 0) {
1663
1664 /* add kernel offset */
1665 dt_image_offset += page_size;
1666 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1667 dt_image_offset += n;
1668
1669 /* add ramdisk offset */
1670 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1671 dt_image_offset += n;
1672
1673 /* add second offset */
1674 if(hdr->second_size != 0) {
1675 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1676 dt_image_offset += n;
1677 }
1678
1679 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001680 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001681
Deepa Dinamani19648b42013-09-05 17:05:55 -07001682 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001683 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1684 return -1;
1685 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001686 /* Find index of device tree within device tree table */
1687 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001688 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1689 return -1;
1690 }
1691
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001692 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001693 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001694 {
1695 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1696 return -1;
1697 }
1698
Amol Jadicb524072012-08-09 16:40:18 -07001699 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001700 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001701 boot_image_start + dt_image_offset + dt_entry.offset,
1702 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001703 } else
1704 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001705
1706 /* Everything looks fine. Return success. */
1707 return 0;
1708}
1709#endif
1710
Brian Swetland9c4c0752009-01-25 16:23:50 -08001711void cmd_boot(const char *arg, void *data, unsigned sz)
1712{
1713 unsigned kernel_actual;
1714 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001715 uint32_t image_actual;
1716 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001717 uint32_t sig_actual = SIGNATURE_SIZE;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001718 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001719 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001720 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001721 int ret = 0;
1722 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001723
Amit Blay6281ebc2015-01-11 14:44:08 +02001724#ifdef MDTP_SUPPORT
1725 /* Go through Firmware Lock verification before continue with boot process */
1726 mdtp_fwlock_verify_lock();
1727 fbcon_clear();
1728#endif /* MDTP_SUPPORT */
1729
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001730#if VERIFIED_BOOT
1731 if(!device.is_unlocked)
1732 {
1733 fastboot_fail("unlock device to use this command");
1734 return;
1735 }
1736#endif
1737
Brian Swetland9c4c0752009-01-25 16:23:50 -08001738 if (sz < sizeof(hdr)) {
1739 fastboot_fail("invalid bootimage header");
1740 return;
1741 }
1742
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001743 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001744
1745 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001746 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001747
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001748 if(target_is_emmc_boot() && hdr->page_size) {
1749 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001750 page_mask = page_size - 1;
1751 }
1752
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001753 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1754 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001755#if DEVICE_TREE
1756 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1757#endif
1758
1759 image_actual = ADD_OF(page_size, kernel_actual);
1760 image_actual = ADD_OF(image_actual, ramdisk_actual);
1761 image_actual = ADD_OF(image_actual, dt_actual);
1762
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001763 if (target_use_signed_kernel() && (!device.is_unlocked))
1764 image_actual = ADD_OF(image_actual, sig_actual);
1765
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001766 /* sz should have atleast raw boot image */
1767 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001768 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001769 return;
1770 }
1771
1772 /* Verify the boot image
1773 * device & page_size are initialized in aboot_init
1774 */
1775 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001776 /* Pass size excluding signature size, otherwise we would try to
1777 * access signature beyond its length
1778 */
1779 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001780
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001781 /*
1782 * Update the kernel/ramdisk/tags address if the boot image header
1783 * has default values, these default values come from mkbootimg when
1784 * the boot image is flashed using fastboot flash:raw
1785 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001786 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1787 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001788
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001789 /* Get virtual addresses since the hdr saves physical addresses. */
1790 hdr->kernel_addr = VA(hdr->kernel_addr);
1791 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1792 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001793
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001794 /* Check if the addresses in the header are valid. */
1795 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1796 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1797 {
1798 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001799 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001800 }
1801
Amol Jadicb524072012-08-09 16:40:18 -07001802#if DEVICE_TREE
1803 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001804 ret = copy_dtb(data);
1805
1806 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001807#else
1808 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1809 {
1810 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001811 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001812 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001813#endif
1814
1815 /* Load ramdisk & kernel */
1816 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1817 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1818
1819#if DEVICE_TREE
1820 /*
1821 * If dtb is not found look for appended DTB in the kernel.
1822 * If appended dev tree is found, update the atags with
1823 * memory address to the DTB appended location on RAM.
1824 * Else update with the atags address in the kernel header
1825 */
1826 if (!dtb_copied) {
1827 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001828 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1829 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001830 if (!dtb) {
1831 fastboot_fail("dtb not found");
1832 return;
1833 }
Amol Jadicb524072012-08-09 16:40:18 -07001834 }
1835#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001836
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001837#ifndef DEVICE_TREE
1838 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1839 {
1840 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001841 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001842 }
1843#endif
1844
Brian Swetland9c4c0752009-01-25 16:23:50 -08001845 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001846 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001847
Dima Zavin77e41f32013-03-06 16:10:43 -08001848 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001849 (const char*) hdr->cmdline, board_machtype(),
1850 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001851}
1852
Sridhar Parasurame94e8152014-10-24 14:06:03 -07001853void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08001854{
1855 struct ptentry *ptn;
1856 struct ptable *ptable;
1857
1858 ptable = flash_get_ptable();
1859 if (ptable == NULL) {
1860 fastboot_fail("partition table doesn't exist");
1861 return;
1862 }
1863
1864 ptn = ptable_find(ptable, arg);
1865 if (ptn == NULL) {
1866 fastboot_fail("unknown partition name");
1867 return;
1868 }
1869
1870 if (flash_erase(ptn)) {
1871 fastboot_fail("failed to erase partition");
1872 return;
1873 }
1874 fastboot_okay("");
1875}
1876
Bikas Gurungd48bd242010-09-04 19:54:32 -07001877
1878void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1879{
1880 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001881 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001882 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001883 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001884
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001885#if VERIFIED_BOOT
1886 if(!strcmp(arg, KEYSTORE_PTN_NAME))
1887 {
1888 if(!device.is_unlocked)
1889 {
1890 fastboot_fail("unlock device to erase keystore");
1891 return;
1892 }
1893 }
1894#endif
1895
Kinson Chikf1a43512011-07-14 11:28:39 -07001896 index = partition_get_index(arg);
1897 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001898 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001899
Kinson Chikf1a43512011-07-14 11:28:39 -07001900 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001901 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001902 return;
1903 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001904
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001905 lun = partition_get_lun(index);
1906 mmc_set_lun(lun);
1907
Sridhar Parasuramd7957122015-02-27 11:33:40 -08001908 if (platform_boot_dev_isemmc())
1909 {
1910 if (mmc_erase_card(ptn, size)) {
1911 fastboot_fail("failed to erase partition\n");
1912 return;
1913 }
1914 } else {
1915 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
1916 size = partition_get_size(index);
1917 if (size > DEFAULT_ERASE_SIZE)
1918 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08001919
Sridhar Parasuramd7957122015-02-27 11:33:40 -08001920 /* Simple inefficient version of erase. Just writing
1921 0 in first several blocks */
1922 if (mmc_write(ptn , size, (unsigned int *)out)) {
1923 fastboot_fail("failed to erase partition");
1924 return;
1925 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07001926 }
1927 fastboot_okay("");
1928}
1929
Sridhar Parasurame94e8152014-10-24 14:06:03 -07001930void cmd_erase(const char *arg, void *data, unsigned sz)
1931{
1932 if(target_is_emmc_boot())
1933 cmd_erase_mmc(arg, data, sz);
1934 else
1935 cmd_erase_nand(arg, data, sz);
1936}
Bikas Gurungd48bd242010-09-04 19:54:32 -07001937
Ajay Dudani5c761132011-04-07 20:19:04 -07001938void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001939{
1940 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001941 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001942 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001943 char *token = NULL;
1944 char *pname = NULL;
1945 uint8_t lun = 0;
1946 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001947
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301948 token = strtok((char *)arg, ":");
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001949 pname = token;
1950 token = strtok(NULL, ":");
1951 if(token)
1952 {
1953 lun = atoi(token);
1954 mmc_set_lun(lun);
1955 lun_set = true;
1956 }
1957
Mao Jinlong226f33a2014-07-04 17:24:10 +08001958 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07001959 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08001960 if (!strcmp(pname, "partition"))
1961 {
1962 dprintf(INFO, "Attempt to write partition image.\n");
1963 if (write_partition(sz, (unsigned char *) data)) {
1964 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07001965 return;
1966 }
1967 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08001968 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001969 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001970#if VERIFIED_BOOT
1971 if(!strcmp(pname, KEYSTORE_PTN_NAME))
1972 {
1973 if(!device.is_unlocked)
1974 {
1975 fastboot_fail("unlock device to flash keystore");
1976 return;
1977 }
1978 if(!boot_verify_validate_keystore((unsigned char *)data))
1979 {
1980 fastboot_fail("image is not a keystore file");
1981 return;
1982 }
1983 }
1984#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08001985 index = partition_get_index(pname);
1986 ptn = partition_get_offset(index);
1987 if(ptn == 0) {
1988 fastboot_fail("partition table doesn't exist");
1989 return;
1990 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001991
Mao Jinlong226f33a2014-07-04 17:24:10 +08001992 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
1993 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1994 fastboot_fail("image is not a boot image");
1995 return;
1996 }
1997 }
1998
1999 if(!lun_set)
2000 {
2001 lun = partition_get_lun(index);
2002 mmc_set_lun(lun);
2003 }
2004
2005 size = partition_get_size(index);
2006 if (ROUND_TO_PAGE(sz,511) > size) {
2007 fastboot_fail("size too large");
2008 return;
2009 }
2010 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2011 fastboot_fail("flash write failure");
2012 return;
2013 }
Greg Grisco6e754772011-06-23 12:19:39 -07002014 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002015 }
2016 fastboot_okay("");
2017 return;
2018}
2019
Ajay Dudanide984792015-03-02 09:57:41 -08002020void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2021{
2022 int i, images;
2023 meta_header_t *meta_header;
2024 img_header_entry_t *img_header_entry;
2025
2026 meta_header = (meta_header_t*) data;
2027 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2028
2029 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2030
2031 for (i=0; i<images; i++) {
2032
2033 if((img_header_entry[i].ptn_name == NULL) ||
2034 (img_header_entry[i].start_offset == 0) ||
2035 (img_header_entry[i].size == 0))
2036 break;
2037
2038 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2039 (void *) data + img_header_entry[i].start_offset,
2040 img_header_entry[i].size);
2041 }
2042
2043 fastboot_okay("");
2044 return;
2045}
2046
Ajay Dudani5c761132011-04-07 20:19:04 -07002047void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2048{
2049 unsigned int chunk;
2050 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002051 uint32_t *fill_buf = NULL;
2052 uint32_t fill_val;
2053 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002054 sparse_header_t *sparse_header;
2055 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002056 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002057 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302058 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002059 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302060 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002061 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002062
Kinson Chikf1a43512011-07-14 11:28:39 -07002063 index = partition_get_index(arg);
2064 ptn = partition_get_offset(index);
2065 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002066 fastboot_fail("partition table doesn't exist");
2067 return;
2068 }
2069
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302070 size = partition_get_size(index);
2071 if (ROUND_TO_PAGE(sz,511) > size) {
2072 fastboot_fail("size too large");
2073 return;
2074 }
2075
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002076 lun = partition_get_lun(index);
2077 mmc_set_lun(lun);
2078
Ajay Dudani5c761132011-04-07 20:19:04 -07002079 /* Read and skip over sparse image header */
2080 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08002081 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
2082 fastboot_fail("size too large");
2083 return;
2084 }
2085
Ajay Dudani5c761132011-04-07 20:19:04 -07002086 data += sparse_header->file_hdr_sz;
2087 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
2088 {
2089 /* Skip the remaining bytes in a header that is longer than
2090 * we expected.
2091 */
2092 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
2093 }
2094
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002095 dprintf (SPEW, "=== Sparse Image Header ===\n");
2096 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2097 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2098 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2099 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2100 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2101 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2102 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2103 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002104
2105 /* Start processing chunks */
2106 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2107 {
2108 /* Read and skip over chunk header */
2109 chunk_header = (chunk_header_t *) data;
2110 data += sizeof(chunk_header_t);
2111
2112 dprintf (SPEW, "=== Chunk Header ===\n");
2113 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2114 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2115 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2116
2117 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
2118 {
2119 /* Skip the remaining bytes in a header that is longer than
2120 * we expected.
2121 */
2122 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
2123 }
2124
2125 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
2126 switch (chunk_header->chunk_type)
2127 {
2128 case CHUNK_TYPE_RAW:
2129 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2130 chunk_data_sz))
2131 {
2132 fastboot_fail("Bogus chunk size for chunk type Raw");
2133 return;
2134 }
2135
Ajay Dudaniab18f022011-05-12 14:39:22 -07002136 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2137 chunk_data_sz,
2138 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002139 {
2140 fastboot_fail("flash write failure");
2141 return;
2142 }
2143 total_blocks += chunk_header->chunk_sz;
2144 data += chunk_data_sz;
2145 break;
2146
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002147 case CHUNK_TYPE_FILL:
2148 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2149 sizeof(uint32_t)))
2150 {
2151 fastboot_fail("Bogus chunk size for chunk type FILL");
2152 return;
2153 }
2154
2155 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2156 if (!fill_buf)
2157 {
2158 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2159 return;
2160 }
2161
2162 fill_val = *(uint32_t *)data;
2163 data = (char *) data + sizeof(uint32_t);
2164 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
2165
2166 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2167 {
2168 fill_buf[i] = fill_val;
2169 }
2170
2171 for (i = 0; i < chunk_blk_cnt; i++)
2172 {
2173 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2174 sparse_header->blk_sz,
2175 fill_buf))
2176 {
2177 fastboot_fail("flash write failure");
2178 free(fill_buf);
2179 return;
2180 }
2181
2182 total_blocks++;
2183 }
2184
2185 free(fill_buf);
2186 break;
2187
Ajay Dudani5c761132011-04-07 20:19:04 -07002188 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002189 total_blocks += chunk_header->chunk_sz;
2190 break;
2191
Ajay Dudani5c761132011-04-07 20:19:04 -07002192 case CHUNK_TYPE_CRC:
2193 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2194 {
2195 fastboot_fail("Bogus chunk size for chunk type Dont Care");
2196 return;
2197 }
2198 total_blocks += chunk_header->chunk_sz;
2199 data += chunk_data_sz;
2200 break;
2201
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002202 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002203 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002204 fastboot_fail("Unknown chunk type");
2205 return;
2206 }
2207 }
2208
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002209 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2210 total_blocks, sparse_header->total_blks);
2211
2212 if(total_blocks != sparse_header->total_blks)
2213 {
2214 fastboot_fail("sparse image write failure");
2215 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002216
2217 fastboot_okay("");
2218 return;
2219}
2220
2221void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2222{
2223 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002224 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002225
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002226#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002227 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2228 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002229 int ret=0;
2230 uint32 major_version=0;
2231 uint32 minor_version=0;
2232
2233 ret = scm_svc_version(&major_version,&minor_version);
2234 if(!ret)
2235 {
2236 if(major_version >= 2)
2237 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002238 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002239 {
2240 ret = encrypt_scm((uint32 **) &data, &sz);
2241 if (ret != 0) {
2242 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2243 return;
2244 }
2245
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002246 /* Protect only for SSD */
2247 if (!strcmp(arg, "ssd")) {
2248 ret = scm_protect_keystore((uint32 *) data, sz);
2249 if (ret != 0) {
2250 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2251 return;
2252 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002253 }
2254 }
2255 else
2256 {
2257 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2258 if(ret != 0)
2259 {
2260 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2261 return;
2262 }
2263 }
2264 }
2265 else
2266 {
2267 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2268 magic_number[1] == DECRYPT_MAGIC_1)
2269 {
2270 ret = decrypt_scm((uint32 **) &data, &sz);
2271 if (ret != 0) {
2272 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2273 return;
2274 }
2275 }
2276 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2277 magic_number[1] == ENCRYPT_MAGIC_1)
2278 {
2279 ret = encrypt_scm((uint32 **) &data, &sz);
2280 if (ret != 0) {
2281 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2282 return;
2283 }
2284 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002285 }
2286 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002287 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002288 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002289 dprintf(CRITICAL,"INVALID SVC Version\n");
2290 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002291 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002292#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002293
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002294#if VERIFIED_BOOT
2295 if(!device.is_unlocked && !device.is_verified)
2296 {
2297 fastboot_fail("device is locked. Cannot flash images");
2298 return;
2299 }
2300 if(!device.is_unlocked && device.is_verified)
2301 {
2302 if(!boot_verify_flash_allowed(arg))
2303 {
2304 fastboot_fail("cannot flash this partition in verified state");
2305 return;
2306 }
2307 }
2308#endif
2309
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002310 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002311 meta_header = (meta_header_t *) data;
2312 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002313 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002314 else if (meta_header->magic == META_HEADER_MAGIC)
2315 cmd_flash_meta_img(arg, data, sz);
2316 else
2317 cmd_flash_mmc_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002318 return;
2319}
2320
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002321void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002322{
2323 struct ptentry *ptn;
2324 struct ptable *ptable;
2325 unsigned extra = 0;
2326
2327 ptable = flash_get_ptable();
2328 if (ptable == NULL) {
2329 fastboot_fail("partition table doesn't exist");
2330 return;
2331 }
2332
2333 ptn = ptable_find(ptable, arg);
2334 if (ptn == NULL) {
2335 fastboot_fail("unknown partition name");
2336 return;
2337 }
2338
2339 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2340 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2341 fastboot_fail("image is not a boot image");
2342 return;
2343 }
2344 }
2345
Amol Jadi5c61a952012-05-04 17:05:35 -07002346 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002347 || !strcmp(ptn->name, "userdata")
2348 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002349 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002350 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002351 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002352 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002353 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002354
2355 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002356 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2357 if (flash_ubi_img(ptn, data, sz)) {
2358 fastboot_fail("flash write failure");
2359 return;
2360 }
2361 } else {
2362 if (flash_write(ptn, extra, data, sz)) {
2363 fastboot_fail("flash write failure");
2364 return;
2365 }
Dima Zavin214cc642009-01-26 11:16:21 -08002366 }
2367 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2368 fastboot_okay("");
2369}
2370
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002371void cmd_flash(const char *arg, void *data, unsigned sz)
2372{
2373 if(target_is_emmc_boot())
2374 cmd_flash_mmc(arg, data, sz);
2375 else
2376 cmd_flash_nand(arg, data, sz);
2377}
2378
Dima Zavin214cc642009-01-26 11:16:21 -08002379void cmd_continue(const char *arg, void *data, unsigned sz)
2380{
2381 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002382 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002383
Amit Blay6281ebc2015-01-11 14:44:08 +02002384#ifdef MDTP_SUPPORT
2385 /* Go through Firmware Lock verification before continue with boot process */
2386 mdtp_fwlock_verify_lock();
2387 fbcon_clear();
2388#endif /* MDTP_SUPPORT */
2389
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002390 if (target_is_emmc_boot())
2391 {
2392 boot_linux_from_mmc();
2393 }
2394 else
2395 {
2396 boot_linux_from_flash();
2397 }
Dima Zavin214cc642009-01-26 11:16:21 -08002398}
2399
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002400void cmd_reboot(const char *arg, void *data, unsigned sz)
2401{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002402 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002403 fastboot_okay("");
2404 reboot_device(0);
2405}
2406
2407void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2408{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002409 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002410 fastboot_okay("");
2411 reboot_device(FASTBOOT_MODE);
2412}
2413
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002414void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2415{
2416 dprintf(INFO, "Enabling charger screen check\n");
2417 device.charger_screen_enabled = 1;
2418 write_device_info(&device);
2419 fastboot_okay("");
2420}
2421
2422void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2423{
2424 dprintf(INFO, "Disabling charger screen check\n");
2425 device.charger_screen_enabled = 0;
2426 write_device_info(&device);
2427 fastboot_okay("");
2428}
2429
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302430void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2431{
2432 dprintf(INFO, "Selecting display panel %s\n", arg);
2433 if (arg)
2434 strlcpy(device.display_panel, arg,
2435 sizeof(device.display_panel));
2436 write_device_info(&device);
2437 fastboot_okay("");
2438}
2439
Shashank Mittal162244e2011-08-08 19:01:25 -07002440void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2441{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002442 /* TODO: Wipe user data */
2443 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002444 {
2445 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002446 device.is_verified = 0;
2447 write_device_info(&device);
2448 }
2449 fastboot_okay("");
2450}
2451
2452void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2453{
2454 /* TODO: Wipe user data */
2455 if(device.is_unlocked || device.is_verified)
2456 {
2457 device.is_unlocked = 0;
2458 device.is_verified = 0;
2459 write_device_info(&device);
2460 }
2461 fastboot_okay("");
2462}
2463
2464void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2465{
2466 /* TODO: Wipe user data */
2467 if(device.is_unlocked || !device.is_verified)
2468 {
2469 device.is_unlocked = 0;
2470 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002471 write_device_info(&device);
2472 }
2473 fastboot_okay("");
2474}
2475
Shashank Mittala0032282011-08-26 14:50:11 -07002476void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2477{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302478 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002479 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002480 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002481 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2482 fastboot_info(response);
2483 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002484 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302485 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2486 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002487 fastboot_okay("");
2488}
2489
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002490void cmd_preflash(const char *arg, void *data, unsigned sz)
2491{
2492 fastboot_okay("");
2493}
2494
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302495struct fbimage* splash_screen_flash();
2496
2497int splash_screen_check_header(struct fbimage *logo)
2498{
2499 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2500 return -1;
2501 if (logo->header.width == 0 || logo->header.height == 0)
2502 return -1;
2503 return 0;
2504}
2505
2506struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002507{
2508 struct ptentry *ptn;
2509 struct ptable *ptable;
2510 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002511 struct fbimage *logo = NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002512
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302513
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002514 logo = (struct fbimage *) malloc(ROUNDUP(page_size, sizeof(struct fbimage)));
2515 ASSERT(logo);
2516
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302517 ptable = flash_get_ptable();
2518 if (ptable == NULL) {
2519 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002520 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302521 }
2522 ptn = ptable_find(ptable, "splash");
2523 if (ptn == NULL) {
2524 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002525 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302526 }
2527
2528 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2529 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002530 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302531 }
2532
2533 if (splash_screen_check_header(logo)) {
2534 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002535 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302536 }
2537
2538 fb_display = fbcon_display();
2539 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302540 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2541 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back to default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002542 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002543 }
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302544 uint8_t *base = (uint8_t *) fb_display->base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302545 if (flash_read(ptn + sizeof(logo->header), 0,
2546 base,
2547 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2548 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302549 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002550 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002551 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302552 logo->image = base;
2553 }
2554
2555 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002556
2557err:
2558 free(logo);
2559 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302560}
2561
2562struct fbimage* splash_screen_mmc()
2563{
2564 int index = INVALID_PTN;
2565 unsigned long long ptn = 0;
2566 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002567 struct fbimage *logo = NULL;
Channagoud Kadabida117392014-11-24 11:01:12 -08002568 uint32_t blocksize;
2569 uint32_t readsize;
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002570 uint32_t logosize;
Channagoud Kadabida117392014-11-24 11:01:12 -08002571 uint32_t ptn_size;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302572
2573 index = partition_get_index("splash");
2574 if (index == 0) {
2575 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2576 return NULL;
2577 }
2578
2579 ptn = partition_get_offset(index);
2580 if (ptn == 0) {
2581 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2582 return NULL;
2583 }
2584
Channagoud Kadabida117392014-11-24 11:01:12 -08002585 ptn_size = partition_get_size(index);
2586 blocksize = mmc_get_device_blocksize();
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002587 logosize = ROUNDUP(sizeof(logo->header), blocksize);
Channagoud Kadabida117392014-11-24 11:01:12 -08002588
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002589 logo = (struct fbimage *)memalign(CACHE_LINE, ROUNDUP(logosize, CACHE_LINE));
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002590 ASSERT(logo);
2591
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002592 if (mmc_read(ptn, (uint32_t *) logo, logosize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302593 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002594 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302595 }
2596
2597 if (splash_screen_check_header(logo)) {
2598 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002599 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302600 }
2601
2602 fb_display = fbcon_display();
2603 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302604 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2605 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002606 goto err;
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302607 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302608 uint8_t *base = (uint8_t *) fb_display->base;
Channagoud Kadabida117392014-11-24 11:01:12 -08002609 readsize = ROUNDUP((logo->header.width * logo->header.height * fb_display->bpp/8), blocksize);
2610
2611 if (readsize > ptn_size)
2612 {
2613 dprintf(CRITICAL, "@%d:Invalid logo header readsize:%u exceeds ptn_size:%u\n", __LINE__, readsize,ptn_size);
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002614 goto err;
Channagoud Kadabida117392014-11-24 11:01:12 -08002615 }
2616
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002617 if (mmc_read(ptn + logosize,(uint32_t *)base, readsize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302618 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302619 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002620 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302621 }
2622
2623 logo->image = base;
2624 }
2625
2626 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002627
2628err:
2629 free(logo);
2630 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302631}
2632
2633
2634struct fbimage* fetch_image_from_partition()
2635{
2636 if (target_is_emmc_boot()) {
2637 return splash_screen_mmc();
2638 } else {
2639 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002640 }
2641}
2642
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002643/* Get the size from partiton name */
2644static void get_partition_size(const char *arg, char *response)
2645{
2646 uint64_t ptn = 0;
2647 uint64_t size;
2648 int index = INVALID_PTN;
2649
2650 index = partition_get_index(arg);
2651
2652 if (index == INVALID_PTN)
2653 {
2654 dprintf(CRITICAL, "Invalid partition index\n");
2655 return;
2656 }
2657
2658 ptn = partition_get_offset(index);
2659
2660 if(!ptn)
2661 {
2662 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2663 return;
2664 }
2665
2666 size = partition_get_size(index);
2667
2668 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2669 return;
2670}
2671
2672/*
2673 * Publish the partition type & size info
2674 * fastboot getvar will publish the required information.
2675 * fastboot getvar partition_size:<partition_name>: partition size in hex
2676 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2677 */
2678static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2679{
2680 uint8_t i;
2681
2682 for (i = 0; i < num_parts; i++) {
2683 get_partition_size(info[i].part_name, info[i].size_response);
2684
2685 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2686 {
2687 dprintf(CRITICAL, "partition size name truncated\n");
2688 return;
2689 }
2690 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2691 {
2692 dprintf(CRITICAL, "partition type name truncated\n");
2693 return;
2694 }
2695
2696 /* publish partition size & type info */
2697 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2698 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2699 }
2700}
2701
Amol Jadi5edf3552013-07-23 14:15:34 -07002702/* register commands and variables for fastboot */
2703void aboot_fastboot_register_commands(void)
2704{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002705 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07002706
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002707 struct fastboot_cmd_desc cmd_list[] = {
2708 /* By default the enabled list is empty. */
2709 {"", NULL},
2710 /* move commands enclosed within the below ifndef to here
2711 * if they need to be enabled in user build.
2712 */
2713#ifndef DISABLE_FASTBOOT_CMDS
2714 /* Register the following commands only for non-user builds */
2715 {"flash:", cmd_flash},
2716 {"erase:", cmd_erase},
2717 {"boot", cmd_boot},
2718 {"continue", cmd_continue},
2719 {"reboot", cmd_reboot},
2720 {"reboot-bootloader", cmd_reboot_bootloader},
2721 {"oem unlock", cmd_oem_unlock},
2722 {"oem lock", cmd_oem_lock},
2723 {"oem verified", cmd_oem_verified},
2724 {"oem device-info", cmd_oem_devinfo},
2725 {"preflash", cmd_preflash},
2726 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
2727 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08002728 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002729#endif
2730 };
2731
2732 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
2733 for (i = 1; i < fastboot_cmds_count; i++)
2734 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
2735
Amol Jadi5edf3552013-07-23 14:15:34 -07002736 /* publish variables and their values */
2737 fastboot_publish("product", TARGET(BOARD));
2738 fastboot_publish("kernel", "lk");
2739 fastboot_publish("serialno", sn_buf);
2740
2741 /*
2742 * partition info is supported only for emmc partitions
2743 * Calling this for NAND prints some error messages which
2744 * is harmless but misleading. Avoid calling this for NAND
2745 * devices.
2746 */
2747 if (target_is_emmc_boot())
2748 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2749
2750 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002751 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2752 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002753 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002754 /* Is the charger screen check enabled */
2755 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2756 device.charger_screen_enabled);
2757 fastboot_publish("charger-screen-enabled",
2758 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302759 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2760 device.display_panel);
2761 fastboot_publish("display-panel",
2762 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002763}
2764
Brian Swetland9c4c0752009-01-25 16:23:50 -08002765void aboot_init(const struct app_descriptor *app)
2766{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002767 unsigned reboot_mode = 0;
lijuang72875802015-02-10 10:35:12 +08002768 unsigned hard_reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002769 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002770
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002771 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002772 if (target_is_emmc_boot())
2773 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002774 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002775 page_mask = page_size - 1;
2776 }
2777 else
2778 {
2779 page_size = flash_page_size();
2780 page_mask = page_size - 1;
2781 }
2782
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002783 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2784
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002785 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002786
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002787 /* Display splash screen if enabled */
2788#if DISPLAY_SPLASH_SCREEN
2789 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302790 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002791 dprintf(SPEW, "Display Init: Done\n");
2792#endif
2793
2794
Greg Griscod6250552011-06-29 14:40:23 -07002795 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002796 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002797
Dhaval Patel223ec952013-07-18 14:49:44 -07002798 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2799
Matthew Qindefd5562014-07-11 18:02:40 +08002800 /*
2801 * Check power off reason if user force reset,
2802 * if yes phone will do normal boot.
2803 */
2804 if (is_user_force_reset())
2805 goto normal_boot;
2806
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002807 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002808 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002809 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002810 dprintf(ALWAYS,"dload mode key sequence detected\n");
2811 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002812 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002813 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002814 }
2815 else
2816 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302817 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002818 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002819 }
2820 boot_into_fastboot = true;
2821 }
2822 if (!boot_into_fastboot)
2823 {
2824 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2825 boot_into_recovery = 1;
2826 if (!boot_into_recovery &&
2827 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002828 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002829 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002830 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002831 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002832 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002833 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002834
Ajay Dudani77421292010-10-27 19:34:06 -07002835 reboot_mode = check_reboot_mode();
lijuang72875802015-02-10 10:35:12 +08002836 hard_reboot_mode = check_hard_reboot_mode();
2837 if (reboot_mode == RECOVERY_MODE ||
2838 hard_reboot_mode == RECOVERY_HARD_RESET_MODE) {
Ajay Dudani77421292010-10-27 19:34:06 -07002839 boot_into_recovery = 1;
lijuang72875802015-02-10 10:35:12 +08002840 } else if(reboot_mode == FASTBOOT_MODE ||
2841 hard_reboot_mode == FASTBOOT_HARD_RESET_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002842 boot_into_fastboot = true;
lijuang72875802015-02-10 10:35:12 +08002843 } else if(reboot_mode == ALARM_BOOT ||
2844 hard_reboot_mode == RTC_HARD_RESET_MODE) {
Matthew Qind886f3c2014-01-17 16:52:01 +08002845 boot_reason_alarm = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002846 }
2847
Matthew Qindefd5562014-07-11 18:02:40 +08002848normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03002849 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002850 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002851 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002852 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002853 if(emmc_recovery_init())
2854 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2855 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002856 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002857 if((device.is_unlocked) || (device.is_tampered))
2858 {
2859 #ifdef TZ_TAMPER_FUSE
2860 set_tamper_fuse_cmd();
2861 #endif
2862 #if USE_PCOM_SECBOOT
2863 set_tamper_flag(device.is_tampered);
2864 #endif
2865 }
Shashank Mittala0032282011-08-26 14:50:11 -07002866 }
Amit Blay6281ebc2015-01-11 14:44:08 +02002867
2868#ifdef MDTP_SUPPORT
2869 /* Go through Firmware Lock verification before continue with boot process */
2870 mdtp_fwlock_verify_lock();
2871 fbcon_clear();
2872#endif /* MDTP_SUPPORT */
2873
Pavel Nedev5d91d412013-04-29 11:34:24 +03002874 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002875 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002876 else
2877 {
2878 recovery_init();
2879 #if USE_PCOM_SECBOOT
2880 if((device.is_unlocked) || (device.is_tampered))
2881 set_tamper_flag(device.is_tampered);
2882 #endif
2883 boot_linux_from_flash();
2884 }
2885 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2886 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002887 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002888
Amol Jadi5edf3552013-07-23 14:15:34 -07002889 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002890
Amol Jadi5edf3552013-07-23 14:15:34 -07002891 /* register aboot specific fastboot commands */
2892 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002893
Amol Jadi5edf3552013-07-23 14:15:34 -07002894 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002895 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002896
2897 /* initialize and start fastboot */
2898 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002899}
2900
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002901uint32_t get_page_size()
2902{
2903 return page_size;
2904}
2905
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002906/*
2907 * Calculated and save hash (SHA256) for non-signed boot image.
2908 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002909 * @param image_addr - Boot image address
2910 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002911 *
2912 * @return int - 0 on success, negative value on failure.
2913 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002914static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002915{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002916 unsigned int digest[8];
2917#if IMAGE_VERIF_ALGO_SHA1
2918 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
2919#else
2920 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
2921#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002922
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002923 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002924 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002925
2926 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002927 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002928
2929 return 0;
2930}
2931
Brian Swetland9c4c0752009-01-25 16:23:50 -08002932APP_START(aboot)
2933 .init = aboot_init,
2934APP_END