blob: 9da4d6a106772a1b1913eb5d42f3e7ff7c7d3e7e [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08005 * Copyright (c) 2009-2015, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
43#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080044#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080045#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080046#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070047#include <target.h>
48#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070049#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070050#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070051#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070052#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070053#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030054#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070055#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070056#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070057#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053058#include <image_verify.h>
Dima Zavin214cc642009-01-26 11:16:21 -080059
Neeti Desai17379b82012-06-04 18:42:53 -070060#if DEVICE_TREE
61#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070062#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070063#endif
64
Shashank Mittalcd98d472011-08-02 14:29:24 -070065#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080066#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080067#include "bootimg.h"
68#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070069#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070070#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070071#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070072#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070073#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070074
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070075extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070076extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070077extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070078extern int get_target_boot_params(const char *cmdline, const char *part,
79 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070080
81void write_device_info_mmc(device_info *dev);
82void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -070083static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070084
Sridhar Parasurame94e8152014-10-24 14:06:03 -070085/* fastboot command function pointer */
86typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
87
88struct fastboot_cmd_desc {
89 char * name;
90 fastboot_cmd_fn cb;
91};
92
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070093#define EXPAND(NAME) #NAME
94#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070095
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080096#ifdef MEMBASE
97#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
98#else
David Ng183a7422009-12-07 14:55:21 -080099#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800100#endif
101
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700102#ifndef MEMSIZE
103#define MEMSIZE 1024*1024
104#endif
105
106#define MAX_TAGS_SIZE 1024
107
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800108#define RECOVERY_MODE 0x77665502
109#define FASTBOOT_MODE 0x77665500
Matthew Qind886f3c2014-01-17 16:52:01 +0800110#define ALARM_BOOT 0x77665503
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800111
Kun Liang2f1601a2013-08-12 16:29:54 +0800112/* make 4096 as default size to ensure EFS,EXT4's erasing */
113#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800114#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800115
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800116#define UBI_MAGIC "UBI#"
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700117#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800118#define UBI_MAGIC_SIZE 0x04
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700119#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800120
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800121#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
122
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700123#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
124
Smita Ghoshb4555da2014-09-18 14:14:56 -0700125#if UFS_SUPPORT || USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700126static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700127#else
David Ng183a7422009-12-07 14:55:21 -0800128static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700129#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800130static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300131static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800132static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800133static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800134static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700135static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300136static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800137
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800138static const char *baseband_apq = " androidboot.baseband=apq";
139static const char *baseband_msm = " androidboot.baseband=msm";
140static const char *baseband_csfb = " androidboot.baseband=csfb";
141static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700142static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800143static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700144static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800145static const char *baseband_dsda = " androidboot.baseband=dsda";
146static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800147static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800148static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800149
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700150static unsigned page_size = 0;
151static unsigned page_mask = 0;
152static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
153static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700154static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800155static bool boot_reason_alarm;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700156
Shashank Mittalcd98d472011-08-02 14:29:24 -0700157/* Assuming unauthorized kernel image by default */
158static int auth_kernel_img = 0;
159
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530160static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}};
Shashank Mittal162244e2011-08-08 19:01:25 -0700161
Dima Zavin42168f22009-01-30 11:52:22 -0800162struct atag_ptbl_entry
163{
164 char name[16];
165 unsigned offset;
166 unsigned size;
167 unsigned flags;
168};
169
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700170/*
171 * Partition info, required to be published
172 * for fastboot
173 */
174struct getvar_partition_info {
175 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
176 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
177 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
178 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
179 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
180};
181
182/*
183 * Right now, we are publishing the info for only
184 * three partitions
185 */
186struct getvar_partition_info part_info[] =
187{
188 { "system" , "partition-size:", "partition-type:", "", "ext4" },
189 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
190 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
191};
192
193char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700194char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800195char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700196char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530197char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700198
Greg Griscod2471ef2011-07-14 13:00:42 -0700199extern int emmc_recovery_init(void);
200
Kinson Chik0b1c8162011-08-31 16:31:57 -0700201#if NO_KEYPAD_DRIVER
202extern int fastboot_trigger(void);
203#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700204
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800205static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700206{
207 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700208#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800209 if (is_arm64)
210 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
211 else
212 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700213 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
214 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700215#endif
216}
217
Dima Zavin42168f22009-01-30 11:52:22 -0800218static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
219{
220 struct atag_ptbl_entry atag_ptn;
221
222 memcpy(atag_ptn.name, ptn->name, 16);
223 atag_ptn.name[15] = '\0';
224 atag_ptn.offset = ptn->start;
225 atag_ptn.size = ptn->length;
226 atag_ptn.flags = ptn->flags;
227 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
228 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
229}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800230
Neeti Desaie245d492012-06-01 12:52:13 -0700231unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800232{
David Ng183a7422009-12-07 14:55:21 -0800233 int cmdline_len = 0;
234 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800235 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700236 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800237 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300238 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700239 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700240 char *boot_dev_buf = NULL;
Dima Zavin42168f22009-01-30 11:52:22 -0800241
Brian Swetland9c4c0752009-01-25 16:23:50 -0800242 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800243 cmdline_len = strlen(cmdline);
244 have_cmdline = 1;
245 }
246 if (target_is_emmc_boot()) {
247 cmdline_len += strlen(emmc_cmdline);
Smita Ghoshb4555da2014-09-18 14:14:56 -0700248#if UFS_SUPPORT || USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700249 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
250 ASSERT(boot_dev_buf);
251 platform_boot_dev_cmdline(boot_dev_buf);
252 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700253#endif
David Ng183a7422009-12-07 14:55:21 -0800254 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800255
256 cmdline_len += strlen(usb_sn_cmdline);
257 cmdline_len += strlen(sn_buf);
258
Pavel Nedev5614d222013-06-17 18:01:02 +0300259 if (boot_into_recovery && gpt_exists)
260 cmdline_len += strlen(secondary_gpt_enable);
261
Pavel Nedev328ac822013-04-05 15:25:11 +0300262 if (boot_into_ffbm) {
263 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700264 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800265 /* reduce kernel console messages to speed-up boot */
266 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800267 } else if (boot_reason_alarm) {
268 cmdline_len += strlen(alarmboot_cmdline);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700269 } else if (device.charger_screen_enabled &&
270 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700271 pause_at_bootup = 1;
272 cmdline_len += strlen(battchg_pause);
273 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800274
Shashank Mittalcd98d472011-08-02 14:29:24 -0700275 if(target_use_signed_kernel() && auth_kernel_img) {
276 cmdline_len += strlen(auth_kernel);
277 }
278
Joonwoo Park61112782013-10-02 19:50:39 -0700279 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
280 "system",
281 target_boot_params,
282 sizeof(target_boot_params)) == 0) {
283 have_target_boot_params = 1;
284 cmdline_len += strlen(target_boot_params);
285 }
286
Ajay Dudanid04110c2011-01-17 23:55:07 -0800287 /* Determine correct androidboot.baseband to use */
288 switch(target_baseband())
289 {
290 case BASEBAND_APQ:
291 cmdline_len += strlen(baseband_apq);
292 break;
293
294 case BASEBAND_MSM:
295 cmdline_len += strlen(baseband_msm);
296 break;
297
298 case BASEBAND_CSFB:
299 cmdline_len += strlen(baseband_csfb);
300 break;
301
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800302 case BASEBAND_SVLTE2A:
303 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800304 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700305
306 case BASEBAND_MDM:
307 cmdline_len += strlen(baseband_mdm);
308 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700309
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800310 case BASEBAND_MDM2:
311 cmdline_len += strlen(baseband_mdm2);
312 break;
313
Amol Jadi5c61a952012-05-04 17:05:35 -0700314 case BASEBAND_SGLTE:
315 cmdline_len += strlen(baseband_sglte);
316 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530317
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800318 case BASEBAND_SGLTE2:
319 cmdline_len += strlen(baseband_sglte2);
320 break;
321
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530322 case BASEBAND_DSDA:
323 cmdline_len += strlen(baseband_dsda);
324 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800325
326 case BASEBAND_DSDA2:
327 cmdline_len += strlen(baseband_dsda2);
328 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800329 }
330
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800331 if (cmdline) {
332 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
333 target_display_panel_node(device.display_panel,
334 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
335 strlen(display_panel_buf)) {
336 cmdline_len += strlen(display_panel_buf);
337 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700338 }
339
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800340 if (target_warm_boot()) {
341 warm_boot = true;
342 cmdline_len += strlen(warmboot_cmdline);
343 }
344
David Ng183a7422009-12-07 14:55:21 -0800345 if (cmdline_len > 0) {
346 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800347 unsigned char *dst;
348
349 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
350 ASSERT(cmdline_final != NULL);
351 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700352
Amol Jadi168b7712012-03-06 16:15:00 -0800353 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800354 if (have_cmdline) {
355 src = cmdline;
356 while ((*dst++ = *src++));
357 }
358 if (target_is_emmc_boot()) {
359 src = emmc_cmdline;
360 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700361 have_cmdline = 1;
362 while ((*dst++ = *src++));
Smita Ghoshb4555da2014-09-18 14:14:56 -0700363#if UFS_SUPPORT || USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700364 src = boot_dev_buf;
365 if (have_cmdline) --dst;
366 while ((*dst++ = *src++));
367#endif
David Ngf773dde2010-07-26 19:55:08 -0700368 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800369
370 src = usb_sn_cmdline;
371 if (have_cmdline) --dst;
372 have_cmdline = 1;
373 while ((*dst++ = *src++));
374 src = sn_buf;
375 if (have_cmdline) --dst;
376 have_cmdline = 1;
377 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800378 if (warm_boot) {
379 if (have_cmdline) --dst;
380 src = warmboot_cmdline;
381 while ((*dst++ = *src++));
382 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800383
Pavel Nedev5614d222013-06-17 18:01:02 +0300384 if (boot_into_recovery && gpt_exists) {
385 src = secondary_gpt_enable;
386 if (have_cmdline) --dst;
387 while ((*dst++ = *src++));
388 }
389
Pavel Nedev328ac822013-04-05 15:25:11 +0300390 if (boot_into_ffbm) {
391 src = androidboot_mode;
392 if (have_cmdline) --dst;
393 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700394 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300395 if (have_cmdline) --dst;
396 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800397 src = loglevel;
398 if (have_cmdline) --dst;
399 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800400 } else if (boot_reason_alarm) {
401 src = alarmboot_cmdline;
402 if (have_cmdline) --dst;
403 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300404 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700405 src = battchg_pause;
406 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800407 while ((*dst++ = *src++));
408 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800409
Shashank Mittalcd98d472011-08-02 14:29:24 -0700410 if(target_use_signed_kernel() && auth_kernel_img) {
411 src = auth_kernel;
412 if (have_cmdline) --dst;
413 while ((*dst++ = *src++));
414 }
415
Ajay Dudanid04110c2011-01-17 23:55:07 -0800416 switch(target_baseband())
417 {
418 case BASEBAND_APQ:
419 src = baseband_apq;
420 if (have_cmdline) --dst;
421 while ((*dst++ = *src++));
422 break;
423
424 case BASEBAND_MSM:
425 src = baseband_msm;
426 if (have_cmdline) --dst;
427 while ((*dst++ = *src++));
428 break;
429
430 case BASEBAND_CSFB:
431 src = baseband_csfb;
432 if (have_cmdline) --dst;
433 while ((*dst++ = *src++));
434 break;
435
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800436 case BASEBAND_SVLTE2A:
437 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800438 if (have_cmdline) --dst;
439 while ((*dst++ = *src++));
440 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700441
442 case BASEBAND_MDM:
443 src = baseband_mdm;
444 if (have_cmdline) --dst;
445 while ((*dst++ = *src++));
446 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700447
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800448 case BASEBAND_MDM2:
449 src = baseband_mdm2;
450 if (have_cmdline) --dst;
451 while ((*dst++ = *src++));
452 break;
453
Amol Jadi5c61a952012-05-04 17:05:35 -0700454 case BASEBAND_SGLTE:
455 src = baseband_sglte;
456 if (have_cmdline) --dst;
457 while ((*dst++ = *src++));
458 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530459
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800460 case BASEBAND_SGLTE2:
461 src = baseband_sglte2;
462 if (have_cmdline) --dst;
463 while ((*dst++ = *src++));
464 break;
465
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530466 case BASEBAND_DSDA:
467 src = baseband_dsda;
468 if (have_cmdline) --dst;
469 while ((*dst++ = *src++));
470 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800471
472 case BASEBAND_DSDA2:
473 src = baseband_dsda2;
474 if (have_cmdline) --dst;
475 while ((*dst++ = *src++));
476 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800477 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700478
479 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700480 src = display_panel_buf;
481 if (have_cmdline) --dst;
482 while ((*dst++ = *src++));
483 }
Joonwoo Park61112782013-10-02 19:50:39 -0700484
485 if (have_target_boot_params) {
486 if (have_cmdline) --dst;
487 src = target_boot_params;
488 while ((*dst++ = *src++));
489 }
Neeti Desaie245d492012-06-01 12:52:13 -0700490 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700491
492
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700493 if (boot_dev_buf)
494 free(boot_dev_buf);
495
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800496 if (cmdline_final)
497 dprintf(INFO, "cmdline: %s\n", cmdline_final);
498 else
499 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700500 return cmdline_final;
501}
502
503unsigned *atag_core(unsigned *ptr)
504{
505 /* CORE */
506 *ptr++ = 2;
507 *ptr++ = 0x54410001;
508
509 return ptr;
510
511}
512
513unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
514 unsigned ramdisk_size)
515{
516 if (ramdisk_size) {
517 *ptr++ = 4;
518 *ptr++ = 0x54420005;
519 *ptr++ = (unsigned)ramdisk;
520 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800521 }
522
Neeti Desaie245d492012-06-01 12:52:13 -0700523 return ptr;
524}
525
526unsigned *atag_ptable(unsigned **ptr_addr)
527{
528 int i;
529 struct ptable *ptable;
530
531 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700532 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
533 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700534 *(*ptr_addr)++ = 0x4d534d70;
535 for (i = 0; i < ptable->count; ++i)
536 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
537 }
538
539 return (*ptr_addr);
540}
541
542unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
543{
544 int cmdline_length = 0;
545 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700546 char *dest;
547
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800548 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700549 n = (cmdline_length + 4) & (~3);
550
551 *ptr++ = (n / 4) + 2;
552 *ptr++ = 0x54410009;
553 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800554 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700555 ptr += (n / 4);
556
557 return ptr;
558}
559
560unsigned *atag_end(unsigned *ptr)
561{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800562 /* END */
563 *ptr++ = 0;
564 *ptr++ = 0;
565
Neeti Desaie245d492012-06-01 12:52:13 -0700566 return ptr;
567}
568
569void generate_atags(unsigned *ptr, const char *cmdline,
570 void *ramdisk, unsigned ramdisk_size)
571{
572
573 ptr = atag_core(ptr);
574 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
575 ptr = target_atag_mem(ptr);
576
577 /* Skip NAND partition ATAGS for eMMC boot */
578 if (!target_is_emmc_boot()){
579 ptr = atag_ptable(&ptr);
580 }
581
582 ptr = atag_cmdline(ptr, cmdline);
583 ptr = atag_end(ptr);
584}
585
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700586typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700587void boot_linux(void *kernel, unsigned *tags,
588 const char *cmdline, unsigned machtype,
589 void *ramdisk, unsigned ramdisk_size)
590{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800591 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800592#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700593 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800594#endif
595
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700596 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800597 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800598 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800599
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530600 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700601
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800602 final_cmdline = update_cmdline((const char*)cmdline);
603
Neeti Desai17379b82012-06-04 18:42:53 -0700604#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800605 dprintf(INFO, "Updating device tree: start\n");
606
Neeti Desai17379b82012-06-04 18:42:53 -0700607 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530608 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700609 if(ret)
610 {
611 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
612 ASSERT(0);
613 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800614 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700615#else
Neeti Desaie245d492012-06-01 12:52:13 -0700616 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800617 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700618#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700619
Maria Yu52254c02014-07-04 16:14:54 +0800620 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700621
622#if VERIFIED_BOOT
623 /* Write protect the device info */
624 if (mmc_write_protect("devinfo", 1))
625 {
626 dprintf(INFO, "Failed to write protect dev info\n");
627 ASSERT(0);
628 }
629#endif
630
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700631 /* Perform target specific cleanup */
632 target_uninit();
633
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800634 /* Turn off splash screen if enabled */
635#if DISPLAY_SPLASH_SCREEN
636 target_display_shutdown();
637#endif
638
639
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800640 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530641 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800642
643 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700644
Amol Jadi4421e652011-06-16 15:00:48 -0700645 /* do any platform specific cleanup before kernel entry */
646 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700647
Brian Swetland9c4c0752009-01-25 16:23:50 -0800648 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700649
Amol Jadi504f9fe2012-08-16 13:56:48 -0700650#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800651 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700652#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700653 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800654
655 if (IS_ARM64(kptr))
656 /* Jump to a 64bit kernel */
657 scm_elexec_call((paddr_t)kernel, tags_phys);
658 else
659 /* Jump to a 32bit kernel */
660 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800661}
662
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700663/* Function to check if the memory address range falls within the aboot
664 * boundaries.
665 * start: Start of the memory region
666 * size: Size of the memory region
667 */
668int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
669{
670 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800671 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700672 return -1;
673
674 /* Check for memory overlap. */
675 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
676 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700677 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700678 return 0;
679 else
680 return -1;
681}
682
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800683#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800684
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800685BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800686#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800687BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800688#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800689
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700690static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
691{
692 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800693
694#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800695#if IMAGE_VERIF_ALGO_SHA1
696 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
697#else
698 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
699#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800700#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700701
702 /* Assume device is rooted at this time. */
703 device.is_tampered = 1;
704
705 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
706
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700707#if VERIFIED_BOOT
708 if(boot_into_recovery)
709 {
710 ret = boot_verify_image((unsigned char *)bootimg_addr,
711 bootimg_size, "recovery");
712 }
713 else
714 {
715 ret = boot_verify_image((unsigned char *)bootimg_addr,
716 bootimg_size, "boot");
717 }
718 boot_verify_print_state();
719#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700720 ret = image_verify((unsigned char *)bootimg_addr,
721 (unsigned char *)(bootimg_addr + bootimg_size),
722 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800723 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700724#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700725 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
726
727 if (ret)
728 {
729 /* Authorized kernel */
730 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700731 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700732 }
733
734#if USE_PCOM_SECBOOT
735 set_tamper_flag(device.is_tampered);
736#endif
737
738 if(device.is_tampered)
739 {
740 write_device_info_mmc(&device);
741 #ifdef TZ_TAMPER_FUSE
742 set_tamper_fuse_cmd();
743 #endif
744 #ifdef ASSERT_ON_TAMPER
745 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
746 ASSERT(0);
747 #endif
748 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700749
750#if VERIFIED_BOOT
751 if(boot_verify_get_state() == RED)
752 {
753 if(!boot_into_recovery)
754 {
755 dprintf(CRITICAL,
756 "Device verification failed. Rebooting into recovery.\n");
757 reboot_device(RECOVERY_MODE);
758 }
759 else
760 {
761 dprintf(CRITICAL,
762 "Recovery image verification failed. Asserting..\n");
763 ASSERT(0);
764 }
765 }
766#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700767}
768
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530769static bool check_format_bit()
770{
771 bool ret = false;
772 int index;
773 uint64_t offset;
774 struct boot_selection_info *in = NULL;
775 char *buf = NULL;
776
777 index = partition_get_index("bootselect");
778 if (index == INVALID_PTN)
779 {
780 dprintf(INFO, "Unable to locate /bootselect partition\n");
781 return ret;
782 }
783 offset = partition_get_offset(index);
784 if(!offset)
785 {
786 dprintf(INFO, "partition /bootselect doesn't exist\n");
787 return ret;
788 }
789 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
790 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530791 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530792 {
793 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
794 free(buf);
795 return ret;
796 }
797 in = (struct boot_selection_info *) buf;
798 if ((in->signature == BOOTSELECT_SIGNATURE) &&
799 (in->version == BOOTSELECT_VERSION)) {
800 if ((in->state_info & BOOTSELECT_FORMAT) &&
801 !(in->state_info & BOOTSELECT_FACTORY))
802 ret = true;
803 } else {
804 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
805 in->signature, in->version);
806 ASSERT(0);
807 }
808 free(buf);
809 return ret;
810}
811
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700812void boot_verifier_init()
813{
814
815 uint32_t boot_state;
816 /* Check if device unlock */
817 if(device.is_unlocked)
818 {
819 boot_verify_send_event(DEV_UNLOCK);
820 boot_verify_print_state();
821 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
822 return;
823 }
824 else
825 {
826 boot_verify_send_event(BOOT_INIT);
827 }
828
829 /* Initialize keystore */
830 boot_state = boot_verify_keystore_init();
831 if(boot_state == YELLOW)
832 {
833 boot_verify_print_state();
834 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
835 }
836}
837
Shashank Mittal23b8f422010-04-16 19:27:21 -0700838int boot_linux_from_mmc(void)
839{
840 struct boot_img_hdr *hdr = (void*) buf;
841 struct boot_img_hdr *uhdr;
842 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700843 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700844 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700845 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700846
Shashank Mittalcd98d472011-08-02 14:29:24 -0700847 unsigned char *image_addr = 0;
848 unsigned kernel_actual;
849 unsigned ramdisk_actual;
850 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700851 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700852
853#if DEVICE_TREE
854 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700855 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700856 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800857 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700858 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700859#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800860 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
861 struct kernel64_hdr *kptr = (void*) kbuf;
862
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530863 if (check_format_bit())
864 boot_into_recovery = 1;
865
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700866 if (!boot_into_recovery) {
867 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
868 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
869 if (rcode <= 0) {
870 boot_into_ffbm = false;
871 if (rcode < 0)
872 dprintf(CRITICAL,"failed to get ffbm cookie");
873 } else
874 boot_into_ffbm = true;
875 } else
876 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700877 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
878 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
879 dprintf(INFO, "Unified boot method!\n");
880 hdr = uhdr;
881 goto unified_boot;
882 }
Greg Griscod6250552011-06-29 14:40:23 -0700883 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700884 index = partition_get_index("boot");
885 ptn = partition_get_offset(index);
886 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700887 dprintf(CRITICAL, "ERROR: No boot partition found\n");
888 return -1;
889 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700890 }
891 else {
892 index = partition_get_index("recovery");
893 ptn = partition_get_offset(index);
894 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700895 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
896 return -1;
897 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700898 }
899
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530900 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700901 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
902 return -1;
903 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700904
905 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700906 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700907 return -1;
908 }
909
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700910 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530911
912 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
913 dprintf(CRITICAL, "ERROR: Invalid page size\n");
914 return -1;
915 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700916 page_size = hdr->page_size;
917 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700918 }
919
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800920 /* Read the next page to get kernel Image header
921 * which lives in the second page for arm64 targets.
922 */
923
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530924 if (mmc_read(ptn + page_size, (uint32_t *) kbuf, page_size)) {
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800925 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
926 return -1;
927 }
928
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700929 /*
930 * Update the kernel/ramdisk/tags address if the boot image header
931 * has default values, these default values come from mkbootimg when
932 * the boot image is flashed using fastboot flash:raw
933 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800934 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700935
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700936 /* Get virtual addresses since the hdr saves physical addresses. */
937 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
938 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
939 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700940
941 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
942 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
943
944 /* Check if the addresses in the header are valid. */
945 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
946 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
947 {
948 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
949 return -1;
950 }
951
952#ifndef DEVICE_TREE
953 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
954 {
955 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
956 return -1;
957 }
958#endif
959
Shashank Mittalcd98d472011-08-02 14:29:24 -0700960 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300961 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
962 (int) target_use_signed_kernel(),
963 device.is_unlocked,
964 device.is_tampered);
965
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700966#if VERIFIED_BOOT
967 boot_verifier_init();
968#endif
969
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700970 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800971 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700972 offset = 0;
973
Shashank Mittalcd98d472011-08-02 14:29:24 -0700974 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700975
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800976#if DEVICE_TREE
977 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
978 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700979
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700980 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700981 {
982 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
983 return -1;
984 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800985#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530986 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700987
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700988#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700989
Amol Jadib6be5c12012-11-14 13:39:51 -0800990 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700991 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800992
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530993 if (check_aboot_addr_range_overlap((uint32_t)image_addr, imagesize_actual))
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700994 {
995 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
996 return -1;
997 }
998
Shashank Mittalcd98d472011-08-02 14:29:24 -0700999 /* Read image without signature */
1000 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1001 {
1002 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1003 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -08001004 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001005
Amol Jadib6be5c12012-11-14 13:39:51 -08001006 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001007 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001008
Shashank Mittalcd98d472011-08-02 14:29:24 -07001009 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -07001010
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301011 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -07001012 {
1013 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1014 return -1;
1015 }
1016
Shashank Mittalcd98d472011-08-02 14:29:24 -07001017 /* Read signature */
1018 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1019 {
1020 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001021 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001022 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001023
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301024 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001025
Neeti Desai465491e2012-07-31 12:53:35 -07001026 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -07001027 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1028 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -07001029
Neeti Desai465491e2012-07-31 12:53:35 -07001030 #if DEVICE_TREE
1031 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001032 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -07001033 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001034
Deepa Dinamani19648b42013-09-05 17:05:55 -07001035 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001036 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1037 return -1;
1038 }
Deepa Dinamani19648b42013-09-05 17:05:55 -07001039
Neeti Desai465491e2012-07-31 12:53:35 -07001040 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -07001041 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001042 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
1043 return -1;
1044 }
1045
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001046 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001047 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001048 {
1049 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1050 return -1;
1051 }
1052
Joel Kingaa335dc2013-06-03 16:11:08 -07001053 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001054 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -08001055 /*
1056 * If appended dev tree is found, update the atags with
1057 * memory address to the DTB appended location on RAM.
1058 * Else update with the atags address in the kernel header
1059 */
1060 void *dtb;
1061 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001062 hdr->kernel_size,
1063 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001064 if (!dtb) {
1065 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1066 return -1;
1067 }
Neeti Desai465491e2012-07-31 12:53:35 -07001068 }
1069 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001070 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001071 else
1072 {
Amol Jadib6be5c12012-11-14 13:39:51 -08001073 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001074
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001075 image_addr = (unsigned char *)target_get_scratch_address();
1076#if DEVICE_TREE
1077 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1078 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1079
1080 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
1081 {
1082 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1083 return -1;
1084 }
1085#else
1086 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1087
1088#endif
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001089 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001090 {
1091 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1092 return -1;
1093 }
1094
Amol Jadib6be5c12012-11-14 13:39:51 -08001095 dprintf(INFO, "Loading boot image (%d): start\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001096 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001097 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001098
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001099 offset = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001100
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001101 /* Load the entire boot image */
1102 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual)) {
1103 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
Shashank Mittalcd98d472011-08-02 14:29:24 -07001104 return -1;
1105 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001106
1107 dprintf(INFO, "Loading boot image (%d): done\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001108 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001109 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -07001110
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001111 #ifdef TZ_SAVE_KERNEL_HASH
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001112 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001113 #endif /* TZ_SAVE_KERNEL_HASH */
1114
1115 /* Move kernel, ramdisk and device tree to correct address */
1116 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1117 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Neeti Desai465491e2012-07-31 12:53:35 -07001118
1119 #if DEVICE_TREE
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001120 if(hdr->dt_size) {
1121 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1122 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001123
Deepa Dinamani19648b42013-09-05 17:05:55 -07001124 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001125 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1126 return -1;
1127 }
1128
Joel Kingaa335dc2013-06-03 16:11:08 -07001129 /* Find index of device tree within device tree table */
1130 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001131 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1132 return -1;
1133 }
1134
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001135 /* Validate and Read device device tree in the tags_addr */
Joel Kingaa335dc2013-06-03 16:11:08 -07001136 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001137 {
1138 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1139 return -1;
1140 }
1141
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001142 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001143 } else {
Aparna Mallavarapu33127372014-06-03 22:15:54 +05301144 /* Validate the tags_addr */
1145 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1146 {
1147 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1148 return -1;
1149 }
Dima Zavin77e41f32013-03-06 16:10:43 -08001150 /*
1151 * If appended dev tree is found, update the atags with
1152 * memory address to the DTB appended location on RAM.
1153 * Else update with the atags address in the kernel header
1154 */
1155 void *dtb;
1156 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001157 kernel_actual,
1158 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001159 if (!dtb) {
1160 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1161 return -1;
1162 }
Neeti Desai465491e2012-07-31 12:53:35 -07001163 }
1164 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001165 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001166
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001167 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1168 target_load_ssd_keystore();
1169
Shashank Mittal23b8f422010-04-16 19:27:21 -07001170unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001171
Dima Zavin77e41f32013-03-06 16:10:43 -08001172 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001173 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001174 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1175
1176 return 0;
1177}
1178
Dima Zavin214cc642009-01-26 11:16:21 -08001179int boot_linux_from_flash(void)
1180{
1181 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001182 struct ptentry *ptn;
1183 struct ptable *ptable;
1184 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001185
Shashank Mittalcd98d472011-08-02 14:29:24 -07001186 unsigned char *image_addr = 0;
1187 unsigned kernel_actual;
1188 unsigned ramdisk_actual;
1189 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001190 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001191
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001192#if DEVICE_TREE
1193 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001194 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001195 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001196 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001197#endif
1198
David Ng183a7422009-12-07 14:55:21 -08001199 if (target_is_emmc_boot()) {
1200 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1201 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1202 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1203 return -1;
1204 }
1205 goto continue_boot;
1206 }
1207
Dima Zavin214cc642009-01-26 11:16:21 -08001208 ptable = flash_get_ptable();
1209 if (ptable == NULL) {
1210 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1211 return -1;
1212 }
1213
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001214 if(!boot_into_recovery)
1215 {
1216 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001217
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001218 if (ptn == NULL) {
1219 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1220 return -1;
1221 }
1222 }
1223 else
1224 {
1225 ptn = ptable_find(ptable, "recovery");
1226 if (ptn == NULL) {
1227 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1228 return -1;
1229 }
Dima Zavin214cc642009-01-26 11:16:21 -08001230 }
1231
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001232 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001233 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1234 return -1;
1235 }
Dima Zavin214cc642009-01-26 11:16:21 -08001236
1237 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001238 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001239 return -1;
1240 }
1241
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001242 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001243 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 -08001244 return -1;
1245 }
1246
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001247 /*
1248 * Update the kernel/ramdisk/tags address if the boot image header
1249 * has default values, these default values come from mkbootimg when
1250 * the boot image is flashed using fastboot flash:raw
1251 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001252 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001253
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001254 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001255 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1256 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1257 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1258
1259 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1260 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1261
1262 /* Check if the addresses in the header are valid. */
1263 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1264 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1265 {
1266 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1267 return -1;
1268 }
1269
1270#ifndef DEVICE_TREE
1271 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1272 {
1273 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1274 return -1;
1275 }
1276#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001277
Shashank Mittalcd98d472011-08-02 14:29:24 -07001278 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001279 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001280 {
1281 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001282 offset = 0;
1283
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001284#if DEVICE_TREE
1285 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1286 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001287
1288 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1289 {
1290 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1291 return -1;
1292 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001293#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001294 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001295#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001296
Amol Jadib6be5c12012-11-14 13:39:51 -08001297 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001298 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001299
Shashank Mittalcd98d472011-08-02 14:29:24 -07001300 /* Read image without signature */
1301 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1302 {
1303 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1304 return -1;
1305 }
Dima Zavin214cc642009-01-26 11:16:21 -08001306
Amol Jadib6be5c12012-11-14 13:39:51 -08001307 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001308 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001309
Shashank Mittalcd98d472011-08-02 14:29:24 -07001310 offset = imagesize_actual;
1311 /* Read signature */
1312 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1313 {
1314 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001315 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001316 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001317
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301318 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001319
1320 /* Move kernel and ramdisk to correct address */
1321 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1322 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001323#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001324 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001325 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001326 {
1327 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1328 return -1;
1329 }
1330
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001331 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1332#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001333
1334 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001335 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001336 {
1337 write_device_info_flash(&device);
1338 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301339#if USE_PCOM_SECBOOT
1340 set_tamper_flag(device.is_tampered);
1341#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001342 }
1343 else
1344 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001345 offset = page_size;
1346
Amol Jadib6be5c12012-11-14 13:39:51 -08001347 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1348 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1349 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1350
1351 dprintf(INFO, "Loading boot image (%d): start\n",
1352 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001353 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001354
1355 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001356 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1357 return -1;
1358 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001359 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001360
Amol Jadib6be5c12012-11-14 13:39:51 -08001361 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001362 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1363 return -1;
1364 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001365 offset += ramdisk_actual;
1366
1367 dprintf(INFO, "Loading boot image (%d): done\n",
1368 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001369 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001370
1371 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001372 offset += second_actual;
1373 /* Second image loading not implemented. */
1374 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001375 }
1376
1377#if DEVICE_TREE
1378 if(hdr->dt_size != 0) {
1379
1380 /* Read the device tree table into buffer */
1381 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1382 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1383 return -1;
1384 }
1385
1386 table = (struct dt_table*) dt_buf;
1387
Deepa Dinamani19648b42013-09-05 17:05:55 -07001388 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001389 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1390 return -1;
1391 }
1392
Deepa Dinamani19648b42013-09-05 17:05:55 -07001393 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1394 if (!table)
1395 return -1;
1396
1397 /* Read the entire device tree table into buffer */
1398 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1399 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1400 return -1;
1401 }
1402
1403
Joel Kingaa335dc2013-06-03 16:11:08 -07001404 /* Find index of device tree within device tree table */
1405 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001406 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1407 return -1;
1408 }
1409
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001410 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001411 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001412 {
1413 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1414 return -1;
1415 }
1416
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001417 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001418 if(flash_read(ptn, offset + dt_entry.offset,
1419 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001420 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1421 return -1;
1422 }
1423 }
1424#endif
1425
Shashank Mittalcd98d472011-08-02 14:29:24 -07001426 }
David Ng183a7422009-12-07 14:55:21 -08001427continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001428
Dima Zavin214cc642009-01-26 11:16:21 -08001429 /* TODO: create/pass atags to kernel */
1430
Ajay Dudanie28a6072011-07-01 13:59:46 -07001431 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001432 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001433 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1434
1435 return 0;
1436}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001437
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001438BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001439void write_device_info_mmc(device_info *dev)
1440{
1441 struct device_info *info = (void*) info_buf;
1442 unsigned long long ptn = 0;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001443#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001444 unsigned long long size;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001445#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001446 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001447 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001448 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001449
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001450#if VERIFIED_BOOT
1451 index = partition_get_index("devinfo");
1452#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001453 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001454#endif
1455
Shashank Mittal162244e2011-08-08 19:01:25 -07001456 ptn = partition_get_offset(index);
1457 if(ptn == 0)
1458 {
1459 return;
1460 }
1461
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001462 lun = partition_get_lun(index);
1463 mmc_set_lun(lun);
1464
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001465#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001466 size = partition_get_size(index);
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001467#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001468
1469 memcpy(info, dev, sizeof(device_info));
1470
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001471 blocksize = mmc_get_device_blocksize();
1472
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001473#if VERIFIED_BOOT
1474 if(mmc_write(ptn, blocksize, (void *)info_buf))
1475#else
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001476 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001477#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001478 {
1479 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1480 return;
1481 }
1482}
1483
1484void read_device_info_mmc(device_info *dev)
1485{
1486 struct device_info *info = (void*) info_buf;
1487 unsigned long long ptn = 0;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001488#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001489 unsigned long long size;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001490#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001491 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001492 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001493
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001494#if VERIFIED_BOOT
1495 index = partition_get_index("devinfo");
1496#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001497 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001498#endif
1499
Shashank Mittal162244e2011-08-08 19:01:25 -07001500 ptn = partition_get_offset(index);
1501 if(ptn == 0)
1502 {
1503 return;
1504 }
1505
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001506 mmc_set_lun(partition_get_lun(index));
1507
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001508#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001509 size = partition_get_size(index);
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001510#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001511
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001512 blocksize = mmc_get_device_blocksize();
1513
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001514#if VERIFIED_BOOT
1515 if(mmc_read(ptn, (void *)info_buf, blocksize))
1516#else
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001517 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001518#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001519 {
1520 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1521 return;
1522 }
1523
1524 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1525 {
1526 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
Shashank Mittal7e033062014-09-25 19:58:07 -07001527#if DEFAULT_UNLOCK
1528 info->is_unlocked = 1;
1529#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001530 info->is_unlocked = 0;
Shashank Mittal7e033062014-09-25 19:58:07 -07001531#endif
1532 info->is_verified = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001533 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001534 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001535
1536 write_device_info_mmc(info);
1537 }
1538 memcpy(dev, info, sizeof(device_info));
1539}
1540
1541void write_device_info_flash(device_info *dev)
1542{
1543 struct device_info *info = (void *) info_buf;
1544 struct ptentry *ptn;
1545 struct ptable *ptable;
1546
1547 ptable = flash_get_ptable();
1548 if (ptable == NULL)
1549 {
1550 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1551 return;
1552 }
1553
1554 ptn = ptable_find(ptable, "devinfo");
1555 if (ptn == NULL)
1556 {
1557 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1558 return;
1559 }
1560
1561 memcpy(info, dev, sizeof(device_info));
1562
1563 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1564 {
1565 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1566 return;
1567 }
1568}
1569
1570void read_device_info_flash(device_info *dev)
1571{
1572 struct device_info *info = (void*) info_buf;
1573 struct ptentry *ptn;
1574 struct ptable *ptable;
1575
1576 ptable = flash_get_ptable();
1577 if (ptable == NULL)
1578 {
1579 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1580 return;
1581 }
1582
1583 ptn = ptable_find(ptable, "devinfo");
1584 if (ptn == NULL)
1585 {
1586 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1587 return;
1588 }
1589
1590 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1591 {
1592 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1593 return;
1594 }
1595
1596 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1597 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001598 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1599 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001600 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001601 write_device_info_flash(info);
1602 }
1603 memcpy(dev, info, sizeof(device_info));
1604}
1605
1606void write_device_info(device_info *dev)
1607{
1608 if(target_is_emmc_boot())
1609 {
1610 write_device_info_mmc(dev);
1611 }
1612 else
1613 {
1614 write_device_info_flash(dev);
1615 }
1616}
1617
1618void read_device_info(device_info *dev)
1619{
1620 if(target_is_emmc_boot())
1621 {
1622 read_device_info_mmc(dev);
1623 }
1624 else
1625 {
1626 read_device_info_flash(dev);
1627 }
1628}
1629
1630void reset_device_info()
1631{
1632 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001633 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001634 write_device_info(&device);
1635}
1636
1637void set_device_root()
1638{
1639 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001640 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001641 write_device_info(&device);
1642}
1643
Amol Jadicb524072012-08-09 16:40:18 -07001644#if DEVICE_TREE
1645int copy_dtb(uint8_t *boot_image_start)
1646{
1647 uint32 dt_image_offset = 0;
1648 uint32_t n;
1649 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001650 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001651 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001652
1653 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1654
Amol Jadicb524072012-08-09 16:40:18 -07001655 if(hdr->dt_size != 0) {
1656
1657 /* add kernel offset */
1658 dt_image_offset += page_size;
1659 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1660 dt_image_offset += n;
1661
1662 /* add ramdisk offset */
1663 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1664 dt_image_offset += n;
1665
1666 /* add second offset */
1667 if(hdr->second_size != 0) {
1668 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1669 dt_image_offset += n;
1670 }
1671
1672 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001673 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001674
Deepa Dinamani19648b42013-09-05 17:05:55 -07001675 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001676 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1677 return -1;
1678 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001679 /* Find index of device tree within device tree table */
1680 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001681 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1682 return -1;
1683 }
1684
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001685 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001686 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001687 {
1688 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1689 return -1;
1690 }
1691
Amol Jadicb524072012-08-09 16:40:18 -07001692 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001693 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001694 boot_image_start + dt_image_offset + dt_entry.offset,
1695 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001696 } else
1697 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001698
1699 /* Everything looks fine. Return success. */
1700 return 0;
1701}
1702#endif
1703
Brian Swetland9c4c0752009-01-25 16:23:50 -08001704void cmd_boot(const char *arg, void *data, unsigned sz)
1705{
1706 unsigned kernel_actual;
1707 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001708 uint32_t image_actual;
1709 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001710 uint32_t sig_actual = SIGNATURE_SIZE;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001711 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001712 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001713 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001714 int ret = 0;
1715 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001716
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001717#if VERIFIED_BOOT
1718 if(!device.is_unlocked)
1719 {
1720 fastboot_fail("unlock device to use this command");
1721 return;
1722 }
1723#endif
1724
Brian Swetland9c4c0752009-01-25 16:23:50 -08001725 if (sz < sizeof(hdr)) {
1726 fastboot_fail("invalid bootimage header");
1727 return;
1728 }
1729
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001730 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001731
1732 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001733 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001734
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001735 if(target_is_emmc_boot() && hdr->page_size) {
1736 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001737 page_mask = page_size - 1;
1738 }
1739
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001740 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1741 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001742#if DEVICE_TREE
1743 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1744#endif
1745
1746 image_actual = ADD_OF(page_size, kernel_actual);
1747 image_actual = ADD_OF(image_actual, ramdisk_actual);
1748 image_actual = ADD_OF(image_actual, dt_actual);
1749
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001750 if (target_use_signed_kernel() && (!device.is_unlocked))
1751 image_actual = ADD_OF(image_actual, sig_actual);
1752
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001753 /* sz should have atleast raw boot image */
1754 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001755 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001756 return;
1757 }
1758
1759 /* Verify the boot image
1760 * device & page_size are initialized in aboot_init
1761 */
1762 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001763 /* Pass size excluding signature size, otherwise we would try to
1764 * access signature beyond its length
1765 */
1766 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001767
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001768 /*
1769 * Update the kernel/ramdisk/tags address if the boot image header
1770 * has default values, these default values come from mkbootimg when
1771 * the boot image is flashed using fastboot flash:raw
1772 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001773 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1774 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001775
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001776 /* Get virtual addresses since the hdr saves physical addresses. */
1777 hdr->kernel_addr = VA(hdr->kernel_addr);
1778 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1779 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001780
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001781 /* Check if the addresses in the header are valid. */
1782 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1783 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1784 {
1785 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001786 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001787 }
1788
Amol Jadicb524072012-08-09 16:40:18 -07001789#if DEVICE_TREE
1790 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001791 ret = copy_dtb(data);
1792
1793 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001794#else
1795 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1796 {
1797 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001798 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001799 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001800#endif
1801
1802 /* Load ramdisk & kernel */
1803 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1804 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1805
1806#if DEVICE_TREE
1807 /*
1808 * If dtb is not found look for appended DTB in the kernel.
1809 * If appended dev tree is found, update the atags with
1810 * memory address to the DTB appended location on RAM.
1811 * Else update with the atags address in the kernel header
1812 */
1813 if (!dtb_copied) {
1814 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001815 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1816 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001817 if (!dtb) {
1818 fastboot_fail("dtb not found");
1819 return;
1820 }
Amol Jadicb524072012-08-09 16:40:18 -07001821 }
1822#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001823
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001824#ifndef DEVICE_TREE
1825 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1826 {
1827 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001828 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001829 }
1830#endif
1831
Brian Swetland9c4c0752009-01-25 16:23:50 -08001832 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001833 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001834
Dima Zavin77e41f32013-03-06 16:10:43 -08001835 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001836 (const char*) hdr->cmdline, board_machtype(),
1837 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001838}
1839
Sridhar Parasurame94e8152014-10-24 14:06:03 -07001840void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08001841{
1842 struct ptentry *ptn;
1843 struct ptable *ptable;
1844
1845 ptable = flash_get_ptable();
1846 if (ptable == NULL) {
1847 fastboot_fail("partition table doesn't exist");
1848 return;
1849 }
1850
1851 ptn = ptable_find(ptable, arg);
1852 if (ptn == NULL) {
1853 fastboot_fail("unknown partition name");
1854 return;
1855 }
1856
1857 if (flash_erase(ptn)) {
1858 fastboot_fail("failed to erase partition");
1859 return;
1860 }
1861 fastboot_okay("");
1862}
1863
Bikas Gurungd48bd242010-09-04 19:54:32 -07001864
1865void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1866{
1867 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001868 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001869 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001870 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001871
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001872#if VERIFIED_BOOT
1873 if(!strcmp(arg, KEYSTORE_PTN_NAME))
1874 {
1875 if(!device.is_unlocked)
1876 {
1877 fastboot_fail("unlock device to erase keystore");
1878 return;
1879 }
1880 }
1881#endif
1882
Kinson Chikf1a43512011-07-14 11:28:39 -07001883 index = partition_get_index(arg);
1884 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001885 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001886
Kinson Chikf1a43512011-07-14 11:28:39 -07001887 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001888 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001889 return;
1890 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001891
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001892 lun = partition_get_lun(index);
1893 mmc_set_lun(lun);
1894
Oliver Wangcee448d2013-10-22 18:40:13 +08001895#if MMC_SDHCI_SUPPORT
1896 if (mmc_erase_card(ptn, size)) {
1897 fastboot_fail("failed to erase partition\n");
1898 return;
1899 }
1900#else
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301901 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Kun Liang2f1601a2013-08-12 16:29:54 +08001902 size = partition_get_size(index);
1903 if (size > DEFAULT_ERASE_SIZE)
1904 size = DEFAULT_ERASE_SIZE;
1905
neetidb4b24d62012-01-20 12:13:09 -08001906 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001907 0 in first several blocks */
1908 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001909 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001910 return;
1911 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001912#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001913 fastboot_okay("");
1914}
1915
Sridhar Parasurame94e8152014-10-24 14:06:03 -07001916void cmd_erase(const char *arg, void *data, unsigned sz)
1917{
1918 if(target_is_emmc_boot())
1919 cmd_erase_mmc(arg, data, sz);
1920 else
1921 cmd_erase_nand(arg, data, sz);
1922}
Bikas Gurungd48bd242010-09-04 19:54:32 -07001923
Ajay Dudani5c761132011-04-07 20:19:04 -07001924void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001925{
1926 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001927 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001928 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001929 char *token = NULL;
1930 char *pname = NULL;
1931 uint8_t lun = 0;
1932 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001933
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301934 token = strtok((char *)arg, ":");
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001935 pname = token;
1936 token = strtok(NULL, ":");
1937 if(token)
1938 {
1939 lun = atoi(token);
1940 mmc_set_lun(lun);
1941 lun_set = true;
1942 }
1943
Mao Jinlong226f33a2014-07-04 17:24:10 +08001944 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07001945 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08001946 if (!strcmp(pname, "partition"))
1947 {
1948 dprintf(INFO, "Attempt to write partition image.\n");
1949 if (write_partition(sz, (unsigned char *) data)) {
1950 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07001951 return;
1952 }
1953 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08001954 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001955 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001956#if VERIFIED_BOOT
1957 if(!strcmp(pname, KEYSTORE_PTN_NAME))
1958 {
1959 if(!device.is_unlocked)
1960 {
1961 fastboot_fail("unlock device to flash keystore");
1962 return;
1963 }
1964 if(!boot_verify_validate_keystore((unsigned char *)data))
1965 {
1966 fastboot_fail("image is not a keystore file");
1967 return;
1968 }
1969 }
1970#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08001971 index = partition_get_index(pname);
1972 ptn = partition_get_offset(index);
1973 if(ptn == 0) {
1974 fastboot_fail("partition table doesn't exist");
1975 return;
1976 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001977
Mao Jinlong226f33a2014-07-04 17:24:10 +08001978 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
1979 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1980 fastboot_fail("image is not a boot image");
1981 return;
1982 }
1983 }
1984
1985 if(!lun_set)
1986 {
1987 lun = partition_get_lun(index);
1988 mmc_set_lun(lun);
1989 }
1990
1991 size = partition_get_size(index);
1992 if (ROUND_TO_PAGE(sz,511) > size) {
1993 fastboot_fail("size too large");
1994 return;
1995 }
1996 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1997 fastboot_fail("flash write failure");
1998 return;
1999 }
Greg Grisco6e754772011-06-23 12:19:39 -07002000 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002001 }
2002 fastboot_okay("");
2003 return;
2004}
2005
Ajay Dudani5c761132011-04-07 20:19:04 -07002006void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2007{
2008 unsigned int chunk;
2009 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002010 uint32_t *fill_buf = NULL;
2011 uint32_t fill_val;
2012 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002013 sparse_header_t *sparse_header;
2014 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002015 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002016 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302017 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002018 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302019 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002020 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002021
Kinson Chikf1a43512011-07-14 11:28:39 -07002022 index = partition_get_index(arg);
2023 ptn = partition_get_offset(index);
2024 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002025 fastboot_fail("partition table doesn't exist");
2026 return;
2027 }
2028
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302029 size = partition_get_size(index);
2030 if (ROUND_TO_PAGE(sz,511) > size) {
2031 fastboot_fail("size too large");
2032 return;
2033 }
2034
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002035 lun = partition_get_lun(index);
2036 mmc_set_lun(lun);
2037
Ajay Dudani5c761132011-04-07 20:19:04 -07002038 /* Read and skip over sparse image header */
2039 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08002040 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
2041 fastboot_fail("size too large");
2042 return;
2043 }
2044
Ajay Dudani5c761132011-04-07 20:19:04 -07002045 data += sparse_header->file_hdr_sz;
2046 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
2047 {
2048 /* Skip the remaining bytes in a header that is longer than
2049 * we expected.
2050 */
2051 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
2052 }
2053
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002054 dprintf (SPEW, "=== Sparse Image Header ===\n");
2055 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2056 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2057 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2058 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2059 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2060 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2061 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2062 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002063
2064 /* Start processing chunks */
2065 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2066 {
2067 /* Read and skip over chunk header */
2068 chunk_header = (chunk_header_t *) data;
2069 data += sizeof(chunk_header_t);
2070
2071 dprintf (SPEW, "=== Chunk Header ===\n");
2072 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2073 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2074 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2075
2076 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
2077 {
2078 /* Skip the remaining bytes in a header that is longer than
2079 * we expected.
2080 */
2081 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
2082 }
2083
2084 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
2085 switch (chunk_header->chunk_type)
2086 {
2087 case CHUNK_TYPE_RAW:
2088 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2089 chunk_data_sz))
2090 {
2091 fastboot_fail("Bogus chunk size for chunk type Raw");
2092 return;
2093 }
2094
Ajay Dudaniab18f022011-05-12 14:39:22 -07002095 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2096 chunk_data_sz,
2097 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002098 {
2099 fastboot_fail("flash write failure");
2100 return;
2101 }
2102 total_blocks += chunk_header->chunk_sz;
2103 data += chunk_data_sz;
2104 break;
2105
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002106 case CHUNK_TYPE_FILL:
2107 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2108 sizeof(uint32_t)))
2109 {
2110 fastboot_fail("Bogus chunk size for chunk type FILL");
2111 return;
2112 }
2113
2114 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2115 if (!fill_buf)
2116 {
2117 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2118 return;
2119 }
2120
2121 fill_val = *(uint32_t *)data;
2122 data = (char *) data + sizeof(uint32_t);
2123 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
2124
2125 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2126 {
2127 fill_buf[i] = fill_val;
2128 }
2129
2130 for (i = 0; i < chunk_blk_cnt; i++)
2131 {
2132 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2133 sparse_header->blk_sz,
2134 fill_buf))
2135 {
2136 fastboot_fail("flash write failure");
2137 free(fill_buf);
2138 return;
2139 }
2140
2141 total_blocks++;
2142 }
2143
2144 free(fill_buf);
2145 break;
2146
Ajay Dudani5c761132011-04-07 20:19:04 -07002147 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002148 total_blocks += chunk_header->chunk_sz;
2149 break;
2150
Ajay Dudani5c761132011-04-07 20:19:04 -07002151 case CHUNK_TYPE_CRC:
2152 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2153 {
2154 fastboot_fail("Bogus chunk size for chunk type Dont Care");
2155 return;
2156 }
2157 total_blocks += chunk_header->chunk_sz;
2158 data += chunk_data_sz;
2159 break;
2160
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002161 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002162 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002163 fastboot_fail("Unknown chunk type");
2164 return;
2165 }
2166 }
2167
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002168 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2169 total_blocks, sparse_header->total_blks);
2170
2171 if(total_blocks != sparse_header->total_blks)
2172 {
2173 fastboot_fail("sparse image write failure");
2174 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002175
2176 fastboot_okay("");
2177 return;
2178}
2179
2180void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2181{
2182 sparse_header_t *sparse_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002183
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002184#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002185 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2186 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002187 int ret=0;
2188 uint32 major_version=0;
2189 uint32 minor_version=0;
2190
2191 ret = scm_svc_version(&major_version,&minor_version);
2192 if(!ret)
2193 {
2194 if(major_version >= 2)
2195 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002196 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002197 {
2198 ret = encrypt_scm((uint32 **) &data, &sz);
2199 if (ret != 0) {
2200 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2201 return;
2202 }
2203
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002204 /* Protect only for SSD */
2205 if (!strcmp(arg, "ssd")) {
2206 ret = scm_protect_keystore((uint32 *) data, sz);
2207 if (ret != 0) {
2208 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2209 return;
2210 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002211 }
2212 }
2213 else
2214 {
2215 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2216 if(ret != 0)
2217 {
2218 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2219 return;
2220 }
2221 }
2222 }
2223 else
2224 {
2225 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2226 magic_number[1] == DECRYPT_MAGIC_1)
2227 {
2228 ret = decrypt_scm((uint32 **) &data, &sz);
2229 if (ret != 0) {
2230 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2231 return;
2232 }
2233 }
2234 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2235 magic_number[1] == ENCRYPT_MAGIC_1)
2236 {
2237 ret = encrypt_scm((uint32 **) &data, &sz);
2238 if (ret != 0) {
2239 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2240 return;
2241 }
2242 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002243 }
2244 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002245 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002246 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002247 dprintf(CRITICAL,"INVALID SVC Version\n");
2248 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002249 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002250#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002251
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002252#if VERIFIED_BOOT
2253 if(!device.is_unlocked && !device.is_verified)
2254 {
2255 fastboot_fail("device is locked. Cannot flash images");
2256 return;
2257 }
2258 if(!device.is_unlocked && device.is_verified)
2259 {
2260 if(!boot_verify_flash_allowed(arg))
2261 {
2262 fastboot_fail("cannot flash this partition in verified state");
2263 return;
2264 }
2265 }
2266#endif
2267
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002268 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002269 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
2270 cmd_flash_mmc_img(arg, data, sz);
2271 else
2272 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002273 return;
2274}
2275
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002276void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002277{
2278 struct ptentry *ptn;
2279 struct ptable *ptable;
2280 unsigned extra = 0;
2281
2282 ptable = flash_get_ptable();
2283 if (ptable == NULL) {
2284 fastboot_fail("partition table doesn't exist");
2285 return;
2286 }
2287
2288 ptn = ptable_find(ptable, arg);
2289 if (ptn == NULL) {
2290 fastboot_fail("unknown partition name");
2291 return;
2292 }
2293
2294 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2295 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2296 fastboot_fail("image is not a boot image");
2297 return;
2298 }
2299 }
2300
Amol Jadi5c61a952012-05-04 17:05:35 -07002301 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002302 || !strcmp(ptn->name, "userdata")
2303 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002304 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002305 || !strcmp(ptn->name, "modem"))
2306 {
2307 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07002308 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002309 else
2310 extra = 0;
2311 }
2312 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002313 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002314
2315 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2316 if (flash_write(ptn, extra, data, sz)) {
2317 fastboot_fail("flash write failure");
2318 return;
2319 }
2320 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2321 fastboot_okay("");
2322}
2323
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002324void cmd_flash(const char *arg, void *data, unsigned sz)
2325{
2326 if(target_is_emmc_boot())
2327 cmd_flash_mmc(arg, data, sz);
2328 else
2329 cmd_flash_nand(arg, data, sz);
2330}
2331
Dima Zavin214cc642009-01-26 11:16:21 -08002332void cmd_continue(const char *arg, void *data, unsigned sz)
2333{
2334 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002335 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002336
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002337 if (target_is_emmc_boot())
2338 {
2339 boot_linux_from_mmc();
2340 }
2341 else
2342 {
2343 boot_linux_from_flash();
2344 }
Dima Zavin214cc642009-01-26 11:16:21 -08002345}
2346
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002347void cmd_reboot(const char *arg, void *data, unsigned sz)
2348{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002349 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002350 fastboot_okay("");
2351 reboot_device(0);
2352}
2353
2354void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2355{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002356 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002357 fastboot_okay("");
2358 reboot_device(FASTBOOT_MODE);
2359}
2360
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002361void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2362{
2363 dprintf(INFO, "Enabling charger screen check\n");
2364 device.charger_screen_enabled = 1;
2365 write_device_info(&device);
2366 fastboot_okay("");
2367}
2368
2369void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2370{
2371 dprintf(INFO, "Disabling charger screen check\n");
2372 device.charger_screen_enabled = 0;
2373 write_device_info(&device);
2374 fastboot_okay("");
2375}
2376
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302377void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2378{
2379 dprintf(INFO, "Selecting display panel %s\n", arg);
2380 if (arg)
2381 strlcpy(device.display_panel, arg,
2382 sizeof(device.display_panel));
2383 write_device_info(&device);
2384 fastboot_okay("");
2385}
2386
Shashank Mittal162244e2011-08-08 19:01:25 -07002387void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2388{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002389 /* TODO: Wipe user data */
2390 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002391 {
2392 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002393 device.is_verified = 0;
2394 write_device_info(&device);
2395 }
2396 fastboot_okay("");
2397}
2398
2399void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2400{
2401 /* TODO: Wipe user data */
2402 if(device.is_unlocked || device.is_verified)
2403 {
2404 device.is_unlocked = 0;
2405 device.is_verified = 0;
2406 write_device_info(&device);
2407 }
2408 fastboot_okay("");
2409}
2410
2411void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2412{
2413 /* TODO: Wipe user data */
2414 if(device.is_unlocked || !device.is_verified)
2415 {
2416 device.is_unlocked = 0;
2417 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002418 write_device_info(&device);
2419 }
2420 fastboot_okay("");
2421}
2422
Shashank Mittala0032282011-08-26 14:50:11 -07002423void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2424{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302425 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002426 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002427 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002428 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2429 fastboot_info(response);
2430 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002431 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302432 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2433 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002434 fastboot_okay("");
2435}
2436
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002437void cmd_preflash(const char *arg, void *data, unsigned sz)
2438{
2439 fastboot_okay("");
2440}
2441
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302442struct fbimage* splash_screen_flash();
2443
2444int splash_screen_check_header(struct fbimage *logo)
2445{
2446 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2447 return -1;
2448 if (logo->header.width == 0 || logo->header.height == 0)
2449 return -1;
2450 return 0;
2451}
2452
2453struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002454{
2455 struct ptentry *ptn;
2456 struct ptable *ptable;
2457 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002458 struct fbimage *logo = NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002459
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302460
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002461 logo = (struct fbimage *) malloc(ROUNDUP(page_size, sizeof(struct fbimage)));
2462 ASSERT(logo);
2463
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302464 ptable = flash_get_ptable();
2465 if (ptable == NULL) {
2466 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002467 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302468 }
2469 ptn = ptable_find(ptable, "splash");
2470 if (ptn == NULL) {
2471 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002472 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302473 }
2474
2475 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2476 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002477 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302478 }
2479
2480 if (splash_screen_check_header(logo)) {
2481 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002482 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302483 }
2484
2485 fb_display = fbcon_display();
2486 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302487 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2488 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back to default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002489 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002490 }
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302491 uint8_t *base = (uint8_t *) fb_display->base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302492 if (flash_read(ptn + sizeof(logo->header), 0,
2493 base,
2494 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2495 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302496 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002497 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002498 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302499 logo->image = base;
2500 }
2501
2502 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002503
2504err:
2505 free(logo);
2506 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302507}
2508
2509struct fbimage* splash_screen_mmc()
2510{
2511 int index = INVALID_PTN;
2512 unsigned long long ptn = 0;
2513 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002514 struct fbimage *logo = NULL;
Channagoud Kadabida117392014-11-24 11:01:12 -08002515 uint32_t blocksize;
2516 uint32_t readsize;
2517 uint32_t ptn_size;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302518
2519 index = partition_get_index("splash");
2520 if (index == 0) {
2521 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2522 return NULL;
2523 }
2524
2525 ptn = partition_get_offset(index);
2526 if (ptn == 0) {
2527 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2528 return NULL;
2529 }
2530
Channagoud Kadabida117392014-11-24 11:01:12 -08002531 ptn_size = partition_get_size(index);
2532 blocksize = mmc_get_device_blocksize();
2533 readsize = ROUNDUP(sizeof(logo->header), blocksize);
2534
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002535 logo = (struct fbimage *)memalign(CACHE_LINE, ROUNDUP(readsize, CACHE_LINE));
2536 ASSERT(logo);
2537
Channagoud Kadabida117392014-11-24 11:01:12 -08002538 if (mmc_read(ptn, (uint32_t *) logo, readsize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302539 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002540 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302541 }
2542
2543 if (splash_screen_check_header(logo)) {
2544 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002545 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302546 }
2547
2548 fb_display = fbcon_display();
2549 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302550 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2551 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002552 goto err;
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302553 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302554 uint8_t *base = (uint8_t *) fb_display->base;
Channagoud Kadabida117392014-11-24 11:01:12 -08002555 readsize = ROUNDUP((logo->header.width * logo->header.height * fb_display->bpp/8), blocksize);
2556
2557 if (readsize > ptn_size)
2558 {
2559 dprintf(CRITICAL, "@%d:Invalid logo header readsize:%u exceeds ptn_size:%u\n", __LINE__, readsize,ptn_size);
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002560 goto err;
Channagoud Kadabida117392014-11-24 11:01:12 -08002561 }
2562
2563 if (mmc_read(ptn + sizeof(logo->header),(uint32_t *)base, readsize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302564 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302565 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002566 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302567 }
2568
2569 logo->image = base;
2570 }
2571
2572 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002573
2574err:
2575 free(logo);
2576 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302577}
2578
2579
2580struct fbimage* fetch_image_from_partition()
2581{
2582 if (target_is_emmc_boot()) {
2583 return splash_screen_mmc();
2584 } else {
2585 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002586 }
2587}
2588
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002589/* Get the size from partiton name */
2590static void get_partition_size(const char *arg, char *response)
2591{
2592 uint64_t ptn = 0;
2593 uint64_t size;
2594 int index = INVALID_PTN;
2595
2596 index = partition_get_index(arg);
2597
2598 if (index == INVALID_PTN)
2599 {
2600 dprintf(CRITICAL, "Invalid partition index\n");
2601 return;
2602 }
2603
2604 ptn = partition_get_offset(index);
2605
2606 if(!ptn)
2607 {
2608 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2609 return;
2610 }
2611
2612 size = partition_get_size(index);
2613
2614 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2615 return;
2616}
2617
2618/*
2619 * Publish the partition type & size info
2620 * fastboot getvar will publish the required information.
2621 * fastboot getvar partition_size:<partition_name>: partition size in hex
2622 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2623 */
2624static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2625{
2626 uint8_t i;
2627
2628 for (i = 0; i < num_parts; i++) {
2629 get_partition_size(info[i].part_name, info[i].size_response);
2630
2631 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2632 {
2633 dprintf(CRITICAL, "partition size name truncated\n");
2634 return;
2635 }
2636 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2637 {
2638 dprintf(CRITICAL, "partition type name truncated\n");
2639 return;
2640 }
2641
2642 /* publish partition size & type info */
2643 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2644 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2645 }
2646}
2647
Amol Jadi5edf3552013-07-23 14:15:34 -07002648/* register commands and variables for fastboot */
2649void aboot_fastboot_register_commands(void)
2650{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002651 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07002652
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002653 struct fastboot_cmd_desc cmd_list[] = {
2654 /* By default the enabled list is empty. */
2655 {"", NULL},
2656 /* move commands enclosed within the below ifndef to here
2657 * if they need to be enabled in user build.
2658 */
2659#ifndef DISABLE_FASTBOOT_CMDS
2660 /* Register the following commands only for non-user builds */
2661 {"flash:", cmd_flash},
2662 {"erase:", cmd_erase},
2663 {"boot", cmd_boot},
2664 {"continue", cmd_continue},
2665 {"reboot", cmd_reboot},
2666 {"reboot-bootloader", cmd_reboot_bootloader},
2667 {"oem unlock", cmd_oem_unlock},
2668 {"oem lock", cmd_oem_lock},
2669 {"oem verified", cmd_oem_verified},
2670 {"oem device-info", cmd_oem_devinfo},
2671 {"preflash", cmd_preflash},
2672 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
2673 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08002674 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002675#endif
2676 };
2677
2678 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
2679 for (i = 1; i < fastboot_cmds_count; i++)
2680 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
2681
Amol Jadi5edf3552013-07-23 14:15:34 -07002682 /* publish variables and their values */
2683 fastboot_publish("product", TARGET(BOARD));
2684 fastboot_publish("kernel", "lk");
2685 fastboot_publish("serialno", sn_buf);
2686
2687 /*
2688 * partition info is supported only for emmc partitions
2689 * Calling this for NAND prints some error messages which
2690 * is harmless but misleading. Avoid calling this for NAND
2691 * devices.
2692 */
2693 if (target_is_emmc_boot())
2694 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2695
2696 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002697 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2698 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002699 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002700 /* Is the charger screen check enabled */
2701 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2702 device.charger_screen_enabled);
2703 fastboot_publish("charger-screen-enabled",
2704 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302705 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2706 device.display_panel);
2707 fastboot_publish("display-panel",
2708 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002709}
2710
Brian Swetland9c4c0752009-01-25 16:23:50 -08002711void aboot_init(const struct app_descriptor *app)
2712{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002713 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002714 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002715
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002716 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002717 if (target_is_emmc_boot())
2718 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002719 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002720 page_mask = page_size - 1;
2721 }
2722 else
2723 {
2724 page_size = flash_page_size();
2725 page_mask = page_size - 1;
2726 }
2727
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002728 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2729
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002730 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002731
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002732 /* Display splash screen if enabled */
2733#if DISPLAY_SPLASH_SCREEN
2734 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302735 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002736 dprintf(SPEW, "Display Init: Done\n");
2737#endif
2738
2739
Greg Griscod6250552011-06-29 14:40:23 -07002740 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002741 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002742
Dhaval Patel223ec952013-07-18 14:49:44 -07002743 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2744
Matthew Qindefd5562014-07-11 18:02:40 +08002745 /*
2746 * Check power off reason if user force reset,
2747 * if yes phone will do normal boot.
2748 */
2749 if (is_user_force_reset())
2750 goto normal_boot;
2751
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002752 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002753 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002754 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002755 dprintf(ALWAYS,"dload mode key sequence detected\n");
2756 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002757 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002758 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002759 }
2760 else
2761 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302762 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002763 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002764 }
2765 boot_into_fastboot = true;
2766 }
2767 if (!boot_into_fastboot)
2768 {
2769 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2770 boot_into_recovery = 1;
2771 if (!boot_into_recovery &&
2772 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002773 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002774 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002775 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002776 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002777 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002778 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002779
Ajay Dudani77421292010-10-27 19:34:06 -07002780 reboot_mode = check_reboot_mode();
2781 if (reboot_mode == RECOVERY_MODE) {
2782 boot_into_recovery = 1;
2783 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002784 boot_into_fastboot = true;
Matthew Qind886f3c2014-01-17 16:52:01 +08002785 } else if(reboot_mode == ALARM_BOOT) {
2786 boot_reason_alarm = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002787 }
2788
Matthew Qindefd5562014-07-11 18:02:40 +08002789normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03002790 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002791 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002792 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002793 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002794 if(emmc_recovery_init())
2795 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2796 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002797 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002798 if((device.is_unlocked) || (device.is_tampered))
2799 {
2800 #ifdef TZ_TAMPER_FUSE
2801 set_tamper_fuse_cmd();
2802 #endif
2803 #if USE_PCOM_SECBOOT
2804 set_tamper_flag(device.is_tampered);
2805 #endif
2806 }
Shashank Mittala0032282011-08-26 14:50:11 -07002807 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002808 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002809 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002810 else
2811 {
2812 recovery_init();
2813 #if USE_PCOM_SECBOOT
2814 if((device.is_unlocked) || (device.is_tampered))
2815 set_tamper_flag(device.is_tampered);
2816 #endif
2817 boot_linux_from_flash();
2818 }
2819 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2820 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002821 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002822
Amol Jadi5edf3552013-07-23 14:15:34 -07002823 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002824
Amol Jadi5edf3552013-07-23 14:15:34 -07002825 /* register aboot specific fastboot commands */
2826 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002827
Amol Jadi5edf3552013-07-23 14:15:34 -07002828 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002829 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002830
2831 /* initialize and start fastboot */
2832 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002833}
2834
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002835uint32_t get_page_size()
2836{
2837 return page_size;
2838}
2839
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002840/*
2841 * Calculated and save hash (SHA256) for non-signed boot image.
2842 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002843 * @param image_addr - Boot image address
2844 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002845 *
2846 * @return int - 0 on success, negative value on failure.
2847 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002848static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002849{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002850 unsigned int digest[8];
2851#if IMAGE_VERIF_ALGO_SHA1
2852 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
2853#else
2854 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
2855#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002856
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002857 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002858 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002859
2860 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002861 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002862
2863 return 0;
2864}
2865
Brian Swetland9c4c0752009-01-25 16:23:50 -08002866APP_START(aboot)
2867 .init = aboot_init,
2868APP_END