blob: 04f177c736794b209bef8ad0f59688c8b94d9b07 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Kuogee Hsieh55e92c52013-12-20 08:34:10 -08005 * Copyright (c) 2009-2014, 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>
Dima Zavin214cc642009-01-26 11:16:21 -080058
Neeti Desai17379b82012-06-04 18:42:53 -070059#if DEVICE_TREE
60#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070061#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070062#endif
63
Shashank Mittalcd98d472011-08-02 14:29:24 -070064#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080065#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080066#include "bootimg.h"
67#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070068#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070069#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070070#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070071#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070072#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070073
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070074extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070075extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070076extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070077extern int get_target_boot_params(const char *cmdline, const char *part,
78 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070079
80void write_device_info_mmc(device_info *dev);
81void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -070082static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070083
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070084#define EXPAND(NAME) #NAME
85#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070086
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080087#ifdef MEMBASE
88#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
89#else
David Ng183a7422009-12-07 14:55:21 -080090#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080091#endif
92
Deepa Dinamani0e163a42013-05-24 17:08:15 -070093#ifndef MEMSIZE
94#define MEMSIZE 1024*1024
95#endif
96
97#define MAX_TAGS_SIZE 1024
98
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080099#define RECOVERY_MODE 0x77665502
100#define FASTBOOT_MODE 0x77665500
101
Kun Liang2f1601a2013-08-12 16:29:54 +0800102/* make 4096 as default size to ensure EFS,EXT4's erasing */
103#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800104#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800105
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800106#define UBI_MAGIC "UBI#"
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700107#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800108#define UBI_MAGIC_SIZE 0x04
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700109#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800110
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800111#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
112
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700113#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
114
Smita Ghoshb4555da2014-09-18 14:14:56 -0700115#if UFS_SUPPORT || USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700116static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700117#else
David Ng183a7422009-12-07 14:55:21 -0800118static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700119#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800120static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300121static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800122static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800123static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700124static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300125static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800126
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800127static const char *baseband_apq = " androidboot.baseband=apq";
128static const char *baseband_msm = " androidboot.baseband=msm";
129static const char *baseband_csfb = " androidboot.baseband=csfb";
130static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700131static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800132static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700133static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800134static const char *baseband_dsda = " androidboot.baseband=dsda";
135static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800136static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800137static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800138
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700139static unsigned page_size = 0;
140static unsigned page_mask = 0;
141static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
142static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700143static char target_boot_params[64];
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700144
Shashank Mittalcd98d472011-08-02 14:29:24 -0700145/* Assuming unauthorized kernel image by default */
146static int auth_kernel_img = 0;
147
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700148static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700149
Dima Zavin42168f22009-01-30 11:52:22 -0800150struct atag_ptbl_entry
151{
152 char name[16];
153 unsigned offset;
154 unsigned size;
155 unsigned flags;
156};
157
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700158/*
159 * Partition info, required to be published
160 * for fastboot
161 */
162struct getvar_partition_info {
163 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
164 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
165 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
166 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
167 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
168};
169
170/*
171 * Right now, we are publishing the info for only
172 * three partitions
173 */
174struct getvar_partition_info part_info[] =
175{
176 { "system" , "partition-size:", "partition-type:", "", "ext4" },
177 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
178 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
179};
180
181char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700182char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800183char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700184char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530185char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700186
Greg Griscod2471ef2011-07-14 13:00:42 -0700187extern int emmc_recovery_init(void);
188
Kinson Chik0b1c8162011-08-31 16:31:57 -0700189#if NO_KEYPAD_DRIVER
190extern int fastboot_trigger(void);
191#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700192
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800193static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700194{
195 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700196#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800197 if (is_arm64)
198 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
199 else
200 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700201 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
202 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700203#endif
204}
205
Dima Zavin42168f22009-01-30 11:52:22 -0800206static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
207{
208 struct atag_ptbl_entry atag_ptn;
209
210 memcpy(atag_ptn.name, ptn->name, 16);
211 atag_ptn.name[15] = '\0';
212 atag_ptn.offset = ptn->start;
213 atag_ptn.size = ptn->length;
214 atag_ptn.flags = ptn->flags;
215 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
216 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
217}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800218
Neeti Desaie245d492012-06-01 12:52:13 -0700219unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800220{
David Ng183a7422009-12-07 14:55:21 -0800221 int cmdline_len = 0;
222 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800223 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700224 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800225 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300226 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700227 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700228 char *boot_dev_buf = NULL;
Dima Zavin42168f22009-01-30 11:52:22 -0800229
Brian Swetland9c4c0752009-01-25 16:23:50 -0800230 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800231 cmdline_len = strlen(cmdline);
232 have_cmdline = 1;
233 }
234 if (target_is_emmc_boot()) {
235 cmdline_len += strlen(emmc_cmdline);
Smita Ghoshb4555da2014-09-18 14:14:56 -0700236#if UFS_SUPPORT || USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700237 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
238 ASSERT(boot_dev_buf);
239 platform_boot_dev_cmdline(boot_dev_buf);
240 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700241#endif
David Ng183a7422009-12-07 14:55:21 -0800242 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800243
244 cmdline_len += strlen(usb_sn_cmdline);
245 cmdline_len += strlen(sn_buf);
246
Pavel Nedev5614d222013-06-17 18:01:02 +0300247 if (boot_into_recovery && gpt_exists)
248 cmdline_len += strlen(secondary_gpt_enable);
249
Pavel Nedev328ac822013-04-05 15:25:11 +0300250 if (boot_into_ffbm) {
251 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700252 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800253 /* reduce kernel console messages to speed-up boot */
254 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700255 } else if (device.charger_screen_enabled &&
256 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700257 pause_at_bootup = 1;
258 cmdline_len += strlen(battchg_pause);
259 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800260
Shashank Mittalcd98d472011-08-02 14:29:24 -0700261 if(target_use_signed_kernel() && auth_kernel_img) {
262 cmdline_len += strlen(auth_kernel);
263 }
264
Joonwoo Park61112782013-10-02 19:50:39 -0700265 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
266 "system",
267 target_boot_params,
268 sizeof(target_boot_params)) == 0) {
269 have_target_boot_params = 1;
270 cmdline_len += strlen(target_boot_params);
271 }
272
Ajay Dudanid04110c2011-01-17 23:55:07 -0800273 /* Determine correct androidboot.baseband to use */
274 switch(target_baseband())
275 {
276 case BASEBAND_APQ:
277 cmdline_len += strlen(baseband_apq);
278 break;
279
280 case BASEBAND_MSM:
281 cmdline_len += strlen(baseband_msm);
282 break;
283
284 case BASEBAND_CSFB:
285 cmdline_len += strlen(baseband_csfb);
286 break;
287
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800288 case BASEBAND_SVLTE2A:
289 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800290 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700291
292 case BASEBAND_MDM:
293 cmdline_len += strlen(baseband_mdm);
294 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700295
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800296 case BASEBAND_MDM2:
297 cmdline_len += strlen(baseband_mdm2);
298 break;
299
Amol Jadi5c61a952012-05-04 17:05:35 -0700300 case BASEBAND_SGLTE:
301 cmdline_len += strlen(baseband_sglte);
302 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530303
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800304 case BASEBAND_SGLTE2:
305 cmdline_len += strlen(baseband_sglte2);
306 break;
307
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530308 case BASEBAND_DSDA:
309 cmdline_len += strlen(baseband_dsda);
310 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800311
312 case BASEBAND_DSDA2:
313 cmdline_len += strlen(baseband_dsda2);
314 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800315 }
316
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800317 if (cmdline) {
318 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
319 target_display_panel_node(device.display_panel,
320 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
321 strlen(display_panel_buf)) {
322 cmdline_len += strlen(display_panel_buf);
323 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700324 }
325
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800326 if (target_warm_boot()) {
327 warm_boot = true;
328 cmdline_len += strlen(warmboot_cmdline);
329 }
330
David Ng183a7422009-12-07 14:55:21 -0800331 if (cmdline_len > 0) {
332 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800333 unsigned char *dst;
334
335 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
336 ASSERT(cmdline_final != NULL);
337 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700338
Amol Jadi168b7712012-03-06 16:15:00 -0800339 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800340 if (have_cmdline) {
341 src = cmdline;
342 while ((*dst++ = *src++));
343 }
344 if (target_is_emmc_boot()) {
345 src = emmc_cmdline;
346 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700347 have_cmdline = 1;
348 while ((*dst++ = *src++));
Smita Ghoshb4555da2014-09-18 14:14:56 -0700349#if UFS_SUPPORT || USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700350 src = boot_dev_buf;
351 if (have_cmdline) --dst;
352 while ((*dst++ = *src++));
353#endif
David Ngf773dde2010-07-26 19:55:08 -0700354 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800355
356 src = usb_sn_cmdline;
357 if (have_cmdline) --dst;
358 have_cmdline = 1;
359 while ((*dst++ = *src++));
360 src = sn_buf;
361 if (have_cmdline) --dst;
362 have_cmdline = 1;
363 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800364 if (warm_boot) {
365 if (have_cmdline) --dst;
366 src = warmboot_cmdline;
367 while ((*dst++ = *src++));
368 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800369
Pavel Nedev5614d222013-06-17 18:01:02 +0300370 if (boot_into_recovery && gpt_exists) {
371 src = secondary_gpt_enable;
372 if (have_cmdline) --dst;
373 while ((*dst++ = *src++));
374 }
375
Pavel Nedev328ac822013-04-05 15:25:11 +0300376 if (boot_into_ffbm) {
377 src = androidboot_mode;
378 if (have_cmdline) --dst;
379 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700380 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300381 if (have_cmdline) --dst;
382 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800383 src = loglevel;
384 if (have_cmdline) --dst;
385 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300386 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700387 src = battchg_pause;
388 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800389 while ((*dst++ = *src++));
390 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800391
Shashank Mittalcd98d472011-08-02 14:29:24 -0700392 if(target_use_signed_kernel() && auth_kernel_img) {
393 src = auth_kernel;
394 if (have_cmdline) --dst;
395 while ((*dst++ = *src++));
396 }
397
Ajay Dudanid04110c2011-01-17 23:55:07 -0800398 switch(target_baseband())
399 {
400 case BASEBAND_APQ:
401 src = baseband_apq;
402 if (have_cmdline) --dst;
403 while ((*dst++ = *src++));
404 break;
405
406 case BASEBAND_MSM:
407 src = baseband_msm;
408 if (have_cmdline) --dst;
409 while ((*dst++ = *src++));
410 break;
411
412 case BASEBAND_CSFB:
413 src = baseband_csfb;
414 if (have_cmdline) --dst;
415 while ((*dst++ = *src++));
416 break;
417
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800418 case BASEBAND_SVLTE2A:
419 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800420 if (have_cmdline) --dst;
421 while ((*dst++ = *src++));
422 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700423
424 case BASEBAND_MDM:
425 src = baseband_mdm;
426 if (have_cmdline) --dst;
427 while ((*dst++ = *src++));
428 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700429
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800430 case BASEBAND_MDM2:
431 src = baseband_mdm2;
432 if (have_cmdline) --dst;
433 while ((*dst++ = *src++));
434 break;
435
Amol Jadi5c61a952012-05-04 17:05:35 -0700436 case BASEBAND_SGLTE:
437 src = baseband_sglte;
438 if (have_cmdline) --dst;
439 while ((*dst++ = *src++));
440 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530441
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800442 case BASEBAND_SGLTE2:
443 src = baseband_sglte2;
444 if (have_cmdline) --dst;
445 while ((*dst++ = *src++));
446 break;
447
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530448 case BASEBAND_DSDA:
449 src = baseband_dsda;
450 if (have_cmdline) --dst;
451 while ((*dst++ = *src++));
452 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800453
454 case BASEBAND_DSDA2:
455 src = baseband_dsda2;
456 if (have_cmdline) --dst;
457 while ((*dst++ = *src++));
458 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800459 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700460
461 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700462 src = display_panel_buf;
463 if (have_cmdline) --dst;
464 while ((*dst++ = *src++));
465 }
Joonwoo Park61112782013-10-02 19:50:39 -0700466
467 if (have_target_boot_params) {
468 if (have_cmdline) --dst;
469 src = target_boot_params;
470 while ((*dst++ = *src++));
471 }
Neeti Desaie245d492012-06-01 12:52:13 -0700472 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700473
474
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700475 if (boot_dev_buf)
476 free(boot_dev_buf);
477
Maria Yu9ca0c402014-07-07 14:49:34 +0800478 dprintf(INFO, "cmdline: %s\n", cmdline_final ? cmdline_final : "");
Neeti Desaie245d492012-06-01 12:52:13 -0700479 return cmdline_final;
480}
481
482unsigned *atag_core(unsigned *ptr)
483{
484 /* CORE */
485 *ptr++ = 2;
486 *ptr++ = 0x54410001;
487
488 return ptr;
489
490}
491
492unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
493 unsigned ramdisk_size)
494{
495 if (ramdisk_size) {
496 *ptr++ = 4;
497 *ptr++ = 0x54420005;
498 *ptr++ = (unsigned)ramdisk;
499 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800500 }
501
Neeti Desaie245d492012-06-01 12:52:13 -0700502 return ptr;
503}
504
505unsigned *atag_ptable(unsigned **ptr_addr)
506{
507 int i;
508 struct ptable *ptable;
509
510 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700511 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
512 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700513 *(*ptr_addr)++ = 0x4d534d70;
514 for (i = 0; i < ptable->count; ++i)
515 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
516 }
517
518 return (*ptr_addr);
519}
520
521unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
522{
523 int cmdline_length = 0;
524 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700525 char *dest;
526
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800527 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700528 n = (cmdline_length + 4) & (~3);
529
530 *ptr++ = (n / 4) + 2;
531 *ptr++ = 0x54410009;
532 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800533 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700534 ptr += (n / 4);
535
536 return ptr;
537}
538
539unsigned *atag_end(unsigned *ptr)
540{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800541 /* END */
542 *ptr++ = 0;
543 *ptr++ = 0;
544
Neeti Desaie245d492012-06-01 12:52:13 -0700545 return ptr;
546}
547
548void generate_atags(unsigned *ptr, const char *cmdline,
549 void *ramdisk, unsigned ramdisk_size)
550{
551
552 ptr = atag_core(ptr);
553 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
554 ptr = target_atag_mem(ptr);
555
556 /* Skip NAND partition ATAGS for eMMC boot */
557 if (!target_is_emmc_boot()){
558 ptr = atag_ptable(&ptr);
559 }
560
561 ptr = atag_cmdline(ptr, cmdline);
562 ptr = atag_end(ptr);
563}
564
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700565typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700566void boot_linux(void *kernel, unsigned *tags,
567 const char *cmdline, unsigned machtype,
568 void *ramdisk, unsigned ramdisk_size)
569{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800570 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800571#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700572 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800573#endif
574
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700575 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800576 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800577 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800578
579 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700580
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800581 final_cmdline = update_cmdline((const char*)cmdline);
582
Neeti Desai17379b82012-06-04 18:42:53 -0700583#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800584 dprintf(INFO, "Updating device tree: start\n");
585
Neeti Desai17379b82012-06-04 18:42:53 -0700586 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800587 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700588 if(ret)
589 {
590 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
591 ASSERT(0);
592 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800593 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700594#else
Neeti Desaie245d492012-06-01 12:52:13 -0700595 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800596 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700597#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700598
Maria Yu52254c02014-07-04 16:14:54 +0800599 free(final_cmdline);
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700600 /* Perform target specific cleanup */
601 target_uninit();
602
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800603 /* Turn off splash screen if enabled */
604#if DISPLAY_SPLASH_SCREEN
605 target_display_shutdown();
606#endif
607
608
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800609 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
610 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800611
612 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700613
Amol Jadi4421e652011-06-16 15:00:48 -0700614 /* do any platform specific cleanup before kernel entry */
615 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700616
Brian Swetland9c4c0752009-01-25 16:23:50 -0800617 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700618
Amol Jadi504f9fe2012-08-16 13:56:48 -0700619#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800620 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700621#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700622 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800623
624 if (IS_ARM64(kptr))
625 /* Jump to a 64bit kernel */
626 scm_elexec_call((paddr_t)kernel, tags_phys);
627 else
628 /* Jump to a 32bit kernel */
629 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800630}
631
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700632/* Function to check if the memory address range falls within the aboot
633 * boundaries.
634 * start: Start of the memory region
635 * size: Size of the memory region
636 */
637int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
638{
639 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800640 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700641 return -1;
642
643 /* Check for memory overlap. */
644 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
645 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700646 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700647 return 0;
648 else
649 return -1;
650}
651
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800652#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800653
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800654BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800655#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800656BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800657#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800658
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700659static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
660{
661 int ret;
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800662#if IMAGE_VERIF_ALGO_SHA1
663 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
664#else
665 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
666#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700667
668 /* Assume device is rooted at this time. */
669 device.is_tampered = 1;
670
671 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
672
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700673#if VERIFIED_BOOT
674 if(boot_into_recovery)
675 {
676 ret = boot_verify_image((unsigned char *)bootimg_addr,
677 bootimg_size, "recovery");
678 }
679 else
680 {
681 ret = boot_verify_image((unsigned char *)bootimg_addr,
682 bootimg_size, "boot");
683 }
684 boot_verify_print_state();
685#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700686 ret = image_verify((unsigned char *)bootimg_addr,
687 (unsigned char *)(bootimg_addr + bootimg_size),
688 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800689 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700690#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700691 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
692
693 if (ret)
694 {
695 /* Authorized kernel */
696 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700697 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700698 }
699
700#if USE_PCOM_SECBOOT
701 set_tamper_flag(device.is_tampered);
702#endif
703
704 if(device.is_tampered)
705 {
706 write_device_info_mmc(&device);
707 #ifdef TZ_TAMPER_FUSE
708 set_tamper_fuse_cmd();
709 #endif
710 #ifdef ASSERT_ON_TAMPER
711 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
712 ASSERT(0);
713 #endif
714 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700715
716#if VERIFIED_BOOT
717 if(boot_verify_get_state() == RED)
718 {
719 if(!boot_into_recovery)
720 {
721 dprintf(CRITICAL,
722 "Device verification failed. Rebooting into recovery.\n");
723 reboot_device(RECOVERY_MODE);
724 }
725 else
726 {
727 dprintf(CRITICAL,
728 "Recovery image verification failed. Asserting..\n");
729 ASSERT(0);
730 }
731 }
732#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700733}
734
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530735static bool check_format_bit()
736{
737 bool ret = false;
738 int index;
739 uint64_t offset;
740 struct boot_selection_info *in = NULL;
741 char *buf = NULL;
742
743 index = partition_get_index("bootselect");
744 if (index == INVALID_PTN)
745 {
746 dprintf(INFO, "Unable to locate /bootselect partition\n");
747 return ret;
748 }
749 offset = partition_get_offset(index);
750 if(!offset)
751 {
752 dprintf(INFO, "partition /bootselect doesn't exist\n");
753 return ret;
754 }
755 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
756 ASSERT(buf);
757 if (mmc_read(offset, (unsigned int *)buf, page_size))
758 {
759 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
760 free(buf);
761 return ret;
762 }
763 in = (struct boot_selection_info *) buf;
764 if ((in->signature == BOOTSELECT_SIGNATURE) &&
765 (in->version == BOOTSELECT_VERSION)) {
766 if ((in->state_info & BOOTSELECT_FORMAT) &&
767 !(in->state_info & BOOTSELECT_FACTORY))
768 ret = true;
769 } else {
770 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
771 in->signature, in->version);
772 ASSERT(0);
773 }
774 free(buf);
775 return ret;
776}
777
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700778void boot_verifier_init()
779{
780
781 uint32_t boot_state;
782 /* Check if device unlock */
783 if(device.is_unlocked)
784 {
785 boot_verify_send_event(DEV_UNLOCK);
786 boot_verify_print_state();
787 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
788 return;
789 }
790 else
791 {
792 boot_verify_send_event(BOOT_INIT);
793 }
794
795 /* Initialize keystore */
796 boot_state = boot_verify_keystore_init();
797 if(boot_state == YELLOW)
798 {
799 boot_verify_print_state();
800 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
801 }
802}
803
Shashank Mittal23b8f422010-04-16 19:27:21 -0700804int boot_linux_from_mmc(void)
805{
806 struct boot_img_hdr *hdr = (void*) buf;
807 struct boot_img_hdr *uhdr;
808 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700809 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700810 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700811 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700812
Shashank Mittalcd98d472011-08-02 14:29:24 -0700813 unsigned char *image_addr = 0;
814 unsigned kernel_actual;
815 unsigned ramdisk_actual;
816 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700817 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700818
819#if DEVICE_TREE
820 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700821 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700822 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800823 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700824 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700825#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800826 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
827 struct kernel64_hdr *kptr = (void*) kbuf;
828
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530829 if (check_format_bit())
830 boot_into_recovery = 1;
831
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700832 if (!boot_into_recovery) {
833 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
834 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
835 if (rcode <= 0) {
836 boot_into_ffbm = false;
837 if (rcode < 0)
838 dprintf(CRITICAL,"failed to get ffbm cookie");
839 } else
840 boot_into_ffbm = true;
841 } else
842 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700843 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
844 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
845 dprintf(INFO, "Unified boot method!\n");
846 hdr = uhdr;
847 goto unified_boot;
848 }
Greg Griscod6250552011-06-29 14:40:23 -0700849 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700850 index = partition_get_index("boot");
851 ptn = partition_get_offset(index);
852 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700853 dprintf(CRITICAL, "ERROR: No boot partition found\n");
854 return -1;
855 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700856 }
857 else {
858 index = partition_get_index("recovery");
859 ptn = partition_get_offset(index);
860 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700861 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
862 return -1;
863 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700864 }
865
Greg Griscod6250552011-06-29 14:40:23 -0700866 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700867 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
868 return -1;
869 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700870
871 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700872 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700873 return -1;
874 }
875
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700876 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530877
878 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
879 dprintf(CRITICAL, "ERROR: Invalid page size\n");
880 return -1;
881 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700882 page_size = hdr->page_size;
883 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700884 }
885
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800886 /* Read the next page to get kernel Image header
887 * which lives in the second page for arm64 targets.
888 */
889
890 if (mmc_read(ptn + page_size, (unsigned int *) kbuf, page_size)) {
891 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
892 return -1;
893 }
894
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700895 /*
896 * Update the kernel/ramdisk/tags address if the boot image header
897 * has default values, these default values come from mkbootimg when
898 * the boot image is flashed using fastboot flash:raw
899 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800900 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700901
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700902 /* Get virtual addresses since the hdr saves physical addresses. */
903 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
904 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
905 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700906
907 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
908 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
909
910 /* Check if the addresses in the header are valid. */
911 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
912 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
913 {
914 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
915 return -1;
916 }
917
918#ifndef DEVICE_TREE
919 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
920 {
921 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
922 return -1;
923 }
924#endif
925
Shashank Mittalcd98d472011-08-02 14:29:24 -0700926 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300927 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
928 (int) target_use_signed_kernel(),
929 device.is_unlocked,
930 device.is_tampered);
931
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700932#if VERIFIED_BOOT
933 boot_verifier_init();
934#endif
935
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700936 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800937 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700938 offset = 0;
939
Shashank Mittalcd98d472011-08-02 14:29:24 -0700940 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700941
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800942#if DEVICE_TREE
943 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
944 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700945
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700946 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700947 {
948 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
949 return -1;
950 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800951#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530952 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700953
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700954#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700955
Amol Jadib6be5c12012-11-14 13:39:51 -0800956 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700957 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800958
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700959 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
960 {
961 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
962 return -1;
963 }
964
Shashank Mittalcd98d472011-08-02 14:29:24 -0700965 /* Read image without signature */
966 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
967 {
968 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
969 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800970 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700971
Amol Jadib6be5c12012-11-14 13:39:51 -0800972 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700973 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800974
Shashank Mittalcd98d472011-08-02 14:29:24 -0700975 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700976
977 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
978 {
979 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
980 return -1;
981 }
982
Shashank Mittalcd98d472011-08-02 14:29:24 -0700983 /* Read signature */
984 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
985 {
986 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700987 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700988 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800989
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700990 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700991
Neeti Desai465491e2012-07-31 12:53:35 -0700992 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700993 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
994 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700995
Neeti Desai465491e2012-07-31 12:53:35 -0700996 #if DEVICE_TREE
997 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700998 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700999 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001000
Deepa Dinamani19648b42013-09-05 17:05:55 -07001001 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001002 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1003 return -1;
1004 }
Deepa Dinamani19648b42013-09-05 17:05:55 -07001005
Neeti Desai465491e2012-07-31 12:53:35 -07001006 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -07001007 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001008 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
1009 return -1;
1010 }
1011
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001012 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001013 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001014 {
1015 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1016 return -1;
1017 }
1018
Joel Kingaa335dc2013-06-03 16:11:08 -07001019 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001020 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -08001021 /*
1022 * If appended dev tree is found, update the atags with
1023 * memory address to the DTB appended location on RAM.
1024 * Else update with the atags address in the kernel header
1025 */
1026 void *dtb;
1027 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001028 hdr->kernel_size,
1029 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001030 if (!dtb) {
1031 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1032 return -1;
1033 }
Neeti Desai465491e2012-07-31 12:53:35 -07001034 }
1035 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001036 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001037 else
1038 {
Amol Jadib6be5c12012-11-14 13:39:51 -08001039 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001040
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001041 image_addr = (unsigned char *)target_get_scratch_address();
1042#if DEVICE_TREE
1043 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1044 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1045
1046 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
1047 {
1048 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1049 return -1;
1050 }
1051#else
1052 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1053
1054#endif
1055 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
1056 {
1057 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1058 return -1;
1059 }
1060
Amol Jadib6be5c12012-11-14 13:39:51 -08001061 dprintf(INFO, "Loading boot image (%d): start\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001062 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001063 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001064
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001065 offset = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001066
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001067 /* Load the entire boot image */
1068 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual)) {
1069 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
Shashank Mittalcd98d472011-08-02 14:29:24 -07001070 return -1;
1071 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001072
1073 dprintf(INFO, "Loading boot image (%d): done\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001074 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001075 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -07001076
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001077 #ifdef TZ_SAVE_KERNEL_HASH
1078 aboot_save_boot_hash_mmc(image_addr, imagesize_actual);
1079 #endif /* TZ_SAVE_KERNEL_HASH */
1080
1081 /* Move kernel, ramdisk and device tree to correct address */
1082 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1083 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Neeti Desai465491e2012-07-31 12:53:35 -07001084
1085 #if DEVICE_TREE
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001086 if(hdr->dt_size) {
1087 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1088 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001089
Deepa Dinamani19648b42013-09-05 17:05:55 -07001090 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001091 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1092 return -1;
1093 }
1094
Joel Kingaa335dc2013-06-03 16:11:08 -07001095 /* Find index of device tree within device tree table */
1096 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001097 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1098 return -1;
1099 }
1100
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001101 /* Validate and Read device device tree in the tags_addr */
Joel Kingaa335dc2013-06-03 16:11:08 -07001102 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001103 {
1104 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1105 return -1;
1106 }
1107
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001108 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001109 } else {
Aparna Mallavarapu33127372014-06-03 22:15:54 +05301110 /* Validate the tags_addr */
1111 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1112 {
1113 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1114 return -1;
1115 }
Dima Zavin77e41f32013-03-06 16:10:43 -08001116 /*
1117 * If appended dev tree is found, update the atags with
1118 * memory address to the DTB appended location on RAM.
1119 * Else update with the atags address in the kernel header
1120 */
1121 void *dtb;
1122 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001123 kernel_actual,
1124 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001125 if (!dtb) {
1126 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1127 return -1;
1128 }
Neeti Desai465491e2012-07-31 12:53:35 -07001129 }
1130 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001131 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001132
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001133 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1134 target_load_ssd_keystore();
1135
Shashank Mittal23b8f422010-04-16 19:27:21 -07001136unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001137
Dima Zavin77e41f32013-03-06 16:10:43 -08001138 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001139 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001140 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1141
1142 return 0;
1143}
1144
Dima Zavin214cc642009-01-26 11:16:21 -08001145int boot_linux_from_flash(void)
1146{
1147 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001148 struct ptentry *ptn;
1149 struct ptable *ptable;
1150 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001151
Shashank Mittalcd98d472011-08-02 14:29:24 -07001152 unsigned char *image_addr = 0;
1153 unsigned kernel_actual;
1154 unsigned ramdisk_actual;
1155 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001156 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001157
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001158#if DEVICE_TREE
1159 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001160 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001161 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001162 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001163#endif
1164
David Ng183a7422009-12-07 14:55:21 -08001165 if (target_is_emmc_boot()) {
1166 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1167 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1168 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1169 return -1;
1170 }
1171 goto continue_boot;
1172 }
1173
Dima Zavin214cc642009-01-26 11:16:21 -08001174 ptable = flash_get_ptable();
1175 if (ptable == NULL) {
1176 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1177 return -1;
1178 }
1179
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001180 if(!boot_into_recovery)
1181 {
1182 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001183
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001184 if (ptn == NULL) {
1185 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1186 return -1;
1187 }
1188 }
1189 else
1190 {
1191 ptn = ptable_find(ptable, "recovery");
1192 if (ptn == NULL) {
1193 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1194 return -1;
1195 }
Dima Zavin214cc642009-01-26 11:16:21 -08001196 }
1197
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001198 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001199 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1200 return -1;
1201 }
Dima Zavin214cc642009-01-26 11:16:21 -08001202
1203 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001204 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001205 return -1;
1206 }
1207
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001208 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001209 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 -08001210 return -1;
1211 }
1212
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001213 /*
1214 * Update the kernel/ramdisk/tags address if the boot image header
1215 * has default values, these default values come from mkbootimg when
1216 * the boot image is flashed using fastboot flash:raw
1217 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001218 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001219
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001220 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001221 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1222 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1223 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1224
1225 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1226 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1227
1228 /* Check if the addresses in the header are valid. */
1229 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1230 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1231 {
1232 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1233 return -1;
1234 }
1235
1236#ifndef DEVICE_TREE
1237 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1238 {
1239 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1240 return -1;
1241 }
1242#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001243
Shashank Mittalcd98d472011-08-02 14:29:24 -07001244 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001245 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001246 {
1247 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001248 offset = 0;
1249
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001250#if DEVICE_TREE
1251 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1252 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001253
1254 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1255 {
1256 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1257 return -1;
1258 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001259#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001260 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001261#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001262
Amol Jadib6be5c12012-11-14 13:39:51 -08001263 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001264 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001265
Shashank Mittalcd98d472011-08-02 14:29:24 -07001266 /* Read image without signature */
1267 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1268 {
1269 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1270 return -1;
1271 }
Dima Zavin214cc642009-01-26 11:16:21 -08001272
Amol Jadib6be5c12012-11-14 13:39:51 -08001273 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001274 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001275
Shashank Mittalcd98d472011-08-02 14:29:24 -07001276 offset = imagesize_actual;
1277 /* Read signature */
1278 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1279 {
1280 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001281 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001282 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001283
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001284 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001285
1286 /* Move kernel and ramdisk to correct address */
1287 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1288 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001289#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001290 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001291 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001292 {
1293 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1294 return -1;
1295 }
1296
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001297 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1298#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001299
1300 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001301 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001302 {
1303 write_device_info_flash(&device);
1304 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301305#if USE_PCOM_SECBOOT
1306 set_tamper_flag(device.is_tampered);
1307#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001308 }
1309 else
1310 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001311 offset = page_size;
1312
Amol Jadib6be5c12012-11-14 13:39:51 -08001313 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1314 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1315 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1316
1317 dprintf(INFO, "Loading boot image (%d): start\n",
1318 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001319 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001320
1321 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001322 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1323 return -1;
1324 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001325 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001326
Amol Jadib6be5c12012-11-14 13:39:51 -08001327 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001328 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1329 return -1;
1330 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001331 offset += ramdisk_actual;
1332
1333 dprintf(INFO, "Loading boot image (%d): done\n",
1334 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001335 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001336
1337 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001338 offset += second_actual;
1339 /* Second image loading not implemented. */
1340 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001341 }
1342
1343#if DEVICE_TREE
1344 if(hdr->dt_size != 0) {
1345
1346 /* Read the device tree table into buffer */
1347 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1348 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1349 return -1;
1350 }
1351
1352 table = (struct dt_table*) dt_buf;
1353
Deepa Dinamani19648b42013-09-05 17:05:55 -07001354 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001355 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1356 return -1;
1357 }
1358
Deepa Dinamani19648b42013-09-05 17:05:55 -07001359 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1360 if (!table)
1361 return -1;
1362
1363 /* Read the entire device tree table into buffer */
1364 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1365 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1366 return -1;
1367 }
1368
1369
Joel Kingaa335dc2013-06-03 16:11:08 -07001370 /* Find index of device tree within device tree table */
1371 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001372 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1373 return -1;
1374 }
1375
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001376 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001377 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001378 {
1379 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1380 return -1;
1381 }
1382
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001383 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001384 if(flash_read(ptn, offset + dt_entry.offset,
1385 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001386 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1387 return -1;
1388 }
1389 }
1390#endif
1391
Shashank Mittalcd98d472011-08-02 14:29:24 -07001392 }
David Ng183a7422009-12-07 14:55:21 -08001393continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001394
Dima Zavin214cc642009-01-26 11:16:21 -08001395 /* TODO: create/pass atags to kernel */
1396
Ajay Dudanie28a6072011-07-01 13:59:46 -07001397 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001398 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001399 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1400
1401 return 0;
1402}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001403
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001404BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001405void write_device_info_mmc(device_info *dev)
1406{
1407 struct device_info *info = (void*) info_buf;
1408 unsigned long long ptn = 0;
1409 unsigned long long size;
1410 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001411 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001412 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001413
1414 index = partition_get_index("aboot");
1415 ptn = partition_get_offset(index);
1416 if(ptn == 0)
1417 {
1418 return;
1419 }
1420
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001421 lun = partition_get_lun(index);
1422 mmc_set_lun(lun);
1423
Shashank Mittal162244e2011-08-08 19:01:25 -07001424 size = partition_get_size(index);
1425
1426 memcpy(info, dev, sizeof(device_info));
1427
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001428 blocksize = mmc_get_device_blocksize();
1429
1430 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Shashank Mittal162244e2011-08-08 19:01:25 -07001431 {
1432 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1433 return;
1434 }
1435}
1436
1437void read_device_info_mmc(device_info *dev)
1438{
1439 struct device_info *info = (void*) info_buf;
1440 unsigned long long ptn = 0;
1441 unsigned long long size;
1442 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001443 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001444
1445 index = partition_get_index("aboot");
1446 ptn = partition_get_offset(index);
1447 if(ptn == 0)
1448 {
1449 return;
1450 }
1451
1452 size = partition_get_size(index);
1453
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001454 blocksize = mmc_get_device_blocksize();
1455
1456 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Shashank Mittal162244e2011-08-08 19:01:25 -07001457 {
1458 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1459 return;
1460 }
1461
1462 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1463 {
1464 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1465 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001466 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001467 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001468
1469 write_device_info_mmc(info);
1470 }
1471 memcpy(dev, info, sizeof(device_info));
1472}
1473
1474void write_device_info_flash(device_info *dev)
1475{
1476 struct device_info *info = (void *) info_buf;
1477 struct ptentry *ptn;
1478 struct ptable *ptable;
1479
1480 ptable = flash_get_ptable();
1481 if (ptable == NULL)
1482 {
1483 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1484 return;
1485 }
1486
1487 ptn = ptable_find(ptable, "devinfo");
1488 if (ptn == NULL)
1489 {
1490 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1491 return;
1492 }
1493
1494 memcpy(info, dev, sizeof(device_info));
1495
1496 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1497 {
1498 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1499 return;
1500 }
1501}
1502
1503void read_device_info_flash(device_info *dev)
1504{
1505 struct device_info *info = (void*) info_buf;
1506 struct ptentry *ptn;
1507 struct ptable *ptable;
1508
1509 ptable = flash_get_ptable();
1510 if (ptable == NULL)
1511 {
1512 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1513 return;
1514 }
1515
1516 ptn = ptable_find(ptable, "devinfo");
1517 if (ptn == NULL)
1518 {
1519 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1520 return;
1521 }
1522
1523 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1524 {
1525 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1526 return;
1527 }
1528
1529 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1530 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001531 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1532 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001533 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001534 write_device_info_flash(info);
1535 }
1536 memcpy(dev, info, sizeof(device_info));
1537}
1538
1539void write_device_info(device_info *dev)
1540{
1541 if(target_is_emmc_boot())
1542 {
1543 write_device_info_mmc(dev);
1544 }
1545 else
1546 {
1547 write_device_info_flash(dev);
1548 }
1549}
1550
1551void read_device_info(device_info *dev)
1552{
1553 if(target_is_emmc_boot())
1554 {
1555 read_device_info_mmc(dev);
1556 }
1557 else
1558 {
1559 read_device_info_flash(dev);
1560 }
1561}
1562
1563void reset_device_info()
1564{
1565 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001566 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001567 write_device_info(&device);
1568}
1569
1570void set_device_root()
1571{
1572 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001573 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001574 write_device_info(&device);
1575}
1576
Amol Jadicb524072012-08-09 16:40:18 -07001577#if DEVICE_TREE
1578int copy_dtb(uint8_t *boot_image_start)
1579{
1580 uint32 dt_image_offset = 0;
1581 uint32_t n;
1582 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001583 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001584 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001585
1586 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1587
Amol Jadicb524072012-08-09 16:40:18 -07001588 if(hdr->dt_size != 0) {
1589
1590 /* add kernel offset */
1591 dt_image_offset += page_size;
1592 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1593 dt_image_offset += n;
1594
1595 /* add ramdisk offset */
1596 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1597 dt_image_offset += n;
1598
1599 /* add second offset */
1600 if(hdr->second_size != 0) {
1601 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1602 dt_image_offset += n;
1603 }
1604
1605 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001606 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001607
Deepa Dinamani19648b42013-09-05 17:05:55 -07001608 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001609 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1610 return -1;
1611 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001612 /* Find index of device tree within device tree table */
1613 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001614 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1615 return -1;
1616 }
1617
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001618 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001619 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001620 {
1621 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1622 return -1;
1623 }
1624
Amol Jadicb524072012-08-09 16:40:18 -07001625 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001626 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001627 boot_image_start + dt_image_offset + dt_entry.offset,
1628 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001629 } else
1630 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001631
1632 /* Everything looks fine. Return success. */
1633 return 0;
1634}
1635#endif
1636
Brian Swetland9c4c0752009-01-25 16:23:50 -08001637void cmd_boot(const char *arg, void *data, unsigned sz)
1638{
1639 unsigned kernel_actual;
1640 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001641 uint32_t image_actual;
1642 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001643 uint32_t sig_actual = SIGNATURE_SIZE;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001644 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001645 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001646 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001647 int ret = 0;
1648 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001649
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001650#if VERIFIED_BOOT
1651 if(!device.is_unlocked)
1652 {
1653 fastboot_fail("unlock device to use this command");
1654 return;
1655 }
1656#endif
1657
Brian Swetland9c4c0752009-01-25 16:23:50 -08001658 if (sz < sizeof(hdr)) {
1659 fastboot_fail("invalid bootimage header");
1660 return;
1661 }
1662
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001663 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001664
1665 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001666 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001667
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001668 if(target_is_emmc_boot() && hdr->page_size) {
1669 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001670 page_mask = page_size - 1;
1671 }
1672
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001673 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1674 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001675#if DEVICE_TREE
1676 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1677#endif
1678
1679 image_actual = ADD_OF(page_size, kernel_actual);
1680 image_actual = ADD_OF(image_actual, ramdisk_actual);
1681 image_actual = ADD_OF(image_actual, dt_actual);
1682
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001683 if (target_use_signed_kernel() && (!device.is_unlocked))
1684 image_actual = ADD_OF(image_actual, sig_actual);
1685
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001686 /* sz should have atleast raw boot image */
1687 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001688 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001689 return;
1690 }
1691
1692 /* Verify the boot image
1693 * device & page_size are initialized in aboot_init
1694 */
1695 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001696 /* Pass size excluding signature size, otherwise we would try to
1697 * access signature beyond its length
1698 */
1699 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001700
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001701 /*
1702 * Update the kernel/ramdisk/tags address if the boot image header
1703 * has default values, these default values come from mkbootimg when
1704 * the boot image is flashed using fastboot flash:raw
1705 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001706 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1707 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001708
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001709 /* Get virtual addresses since the hdr saves physical addresses. */
1710 hdr->kernel_addr = VA(hdr->kernel_addr);
1711 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1712 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001713
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001714 /* Check if the addresses in the header are valid. */
1715 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1716 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1717 {
1718 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001719 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001720 }
1721
Amol Jadicb524072012-08-09 16:40:18 -07001722#if DEVICE_TREE
1723 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001724 ret = copy_dtb(data);
1725
1726 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001727#else
1728 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1729 {
1730 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001731 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001732 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001733#endif
1734
1735 /* Load ramdisk & kernel */
1736 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1737 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1738
1739#if DEVICE_TREE
1740 /*
1741 * If dtb is not found look for appended DTB in the kernel.
1742 * If appended dev tree is found, update the atags with
1743 * memory address to the DTB appended location on RAM.
1744 * Else update with the atags address in the kernel header
1745 */
1746 if (!dtb_copied) {
1747 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001748 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1749 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001750 if (!dtb) {
1751 fastboot_fail("dtb not found");
1752 return;
1753 }
Amol Jadicb524072012-08-09 16:40:18 -07001754 }
1755#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001756
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001757#ifndef DEVICE_TREE
1758 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1759 {
1760 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001761 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001762 }
1763#endif
1764
Brian Swetland9c4c0752009-01-25 16:23:50 -08001765 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001766 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001767
Dima Zavin77e41f32013-03-06 16:10:43 -08001768 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001769 (const char*) hdr->cmdline, board_machtype(),
1770 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001771}
1772
Dima Zavin214cc642009-01-26 11:16:21 -08001773void cmd_erase(const char *arg, void *data, unsigned sz)
1774{
1775 struct ptentry *ptn;
1776 struct ptable *ptable;
1777
1778 ptable = flash_get_ptable();
1779 if (ptable == NULL) {
1780 fastboot_fail("partition table doesn't exist");
1781 return;
1782 }
1783
1784 ptn = ptable_find(ptable, arg);
1785 if (ptn == NULL) {
1786 fastboot_fail("unknown partition name");
1787 return;
1788 }
1789
1790 if (flash_erase(ptn)) {
1791 fastboot_fail("failed to erase partition");
1792 return;
1793 }
1794 fastboot_okay("");
1795}
1796
Bikas Gurungd48bd242010-09-04 19:54:32 -07001797
1798void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1799{
Kun Liang2f1601a2013-08-12 16:29:54 +08001800 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001801 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001802 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001803 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001804 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001805
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001806#if VERIFIED_BOOT
1807 if(!strcmp(arg, KEYSTORE_PTN_NAME))
1808 {
1809 if(!device.is_unlocked)
1810 {
1811 fastboot_fail("unlock device to erase keystore");
1812 return;
1813 }
1814 }
1815#endif
1816
Kinson Chikf1a43512011-07-14 11:28:39 -07001817 index = partition_get_index(arg);
1818 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001819 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001820
Kinson Chikf1a43512011-07-14 11:28:39 -07001821 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001822 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001823 return;
1824 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001825
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001826 lun = partition_get_lun(index);
1827 mmc_set_lun(lun);
1828
Oliver Wangcee448d2013-10-22 18:40:13 +08001829#if MMC_SDHCI_SUPPORT
1830 if (mmc_erase_card(ptn, size)) {
1831 fastboot_fail("failed to erase partition\n");
1832 return;
1833 }
1834#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001835 size = partition_get_size(index);
1836 if (size > DEFAULT_ERASE_SIZE)
1837 size = DEFAULT_ERASE_SIZE;
1838
neetidb4b24d62012-01-20 12:13:09 -08001839 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001840 0 in first several blocks */
1841 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001842 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001843 return;
1844 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001845#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001846 fastboot_okay("");
1847}
1848
1849
Ajay Dudani5c761132011-04-07 20:19:04 -07001850void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001851{
1852 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001853 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001854 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001855 char *token = NULL;
1856 char *pname = NULL;
1857 uint8_t lun = 0;
1858 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001859
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001860 token = strtok(arg, ":");
1861 pname = token;
1862 token = strtok(NULL, ":");
1863 if(token)
1864 {
1865 lun = atoi(token);
1866 mmc_set_lun(lun);
1867 lun_set = true;
1868 }
1869
Mao Jinlong226f33a2014-07-04 17:24:10 +08001870 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07001871 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08001872 if (!strcmp(pname, "partition"))
1873 {
1874 dprintf(INFO, "Attempt to write partition image.\n");
1875 if (write_partition(sz, (unsigned char *) data)) {
1876 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07001877 return;
1878 }
1879 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08001880 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001881 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001882#if VERIFIED_BOOT
1883 if(!strcmp(pname, KEYSTORE_PTN_NAME))
1884 {
1885 if(!device.is_unlocked)
1886 {
1887 fastboot_fail("unlock device to flash keystore");
1888 return;
1889 }
1890 if(!boot_verify_validate_keystore((unsigned char *)data))
1891 {
1892 fastboot_fail("image is not a keystore file");
1893 return;
1894 }
1895 }
1896#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08001897 index = partition_get_index(pname);
1898 ptn = partition_get_offset(index);
1899 if(ptn == 0) {
1900 fastboot_fail("partition table doesn't exist");
1901 return;
1902 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001903
Mao Jinlong226f33a2014-07-04 17:24:10 +08001904 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
1905 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1906 fastboot_fail("image is not a boot image");
1907 return;
1908 }
1909 }
1910
1911 if(!lun_set)
1912 {
1913 lun = partition_get_lun(index);
1914 mmc_set_lun(lun);
1915 }
1916
1917 size = partition_get_size(index);
1918 if (ROUND_TO_PAGE(sz,511) > size) {
1919 fastboot_fail("size too large");
1920 return;
1921 }
1922 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1923 fastboot_fail("flash write failure");
1924 return;
1925 }
Greg Grisco6e754772011-06-23 12:19:39 -07001926 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001927 }
1928 fastboot_okay("");
1929 return;
1930}
1931
Ajay Dudani5c761132011-04-07 20:19:04 -07001932void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1933{
1934 unsigned int chunk;
1935 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001936 uint32_t *fill_buf = NULL;
1937 uint32_t fill_val;
1938 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001939 sparse_header_t *sparse_header;
1940 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001941 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001942 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301943 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001944 int index = INVALID_PTN;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001945 int i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001946 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001947
Kinson Chikf1a43512011-07-14 11:28:39 -07001948 index = partition_get_index(arg);
1949 ptn = partition_get_offset(index);
1950 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001951 fastboot_fail("partition table doesn't exist");
1952 return;
1953 }
1954
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301955 size = partition_get_size(index);
1956 if (ROUND_TO_PAGE(sz,511) > size) {
1957 fastboot_fail("size too large");
1958 return;
1959 }
1960
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001961 lun = partition_get_lun(index);
1962 mmc_set_lun(lun);
1963
Ajay Dudani5c761132011-04-07 20:19:04 -07001964 /* Read and skip over sparse image header */
1965 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001966 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1967 fastboot_fail("size too large");
1968 return;
1969 }
1970
Ajay Dudani5c761132011-04-07 20:19:04 -07001971 data += sparse_header->file_hdr_sz;
1972 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1973 {
1974 /* Skip the remaining bytes in a header that is longer than
1975 * we expected.
1976 */
1977 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1978 }
1979
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001980 dprintf (SPEW, "=== Sparse Image Header ===\n");
1981 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1982 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1983 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1984 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1985 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1986 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1987 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1988 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001989
1990 /* Start processing chunks */
1991 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1992 {
1993 /* Read and skip over chunk header */
1994 chunk_header = (chunk_header_t *) data;
1995 data += sizeof(chunk_header_t);
1996
1997 dprintf (SPEW, "=== Chunk Header ===\n");
1998 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1999 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2000 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2001
2002 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
2003 {
2004 /* Skip the remaining bytes in a header that is longer than
2005 * we expected.
2006 */
2007 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
2008 }
2009
2010 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
2011 switch (chunk_header->chunk_type)
2012 {
2013 case CHUNK_TYPE_RAW:
2014 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2015 chunk_data_sz))
2016 {
2017 fastboot_fail("Bogus chunk size for chunk type Raw");
2018 return;
2019 }
2020
Ajay Dudaniab18f022011-05-12 14:39:22 -07002021 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2022 chunk_data_sz,
2023 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002024 {
2025 fastboot_fail("flash write failure");
2026 return;
2027 }
2028 total_blocks += chunk_header->chunk_sz;
2029 data += chunk_data_sz;
2030 break;
2031
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002032 case CHUNK_TYPE_FILL:
2033 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2034 sizeof(uint32_t)))
2035 {
2036 fastboot_fail("Bogus chunk size for chunk type FILL");
2037 return;
2038 }
2039
2040 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2041 if (!fill_buf)
2042 {
2043 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2044 return;
2045 }
2046
2047 fill_val = *(uint32_t *)data;
2048 data = (char *) data + sizeof(uint32_t);
2049 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
2050
2051 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2052 {
2053 fill_buf[i] = fill_val;
2054 }
2055
2056 for (i = 0; i < chunk_blk_cnt; i++)
2057 {
2058 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2059 sparse_header->blk_sz,
2060 fill_buf))
2061 {
2062 fastboot_fail("flash write failure");
2063 free(fill_buf);
2064 return;
2065 }
2066
2067 total_blocks++;
2068 }
2069
2070 free(fill_buf);
2071 break;
2072
Ajay Dudani5c761132011-04-07 20:19:04 -07002073 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002074 total_blocks += chunk_header->chunk_sz;
2075 break;
2076
Ajay Dudani5c761132011-04-07 20:19:04 -07002077 case CHUNK_TYPE_CRC:
2078 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2079 {
2080 fastboot_fail("Bogus chunk size for chunk type Dont Care");
2081 return;
2082 }
2083 total_blocks += chunk_header->chunk_sz;
2084 data += chunk_data_sz;
2085 break;
2086
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002087 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002088 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002089 fastboot_fail("Unknown chunk type");
2090 return;
2091 }
2092 }
2093
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002094 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2095 total_blocks, sparse_header->total_blks);
2096
2097 if(total_blocks != sparse_header->total_blks)
2098 {
2099 fastboot_fail("sparse image write failure");
2100 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002101
2102 fastboot_okay("");
2103 return;
2104}
2105
2106void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2107{
2108 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002109 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2110 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002111
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002112#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002113 int ret=0;
2114 uint32 major_version=0;
2115 uint32 minor_version=0;
2116
2117 ret = scm_svc_version(&major_version,&minor_version);
2118 if(!ret)
2119 {
2120 if(major_version >= 2)
2121 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002122 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002123 {
2124 ret = encrypt_scm((uint32 **) &data, &sz);
2125 if (ret != 0) {
2126 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2127 return;
2128 }
2129
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002130 /* Protect only for SSD */
2131 if (!strcmp(arg, "ssd")) {
2132 ret = scm_protect_keystore((uint32 *) data, sz);
2133 if (ret != 0) {
2134 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2135 return;
2136 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002137 }
2138 }
2139 else
2140 {
2141 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2142 if(ret != 0)
2143 {
2144 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2145 return;
2146 }
2147 }
2148 }
2149 else
2150 {
2151 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2152 magic_number[1] == DECRYPT_MAGIC_1)
2153 {
2154 ret = decrypt_scm((uint32 **) &data, &sz);
2155 if (ret != 0) {
2156 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2157 return;
2158 }
2159 }
2160 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2161 magic_number[1] == ENCRYPT_MAGIC_1)
2162 {
2163 ret = encrypt_scm((uint32 **) &data, &sz);
2164 if (ret != 0) {
2165 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2166 return;
2167 }
2168 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002169 }
2170 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002171 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002172 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002173 dprintf(CRITICAL,"INVALID SVC Version\n");
2174 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002175 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002176#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002177
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002178#if VERIFIED_BOOT
2179 if(!device.is_unlocked && !device.is_verified)
2180 {
2181 fastboot_fail("device is locked. Cannot flash images");
2182 return;
2183 }
2184 if(!device.is_unlocked && device.is_verified)
2185 {
2186 if(!boot_verify_flash_allowed(arg))
2187 {
2188 fastboot_fail("cannot flash this partition in verified state");
2189 return;
2190 }
2191 }
2192#endif
2193
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002194 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002195 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
2196 cmd_flash_mmc_img(arg, data, sz);
2197 else
2198 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002199 return;
2200}
2201
Dima Zavin214cc642009-01-26 11:16:21 -08002202void cmd_flash(const char *arg, void *data, unsigned sz)
2203{
2204 struct ptentry *ptn;
2205 struct ptable *ptable;
2206 unsigned extra = 0;
2207
2208 ptable = flash_get_ptable();
2209 if (ptable == NULL) {
2210 fastboot_fail("partition table doesn't exist");
2211 return;
2212 }
2213
2214 ptn = ptable_find(ptable, arg);
2215 if (ptn == NULL) {
2216 fastboot_fail("unknown partition name");
2217 return;
2218 }
2219
2220 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2221 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2222 fastboot_fail("image is not a boot image");
2223 return;
2224 }
2225 }
2226
Amol Jadi5c61a952012-05-04 17:05:35 -07002227 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002228 || !strcmp(ptn->name, "userdata")
2229 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002230 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002231 || !strcmp(ptn->name, "modem"))
2232 {
2233 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07002234 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002235 else
2236 extra = 0;
2237 }
2238 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002239 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002240
2241 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2242 if (flash_write(ptn, extra, data, sz)) {
2243 fastboot_fail("flash write failure");
2244 return;
2245 }
2246 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2247 fastboot_okay("");
2248}
2249
2250void cmd_continue(const char *arg, void *data, unsigned sz)
2251{
2252 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002253 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002254
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002255 if (target_is_emmc_boot())
2256 {
2257 boot_linux_from_mmc();
2258 }
2259 else
2260 {
2261 boot_linux_from_flash();
2262 }
Dima Zavin214cc642009-01-26 11:16:21 -08002263}
2264
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002265void cmd_reboot(const char *arg, void *data, unsigned sz)
2266{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002267 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002268 fastboot_okay("");
2269 reboot_device(0);
2270}
2271
2272void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2273{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002274 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002275 fastboot_okay("");
2276 reboot_device(FASTBOOT_MODE);
2277}
2278
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002279void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2280{
2281 dprintf(INFO, "Enabling charger screen check\n");
2282 device.charger_screen_enabled = 1;
2283 write_device_info(&device);
2284 fastboot_okay("");
2285}
2286
2287void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2288{
2289 dprintf(INFO, "Disabling charger screen check\n");
2290 device.charger_screen_enabled = 0;
2291 write_device_info(&device);
2292 fastboot_okay("");
2293}
2294
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302295void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2296{
2297 dprintf(INFO, "Selecting display panel %s\n", arg);
2298 if (arg)
2299 strlcpy(device.display_panel, arg,
2300 sizeof(device.display_panel));
2301 write_device_info(&device);
2302 fastboot_okay("");
2303}
2304
Shashank Mittal162244e2011-08-08 19:01:25 -07002305void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2306{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002307 /* TODO: Wipe user data */
2308 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002309 {
2310 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002311 device.is_verified = 0;
2312 write_device_info(&device);
2313 }
2314 fastboot_okay("");
2315}
2316
2317void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2318{
2319 /* TODO: Wipe user data */
2320 if(device.is_unlocked || device.is_verified)
2321 {
2322 device.is_unlocked = 0;
2323 device.is_verified = 0;
2324 write_device_info(&device);
2325 }
2326 fastboot_okay("");
2327}
2328
2329void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2330{
2331 /* TODO: Wipe user data */
2332 if(device.is_unlocked || !device.is_verified)
2333 {
2334 device.is_unlocked = 0;
2335 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002336 write_device_info(&device);
2337 }
2338 fastboot_okay("");
2339}
2340
Shashank Mittala0032282011-08-26 14:50:11 -07002341void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2342{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302343 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002344 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002345 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002346 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2347 fastboot_info(response);
2348 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002349 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302350 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2351 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002352 fastboot_okay("");
2353}
2354
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002355void cmd_preflash(const char *arg, void *data, unsigned sz)
2356{
2357 fastboot_okay("");
2358}
2359
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302360static struct fbimage logo_header = {0};
2361struct fbimage* splash_screen_flash();
2362
2363int splash_screen_check_header(struct fbimage *logo)
2364{
2365 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2366 return -1;
2367 if (logo->header.width == 0 || logo->header.height == 0)
2368 return -1;
2369 return 0;
2370}
2371
2372struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002373{
2374 struct ptentry *ptn;
2375 struct ptable *ptable;
2376 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302377 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002378
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302379
2380 ptable = flash_get_ptable();
2381 if (ptable == NULL) {
2382 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2383 return NULL;
2384 }
2385 ptn = ptable_find(ptable, "splash");
2386 if (ptn == NULL) {
2387 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2388 return NULL;
2389 }
2390
2391 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2392 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2393 return NULL;
2394 }
2395
2396 if (splash_screen_check_header(logo)) {
2397 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2398 return NULL;
2399 }
2400
2401 fb_display = fbcon_display();
2402 if (fb_display) {
2403 uint8_t *base = (uint8_t *) fb_display->base;
2404 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
2405 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002406 }
2407
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302408 if (flash_read(ptn + sizeof(logo->header), 0,
2409 base,
2410 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2411 fbcon_clear();
2412 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2413 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002414 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302415 logo->image = base;
2416 }
2417
2418 return logo;
2419}
2420
2421struct fbimage* splash_screen_mmc()
2422{
2423 int index = INVALID_PTN;
2424 unsigned long long ptn = 0;
2425 struct fbcon_config *fb_display = NULL;
2426 struct fbimage *logo = &logo_header;
2427
2428 index = partition_get_index("splash");
2429 if (index == 0) {
2430 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2431 return NULL;
2432 }
2433
2434 ptn = partition_get_offset(index);
2435 if (ptn == 0) {
2436 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2437 return NULL;
2438 }
2439
2440 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2441 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2442 return NULL;
2443 }
2444
2445 if (splash_screen_check_header(logo)) {
2446 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2447 return NULL;
2448 }
2449
2450 fb_display = fbcon_display();
2451 if (fb_display) {
2452 uint8_t *base = (uint8_t *) fb_display->base;
2453 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2454 base += LOGO_IMG_OFFSET;
2455
2456 if (mmc_read(ptn + sizeof(logo->header),
2457 base,
2458 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2459 fbcon_clear();
2460 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2461 return NULL;
2462 }
2463
2464 logo->image = base;
2465 }
2466
2467 return logo;
2468}
2469
2470
2471struct fbimage* fetch_image_from_partition()
2472{
2473 if (target_is_emmc_boot()) {
2474 return splash_screen_mmc();
2475 } else {
2476 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002477 }
2478}
2479
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002480/* Get the size from partiton name */
2481static void get_partition_size(const char *arg, char *response)
2482{
2483 uint64_t ptn = 0;
2484 uint64_t size;
2485 int index = INVALID_PTN;
2486
2487 index = partition_get_index(arg);
2488
2489 if (index == INVALID_PTN)
2490 {
2491 dprintf(CRITICAL, "Invalid partition index\n");
2492 return;
2493 }
2494
2495 ptn = partition_get_offset(index);
2496
2497 if(!ptn)
2498 {
2499 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2500 return;
2501 }
2502
2503 size = partition_get_size(index);
2504
2505 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2506 return;
2507}
2508
2509/*
2510 * Publish the partition type & size info
2511 * fastboot getvar will publish the required information.
2512 * fastboot getvar partition_size:<partition_name>: partition size in hex
2513 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2514 */
2515static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2516{
2517 uint8_t i;
2518
2519 for (i = 0; i < num_parts; i++) {
2520 get_partition_size(info[i].part_name, info[i].size_response);
2521
2522 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2523 {
2524 dprintf(CRITICAL, "partition size name truncated\n");
2525 return;
2526 }
2527 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2528 {
2529 dprintf(CRITICAL, "partition type name truncated\n");
2530 return;
2531 }
2532
2533 /* publish partition size & type info */
2534 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2535 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2536 }
2537}
2538
Amol Jadi5edf3552013-07-23 14:15:34 -07002539/* register commands and variables for fastboot */
2540void aboot_fastboot_register_commands(void)
2541{
2542 if (target_is_emmc_boot())
2543 {
2544 fastboot_register("flash:", cmd_flash_mmc);
2545 fastboot_register("erase:", cmd_erase_mmc);
2546 }
2547 else
2548 {
2549 fastboot_register("flash:", cmd_flash);
2550 fastboot_register("erase:", cmd_erase);
2551 }
2552
2553 fastboot_register("boot", cmd_boot);
2554 fastboot_register("continue", cmd_continue);
2555 fastboot_register("reboot", cmd_reboot);
2556 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2557 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002558 fastboot_register("oem lock", cmd_oem_lock);
2559 fastboot_register("oem verified", cmd_oem_verified);
Amol Jadi5edf3552013-07-23 14:15:34 -07002560 fastboot_register("oem device-info", cmd_oem_devinfo);
2561 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002562 fastboot_register("oem enable-charger-screen",
2563 cmd_oem_enable_charger_screen);
2564 fastboot_register("oem disable-charger-screen",
2565 cmd_oem_disable_charger_screen);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302566 fastboot_register("oem select-display-panel",
2567 cmd_oem_select_display_panel);
Amol Jadi5edf3552013-07-23 14:15:34 -07002568 /* publish variables and their values */
2569 fastboot_publish("product", TARGET(BOARD));
2570 fastboot_publish("kernel", "lk");
2571 fastboot_publish("serialno", sn_buf);
2572
2573 /*
2574 * partition info is supported only for emmc partitions
2575 * Calling this for NAND prints some error messages which
2576 * is harmless but misleading. Avoid calling this for NAND
2577 * devices.
2578 */
2579 if (target_is_emmc_boot())
2580 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2581
2582 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002583 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2584 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002585 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002586 /* Is the charger screen check enabled */
2587 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2588 device.charger_screen_enabled);
2589 fastboot_publish("charger-screen-enabled",
2590 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302591 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2592 device.display_panel);
2593 fastboot_publish("display-panel",
2594 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002595}
2596
Brian Swetland9c4c0752009-01-25 16:23:50 -08002597void aboot_init(const struct app_descriptor *app)
2598{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002599 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002600 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002601
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002602 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002603 if (target_is_emmc_boot())
2604 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002605 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002606 page_mask = page_size - 1;
2607 }
2608 else
2609 {
2610 page_size = flash_page_size();
2611 page_mask = page_size - 1;
2612 }
2613
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002614 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2615
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002616 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002617
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002618 /* Display splash screen if enabled */
2619#if DISPLAY_SPLASH_SCREEN
2620 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302621 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002622 dprintf(SPEW, "Display Init: Done\n");
2623#endif
2624
2625
Greg Griscod6250552011-06-29 14:40:23 -07002626 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002627 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002628
Dhaval Patel223ec952013-07-18 14:49:44 -07002629 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2630
Matthew Qindefd5562014-07-11 18:02:40 +08002631 /*
2632 * Check power off reason if user force reset,
2633 * if yes phone will do normal boot.
2634 */
2635 if (is_user_force_reset())
2636 goto normal_boot;
2637
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002638 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002639 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002640 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002641 dprintf(ALWAYS,"dload mode key sequence detected\n");
2642 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002643 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002644 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002645 }
2646 else
2647 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302648 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002649 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002650 }
2651 boot_into_fastboot = true;
2652 }
2653 if (!boot_into_fastboot)
2654 {
2655 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2656 boot_into_recovery = 1;
2657 if (!boot_into_recovery &&
2658 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002659 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002660 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002661 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002662 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002663 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002664 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002665
Ajay Dudani77421292010-10-27 19:34:06 -07002666 reboot_mode = check_reboot_mode();
2667 if (reboot_mode == RECOVERY_MODE) {
2668 boot_into_recovery = 1;
2669 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002670 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002671 }
2672
Matthew Qindefd5562014-07-11 18:02:40 +08002673normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03002674 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002675 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002676 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002677 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002678 if(emmc_recovery_init())
2679 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2680 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002681 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002682 if((device.is_unlocked) || (device.is_tampered))
2683 {
2684 #ifdef TZ_TAMPER_FUSE
2685 set_tamper_fuse_cmd();
2686 #endif
2687 #if USE_PCOM_SECBOOT
2688 set_tamper_flag(device.is_tampered);
2689 #endif
2690 }
Shashank Mittala0032282011-08-26 14:50:11 -07002691 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002692 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002693 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002694 else
2695 {
2696 recovery_init();
2697 #if USE_PCOM_SECBOOT
2698 if((device.is_unlocked) || (device.is_tampered))
2699 set_tamper_flag(device.is_tampered);
2700 #endif
2701 boot_linux_from_flash();
2702 }
2703 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2704 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002705 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002706
Amol Jadi5edf3552013-07-23 14:15:34 -07002707 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002708
Amol Jadi5edf3552013-07-23 14:15:34 -07002709 /* register aboot specific fastboot commands */
2710 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002711
Amol Jadi5edf3552013-07-23 14:15:34 -07002712 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002713 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002714
2715 /* initialize and start fastboot */
2716 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002717}
2718
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002719uint32_t get_page_size()
2720{
2721 return page_size;
2722}
2723
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002724/*
2725 * Calculated and save hash (SHA256) for non-signed boot image.
2726 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002727 * @param image_addr - Boot image address
2728 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002729 *
2730 * @return int - 0 on success, negative value on failure.
2731 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002732static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002733{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002734 unsigned int digest[8];
2735#if IMAGE_VERIF_ALGO_SHA1
2736 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
2737#else
2738 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
2739#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002740
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002741 target_crypto_init_params();
2742 hash_find(image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002743
2744 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002745 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002746
2747 return 0;
2748}
2749
Brian Swetland9c4c0752009-01-25 16:23:50 -08002750APP_START(aboot)
2751 .init = aboot_init,
2752APP_END