blob: bce7ec140579f5232c5378d6d36560cdd598e5ea [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";
David Ng183a7422009-12-07 14:55:21 -0800146
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800147static const char *baseband_apq = " androidboot.baseband=apq";
148static const char *baseband_msm = " androidboot.baseband=msm";
149static const char *baseband_csfb = " androidboot.baseband=csfb";
150static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700151static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800152static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700153static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800154static const char *baseband_dsda = " androidboot.baseband=dsda";
155static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800156static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800157static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800158
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700159static unsigned page_size = 0;
160static unsigned page_mask = 0;
161static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
162static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700163static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800164static bool boot_reason_alarm;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700165
Shashank Mittalcd98d472011-08-02 14:29:24 -0700166/* Assuming unauthorized kernel image by default */
167static int auth_kernel_img = 0;
168
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -0800169static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
Shashank Mittal162244e2011-08-08 19:01:25 -0700170
Dima Zavin42168f22009-01-30 11:52:22 -0800171struct atag_ptbl_entry
172{
173 char name[16];
174 unsigned offset;
175 unsigned size;
176 unsigned flags;
177};
178
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700179/*
180 * Partition info, required to be published
181 * for fastboot
182 */
183struct getvar_partition_info {
184 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
185 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
186 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
187 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
188 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
189};
190
191/*
192 * Right now, we are publishing the info for only
193 * three partitions
194 */
195struct getvar_partition_info part_info[] =
196{
197 { "system" , "partition-size:", "partition-type:", "", "ext4" },
198 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
199 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
200};
201
202char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700203char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800204char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700205char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530206char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700207
Greg Griscod2471ef2011-07-14 13:00:42 -0700208extern int emmc_recovery_init(void);
209
Kinson Chik0b1c8162011-08-31 16:31:57 -0700210#if NO_KEYPAD_DRIVER
211extern int fastboot_trigger(void);
212#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700213
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800214static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700215{
216 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700217#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800218 if (is_arm64)
219 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
220 else
221 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700222 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
223 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700224#endif
225}
226
Dima Zavin42168f22009-01-30 11:52:22 -0800227static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
228{
229 struct atag_ptbl_entry atag_ptn;
230
231 memcpy(atag_ptn.name, ptn->name, 16);
232 atag_ptn.name[15] = '\0';
233 atag_ptn.offset = ptn->start;
234 atag_ptn.size = ptn->length;
235 atag_ptn.flags = ptn->flags;
236 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
237 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
238}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800239
Neeti Desaie245d492012-06-01 12:52:13 -0700240unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800241{
David Ng183a7422009-12-07 14:55:21 -0800242 int cmdline_len = 0;
243 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800244 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700245 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800246 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300247 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700248 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700249 char *boot_dev_buf = NULL;
Dima Zavin42168f22009-01-30 11:52:22 -0800250
Brian Swetland9c4c0752009-01-25 16:23:50 -0800251 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800252 cmdline_len = strlen(cmdline);
253 have_cmdline = 1;
254 }
255 if (target_is_emmc_boot()) {
256 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800257#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700258 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
259 ASSERT(boot_dev_buf);
260 platform_boot_dev_cmdline(boot_dev_buf);
261 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700262#endif
David Ng183a7422009-12-07 14:55:21 -0800263 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800264
265 cmdline_len += strlen(usb_sn_cmdline);
266 cmdline_len += strlen(sn_buf);
267
Pavel Nedev5614d222013-06-17 18:01:02 +0300268 if (boot_into_recovery && gpt_exists)
269 cmdline_len += strlen(secondary_gpt_enable);
270
Pavel Nedev328ac822013-04-05 15:25:11 +0300271 if (boot_into_ffbm) {
272 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700273 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800274 /* reduce kernel console messages to speed-up boot */
275 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800276 } else if (boot_reason_alarm) {
277 cmdline_len += strlen(alarmboot_cmdline);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700278 } else if (device.charger_screen_enabled &&
279 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700280 pause_at_bootup = 1;
281 cmdline_len += strlen(battchg_pause);
282 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800283
Shashank Mittalcd98d472011-08-02 14:29:24 -0700284 if(target_use_signed_kernel() && auth_kernel_img) {
285 cmdline_len += strlen(auth_kernel);
286 }
287
Joonwoo Park61112782013-10-02 19:50:39 -0700288 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
289 "system",
290 target_boot_params,
291 sizeof(target_boot_params)) == 0) {
292 have_target_boot_params = 1;
293 cmdline_len += strlen(target_boot_params);
294 }
295
Ajay Dudanid04110c2011-01-17 23:55:07 -0800296 /* Determine correct androidboot.baseband to use */
297 switch(target_baseband())
298 {
299 case BASEBAND_APQ:
300 cmdline_len += strlen(baseband_apq);
301 break;
302
303 case BASEBAND_MSM:
304 cmdline_len += strlen(baseband_msm);
305 break;
306
307 case BASEBAND_CSFB:
308 cmdline_len += strlen(baseband_csfb);
309 break;
310
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800311 case BASEBAND_SVLTE2A:
312 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800313 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700314
315 case BASEBAND_MDM:
316 cmdline_len += strlen(baseband_mdm);
317 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700318
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800319 case BASEBAND_MDM2:
320 cmdline_len += strlen(baseband_mdm2);
321 break;
322
Amol Jadi5c61a952012-05-04 17:05:35 -0700323 case BASEBAND_SGLTE:
324 cmdline_len += strlen(baseband_sglte);
325 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530326
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800327 case BASEBAND_SGLTE2:
328 cmdline_len += strlen(baseband_sglte2);
329 break;
330
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530331 case BASEBAND_DSDA:
332 cmdline_len += strlen(baseband_dsda);
333 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800334
335 case BASEBAND_DSDA2:
336 cmdline_len += strlen(baseband_dsda2);
337 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800338 }
339
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800340 if (cmdline) {
341 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
342 target_display_panel_node(device.display_panel,
343 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
344 strlen(display_panel_buf)) {
345 cmdline_len += strlen(display_panel_buf);
346 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700347 }
348
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800349 if (target_warm_boot()) {
350 warm_boot = true;
351 cmdline_len += strlen(warmboot_cmdline);
352 }
353
David Ng183a7422009-12-07 14:55:21 -0800354 if (cmdline_len > 0) {
355 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800356 unsigned char *dst;
357
358 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
359 ASSERT(cmdline_final != NULL);
360 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700361
Amol Jadi168b7712012-03-06 16:15:00 -0800362 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800363 if (have_cmdline) {
364 src = cmdline;
365 while ((*dst++ = *src++));
366 }
367 if (target_is_emmc_boot()) {
368 src = emmc_cmdline;
369 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700370 have_cmdline = 1;
371 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800372#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700373 src = boot_dev_buf;
374 if (have_cmdline) --dst;
375 while ((*dst++ = *src++));
376#endif
David Ngf773dde2010-07-26 19:55:08 -0700377 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800378
379 src = usb_sn_cmdline;
380 if (have_cmdline) --dst;
381 have_cmdline = 1;
382 while ((*dst++ = *src++));
383 src = sn_buf;
384 if (have_cmdline) --dst;
385 have_cmdline = 1;
386 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800387 if (warm_boot) {
388 if (have_cmdline) --dst;
389 src = warmboot_cmdline;
390 while ((*dst++ = *src++));
391 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800392
Pavel Nedev5614d222013-06-17 18:01:02 +0300393 if (boot_into_recovery && gpt_exists) {
394 src = secondary_gpt_enable;
395 if (have_cmdline) --dst;
396 while ((*dst++ = *src++));
397 }
398
Pavel Nedev328ac822013-04-05 15:25:11 +0300399 if (boot_into_ffbm) {
400 src = androidboot_mode;
401 if (have_cmdline) --dst;
402 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700403 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300404 if (have_cmdline) --dst;
405 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800406 src = loglevel;
407 if (have_cmdline) --dst;
408 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800409 } else if (boot_reason_alarm) {
410 src = alarmboot_cmdline;
411 if (have_cmdline) --dst;
412 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300413 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700414 src = battchg_pause;
415 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800416 while ((*dst++ = *src++));
417 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800418
Shashank Mittalcd98d472011-08-02 14:29:24 -0700419 if(target_use_signed_kernel() && auth_kernel_img) {
420 src = auth_kernel;
421 if (have_cmdline) --dst;
422 while ((*dst++ = *src++));
423 }
424
Ajay Dudanid04110c2011-01-17 23:55:07 -0800425 switch(target_baseband())
426 {
427 case BASEBAND_APQ:
428 src = baseband_apq;
429 if (have_cmdline) --dst;
430 while ((*dst++ = *src++));
431 break;
432
433 case BASEBAND_MSM:
434 src = baseband_msm;
435 if (have_cmdline) --dst;
436 while ((*dst++ = *src++));
437 break;
438
439 case BASEBAND_CSFB:
440 src = baseband_csfb;
441 if (have_cmdline) --dst;
442 while ((*dst++ = *src++));
443 break;
444
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800445 case BASEBAND_SVLTE2A:
446 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800447 if (have_cmdline) --dst;
448 while ((*dst++ = *src++));
449 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700450
451 case BASEBAND_MDM:
452 src = baseband_mdm;
453 if (have_cmdline) --dst;
454 while ((*dst++ = *src++));
455 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700456
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800457 case BASEBAND_MDM2:
458 src = baseband_mdm2;
459 if (have_cmdline) --dst;
460 while ((*dst++ = *src++));
461 break;
462
Amol Jadi5c61a952012-05-04 17:05:35 -0700463 case BASEBAND_SGLTE:
464 src = baseband_sglte;
465 if (have_cmdline) --dst;
466 while ((*dst++ = *src++));
467 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530468
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800469 case BASEBAND_SGLTE2:
470 src = baseband_sglte2;
471 if (have_cmdline) --dst;
472 while ((*dst++ = *src++));
473 break;
474
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530475 case BASEBAND_DSDA:
476 src = baseband_dsda;
477 if (have_cmdline) --dst;
478 while ((*dst++ = *src++));
479 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800480
481 case BASEBAND_DSDA2:
482 src = baseband_dsda2;
483 if (have_cmdline) --dst;
484 while ((*dst++ = *src++));
485 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800486 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700487
488 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700489 src = display_panel_buf;
490 if (have_cmdline) --dst;
491 while ((*dst++ = *src++));
492 }
Joonwoo Park61112782013-10-02 19:50:39 -0700493
494 if (have_target_boot_params) {
495 if (have_cmdline) --dst;
496 src = target_boot_params;
497 while ((*dst++ = *src++));
498 }
Neeti Desaie245d492012-06-01 12:52:13 -0700499 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700500
501
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700502 if (boot_dev_buf)
503 free(boot_dev_buf);
504
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800505 if (cmdline_final)
506 dprintf(INFO, "cmdline: %s\n", cmdline_final);
507 else
508 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700509 return cmdline_final;
510}
511
512unsigned *atag_core(unsigned *ptr)
513{
514 /* CORE */
515 *ptr++ = 2;
516 *ptr++ = 0x54410001;
517
518 return ptr;
519
520}
521
522unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
523 unsigned ramdisk_size)
524{
525 if (ramdisk_size) {
526 *ptr++ = 4;
527 *ptr++ = 0x54420005;
528 *ptr++ = (unsigned)ramdisk;
529 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800530 }
531
Neeti Desaie245d492012-06-01 12:52:13 -0700532 return ptr;
533}
534
535unsigned *atag_ptable(unsigned **ptr_addr)
536{
537 int i;
538 struct ptable *ptable;
539
540 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700541 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
542 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700543 *(*ptr_addr)++ = 0x4d534d70;
544 for (i = 0; i < ptable->count; ++i)
545 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
546 }
547
548 return (*ptr_addr);
549}
550
551unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
552{
553 int cmdline_length = 0;
554 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700555 char *dest;
556
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800557 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700558 n = (cmdline_length + 4) & (~3);
559
560 *ptr++ = (n / 4) + 2;
561 *ptr++ = 0x54410009;
562 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800563 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700564 ptr += (n / 4);
565
566 return ptr;
567}
568
569unsigned *atag_end(unsigned *ptr)
570{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800571 /* END */
572 *ptr++ = 0;
573 *ptr++ = 0;
574
Neeti Desaie245d492012-06-01 12:52:13 -0700575 return ptr;
576}
577
578void generate_atags(unsigned *ptr, const char *cmdline,
579 void *ramdisk, unsigned ramdisk_size)
580{
581
582 ptr = atag_core(ptr);
583 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
584 ptr = target_atag_mem(ptr);
585
586 /* Skip NAND partition ATAGS for eMMC boot */
587 if (!target_is_emmc_boot()){
588 ptr = atag_ptable(&ptr);
589 }
590
591 ptr = atag_cmdline(ptr, cmdline);
592 ptr = atag_end(ptr);
593}
594
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700595typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700596void boot_linux(void *kernel, unsigned *tags,
597 const char *cmdline, unsigned machtype,
598 void *ramdisk, unsigned ramdisk_size)
599{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800600 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800601#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700602 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800603#endif
604
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700605 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800606 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800607 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800608
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530609 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700610
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800611 final_cmdline = update_cmdline((const char*)cmdline);
612
Neeti Desai17379b82012-06-04 18:42:53 -0700613#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800614 dprintf(INFO, "Updating device tree: start\n");
615
Neeti Desai17379b82012-06-04 18:42:53 -0700616 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530617 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700618 if(ret)
619 {
620 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
621 ASSERT(0);
622 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800623 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700624#else
Neeti Desaie245d492012-06-01 12:52:13 -0700625 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800626 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700627#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700628
Maria Yu52254c02014-07-04 16:14:54 +0800629 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700630
631#if VERIFIED_BOOT
632 /* Write protect the device info */
Channagoud Kadabibc4f15f2015-02-25 12:09:51 -0800633 if (target_build_variant_user() && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700634 {
635 dprintf(INFO, "Failed to write protect dev info\n");
636 ASSERT(0);
637 }
638#endif
639
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700640 /* Perform target specific cleanup */
641 target_uninit();
642
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800643 /* Turn off splash screen if enabled */
644#if DISPLAY_SPLASH_SCREEN
645 target_display_shutdown();
646#endif
647
648
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800649 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530650 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800651
652 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700653
Amol Jadi4421e652011-06-16 15:00:48 -0700654 /* do any platform specific cleanup before kernel entry */
655 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700656
Brian Swetland9c4c0752009-01-25 16:23:50 -0800657 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700658
Amol Jadi504f9fe2012-08-16 13:56:48 -0700659#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800660 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700661#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700662 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800663
664 if (IS_ARM64(kptr))
665 /* Jump to a 64bit kernel */
666 scm_elexec_call((paddr_t)kernel, tags_phys);
667 else
668 /* Jump to a 32bit kernel */
669 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800670}
671
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700672/* Function to check if the memory address range falls within the aboot
673 * boundaries.
674 * start: Start of the memory region
675 * size: Size of the memory region
676 */
677int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
678{
679 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800680 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700681 return -1;
682
683 /* Check for memory overlap. */
684 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
685 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700686 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700687 return 0;
688 else
689 return -1;
690}
691
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800692#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800693
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800694BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800695#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800696BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800697#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800698
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700699static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
700{
701 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800702
703#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800704#if IMAGE_VERIF_ALGO_SHA1
705 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
706#else
707 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
708#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800709#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700710
711 /* Assume device is rooted at this time. */
712 device.is_tampered = 1;
713
714 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
715
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700716#if VERIFIED_BOOT
717 if(boot_into_recovery)
718 {
719 ret = boot_verify_image((unsigned char *)bootimg_addr,
720 bootimg_size, "recovery");
721 }
722 else
723 {
724 ret = boot_verify_image((unsigned char *)bootimg_addr,
725 bootimg_size, "boot");
726 }
727 boot_verify_print_state();
728#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700729 ret = image_verify((unsigned char *)bootimg_addr,
730 (unsigned char *)(bootimg_addr + bootimg_size),
731 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800732 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700733#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700734 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
735
736 if (ret)
737 {
738 /* Authorized kernel */
739 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700740 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700741 }
742
743#if USE_PCOM_SECBOOT
744 set_tamper_flag(device.is_tampered);
745#endif
746
747 if(device.is_tampered)
748 {
749 write_device_info_mmc(&device);
750 #ifdef TZ_TAMPER_FUSE
751 set_tamper_fuse_cmd();
752 #endif
753 #ifdef ASSERT_ON_TAMPER
754 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
755 ASSERT(0);
756 #endif
757 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700758
759#if VERIFIED_BOOT
760 if(boot_verify_get_state() == RED)
761 {
762 if(!boot_into_recovery)
763 {
764 dprintf(CRITICAL,
765 "Device verification failed. Rebooting into recovery.\n");
766 reboot_device(RECOVERY_MODE);
767 }
768 else
769 {
770 dprintf(CRITICAL,
771 "Recovery image verification failed. Asserting..\n");
772 ASSERT(0);
773 }
774 }
775#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700776}
777
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530778static bool check_format_bit()
779{
780 bool ret = false;
781 int index;
782 uint64_t offset;
783 struct boot_selection_info *in = NULL;
784 char *buf = NULL;
785
786 index = partition_get_index("bootselect");
787 if (index == INVALID_PTN)
788 {
789 dprintf(INFO, "Unable to locate /bootselect partition\n");
790 return ret;
791 }
792 offset = partition_get_offset(index);
793 if(!offset)
794 {
795 dprintf(INFO, "partition /bootselect doesn't exist\n");
796 return ret;
797 }
798 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
799 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530800 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530801 {
802 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
803 free(buf);
804 return ret;
805 }
806 in = (struct boot_selection_info *) buf;
807 if ((in->signature == BOOTSELECT_SIGNATURE) &&
808 (in->version == BOOTSELECT_VERSION)) {
809 if ((in->state_info & BOOTSELECT_FORMAT) &&
810 !(in->state_info & BOOTSELECT_FACTORY))
811 ret = true;
812 } else {
813 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
814 in->signature, in->version);
815 ASSERT(0);
816 }
817 free(buf);
818 return ret;
819}
820
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700821void boot_verifier_init()
822{
823
824 uint32_t boot_state;
825 /* Check if device unlock */
826 if(device.is_unlocked)
827 {
828 boot_verify_send_event(DEV_UNLOCK);
829 boot_verify_print_state();
830 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
831 return;
832 }
833 else
834 {
835 boot_verify_send_event(BOOT_INIT);
836 }
837
838 /* Initialize keystore */
839 boot_state = boot_verify_keystore_init();
840 if(boot_state == YELLOW)
841 {
842 boot_verify_print_state();
843 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
844 }
845}
846
Shashank Mittal23b8f422010-04-16 19:27:21 -0700847int boot_linux_from_mmc(void)
848{
849 struct boot_img_hdr *hdr = (void*) buf;
850 struct boot_img_hdr *uhdr;
851 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700852 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700853 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700854 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700855
Shashank Mittalcd98d472011-08-02 14:29:24 -0700856 unsigned char *image_addr = 0;
857 unsigned kernel_actual;
858 unsigned ramdisk_actual;
859 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700860 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700861
Matthew Qinbb7923d2015-02-09 10:56:09 +0800862 unsigned int out_len = 0;
863 unsigned int out_avai_len = 0;
864 unsigned char *out_addr = NULL;
865 uint32_t dtb_offset = 0;
866 unsigned char *kernel_start_addr = NULL;
867 unsigned int kernel_size = 0;
868 int rc;
869
Neeti Desai465491e2012-07-31 12:53:35 -0700870#if DEVICE_TREE
871 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700872 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700873 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800874 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700875 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700876#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +0800877 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800878
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530879 if (check_format_bit())
880 boot_into_recovery = 1;
881
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700882 if (!boot_into_recovery) {
883 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
884 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
885 if (rcode <= 0) {
886 boot_into_ffbm = false;
887 if (rcode < 0)
888 dprintf(CRITICAL,"failed to get ffbm cookie");
889 } else
890 boot_into_ffbm = true;
891 } else
892 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700893 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
894 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
895 dprintf(INFO, "Unified boot method!\n");
896 hdr = uhdr;
897 goto unified_boot;
898 }
Greg Griscod6250552011-06-29 14:40:23 -0700899 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700900 index = partition_get_index("boot");
901 ptn = partition_get_offset(index);
902 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700903 dprintf(CRITICAL, "ERROR: No boot partition found\n");
904 return -1;
905 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700906 }
907 else {
908 index = partition_get_index("recovery");
909 ptn = partition_get_offset(index);
910 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700911 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
912 return -1;
913 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700914 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -0800915 /* Set Lun for boot & recovery partitions */
916 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -0700917
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530918 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700919 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
920 return -1;
921 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700922
923 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700924 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700925 return -1;
926 }
927
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700928 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530929
930 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
931 dprintf(CRITICAL, "ERROR: Invalid page size\n");
932 return -1;
933 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700934 page_size = hdr->page_size;
935 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700936 }
937
Matthew Qin49e51fa2015-02-09 10:40:45 +0800938 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
939 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800940
Matthew Qin49e51fa2015-02-09 10:40:45 +0800941 image_addr = (unsigned char *)target_get_scratch_address();
942
943#if DEVICE_TREE
944 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
945 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
946#else
947 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
948#endif
949
950#if VERIFIED_BOOT
951 boot_verifier_init();
952#endif
953
954 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
955 {
956 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
957 return -1;
958 }
959
Matthew Qinbb7923d2015-02-09 10:56:09 +0800960 /*
961 * Update loading flow of bootimage to support compressed/uncompressed
962 * bootimage on both 64bit and 32bit platform.
963 * 1. Load bootimage from emmc partition onto DDR.
964 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
965 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
966 * platform accordingly.
967 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
968 */
969
Matthew Qin49e51fa2015-02-09 10:40:45 +0800970 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
971 bs_set_timestamp(BS_KERNEL_LOAD_START);
972
Matthew Qinbb7923d2015-02-09 10:56:09 +0800973 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +0800974 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
975 {
976 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
977 return -1;
978 }
979
980 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
981 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
982
983 /* Authenticate Kernel */
984 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
985 (int) target_use_signed_kernel(),
986 device.is_unlocked,
987 device.is_tampered);
988
989 if(target_use_signed_kernel() && (!device.is_unlocked))
990 {
991 offset = imagesize_actual;
992 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
993 {
994 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
995 return -1;
996 }
997
998 /* Read signature */
999 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1000 {
1001 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1002 return -1;
1003 }
1004
1005 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
1006 } else {
1007 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1008 #ifdef TZ_SAVE_KERNEL_HASH
1009 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1010 #endif /* TZ_SAVE_KERNEL_HASH */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001011 }
1012
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001013 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001014 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1015 * If not, continue booting.
1016 */
1017 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1018 {
1019 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1020 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
1021 dprintf(INFO, "decompress image start\n");
1022 rc = decompress((unsigned char *)(image_addr + page_size),
1023 hdr->kernel_size, out_addr, out_avai_len,
1024 &dtb_offset, &out_len);
1025 if (rc)
1026 {
1027 dprintf(INFO, "decompress image failed!!!\n");
1028 ASSERT(0);
1029 }
1030
1031 dprintf(INFO, "decompressed image finished.\n");
1032 kptr = (struct kernel64_hdr *)out_addr;
1033 kernel_start_addr = out_addr;
1034 kernel_size = out_len;
1035 } else {
1036 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1037 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1038 kernel_size = hdr->kernel_size;
1039 }
1040
1041 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001042 * Update the kernel/ramdisk/tags address if the boot image header
1043 * has default values, these default values come from mkbootimg when
1044 * the boot image is flashed using fastboot flash:raw
1045 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001046 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001047
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001048 /* Get virtual addresses since the hdr saves physical addresses. */
1049 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1050 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1051 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001052
Matthew Qinbb7923d2015-02-09 10:56:09 +08001053 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001054 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001055 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001056 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1057 {
1058 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1059 return -1;
1060 }
1061
1062#ifndef DEVICE_TREE
1063 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1064 {
1065 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1066 return -1;
1067 }
1068#endif
1069
Matthew Qin49e51fa2015-02-09 10:40:45 +08001070 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001071 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001072 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001073
Matthew Qin49e51fa2015-02-09 10:40:45 +08001074 #if DEVICE_TREE
1075 if(hdr->dt_size) {
1076 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1077 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001078
Matthew Qin49e51fa2015-02-09 10:40:45 +08001079 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1080 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1081 return -1;
1082 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001083
Matthew Qin49e51fa2015-02-09 10:40:45 +08001084 /* Find index of device tree within device tree table */
1085 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1086 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1087 return -1;
1088 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001089
Matthew Qin49e51fa2015-02-09 10:40:45 +08001090 /* Validate and Read device device tree in the tags_addr */
1091 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001092 {
1093 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1094 return -1;
1095 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001096
Matthew Qin49e51fa2015-02-09 10:40:45 +08001097 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
1098 } else {
1099 /* Validate the tags_addr */
1100 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001101 {
1102 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1103 return -1;
1104 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001105 /*
1106 * If appended dev tree is found, update the atags with
1107 * memory address to the DTB appended location on RAM.
1108 * Else update with the atags address in the kernel header
1109 */
1110 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001111 dtb = dev_tree_appended((void*)(image_addr + page_size),
1112 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001113 (void *)hdr->tags_addr);
1114 if (!dtb) {
1115 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001116 return -1;
1117 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001118 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001119 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001120
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001121 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1122 target_load_ssd_keystore();
1123
Shashank Mittal23b8f422010-04-16 19:27:21 -07001124unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001125
Dima Zavin77e41f32013-03-06 16:10:43 -08001126 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001127 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001128 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1129
1130 return 0;
1131}
1132
Dima Zavin214cc642009-01-26 11:16:21 -08001133int boot_linux_from_flash(void)
1134{
1135 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001136 struct ptentry *ptn;
1137 struct ptable *ptable;
1138 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001139
Shashank Mittalcd98d472011-08-02 14:29:24 -07001140 unsigned char *image_addr = 0;
1141 unsigned kernel_actual;
1142 unsigned ramdisk_actual;
1143 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001144 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001145
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001146#if DEVICE_TREE
1147 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001148 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001149 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001150 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001151#endif
1152
David Ng183a7422009-12-07 14:55:21 -08001153 if (target_is_emmc_boot()) {
1154 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1155 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1156 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1157 return -1;
1158 }
1159 goto continue_boot;
1160 }
1161
Dima Zavin214cc642009-01-26 11:16:21 -08001162 ptable = flash_get_ptable();
1163 if (ptable == NULL) {
1164 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1165 return -1;
1166 }
1167
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001168 if(!boot_into_recovery)
1169 {
1170 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001171
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001172 if (ptn == NULL) {
1173 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1174 return -1;
1175 }
1176 }
1177 else
1178 {
1179 ptn = ptable_find(ptable, "recovery");
1180 if (ptn == NULL) {
1181 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1182 return -1;
1183 }
Dima Zavin214cc642009-01-26 11:16:21 -08001184 }
1185
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001186 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001187 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1188 return -1;
1189 }
Dima Zavin214cc642009-01-26 11:16:21 -08001190
1191 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001192 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001193 return -1;
1194 }
1195
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001196 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001197 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 -08001198 return -1;
1199 }
1200
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001201 /*
1202 * Update the kernel/ramdisk/tags address if the boot image header
1203 * has default values, these default values come from mkbootimg when
1204 * the boot image is flashed using fastboot flash:raw
1205 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001206 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001207
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001208 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001209 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1210 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1211 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1212
1213 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1214 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1215
1216 /* Check if the addresses in the header are valid. */
1217 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1218 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1219 {
1220 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1221 return -1;
1222 }
1223
1224#ifndef DEVICE_TREE
1225 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1226 {
1227 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1228 return -1;
1229 }
1230#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001231
Shashank Mittalcd98d472011-08-02 14:29:24 -07001232 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001233 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001234 {
1235 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001236 offset = 0;
1237
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001238#if DEVICE_TREE
1239 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1240 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001241
1242 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1243 {
1244 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1245 return -1;
1246 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001247#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001248 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001249#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001250
Amol Jadib6be5c12012-11-14 13:39:51 -08001251 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001252 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001253
Shashank Mittalcd98d472011-08-02 14:29:24 -07001254 /* Read image without signature */
1255 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1256 {
1257 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1258 return -1;
1259 }
Dima Zavin214cc642009-01-26 11:16:21 -08001260
Amol Jadib6be5c12012-11-14 13:39:51 -08001261 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001262 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001263
Shashank Mittalcd98d472011-08-02 14:29:24 -07001264 offset = imagesize_actual;
1265 /* Read signature */
1266 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1267 {
1268 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001269 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001270 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001271
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301272 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001273
1274 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001275 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1276 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001277#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001278 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001279 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001280 {
1281 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1282 return -1;
1283 }
1284
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001285 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1286#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001287
1288 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001289 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001290 {
1291 write_device_info_flash(&device);
1292 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301293#if USE_PCOM_SECBOOT
1294 set_tamper_flag(device.is_tampered);
1295#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001296 }
1297 else
1298 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001299 offset = page_size;
1300
Amol Jadib6be5c12012-11-14 13:39:51 -08001301 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1302 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1303 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1304
1305 dprintf(INFO, "Loading boot image (%d): start\n",
1306 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001307 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001308
1309 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001310 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1311 return -1;
1312 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001313 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001314
Amol Jadib6be5c12012-11-14 13:39:51 -08001315 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001316 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1317 return -1;
1318 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001319 offset += ramdisk_actual;
1320
1321 dprintf(INFO, "Loading boot image (%d): done\n",
1322 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001323 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001324
1325 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001326 offset += second_actual;
1327 /* Second image loading not implemented. */
1328 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001329 }
1330
1331#if DEVICE_TREE
1332 if(hdr->dt_size != 0) {
1333
1334 /* Read the device tree table into buffer */
1335 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1336 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1337 return -1;
1338 }
1339
1340 table = (struct dt_table*) dt_buf;
1341
Deepa Dinamani19648b42013-09-05 17:05:55 -07001342 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001343 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1344 return -1;
1345 }
1346
Deepa Dinamani19648b42013-09-05 17:05:55 -07001347 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1348 if (!table)
1349 return -1;
1350
1351 /* Read the entire device tree table into buffer */
1352 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1353 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1354 return -1;
1355 }
1356
1357
Joel Kingaa335dc2013-06-03 16:11:08 -07001358 /* Find index of device tree within device tree table */
1359 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001360 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1361 return -1;
1362 }
1363
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001364 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001365 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001366 {
1367 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1368 return -1;
1369 }
1370
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001371 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001372 if(flash_read(ptn, offset + dt_entry.offset,
1373 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001374 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1375 return -1;
1376 }
1377 }
1378#endif
1379
Shashank Mittalcd98d472011-08-02 14:29:24 -07001380 }
David Ng183a7422009-12-07 14:55:21 -08001381continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001382
Dima Zavin214cc642009-01-26 11:16:21 -08001383 /* TODO: create/pass atags to kernel */
1384
Ajay Dudanie28a6072011-07-01 13:59:46 -07001385 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001386 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001387 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1388
1389 return 0;
1390}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001391
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001392BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001393void write_device_info_mmc(device_info *dev)
1394{
Shashank Mittal162244e2011-08-08 19:01:25 -07001395 unsigned long long ptn = 0;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001396#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001397 unsigned long long size;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001398#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001399 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001400 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001401 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001402
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001403#if VERIFIED_BOOT
1404 index = partition_get_index("devinfo");
1405#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001406 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001407#endif
1408
Shashank Mittal162244e2011-08-08 19:01:25 -07001409 ptn = partition_get_offset(index);
1410 if(ptn == 0)
1411 {
1412 return;
1413 }
1414
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001415 lun = partition_get_lun(index);
1416 mmc_set_lun(lun);
1417
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001418#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001419 size = partition_get_size(index);
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001420#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001421
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001422 blocksize = mmc_get_device_blocksize();
1423
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001424#if VERIFIED_BOOT
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001425 if(mmc_write(ptn, blocksize, (void *)dev))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001426#else
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001427 if(mmc_write((ptn + size - blocksize), blocksize, (void *)dev))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001428#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001429 {
1430 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1431 return;
1432 }
1433}
1434
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001435void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001436{
Shashank Mittal162244e2011-08-08 19:01:25 -07001437 unsigned long long ptn = 0;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001438#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001439 unsigned long long size;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001440#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001441 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001442 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001443
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001444#if VERIFIED_BOOT
1445 index = partition_get_index("devinfo");
1446#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001447 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001448#endif
1449
Shashank Mittal162244e2011-08-08 19:01:25 -07001450 ptn = partition_get_offset(index);
1451 if(ptn == 0)
1452 {
1453 return;
1454 }
1455
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001456 mmc_set_lun(partition_get_lun(index));
1457
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001458#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001459 size = partition_get_size(index);
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001460#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001461
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001462 blocksize = mmc_get_device_blocksize();
1463
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001464#if VERIFIED_BOOT
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001465 if(mmc_read(ptn, (void *)info, blocksize))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001466#else
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001467 if(mmc_read((ptn + size - blocksize), (void *)info, blocksize))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001468#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001469 {
1470 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1471 return;
1472 }
1473
Shashank Mittal162244e2011-08-08 19:01:25 -07001474}
1475
1476void write_device_info_flash(device_info *dev)
1477{
1478 struct device_info *info = (void *) info_buf;
1479 struct ptentry *ptn;
1480 struct ptable *ptable;
1481
1482 ptable = flash_get_ptable();
1483 if (ptable == NULL)
1484 {
1485 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1486 return;
1487 }
1488
1489 ptn = ptable_find(ptable, "devinfo");
1490 if (ptn == NULL)
1491 {
1492 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1493 return;
1494 }
1495
1496 memcpy(info, dev, sizeof(device_info));
1497
1498 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1499 {
1500 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1501 return;
1502 }
1503}
1504
1505void read_device_info_flash(device_info *dev)
1506{
1507 struct device_info *info = (void*) info_buf;
1508 struct ptentry *ptn;
1509 struct ptable *ptable;
1510
1511 ptable = flash_get_ptable();
1512 if (ptable == NULL)
1513 {
1514 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1515 return;
1516 }
1517
1518 ptn = ptable_find(ptable, "devinfo");
1519 if (ptn == NULL)
1520 {
1521 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1522 return;
1523 }
1524
1525 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1526 {
1527 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1528 return;
1529 }
1530
1531 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1532 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001533 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1534 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001535 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001536 write_device_info_flash(info);
1537 }
1538 memcpy(dev, info, sizeof(device_info));
1539}
1540
1541void write_device_info(device_info *dev)
1542{
1543 if(target_is_emmc_boot())
1544 {
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001545 struct device_info *info = (void*) info_buf;
1546 memcpy(info, dev, sizeof(struct device_info));
1547
1548#if USE_RPMB_FOR_DEVINFO
1549 if (is_secure_boot_enable())
1550 write_device_info_rpmb((void*) info, mmc_get_device_blocksize());
1551#else
1552 write_device_info_mmc(info);
1553#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001554 }
1555 else
1556 {
1557 write_device_info_flash(dev);
1558 }
1559}
1560
1561void read_device_info(device_info *dev)
1562{
1563 if(target_is_emmc_boot())
1564 {
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001565 struct device_info *info = (void*) info_buf;
1566
1567#if USE_RPMB_FOR_DEVINFO
1568 if (is_secure_boot_enable())
1569 read_device_info_rpmb((void*) info, mmc_get_device_blocksize());
1570#else
1571 read_device_info_mmc(info);
1572#endif
1573
1574 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1575 {
1576 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1577 if (is_secure_boot_enable())
1578 info->is_unlocked = 0;
1579 else
1580 info->is_unlocked = 1;
1581 info->is_verified = 0;
1582 info->is_tampered = 0;
1583 info->charger_screen_enabled = 0;
1584
1585 write_device_info(info);
1586 }
1587 memcpy(dev, info, sizeof(device_info));
Shashank Mittal162244e2011-08-08 19:01:25 -07001588 }
1589 else
1590 {
1591 read_device_info_flash(dev);
1592 }
1593}
1594
1595void reset_device_info()
1596{
1597 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001598 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001599 write_device_info(&device);
1600}
1601
1602void set_device_root()
1603{
1604 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001605 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001606 write_device_info(&device);
1607}
1608
Amol Jadicb524072012-08-09 16:40:18 -07001609#if DEVICE_TREE
1610int copy_dtb(uint8_t *boot_image_start)
1611{
1612 uint32 dt_image_offset = 0;
1613 uint32_t n;
1614 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001615 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001616 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001617
1618 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1619
Amol Jadicb524072012-08-09 16:40:18 -07001620 if(hdr->dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001621 /* add kernel offset */
1622 dt_image_offset += page_size;
1623 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1624 dt_image_offset += n;
1625
1626 /* add ramdisk offset */
1627 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1628 dt_image_offset += n;
1629
1630 /* add second offset */
1631 if(hdr->second_size != 0) {
1632 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1633 dt_image_offset += n;
1634 }
1635
1636 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001637 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001638
Deepa Dinamani19648b42013-09-05 17:05:55 -07001639 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001640 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1641 return -1;
1642 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001643 /* Find index of device tree within device tree table */
1644 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001645 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1646 return -1;
1647 }
1648
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001649 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001650 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001651 {
1652 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1653 return -1;
1654 }
1655
Amol Jadicb524072012-08-09 16:40:18 -07001656 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001657 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001658 boot_image_start + dt_image_offset + dt_entry.offset,
1659 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001660 } else
1661 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001662
1663 /* Everything looks fine. Return success. */
1664 return 0;
1665}
1666#endif
1667
Brian Swetland9c4c0752009-01-25 16:23:50 -08001668void cmd_boot(const char *arg, void *data, unsigned sz)
1669{
1670 unsigned kernel_actual;
1671 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001672 uint32_t image_actual;
1673 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001674 uint32_t sig_actual = SIGNATURE_SIZE;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001675 struct boot_img_hdr *hdr = NULL;
1676 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001677 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001678 int ret = 0;
1679 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001680 unsigned int out_len = 0;
1681 unsigned int out_avai_len = 0;
1682 unsigned char *out_addr = NULL;
1683 uint32_t dtb_offset = 0;
1684 unsigned char *kernel_start_addr = NULL;
1685 unsigned int kernel_size = 0;
1686
Brian Swetland9c4c0752009-01-25 16:23:50 -08001687
Amit Blay6281ebc2015-01-11 14:44:08 +02001688#ifdef MDTP_SUPPORT
1689 /* Go through Firmware Lock verification before continue with boot process */
1690 mdtp_fwlock_verify_lock();
Reut Zysman18411272015-02-09 13:47:27 +02001691 display_image_on_screen();
Amit Blay6281ebc2015-01-11 14:44:08 +02001692#endif /* MDTP_SUPPORT */
1693
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001694#if VERIFIED_BOOT
1695 if(!device.is_unlocked)
1696 {
1697 fastboot_fail("unlock device to use this command");
1698 return;
1699 }
1700#endif
1701
Brian Swetland9c4c0752009-01-25 16:23:50 -08001702 if (sz < sizeof(hdr)) {
1703 fastboot_fail("invalid bootimage header");
1704 return;
1705 }
1706
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001707 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001708
1709 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001710 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001711
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001712 if(target_is_emmc_boot() && hdr->page_size) {
1713 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001714 page_mask = page_size - 1;
1715 }
1716
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001717 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1718 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001719#if DEVICE_TREE
1720 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1721#endif
1722
1723 image_actual = ADD_OF(page_size, kernel_actual);
1724 image_actual = ADD_OF(image_actual, ramdisk_actual);
1725 image_actual = ADD_OF(image_actual, dt_actual);
1726
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001727 if (target_use_signed_kernel() && (!device.is_unlocked))
1728 image_actual = ADD_OF(image_actual, sig_actual);
1729
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001730 /* sz should have atleast raw boot image */
1731 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001732 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001733 return;
1734 }
1735
1736 /* Verify the boot image
1737 * device & page_size are initialized in aboot_init
1738 */
1739 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001740 /* Pass size excluding signature size, otherwise we would try to
1741 * access signature beyond its length
1742 */
1743 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001744
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001745 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001746 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1747 * If not, continue booting.
1748 */
1749 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
1750 {
1751 out_addr = (unsigned char *)target_get_scratch_address();
1752 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
1753 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
1754 dprintf(INFO, "decompress image start\n");
1755 ret = decompress((unsigned char *)(ptr + page_size),
1756 hdr->kernel_size, out_addr, out_avai_len,
1757 &dtb_offset, &out_len);
1758 if (ret)
1759 {
1760 dprintf(INFO, "decompress image failed!!!\n");
1761 ASSERT(0);
1762 }
1763
1764 dprintf(INFO, "decompressed image finished.\n");
1765 kptr = (struct kernel64_hdr *)out_addr;
1766 kernel_start_addr = out_addr;
1767 kernel_size = out_len;
1768 } else {
1769 kptr = (struct kernel64_hdr*)((char *)data + page_size);
1770 kernel_start_addr = (unsigned char *)((char *)data + page_size);
1771 kernel_size = hdr->kernel_size;
1772 }
1773
1774 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001775 * Update the kernel/ramdisk/tags address if the boot image header
1776 * has default values, these default values come from mkbootimg when
1777 * the boot image is flashed using fastboot flash:raw
1778 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001779 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001780
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001781 /* Get virtual addresses since the hdr saves physical addresses. */
1782 hdr->kernel_addr = VA(hdr->kernel_addr);
1783 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1784 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001785
Matthew Qinbb7923d2015-02-09 10:56:09 +08001786 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001787 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001788 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001789 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1790 {
1791 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001792 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001793 }
1794
Amol Jadicb524072012-08-09 16:40:18 -07001795#if DEVICE_TREE
1796 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001797 ret = copy_dtb(data);
1798
1799 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001800#else
1801 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1802 {
1803 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001804 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001805 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001806#endif
1807
1808 /* Load ramdisk & kernel */
1809 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08001810 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001811
1812#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08001813 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1814 {
1815 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1816 return;
1817 }
1818
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001819 /*
1820 * If dtb is not found look for appended DTB in the kernel.
1821 * If appended dev tree is found, update the atags with
1822 * memory address to the DTB appended location on RAM.
1823 * Else update with the atags address in the kernel header
1824 */
1825 if (!dtb_copied) {
1826 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001827 dtb = dev_tree_appended((void*)(ptr + page_size),
1828 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07001829 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001830 if (!dtb) {
1831 fastboot_fail("dtb not found");
1832 return;
1833 }
Amol Jadicb524072012-08-09 16:40:18 -07001834 }
1835#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001836
1837 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001838 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001839
Dima Zavin77e41f32013-03-06 16:10:43 -08001840 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001841 (const char*) hdr->cmdline, board_machtype(),
1842 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001843}
1844
Sridhar Parasurame94e8152014-10-24 14:06:03 -07001845void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08001846{
1847 struct ptentry *ptn;
1848 struct ptable *ptable;
1849
1850 ptable = flash_get_ptable();
1851 if (ptable == NULL) {
1852 fastboot_fail("partition table doesn't exist");
1853 return;
1854 }
1855
1856 ptn = ptable_find(ptable, arg);
1857 if (ptn == NULL) {
1858 fastboot_fail("unknown partition name");
1859 return;
1860 }
1861
1862 if (flash_erase(ptn)) {
1863 fastboot_fail("failed to erase partition");
1864 return;
1865 }
1866 fastboot_okay("");
1867}
1868
Bikas Gurungd48bd242010-09-04 19:54:32 -07001869
1870void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1871{
1872 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001873 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001874 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001875 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001876
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001877#if VERIFIED_BOOT
1878 if(!strcmp(arg, KEYSTORE_PTN_NAME))
1879 {
1880 if(!device.is_unlocked)
1881 {
1882 fastboot_fail("unlock device to erase keystore");
1883 return;
1884 }
1885 }
1886#endif
1887
Kinson Chikf1a43512011-07-14 11:28:39 -07001888 index = partition_get_index(arg);
1889 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001890 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001891
Kinson Chikf1a43512011-07-14 11:28:39 -07001892 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001893 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001894 return;
1895 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001896
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001897 lun = partition_get_lun(index);
1898 mmc_set_lun(lun);
1899
Sridhar Parasuramd7957122015-02-27 11:33:40 -08001900 if (platform_boot_dev_isemmc())
1901 {
1902 if (mmc_erase_card(ptn, size)) {
1903 fastboot_fail("failed to erase partition\n");
1904 return;
1905 }
1906 } else {
1907 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
1908 size = partition_get_size(index);
1909 if (size > DEFAULT_ERASE_SIZE)
1910 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08001911
Sridhar Parasuramd7957122015-02-27 11:33:40 -08001912 /* Simple inefficient version of erase. Just writing
1913 0 in first several blocks */
1914 if (mmc_write(ptn , size, (unsigned int *)out)) {
1915 fastboot_fail("failed to erase partition");
1916 return;
1917 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07001918 }
1919 fastboot_okay("");
1920}
1921
Sridhar Parasurame94e8152014-10-24 14:06:03 -07001922void cmd_erase(const char *arg, void *data, unsigned sz)
1923{
1924 if(target_is_emmc_boot())
1925 cmd_erase_mmc(arg, data, sz);
1926 else
1927 cmd_erase_nand(arg, data, sz);
1928}
Bikas Gurungd48bd242010-09-04 19:54:32 -07001929
Ajay Dudani5c761132011-04-07 20:19:04 -07001930void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001931{
1932 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001933 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001934 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001935 char *token = NULL;
1936 char *pname = NULL;
1937 uint8_t lun = 0;
1938 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001939
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301940 token = strtok((char *)arg, ":");
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001941 pname = token;
1942 token = strtok(NULL, ":");
1943 if(token)
1944 {
1945 lun = atoi(token);
1946 mmc_set_lun(lun);
1947 lun_set = true;
1948 }
1949
Mao Jinlong226f33a2014-07-04 17:24:10 +08001950 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07001951 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08001952 if (!strcmp(pname, "partition"))
1953 {
1954 dprintf(INFO, "Attempt to write partition image.\n");
1955 if (write_partition(sz, (unsigned char *) data)) {
1956 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07001957 return;
1958 }
1959 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08001960 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001961 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001962#if VERIFIED_BOOT
1963 if(!strcmp(pname, KEYSTORE_PTN_NAME))
1964 {
1965 if(!device.is_unlocked)
1966 {
1967 fastboot_fail("unlock device to flash keystore");
1968 return;
1969 }
1970 if(!boot_verify_validate_keystore((unsigned char *)data))
1971 {
1972 fastboot_fail("image is not a keystore file");
1973 return;
1974 }
1975 }
1976#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08001977 index = partition_get_index(pname);
1978 ptn = partition_get_offset(index);
1979 if(ptn == 0) {
1980 fastboot_fail("partition table doesn't exist");
1981 return;
1982 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001983
Mao Jinlong226f33a2014-07-04 17:24:10 +08001984 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
1985 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1986 fastboot_fail("image is not a boot image");
1987 return;
1988 }
1989 }
1990
1991 if(!lun_set)
1992 {
1993 lun = partition_get_lun(index);
1994 mmc_set_lun(lun);
1995 }
1996
1997 size = partition_get_size(index);
1998 if (ROUND_TO_PAGE(sz,511) > size) {
1999 fastboot_fail("size too large");
2000 return;
2001 }
2002 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2003 fastboot_fail("flash write failure");
2004 return;
2005 }
Greg Grisco6e754772011-06-23 12:19:39 -07002006 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002007 }
2008 fastboot_okay("");
2009 return;
2010}
2011
Ajay Dudanide984792015-03-02 09:57:41 -08002012void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2013{
2014 int i, images;
2015 meta_header_t *meta_header;
2016 img_header_entry_t *img_header_entry;
2017
2018 meta_header = (meta_header_t*) data;
2019 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2020
2021 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2022
2023 for (i=0; i<images; i++) {
2024
2025 if((img_header_entry[i].ptn_name == NULL) ||
2026 (img_header_entry[i].start_offset == 0) ||
2027 (img_header_entry[i].size == 0))
2028 break;
2029
2030 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2031 (void *) data + img_header_entry[i].start_offset,
2032 img_header_entry[i].size);
2033 }
2034
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002035 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2036 {
2037 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2038 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2039 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2040 }
2041 else
2042 {
2043 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2044 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2045 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2046 }
2047
2048 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002049 fastboot_okay("");
2050 return;
2051}
2052
Ajay Dudani5c761132011-04-07 20:19:04 -07002053void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2054{
2055 unsigned int chunk;
2056 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002057 uint32_t *fill_buf = NULL;
2058 uint32_t fill_val;
2059 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002060 sparse_header_t *sparse_header;
2061 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002062 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002063 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302064 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002065 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302066 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002067 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002068
Kinson Chikf1a43512011-07-14 11:28:39 -07002069 index = partition_get_index(arg);
2070 ptn = partition_get_offset(index);
2071 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002072 fastboot_fail("partition table doesn't exist");
2073 return;
2074 }
2075
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302076 size = partition_get_size(index);
2077 if (ROUND_TO_PAGE(sz,511) > size) {
2078 fastboot_fail("size too large");
2079 return;
2080 }
2081
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002082 lun = partition_get_lun(index);
2083 mmc_set_lun(lun);
2084
Ajay Dudani5c761132011-04-07 20:19:04 -07002085 /* Read and skip over sparse image header */
2086 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08002087 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
2088 fastboot_fail("size too large");
2089 return;
2090 }
2091
Ajay Dudani5c761132011-04-07 20:19:04 -07002092 data += sparse_header->file_hdr_sz;
2093 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
2094 {
2095 /* Skip the remaining bytes in a header that is longer than
2096 * we expected.
2097 */
2098 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
2099 }
2100
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002101 dprintf (SPEW, "=== Sparse Image Header ===\n");
2102 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2103 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2104 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2105 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2106 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2107 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2108 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2109 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002110
2111 /* Start processing chunks */
2112 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2113 {
2114 /* Read and skip over chunk header */
2115 chunk_header = (chunk_header_t *) data;
2116 data += sizeof(chunk_header_t);
2117
2118 dprintf (SPEW, "=== Chunk Header ===\n");
2119 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2120 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2121 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2122
2123 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
2124 {
2125 /* Skip the remaining bytes in a header that is longer than
2126 * we expected.
2127 */
2128 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
2129 }
2130
2131 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
2132 switch (chunk_header->chunk_type)
2133 {
2134 case CHUNK_TYPE_RAW:
2135 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2136 chunk_data_sz))
2137 {
2138 fastboot_fail("Bogus chunk size for chunk type Raw");
2139 return;
2140 }
2141
Ajay Dudaniab18f022011-05-12 14:39:22 -07002142 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2143 chunk_data_sz,
2144 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002145 {
2146 fastboot_fail("flash write failure");
2147 return;
2148 }
2149 total_blocks += chunk_header->chunk_sz;
2150 data += chunk_data_sz;
2151 break;
2152
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002153 case CHUNK_TYPE_FILL:
2154 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2155 sizeof(uint32_t)))
2156 {
2157 fastboot_fail("Bogus chunk size for chunk type FILL");
2158 return;
2159 }
2160
2161 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2162 if (!fill_buf)
2163 {
2164 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2165 return;
2166 }
2167
2168 fill_val = *(uint32_t *)data;
2169 data = (char *) data + sizeof(uint32_t);
2170 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
2171
2172 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2173 {
2174 fill_buf[i] = fill_val;
2175 }
2176
2177 for (i = 0; i < chunk_blk_cnt; i++)
2178 {
2179 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2180 sparse_header->blk_sz,
2181 fill_buf))
2182 {
2183 fastboot_fail("flash write failure");
2184 free(fill_buf);
2185 return;
2186 }
2187
2188 total_blocks++;
2189 }
2190
2191 free(fill_buf);
2192 break;
2193
Ajay Dudani5c761132011-04-07 20:19:04 -07002194 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002195 total_blocks += chunk_header->chunk_sz;
2196 break;
2197
Ajay Dudani5c761132011-04-07 20:19:04 -07002198 case CHUNK_TYPE_CRC:
2199 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2200 {
2201 fastboot_fail("Bogus chunk size for chunk type Dont Care");
2202 return;
2203 }
2204 total_blocks += chunk_header->chunk_sz;
2205 data += chunk_data_sz;
2206 break;
2207
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002208 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002209 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002210 fastboot_fail("Unknown chunk type");
2211 return;
2212 }
2213 }
2214
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002215 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2216 total_blocks, sparse_header->total_blks);
2217
2218 if(total_blocks != sparse_header->total_blks)
2219 {
2220 fastboot_fail("sparse image write failure");
2221 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002222
2223 fastboot_okay("");
2224 return;
2225}
2226
2227void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2228{
2229 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002230 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002231
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002232#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002233 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2234 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002235 int ret=0;
2236 uint32 major_version=0;
2237 uint32 minor_version=0;
2238
2239 ret = scm_svc_version(&major_version,&minor_version);
2240 if(!ret)
2241 {
2242 if(major_version >= 2)
2243 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002244 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002245 {
2246 ret = encrypt_scm((uint32 **) &data, &sz);
2247 if (ret != 0) {
2248 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2249 return;
2250 }
2251
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002252 /* Protect only for SSD */
2253 if (!strcmp(arg, "ssd")) {
2254 ret = scm_protect_keystore((uint32 *) data, sz);
2255 if (ret != 0) {
2256 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2257 return;
2258 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002259 }
2260 }
2261 else
2262 {
2263 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2264 if(ret != 0)
2265 {
2266 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2267 return;
2268 }
2269 }
2270 }
2271 else
2272 {
2273 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2274 magic_number[1] == DECRYPT_MAGIC_1)
2275 {
2276 ret = decrypt_scm((uint32 **) &data, &sz);
2277 if (ret != 0) {
2278 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2279 return;
2280 }
2281 }
2282 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2283 magic_number[1] == ENCRYPT_MAGIC_1)
2284 {
2285 ret = encrypt_scm((uint32 **) &data, &sz);
2286 if (ret != 0) {
2287 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2288 return;
2289 }
2290 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002291 }
2292 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002293 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002294 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002295 dprintf(CRITICAL,"INVALID SVC Version\n");
2296 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002297 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002298#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002299
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002300#if VERIFIED_BOOT
2301 if(!device.is_unlocked && !device.is_verified)
2302 {
2303 fastboot_fail("device is locked. Cannot flash images");
2304 return;
2305 }
2306 if(!device.is_unlocked && device.is_verified)
2307 {
2308 if(!boot_verify_flash_allowed(arg))
2309 {
2310 fastboot_fail("cannot flash this partition in verified state");
2311 return;
2312 }
2313 }
2314#endif
2315
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002316 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002317 meta_header = (meta_header_t *) data;
2318 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002319 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002320 else if (meta_header->magic == META_HEADER_MAGIC)
2321 cmd_flash_meta_img(arg, data, sz);
2322 else
2323 cmd_flash_mmc_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002324 return;
2325}
2326
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002327void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002328{
2329 struct ptentry *ptn;
2330 struct ptable *ptable;
2331 unsigned extra = 0;
2332
2333 ptable = flash_get_ptable();
2334 if (ptable == NULL) {
2335 fastboot_fail("partition table doesn't exist");
2336 return;
2337 }
2338
2339 ptn = ptable_find(ptable, arg);
2340 if (ptn == NULL) {
2341 fastboot_fail("unknown partition name");
2342 return;
2343 }
2344
2345 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2346 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2347 fastboot_fail("image is not a boot image");
2348 return;
2349 }
2350 }
2351
Amol Jadi5c61a952012-05-04 17:05:35 -07002352 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002353 || !strcmp(ptn->name, "userdata")
2354 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002355 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002356 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002357 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002358 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002359 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002360
2361 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002362 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2363 if (flash_ubi_img(ptn, data, sz)) {
2364 fastboot_fail("flash write failure");
2365 return;
2366 }
2367 } else {
2368 if (flash_write(ptn, extra, data, sz)) {
2369 fastboot_fail("flash write failure");
2370 return;
2371 }
Dima Zavin214cc642009-01-26 11:16:21 -08002372 }
2373 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2374 fastboot_okay("");
2375}
2376
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002377void cmd_flash(const char *arg, void *data, unsigned sz)
2378{
2379 if(target_is_emmc_boot())
2380 cmd_flash_mmc(arg, data, sz);
2381 else
2382 cmd_flash_nand(arg, data, sz);
2383}
2384
Dima Zavin214cc642009-01-26 11:16:21 -08002385void cmd_continue(const char *arg, void *data, unsigned sz)
2386{
2387 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002388 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002389
Amit Blay6281ebc2015-01-11 14:44:08 +02002390#ifdef MDTP_SUPPORT
2391 /* Go through Firmware Lock verification before continue with boot process */
2392 mdtp_fwlock_verify_lock();
Reut Zysman18411272015-02-09 13:47:27 +02002393 display_image_on_screen();
Amit Blay6281ebc2015-01-11 14:44:08 +02002394#endif /* MDTP_SUPPORT */
2395
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002396 if (target_is_emmc_boot())
2397 {
2398 boot_linux_from_mmc();
2399 }
2400 else
2401 {
2402 boot_linux_from_flash();
2403 }
Dima Zavin214cc642009-01-26 11:16:21 -08002404}
2405
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002406void cmd_reboot(const char *arg, void *data, unsigned sz)
2407{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002408 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002409 fastboot_okay("");
2410 reboot_device(0);
2411}
2412
2413void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2414{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002415 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002416 fastboot_okay("");
2417 reboot_device(FASTBOOT_MODE);
2418}
2419
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002420void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2421{
2422 dprintf(INFO, "Enabling charger screen check\n");
2423 device.charger_screen_enabled = 1;
2424 write_device_info(&device);
2425 fastboot_okay("");
2426}
2427
2428void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2429{
2430 dprintf(INFO, "Disabling charger screen check\n");
2431 device.charger_screen_enabled = 0;
2432 write_device_info(&device);
2433 fastboot_okay("");
2434}
2435
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302436void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2437{
2438 dprintf(INFO, "Selecting display panel %s\n", arg);
2439 if (arg)
2440 strlcpy(device.display_panel, arg,
2441 sizeof(device.display_panel));
2442 write_device_info(&device);
2443 fastboot_okay("");
2444}
2445
Shashank Mittal162244e2011-08-08 19:01:25 -07002446void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2447{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002448 /* TODO: Wipe user data */
2449 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002450 {
2451 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002452 device.is_verified = 0;
2453 write_device_info(&device);
2454 }
2455 fastboot_okay("");
2456}
2457
2458void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2459{
2460 /* TODO: Wipe user data */
2461 if(device.is_unlocked || device.is_verified)
2462 {
2463 device.is_unlocked = 0;
2464 device.is_verified = 0;
2465 write_device_info(&device);
2466 }
2467 fastboot_okay("");
2468}
2469
2470void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2471{
2472 /* TODO: Wipe user data */
2473 if(device.is_unlocked || !device.is_verified)
2474 {
2475 device.is_unlocked = 0;
2476 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002477 write_device_info(&device);
2478 }
2479 fastboot_okay("");
2480}
2481
Shashank Mittala0032282011-08-26 14:50:11 -07002482void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2483{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302484 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002485 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002486 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002487 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2488 fastboot_info(response);
2489 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002490 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302491 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2492 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002493 fastboot_okay("");
2494}
2495
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002496void cmd_preflash(const char *arg, void *data, unsigned sz)
2497{
2498 fastboot_okay("");
2499}
2500
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302501struct fbimage* splash_screen_flash();
2502
2503int splash_screen_check_header(struct fbimage *logo)
2504{
2505 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2506 return -1;
2507 if (logo->header.width == 0 || logo->header.height == 0)
2508 return -1;
2509 return 0;
2510}
2511
2512struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002513{
2514 struct ptentry *ptn;
2515 struct ptable *ptable;
2516 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002517 struct fbimage *logo = NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002518
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302519
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002520 logo = (struct fbimage *) malloc(ROUNDUP(page_size, sizeof(struct fbimage)));
2521 ASSERT(logo);
2522
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302523 ptable = flash_get_ptable();
2524 if (ptable == NULL) {
2525 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002526 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302527 }
2528 ptn = ptable_find(ptable, "splash");
2529 if (ptn == NULL) {
2530 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002531 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302532 }
2533
2534 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2535 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002536 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302537 }
2538
2539 if (splash_screen_check_header(logo)) {
2540 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002541 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302542 }
2543
2544 fb_display = fbcon_display();
2545 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302546 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2547 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back to default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002548 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002549 }
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302550 uint8_t *base = (uint8_t *) fb_display->base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302551 if (flash_read(ptn + sizeof(logo->header), 0,
2552 base,
2553 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2554 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302555 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002556 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002557 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302558 logo->image = base;
2559 }
2560
2561 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002562
2563err:
2564 free(logo);
2565 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302566}
2567
2568struct fbimage* splash_screen_mmc()
2569{
2570 int index = INVALID_PTN;
2571 unsigned long long ptn = 0;
2572 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002573 struct fbimage *logo = NULL;
Channagoud Kadabida117392014-11-24 11:01:12 -08002574 uint32_t blocksize;
2575 uint32_t readsize;
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002576 uint32_t logosize;
Channagoud Kadabida117392014-11-24 11:01:12 -08002577 uint32_t ptn_size;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302578
2579 index = partition_get_index("splash");
2580 if (index == 0) {
2581 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2582 return NULL;
2583 }
2584
2585 ptn = partition_get_offset(index);
2586 if (ptn == 0) {
2587 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2588 return NULL;
2589 }
2590
Channagoud Kadabida117392014-11-24 11:01:12 -08002591 ptn_size = partition_get_size(index);
2592 blocksize = mmc_get_device_blocksize();
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002593 logosize = ROUNDUP(sizeof(logo->header), blocksize);
Channagoud Kadabida117392014-11-24 11:01:12 -08002594
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002595 logo = (struct fbimage *)memalign(CACHE_LINE, ROUNDUP(logosize, CACHE_LINE));
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002596 ASSERT(logo);
2597
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002598 if (mmc_read(ptn, (uint32_t *) logo, logosize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302599 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002600 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302601 }
2602
2603 if (splash_screen_check_header(logo)) {
2604 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002605 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302606 }
2607
2608 fb_display = fbcon_display();
2609 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302610 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2611 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002612 goto err;
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302613 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302614 uint8_t *base = (uint8_t *) fb_display->base;
Channagoud Kadabida117392014-11-24 11:01:12 -08002615 readsize = ROUNDUP((logo->header.width * logo->header.height * fb_display->bpp/8), blocksize);
2616
2617 if (readsize > ptn_size)
2618 {
2619 dprintf(CRITICAL, "@%d:Invalid logo header readsize:%u exceeds ptn_size:%u\n", __LINE__, readsize,ptn_size);
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002620 goto err;
Channagoud Kadabida117392014-11-24 11:01:12 -08002621 }
2622
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002623 if (mmc_read(ptn + logosize,(uint32_t *)base, readsize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302624 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302625 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002626 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302627 }
2628
2629 logo->image = base;
2630 }
2631
2632 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002633
2634err:
2635 free(logo);
2636 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302637}
2638
2639
2640struct fbimage* fetch_image_from_partition()
2641{
2642 if (target_is_emmc_boot()) {
2643 return splash_screen_mmc();
2644 } else {
2645 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002646 }
2647}
2648
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002649/* Get the size from partiton name */
2650static void get_partition_size(const char *arg, char *response)
2651{
2652 uint64_t ptn = 0;
2653 uint64_t size;
2654 int index = INVALID_PTN;
2655
2656 index = partition_get_index(arg);
2657
2658 if (index == INVALID_PTN)
2659 {
2660 dprintf(CRITICAL, "Invalid partition index\n");
2661 return;
2662 }
2663
2664 ptn = partition_get_offset(index);
2665
2666 if(!ptn)
2667 {
2668 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2669 return;
2670 }
2671
2672 size = partition_get_size(index);
2673
2674 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2675 return;
2676}
2677
2678/*
2679 * Publish the partition type & size info
2680 * fastboot getvar will publish the required information.
2681 * fastboot getvar partition_size:<partition_name>: partition size in hex
2682 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2683 */
2684static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2685{
2686 uint8_t i;
2687
2688 for (i = 0; i < num_parts; i++) {
2689 get_partition_size(info[i].part_name, info[i].size_response);
2690
2691 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2692 {
2693 dprintf(CRITICAL, "partition size name truncated\n");
2694 return;
2695 }
2696 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2697 {
2698 dprintf(CRITICAL, "partition type name truncated\n");
2699 return;
2700 }
2701
2702 /* publish partition size & type info */
2703 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2704 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2705 }
2706}
2707
Amol Jadi5edf3552013-07-23 14:15:34 -07002708/* register commands and variables for fastboot */
2709void aboot_fastboot_register_commands(void)
2710{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002711 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07002712
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002713 struct fastboot_cmd_desc cmd_list[] = {
2714 /* By default the enabled list is empty. */
2715 {"", NULL},
2716 /* move commands enclosed within the below ifndef to here
2717 * if they need to be enabled in user build.
2718 */
2719#ifndef DISABLE_FASTBOOT_CMDS
2720 /* Register the following commands only for non-user builds */
2721 {"flash:", cmd_flash},
2722 {"erase:", cmd_erase},
2723 {"boot", cmd_boot},
2724 {"continue", cmd_continue},
2725 {"reboot", cmd_reboot},
2726 {"reboot-bootloader", cmd_reboot_bootloader},
2727 {"oem unlock", cmd_oem_unlock},
2728 {"oem lock", cmd_oem_lock},
2729 {"oem verified", cmd_oem_verified},
2730 {"oem device-info", cmd_oem_devinfo},
2731 {"preflash", cmd_preflash},
2732 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
2733 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08002734 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002735#endif
2736 };
2737
2738 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
2739 for (i = 1; i < fastboot_cmds_count; i++)
2740 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
2741
Amol Jadi5edf3552013-07-23 14:15:34 -07002742 /* publish variables and their values */
2743 fastboot_publish("product", TARGET(BOARD));
2744 fastboot_publish("kernel", "lk");
2745 fastboot_publish("serialno", sn_buf);
2746
2747 /*
2748 * partition info is supported only for emmc partitions
2749 * Calling this for NAND prints some error messages which
2750 * is harmless but misleading. Avoid calling this for NAND
2751 * devices.
2752 */
2753 if (target_is_emmc_boot())
2754 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2755
2756 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002757 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2758 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002759 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002760 /* Is the charger screen check enabled */
2761 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2762 device.charger_screen_enabled);
2763 fastboot_publish("charger-screen-enabled",
2764 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302765 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2766 device.display_panel);
2767 fastboot_publish("display-panel",
2768 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002769 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
2770 fastboot_publish("version-baseband", (const char *) device.radio_version);
Amol Jadi5edf3552013-07-23 14:15:34 -07002771}
2772
Brian Swetland9c4c0752009-01-25 16:23:50 -08002773void aboot_init(const struct app_descriptor *app)
2774{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002775 unsigned reboot_mode = 0;
lijuang72875802015-02-10 10:35:12 +08002776 unsigned hard_reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002777 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002778
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002779 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002780 if (target_is_emmc_boot())
2781 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002782 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002783 page_mask = page_size - 1;
2784 }
2785 else
2786 {
2787 page_size = flash_page_size();
2788 page_mask = page_size - 1;
2789 }
2790
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002791 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2792
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002793 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002794
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002795 /* Display splash screen if enabled */
2796#if DISPLAY_SPLASH_SCREEN
2797 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302798 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002799 dprintf(SPEW, "Display Init: Done\n");
2800#endif
2801
2802
Greg Griscod6250552011-06-29 14:40:23 -07002803 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002804 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002805
Dhaval Patel223ec952013-07-18 14:49:44 -07002806 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2807
Matthew Qindefd5562014-07-11 18:02:40 +08002808 /*
2809 * Check power off reason if user force reset,
2810 * if yes phone will do normal boot.
2811 */
2812 if (is_user_force_reset())
2813 goto normal_boot;
2814
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002815 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002816 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002817 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002818 dprintf(ALWAYS,"dload mode key sequence detected\n");
2819 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002820 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002821 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002822 }
2823 else
2824 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302825 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002826 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002827 }
2828 boot_into_fastboot = true;
2829 }
2830 if (!boot_into_fastboot)
2831 {
2832 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2833 boot_into_recovery = 1;
2834 if (!boot_into_recovery &&
2835 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002836 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002837 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002838 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002839 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002840 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002841 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002842
Ajay Dudani77421292010-10-27 19:34:06 -07002843 reboot_mode = check_reboot_mode();
lijuang72875802015-02-10 10:35:12 +08002844 hard_reboot_mode = check_hard_reboot_mode();
2845 if (reboot_mode == RECOVERY_MODE ||
2846 hard_reboot_mode == RECOVERY_HARD_RESET_MODE) {
Ajay Dudani77421292010-10-27 19:34:06 -07002847 boot_into_recovery = 1;
lijuang72875802015-02-10 10:35:12 +08002848 } else if(reboot_mode == FASTBOOT_MODE ||
2849 hard_reboot_mode == FASTBOOT_HARD_RESET_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002850 boot_into_fastboot = true;
lijuang72875802015-02-10 10:35:12 +08002851 } else if(reboot_mode == ALARM_BOOT ||
2852 hard_reboot_mode == RTC_HARD_RESET_MODE) {
Matthew Qind886f3c2014-01-17 16:52:01 +08002853 boot_reason_alarm = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002854 }
2855
Matthew Qindefd5562014-07-11 18:02:40 +08002856normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03002857 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002858 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002859 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002860 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002861 if(emmc_recovery_init())
2862 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2863 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002864 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002865 if((device.is_unlocked) || (device.is_tampered))
2866 {
2867 #ifdef TZ_TAMPER_FUSE
2868 set_tamper_fuse_cmd();
2869 #endif
2870 #if USE_PCOM_SECBOOT
2871 set_tamper_flag(device.is_tampered);
2872 #endif
2873 }
Shashank Mittala0032282011-08-26 14:50:11 -07002874 }
Amit Blay6281ebc2015-01-11 14:44:08 +02002875
2876#ifdef MDTP_SUPPORT
2877 /* Go through Firmware Lock verification before continue with boot process */
2878 mdtp_fwlock_verify_lock();
Reut Zysman18411272015-02-09 13:47:27 +02002879 display_image_on_screen();
Amit Blay6281ebc2015-01-11 14:44:08 +02002880#endif /* MDTP_SUPPORT */
2881
Pavel Nedev5d91d412013-04-29 11:34:24 +03002882 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002883 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002884 else
2885 {
2886 recovery_init();
2887 #if USE_PCOM_SECBOOT
2888 if((device.is_unlocked) || (device.is_tampered))
2889 set_tamper_flag(device.is_tampered);
2890 #endif
2891 boot_linux_from_flash();
2892 }
2893 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2894 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002895 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002896
Amol Jadi5edf3552013-07-23 14:15:34 -07002897 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002898
Amol Jadi5edf3552013-07-23 14:15:34 -07002899 /* register aboot specific fastboot commands */
2900 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002901
Amol Jadi5edf3552013-07-23 14:15:34 -07002902 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002903 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002904
2905 /* initialize and start fastboot */
2906 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002907}
2908
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002909uint32_t get_page_size()
2910{
2911 return page_size;
2912}
2913
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002914/*
2915 * Calculated and save hash (SHA256) for non-signed boot image.
2916 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002917 * @param image_addr - Boot image address
2918 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002919 *
2920 * @return int - 0 on success, negative value on failure.
2921 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002922static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002923{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002924 unsigned int digest[8];
2925#if IMAGE_VERIF_ALGO_SHA1
2926 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
2927#else
2928 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
2929#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002930
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002931 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002932 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002933
2934 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002935 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002936
2937 return 0;
2938}
2939
Brian Swetland9c4c0752009-01-25 16:23:50 -08002940APP_START(aboot)
2941 .init = aboot_init,
2942APP_END