blob: d011532a1656ff0dfe295441c500ecf4feb75554 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08005 * Copyright (c) 2009-2015, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080060#if USE_RPMB_FOR_DEVINFO
61#include <rpmb.h>
62#endif
Dima Zavin214cc642009-01-26 11:16:21 -080063
Neeti Desai17379b82012-06-04 18:42:53 -070064#if DEVICE_TREE
65#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070066#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070067#endif
68
Shashank Mittalcd98d472011-08-02 14:29:24 -070069#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080070#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080071#include "bootimg.h"
72#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070073#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080074#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070075#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070076#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070077#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070078#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020079#include "mdtp.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070080
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070081extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070082extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070083extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070084extern int get_target_boot_params(const char *cmdline, const char *part,
85 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070086
87void write_device_info_mmc(device_info *dev);
88void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -070089static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070090
Sridhar Parasurame94e8152014-10-24 14:06:03 -070091/* fastboot command function pointer */
92typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
93
94struct fastboot_cmd_desc {
95 char * name;
96 fastboot_cmd_fn cb;
97};
98
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070099#define EXPAND(NAME) #NAME
100#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700101
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800102#ifdef MEMBASE
103#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
104#else
David Ng183a7422009-12-07 14:55:21 -0800105#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800106#endif
107
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700108#ifndef MEMSIZE
109#define MEMSIZE 1024*1024
110#endif
111
112#define MAX_TAGS_SIZE 1024
113
lijuang72875802015-02-10 10:35:12 +0800114#define RECOVERY_HARD_RESET_MODE 0x01
115#define FASTBOOT_HARD_RESET_MODE 0x02
116#define RTC_HARD_RESET_MODE 0x03
117
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800118#define RECOVERY_MODE 0x77665502
119#define FASTBOOT_MODE 0x77665500
Matthew Qind886f3c2014-01-17 16:52:01 +0800120#define ALARM_BOOT 0x77665503
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800121
Kun Liang2f1601a2013-08-12 16:29:54 +0800122/* make 4096 as default size to ensure EFS,EXT4's erasing */
123#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800124#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800125
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700126#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700127#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800128
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800129#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
130
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700131#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
132
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800133#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700134static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700135#else
David Ng183a7422009-12-07 14:55:21 -0800136static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700137#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800138static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300139static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800140static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800141static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800142static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700143static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300144static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800145
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800146static const char *baseband_apq = " androidboot.baseband=apq";
147static const char *baseband_msm = " androidboot.baseband=msm";
148static const char *baseband_csfb = " androidboot.baseband=csfb";
149static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700150static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800151static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700152static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800153static const char *baseband_dsda = " androidboot.baseband=dsda";
154static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800155static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800156static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800157
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700158static unsigned page_size = 0;
159static unsigned page_mask = 0;
160static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
161static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700162static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800163static bool boot_reason_alarm;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700164
Shashank Mittalcd98d472011-08-02 14:29:24 -0700165/* Assuming unauthorized kernel image by default */
166static int auth_kernel_img = 0;
167
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530168static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}};
Shashank Mittal162244e2011-08-08 19:01:25 -0700169
Dima Zavin42168f22009-01-30 11:52:22 -0800170struct atag_ptbl_entry
171{
172 char name[16];
173 unsigned offset;
174 unsigned size;
175 unsigned flags;
176};
177
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700178/*
179 * Partition info, required to be published
180 * for fastboot
181 */
182struct getvar_partition_info {
183 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
184 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
185 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
186 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
187 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
188};
189
190/*
191 * Right now, we are publishing the info for only
192 * three partitions
193 */
194struct getvar_partition_info part_info[] =
195{
196 { "system" , "partition-size:", "partition-type:", "", "ext4" },
197 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
198 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
199};
200
201char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700202char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800203char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700204char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530205char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700206
Greg Griscod2471ef2011-07-14 13:00:42 -0700207extern int emmc_recovery_init(void);
208
Kinson Chik0b1c8162011-08-31 16:31:57 -0700209#if NO_KEYPAD_DRIVER
210extern int fastboot_trigger(void);
211#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700212
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800213static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700214{
215 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700216#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800217 if (is_arm64)
218 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
219 else
220 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700221 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
222 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700223#endif
224}
225
Dima Zavin42168f22009-01-30 11:52:22 -0800226static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
227{
228 struct atag_ptbl_entry atag_ptn;
229
230 memcpy(atag_ptn.name, ptn->name, 16);
231 atag_ptn.name[15] = '\0';
232 atag_ptn.offset = ptn->start;
233 atag_ptn.size = ptn->length;
234 atag_ptn.flags = ptn->flags;
235 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
236 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
237}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800238
Neeti Desaie245d492012-06-01 12:52:13 -0700239unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800240{
David Ng183a7422009-12-07 14:55:21 -0800241 int cmdline_len = 0;
242 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800243 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700244 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800245 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300246 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700247 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700248 char *boot_dev_buf = NULL;
Dima Zavin42168f22009-01-30 11:52:22 -0800249
Brian Swetland9c4c0752009-01-25 16:23:50 -0800250 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800251 cmdline_len = strlen(cmdline);
252 have_cmdline = 1;
253 }
254 if (target_is_emmc_boot()) {
255 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800256#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700257 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
258 ASSERT(boot_dev_buf);
259 platform_boot_dev_cmdline(boot_dev_buf);
260 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700261#endif
David Ng183a7422009-12-07 14:55:21 -0800262 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800263
264 cmdline_len += strlen(usb_sn_cmdline);
265 cmdline_len += strlen(sn_buf);
266
Pavel Nedev5614d222013-06-17 18:01:02 +0300267 if (boot_into_recovery && gpt_exists)
268 cmdline_len += strlen(secondary_gpt_enable);
269
Pavel Nedev328ac822013-04-05 15:25:11 +0300270 if (boot_into_ffbm) {
271 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700272 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800273 /* reduce kernel console messages to speed-up boot */
274 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800275 } else if (boot_reason_alarm) {
276 cmdline_len += strlen(alarmboot_cmdline);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700277 } else if (device.charger_screen_enabled &&
278 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700279 pause_at_bootup = 1;
280 cmdline_len += strlen(battchg_pause);
281 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800282
Shashank Mittalcd98d472011-08-02 14:29:24 -0700283 if(target_use_signed_kernel() && auth_kernel_img) {
284 cmdline_len += strlen(auth_kernel);
285 }
286
Joonwoo Park61112782013-10-02 19:50:39 -0700287 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
288 "system",
289 target_boot_params,
290 sizeof(target_boot_params)) == 0) {
291 have_target_boot_params = 1;
292 cmdline_len += strlen(target_boot_params);
293 }
294
Ajay Dudanid04110c2011-01-17 23:55:07 -0800295 /* Determine correct androidboot.baseband to use */
296 switch(target_baseband())
297 {
298 case BASEBAND_APQ:
299 cmdline_len += strlen(baseband_apq);
300 break;
301
302 case BASEBAND_MSM:
303 cmdline_len += strlen(baseband_msm);
304 break;
305
306 case BASEBAND_CSFB:
307 cmdline_len += strlen(baseband_csfb);
308 break;
309
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800310 case BASEBAND_SVLTE2A:
311 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800312 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700313
314 case BASEBAND_MDM:
315 cmdline_len += strlen(baseband_mdm);
316 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700317
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800318 case BASEBAND_MDM2:
319 cmdline_len += strlen(baseband_mdm2);
320 break;
321
Amol Jadi5c61a952012-05-04 17:05:35 -0700322 case BASEBAND_SGLTE:
323 cmdline_len += strlen(baseband_sglte);
324 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530325
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800326 case BASEBAND_SGLTE2:
327 cmdline_len += strlen(baseband_sglte2);
328 break;
329
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530330 case BASEBAND_DSDA:
331 cmdline_len += strlen(baseband_dsda);
332 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800333
334 case BASEBAND_DSDA2:
335 cmdline_len += strlen(baseband_dsda2);
336 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800337 }
338
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800339 if (cmdline) {
340 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
341 target_display_panel_node(device.display_panel,
342 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
343 strlen(display_panel_buf)) {
344 cmdline_len += strlen(display_panel_buf);
345 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700346 }
347
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800348 if (target_warm_boot()) {
349 warm_boot = true;
350 cmdline_len += strlen(warmboot_cmdline);
351 }
352
David Ng183a7422009-12-07 14:55:21 -0800353 if (cmdline_len > 0) {
354 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800355 unsigned char *dst;
356
357 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
358 ASSERT(cmdline_final != NULL);
359 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700360
Amol Jadi168b7712012-03-06 16:15:00 -0800361 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800362 if (have_cmdline) {
363 src = cmdline;
364 while ((*dst++ = *src++));
365 }
366 if (target_is_emmc_boot()) {
367 src = emmc_cmdline;
368 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700369 have_cmdline = 1;
370 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800371#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700372 src = boot_dev_buf;
373 if (have_cmdline) --dst;
374 while ((*dst++ = *src++));
375#endif
David Ngf773dde2010-07-26 19:55:08 -0700376 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800377
378 src = usb_sn_cmdline;
379 if (have_cmdline) --dst;
380 have_cmdline = 1;
381 while ((*dst++ = *src++));
382 src = sn_buf;
383 if (have_cmdline) --dst;
384 have_cmdline = 1;
385 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800386 if (warm_boot) {
387 if (have_cmdline) --dst;
388 src = warmboot_cmdline;
389 while ((*dst++ = *src++));
390 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800391
Pavel Nedev5614d222013-06-17 18:01:02 +0300392 if (boot_into_recovery && gpt_exists) {
393 src = secondary_gpt_enable;
394 if (have_cmdline) --dst;
395 while ((*dst++ = *src++));
396 }
397
Pavel Nedev328ac822013-04-05 15:25:11 +0300398 if (boot_into_ffbm) {
399 src = androidboot_mode;
400 if (have_cmdline) --dst;
401 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700402 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300403 if (have_cmdline) --dst;
404 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800405 src = loglevel;
406 if (have_cmdline) --dst;
407 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800408 } else if (boot_reason_alarm) {
409 src = alarmboot_cmdline;
410 if (have_cmdline) --dst;
411 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300412 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700413 src = battchg_pause;
414 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800415 while ((*dst++ = *src++));
416 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800417
Shashank Mittalcd98d472011-08-02 14:29:24 -0700418 if(target_use_signed_kernel() && auth_kernel_img) {
419 src = auth_kernel;
420 if (have_cmdline) --dst;
421 while ((*dst++ = *src++));
422 }
423
Ajay Dudanid04110c2011-01-17 23:55:07 -0800424 switch(target_baseband())
425 {
426 case BASEBAND_APQ:
427 src = baseband_apq;
428 if (have_cmdline) --dst;
429 while ((*dst++ = *src++));
430 break;
431
432 case BASEBAND_MSM:
433 src = baseband_msm;
434 if (have_cmdline) --dst;
435 while ((*dst++ = *src++));
436 break;
437
438 case BASEBAND_CSFB:
439 src = baseband_csfb;
440 if (have_cmdline) --dst;
441 while ((*dst++ = *src++));
442 break;
443
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800444 case BASEBAND_SVLTE2A:
445 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800446 if (have_cmdline) --dst;
447 while ((*dst++ = *src++));
448 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700449
450 case BASEBAND_MDM:
451 src = baseband_mdm;
452 if (have_cmdline) --dst;
453 while ((*dst++ = *src++));
454 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700455
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800456 case BASEBAND_MDM2:
457 src = baseband_mdm2;
458 if (have_cmdline) --dst;
459 while ((*dst++ = *src++));
460 break;
461
Amol Jadi5c61a952012-05-04 17:05:35 -0700462 case BASEBAND_SGLTE:
463 src = baseband_sglte;
464 if (have_cmdline) --dst;
465 while ((*dst++ = *src++));
466 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530467
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800468 case BASEBAND_SGLTE2:
469 src = baseband_sglte2;
470 if (have_cmdline) --dst;
471 while ((*dst++ = *src++));
472 break;
473
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530474 case BASEBAND_DSDA:
475 src = baseband_dsda;
476 if (have_cmdline) --dst;
477 while ((*dst++ = *src++));
478 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800479
480 case BASEBAND_DSDA2:
481 src = baseband_dsda2;
482 if (have_cmdline) --dst;
483 while ((*dst++ = *src++));
484 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800485 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700486
487 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700488 src = display_panel_buf;
489 if (have_cmdline) --dst;
490 while ((*dst++ = *src++));
491 }
Joonwoo Park61112782013-10-02 19:50:39 -0700492
493 if (have_target_boot_params) {
494 if (have_cmdline) --dst;
495 src = target_boot_params;
496 while ((*dst++ = *src++));
497 }
Neeti Desaie245d492012-06-01 12:52:13 -0700498 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700499
500
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700501 if (boot_dev_buf)
502 free(boot_dev_buf);
503
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800504 if (cmdline_final)
505 dprintf(INFO, "cmdline: %s\n", cmdline_final);
506 else
507 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700508 return cmdline_final;
509}
510
511unsigned *atag_core(unsigned *ptr)
512{
513 /* CORE */
514 *ptr++ = 2;
515 *ptr++ = 0x54410001;
516
517 return ptr;
518
519}
520
521unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
522 unsigned ramdisk_size)
523{
524 if (ramdisk_size) {
525 *ptr++ = 4;
526 *ptr++ = 0x54420005;
527 *ptr++ = (unsigned)ramdisk;
528 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800529 }
530
Neeti Desaie245d492012-06-01 12:52:13 -0700531 return ptr;
532}
533
534unsigned *atag_ptable(unsigned **ptr_addr)
535{
536 int i;
537 struct ptable *ptable;
538
539 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700540 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
541 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700542 *(*ptr_addr)++ = 0x4d534d70;
543 for (i = 0; i < ptable->count; ++i)
544 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
545 }
546
547 return (*ptr_addr);
548}
549
550unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
551{
552 int cmdline_length = 0;
553 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700554 char *dest;
555
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800556 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700557 n = (cmdline_length + 4) & (~3);
558
559 *ptr++ = (n / 4) + 2;
560 *ptr++ = 0x54410009;
561 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800562 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700563 ptr += (n / 4);
564
565 return ptr;
566}
567
568unsigned *atag_end(unsigned *ptr)
569{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800570 /* END */
571 *ptr++ = 0;
572 *ptr++ = 0;
573
Neeti Desaie245d492012-06-01 12:52:13 -0700574 return ptr;
575}
576
577void generate_atags(unsigned *ptr, const char *cmdline,
578 void *ramdisk, unsigned ramdisk_size)
579{
580
581 ptr = atag_core(ptr);
582 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
583 ptr = target_atag_mem(ptr);
584
585 /* Skip NAND partition ATAGS for eMMC boot */
586 if (!target_is_emmc_boot()){
587 ptr = atag_ptable(&ptr);
588 }
589
590 ptr = atag_cmdline(ptr, cmdline);
591 ptr = atag_end(ptr);
592}
593
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700594typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700595void boot_linux(void *kernel, unsigned *tags,
596 const char *cmdline, unsigned machtype,
597 void *ramdisk, unsigned ramdisk_size)
598{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800599 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800600#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700601 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800602#endif
603
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700604 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800605 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800606 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800607
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530608 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700609
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800610 final_cmdline = update_cmdline((const char*)cmdline);
611
Neeti Desai17379b82012-06-04 18:42:53 -0700612#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800613 dprintf(INFO, "Updating device tree: start\n");
614
Neeti Desai17379b82012-06-04 18:42:53 -0700615 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530616 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700617 if(ret)
618 {
619 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
620 ASSERT(0);
621 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800622 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700623#else
Neeti Desaie245d492012-06-01 12:52:13 -0700624 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800625 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700626#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700627
Maria Yu52254c02014-07-04 16:14:54 +0800628 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700629
630#if VERIFIED_BOOT
631 /* Write protect the device info */
Channagoud Kadabibc4f15f2015-02-25 12:09:51 -0800632 if (target_build_variant_user() && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700633 {
634 dprintf(INFO, "Failed to write protect dev info\n");
635 ASSERT(0);
636 }
637#endif
638
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700639 /* Perform target specific cleanup */
640 target_uninit();
641
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800642 /* Turn off splash screen if enabled */
643#if DISPLAY_SPLASH_SCREEN
644 target_display_shutdown();
645#endif
646
647
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800648 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530649 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800650
651 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700652
Amol Jadi4421e652011-06-16 15:00:48 -0700653 /* do any platform specific cleanup before kernel entry */
654 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700655
Brian Swetland9c4c0752009-01-25 16:23:50 -0800656 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700657
Amol Jadi504f9fe2012-08-16 13:56:48 -0700658#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800659 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700660#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700661 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800662
663 if (IS_ARM64(kptr))
664 /* Jump to a 64bit kernel */
665 scm_elexec_call((paddr_t)kernel, tags_phys);
666 else
667 /* Jump to a 32bit kernel */
668 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800669}
670
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700671/* Function to check if the memory address range falls within the aboot
672 * boundaries.
673 * start: Start of the memory region
674 * size: Size of the memory region
675 */
676int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
677{
678 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800679 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700680 return -1;
681
682 /* Check for memory overlap. */
683 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
684 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700685 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700686 return 0;
687 else
688 return -1;
689}
690
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800691#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800692
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800693BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800694#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800695BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800696#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800697
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700698static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
699{
700 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800701
702#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800703#if IMAGE_VERIF_ALGO_SHA1
704 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
705#else
706 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
707#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800708#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700709
710 /* Assume device is rooted at this time. */
711 device.is_tampered = 1;
712
713 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
714
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700715#if VERIFIED_BOOT
716 if(boot_into_recovery)
717 {
718 ret = boot_verify_image((unsigned char *)bootimg_addr,
719 bootimg_size, "recovery");
720 }
721 else
722 {
723 ret = boot_verify_image((unsigned char *)bootimg_addr,
724 bootimg_size, "boot");
725 }
726 boot_verify_print_state();
727#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700728 ret = image_verify((unsigned char *)bootimg_addr,
729 (unsigned char *)(bootimg_addr + bootimg_size),
730 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800731 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700732#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700733 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
734
735 if (ret)
736 {
737 /* Authorized kernel */
738 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700739 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700740 }
741
742#if USE_PCOM_SECBOOT
743 set_tamper_flag(device.is_tampered);
744#endif
745
746 if(device.is_tampered)
747 {
748 write_device_info_mmc(&device);
749 #ifdef TZ_TAMPER_FUSE
750 set_tamper_fuse_cmd();
751 #endif
752 #ifdef ASSERT_ON_TAMPER
753 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
754 ASSERT(0);
755 #endif
756 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700757
758#if VERIFIED_BOOT
759 if(boot_verify_get_state() == RED)
760 {
761 if(!boot_into_recovery)
762 {
763 dprintf(CRITICAL,
764 "Device verification failed. Rebooting into recovery.\n");
765 reboot_device(RECOVERY_MODE);
766 }
767 else
768 {
769 dprintf(CRITICAL,
770 "Recovery image verification failed. Asserting..\n");
771 ASSERT(0);
772 }
773 }
774#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700775}
776
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530777static bool check_format_bit()
778{
779 bool ret = false;
780 int index;
781 uint64_t offset;
782 struct boot_selection_info *in = NULL;
783 char *buf = NULL;
784
785 index = partition_get_index("bootselect");
786 if (index == INVALID_PTN)
787 {
788 dprintf(INFO, "Unable to locate /bootselect partition\n");
789 return ret;
790 }
791 offset = partition_get_offset(index);
792 if(!offset)
793 {
794 dprintf(INFO, "partition /bootselect doesn't exist\n");
795 return ret;
796 }
797 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
798 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530799 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530800 {
801 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
802 free(buf);
803 return ret;
804 }
805 in = (struct boot_selection_info *) buf;
806 if ((in->signature == BOOTSELECT_SIGNATURE) &&
807 (in->version == BOOTSELECT_VERSION)) {
808 if ((in->state_info & BOOTSELECT_FORMAT) &&
809 !(in->state_info & BOOTSELECT_FACTORY))
810 ret = true;
811 } else {
812 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
813 in->signature, in->version);
814 ASSERT(0);
815 }
816 free(buf);
817 return ret;
818}
819
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700820void boot_verifier_init()
821{
822
823 uint32_t boot_state;
824 /* Check if device unlock */
825 if(device.is_unlocked)
826 {
827 boot_verify_send_event(DEV_UNLOCK);
828 boot_verify_print_state();
829 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
830 return;
831 }
832 else
833 {
834 boot_verify_send_event(BOOT_INIT);
835 }
836
837 /* Initialize keystore */
838 boot_state = boot_verify_keystore_init();
839 if(boot_state == YELLOW)
840 {
841 boot_verify_print_state();
842 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
843 }
844}
845
Shashank Mittal23b8f422010-04-16 19:27:21 -0700846int boot_linux_from_mmc(void)
847{
848 struct boot_img_hdr *hdr = (void*) buf;
849 struct boot_img_hdr *uhdr;
850 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700851 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700852 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700853 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700854
Shashank Mittalcd98d472011-08-02 14:29:24 -0700855 unsigned char *image_addr = 0;
856 unsigned kernel_actual;
857 unsigned ramdisk_actual;
858 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700859 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700860
861#if DEVICE_TREE
862 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700863 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700864 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800865 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700866 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700867#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800868 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
869 struct kernel64_hdr *kptr = (void*) kbuf;
870
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530871 if (check_format_bit())
872 boot_into_recovery = 1;
873
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700874 if (!boot_into_recovery) {
875 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
876 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
877 if (rcode <= 0) {
878 boot_into_ffbm = false;
879 if (rcode < 0)
880 dprintf(CRITICAL,"failed to get ffbm cookie");
881 } else
882 boot_into_ffbm = true;
883 } else
884 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700885 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
886 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
887 dprintf(INFO, "Unified boot method!\n");
888 hdr = uhdr;
889 goto unified_boot;
890 }
Greg Griscod6250552011-06-29 14:40:23 -0700891 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700892 index = partition_get_index("boot");
893 ptn = partition_get_offset(index);
894 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700895 dprintf(CRITICAL, "ERROR: No boot partition found\n");
896 return -1;
897 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700898 }
899 else {
900 index = partition_get_index("recovery");
901 ptn = partition_get_offset(index);
902 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700903 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
904 return -1;
905 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700906 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -0800907 /* Set Lun for boot & recovery partitions */
908 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -0700909
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530910 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700911 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
912 return -1;
913 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700914
915 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700916 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700917 return -1;
918 }
919
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700920 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530921
922 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
923 dprintf(CRITICAL, "ERROR: Invalid page size\n");
924 return -1;
925 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700926 page_size = hdr->page_size;
927 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700928 }
929
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800930 /* Read the next page to get kernel Image header
931 * which lives in the second page for arm64 targets.
932 */
933
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530934 if (mmc_read(ptn + page_size, (uint32_t *) kbuf, page_size)) {
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800935 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
936 return -1;
937 }
938
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700939 /*
940 * Update the kernel/ramdisk/tags address if the boot image header
941 * has default values, these default values come from mkbootimg when
942 * the boot image is flashed using fastboot flash:raw
943 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800944 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700945
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700946 /* Get virtual addresses since the hdr saves physical addresses. */
947 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
948 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
949 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700950
951 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
952 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
953
954 /* Check if the addresses in the header are valid. */
955 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
956 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
957 {
958 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
959 return -1;
960 }
961
962#ifndef DEVICE_TREE
963 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
964 {
965 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
966 return -1;
967 }
968#endif
969
Shashank Mittalcd98d472011-08-02 14:29:24 -0700970 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300971 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
972 (int) target_use_signed_kernel(),
973 device.is_unlocked,
974 device.is_tampered);
975
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700976#if VERIFIED_BOOT
977 boot_verifier_init();
978#endif
979
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700980 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800981 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700982 offset = 0;
983
Shashank Mittalcd98d472011-08-02 14:29:24 -0700984 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700985
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800986#if DEVICE_TREE
987 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
988 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700989
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700990 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700991 {
992 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
993 return -1;
994 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800995#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530996 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700997
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700998#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700999
Amol Jadib6be5c12012-11-14 13:39:51 -08001000 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001001 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001002
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301003 if (check_aboot_addr_range_overlap((uint32_t)image_addr, imagesize_actual))
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -07001004 {
1005 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1006 return -1;
1007 }
1008
Shashank Mittalcd98d472011-08-02 14:29:24 -07001009 /* Read image without signature */
1010 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1011 {
1012 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1013 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -08001014 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001015
Amol Jadib6be5c12012-11-14 13:39:51 -08001016 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001017 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001018
Shashank Mittalcd98d472011-08-02 14:29:24 -07001019 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -07001020
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301021 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -07001022 {
1023 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1024 return -1;
1025 }
1026
Shashank Mittalcd98d472011-08-02 14:29:24 -07001027 /* Read signature */
1028 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1029 {
1030 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001031 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001032 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001033
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301034 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001035
Neeti Desai465491e2012-07-31 12:53:35 -07001036 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -07001037 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1038 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -07001039
Neeti Desai465491e2012-07-31 12:53:35 -07001040 #if DEVICE_TREE
1041 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001042 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -07001043 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001044
Deepa Dinamani19648b42013-09-05 17:05:55 -07001045 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001046 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1047 return -1;
1048 }
Deepa Dinamani19648b42013-09-05 17:05:55 -07001049
Neeti Desai465491e2012-07-31 12:53:35 -07001050 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -07001051 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001052 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
1053 return -1;
1054 }
1055
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001056 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001057 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001058 {
1059 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1060 return -1;
1061 }
1062
Joel Kingaa335dc2013-06-03 16:11:08 -07001063 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001064 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -08001065 /*
1066 * If appended dev tree is found, update the atags with
1067 * memory address to the DTB appended location on RAM.
1068 * Else update with the atags address in the kernel header
1069 */
1070 void *dtb;
1071 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001072 hdr->kernel_size,
1073 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001074 if (!dtb) {
1075 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1076 return -1;
1077 }
Neeti Desai465491e2012-07-31 12:53:35 -07001078 }
1079 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001080 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001081 else
1082 {
Amol Jadib6be5c12012-11-14 13:39:51 -08001083 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001084
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001085 image_addr = (unsigned char *)target_get_scratch_address();
1086#if DEVICE_TREE
1087 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1088 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1089
1090 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
1091 {
1092 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1093 return -1;
1094 }
1095#else
1096 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1097
1098#endif
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001099 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001100 {
1101 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1102 return -1;
1103 }
1104
Amol Jadib6be5c12012-11-14 13:39:51 -08001105 dprintf(INFO, "Loading boot image (%d): start\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001106 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001107 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001108
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001109 offset = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001110
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001111 /* Load the entire boot image */
1112 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual)) {
1113 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
Shashank Mittalcd98d472011-08-02 14:29:24 -07001114 return -1;
1115 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001116
1117 dprintf(INFO, "Loading boot image (%d): done\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001118 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001119 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -07001120
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001121 #ifdef TZ_SAVE_KERNEL_HASH
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001122 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001123 #endif /* TZ_SAVE_KERNEL_HASH */
1124
1125 /* Move kernel, ramdisk and device tree to correct address */
1126 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1127 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Neeti Desai465491e2012-07-31 12:53:35 -07001128
1129 #if DEVICE_TREE
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001130 if(hdr->dt_size) {
1131 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1132 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001133
Deepa Dinamani19648b42013-09-05 17:05:55 -07001134 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001135 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1136 return -1;
1137 }
1138
Joel Kingaa335dc2013-06-03 16:11:08 -07001139 /* Find index of device tree within device tree table */
1140 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001141 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1142 return -1;
1143 }
1144
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001145 /* Validate and Read device device tree in the tags_addr */
Joel Kingaa335dc2013-06-03 16:11:08 -07001146 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001147 {
1148 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1149 return -1;
1150 }
1151
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001152 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001153 } else {
Aparna Mallavarapu33127372014-06-03 22:15:54 +05301154 /* Validate the tags_addr */
1155 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1156 {
1157 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1158 return -1;
1159 }
Dima Zavin77e41f32013-03-06 16:10:43 -08001160 /*
1161 * If appended dev tree is found, update the atags with
1162 * memory address to the DTB appended location on RAM.
1163 * Else update with the atags address in the kernel header
1164 */
1165 void *dtb;
1166 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001167 kernel_actual,
1168 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001169 if (!dtb) {
1170 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1171 return -1;
1172 }
Neeti Desai465491e2012-07-31 12:53:35 -07001173 }
1174 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001175 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001176
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001177 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1178 target_load_ssd_keystore();
1179
Shashank Mittal23b8f422010-04-16 19:27:21 -07001180unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001181
Dima Zavin77e41f32013-03-06 16:10:43 -08001182 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001183 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001184 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1185
1186 return 0;
1187}
1188
Dima Zavin214cc642009-01-26 11:16:21 -08001189int boot_linux_from_flash(void)
1190{
1191 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001192 struct ptentry *ptn;
1193 struct ptable *ptable;
1194 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001195
Shashank Mittalcd98d472011-08-02 14:29:24 -07001196 unsigned char *image_addr = 0;
1197 unsigned kernel_actual;
1198 unsigned ramdisk_actual;
1199 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001200 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001201
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001202#if DEVICE_TREE
1203 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001204 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001205 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001206 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001207#endif
1208
David Ng183a7422009-12-07 14:55:21 -08001209 if (target_is_emmc_boot()) {
1210 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1211 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1212 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1213 return -1;
1214 }
1215 goto continue_boot;
1216 }
1217
Dima Zavin214cc642009-01-26 11:16:21 -08001218 ptable = flash_get_ptable();
1219 if (ptable == NULL) {
1220 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1221 return -1;
1222 }
1223
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001224 if(!boot_into_recovery)
1225 {
1226 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001227
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001228 if (ptn == NULL) {
1229 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1230 return -1;
1231 }
1232 }
1233 else
1234 {
1235 ptn = ptable_find(ptable, "recovery");
1236 if (ptn == NULL) {
1237 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1238 return -1;
1239 }
Dima Zavin214cc642009-01-26 11:16:21 -08001240 }
1241
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001242 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001243 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1244 return -1;
1245 }
Dima Zavin214cc642009-01-26 11:16:21 -08001246
1247 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001248 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001249 return -1;
1250 }
1251
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001252 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001253 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 -08001254 return -1;
1255 }
1256
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001257 /*
1258 * Update the kernel/ramdisk/tags address if the boot image header
1259 * has default values, these default values come from mkbootimg when
1260 * the boot image is flashed using fastboot flash:raw
1261 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001262 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001263
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001264 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001265 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1266 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1267 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1268
1269 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1270 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1271
1272 /* Check if the addresses in the header are valid. */
1273 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1274 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1275 {
1276 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1277 return -1;
1278 }
1279
1280#ifndef DEVICE_TREE
1281 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1282 {
1283 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1284 return -1;
1285 }
1286#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001287
Shashank Mittalcd98d472011-08-02 14:29:24 -07001288 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001289 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001290 {
1291 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001292 offset = 0;
1293
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001294#if DEVICE_TREE
1295 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1296 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001297
1298 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1299 {
1300 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1301 return -1;
1302 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001303#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001304 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001305#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001306
Amol Jadib6be5c12012-11-14 13:39:51 -08001307 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001308 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001309
Shashank Mittalcd98d472011-08-02 14:29:24 -07001310 /* Read image without signature */
1311 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1312 {
1313 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1314 return -1;
1315 }
Dima Zavin214cc642009-01-26 11:16:21 -08001316
Amol Jadib6be5c12012-11-14 13:39:51 -08001317 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001318 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001319
Shashank Mittalcd98d472011-08-02 14:29:24 -07001320 offset = imagesize_actual;
1321 /* Read signature */
1322 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1323 {
1324 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001325 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001326 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001327
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301328 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001329
1330 /* Move kernel and ramdisk to correct address */
1331 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1332 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001333#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001334 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001335 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001336 {
1337 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1338 return -1;
1339 }
1340
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001341 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1342#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001343
1344 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001345 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001346 {
1347 write_device_info_flash(&device);
1348 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301349#if USE_PCOM_SECBOOT
1350 set_tamper_flag(device.is_tampered);
1351#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001352 }
1353 else
1354 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001355 offset = page_size;
1356
Amol Jadib6be5c12012-11-14 13:39:51 -08001357 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1358 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1359 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1360
1361 dprintf(INFO, "Loading boot image (%d): start\n",
1362 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001363 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001364
1365 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001366 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1367 return -1;
1368 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001369 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001370
Amol Jadib6be5c12012-11-14 13:39:51 -08001371 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001372 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1373 return -1;
1374 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001375 offset += ramdisk_actual;
1376
1377 dprintf(INFO, "Loading boot image (%d): done\n",
1378 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001379 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001380
1381 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001382 offset += second_actual;
1383 /* Second image loading not implemented. */
1384 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001385 }
1386
1387#if DEVICE_TREE
1388 if(hdr->dt_size != 0) {
1389
1390 /* Read the device tree table into buffer */
1391 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1392 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1393 return -1;
1394 }
1395
1396 table = (struct dt_table*) dt_buf;
1397
Deepa Dinamani19648b42013-09-05 17:05:55 -07001398 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001399 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1400 return -1;
1401 }
1402
Deepa Dinamani19648b42013-09-05 17:05:55 -07001403 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1404 if (!table)
1405 return -1;
1406
1407 /* Read the entire device tree table into buffer */
1408 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1409 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1410 return -1;
1411 }
1412
1413
Joel Kingaa335dc2013-06-03 16:11:08 -07001414 /* Find index of device tree within device tree table */
1415 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001416 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1417 return -1;
1418 }
1419
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001420 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001421 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001422 {
1423 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1424 return -1;
1425 }
1426
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001427 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001428 if(flash_read(ptn, offset + dt_entry.offset,
1429 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001430 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1431 return -1;
1432 }
1433 }
1434#endif
1435
Shashank Mittalcd98d472011-08-02 14:29:24 -07001436 }
David Ng183a7422009-12-07 14:55:21 -08001437continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001438
Dima Zavin214cc642009-01-26 11:16:21 -08001439 /* TODO: create/pass atags to kernel */
1440
Ajay Dudanie28a6072011-07-01 13:59:46 -07001441 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001442 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001443 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1444
1445 return 0;
1446}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001447
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001448BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001449void write_device_info_mmc(device_info *dev)
1450{
Shashank Mittal162244e2011-08-08 19:01:25 -07001451 unsigned long long ptn = 0;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001452#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001453 unsigned long long size;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001454#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001455 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001456 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001457 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001458
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001459#if VERIFIED_BOOT
1460 index = partition_get_index("devinfo");
1461#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001462 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001463#endif
1464
Shashank Mittal162244e2011-08-08 19:01:25 -07001465 ptn = partition_get_offset(index);
1466 if(ptn == 0)
1467 {
1468 return;
1469 }
1470
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001471 lun = partition_get_lun(index);
1472 mmc_set_lun(lun);
1473
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001474#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001475 size = partition_get_size(index);
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001476#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001477
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001478 blocksize = mmc_get_device_blocksize();
1479
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001480#if VERIFIED_BOOT
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001481 if(mmc_write(ptn, blocksize, (void *)dev))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001482#else
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001483 if(mmc_write((ptn + size - blocksize), blocksize, (void *)dev))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001484#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001485 {
1486 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1487 return;
1488 }
1489}
1490
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001491void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001492{
Shashank Mittal162244e2011-08-08 19:01:25 -07001493 unsigned long long ptn = 0;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001494#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001495 unsigned long long size;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001496#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001497 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001498 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001499
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001500#if VERIFIED_BOOT
1501 index = partition_get_index("devinfo");
1502#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001503 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001504#endif
1505
Shashank Mittal162244e2011-08-08 19:01:25 -07001506 ptn = partition_get_offset(index);
1507 if(ptn == 0)
1508 {
1509 return;
1510 }
1511
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001512 mmc_set_lun(partition_get_lun(index));
1513
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001514#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001515 size = partition_get_size(index);
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001516#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001517
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001518 blocksize = mmc_get_device_blocksize();
1519
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001520#if VERIFIED_BOOT
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001521 if(mmc_read(ptn, (void *)info, blocksize))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001522#else
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001523 if(mmc_read((ptn + size - blocksize), (void *)info, blocksize))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001524#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001525 {
1526 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1527 return;
1528 }
1529
Shashank Mittal162244e2011-08-08 19:01:25 -07001530}
1531
1532void write_device_info_flash(device_info *dev)
1533{
1534 struct device_info *info = (void *) info_buf;
1535 struct ptentry *ptn;
1536 struct ptable *ptable;
1537
1538 ptable = flash_get_ptable();
1539 if (ptable == NULL)
1540 {
1541 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1542 return;
1543 }
1544
1545 ptn = ptable_find(ptable, "devinfo");
1546 if (ptn == NULL)
1547 {
1548 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1549 return;
1550 }
1551
1552 memcpy(info, dev, sizeof(device_info));
1553
1554 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1555 {
1556 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1557 return;
1558 }
1559}
1560
1561void read_device_info_flash(device_info *dev)
1562{
1563 struct device_info *info = (void*) info_buf;
1564 struct ptentry *ptn;
1565 struct ptable *ptable;
1566
1567 ptable = flash_get_ptable();
1568 if (ptable == NULL)
1569 {
1570 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1571 return;
1572 }
1573
1574 ptn = ptable_find(ptable, "devinfo");
1575 if (ptn == NULL)
1576 {
1577 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1578 return;
1579 }
1580
1581 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1582 {
1583 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1584 return;
1585 }
1586
1587 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1588 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001589 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1590 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001591 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001592 write_device_info_flash(info);
1593 }
1594 memcpy(dev, info, sizeof(device_info));
1595}
1596
1597void write_device_info(device_info *dev)
1598{
1599 if(target_is_emmc_boot())
1600 {
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001601 struct device_info *info = (void*) info_buf;
1602 memcpy(info, dev, sizeof(struct device_info));
1603
1604#if USE_RPMB_FOR_DEVINFO
1605 if (is_secure_boot_enable())
1606 write_device_info_rpmb((void*) info, mmc_get_device_blocksize());
1607#else
1608 write_device_info_mmc(info);
1609#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001610 }
1611 else
1612 {
1613 write_device_info_flash(dev);
1614 }
1615}
1616
1617void read_device_info(device_info *dev)
1618{
1619 if(target_is_emmc_boot())
1620 {
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001621 struct device_info *info = (void*) info_buf;
1622
1623#if USE_RPMB_FOR_DEVINFO
1624 if (is_secure_boot_enable())
1625 read_device_info_rpmb((void*) info, mmc_get_device_blocksize());
1626#else
1627 read_device_info_mmc(info);
1628#endif
1629
1630 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1631 {
1632 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1633 if (is_secure_boot_enable())
1634 info->is_unlocked = 0;
1635 else
1636 info->is_unlocked = 1;
1637 info->is_verified = 0;
1638 info->is_tampered = 0;
1639 info->charger_screen_enabled = 0;
1640
1641 write_device_info(info);
1642 }
1643 memcpy(dev, info, sizeof(device_info));
Shashank Mittal162244e2011-08-08 19:01:25 -07001644 }
1645 else
1646 {
1647 read_device_info_flash(dev);
1648 }
1649}
1650
1651void reset_device_info()
1652{
1653 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001654 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001655 write_device_info(&device);
1656}
1657
1658void set_device_root()
1659{
1660 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001661 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001662 write_device_info(&device);
1663}
1664
Amol Jadicb524072012-08-09 16:40:18 -07001665#if DEVICE_TREE
1666int copy_dtb(uint8_t *boot_image_start)
1667{
1668 uint32 dt_image_offset = 0;
1669 uint32_t n;
1670 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001671 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001672 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001673
1674 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1675
Amol Jadicb524072012-08-09 16:40:18 -07001676 if(hdr->dt_size != 0) {
1677
1678 /* add kernel offset */
1679 dt_image_offset += page_size;
1680 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1681 dt_image_offset += n;
1682
1683 /* add ramdisk offset */
1684 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1685 dt_image_offset += n;
1686
1687 /* add second offset */
1688 if(hdr->second_size != 0) {
1689 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1690 dt_image_offset += n;
1691 }
1692
1693 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001694 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001695
Deepa Dinamani19648b42013-09-05 17:05:55 -07001696 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001697 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1698 return -1;
1699 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001700 /* Find index of device tree within device tree table */
1701 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001702 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1703 return -1;
1704 }
1705
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001706 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001707 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001708 {
1709 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1710 return -1;
1711 }
1712
Amol Jadicb524072012-08-09 16:40:18 -07001713 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001714 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001715 boot_image_start + dt_image_offset + dt_entry.offset,
1716 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001717 } else
1718 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001719
1720 /* Everything looks fine. Return success. */
1721 return 0;
1722}
1723#endif
1724
Brian Swetland9c4c0752009-01-25 16:23:50 -08001725void cmd_boot(const char *arg, void *data, unsigned sz)
1726{
1727 unsigned kernel_actual;
1728 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001729 uint32_t image_actual;
1730 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001731 uint32_t sig_actual = SIGNATURE_SIZE;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001732 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001733 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001734 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001735 int ret = 0;
1736 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001737
Amit Blay6281ebc2015-01-11 14:44:08 +02001738#ifdef MDTP_SUPPORT
1739 /* Go through Firmware Lock verification before continue with boot process */
1740 mdtp_fwlock_verify_lock();
1741 fbcon_clear();
1742#endif /* MDTP_SUPPORT */
1743
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001744#if VERIFIED_BOOT
1745 if(!device.is_unlocked)
1746 {
1747 fastboot_fail("unlock device to use this command");
1748 return;
1749 }
1750#endif
1751
Brian Swetland9c4c0752009-01-25 16:23:50 -08001752 if (sz < sizeof(hdr)) {
1753 fastboot_fail("invalid bootimage header");
1754 return;
1755 }
1756
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001757 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001758
1759 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001760 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001761
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001762 if(target_is_emmc_boot() && hdr->page_size) {
1763 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001764 page_mask = page_size - 1;
1765 }
1766
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001767 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1768 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001769#if DEVICE_TREE
1770 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1771#endif
1772
1773 image_actual = ADD_OF(page_size, kernel_actual);
1774 image_actual = ADD_OF(image_actual, ramdisk_actual);
1775 image_actual = ADD_OF(image_actual, dt_actual);
1776
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001777 if (target_use_signed_kernel() && (!device.is_unlocked))
1778 image_actual = ADD_OF(image_actual, sig_actual);
1779
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001780 /* sz should have atleast raw boot image */
1781 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001782 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001783 return;
1784 }
1785
1786 /* Verify the boot image
1787 * device & page_size are initialized in aboot_init
1788 */
1789 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001790 /* Pass size excluding signature size, otherwise we would try to
1791 * access signature beyond its length
1792 */
1793 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001794
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001795 /*
1796 * Update the kernel/ramdisk/tags address if the boot image header
1797 * has default values, these default values come from mkbootimg when
1798 * the boot image is flashed using fastboot flash:raw
1799 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001800 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1801 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001802
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001803 /* Get virtual addresses since the hdr saves physical addresses. */
1804 hdr->kernel_addr = VA(hdr->kernel_addr);
1805 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1806 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001807
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001808 /* Check if the addresses in the header are valid. */
1809 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1810 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1811 {
1812 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001813 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001814 }
1815
Amol Jadicb524072012-08-09 16:40:18 -07001816#if DEVICE_TREE
1817 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001818 ret = copy_dtb(data);
1819
1820 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001821#else
1822 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1823 {
1824 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001825 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001826 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001827#endif
1828
1829 /* Load ramdisk & kernel */
1830 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1831 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1832
1833#if DEVICE_TREE
1834 /*
1835 * If dtb is not found look for appended DTB in the kernel.
1836 * If appended dev tree is found, update the atags with
1837 * memory address to the DTB appended location on RAM.
1838 * Else update with the atags address in the kernel header
1839 */
1840 if (!dtb_copied) {
1841 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001842 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1843 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001844 if (!dtb) {
1845 fastboot_fail("dtb not found");
1846 return;
1847 }
Amol Jadicb524072012-08-09 16:40:18 -07001848 }
1849#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001850
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001851#ifndef DEVICE_TREE
1852 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1853 {
1854 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001855 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001856 }
1857#endif
1858
Brian Swetland9c4c0752009-01-25 16:23:50 -08001859 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001860 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001861
Dima Zavin77e41f32013-03-06 16:10:43 -08001862 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001863 (const char*) hdr->cmdline, board_machtype(),
1864 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001865}
1866
Sridhar Parasurame94e8152014-10-24 14:06:03 -07001867void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08001868{
1869 struct ptentry *ptn;
1870 struct ptable *ptable;
1871
1872 ptable = flash_get_ptable();
1873 if (ptable == NULL) {
1874 fastboot_fail("partition table doesn't exist");
1875 return;
1876 }
1877
1878 ptn = ptable_find(ptable, arg);
1879 if (ptn == NULL) {
1880 fastboot_fail("unknown partition name");
1881 return;
1882 }
1883
1884 if (flash_erase(ptn)) {
1885 fastboot_fail("failed to erase partition");
1886 return;
1887 }
1888 fastboot_okay("");
1889}
1890
Bikas Gurungd48bd242010-09-04 19:54:32 -07001891
1892void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1893{
1894 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001895 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001896 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001897 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001898
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001899#if VERIFIED_BOOT
1900 if(!strcmp(arg, KEYSTORE_PTN_NAME))
1901 {
1902 if(!device.is_unlocked)
1903 {
1904 fastboot_fail("unlock device to erase keystore");
1905 return;
1906 }
1907 }
1908#endif
1909
Kinson Chikf1a43512011-07-14 11:28:39 -07001910 index = partition_get_index(arg);
1911 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001912 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001913
Kinson Chikf1a43512011-07-14 11:28:39 -07001914 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001915 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001916 return;
1917 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001918
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001919 lun = partition_get_lun(index);
1920 mmc_set_lun(lun);
1921
Sridhar Parasuramd7957122015-02-27 11:33:40 -08001922 if (platform_boot_dev_isemmc())
1923 {
1924 if (mmc_erase_card(ptn, size)) {
1925 fastboot_fail("failed to erase partition\n");
1926 return;
1927 }
1928 } else {
1929 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
1930 size = partition_get_size(index);
1931 if (size > DEFAULT_ERASE_SIZE)
1932 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08001933
Sridhar Parasuramd7957122015-02-27 11:33:40 -08001934 /* Simple inefficient version of erase. Just writing
1935 0 in first several blocks */
1936 if (mmc_write(ptn , size, (unsigned int *)out)) {
1937 fastboot_fail("failed to erase partition");
1938 return;
1939 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07001940 }
1941 fastboot_okay("");
1942}
1943
Sridhar Parasurame94e8152014-10-24 14:06:03 -07001944void cmd_erase(const char *arg, void *data, unsigned sz)
1945{
1946 if(target_is_emmc_boot())
1947 cmd_erase_mmc(arg, data, sz);
1948 else
1949 cmd_erase_nand(arg, data, sz);
1950}
Bikas Gurungd48bd242010-09-04 19:54:32 -07001951
Ajay Dudani5c761132011-04-07 20:19:04 -07001952void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001953{
1954 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001955 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001956 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001957 char *token = NULL;
1958 char *pname = NULL;
1959 uint8_t lun = 0;
1960 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001961
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301962 token = strtok((char *)arg, ":");
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001963 pname = token;
1964 token = strtok(NULL, ":");
1965 if(token)
1966 {
1967 lun = atoi(token);
1968 mmc_set_lun(lun);
1969 lun_set = true;
1970 }
1971
Mao Jinlong226f33a2014-07-04 17:24:10 +08001972 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07001973 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08001974 if (!strcmp(pname, "partition"))
1975 {
1976 dprintf(INFO, "Attempt to write partition image.\n");
1977 if (write_partition(sz, (unsigned char *) data)) {
1978 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07001979 return;
1980 }
1981 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08001982 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001983 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001984#if VERIFIED_BOOT
1985 if(!strcmp(pname, KEYSTORE_PTN_NAME))
1986 {
1987 if(!device.is_unlocked)
1988 {
1989 fastboot_fail("unlock device to flash keystore");
1990 return;
1991 }
1992 if(!boot_verify_validate_keystore((unsigned char *)data))
1993 {
1994 fastboot_fail("image is not a keystore file");
1995 return;
1996 }
1997 }
1998#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08001999 index = partition_get_index(pname);
2000 ptn = partition_get_offset(index);
2001 if(ptn == 0) {
2002 fastboot_fail("partition table doesn't exist");
2003 return;
2004 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002005
Mao Jinlong226f33a2014-07-04 17:24:10 +08002006 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2007 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2008 fastboot_fail("image is not a boot image");
2009 return;
2010 }
2011 }
2012
2013 if(!lun_set)
2014 {
2015 lun = partition_get_lun(index);
2016 mmc_set_lun(lun);
2017 }
2018
2019 size = partition_get_size(index);
2020 if (ROUND_TO_PAGE(sz,511) > size) {
2021 fastboot_fail("size too large");
2022 return;
2023 }
2024 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2025 fastboot_fail("flash write failure");
2026 return;
2027 }
Greg Grisco6e754772011-06-23 12:19:39 -07002028 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002029 }
2030 fastboot_okay("");
2031 return;
2032}
2033
Ajay Dudanide984792015-03-02 09:57:41 -08002034void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2035{
2036 int i, images;
2037 meta_header_t *meta_header;
2038 img_header_entry_t *img_header_entry;
2039
2040 meta_header = (meta_header_t*) data;
2041 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2042
2043 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2044
2045 for (i=0; i<images; i++) {
2046
2047 if((img_header_entry[i].ptn_name == NULL) ||
2048 (img_header_entry[i].start_offset == 0) ||
2049 (img_header_entry[i].size == 0))
2050 break;
2051
2052 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2053 (void *) data + img_header_entry[i].start_offset,
2054 img_header_entry[i].size);
2055 }
2056
2057 fastboot_okay("");
2058 return;
2059}
2060
Ajay Dudani5c761132011-04-07 20:19:04 -07002061void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2062{
2063 unsigned int chunk;
2064 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002065 uint32_t *fill_buf = NULL;
2066 uint32_t fill_val;
2067 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002068 sparse_header_t *sparse_header;
2069 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002070 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002071 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302072 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002073 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302074 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002075 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002076
Kinson Chikf1a43512011-07-14 11:28:39 -07002077 index = partition_get_index(arg);
2078 ptn = partition_get_offset(index);
2079 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002080 fastboot_fail("partition table doesn't exist");
2081 return;
2082 }
2083
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302084 size = partition_get_size(index);
2085 if (ROUND_TO_PAGE(sz,511) > size) {
2086 fastboot_fail("size too large");
2087 return;
2088 }
2089
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002090 lun = partition_get_lun(index);
2091 mmc_set_lun(lun);
2092
Ajay Dudani5c761132011-04-07 20:19:04 -07002093 /* Read and skip over sparse image header */
2094 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08002095 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
2096 fastboot_fail("size too large");
2097 return;
2098 }
2099
Ajay Dudani5c761132011-04-07 20:19:04 -07002100 data += sparse_header->file_hdr_sz;
2101 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
2102 {
2103 /* Skip the remaining bytes in a header that is longer than
2104 * we expected.
2105 */
2106 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
2107 }
2108
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002109 dprintf (SPEW, "=== Sparse Image Header ===\n");
2110 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2111 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2112 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2113 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2114 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2115 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2116 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2117 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002118
2119 /* Start processing chunks */
2120 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2121 {
2122 /* Read and skip over chunk header */
2123 chunk_header = (chunk_header_t *) data;
2124 data += sizeof(chunk_header_t);
2125
2126 dprintf (SPEW, "=== Chunk Header ===\n");
2127 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2128 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2129 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2130
2131 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
2132 {
2133 /* Skip the remaining bytes in a header that is longer than
2134 * we expected.
2135 */
2136 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
2137 }
2138
2139 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
2140 switch (chunk_header->chunk_type)
2141 {
2142 case CHUNK_TYPE_RAW:
2143 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2144 chunk_data_sz))
2145 {
2146 fastboot_fail("Bogus chunk size for chunk type Raw");
2147 return;
2148 }
2149
Ajay Dudaniab18f022011-05-12 14:39:22 -07002150 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2151 chunk_data_sz,
2152 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002153 {
2154 fastboot_fail("flash write failure");
2155 return;
2156 }
2157 total_blocks += chunk_header->chunk_sz;
2158 data += chunk_data_sz;
2159 break;
2160
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002161 case CHUNK_TYPE_FILL:
2162 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2163 sizeof(uint32_t)))
2164 {
2165 fastboot_fail("Bogus chunk size for chunk type FILL");
2166 return;
2167 }
2168
2169 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2170 if (!fill_buf)
2171 {
2172 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2173 return;
2174 }
2175
2176 fill_val = *(uint32_t *)data;
2177 data = (char *) data + sizeof(uint32_t);
2178 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
2179
2180 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2181 {
2182 fill_buf[i] = fill_val;
2183 }
2184
2185 for (i = 0; i < chunk_blk_cnt; i++)
2186 {
2187 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2188 sparse_header->blk_sz,
2189 fill_buf))
2190 {
2191 fastboot_fail("flash write failure");
2192 free(fill_buf);
2193 return;
2194 }
2195
2196 total_blocks++;
2197 }
2198
2199 free(fill_buf);
2200 break;
2201
Ajay Dudani5c761132011-04-07 20:19:04 -07002202 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002203 total_blocks += chunk_header->chunk_sz;
2204 break;
2205
Ajay Dudani5c761132011-04-07 20:19:04 -07002206 case CHUNK_TYPE_CRC:
2207 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2208 {
2209 fastboot_fail("Bogus chunk size for chunk type Dont Care");
2210 return;
2211 }
2212 total_blocks += chunk_header->chunk_sz;
2213 data += chunk_data_sz;
2214 break;
2215
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002216 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002217 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002218 fastboot_fail("Unknown chunk type");
2219 return;
2220 }
2221 }
2222
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002223 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2224 total_blocks, sparse_header->total_blks);
2225
2226 if(total_blocks != sparse_header->total_blks)
2227 {
2228 fastboot_fail("sparse image write failure");
2229 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002230
2231 fastboot_okay("");
2232 return;
2233}
2234
2235void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2236{
2237 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002238 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002239
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002240#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002241 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2242 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002243 int ret=0;
2244 uint32 major_version=0;
2245 uint32 minor_version=0;
2246
2247 ret = scm_svc_version(&major_version,&minor_version);
2248 if(!ret)
2249 {
2250 if(major_version >= 2)
2251 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002252 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002253 {
2254 ret = encrypt_scm((uint32 **) &data, &sz);
2255 if (ret != 0) {
2256 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2257 return;
2258 }
2259
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002260 /* Protect only for SSD */
2261 if (!strcmp(arg, "ssd")) {
2262 ret = scm_protect_keystore((uint32 *) data, sz);
2263 if (ret != 0) {
2264 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2265 return;
2266 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002267 }
2268 }
2269 else
2270 {
2271 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2272 if(ret != 0)
2273 {
2274 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2275 return;
2276 }
2277 }
2278 }
2279 else
2280 {
2281 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2282 magic_number[1] == DECRYPT_MAGIC_1)
2283 {
2284 ret = decrypt_scm((uint32 **) &data, &sz);
2285 if (ret != 0) {
2286 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2287 return;
2288 }
2289 }
2290 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2291 magic_number[1] == ENCRYPT_MAGIC_1)
2292 {
2293 ret = encrypt_scm((uint32 **) &data, &sz);
2294 if (ret != 0) {
2295 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2296 return;
2297 }
2298 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002299 }
2300 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002301 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002302 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002303 dprintf(CRITICAL,"INVALID SVC Version\n");
2304 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002305 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002306#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002307
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002308#if VERIFIED_BOOT
2309 if(!device.is_unlocked && !device.is_verified)
2310 {
2311 fastboot_fail("device is locked. Cannot flash images");
2312 return;
2313 }
2314 if(!device.is_unlocked && device.is_verified)
2315 {
2316 if(!boot_verify_flash_allowed(arg))
2317 {
2318 fastboot_fail("cannot flash this partition in verified state");
2319 return;
2320 }
2321 }
2322#endif
2323
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002324 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002325 meta_header = (meta_header_t *) data;
2326 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002327 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002328 else if (meta_header->magic == META_HEADER_MAGIC)
2329 cmd_flash_meta_img(arg, data, sz);
2330 else
2331 cmd_flash_mmc_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002332 return;
2333}
2334
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002335void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002336{
2337 struct ptentry *ptn;
2338 struct ptable *ptable;
2339 unsigned extra = 0;
2340
2341 ptable = flash_get_ptable();
2342 if (ptable == NULL) {
2343 fastboot_fail("partition table doesn't exist");
2344 return;
2345 }
2346
2347 ptn = ptable_find(ptable, arg);
2348 if (ptn == NULL) {
2349 fastboot_fail("unknown partition name");
2350 return;
2351 }
2352
2353 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2354 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2355 fastboot_fail("image is not a boot image");
2356 return;
2357 }
2358 }
2359
Amol Jadi5c61a952012-05-04 17:05:35 -07002360 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002361 || !strcmp(ptn->name, "userdata")
2362 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002363 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002364 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002365 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002366 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002367 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002368
2369 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002370 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2371 if (flash_ubi_img(ptn, data, sz)) {
2372 fastboot_fail("flash write failure");
2373 return;
2374 }
2375 } else {
2376 if (flash_write(ptn, extra, data, sz)) {
2377 fastboot_fail("flash write failure");
2378 return;
2379 }
Dima Zavin214cc642009-01-26 11:16:21 -08002380 }
2381 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2382 fastboot_okay("");
2383}
2384
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002385void cmd_flash(const char *arg, void *data, unsigned sz)
2386{
2387 if(target_is_emmc_boot())
2388 cmd_flash_mmc(arg, data, sz);
2389 else
2390 cmd_flash_nand(arg, data, sz);
2391}
2392
Dima Zavin214cc642009-01-26 11:16:21 -08002393void cmd_continue(const char *arg, void *data, unsigned sz)
2394{
2395 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002396 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002397
Amit Blay6281ebc2015-01-11 14:44:08 +02002398#ifdef MDTP_SUPPORT
2399 /* Go through Firmware Lock verification before continue with boot process */
2400 mdtp_fwlock_verify_lock();
2401 fbcon_clear();
2402#endif /* MDTP_SUPPORT */
2403
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002404 if (target_is_emmc_boot())
2405 {
2406 boot_linux_from_mmc();
2407 }
2408 else
2409 {
2410 boot_linux_from_flash();
2411 }
Dima Zavin214cc642009-01-26 11:16:21 -08002412}
2413
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002414void cmd_reboot(const char *arg, void *data, unsigned sz)
2415{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002416 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002417 fastboot_okay("");
2418 reboot_device(0);
2419}
2420
2421void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2422{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002423 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002424 fastboot_okay("");
2425 reboot_device(FASTBOOT_MODE);
2426}
2427
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002428void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2429{
2430 dprintf(INFO, "Enabling charger screen check\n");
2431 device.charger_screen_enabled = 1;
2432 write_device_info(&device);
2433 fastboot_okay("");
2434}
2435
2436void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2437{
2438 dprintf(INFO, "Disabling charger screen check\n");
2439 device.charger_screen_enabled = 0;
2440 write_device_info(&device);
2441 fastboot_okay("");
2442}
2443
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302444void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2445{
2446 dprintf(INFO, "Selecting display panel %s\n", arg);
2447 if (arg)
2448 strlcpy(device.display_panel, arg,
2449 sizeof(device.display_panel));
2450 write_device_info(&device);
2451 fastboot_okay("");
2452}
2453
Shashank Mittal162244e2011-08-08 19:01:25 -07002454void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2455{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002456 /* TODO: Wipe user data */
2457 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002458 {
2459 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002460 device.is_verified = 0;
2461 write_device_info(&device);
2462 }
2463 fastboot_okay("");
2464}
2465
2466void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2467{
2468 /* TODO: Wipe user data */
2469 if(device.is_unlocked || device.is_verified)
2470 {
2471 device.is_unlocked = 0;
2472 device.is_verified = 0;
2473 write_device_info(&device);
2474 }
2475 fastboot_okay("");
2476}
2477
2478void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2479{
2480 /* TODO: Wipe user data */
2481 if(device.is_unlocked || !device.is_verified)
2482 {
2483 device.is_unlocked = 0;
2484 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002485 write_device_info(&device);
2486 }
2487 fastboot_okay("");
2488}
2489
Shashank Mittala0032282011-08-26 14:50:11 -07002490void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2491{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302492 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002493 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002494 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002495 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2496 fastboot_info(response);
2497 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002498 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302499 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2500 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002501 fastboot_okay("");
2502}
2503
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002504void cmd_preflash(const char *arg, void *data, unsigned sz)
2505{
2506 fastboot_okay("");
2507}
2508
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302509struct fbimage* splash_screen_flash();
2510
2511int splash_screen_check_header(struct fbimage *logo)
2512{
2513 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2514 return -1;
2515 if (logo->header.width == 0 || logo->header.height == 0)
2516 return -1;
2517 return 0;
2518}
2519
2520struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002521{
2522 struct ptentry *ptn;
2523 struct ptable *ptable;
2524 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002525 struct fbimage *logo = NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002526
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302527
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002528 logo = (struct fbimage *) malloc(ROUNDUP(page_size, sizeof(struct fbimage)));
2529 ASSERT(logo);
2530
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302531 ptable = flash_get_ptable();
2532 if (ptable == NULL) {
2533 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002534 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302535 }
2536 ptn = ptable_find(ptable, "splash");
2537 if (ptn == NULL) {
2538 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002539 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302540 }
2541
2542 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2543 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002544 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302545 }
2546
2547 if (splash_screen_check_header(logo)) {
2548 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002549 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302550 }
2551
2552 fb_display = fbcon_display();
2553 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302554 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2555 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back to default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002556 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002557 }
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302558 uint8_t *base = (uint8_t *) fb_display->base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302559 if (flash_read(ptn + sizeof(logo->header), 0,
2560 base,
2561 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2562 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302563 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002564 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002565 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302566 logo->image = base;
2567 }
2568
2569 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002570
2571err:
2572 free(logo);
2573 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302574}
2575
2576struct fbimage* splash_screen_mmc()
2577{
2578 int index = INVALID_PTN;
2579 unsigned long long ptn = 0;
2580 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002581 struct fbimage *logo = NULL;
Channagoud Kadabida117392014-11-24 11:01:12 -08002582 uint32_t blocksize;
2583 uint32_t readsize;
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002584 uint32_t logosize;
Channagoud Kadabida117392014-11-24 11:01:12 -08002585 uint32_t ptn_size;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302586
2587 index = partition_get_index("splash");
2588 if (index == 0) {
2589 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2590 return NULL;
2591 }
2592
2593 ptn = partition_get_offset(index);
2594 if (ptn == 0) {
2595 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2596 return NULL;
2597 }
2598
Channagoud Kadabida117392014-11-24 11:01:12 -08002599 ptn_size = partition_get_size(index);
2600 blocksize = mmc_get_device_blocksize();
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002601 logosize = ROUNDUP(sizeof(logo->header), blocksize);
Channagoud Kadabida117392014-11-24 11:01:12 -08002602
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002603 logo = (struct fbimage *)memalign(CACHE_LINE, ROUNDUP(logosize, CACHE_LINE));
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002604 ASSERT(logo);
2605
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002606 if (mmc_read(ptn, (uint32_t *) logo, logosize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302607 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002608 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302609 }
2610
2611 if (splash_screen_check_header(logo)) {
2612 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002613 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302614 }
2615
2616 fb_display = fbcon_display();
2617 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302618 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2619 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002620 goto err;
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302621 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302622 uint8_t *base = (uint8_t *) fb_display->base;
Channagoud Kadabida117392014-11-24 11:01:12 -08002623 readsize = ROUNDUP((logo->header.width * logo->header.height * fb_display->bpp/8), blocksize);
2624
2625 if (readsize > ptn_size)
2626 {
2627 dprintf(CRITICAL, "@%d:Invalid logo header readsize:%u exceeds ptn_size:%u\n", __LINE__, readsize,ptn_size);
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002628 goto err;
Channagoud Kadabida117392014-11-24 11:01:12 -08002629 }
2630
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002631 if (mmc_read(ptn + logosize,(uint32_t *)base, readsize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302632 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302633 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002634 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302635 }
2636
2637 logo->image = base;
2638 }
2639
2640 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002641
2642err:
2643 free(logo);
2644 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302645}
2646
2647
2648struct fbimage* fetch_image_from_partition()
2649{
2650 if (target_is_emmc_boot()) {
2651 return splash_screen_mmc();
2652 } else {
2653 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002654 }
2655}
2656
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002657/* Get the size from partiton name */
2658static void get_partition_size(const char *arg, char *response)
2659{
2660 uint64_t ptn = 0;
2661 uint64_t size;
2662 int index = INVALID_PTN;
2663
2664 index = partition_get_index(arg);
2665
2666 if (index == INVALID_PTN)
2667 {
2668 dprintf(CRITICAL, "Invalid partition index\n");
2669 return;
2670 }
2671
2672 ptn = partition_get_offset(index);
2673
2674 if(!ptn)
2675 {
2676 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2677 return;
2678 }
2679
2680 size = partition_get_size(index);
2681
2682 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2683 return;
2684}
2685
2686/*
2687 * Publish the partition type & size info
2688 * fastboot getvar will publish the required information.
2689 * fastboot getvar partition_size:<partition_name>: partition size in hex
2690 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2691 */
2692static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2693{
2694 uint8_t i;
2695
2696 for (i = 0; i < num_parts; i++) {
2697 get_partition_size(info[i].part_name, info[i].size_response);
2698
2699 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2700 {
2701 dprintf(CRITICAL, "partition size name truncated\n");
2702 return;
2703 }
2704 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2705 {
2706 dprintf(CRITICAL, "partition type name truncated\n");
2707 return;
2708 }
2709
2710 /* publish partition size & type info */
2711 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2712 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2713 }
2714}
2715
Amol Jadi5edf3552013-07-23 14:15:34 -07002716/* register commands and variables for fastboot */
2717void aboot_fastboot_register_commands(void)
2718{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002719 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07002720
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002721 struct fastboot_cmd_desc cmd_list[] = {
2722 /* By default the enabled list is empty. */
2723 {"", NULL},
2724 /* move commands enclosed within the below ifndef to here
2725 * if they need to be enabled in user build.
2726 */
2727#ifndef DISABLE_FASTBOOT_CMDS
2728 /* Register the following commands only for non-user builds */
2729 {"flash:", cmd_flash},
2730 {"erase:", cmd_erase},
2731 {"boot", cmd_boot},
2732 {"continue", cmd_continue},
2733 {"reboot", cmd_reboot},
2734 {"reboot-bootloader", cmd_reboot_bootloader},
2735 {"oem unlock", cmd_oem_unlock},
2736 {"oem lock", cmd_oem_lock},
2737 {"oem verified", cmd_oem_verified},
2738 {"oem device-info", cmd_oem_devinfo},
2739 {"preflash", cmd_preflash},
2740 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
2741 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08002742 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002743#endif
2744 };
2745
2746 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
2747 for (i = 1; i < fastboot_cmds_count; i++)
2748 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
2749
Amol Jadi5edf3552013-07-23 14:15:34 -07002750 /* publish variables and their values */
2751 fastboot_publish("product", TARGET(BOARD));
2752 fastboot_publish("kernel", "lk");
2753 fastboot_publish("serialno", sn_buf);
2754
2755 /*
2756 * partition info is supported only for emmc partitions
2757 * Calling this for NAND prints some error messages which
2758 * is harmless but misleading. Avoid calling this for NAND
2759 * devices.
2760 */
2761 if (target_is_emmc_boot())
2762 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2763
2764 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002765 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2766 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002767 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002768 /* Is the charger screen check enabled */
2769 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2770 device.charger_screen_enabled);
2771 fastboot_publish("charger-screen-enabled",
2772 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302773 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2774 device.display_panel);
2775 fastboot_publish("display-panel",
2776 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002777}
2778
Brian Swetland9c4c0752009-01-25 16:23:50 -08002779void aboot_init(const struct app_descriptor *app)
2780{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002781 unsigned reboot_mode = 0;
lijuang72875802015-02-10 10:35:12 +08002782 unsigned hard_reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002783 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002784
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002785 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002786 if (target_is_emmc_boot())
2787 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002788 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002789 page_mask = page_size - 1;
2790 }
2791 else
2792 {
2793 page_size = flash_page_size();
2794 page_mask = page_size - 1;
2795 }
2796
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002797 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2798
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002799 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002800
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002801 /* Display splash screen if enabled */
2802#if DISPLAY_SPLASH_SCREEN
2803 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302804 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002805 dprintf(SPEW, "Display Init: Done\n");
2806#endif
2807
2808
Greg Griscod6250552011-06-29 14:40:23 -07002809 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002810 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002811
Dhaval Patel223ec952013-07-18 14:49:44 -07002812 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2813
Matthew Qindefd5562014-07-11 18:02:40 +08002814 /*
2815 * Check power off reason if user force reset,
2816 * if yes phone will do normal boot.
2817 */
2818 if (is_user_force_reset())
2819 goto normal_boot;
2820
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002821 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002822 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002823 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002824 dprintf(ALWAYS,"dload mode key sequence detected\n");
2825 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002826 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002827 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002828 }
2829 else
2830 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302831 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002832 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002833 }
2834 boot_into_fastboot = true;
2835 }
2836 if (!boot_into_fastboot)
2837 {
2838 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2839 boot_into_recovery = 1;
2840 if (!boot_into_recovery &&
2841 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002842 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002843 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002844 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002845 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002846 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002847 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002848
Ajay Dudani77421292010-10-27 19:34:06 -07002849 reboot_mode = check_reboot_mode();
lijuang72875802015-02-10 10:35:12 +08002850 hard_reboot_mode = check_hard_reboot_mode();
2851 if (reboot_mode == RECOVERY_MODE ||
2852 hard_reboot_mode == RECOVERY_HARD_RESET_MODE) {
Ajay Dudani77421292010-10-27 19:34:06 -07002853 boot_into_recovery = 1;
lijuang72875802015-02-10 10:35:12 +08002854 } else if(reboot_mode == FASTBOOT_MODE ||
2855 hard_reboot_mode == FASTBOOT_HARD_RESET_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002856 boot_into_fastboot = true;
lijuang72875802015-02-10 10:35:12 +08002857 } else if(reboot_mode == ALARM_BOOT ||
2858 hard_reboot_mode == RTC_HARD_RESET_MODE) {
Matthew Qind886f3c2014-01-17 16:52:01 +08002859 boot_reason_alarm = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002860 }
2861
Matthew Qindefd5562014-07-11 18:02:40 +08002862normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03002863 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002864 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002865 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002866 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002867 if(emmc_recovery_init())
2868 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2869 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002870 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002871 if((device.is_unlocked) || (device.is_tampered))
2872 {
2873 #ifdef TZ_TAMPER_FUSE
2874 set_tamper_fuse_cmd();
2875 #endif
2876 #if USE_PCOM_SECBOOT
2877 set_tamper_flag(device.is_tampered);
2878 #endif
2879 }
Shashank Mittala0032282011-08-26 14:50:11 -07002880 }
Amit Blay6281ebc2015-01-11 14:44:08 +02002881
2882#ifdef MDTP_SUPPORT
2883 /* Go through Firmware Lock verification before continue with boot process */
2884 mdtp_fwlock_verify_lock();
2885 fbcon_clear();
2886#endif /* MDTP_SUPPORT */
2887
Pavel Nedev5d91d412013-04-29 11:34:24 +03002888 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002889 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002890 else
2891 {
2892 recovery_init();
2893 #if USE_PCOM_SECBOOT
2894 if((device.is_unlocked) || (device.is_tampered))
2895 set_tamper_flag(device.is_tampered);
2896 #endif
2897 boot_linux_from_flash();
2898 }
2899 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2900 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002901 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002902
Amol Jadi5edf3552013-07-23 14:15:34 -07002903 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002904
Amol Jadi5edf3552013-07-23 14:15:34 -07002905 /* register aboot specific fastboot commands */
2906 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002907
Amol Jadi5edf3552013-07-23 14:15:34 -07002908 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002909 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002910
2911 /* initialize and start fastboot */
2912 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002913}
2914
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002915uint32_t get_page_size()
2916{
2917 return page_size;
2918}
2919
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002920/*
2921 * Calculated and save hash (SHA256) for non-signed boot image.
2922 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002923 * @param image_addr - Boot image address
2924 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002925 *
2926 * @return int - 0 on success, negative value on failure.
2927 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002928static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002929{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002930 unsigned int digest[8];
2931#if IMAGE_VERIF_ALGO_SHA1
2932 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
2933#else
2934 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
2935#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002936
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002937 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002938 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002939
2940 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002941 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002942
2943 return 0;
2944}
2945
Brian Swetland9c4c0752009-01-25 16:23:50 -08002946APP_START(aboot)
2947 .init = aboot_init,
2948APP_END