blob: 23e6350812af4d3510afef00e495c540d9c3085b [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08005 * Copyright (c) 2009-2015, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080060#include <decompress.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080061#if USE_RPMB_FOR_DEVINFO
62#include <rpmb.h>
63#endif
Dima Zavin214cc642009-01-26 11:16:21 -080064
Neeti Desai17379b82012-06-04 18:42:53 -070065#if DEVICE_TREE
66#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070067#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070068#endif
69
Shashank Mittalcd98d472011-08-02 14:29:24 -070070#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080071#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080072#include "bootimg.h"
73#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070074#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080075#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070076#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070077#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070078#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070079#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020080#include "mdtp.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070081
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070082extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070083extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070084extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070085extern int get_target_boot_params(const char *cmdline, const char *part,
86 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070087
88void write_device_info_mmc(device_info *dev);
89void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -070090static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070091
Sridhar Parasurame94e8152014-10-24 14:06:03 -070092/* fastboot command function pointer */
93typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
94
95struct fastboot_cmd_desc {
96 char * name;
97 fastboot_cmd_fn cb;
98};
99
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700100#define EXPAND(NAME) #NAME
101#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700102
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800103#ifdef MEMBASE
104#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
105#else
David Ng183a7422009-12-07 14:55:21 -0800106#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800107#endif
108
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700109#ifndef MEMSIZE
110#define MEMSIZE 1024*1024
111#endif
112
113#define MAX_TAGS_SIZE 1024
114
lijuang72875802015-02-10 10:35:12 +0800115#define RECOVERY_HARD_RESET_MODE 0x01
116#define FASTBOOT_HARD_RESET_MODE 0x02
117#define RTC_HARD_RESET_MODE 0x03
118
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800119#define RECOVERY_MODE 0x77665502
120#define FASTBOOT_MODE 0x77665500
Matthew Qind886f3c2014-01-17 16:52:01 +0800121#define ALARM_BOOT 0x77665503
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800122
Kun Liang2f1601a2013-08-12 16:29:54 +0800123/* make 4096 as default size to ensure EFS,EXT4's erasing */
124#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800125#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800126
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700127#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700128#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800129
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800130#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
131
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700132#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
133
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800134#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700135static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700136#else
David Ng183a7422009-12-07 14:55:21 -0800137static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700138#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800139static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300140static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800141static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800142static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800143static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700144static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300145static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200146static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800147
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800148static const char *baseband_apq = " androidboot.baseband=apq";
149static const char *baseband_msm = " androidboot.baseband=msm";
150static const char *baseband_csfb = " androidboot.baseband=csfb";
151static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700152static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800153static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700154static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800155static const char *baseband_dsda = " androidboot.baseband=dsda";
156static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800157static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800158static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800159
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700160static unsigned page_size = 0;
161static unsigned page_mask = 0;
162static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
163static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700164static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800165static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700166static bool devinfo_present = true;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700167
Shashank Mittalcd98d472011-08-02 14:29:24 -0700168/* Assuming unauthorized kernel image by default */
169static int auth_kernel_img = 0;
170
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -0800171static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
Shashank Mittal162244e2011-08-08 19:01:25 -0700172
Dima Zavin42168f22009-01-30 11:52:22 -0800173struct atag_ptbl_entry
174{
175 char name[16];
176 unsigned offset;
177 unsigned size;
178 unsigned flags;
179};
180
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700181/*
182 * Partition info, required to be published
183 * for fastboot
184 */
185struct getvar_partition_info {
186 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
187 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
188 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
189 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
190 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
191};
192
193/*
194 * Right now, we are publishing the info for only
195 * three partitions
196 */
197struct getvar_partition_info part_info[] =
198{
199 { "system" , "partition-size:", "partition-type:", "", "ext4" },
200 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
201 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
202};
203
204char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700205char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800206char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700207char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530208char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700209
Greg Griscod2471ef2011-07-14 13:00:42 -0700210extern int emmc_recovery_init(void);
211
Kinson Chik0b1c8162011-08-31 16:31:57 -0700212#if NO_KEYPAD_DRIVER
213extern int fastboot_trigger(void);
214#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700215
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800216static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700217{
218 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700219#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800220 if (is_arm64)
221 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
222 else
223 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700224 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
225 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700226#endif
227}
228
Dima Zavin42168f22009-01-30 11:52:22 -0800229static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
230{
231 struct atag_ptbl_entry atag_ptn;
232
233 memcpy(atag_ptn.name, ptn->name, 16);
234 atag_ptn.name[15] = '\0';
235 atag_ptn.offset = ptn->start;
236 atag_ptn.size = ptn->length;
237 atag_ptn.flags = ptn->flags;
238 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
239 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
240}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800241
Neeti Desaie245d492012-06-01 12:52:13 -0700242unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800243{
David Ng183a7422009-12-07 14:55:21 -0800244 int cmdline_len = 0;
245 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800246 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700247 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800248 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300249 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700250 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700251 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200252 bool is_mdtp_activated = 0;
253#ifdef MDTP_SUPPORT
254 mdtp_activated(&is_mdtp_activated);
255#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800256
Brian Swetland9c4c0752009-01-25 16:23:50 -0800257 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800258 cmdline_len = strlen(cmdline);
259 have_cmdline = 1;
260 }
261 if (target_is_emmc_boot()) {
262 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800263#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700264 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
265 ASSERT(boot_dev_buf);
266 platform_boot_dev_cmdline(boot_dev_buf);
267 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700268#endif
David Ng183a7422009-12-07 14:55:21 -0800269 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800270
271 cmdline_len += strlen(usb_sn_cmdline);
272 cmdline_len += strlen(sn_buf);
273
Pavel Nedev5614d222013-06-17 18:01:02 +0300274 if (boot_into_recovery && gpt_exists)
275 cmdline_len += strlen(secondary_gpt_enable);
276
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200277 if(is_mdtp_activated)
278 cmdline_len += strlen(mdtp_activated_flag);
279
Pavel Nedev328ac822013-04-05 15:25:11 +0300280 if (boot_into_ffbm) {
281 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700282 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800283 /* reduce kernel console messages to speed-up boot */
284 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800285 } else if (boot_reason_alarm) {
286 cmdline_len += strlen(alarmboot_cmdline);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700287 } else if (device.charger_screen_enabled &&
288 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700289 pause_at_bootup = 1;
290 cmdline_len += strlen(battchg_pause);
291 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800292
Shashank Mittalcd98d472011-08-02 14:29:24 -0700293 if(target_use_signed_kernel() && auth_kernel_img) {
294 cmdline_len += strlen(auth_kernel);
295 }
296
Joonwoo Park61112782013-10-02 19:50:39 -0700297 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
298 "system",
299 target_boot_params,
300 sizeof(target_boot_params)) == 0) {
301 have_target_boot_params = 1;
302 cmdline_len += strlen(target_boot_params);
303 }
304
Ajay Dudanid04110c2011-01-17 23:55:07 -0800305 /* Determine correct androidboot.baseband to use */
306 switch(target_baseband())
307 {
308 case BASEBAND_APQ:
309 cmdline_len += strlen(baseband_apq);
310 break;
311
312 case BASEBAND_MSM:
313 cmdline_len += strlen(baseband_msm);
314 break;
315
316 case BASEBAND_CSFB:
317 cmdline_len += strlen(baseband_csfb);
318 break;
319
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800320 case BASEBAND_SVLTE2A:
321 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800322 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700323
324 case BASEBAND_MDM:
325 cmdline_len += strlen(baseband_mdm);
326 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700327
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800328 case BASEBAND_MDM2:
329 cmdline_len += strlen(baseband_mdm2);
330 break;
331
Amol Jadi5c61a952012-05-04 17:05:35 -0700332 case BASEBAND_SGLTE:
333 cmdline_len += strlen(baseband_sglte);
334 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530335
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800336 case BASEBAND_SGLTE2:
337 cmdline_len += strlen(baseband_sglte2);
338 break;
339
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530340 case BASEBAND_DSDA:
341 cmdline_len += strlen(baseband_dsda);
342 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800343
344 case BASEBAND_DSDA2:
345 cmdline_len += strlen(baseband_dsda2);
346 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800347 }
348
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800349 if (cmdline) {
350 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
351 target_display_panel_node(device.display_panel,
352 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
353 strlen(display_panel_buf)) {
354 cmdline_len += strlen(display_panel_buf);
355 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700356 }
357
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800358 if (target_warm_boot()) {
359 warm_boot = true;
360 cmdline_len += strlen(warmboot_cmdline);
361 }
362
David Ng183a7422009-12-07 14:55:21 -0800363 if (cmdline_len > 0) {
364 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800365 unsigned char *dst;
366
367 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
368 ASSERT(cmdline_final != NULL);
369 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700370
Amol Jadi168b7712012-03-06 16:15:00 -0800371 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800372 if (have_cmdline) {
373 src = cmdline;
374 while ((*dst++ = *src++));
375 }
376 if (target_is_emmc_boot()) {
377 src = emmc_cmdline;
378 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700379 have_cmdline = 1;
380 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800381#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700382 src = boot_dev_buf;
383 if (have_cmdline) --dst;
384 while ((*dst++ = *src++));
385#endif
David Ngf773dde2010-07-26 19:55:08 -0700386 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800387
388 src = usb_sn_cmdline;
389 if (have_cmdline) --dst;
390 have_cmdline = 1;
391 while ((*dst++ = *src++));
392 src = sn_buf;
393 if (have_cmdline) --dst;
394 have_cmdline = 1;
395 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800396 if (warm_boot) {
397 if (have_cmdline) --dst;
398 src = warmboot_cmdline;
399 while ((*dst++ = *src++));
400 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800401
Pavel Nedev5614d222013-06-17 18:01:02 +0300402 if (boot_into_recovery && gpt_exists) {
403 src = secondary_gpt_enable;
404 if (have_cmdline) --dst;
405 while ((*dst++ = *src++));
406 }
407
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200408 if (is_mdtp_activated) {
409 src = mdtp_activated_flag;
410 if (have_cmdline) --dst;
411 while ((*dst++ = *src++));
412 }
413
Pavel Nedev328ac822013-04-05 15:25:11 +0300414 if (boot_into_ffbm) {
415 src = androidboot_mode;
416 if (have_cmdline) --dst;
417 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700418 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300419 if (have_cmdline) --dst;
420 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800421 src = loglevel;
422 if (have_cmdline) --dst;
423 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800424 } else if (boot_reason_alarm) {
425 src = alarmboot_cmdline;
426 if (have_cmdline) --dst;
427 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300428 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700429 src = battchg_pause;
430 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800431 while ((*dst++ = *src++));
432 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800433
Shashank Mittalcd98d472011-08-02 14:29:24 -0700434 if(target_use_signed_kernel() && auth_kernel_img) {
435 src = auth_kernel;
436 if (have_cmdline) --dst;
437 while ((*dst++ = *src++));
438 }
439
Ajay Dudanid04110c2011-01-17 23:55:07 -0800440 switch(target_baseband())
441 {
442 case BASEBAND_APQ:
443 src = baseband_apq;
444 if (have_cmdline) --dst;
445 while ((*dst++ = *src++));
446 break;
447
448 case BASEBAND_MSM:
449 src = baseband_msm;
450 if (have_cmdline) --dst;
451 while ((*dst++ = *src++));
452 break;
453
454 case BASEBAND_CSFB:
455 src = baseband_csfb;
456 if (have_cmdline) --dst;
457 while ((*dst++ = *src++));
458 break;
459
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800460 case BASEBAND_SVLTE2A:
461 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800462 if (have_cmdline) --dst;
463 while ((*dst++ = *src++));
464 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700465
466 case BASEBAND_MDM:
467 src = baseband_mdm;
468 if (have_cmdline) --dst;
469 while ((*dst++ = *src++));
470 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700471
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800472 case BASEBAND_MDM2:
473 src = baseband_mdm2;
474 if (have_cmdline) --dst;
475 while ((*dst++ = *src++));
476 break;
477
Amol Jadi5c61a952012-05-04 17:05:35 -0700478 case BASEBAND_SGLTE:
479 src = baseband_sglte;
480 if (have_cmdline) --dst;
481 while ((*dst++ = *src++));
482 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530483
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800484 case BASEBAND_SGLTE2:
485 src = baseband_sglte2;
486 if (have_cmdline) --dst;
487 while ((*dst++ = *src++));
488 break;
489
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530490 case BASEBAND_DSDA:
491 src = baseband_dsda;
492 if (have_cmdline) --dst;
493 while ((*dst++ = *src++));
494 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800495
496 case BASEBAND_DSDA2:
497 src = baseband_dsda2;
498 if (have_cmdline) --dst;
499 while ((*dst++ = *src++));
500 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800501 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700502
503 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700504 src = display_panel_buf;
505 if (have_cmdline) --dst;
506 while ((*dst++ = *src++));
507 }
Joonwoo Park61112782013-10-02 19:50:39 -0700508
509 if (have_target_boot_params) {
510 if (have_cmdline) --dst;
511 src = target_boot_params;
512 while ((*dst++ = *src++));
513 }
Neeti Desaie245d492012-06-01 12:52:13 -0700514 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700515
516
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700517 if (boot_dev_buf)
518 free(boot_dev_buf);
519
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800520 if (cmdline_final)
521 dprintf(INFO, "cmdline: %s\n", cmdline_final);
522 else
523 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700524 return cmdline_final;
525}
526
527unsigned *atag_core(unsigned *ptr)
528{
529 /* CORE */
530 *ptr++ = 2;
531 *ptr++ = 0x54410001;
532
533 return ptr;
534
535}
536
537unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
538 unsigned ramdisk_size)
539{
540 if (ramdisk_size) {
541 *ptr++ = 4;
542 *ptr++ = 0x54420005;
543 *ptr++ = (unsigned)ramdisk;
544 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800545 }
546
Neeti Desaie245d492012-06-01 12:52:13 -0700547 return ptr;
548}
549
550unsigned *atag_ptable(unsigned **ptr_addr)
551{
552 int i;
553 struct ptable *ptable;
554
555 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700556 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
557 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700558 *(*ptr_addr)++ = 0x4d534d70;
559 for (i = 0; i < ptable->count; ++i)
560 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
561 }
562
563 return (*ptr_addr);
564}
565
566unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
567{
568 int cmdline_length = 0;
569 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700570 char *dest;
571
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800572 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700573 n = (cmdline_length + 4) & (~3);
574
575 *ptr++ = (n / 4) + 2;
576 *ptr++ = 0x54410009;
577 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800578 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700579 ptr += (n / 4);
580
581 return ptr;
582}
583
584unsigned *atag_end(unsigned *ptr)
585{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800586 /* END */
587 *ptr++ = 0;
588 *ptr++ = 0;
589
Neeti Desaie245d492012-06-01 12:52:13 -0700590 return ptr;
591}
592
593void generate_atags(unsigned *ptr, const char *cmdline,
594 void *ramdisk, unsigned ramdisk_size)
595{
596
597 ptr = atag_core(ptr);
598 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
599 ptr = target_atag_mem(ptr);
600
601 /* Skip NAND partition ATAGS for eMMC boot */
602 if (!target_is_emmc_boot()){
603 ptr = atag_ptable(&ptr);
604 }
605
606 ptr = atag_cmdline(ptr, cmdline);
607 ptr = atag_end(ptr);
608}
609
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700610typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700611void boot_linux(void *kernel, unsigned *tags,
612 const char *cmdline, unsigned machtype,
613 void *ramdisk, unsigned ramdisk_size)
614{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800615 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800616#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700617 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800618#endif
619
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700620 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800621 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800622 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800623
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530624 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700625
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800626 final_cmdline = update_cmdline((const char*)cmdline);
627
Neeti Desai17379b82012-06-04 18:42:53 -0700628#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800629 dprintf(INFO, "Updating device tree: start\n");
630
Neeti Desai17379b82012-06-04 18:42:53 -0700631 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530632 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700633 if(ret)
634 {
635 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
636 ASSERT(0);
637 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800638 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700639#else
Neeti Desaie245d492012-06-01 12:52:13 -0700640 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800641 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700642#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700643
Maria Yu52254c02014-07-04 16:14:54 +0800644 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700645
646#if VERIFIED_BOOT
647 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700648 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700649 {
650 dprintf(INFO, "Failed to write protect dev info\n");
651 ASSERT(0);
652 }
653#endif
654
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700655 /* Perform target specific cleanup */
656 target_uninit();
657
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800658 /* Turn off splash screen if enabled */
659#if DISPLAY_SPLASH_SCREEN
660 target_display_shutdown();
661#endif
662
663
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800664 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530665 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800666
667 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700668
Amol Jadi4421e652011-06-16 15:00:48 -0700669 /* do any platform specific cleanup before kernel entry */
670 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700671
Brian Swetland9c4c0752009-01-25 16:23:50 -0800672 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700673
Amol Jadi504f9fe2012-08-16 13:56:48 -0700674#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800675 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700676#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700677 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800678
679 if (IS_ARM64(kptr))
680 /* Jump to a 64bit kernel */
681 scm_elexec_call((paddr_t)kernel, tags_phys);
682 else
683 /* Jump to a 32bit kernel */
684 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800685}
686
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700687/* Function to check if the memory address range falls within the aboot
688 * boundaries.
689 * start: Start of the memory region
690 * size: Size of the memory region
691 */
692int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
693{
694 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800695 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700696 return -1;
697
698 /* Check for memory overlap. */
699 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
700 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700701 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700702 return 0;
703 else
704 return -1;
705}
706
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800707#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800708
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800709BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800710#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800711BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800712#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800713
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700714static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
715{
716 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800717
718#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800719#if IMAGE_VERIF_ALGO_SHA1
720 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
721#else
722 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
723#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800724#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700725
726 /* Assume device is rooted at this time. */
727 device.is_tampered = 1;
728
729 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
730
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700731#if VERIFIED_BOOT
732 if(boot_into_recovery)
733 {
734 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530735 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700736 }
737 else
738 {
739 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530740 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700741 }
742 boot_verify_print_state();
743#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700744 ret = image_verify((unsigned char *)bootimg_addr,
745 (unsigned char *)(bootimg_addr + bootimg_size),
746 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800747 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700748#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700749 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
750
751 if (ret)
752 {
753 /* Authorized kernel */
754 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700755 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700756 }
757
Amit Blay4aa292f2015-04-28 21:55:59 +0300758#ifdef MDTP_SUPPORT
759 {
760 /* Verify MDTP lock.
761 * For boot & recovery partitions, use aboot's verification result.
762 */
763 mdtp_ext_partition_verification_t ext_partition;
764 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
765 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
766 ext_partition.page_size = 0; /* Not needed since already validated */
767 ext_partition.image_addr = 0; /* Not needed since already validated */
768 ext_partition.image_size = 0; /* Not needed since already validated */
769 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
770 mdtp_fwlock_verify_lock(&ext_partition);
771 }
772#endif /* MDTP_SUPPORT */
773
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700774#if USE_PCOM_SECBOOT
775 set_tamper_flag(device.is_tampered);
776#endif
777
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700778#if VERIFIED_BOOT
779 if(boot_verify_get_state() == RED)
780 {
781 if(!boot_into_recovery)
782 {
783 dprintf(CRITICAL,
784 "Device verification failed. Rebooting into recovery.\n");
785 reboot_device(RECOVERY_MODE);
786 }
787 else
788 {
789 dprintf(CRITICAL,
790 "Recovery image verification failed. Asserting..\n");
791 ASSERT(0);
792 }
793 }
794#endif
Unnati Gandhi1be04752015-03-27 19:41:53 +0530795
796 if(device.is_tampered)
797 {
798 write_device_info_mmc(&device);
799 #ifdef TZ_TAMPER_FUSE
800 set_tamper_fuse_cmd();
801 #endif
802 #ifdef ASSERT_ON_TAMPER
803 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
804 ASSERT(0);
805 #endif
806 }
807
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700808}
809
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530810static bool check_format_bit()
811{
812 bool ret = false;
813 int index;
814 uint64_t offset;
815 struct boot_selection_info *in = NULL;
816 char *buf = NULL;
817
818 index = partition_get_index("bootselect");
819 if (index == INVALID_PTN)
820 {
821 dprintf(INFO, "Unable to locate /bootselect partition\n");
822 return ret;
823 }
824 offset = partition_get_offset(index);
825 if(!offset)
826 {
827 dprintf(INFO, "partition /bootselect doesn't exist\n");
828 return ret;
829 }
830 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
831 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530832 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530833 {
834 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
835 free(buf);
836 return ret;
837 }
838 in = (struct boot_selection_info *) buf;
839 if ((in->signature == BOOTSELECT_SIGNATURE) &&
840 (in->version == BOOTSELECT_VERSION)) {
841 if ((in->state_info & BOOTSELECT_FORMAT) &&
842 !(in->state_info & BOOTSELECT_FACTORY))
843 ret = true;
844 } else {
845 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
846 in->signature, in->version);
847 ASSERT(0);
848 }
849 free(buf);
850 return ret;
851}
852
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700853void boot_verifier_init()
854{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700855 uint32_t boot_state;
856 /* Check if device unlock */
857 if(device.is_unlocked)
858 {
859 boot_verify_send_event(DEV_UNLOCK);
860 boot_verify_print_state();
861 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
862 return;
863 }
864 else
865 {
866 boot_verify_send_event(BOOT_INIT);
867 }
868
869 /* Initialize keystore */
870 boot_state = boot_verify_keystore_init();
871 if(boot_state == YELLOW)
872 {
873 boot_verify_print_state();
874 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
875 }
876}
877
Shashank Mittal23b8f422010-04-16 19:27:21 -0700878int boot_linux_from_mmc(void)
879{
880 struct boot_img_hdr *hdr = (void*) buf;
881 struct boot_img_hdr *uhdr;
882 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700883 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700884 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700885 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700886
Shashank Mittalcd98d472011-08-02 14:29:24 -0700887 unsigned char *image_addr = 0;
888 unsigned kernel_actual;
889 unsigned ramdisk_actual;
890 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700891 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700892
Matthew Qin271927e2015-03-31 22:07:07 -0400893 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +0800894 unsigned int out_len = 0;
895 unsigned int out_avai_len = 0;
896 unsigned char *out_addr = NULL;
897 uint32_t dtb_offset = 0;
898 unsigned char *kernel_start_addr = NULL;
899 unsigned int kernel_size = 0;
900 int rc;
901
Neeti Desai465491e2012-07-31 12:53:35 -0700902#if DEVICE_TREE
903 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700904 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700905 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800906 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700907 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -0400908 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -0700909#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +0800910 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800911
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530912 if (check_format_bit())
913 boot_into_recovery = 1;
914
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700915 if (!boot_into_recovery) {
916 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
917 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
918 if (rcode <= 0) {
919 boot_into_ffbm = false;
920 if (rcode < 0)
921 dprintf(CRITICAL,"failed to get ffbm cookie");
922 } else
923 boot_into_ffbm = true;
924 } else
925 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700926 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
927 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
928 dprintf(INFO, "Unified boot method!\n");
929 hdr = uhdr;
930 goto unified_boot;
931 }
Greg Griscod6250552011-06-29 14:40:23 -0700932 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700933 index = partition_get_index("boot");
934 ptn = partition_get_offset(index);
935 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700936 dprintf(CRITICAL, "ERROR: No boot partition found\n");
937 return -1;
938 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700939 }
940 else {
941 index = partition_get_index("recovery");
942 ptn = partition_get_offset(index);
943 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700944 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
945 return -1;
946 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700947 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -0800948 /* Set Lun for boot & recovery partitions */
949 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -0700950
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530951 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700952 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
953 return -1;
954 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700955
956 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700957 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700958 return -1;
959 }
960
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700961 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530962
963 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
964 dprintf(CRITICAL, "ERROR: Invalid page size\n");
965 return -1;
966 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700967 page_size = hdr->page_size;
968 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700969 }
970
Matthew Qin49e51fa2015-02-09 10:40:45 +0800971 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
972 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800973
Matthew Qin49e51fa2015-02-09 10:40:45 +0800974 image_addr = (unsigned char *)target_get_scratch_address();
975
976#if DEVICE_TREE
977 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
978 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
979#else
980 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
981#endif
982
983#if VERIFIED_BOOT
984 boot_verifier_init();
985#endif
986
987 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
988 {
989 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
990 return -1;
991 }
992
Matthew Qinbb7923d2015-02-09 10:56:09 +0800993 /*
994 * Update loading flow of bootimage to support compressed/uncompressed
995 * bootimage on both 64bit and 32bit platform.
996 * 1. Load bootimage from emmc partition onto DDR.
997 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
998 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
999 * platform accordingly.
1000 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1001 */
1002
Matthew Qin49e51fa2015-02-09 10:40:45 +08001003 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
1004 bs_set_timestamp(BS_KERNEL_LOAD_START);
1005
Matthew Qinbb7923d2015-02-09 10:56:09 +08001006 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001007 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1008 {
1009 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1010 return -1;
1011 }
1012
1013 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
1014 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1015
1016 /* Authenticate Kernel */
1017 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1018 (int) target_use_signed_kernel(),
1019 device.is_unlocked,
1020 device.is_tampered);
1021
1022 if(target_use_signed_kernel() && (!device.is_unlocked))
1023 {
1024 offset = imagesize_actual;
1025 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1026 {
1027 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1028 return -1;
1029 }
1030
1031 /* Read signature */
1032 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1033 {
1034 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1035 return -1;
1036 }
1037
1038 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
1039 } else {
1040 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1041 #ifdef TZ_SAVE_KERNEL_HASH
1042 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1043 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001044
1045#ifdef MDTP_SUPPORT
1046 {
1047 /* Verify MDTP lock.
1048 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1049 * since verification was skipped in aboot. The signature is not part of the loaded image.
1050 */
1051 mdtp_ext_partition_verification_t ext_partition;
1052 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1053 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1054 ext_partition.page_size = page_size;
1055 ext_partition.image_addr = (uint32)image_addr;
1056 ext_partition.image_size = imagesize_actual;
1057 ext_partition.sig_avail = FALSE;
1058 mdtp_fwlock_verify_lock(&ext_partition);
1059 }
1060#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001061 }
1062
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001063 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001064 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1065 * If not, continue booting.
1066 */
1067 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1068 {
1069 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1070 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001071 dprintf(SPEW, "decompress image start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001072 rc = decompress((unsigned char *)(image_addr + page_size),
1073 hdr->kernel_size, out_addr, out_avai_len,
1074 &dtb_offset, &out_len);
1075 if (rc)
1076 {
Matthew Qin271927e2015-03-31 22:07:07 -04001077 dprintf(CRITICAL, "decompress image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001078 ASSERT(0);
1079 }
1080
Matthew Qin271927e2015-03-31 22:07:07 -04001081 dprintf(SPEW, "decompressed image finished.\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001082 kptr = (struct kernel64_hdr *)out_addr;
1083 kernel_start_addr = out_addr;
1084 kernel_size = out_len;
1085 } else {
1086 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1087 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1088 kernel_size = hdr->kernel_size;
1089 }
1090
1091 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001092 * Update the kernel/ramdisk/tags address if the boot image header
1093 * has default values, these default values come from mkbootimg when
1094 * the boot image is flashed using fastboot flash:raw
1095 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001096 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001097
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001098 /* Get virtual addresses since the hdr saves physical addresses. */
1099 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1100 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1101 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001102
Matthew Qinbb7923d2015-02-09 10:56:09 +08001103 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001104 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001105 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001106 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1107 {
1108 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1109 return -1;
1110 }
1111
1112#ifndef DEVICE_TREE
1113 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1114 {
1115 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1116 return -1;
1117 }
1118#endif
1119
Matthew Qin49e51fa2015-02-09 10:40:45 +08001120 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001121 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001122 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001123
Matthew Qin49e51fa2015-02-09 10:40:45 +08001124 #if DEVICE_TREE
1125 if(hdr->dt_size) {
1126 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1127 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001128
Matthew Qin49e51fa2015-02-09 10:40:45 +08001129 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1130 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1131 return -1;
1132 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001133
Matthew Qin49e51fa2015-02-09 10:40:45 +08001134 /* Find index of device tree within device tree table */
1135 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1136 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1137 return -1;
1138 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001139
Matthew Qin271927e2015-03-31 22:07:07 -04001140 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1141 {
1142 unsigned int compressed_size = 0;
1143 out_addr += out_len;
1144 out_avai_len -= out_len;
1145 dprintf(SPEW, "decompress dtb start\n");
1146 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1147 dt_entry.size, out_addr, out_avai_len,
1148 &compressed_size, &dtb_size);
1149 if (rc)
1150 {
1151 dprintf(CRITICAL, "decompress dtb failed!!!\n");
1152 ASSERT(0);
1153 }
1154
1155 dprintf(SPEW, "decompressed dtb finished.\n");
1156 best_match_dt_addr = out_addr;
1157 } else {
1158 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1159 dtb_size = dt_entry.size;
1160 }
1161
Matthew Qin49e51fa2015-02-09 10:40:45 +08001162 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001163 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001164 {
1165 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1166 return -1;
1167 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001168
Matthew Qin271927e2015-03-31 22:07:07 -04001169 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001170 } else {
1171 /* Validate the tags_addr */
1172 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001173 {
1174 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1175 return -1;
1176 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001177 /*
1178 * If appended dev tree is found, update the atags with
1179 * memory address to the DTB appended location on RAM.
1180 * Else update with the atags address in the kernel header
1181 */
1182 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001183 dtb = dev_tree_appended((void*)(image_addr + page_size),
1184 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001185 (void *)hdr->tags_addr);
1186 if (!dtb) {
1187 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001188 return -1;
1189 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001190 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001191 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001192
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001193 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1194 target_load_ssd_keystore();
1195
Shashank Mittal23b8f422010-04-16 19:27:21 -07001196unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001197
Dima Zavin77e41f32013-03-06 16:10:43 -08001198 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001199 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001200 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1201
1202 return 0;
1203}
1204
Dima Zavin214cc642009-01-26 11:16:21 -08001205int boot_linux_from_flash(void)
1206{
1207 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001208 struct ptentry *ptn;
1209 struct ptable *ptable;
1210 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001211
Shashank Mittalcd98d472011-08-02 14:29:24 -07001212 unsigned char *image_addr = 0;
1213 unsigned kernel_actual;
1214 unsigned ramdisk_actual;
1215 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001216 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001217
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001218#if DEVICE_TREE
1219 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001220 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001221 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001222 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001223#endif
1224
David Ng183a7422009-12-07 14:55:21 -08001225 if (target_is_emmc_boot()) {
1226 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1227 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1228 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1229 return -1;
1230 }
1231 goto continue_boot;
1232 }
1233
Dima Zavin214cc642009-01-26 11:16:21 -08001234 ptable = flash_get_ptable();
1235 if (ptable == NULL) {
1236 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1237 return -1;
1238 }
1239
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001240 if(!boot_into_recovery)
1241 {
1242 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001243
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001244 if (ptn == NULL) {
1245 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1246 return -1;
1247 }
1248 }
1249 else
1250 {
1251 ptn = ptable_find(ptable, "recovery");
1252 if (ptn == NULL) {
1253 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1254 return -1;
1255 }
Dima Zavin214cc642009-01-26 11:16:21 -08001256 }
1257
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001258 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001259 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1260 return -1;
1261 }
Dima Zavin214cc642009-01-26 11:16:21 -08001262
1263 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001264 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001265 return -1;
1266 }
1267
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001268 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001269 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 -08001270 return -1;
1271 }
1272
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001273 /*
1274 * Update the kernel/ramdisk/tags address if the boot image header
1275 * has default values, these default values come from mkbootimg when
1276 * the boot image is flashed using fastboot flash:raw
1277 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001278 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001279
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001280 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001281 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1282 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1283 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1284
1285 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1286 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1287
1288 /* Check if the addresses in the header are valid. */
1289 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1290 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1291 {
1292 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1293 return -1;
1294 }
1295
1296#ifndef DEVICE_TREE
1297 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1298 {
1299 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1300 return -1;
1301 }
1302#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001303
Shashank Mittalcd98d472011-08-02 14:29:24 -07001304 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001305 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001306 {
1307 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001308 offset = 0;
1309
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001310#if DEVICE_TREE
1311 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1312 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001313
1314 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1315 {
1316 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1317 return -1;
1318 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001319#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001320 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001321#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001322
Amol Jadib6be5c12012-11-14 13:39:51 -08001323 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001324 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001325
Shashank Mittalcd98d472011-08-02 14:29:24 -07001326 /* Read image without signature */
1327 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1328 {
1329 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1330 return -1;
1331 }
Dima Zavin214cc642009-01-26 11:16:21 -08001332
Amol Jadib6be5c12012-11-14 13:39:51 -08001333 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001334 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001335
Shashank Mittalcd98d472011-08-02 14:29:24 -07001336 offset = imagesize_actual;
1337 /* Read signature */
1338 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1339 {
1340 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001341 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001342 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001343
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301344 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001345
1346 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001347 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1348 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001349#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001350 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001351 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001352 {
1353 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1354 return -1;
1355 }
1356
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001357 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1358#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001359
1360 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001361 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001362 {
1363 write_device_info_flash(&device);
1364 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301365#if USE_PCOM_SECBOOT
1366 set_tamper_flag(device.is_tampered);
1367#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001368 }
1369 else
1370 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001371 offset = page_size;
1372
Amol Jadib6be5c12012-11-14 13:39:51 -08001373 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1374 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1375 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1376
1377 dprintf(INFO, "Loading boot image (%d): start\n",
1378 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001379 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001380
1381 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001382 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1383 return -1;
1384 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001385 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001386
Amol Jadib6be5c12012-11-14 13:39:51 -08001387 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001388 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1389 return -1;
1390 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001391 offset += ramdisk_actual;
1392
1393 dprintf(INFO, "Loading boot image (%d): done\n",
1394 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001395 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001396
1397 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001398 offset += second_actual;
1399 /* Second image loading not implemented. */
1400 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001401 }
1402
1403#if DEVICE_TREE
1404 if(hdr->dt_size != 0) {
1405
1406 /* Read the device tree table into buffer */
1407 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1408 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1409 return -1;
1410 }
1411
1412 table = (struct dt_table*) dt_buf;
1413
Deepa Dinamani19648b42013-09-05 17:05:55 -07001414 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001415 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1416 return -1;
1417 }
1418
Deepa Dinamani19648b42013-09-05 17:05:55 -07001419 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1420 if (!table)
1421 return -1;
1422
1423 /* Read the entire device tree table into buffer */
1424 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1425 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1426 return -1;
1427 }
1428
1429
Joel Kingaa335dc2013-06-03 16:11:08 -07001430 /* Find index of device tree within device tree table */
1431 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001432 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1433 return -1;
1434 }
1435
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001436 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001437 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001438 {
1439 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1440 return -1;
1441 }
1442
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001443 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001444 if(flash_read(ptn, offset + dt_entry.offset,
1445 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001446 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1447 return -1;
1448 }
1449 }
1450#endif
1451
Shashank Mittalcd98d472011-08-02 14:29:24 -07001452 }
David Ng183a7422009-12-07 14:55:21 -08001453continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001454
Dima Zavin214cc642009-01-26 11:16:21 -08001455 /* TODO: create/pass atags to kernel */
1456
Ajay Dudanie28a6072011-07-01 13:59:46 -07001457 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001458 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001459 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1460
1461 return 0;
1462}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001463
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001464BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001465void write_device_info_mmc(device_info *dev)
1466{
Shashank Mittal162244e2011-08-08 19:01:25 -07001467 unsigned long long ptn = 0;
1468 unsigned long long size;
1469 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001470 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001471 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001472 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001473
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001474 if (devinfo_present)
1475 index = partition_get_index("devinfo");
1476 else
1477 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001478
Shashank Mittal162244e2011-08-08 19:01:25 -07001479 ptn = partition_get_offset(index);
1480 if(ptn == 0)
1481 {
1482 return;
1483 }
1484
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001485 lun = partition_get_lun(index);
1486 mmc_set_lun(lun);
1487
Shashank Mittal162244e2011-08-08 19:01:25 -07001488 size = partition_get_size(index);
1489
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001490 blocksize = mmc_get_device_blocksize();
1491
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001492 if (devinfo_present)
1493 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1494 else
1495 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1496 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001497 {
1498 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1499 return;
1500 }
1501}
1502
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001503void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001504{
Shashank Mittal162244e2011-08-08 19:01:25 -07001505 unsigned long long ptn = 0;
1506 unsigned long long size;
1507 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001508 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001509 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001510
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001511 if ((index = partition_get_index("devinfo")) < 0)
1512 {
1513 devinfo_present = false;
1514 index = partition_get_index("aboot");
1515 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001516
Shashank Mittal162244e2011-08-08 19:01:25 -07001517 ptn = partition_get_offset(index);
1518 if(ptn == 0)
1519 {
1520 return;
1521 }
1522
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001523 mmc_set_lun(partition_get_lun(index));
1524
Shashank Mittal162244e2011-08-08 19:01:25 -07001525 size = partition_get_size(index);
1526
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001527 blocksize = mmc_get_device_blocksize();
1528
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001529 if (devinfo_present)
1530 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1531 else
1532 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1533 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001534 {
1535 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1536 return;
1537 }
1538
Shashank Mittal162244e2011-08-08 19:01:25 -07001539}
1540
1541void write_device_info_flash(device_info *dev)
1542{
1543 struct device_info *info = (void *) info_buf;
1544 struct ptentry *ptn;
1545 struct ptable *ptable;
1546
1547 ptable = flash_get_ptable();
1548 if (ptable == NULL)
1549 {
1550 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1551 return;
1552 }
1553
1554 ptn = ptable_find(ptable, "devinfo");
1555 if (ptn == NULL)
1556 {
1557 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1558 return;
1559 }
1560
1561 memcpy(info, dev, sizeof(device_info));
1562
1563 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1564 {
1565 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1566 return;
1567 }
1568}
1569
1570void read_device_info_flash(device_info *dev)
1571{
1572 struct device_info *info = (void*) info_buf;
1573 struct ptentry *ptn;
1574 struct ptable *ptable;
1575
1576 ptable = flash_get_ptable();
1577 if (ptable == NULL)
1578 {
1579 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1580 return;
1581 }
1582
1583 ptn = ptable_find(ptable, "devinfo");
1584 if (ptn == NULL)
1585 {
1586 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1587 return;
1588 }
1589
1590 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1591 {
1592 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1593 return;
1594 }
1595
1596 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1597 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001598 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1599 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001600 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001601 write_device_info_flash(info);
1602 }
1603 memcpy(dev, info, sizeof(device_info));
1604}
1605
1606void write_device_info(device_info *dev)
1607{
1608 if(target_is_emmc_boot())
1609 {
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001610 struct device_info *info = (void*) info_buf;
1611 memcpy(info, dev, sizeof(struct device_info));
1612
1613#if USE_RPMB_FOR_DEVINFO
1614 if (is_secure_boot_enable())
1615 write_device_info_rpmb((void*) info, mmc_get_device_blocksize());
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001616 else
1617 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001618#else
1619 write_device_info_mmc(info);
1620#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001621 }
1622 else
1623 {
1624 write_device_info_flash(dev);
1625 }
1626}
1627
1628void read_device_info(device_info *dev)
1629{
1630 if(target_is_emmc_boot())
1631 {
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001632 struct device_info *info = (void*) info_buf;
1633
1634#if USE_RPMB_FOR_DEVINFO
1635 if (is_secure_boot_enable())
1636 read_device_info_rpmb((void*) info, mmc_get_device_blocksize());
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001637 else
1638 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001639#else
1640 read_device_info_mmc(info);
1641#endif
1642
1643 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1644 {
1645 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1646 if (is_secure_boot_enable())
1647 info->is_unlocked = 0;
1648 else
1649 info->is_unlocked = 1;
1650 info->is_verified = 0;
1651 info->is_tampered = 0;
Unnati Gandhia49daa12015-03-23 18:08:38 +05301652#if USER_BUILD_VARIANT
1653 info->charger_screen_enabled = 1;
1654#else
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001655 info->charger_screen_enabled = 0;
Unnati Gandhia49daa12015-03-23 18:08:38 +05301656#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001657 write_device_info(info);
1658 }
1659 memcpy(dev, info, sizeof(device_info));
Shashank Mittal162244e2011-08-08 19:01:25 -07001660 }
1661 else
1662 {
1663 read_device_info_flash(dev);
1664 }
1665}
1666
1667void reset_device_info()
1668{
1669 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001670 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001671 write_device_info(&device);
1672}
1673
1674void set_device_root()
1675{
1676 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001677 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001678 write_device_info(&device);
1679}
1680
Amol Jadicb524072012-08-09 16:40:18 -07001681#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001682int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
Amol Jadicb524072012-08-09 16:40:18 -07001683{
1684 uint32 dt_image_offset = 0;
1685 uint32_t n;
1686 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001687 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001688 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001689 unsigned int compressed_size = 0;
1690 unsigned int dtb_size = 0;
1691 unsigned int out_avai_len = 0;
1692 unsigned char *out_addr = NULL;
1693 unsigned char *best_match_dt_addr = NULL;
1694 int rc;
Amol Jadicb524072012-08-09 16:40:18 -07001695
1696 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1697
Amol Jadicb524072012-08-09 16:40:18 -07001698 if(hdr->dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001699 /* add kernel offset */
1700 dt_image_offset += page_size;
1701 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1702 dt_image_offset += n;
1703
1704 /* add ramdisk offset */
1705 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1706 dt_image_offset += n;
1707
1708 /* add second offset */
1709 if(hdr->second_size != 0) {
1710 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1711 dt_image_offset += n;
1712 }
1713
1714 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001715 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001716
Deepa Dinamani19648b42013-09-05 17:05:55 -07001717 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001718 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1719 return -1;
1720 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001721 /* Find index of device tree within device tree table */
1722 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001723 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1724 return -1;
1725 }
1726
Matthew Qin271927e2015-03-31 22:07:07 -04001727 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
1728 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
1729 {
1730 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
1731 out_avai_len = target_get_max_flash_size() - scratch_offset;
1732 dprintf(SPEW, "decompress dtb start\n");
1733 rc = decompress(best_match_dt_addr,
1734 dt_entry.size, out_addr, out_avai_len,
1735 &compressed_size, &dtb_size);
1736 if (rc)
1737 {
1738 dprintf(CRITICAL, "decompress dtb failed!!!\n");
1739 ASSERT(0);
1740 }
1741
1742 dprintf(SPEW, "decompressed dtb finished.\n");
1743 best_match_dt_addr = out_addr;
1744 } else {
1745 dtb_size = dt_entry.size;
1746 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001747 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001748 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001749 {
1750 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1751 return -1;
1752 }
1753
Amol Jadicb524072012-08-09 16:40:18 -07001754 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001755 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001756 } else
1757 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001758
1759 /* Everything looks fine. Return success. */
1760 return 0;
1761}
1762#endif
1763
Brian Swetland9c4c0752009-01-25 16:23:50 -08001764void cmd_boot(const char *arg, void *data, unsigned sz)
1765{
1766 unsigned kernel_actual;
1767 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001768 uint32_t image_actual;
1769 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001770 uint32_t sig_actual = SIGNATURE_SIZE;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001771 struct boot_img_hdr *hdr = NULL;
1772 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001773 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001774 int ret = 0;
1775 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001776 unsigned int out_len = 0;
1777 unsigned int out_avai_len = 0;
1778 unsigned char *out_addr = NULL;
1779 uint32_t dtb_offset = 0;
1780 unsigned char *kernel_start_addr = NULL;
1781 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04001782 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001783
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001784#if VERIFIED_BOOT
1785 if(!device.is_unlocked)
1786 {
1787 fastboot_fail("unlock device to use this command");
1788 return;
1789 }
1790#endif
1791
Brian Swetland9c4c0752009-01-25 16:23:50 -08001792 if (sz < sizeof(hdr)) {
1793 fastboot_fail("invalid bootimage header");
1794 return;
1795 }
1796
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001797 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001798
1799 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001800 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001801
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001802 if(target_is_emmc_boot() && hdr->page_size) {
1803 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001804 page_mask = page_size - 1;
1805 }
1806
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001807 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1808 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001809#if DEVICE_TREE
1810 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1811#endif
1812
1813 image_actual = ADD_OF(page_size, kernel_actual);
1814 image_actual = ADD_OF(image_actual, ramdisk_actual);
1815 image_actual = ADD_OF(image_actual, dt_actual);
1816
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001817 if (target_use_signed_kernel() && (!device.is_unlocked))
1818 image_actual = ADD_OF(image_actual, sig_actual);
1819
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001820 /* sz should have atleast raw boot image */
1821 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001822 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001823 return;
1824 }
1825
1826 /* Verify the boot image
1827 * device & page_size are initialized in aboot_init
1828 */
1829 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001830 /* Pass size excluding signature size, otherwise we would try to
1831 * access signature beyond its length
1832 */
1833 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001834
Amit Blay4aa292f2015-04-28 21:55:59 +03001835#ifdef MDTP_SUPPORT
1836 else
1837 {
1838 /* Verify MDTP lock before continue with boot process.
1839 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1840 * since verification was skipped in aboot. The signarue is already part of the loaded image.
1841 */
1842 mdtp_ext_partition_verification_t ext_partition;
1843 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1844 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1845 ext_partition.page_size = page_size;
1846 ext_partition.image_addr = (uint32_t)data;
1847 ext_partition.image_size = image_actual - sig_actual;
1848 ext_partition.sig_avail = TRUE;
1849 mdtp_fwlock_verify_lock(&ext_partition);
1850 }
1851#endif /* MDTP_SUPPORT */
1852
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001853 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001854 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1855 * If not, continue booting.
1856 */
1857 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
1858 {
1859 out_addr = (unsigned char *)target_get_scratch_address();
1860 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
1861 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001862 dprintf(SPEW, "decompress image start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001863 ret = decompress((unsigned char *)(ptr + page_size),
1864 hdr->kernel_size, out_addr, out_avai_len,
1865 &dtb_offset, &out_len);
1866 if (ret)
1867 {
Matthew Qin271927e2015-03-31 22:07:07 -04001868 dprintf(CRITICAL, "decompress image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001869 ASSERT(0);
1870 }
1871
Matthew Qin271927e2015-03-31 22:07:07 -04001872 dprintf(SPEW, "decompressed image finished.\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001873 kptr = (struct kernel64_hdr *)out_addr;
1874 kernel_start_addr = out_addr;
1875 kernel_size = out_len;
1876 } else {
1877 kptr = (struct kernel64_hdr*)((char *)data + page_size);
1878 kernel_start_addr = (unsigned char *)((char *)data + page_size);
1879 kernel_size = hdr->kernel_size;
1880 }
1881
1882 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001883 * Update the kernel/ramdisk/tags address if the boot image header
1884 * has default values, these default values come from mkbootimg when
1885 * the boot image is flashed using fastboot flash:raw
1886 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001887 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001888
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001889 /* Get virtual addresses since the hdr saves physical addresses. */
1890 hdr->kernel_addr = VA(hdr->kernel_addr);
1891 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1892 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001893
Matthew Qinbb7923d2015-02-09 10:56:09 +08001894 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001895 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001896 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001897 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1898 {
1899 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001900 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001901 }
1902
Amol Jadicb524072012-08-09 16:40:18 -07001903#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001904 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07001905 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04001906 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001907
1908 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001909#else
1910 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1911 {
1912 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001913 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001914 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001915#endif
1916
1917 /* Load ramdisk & kernel */
1918 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08001919 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001920
1921#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08001922 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1923 {
1924 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1925 return;
1926 }
1927
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001928 /*
1929 * If dtb is not found look for appended DTB in the kernel.
1930 * If appended dev tree is found, update the atags with
1931 * memory address to the DTB appended location on RAM.
1932 * Else update with the atags address in the kernel header
1933 */
1934 if (!dtb_copied) {
1935 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001936 dtb = dev_tree_appended((void*)(ptr + page_size),
1937 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07001938 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001939 if (!dtb) {
1940 fastboot_fail("dtb not found");
1941 return;
1942 }
Amol Jadicb524072012-08-09 16:40:18 -07001943 }
1944#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001945
1946 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001947 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001948
Dima Zavin77e41f32013-03-06 16:10:43 -08001949 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001950 (const char*) hdr->cmdline, board_machtype(),
1951 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001952}
1953
Sridhar Parasurame94e8152014-10-24 14:06:03 -07001954void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08001955{
1956 struct ptentry *ptn;
1957 struct ptable *ptable;
1958
1959 ptable = flash_get_ptable();
1960 if (ptable == NULL) {
1961 fastboot_fail("partition table doesn't exist");
1962 return;
1963 }
1964
1965 ptn = ptable_find(ptable, arg);
1966 if (ptn == NULL) {
1967 fastboot_fail("unknown partition name");
1968 return;
1969 }
1970
1971 if (flash_erase(ptn)) {
1972 fastboot_fail("failed to erase partition");
1973 return;
1974 }
1975 fastboot_okay("");
1976}
1977
Bikas Gurungd48bd242010-09-04 19:54:32 -07001978
1979void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1980{
1981 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001982 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001983 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001984 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001985
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001986#if VERIFIED_BOOT
1987 if(!strcmp(arg, KEYSTORE_PTN_NAME))
1988 {
1989 if(!device.is_unlocked)
1990 {
1991 fastboot_fail("unlock device to erase keystore");
1992 return;
1993 }
1994 }
1995#endif
1996
Kinson Chikf1a43512011-07-14 11:28:39 -07001997 index = partition_get_index(arg);
1998 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001999 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002000
Kinson Chikf1a43512011-07-14 11:28:39 -07002001 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002002 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002003 return;
2004 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002005
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002006 lun = partition_get_lun(index);
2007 mmc_set_lun(lun);
2008
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002009 if (platform_boot_dev_isemmc())
2010 {
2011 if (mmc_erase_card(ptn, size)) {
2012 fastboot_fail("failed to erase partition\n");
2013 return;
2014 }
2015 } else {
2016 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2017 size = partition_get_size(index);
2018 if (size > DEFAULT_ERASE_SIZE)
2019 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002020
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002021 /* Simple inefficient version of erase. Just writing
2022 0 in first several blocks */
2023 if (mmc_write(ptn , size, (unsigned int *)out)) {
2024 fastboot_fail("failed to erase partition");
2025 return;
2026 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002027 }
2028 fastboot_okay("");
2029}
2030
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002031void cmd_erase(const char *arg, void *data, unsigned sz)
2032{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002033#if VERIFIED_BOOT
2034 if(!device.is_unlocked && !device.is_verified)
2035 {
2036 fastboot_fail("device is locked. Cannot erase");
2037 return;
2038 }
2039 if(!device.is_unlocked && device.is_verified)
2040 {
2041 if(!boot_verify_flash_allowed(arg))
2042 {
2043 fastboot_fail("cannot flash this partition in verified state");
2044 return;
2045 }
2046 }
2047#endif
2048
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002049 if(target_is_emmc_boot())
2050 cmd_erase_mmc(arg, data, sz);
2051 else
2052 cmd_erase_nand(arg, data, sz);
2053}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002054
Ajay Dudani5c761132011-04-07 20:19:04 -07002055void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002056{
2057 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002058 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002059 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002060 char *token = NULL;
2061 char *pname = NULL;
2062 uint8_t lun = 0;
2063 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002064
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302065 token = strtok((char *)arg, ":");
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002066 pname = token;
2067 token = strtok(NULL, ":");
2068 if(token)
2069 {
2070 lun = atoi(token);
2071 mmc_set_lun(lun);
2072 lun_set = true;
2073 }
2074
Mao Jinlong226f33a2014-07-04 17:24:10 +08002075 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002076 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002077 if (!strcmp(pname, "partition"))
2078 {
2079 dprintf(INFO, "Attempt to write partition image.\n");
2080 if (write_partition(sz, (unsigned char *) data)) {
2081 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002082 return;
2083 }
2084 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002085 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002086 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002087#if VERIFIED_BOOT
2088 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2089 {
2090 if(!device.is_unlocked)
2091 {
2092 fastboot_fail("unlock device to flash keystore");
2093 return;
2094 }
2095 if(!boot_verify_validate_keystore((unsigned char *)data))
2096 {
2097 fastboot_fail("image is not a keystore file");
2098 return;
2099 }
2100 }
2101#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002102 index = partition_get_index(pname);
2103 ptn = partition_get_offset(index);
2104 if(ptn == 0) {
2105 fastboot_fail("partition table doesn't exist");
2106 return;
2107 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002108
Mao Jinlong226f33a2014-07-04 17:24:10 +08002109 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2110 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2111 fastboot_fail("image is not a boot image");
2112 return;
2113 }
2114 }
2115
2116 if(!lun_set)
2117 {
2118 lun = partition_get_lun(index);
2119 mmc_set_lun(lun);
2120 }
2121
2122 size = partition_get_size(index);
2123 if (ROUND_TO_PAGE(sz,511) > size) {
2124 fastboot_fail("size too large");
2125 return;
2126 }
2127 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2128 fastboot_fail("flash write failure");
2129 return;
2130 }
Greg Grisco6e754772011-06-23 12:19:39 -07002131 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002132 }
2133 fastboot_okay("");
2134 return;
2135}
2136
Ajay Dudanide984792015-03-02 09:57:41 -08002137void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2138{
2139 int i, images;
2140 meta_header_t *meta_header;
2141 img_header_entry_t *img_header_entry;
2142
2143 meta_header = (meta_header_t*) data;
2144 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2145
2146 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2147
2148 for (i=0; i<images; i++) {
2149
2150 if((img_header_entry[i].ptn_name == NULL) ||
2151 (img_header_entry[i].start_offset == 0) ||
2152 (img_header_entry[i].size == 0))
2153 break;
2154
2155 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2156 (void *) data + img_header_entry[i].start_offset,
2157 img_header_entry[i].size);
2158 }
2159
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002160 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2161 {
2162 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2163 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2164 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2165 }
2166 else
2167 {
2168 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2169 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2170 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2171 }
2172
2173 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002174 fastboot_okay("");
2175 return;
2176}
2177
Ajay Dudani5c761132011-04-07 20:19:04 -07002178void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2179{
2180 unsigned int chunk;
2181 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002182 uint32_t *fill_buf = NULL;
2183 uint32_t fill_val;
2184 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002185 sparse_header_t *sparse_header;
2186 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002187 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002188 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302189 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002190 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302191 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002192 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002193
Kinson Chikf1a43512011-07-14 11:28:39 -07002194 index = partition_get_index(arg);
2195 ptn = partition_get_offset(index);
2196 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002197 fastboot_fail("partition table doesn't exist");
2198 return;
2199 }
2200
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302201 size = partition_get_size(index);
2202 if (ROUND_TO_PAGE(sz,511) > size) {
2203 fastboot_fail("size too large");
2204 return;
2205 }
2206
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002207 lun = partition_get_lun(index);
2208 mmc_set_lun(lun);
2209
Ajay Dudani5c761132011-04-07 20:19:04 -07002210 /* Read and skip over sparse image header */
2211 sparse_header = (sparse_header_t *) data;
vijay kumar1321f342015-03-27 12:13:42 +05302212 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002213 fastboot_fail("size too large");
2214 return;
2215 }
2216
Ajay Dudani5c761132011-04-07 20:19:04 -07002217 data += sparse_header->file_hdr_sz;
2218 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
2219 {
2220 /* Skip the remaining bytes in a header that is longer than
2221 * we expected.
2222 */
2223 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
2224 }
2225
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002226 dprintf (SPEW, "=== Sparse Image Header ===\n");
2227 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2228 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2229 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2230 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2231 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2232 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2233 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2234 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002235
2236 /* Start processing chunks */
2237 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2238 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302239 /* Make sure the total image size does not exceed the partition size */
2240 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2241 fastboot_fail("size too large");
2242 return;
2243 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002244 /* Read and skip over chunk header */
2245 chunk_header = (chunk_header_t *) data;
2246 data += sizeof(chunk_header_t);
2247
2248 dprintf (SPEW, "=== Chunk Header ===\n");
2249 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2250 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2251 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2252
2253 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
2254 {
2255 /* Skip the remaining bytes in a header that is longer than
2256 * we expected.
2257 */
2258 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
2259 }
2260
2261 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302262
2263 /* Make sure multiplication does not overflow uint32 size */
2264 if (sparse_header->blk_sz && (chunk_header->chunk_sz != chunk_data_sz / sparse_header->blk_sz))
2265 {
2266 fastboot_fail("Bogus size sparse and chunk header");
2267 return;
2268 }
2269
2270 /* Make sure that the chunk size calculated from sparse image does not
2271 * exceed partition size
2272 */
2273 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2274 {
2275 fastboot_fail("Chunk data size exceeds partition size");
2276 return;
2277 }
2278
Ajay Dudani5c761132011-04-07 20:19:04 -07002279 switch (chunk_header->chunk_type)
2280 {
2281 case CHUNK_TYPE_RAW:
2282 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2283 chunk_data_sz))
2284 {
2285 fastboot_fail("Bogus chunk size for chunk type Raw");
2286 return;
2287 }
2288
Ajay Dudaniab18f022011-05-12 14:39:22 -07002289 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2290 chunk_data_sz,
2291 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002292 {
2293 fastboot_fail("flash write failure");
2294 return;
2295 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302296 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2297 fastboot_fail("Bogus size for RAW chunk type");
2298 return;
2299 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002300 total_blocks += chunk_header->chunk_sz;
2301 data += chunk_data_sz;
2302 break;
2303
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002304 case CHUNK_TYPE_FILL:
2305 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2306 sizeof(uint32_t)))
2307 {
2308 fastboot_fail("Bogus chunk size for chunk type FILL");
2309 return;
2310 }
2311
2312 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2313 if (!fill_buf)
2314 {
2315 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2316 return;
2317 }
2318
2319 fill_val = *(uint32_t *)data;
2320 data = (char *) data + sizeof(uint32_t);
2321 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
2322
2323 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2324 {
2325 fill_buf[i] = fill_val;
2326 }
2327
2328 for (i = 0; i < chunk_blk_cnt; i++)
2329 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302330 /* Make sure that the data written to partition does not exceed partition size */
2331 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2332 {
2333 fastboot_fail("Chunk data size for fill type exceeds partition size");
2334 return;
2335 }
2336
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002337 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2338 sparse_header->blk_sz,
2339 fill_buf))
2340 {
2341 fastboot_fail("flash write failure");
2342 free(fill_buf);
2343 return;
2344 }
2345
2346 total_blocks++;
2347 }
2348
2349 free(fill_buf);
2350 break;
2351
Ajay Dudani5c761132011-04-07 20:19:04 -07002352 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302353 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2354 fastboot_fail("bogus size for chunk DONT CARE type");
2355 return;
2356 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002357 total_blocks += chunk_header->chunk_sz;
2358 break;
2359
Ajay Dudani5c761132011-04-07 20:19:04 -07002360 case CHUNK_TYPE_CRC:
2361 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2362 {
2363 fastboot_fail("Bogus chunk size for chunk type Dont Care");
2364 return;
2365 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302366 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2367 fastboot_fail("bogus size for chunk CRC type");
2368 return;
2369 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002370 total_blocks += chunk_header->chunk_sz;
2371 data += chunk_data_sz;
2372 break;
2373
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002374 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002375 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002376 fastboot_fail("Unknown chunk type");
2377 return;
2378 }
2379 }
2380
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002381 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2382 total_blocks, sparse_header->total_blks);
2383
2384 if(total_blocks != sparse_header->total_blks)
2385 {
2386 fastboot_fail("sparse image write failure");
2387 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002388
2389 fastboot_okay("");
2390 return;
2391}
2392
2393void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2394{
2395 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002396 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002397
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002398#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002399 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2400 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002401 int ret=0;
2402 uint32 major_version=0;
2403 uint32 minor_version=0;
2404
2405 ret = scm_svc_version(&major_version,&minor_version);
2406 if(!ret)
2407 {
2408 if(major_version >= 2)
2409 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002410 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002411 {
2412 ret = encrypt_scm((uint32 **) &data, &sz);
2413 if (ret != 0) {
2414 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2415 return;
2416 }
2417
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002418 /* Protect only for SSD */
2419 if (!strcmp(arg, "ssd")) {
2420 ret = scm_protect_keystore((uint32 *) data, sz);
2421 if (ret != 0) {
2422 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2423 return;
2424 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002425 }
2426 }
2427 else
2428 {
2429 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2430 if(ret != 0)
2431 {
2432 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2433 return;
2434 }
2435 }
2436 }
2437 else
2438 {
2439 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2440 magic_number[1] == DECRYPT_MAGIC_1)
2441 {
2442 ret = decrypt_scm((uint32 **) &data, &sz);
2443 if (ret != 0) {
2444 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2445 return;
2446 }
2447 }
2448 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2449 magic_number[1] == ENCRYPT_MAGIC_1)
2450 {
2451 ret = encrypt_scm((uint32 **) &data, &sz);
2452 if (ret != 0) {
2453 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2454 return;
2455 }
2456 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002457 }
2458 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002459 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002460 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002461 dprintf(CRITICAL,"INVALID SVC Version\n");
2462 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002463 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002464#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002465
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002466#if VERIFIED_BOOT
2467 if(!device.is_unlocked && !device.is_verified)
2468 {
2469 fastboot_fail("device is locked. Cannot flash images");
2470 return;
2471 }
2472 if(!device.is_unlocked && device.is_verified)
2473 {
2474 if(!boot_verify_flash_allowed(arg))
2475 {
2476 fastboot_fail("cannot flash this partition in verified state");
2477 return;
2478 }
2479 }
2480#endif
2481
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002482 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002483 meta_header = (meta_header_t *) data;
2484 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002485 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002486 else if (meta_header->magic == META_HEADER_MAGIC)
2487 cmd_flash_meta_img(arg, data, sz);
2488 else
2489 cmd_flash_mmc_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002490 return;
2491}
2492
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002493void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2494{
2495 struct ptentry *sys_ptn;
2496 struct ptable *ptable;
2497
2498 ptable = flash_get_ptable();
2499 if (ptable == NULL) {
2500 fastboot_fail("partition table doesn't exist");
2501 return;
2502 }
2503
2504 sys_ptn = ptable_find(ptable, "system");
2505 if (sys_ptn == NULL) {
2506 fastboot_fail("system partition not found");
2507 return;
2508 }
2509
2510 sz = ROUND_TO_PAGE(sz, page_mask);
2511 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2512 fastboot_fail("update_ubi_vol failed");
2513 else
2514 fastboot_okay("");
2515}
2516
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002517void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002518{
2519 struct ptentry *ptn;
2520 struct ptable *ptable;
2521 unsigned extra = 0;
2522
2523 ptable = flash_get_ptable();
2524 if (ptable == NULL) {
2525 fastboot_fail("partition table doesn't exist");
2526 return;
2527 }
2528
2529 ptn = ptable_find(ptable, arg);
2530 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002531 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2532 arg);
2533 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002534 return;
2535 }
2536
2537 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2538 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2539 fastboot_fail("image is not a boot image");
2540 return;
2541 }
2542 }
2543
Amol Jadi5c61a952012-05-04 17:05:35 -07002544 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002545 || !strcmp(ptn->name, "userdata")
2546 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002547 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002548 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002549 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002550 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002551 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002552
2553 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002554 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2555 if (flash_ubi_img(ptn, data, sz)) {
2556 fastboot_fail("flash write failure");
2557 return;
2558 }
2559 } else {
2560 if (flash_write(ptn, extra, data, sz)) {
2561 fastboot_fail("flash write failure");
2562 return;
2563 }
Dima Zavin214cc642009-01-26 11:16:21 -08002564 }
2565 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2566 fastboot_okay("");
2567}
2568
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002569void cmd_flash(const char *arg, void *data, unsigned sz)
2570{
2571 if(target_is_emmc_boot())
2572 cmd_flash_mmc(arg, data, sz);
2573 else
2574 cmd_flash_nand(arg, data, sz);
2575}
2576
Dima Zavin214cc642009-01-26 11:16:21 -08002577void cmd_continue(const char *arg, void *data, unsigned sz)
2578{
2579 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002580 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002581
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002582 if (target_is_emmc_boot())
2583 {
2584 boot_linux_from_mmc();
2585 }
2586 else
2587 {
2588 boot_linux_from_flash();
2589 }
Dima Zavin214cc642009-01-26 11:16:21 -08002590}
2591
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002592void cmd_reboot(const char *arg, void *data, unsigned sz)
2593{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002594 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002595 fastboot_okay("");
2596 reboot_device(0);
2597}
2598
2599void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2600{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002601 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002602 fastboot_okay("");
2603 reboot_device(FASTBOOT_MODE);
2604}
2605
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002606void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2607{
2608 dprintf(INFO, "Enabling charger screen check\n");
2609 device.charger_screen_enabled = 1;
2610 write_device_info(&device);
2611 fastboot_okay("");
2612}
2613
2614void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2615{
2616 dprintf(INFO, "Disabling charger screen check\n");
2617 device.charger_screen_enabled = 0;
2618 write_device_info(&device);
2619 fastboot_okay("");
2620}
2621
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302622void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2623{
2624 dprintf(INFO, "Selecting display panel %s\n", arg);
2625 if (arg)
2626 strlcpy(device.display_panel, arg,
2627 sizeof(device.display_panel));
2628 write_device_info(&device);
2629 fastboot_okay("");
2630}
2631
Shashank Mittal162244e2011-08-08 19:01:25 -07002632void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2633{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002634 /* TODO: Wipe user data */
2635 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002636 {
2637 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002638 device.is_verified = 0;
2639 write_device_info(&device);
2640 }
2641 fastboot_okay("");
2642}
2643
2644void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2645{
2646 /* TODO: Wipe user data */
2647 if(device.is_unlocked || device.is_verified)
2648 {
2649 device.is_unlocked = 0;
2650 device.is_verified = 0;
2651 write_device_info(&device);
2652 }
2653 fastboot_okay("");
2654}
2655
2656void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2657{
2658 /* TODO: Wipe user data */
2659 if(device.is_unlocked || !device.is_verified)
2660 {
2661 device.is_unlocked = 0;
2662 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002663 write_device_info(&device);
2664 }
2665 fastboot_okay("");
2666}
2667
Shashank Mittala0032282011-08-26 14:50:11 -07002668void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2669{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302670 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002671 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002672 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002673 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2674 fastboot_info(response);
2675 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002676 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302677 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2678 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002679 fastboot_okay("");
2680}
2681
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002682void cmd_preflash(const char *arg, void *data, unsigned sz)
2683{
2684 fastboot_okay("");
2685}
2686
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302687struct fbimage* splash_screen_flash();
2688
2689int splash_screen_check_header(struct fbimage *logo)
2690{
2691 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2692 return -1;
2693 if (logo->header.width == 0 || logo->header.height == 0)
2694 return -1;
2695 return 0;
2696}
2697
2698struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002699{
2700 struct ptentry *ptn;
2701 struct ptable *ptable;
2702 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002703 struct fbimage *logo = NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002704
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302705
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002706 logo = (struct fbimage *) malloc(ROUNDUP(page_size, sizeof(struct fbimage)));
2707 ASSERT(logo);
2708
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302709 ptable = flash_get_ptable();
2710 if (ptable == NULL) {
2711 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002712 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302713 }
2714 ptn = ptable_find(ptable, "splash");
2715 if (ptn == NULL) {
2716 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002717 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302718 }
2719
2720 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2721 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002722 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302723 }
2724
2725 if (splash_screen_check_header(logo)) {
2726 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002727 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302728 }
2729
2730 fb_display = fbcon_display();
2731 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302732 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2733 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back to default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002734 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002735 }
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302736 uint8_t *base = (uint8_t *) fb_display->base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302737 if (flash_read(ptn + sizeof(logo->header), 0,
2738 base,
2739 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2740 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302741 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002742 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002743 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302744 logo->image = base;
2745 }
2746
2747 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002748
2749err:
2750 free(logo);
2751 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302752}
2753
2754struct fbimage* splash_screen_mmc()
2755{
2756 int index = INVALID_PTN;
2757 unsigned long long ptn = 0;
2758 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002759 struct fbimage *logo = NULL;
Channagoud Kadabida117392014-11-24 11:01:12 -08002760 uint32_t blocksize;
2761 uint32_t readsize;
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002762 uint32_t logosize;
Channagoud Kadabida117392014-11-24 11:01:12 -08002763 uint32_t ptn_size;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302764
2765 index = partition_get_index("splash");
2766 if (index == 0) {
2767 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2768 return NULL;
2769 }
2770
2771 ptn = partition_get_offset(index);
2772 if (ptn == 0) {
2773 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2774 return NULL;
2775 }
2776
Channagoud Kadabida117392014-11-24 11:01:12 -08002777 ptn_size = partition_get_size(index);
2778 blocksize = mmc_get_device_blocksize();
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002779 logosize = ROUNDUP(sizeof(logo->header), blocksize);
Channagoud Kadabida117392014-11-24 11:01:12 -08002780
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002781 logo = (struct fbimage *)memalign(CACHE_LINE, ROUNDUP(logosize, CACHE_LINE));
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002782 ASSERT(logo);
2783
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002784 if (mmc_read(ptn, (uint32_t *) logo, logosize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302785 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002786 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302787 }
2788
2789 if (splash_screen_check_header(logo)) {
2790 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002791 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302792 }
2793
2794 fb_display = fbcon_display();
2795 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302796 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2797 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002798 goto err;
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302799 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302800 uint8_t *base = (uint8_t *) fb_display->base;
Channagoud Kadabida117392014-11-24 11:01:12 -08002801 readsize = ROUNDUP((logo->header.width * logo->header.height * fb_display->bpp/8), blocksize);
2802
2803 if (readsize > ptn_size)
2804 {
2805 dprintf(CRITICAL, "@%d:Invalid logo header readsize:%u exceeds ptn_size:%u\n", __LINE__, readsize,ptn_size);
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002806 goto err;
Channagoud Kadabida117392014-11-24 11:01:12 -08002807 }
2808
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002809 if (mmc_read(ptn + logosize,(uint32_t *)base, readsize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302810 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302811 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002812 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302813 }
2814
2815 logo->image = base;
2816 }
2817
2818 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002819
2820err:
2821 free(logo);
2822 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302823}
2824
2825
2826struct fbimage* fetch_image_from_partition()
2827{
2828 if (target_is_emmc_boot()) {
2829 return splash_screen_mmc();
2830 } else {
2831 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002832 }
2833}
2834
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002835/* Get the size from partiton name */
2836static void get_partition_size(const char *arg, char *response)
2837{
2838 uint64_t ptn = 0;
2839 uint64_t size;
2840 int index = INVALID_PTN;
2841
2842 index = partition_get_index(arg);
2843
2844 if (index == INVALID_PTN)
2845 {
2846 dprintf(CRITICAL, "Invalid partition index\n");
2847 return;
2848 }
2849
2850 ptn = partition_get_offset(index);
2851
2852 if(!ptn)
2853 {
2854 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2855 return;
2856 }
2857
2858 size = partition_get_size(index);
2859
2860 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2861 return;
2862}
2863
2864/*
2865 * Publish the partition type & size info
2866 * fastboot getvar will publish the required information.
2867 * fastboot getvar partition_size:<partition_name>: partition size in hex
2868 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2869 */
2870static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2871{
2872 uint8_t i;
2873
2874 for (i = 0; i < num_parts; i++) {
2875 get_partition_size(info[i].part_name, info[i].size_response);
2876
2877 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2878 {
2879 dprintf(CRITICAL, "partition size name truncated\n");
2880 return;
2881 }
2882 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2883 {
2884 dprintf(CRITICAL, "partition type name truncated\n");
2885 return;
2886 }
2887
2888 /* publish partition size & type info */
2889 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2890 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2891 }
2892}
2893
Amol Jadi5edf3552013-07-23 14:15:34 -07002894/* register commands and variables for fastboot */
2895void aboot_fastboot_register_commands(void)
2896{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002897 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07002898
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002899 struct fastboot_cmd_desc cmd_list[] = {
2900 /* By default the enabled list is empty. */
2901 {"", NULL},
2902 /* move commands enclosed within the below ifndef to here
2903 * if they need to be enabled in user build.
2904 */
2905#ifndef DISABLE_FASTBOOT_CMDS
2906 /* Register the following commands only for non-user builds */
2907 {"flash:", cmd_flash},
2908 {"erase:", cmd_erase},
2909 {"boot", cmd_boot},
2910 {"continue", cmd_continue},
2911 {"reboot", cmd_reboot},
2912 {"reboot-bootloader", cmd_reboot_bootloader},
2913 {"oem unlock", cmd_oem_unlock},
2914 {"oem lock", cmd_oem_lock},
2915 {"oem verified", cmd_oem_verified},
2916 {"oem device-info", cmd_oem_devinfo},
2917 {"preflash", cmd_preflash},
2918 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
2919 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08002920 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002921#endif
2922 };
2923
2924 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
2925 for (i = 1; i < fastboot_cmds_count; i++)
2926 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
2927
Amol Jadi5edf3552013-07-23 14:15:34 -07002928 /* publish variables and their values */
2929 fastboot_publish("product", TARGET(BOARD));
2930 fastboot_publish("kernel", "lk");
2931 fastboot_publish("serialno", sn_buf);
2932
2933 /*
2934 * partition info is supported only for emmc partitions
2935 * Calling this for NAND prints some error messages which
2936 * is harmless but misleading. Avoid calling this for NAND
2937 * devices.
2938 */
2939 if (target_is_emmc_boot())
2940 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2941
2942 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002943 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2944 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002945 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002946 /* Is the charger screen check enabled */
2947 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2948 device.charger_screen_enabled);
2949 fastboot_publish("charger-screen-enabled",
2950 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302951 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2952 device.display_panel);
2953 fastboot_publish("display-panel",
2954 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002955 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
2956 fastboot_publish("version-baseband", (const char *) device.radio_version);
Amol Jadi5edf3552013-07-23 14:15:34 -07002957}
2958
Brian Swetland9c4c0752009-01-25 16:23:50 -08002959void aboot_init(const struct app_descriptor *app)
2960{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002961 unsigned reboot_mode = 0;
lijuang72875802015-02-10 10:35:12 +08002962 unsigned hard_reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002963 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002964
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002965 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002966 if (target_is_emmc_boot())
2967 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002968 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002969 page_mask = page_size - 1;
2970 }
2971 else
2972 {
2973 page_size = flash_page_size();
2974 page_mask = page_size - 1;
2975 }
2976
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002977 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2978
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002979 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002980
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002981 /* Display splash screen if enabled */
2982#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08002983#if NO_ALARM_DISPLAY
2984 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002985#endif
lijuang99c02d82015-02-13 19:04:34 +08002986 dprintf(SPEW, "Display Init: Start\n");
2987 target_display_init(device.display_panel);
2988 dprintf(SPEW, "Display Init: Done\n");
2989#if NO_ALARM_DISPLAY
2990 }
2991#endif
2992#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002993
Greg Griscod6250552011-06-29 14:40:23 -07002994 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002995 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002996
Dhaval Patel223ec952013-07-18 14:49:44 -07002997 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2998
Matthew Qindefd5562014-07-11 18:02:40 +08002999 /*
3000 * Check power off reason if user force reset,
3001 * if yes phone will do normal boot.
3002 */
3003 if (is_user_force_reset())
3004 goto normal_boot;
3005
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003006 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003007 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003008 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003009 dprintf(ALWAYS,"dload mode key sequence detected\n");
3010 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003011 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003012 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003013 }
3014 else
3015 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303016 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003017 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003018 }
3019 boot_into_fastboot = true;
3020 }
3021 if (!boot_into_fastboot)
3022 {
3023 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3024 boot_into_recovery = 1;
3025 if (!boot_into_recovery &&
3026 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003027 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003028 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003029 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003030 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003031 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003032 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003033
Ajay Dudani77421292010-10-27 19:34:06 -07003034 reboot_mode = check_reboot_mode();
lijuang72875802015-02-10 10:35:12 +08003035 hard_reboot_mode = check_hard_reboot_mode();
3036 if (reboot_mode == RECOVERY_MODE ||
3037 hard_reboot_mode == RECOVERY_HARD_RESET_MODE) {
Ajay Dudani77421292010-10-27 19:34:06 -07003038 boot_into_recovery = 1;
lijuang72875802015-02-10 10:35:12 +08003039 } else if(reboot_mode == FASTBOOT_MODE ||
3040 hard_reboot_mode == FASTBOOT_HARD_RESET_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003041 boot_into_fastboot = true;
lijuang72875802015-02-10 10:35:12 +08003042 } else if(reboot_mode == ALARM_BOOT ||
3043 hard_reboot_mode == RTC_HARD_RESET_MODE) {
Matthew Qind886f3c2014-01-17 16:52:01 +08003044 boot_reason_alarm = true;
Ajay Dudani77421292010-10-27 19:34:06 -07003045 }
3046
Matthew Qindefd5562014-07-11 18:02:40 +08003047normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003048 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003049 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003050 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003051 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003052 if(emmc_recovery_init())
3053 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3054 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003055 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003056 if((device.is_unlocked) || (device.is_tampered))
3057 {
3058 #ifdef TZ_TAMPER_FUSE
3059 set_tamper_fuse_cmd();
3060 #endif
3061 #if USE_PCOM_SECBOOT
3062 set_tamper_flag(device.is_tampered);
3063 #endif
3064 }
Shashank Mittala0032282011-08-26 14:50:11 -07003065 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003066
Pavel Nedev5d91d412013-04-29 11:34:24 +03003067 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003068 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003069 else
3070 {
3071 recovery_init();
3072 #if USE_PCOM_SECBOOT
3073 if((device.is_unlocked) || (device.is_tampered))
3074 set_tamper_flag(device.is_tampered);
3075 #endif
3076 boot_linux_from_flash();
3077 }
3078 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3079 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003080 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003081
Amol Jadi5edf3552013-07-23 14:15:34 -07003082 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003083
Amol Jadi5edf3552013-07-23 14:15:34 -07003084 /* register aboot specific fastboot commands */
3085 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003086
Amol Jadi5edf3552013-07-23 14:15:34 -07003087 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003088 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003089
3090 /* initialize and start fastboot */
3091 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08003092}
3093
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003094uint32_t get_page_size()
3095{
3096 return page_size;
3097}
3098
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003099/*
3100 * Calculated and save hash (SHA256) for non-signed boot image.
3101 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003102 * @param image_addr - Boot image address
3103 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003104 *
3105 * @return int - 0 on success, negative value on failure.
3106 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003107static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003108{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003109 unsigned int digest[8];
3110#if IMAGE_VERIF_ALGO_SHA1
3111 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3112#else
3113 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3114#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003115
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003116 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003117 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003118
3119 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003120 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003121
3122 return 0;
3123}
3124
Brian Swetland9c4c0752009-01-25 16:23:50 -08003125APP_START(aboot)
3126 .init = aboot_init,
3127APP_END