blob: 07f87e52ec93f9d2f9807256a80a3db52a60b60e [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08005 * Copyright (c) 2009-2015, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080060#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053061#include <platform/timer.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080062#if USE_RPMB_FOR_DEVINFO
63#include <rpmb.h>
64#endif
Dima Zavin214cc642009-01-26 11:16:21 -080065
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070066#if ENABLE_WBC
67#include <pm_app_smbchg.h>
68#endif
69
Neeti Desai17379b82012-06-04 18:42:53 -070070#if DEVICE_TREE
71#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070072#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070073#endif
74
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053075#if WDOG_SUPPORT
76#include <wdog.h>
77#endif
78
Channagoud Kadabib6e476b2015-08-05 16:23:42 -070079#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070080#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080081#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080082#include "bootimg.h"
83#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070084#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080085#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070086#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070087#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070088#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070089#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020090#include "mdtp.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070091
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070092extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070093extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070094extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070095extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +053096 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070097
Sridhar Parasuram7e16d172015-07-05 11:35:23 -070098void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070099void write_device_info_mmc(device_info *dev);
100void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700101static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabi35227322015-05-29 11:14:17 -0700102static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700103
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700104/* fastboot command function pointer */
105typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
106
107struct fastboot_cmd_desc {
108 char * name;
109 fastboot_cmd_fn cb;
110};
111
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700112#define EXPAND(NAME) #NAME
113#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700114
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800115#ifdef MEMBASE
116#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
117#else
David Ng183a7422009-12-07 14:55:21 -0800118#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800119#endif
120
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700121#ifndef MEMSIZE
122#define MEMSIZE 1024*1024
123#endif
124
125#define MAX_TAGS_SIZE 1024
126
Kun Liang2f1601a2013-08-12 16:29:54 +0800127/* make 4096 as default size to ensure EFS,EXT4's erasing */
128#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800129#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800130
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700131#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700132#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800133
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800134#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
135
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700136#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
137
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800138#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700139static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700140#else
David Ng183a7422009-12-07 14:55:21 -0800141static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700142#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800143static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300144static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800145static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800146static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800147static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700148static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300149static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200150static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800151
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800152static const char *baseband_apq = " androidboot.baseband=apq";
153static const char *baseband_msm = " androidboot.baseband=msm";
154static const char *baseband_csfb = " androidboot.baseband=csfb";
155static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700156static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800157static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700158static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800159static const char *baseband_dsda = " androidboot.baseband=dsda";
160static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800161static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800162static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800163
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700164static unsigned page_size = 0;
165static unsigned page_mask = 0;
166static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
167static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530168static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800169static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700170static bool devinfo_present = true;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700171
Shashank Mittalcd98d472011-08-02 14:29:24 -0700172/* Assuming unauthorized kernel image by default */
173static int auth_kernel_img = 0;
174
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -0800175static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
vijay kumarc65876c2015-04-24 13:29:16 +0530176static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700177
vijay kumarca2e6812015-07-08 20:28:25 +0530178static char frp_ptns[2][8] = {"config","frp"};
179
Dima Zavin42168f22009-01-30 11:52:22 -0800180struct atag_ptbl_entry
181{
182 char name[16];
183 unsigned offset;
184 unsigned size;
185 unsigned flags;
186};
187
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700188/*
189 * Partition info, required to be published
190 * for fastboot
191 */
192struct getvar_partition_info {
193 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
194 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
195 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
196 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
197 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
198};
199
200/*
201 * Right now, we are publishing the info for only
202 * three partitions
203 */
204struct getvar_partition_info part_info[] =
205{
206 { "system" , "partition-size:", "partition-type:", "", "ext4" },
207 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
208 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
209};
210
211char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700212char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800213char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700214char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530215char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700216
Greg Griscod2471ef2011-07-14 13:00:42 -0700217extern int emmc_recovery_init(void);
218
Kinson Chik0b1c8162011-08-31 16:31:57 -0700219#if NO_KEYPAD_DRIVER
220extern int fastboot_trigger(void);
221#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700222
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800223static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700224{
225 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700226#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800227 if (is_arm64)
228 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
229 else
230 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700231 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
232 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700233#endif
234}
235
Dima Zavin42168f22009-01-30 11:52:22 -0800236static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
237{
238 struct atag_ptbl_entry atag_ptn;
239
240 memcpy(atag_ptn.name, ptn->name, 16);
241 atag_ptn.name[15] = '\0';
242 atag_ptn.offset = ptn->start;
243 atag_ptn.size = ptn->length;
244 atag_ptn.flags = ptn->flags;
245 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
246 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
247}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800248
Neeti Desaie245d492012-06-01 12:52:13 -0700249unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800250{
David Ng183a7422009-12-07 14:55:21 -0800251 int cmdline_len = 0;
252 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800253 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700254 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800255 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300256 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700257 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700258 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200259 bool is_mdtp_activated = 0;
260#ifdef MDTP_SUPPORT
261 mdtp_activated(&is_mdtp_activated);
262#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800263
Brian Swetland9c4c0752009-01-25 16:23:50 -0800264 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800265 cmdline_len = strlen(cmdline);
266 have_cmdline = 1;
267 }
268 if (target_is_emmc_boot()) {
269 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800270#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700271 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
272 ASSERT(boot_dev_buf);
273 platform_boot_dev_cmdline(boot_dev_buf);
274 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700275#endif
David Ng183a7422009-12-07 14:55:21 -0800276 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800277
278 cmdline_len += strlen(usb_sn_cmdline);
279 cmdline_len += strlen(sn_buf);
280
Pavel Nedev5614d222013-06-17 18:01:02 +0300281 if (boot_into_recovery && gpt_exists)
282 cmdline_len += strlen(secondary_gpt_enable);
283
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200284 if(is_mdtp_activated)
285 cmdline_len += strlen(mdtp_activated_flag);
286
Pavel Nedev328ac822013-04-05 15:25:11 +0300287 if (boot_into_ffbm) {
288 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700289 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800290 /* reduce kernel console messages to speed-up boot */
291 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800292 } else if (boot_reason_alarm) {
293 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800294 } else if ((target_build_variant_user() || device.charger_screen_enabled)
295 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700296 pause_at_bootup = 1;
297 cmdline_len += strlen(battchg_pause);
298 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800299
Shashank Mittalcd98d472011-08-02 14:29:24 -0700300 if(target_use_signed_kernel() && auth_kernel_img) {
301 cmdline_len += strlen(auth_kernel);
302 }
303
Joonwoo Park61112782013-10-02 19:50:39 -0700304 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
305 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530306 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700307 have_target_boot_params = 1;
308 cmdline_len += strlen(target_boot_params);
309 }
310
Ajay Dudanid04110c2011-01-17 23:55:07 -0800311 /* Determine correct androidboot.baseband to use */
312 switch(target_baseband())
313 {
314 case BASEBAND_APQ:
315 cmdline_len += strlen(baseband_apq);
316 break;
317
318 case BASEBAND_MSM:
319 cmdline_len += strlen(baseband_msm);
320 break;
321
322 case BASEBAND_CSFB:
323 cmdline_len += strlen(baseband_csfb);
324 break;
325
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800326 case BASEBAND_SVLTE2A:
327 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800328 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700329
330 case BASEBAND_MDM:
331 cmdline_len += strlen(baseband_mdm);
332 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700333
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800334 case BASEBAND_MDM2:
335 cmdline_len += strlen(baseband_mdm2);
336 break;
337
Amol Jadi5c61a952012-05-04 17:05:35 -0700338 case BASEBAND_SGLTE:
339 cmdline_len += strlen(baseband_sglte);
340 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530341
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800342 case BASEBAND_SGLTE2:
343 cmdline_len += strlen(baseband_sglte2);
344 break;
345
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530346 case BASEBAND_DSDA:
347 cmdline_len += strlen(baseband_dsda);
348 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800349
350 case BASEBAND_DSDA2:
351 cmdline_len += strlen(baseband_dsda2);
352 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800353 }
354
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800355 if (cmdline) {
356 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530357 target_display_panel_node(display_panel_buf,
358 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800359 strlen(display_panel_buf)) {
360 cmdline_len += strlen(display_panel_buf);
361 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700362 }
363
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800364 if (target_warm_boot()) {
365 warm_boot = true;
366 cmdline_len += strlen(warmboot_cmdline);
367 }
368
David Ng183a7422009-12-07 14:55:21 -0800369 if (cmdline_len > 0) {
370 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800371 unsigned char *dst;
372
373 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
374 ASSERT(cmdline_final != NULL);
375 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700376
Amol Jadi168b7712012-03-06 16:15:00 -0800377 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800378 if (have_cmdline) {
379 src = cmdline;
380 while ((*dst++ = *src++));
381 }
382 if (target_is_emmc_boot()) {
383 src = emmc_cmdline;
384 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700385 have_cmdline = 1;
386 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800387#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700388 src = boot_dev_buf;
389 if (have_cmdline) --dst;
390 while ((*dst++ = *src++));
391#endif
David Ngf773dde2010-07-26 19:55:08 -0700392 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800393
394 src = usb_sn_cmdline;
395 if (have_cmdline) --dst;
396 have_cmdline = 1;
397 while ((*dst++ = *src++));
398 src = sn_buf;
399 if (have_cmdline) --dst;
400 have_cmdline = 1;
401 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800402 if (warm_boot) {
403 if (have_cmdline) --dst;
404 src = warmboot_cmdline;
405 while ((*dst++ = *src++));
406 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800407
Pavel Nedev5614d222013-06-17 18:01:02 +0300408 if (boot_into_recovery && gpt_exists) {
409 src = secondary_gpt_enable;
410 if (have_cmdline) --dst;
411 while ((*dst++ = *src++));
412 }
413
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200414 if (is_mdtp_activated) {
415 src = mdtp_activated_flag;
416 if (have_cmdline) --dst;
417 while ((*dst++ = *src++));
418 }
419
Pavel Nedev328ac822013-04-05 15:25:11 +0300420 if (boot_into_ffbm) {
421 src = androidboot_mode;
422 if (have_cmdline) --dst;
423 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700424 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300425 if (have_cmdline) --dst;
426 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800427 src = loglevel;
428 if (have_cmdline) --dst;
429 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800430 } else if (boot_reason_alarm) {
431 src = alarmboot_cmdline;
432 if (have_cmdline) --dst;
433 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300434 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700435 src = battchg_pause;
436 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800437 while ((*dst++ = *src++));
438 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800439
Shashank Mittalcd98d472011-08-02 14:29:24 -0700440 if(target_use_signed_kernel() && auth_kernel_img) {
441 src = auth_kernel;
442 if (have_cmdline) --dst;
443 while ((*dst++ = *src++));
444 }
445
Ajay Dudanid04110c2011-01-17 23:55:07 -0800446 switch(target_baseband())
447 {
448 case BASEBAND_APQ:
449 src = baseband_apq;
450 if (have_cmdline) --dst;
451 while ((*dst++ = *src++));
452 break;
453
454 case BASEBAND_MSM:
455 src = baseband_msm;
456 if (have_cmdline) --dst;
457 while ((*dst++ = *src++));
458 break;
459
460 case BASEBAND_CSFB:
461 src = baseband_csfb;
462 if (have_cmdline) --dst;
463 while ((*dst++ = *src++));
464 break;
465
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800466 case BASEBAND_SVLTE2A:
467 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800468 if (have_cmdline) --dst;
469 while ((*dst++ = *src++));
470 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700471
472 case BASEBAND_MDM:
473 src = baseband_mdm;
474 if (have_cmdline) --dst;
475 while ((*dst++ = *src++));
476 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700477
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800478 case BASEBAND_MDM2:
479 src = baseband_mdm2;
480 if (have_cmdline) --dst;
481 while ((*dst++ = *src++));
482 break;
483
Amol Jadi5c61a952012-05-04 17:05:35 -0700484 case BASEBAND_SGLTE:
485 src = baseband_sglte;
486 if (have_cmdline) --dst;
487 while ((*dst++ = *src++));
488 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530489
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800490 case BASEBAND_SGLTE2:
491 src = baseband_sglte2;
492 if (have_cmdline) --dst;
493 while ((*dst++ = *src++));
494 break;
495
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530496 case BASEBAND_DSDA:
497 src = baseband_dsda;
498 if (have_cmdline) --dst;
499 while ((*dst++ = *src++));
500 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800501
502 case BASEBAND_DSDA2:
503 src = baseband_dsda2;
504 if (have_cmdline) --dst;
505 while ((*dst++ = *src++));
506 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800507 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700508
509 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700510 src = display_panel_buf;
511 if (have_cmdline) --dst;
512 while ((*dst++ = *src++));
513 }
Joonwoo Park61112782013-10-02 19:50:39 -0700514
515 if (have_target_boot_params) {
516 if (have_cmdline) --dst;
517 src = target_boot_params;
518 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530519 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700520 }
Neeti Desaie245d492012-06-01 12:52:13 -0700521 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700522
523
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700524 if (boot_dev_buf)
525 free(boot_dev_buf);
526
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800527 if (cmdline_final)
528 dprintf(INFO, "cmdline: %s\n", cmdline_final);
529 else
530 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700531 return cmdline_final;
532}
533
534unsigned *atag_core(unsigned *ptr)
535{
536 /* CORE */
537 *ptr++ = 2;
538 *ptr++ = 0x54410001;
539
540 return ptr;
541
542}
543
544unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
545 unsigned ramdisk_size)
546{
547 if (ramdisk_size) {
548 *ptr++ = 4;
549 *ptr++ = 0x54420005;
550 *ptr++ = (unsigned)ramdisk;
551 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800552 }
553
Neeti Desaie245d492012-06-01 12:52:13 -0700554 return ptr;
555}
556
557unsigned *atag_ptable(unsigned **ptr_addr)
558{
559 int i;
560 struct ptable *ptable;
561
562 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700563 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
564 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700565 *(*ptr_addr)++ = 0x4d534d70;
566 for (i = 0; i < ptable->count; ++i)
567 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
568 }
569
570 return (*ptr_addr);
571}
572
573unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
574{
575 int cmdline_length = 0;
576 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700577 char *dest;
578
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800579 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700580 n = (cmdline_length + 4) & (~3);
581
582 *ptr++ = (n / 4) + 2;
583 *ptr++ = 0x54410009;
584 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800585 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700586 ptr += (n / 4);
587
588 return ptr;
589}
590
591unsigned *atag_end(unsigned *ptr)
592{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800593 /* END */
594 *ptr++ = 0;
595 *ptr++ = 0;
596
Neeti Desaie245d492012-06-01 12:52:13 -0700597 return ptr;
598}
599
600void generate_atags(unsigned *ptr, const char *cmdline,
601 void *ramdisk, unsigned ramdisk_size)
602{
603
604 ptr = atag_core(ptr);
605 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
606 ptr = target_atag_mem(ptr);
607
608 /* Skip NAND partition ATAGS for eMMC boot */
609 if (!target_is_emmc_boot()){
610 ptr = atag_ptable(&ptr);
611 }
612
613 ptr = atag_cmdline(ptr, cmdline);
614 ptr = atag_end(ptr);
615}
616
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700617typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700618void boot_linux(void *kernel, unsigned *tags,
619 const char *cmdline, unsigned machtype,
620 void *ramdisk, unsigned ramdisk_size)
621{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800622 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800623#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700624 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800625#endif
626
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700627 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800628 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800629 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800630
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530631 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700632
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800633 final_cmdline = update_cmdline((const char*)cmdline);
634
Neeti Desai17379b82012-06-04 18:42:53 -0700635#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800636 dprintf(INFO, "Updating device tree: start\n");
637
Neeti Desai17379b82012-06-04 18:42:53 -0700638 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530639 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700640 if(ret)
641 {
642 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
643 ASSERT(0);
644 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800645 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700646#else
Neeti Desaie245d492012-06-01 12:52:13 -0700647 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800648 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700649#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700650
Maria Yu52254c02014-07-04 16:14:54 +0800651 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700652
653#if VERIFIED_BOOT
654 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700655 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700656 {
657 dprintf(INFO, "Failed to write protect dev info\n");
658 ASSERT(0);
659 }
660#endif
661
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700662 /* Perform target specific cleanup */
663 target_uninit();
664
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800665 /* Turn off splash screen if enabled */
666#if DISPLAY_SPLASH_SCREEN
667 target_display_shutdown();
668#endif
669
670
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800671 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530672 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800673
674 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700675
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +0530676 /* Initialise wdog to catch early kernel crashes */
677#if WDOG_SUPPORT
678 msm_wdog_init();
679#endif
Amol Jadi4421e652011-06-16 15:00:48 -0700680 /* do any platform specific cleanup before kernel entry */
681 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700682
Brian Swetland9c4c0752009-01-25 16:23:50 -0800683 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700684
Amol Jadi504f9fe2012-08-16 13:56:48 -0700685#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800686 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700687#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700688 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800689
690 if (IS_ARM64(kptr))
691 /* Jump to a 64bit kernel */
692 scm_elexec_call((paddr_t)kernel, tags_phys);
693 else
694 /* Jump to a 32bit kernel */
695 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800696}
697
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700698/* Function to check if the memory address range falls within the aboot
699 * boundaries.
700 * start: Start of the memory region
701 * size: Size of the memory region
702 */
703int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
704{
705 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800706 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700707 return -1;
708
709 /* Check for memory overlap. */
710 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
711 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700712 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700713 return 0;
714 else
715 return -1;
716}
717
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800718#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800719
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800720BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800721#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800722BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800723#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800724
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700725static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
726{
727 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800728
729#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800730#if IMAGE_VERIF_ALGO_SHA1
731 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
732#else
733 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
734#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800735#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700736
737 /* Assume device is rooted at this time. */
738 device.is_tampered = 1;
739
740 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
741
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700742#if VERIFIED_BOOT
743 if(boot_into_recovery)
744 {
745 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530746 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700747 }
748 else
749 {
750 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530751 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700752 }
753 boot_verify_print_state();
754#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700755 ret = image_verify((unsigned char *)bootimg_addr,
756 (unsigned char *)(bootimg_addr + bootimg_size),
757 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800758 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700759#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700760 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
761
762 if (ret)
763 {
764 /* Authorized kernel */
765 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700766 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700767 }
768
Amit Blay4aa292f2015-04-28 21:55:59 +0300769#ifdef MDTP_SUPPORT
770 {
771 /* Verify MDTP lock.
772 * For boot & recovery partitions, use aboot's verification result.
773 */
774 mdtp_ext_partition_verification_t ext_partition;
775 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
776 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
777 ext_partition.page_size = 0; /* Not needed since already validated */
778 ext_partition.image_addr = 0; /* Not needed since already validated */
779 ext_partition.image_size = 0; /* Not needed since already validated */
780 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
781 mdtp_fwlock_verify_lock(&ext_partition);
782 }
783#endif /* MDTP_SUPPORT */
784
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700785#if USE_PCOM_SECBOOT
786 set_tamper_flag(device.is_tampered);
787#endif
788
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700789#if VERIFIED_BOOT
790 if(boot_verify_get_state() == RED)
791 {
792 if(!boot_into_recovery)
793 {
794 dprintf(CRITICAL,
795 "Device verification failed. Rebooting into recovery.\n");
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +0530796 mdelay(1000);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700797 reboot_device(RECOVERY_MODE);
798 }
799 else
800 {
801 dprintf(CRITICAL,
802 "Recovery image verification failed. Asserting..\n");
803 ASSERT(0);
804 }
805 }
806#endif
Unnati Gandhi1be04752015-03-27 19:41:53 +0530807
808 if(device.is_tampered)
809 {
810 write_device_info_mmc(&device);
811 #ifdef TZ_TAMPER_FUSE
812 set_tamper_fuse_cmd();
813 #endif
814 #ifdef ASSERT_ON_TAMPER
815 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
816 ASSERT(0);
817 #endif
818 }
819
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700820}
821
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530822static bool check_format_bit()
823{
824 bool ret = false;
825 int index;
826 uint64_t offset;
827 struct boot_selection_info *in = NULL;
828 char *buf = NULL;
829
830 index = partition_get_index("bootselect");
831 if (index == INVALID_PTN)
832 {
833 dprintf(INFO, "Unable to locate /bootselect partition\n");
834 return ret;
835 }
836 offset = partition_get_offset(index);
837 if(!offset)
838 {
839 dprintf(INFO, "partition /bootselect doesn't exist\n");
840 return ret;
841 }
842 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
843 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530844 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530845 {
846 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
847 free(buf);
848 return ret;
849 }
850 in = (struct boot_selection_info *) buf;
851 if ((in->signature == BOOTSELECT_SIGNATURE) &&
852 (in->version == BOOTSELECT_VERSION)) {
853 if ((in->state_info & BOOTSELECT_FORMAT) &&
854 !(in->state_info & BOOTSELECT_FACTORY))
855 ret = true;
856 } else {
857 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
858 in->signature, in->version);
859 ASSERT(0);
860 }
861 free(buf);
862 return ret;
863}
864
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700865void boot_verifier_init()
866{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700867 uint32_t boot_state;
868 /* Check if device unlock */
869 if(device.is_unlocked)
870 {
871 boot_verify_send_event(DEV_UNLOCK);
872 boot_verify_print_state();
873 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
874 return;
875 }
876 else
877 {
878 boot_verify_send_event(BOOT_INIT);
879 }
880
881 /* Initialize keystore */
882 boot_state = boot_verify_keystore_init();
883 if(boot_state == YELLOW)
884 {
885 boot_verify_print_state();
886 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
887 }
888}
889
Shashank Mittal23b8f422010-04-16 19:27:21 -0700890int boot_linux_from_mmc(void)
891{
892 struct boot_img_hdr *hdr = (void*) buf;
893 struct boot_img_hdr *uhdr;
894 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700895 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700896 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700897 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700898
Shashank Mittalcd98d472011-08-02 14:29:24 -0700899 unsigned char *image_addr = 0;
900 unsigned kernel_actual;
901 unsigned ramdisk_actual;
902 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700903 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700904
Matthew Qin271927e2015-03-31 22:07:07 -0400905 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +0800906 unsigned int out_len = 0;
907 unsigned int out_avai_len = 0;
908 unsigned char *out_addr = NULL;
909 uint32_t dtb_offset = 0;
910 unsigned char *kernel_start_addr = NULL;
911 unsigned int kernel_size = 0;
912 int rc;
913
Neeti Desai465491e2012-07-31 12:53:35 -0700914#if DEVICE_TREE
915 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700916 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700917 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800918 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700919 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -0400920 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -0700921#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +0800922 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800923
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530924 if (check_format_bit())
925 boot_into_recovery = 1;
926
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700927 if (!boot_into_recovery) {
928 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
929 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
930 if (rcode <= 0) {
931 boot_into_ffbm = false;
932 if (rcode < 0)
933 dprintf(CRITICAL,"failed to get ffbm cookie");
934 } else
935 boot_into_ffbm = true;
936 } else
937 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700938 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
939 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
940 dprintf(INFO, "Unified boot method!\n");
941 hdr = uhdr;
942 goto unified_boot;
943 }
Greg Griscod6250552011-06-29 14:40:23 -0700944 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700945 index = partition_get_index("boot");
946 ptn = partition_get_offset(index);
947 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700948 dprintf(CRITICAL, "ERROR: No boot partition found\n");
949 return -1;
950 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700951 }
952 else {
953 index = partition_get_index("recovery");
954 ptn = partition_get_offset(index);
955 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700956 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
957 return -1;
958 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700959 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -0800960 /* Set Lun for boot & recovery partitions */
961 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -0700962
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530963 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700964 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
965 return -1;
966 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700967
968 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700969 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700970 return -1;
971 }
972
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700973 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530974
975 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
976 dprintf(CRITICAL, "ERROR: Invalid page size\n");
977 return -1;
978 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700979 page_size = hdr->page_size;
980 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700981 }
982
Matthew Qin49e51fa2015-02-09 10:40:45 +0800983 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
984 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800985
Matthew Qin49e51fa2015-02-09 10:40:45 +0800986 image_addr = (unsigned char *)target_get_scratch_address();
987
988#if DEVICE_TREE
989 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
990 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
991#else
992 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
993#endif
994
995#if VERIFIED_BOOT
996 boot_verifier_init();
997#endif
998
999 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
1000 {
1001 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1002 return -1;
1003 }
1004
Matthew Qinbb7923d2015-02-09 10:56:09 +08001005 /*
1006 * Update loading flow of bootimage to support compressed/uncompressed
1007 * bootimage on both 64bit and 32bit platform.
1008 * 1. Load bootimage from emmc partition onto DDR.
1009 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1010 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1011 * platform accordingly.
1012 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1013 */
1014
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001015 dprintf(INFO, "Loading (%s) image (%d): start\n",
1016 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001017 bs_set_timestamp(BS_KERNEL_LOAD_START);
1018
Matthew Qinbb7923d2015-02-09 10:56:09 +08001019 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001020 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1021 {
1022 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1023 return -1;
1024 }
1025
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001026 dprintf(INFO, "Loading (%s) image (%d): done\n",
1027 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1028
Matthew Qin49e51fa2015-02-09 10:40:45 +08001029 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1030
1031 /* Authenticate Kernel */
1032 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1033 (int) target_use_signed_kernel(),
1034 device.is_unlocked,
1035 device.is_tampered);
1036
1037 if(target_use_signed_kernel() && (!device.is_unlocked))
1038 {
1039 offset = imagesize_actual;
1040 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1041 {
1042 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1043 return -1;
1044 }
1045
1046 /* Read signature */
1047 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1048 {
1049 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1050 return -1;
1051 }
1052
1053 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
1054 } else {
1055 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1056 #ifdef TZ_SAVE_KERNEL_HASH
1057 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1058 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001059
1060#ifdef MDTP_SUPPORT
1061 {
1062 /* Verify MDTP lock.
1063 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1064 * since verification was skipped in aboot. The signature is not part of the loaded image.
1065 */
1066 mdtp_ext_partition_verification_t ext_partition;
1067 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1068 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1069 ext_partition.page_size = page_size;
1070 ext_partition.image_addr = (uint32)image_addr;
1071 ext_partition.image_size = imagesize_actual;
1072 ext_partition.sig_avail = FALSE;
1073 mdtp_fwlock_verify_lock(&ext_partition);
1074 }
1075#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001076 }
1077
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001078 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001079 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1080 * If not, continue booting.
1081 */
1082 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1083 {
1084 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1085 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001086 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001087 rc = decompress((unsigned char *)(image_addr + page_size),
1088 hdr->kernel_size, out_addr, out_avai_len,
1089 &dtb_offset, &out_len);
1090 if (rc)
1091 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001092 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001093 ASSERT(0);
1094 }
1095
Matthew Qin0b15b322015-05-19 05:20:54 -04001096 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001097 kptr = (struct kernel64_hdr *)out_addr;
1098 kernel_start_addr = out_addr;
1099 kernel_size = out_len;
1100 } else {
1101 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1102 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1103 kernel_size = hdr->kernel_size;
1104 }
1105
1106 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001107 * Update the kernel/ramdisk/tags address if the boot image header
1108 * has default values, these default values come from mkbootimg when
1109 * the boot image is flashed using fastboot flash:raw
1110 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001111 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001112
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001113 /* Get virtual addresses since the hdr saves physical addresses. */
1114 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1115 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1116 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001117
Matthew Qinbb7923d2015-02-09 10:56:09 +08001118 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001119 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001120 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001121 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1122 {
1123 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1124 return -1;
1125 }
1126
1127#ifndef DEVICE_TREE
1128 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1129 {
1130 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1131 return -1;
1132 }
1133#endif
1134
Matthew Qin49e51fa2015-02-09 10:40:45 +08001135 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001136 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001137 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001138
Matthew Qin49e51fa2015-02-09 10:40:45 +08001139 #if DEVICE_TREE
1140 if(hdr->dt_size) {
1141 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1142 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001143
Matthew Qin49e51fa2015-02-09 10:40:45 +08001144 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1145 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1146 return -1;
1147 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001148
Matthew Qin49e51fa2015-02-09 10:40:45 +08001149 /* Find index of device tree within device tree table */
1150 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1151 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1152 return -1;
1153 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001154
Matthew Qin271927e2015-03-31 22:07:07 -04001155 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1156 {
1157 unsigned int compressed_size = 0;
1158 out_addr += out_len;
1159 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001160 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001161 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1162 dt_entry.size, out_addr, out_avai_len,
1163 &compressed_size, &dtb_size);
1164 if (rc)
1165 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001166 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001167 ASSERT(0);
1168 }
1169
Matthew Qin0b15b322015-05-19 05:20:54 -04001170 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001171 best_match_dt_addr = out_addr;
1172 } else {
1173 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1174 dtb_size = dt_entry.size;
1175 }
1176
Matthew Qin49e51fa2015-02-09 10:40:45 +08001177 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001178 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001179 {
1180 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1181 return -1;
1182 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001183
Matthew Qin271927e2015-03-31 22:07:07 -04001184 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001185 } else {
1186 /* Validate the tags_addr */
1187 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001188 {
1189 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1190 return -1;
1191 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001192 /*
1193 * If appended dev tree is found, update the atags with
1194 * memory address to the DTB appended location on RAM.
1195 * Else update with the atags address in the kernel header
1196 */
1197 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001198 dtb = dev_tree_appended((void*)(image_addr + page_size),
1199 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001200 (void *)hdr->tags_addr);
1201 if (!dtb) {
1202 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001203 return -1;
1204 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001205 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001206 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001207
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001208 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1209 target_load_ssd_keystore();
1210
Shashank Mittal23b8f422010-04-16 19:27:21 -07001211unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001212
Dima Zavin77e41f32013-03-06 16:10:43 -08001213 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001214 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001215 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1216
1217 return 0;
1218}
1219
Dima Zavin214cc642009-01-26 11:16:21 -08001220int boot_linux_from_flash(void)
1221{
1222 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001223 struct ptentry *ptn;
1224 struct ptable *ptable;
1225 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001226
Shashank Mittalcd98d472011-08-02 14:29:24 -07001227 unsigned char *image_addr = 0;
1228 unsigned kernel_actual;
1229 unsigned ramdisk_actual;
1230 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001231 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001232
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001233#if DEVICE_TREE
1234 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001235 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001236 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001237 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001238#endif
1239
David Ng183a7422009-12-07 14:55:21 -08001240 if (target_is_emmc_boot()) {
1241 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1242 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1243 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1244 return -1;
1245 }
1246 goto continue_boot;
1247 }
1248
Dima Zavin214cc642009-01-26 11:16:21 -08001249 ptable = flash_get_ptable();
1250 if (ptable == NULL) {
1251 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1252 return -1;
1253 }
1254
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001255 if(!boot_into_recovery)
1256 {
1257 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001258
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001259 if (ptn == NULL) {
1260 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1261 return -1;
1262 }
1263 }
1264 else
1265 {
1266 ptn = ptable_find(ptable, "recovery");
1267 if (ptn == NULL) {
1268 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1269 return -1;
1270 }
Dima Zavin214cc642009-01-26 11:16:21 -08001271 }
1272
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001273 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001274 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1275 return -1;
1276 }
Dima Zavin214cc642009-01-26 11:16:21 -08001277
1278 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001279 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001280 return -1;
1281 }
1282
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001283 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001284 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 -08001285 return -1;
1286 }
1287
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001288 /*
1289 * Update the kernel/ramdisk/tags address if the boot image header
1290 * has default values, these default values come from mkbootimg when
1291 * the boot image is flashed using fastboot flash:raw
1292 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001293 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001294
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001295 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001296 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1297 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1298 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1299
1300 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1301 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1302
1303 /* Check if the addresses in the header are valid. */
1304 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1305 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1306 {
1307 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1308 return -1;
1309 }
1310
1311#ifndef DEVICE_TREE
1312 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1313 {
1314 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1315 return -1;
1316 }
1317#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001318
Shashank Mittalcd98d472011-08-02 14:29:24 -07001319 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001320 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001321 {
1322 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001323 offset = 0;
1324
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001325#if DEVICE_TREE
1326 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1327 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001328
1329 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1330 {
1331 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1332 return -1;
1333 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001334#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001335 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001336#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001337
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001338 dprintf(INFO, "Loading (%s) image (%d): start\n",
1339 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001340 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001341
Shashank Mittalcd98d472011-08-02 14:29:24 -07001342 /* Read image without signature */
1343 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1344 {
1345 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1346 return -1;
1347 }
Dima Zavin214cc642009-01-26 11:16:21 -08001348
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001349 dprintf(INFO, "Loading (%s) image (%d): done\n",
1350 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001351 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001352
Shashank Mittalcd98d472011-08-02 14:29:24 -07001353 offset = imagesize_actual;
1354 /* Read signature */
1355 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1356 {
1357 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001358 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001359 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001360
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301361 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001362
1363 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001364 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1365 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001366#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001367 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001368 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001369 {
1370 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1371 return -1;
1372 }
1373
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001374 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1375#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001376
1377 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001378 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001379 {
1380 write_device_info_flash(&device);
1381 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301382#if USE_PCOM_SECBOOT
1383 set_tamper_flag(device.is_tampered);
1384#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001385 }
1386 else
1387 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001388 offset = page_size;
1389
Amol Jadib6be5c12012-11-14 13:39:51 -08001390 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1391 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1392 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1393
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001394 dprintf(INFO, "Loading (%s) image (%d): start\n",
1395 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1396
Amol Jadi492d5a52013-03-15 16:12:34 -07001397 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001398
1399 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001400 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1401 return -1;
1402 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001403 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001404
Amol Jadib6be5c12012-11-14 13:39:51 -08001405 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001406 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1407 return -1;
1408 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001409 offset += ramdisk_actual;
1410
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001411 dprintf(INFO, "Loading (%s) image (%d): done\n",
1412 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1413
Amol Jadi492d5a52013-03-15 16:12:34 -07001414 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001415
1416 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001417 offset += second_actual;
1418 /* Second image loading not implemented. */
1419 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001420 }
1421
1422#if DEVICE_TREE
1423 if(hdr->dt_size != 0) {
1424
1425 /* Read the device tree table into buffer */
1426 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1427 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1428 return -1;
1429 }
1430
1431 table = (struct dt_table*) dt_buf;
1432
Deepa Dinamani19648b42013-09-05 17:05:55 -07001433 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001434 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1435 return -1;
1436 }
1437
Deepa Dinamani19648b42013-09-05 17:05:55 -07001438 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1439 if (!table)
1440 return -1;
1441
1442 /* Read the entire device tree table into buffer */
1443 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1444 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1445 return -1;
1446 }
1447
1448
Joel Kingaa335dc2013-06-03 16:11:08 -07001449 /* Find index of device tree within device tree table */
1450 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001451 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1452 return -1;
1453 }
1454
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001455 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001456 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001457 {
1458 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1459 return -1;
1460 }
1461
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001462 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001463 if(flash_read(ptn, offset + dt_entry.offset,
1464 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001465 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1466 return -1;
1467 }
1468 }
1469#endif
1470
Shashank Mittalcd98d472011-08-02 14:29:24 -07001471 }
David Ng183a7422009-12-07 14:55:21 -08001472continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001473
Dima Zavin214cc642009-01-26 11:16:21 -08001474 /* TODO: create/pass atags to kernel */
1475
Ajay Dudanie28a6072011-07-01 13:59:46 -07001476 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001477 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001478 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1479
1480 return 0;
1481}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001482
Shashank Mittal162244e2011-08-08 19:01:25 -07001483void write_device_info_mmc(device_info *dev)
1484{
Shashank Mittal162244e2011-08-08 19:01:25 -07001485 unsigned long long ptn = 0;
1486 unsigned long long size;
1487 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001488 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001489 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001490 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001491
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001492 if (devinfo_present)
1493 index = partition_get_index("devinfo");
1494 else
1495 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001496
Shashank Mittal162244e2011-08-08 19:01:25 -07001497 ptn = partition_get_offset(index);
1498 if(ptn == 0)
1499 {
1500 return;
1501 }
1502
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001503 lun = partition_get_lun(index);
1504 mmc_set_lun(lun);
1505
Shashank Mittal162244e2011-08-08 19:01:25 -07001506 size = partition_get_size(index);
1507
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001508 blocksize = mmc_get_device_blocksize();
1509
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001510 if (devinfo_present)
1511 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1512 else
1513 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1514 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001515 {
1516 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001517 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001518 }
1519}
1520
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001521void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001522{
Shashank Mittal162244e2011-08-08 19:01:25 -07001523 unsigned long long ptn = 0;
1524 unsigned long long size;
1525 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001526 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001527 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001528
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001529 if ((index = partition_get_index("devinfo")) < 0)
1530 {
1531 devinfo_present = false;
1532 index = partition_get_index("aboot");
1533 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001534
Shashank Mittal162244e2011-08-08 19:01:25 -07001535 ptn = partition_get_offset(index);
1536 if(ptn == 0)
1537 {
1538 return;
1539 }
1540
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001541 mmc_set_lun(partition_get_lun(index));
1542
Shashank Mittal162244e2011-08-08 19:01:25 -07001543 size = partition_get_size(index);
1544
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001545 blocksize = mmc_get_device_blocksize();
1546
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001547 if (devinfo_present)
1548 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1549 else
1550 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1551 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001552 {
1553 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001554 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001555 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001556}
1557
1558void write_device_info_flash(device_info *dev)
1559{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001560 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001561 struct ptentry *ptn;
1562 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001563 if(info == NULL)
1564 {
1565 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1566 ASSERT(0);
1567 }
1568 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001569 ptable = flash_get_ptable();
1570 if (ptable == NULL)
1571 {
1572 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1573 return;
1574 }
1575
1576 ptn = ptable_find(ptable, "devinfo");
1577 if (ptn == NULL)
1578 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001579 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001580 return;
1581 }
1582
1583 memcpy(info, dev, sizeof(device_info));
1584
1585 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1586 {
1587 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1588 return;
1589 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001590 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001591}
1592
vijay kumarc65876c2015-04-24 13:29:16 +05301593static int read_allow_oem_unlock(device_info *dev)
1594{
vijay kumarc65876c2015-04-24 13:29:16 +05301595 unsigned offset;
1596 int index;
1597 unsigned long long ptn;
1598 unsigned long long ptn_size;
1599 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001600 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301601
vijay kumarca2e6812015-07-08 20:28:25 +05301602 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301603 if (index == INVALID_PTN)
1604 {
vijay kumarca2e6812015-07-08 20:28:25 +05301605 index = partition_get_index(frp_ptns[1]);
1606 if (index == INVALID_PTN)
1607 {
1608 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1609 return -1;
1610 }
vijay kumarc65876c2015-04-24 13:29:16 +05301611 }
1612
1613 ptn = partition_get_offset(index);
1614 ptn_size = partition_get_size(index);
1615 offset = ptn_size - blocksize;
1616
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001617 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301618 {
1619 dprintf(CRITICAL, "Reading MMC failed\n");
1620 return -1;
1621 }
1622
1623 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1624 is_allow_unlock = buf[blocksize-1] & 0x01;
1625 return 0;
1626}
1627
1628static int write_allow_oem_unlock(bool allow_unlock)
1629{
vijay kumarc65876c2015-04-24 13:29:16 +05301630 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301631 int index;
1632 unsigned long long ptn;
1633 unsigned long long ptn_size;
1634 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001635 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301636
vijay kumarca2e6812015-07-08 20:28:25 +05301637 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301638 if (index == INVALID_PTN)
1639 {
vijay kumarca2e6812015-07-08 20:28:25 +05301640 index = partition_get_index(frp_ptns[1]);
1641 if (index == INVALID_PTN)
1642 {
1643 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1644 return -1;
1645 }
vijay kumarc65876c2015-04-24 13:29:16 +05301646 }
1647
1648 ptn = partition_get_offset(index);
1649 ptn_size = partition_get_size(index);
1650 offset = ptn_size - blocksize;
1651
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001652 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301653 {
1654 dprintf(CRITICAL, "Reading MMC failed\n");
1655 return -1;
1656 }
1657
1658 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1659 buf[blocksize-1] = allow_unlock;
1660 if (mmc_write(ptn + offset, blocksize, buf))
1661 {
1662 dprintf(CRITICAL, "Writing MMC failed\n");
1663 return -1;
1664 }
1665
1666 return 0;
1667}
1668
Shashank Mittal162244e2011-08-08 19:01:25 -07001669void read_device_info_flash(device_info *dev)
1670{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001671 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001672 struct ptentry *ptn;
1673 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001674 if(info == NULL)
1675 {
1676 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1677 ASSERT(0);
1678 }
1679 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001680 ptable = flash_get_ptable();
1681 if (ptable == NULL)
1682 {
1683 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1684 return;
1685 }
1686
1687 ptn = ptable_find(ptable, "devinfo");
1688 if (ptn == NULL)
1689 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001690 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001691 return;
1692 }
1693
1694 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1695 {
1696 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1697 return;
1698 }
1699
1700 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1701 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001702 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1703 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001704 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001705 write_device_info_flash(info);
1706 }
1707 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001708 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001709}
1710
1711void write_device_info(device_info *dev)
1712{
1713 if(target_is_emmc_boot())
1714 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001715 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1716 if(info == NULL)
1717 {
1718 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1719 ASSERT(0);
1720 }
1721 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001722 memcpy(info, dev, sizeof(struct device_info));
1723
1724#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001725 if (is_secure_boot_enable()) {
1726 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1727 ASSERT(0);
1728 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001729 else
1730 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001731#else
1732 write_device_info_mmc(info);
1733#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001734 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001735 }
1736 else
1737 {
1738 write_device_info_flash(dev);
1739 }
1740}
1741
1742void read_device_info(device_info *dev)
1743{
1744 if(target_is_emmc_boot())
1745 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001746 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1747 if(info == NULL)
1748 {
1749 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1750 ASSERT(0);
1751 }
1752 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001753
1754#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001755 if (is_secure_boot_enable()) {
1756 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1757 ASSERT(0);
1758 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001759 else
1760 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001761#else
1762 read_device_info_mmc(info);
1763#endif
1764
1765 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1766 {
1767 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001768 if (is_secure_boot_enable())
1769 info->is_unlocked = 0;
1770 else
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001771 info->is_unlocked = 1;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001772 info->is_tampered = 0;
1773 info->charger_screen_enabled = 0;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001774 write_device_info(info);
1775 }
1776 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001777 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001778 }
1779 else
1780 {
1781 read_device_info_flash(dev);
1782 }
1783}
1784
1785void reset_device_info()
1786{
1787 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001788 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001789 write_device_info(&device);
1790}
1791
1792void set_device_root()
1793{
1794 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001795 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001796 write_device_info(&device);
1797}
1798
Amol Jadicb524072012-08-09 16:40:18 -07001799#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001800int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
Amol Jadicb524072012-08-09 16:40:18 -07001801{
1802 uint32 dt_image_offset = 0;
1803 uint32_t n;
1804 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001805 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001806 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001807 unsigned int compressed_size = 0;
1808 unsigned int dtb_size = 0;
1809 unsigned int out_avai_len = 0;
1810 unsigned char *out_addr = NULL;
1811 unsigned char *best_match_dt_addr = NULL;
1812 int rc;
Amol Jadicb524072012-08-09 16:40:18 -07001813
1814 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1815
Amol Jadicb524072012-08-09 16:40:18 -07001816 if(hdr->dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001817 /* add kernel offset */
1818 dt_image_offset += page_size;
1819 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1820 dt_image_offset += n;
1821
1822 /* add ramdisk offset */
1823 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1824 dt_image_offset += n;
1825
1826 /* add second offset */
1827 if(hdr->second_size != 0) {
1828 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1829 dt_image_offset += n;
1830 }
1831
1832 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001833 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001834
Deepa Dinamani19648b42013-09-05 17:05:55 -07001835 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001836 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1837 return -1;
1838 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001839 /* Find index of device tree within device tree table */
1840 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001841 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1842 return -1;
1843 }
1844
Matthew Qin271927e2015-03-31 22:07:07 -04001845 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
1846 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
1847 {
1848 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
1849 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04001850 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001851 rc = decompress(best_match_dt_addr,
1852 dt_entry.size, out_addr, out_avai_len,
1853 &compressed_size, &dtb_size);
1854 if (rc)
1855 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001856 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001857 ASSERT(0);
1858 }
1859
Matthew Qin0b15b322015-05-19 05:20:54 -04001860 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001861 best_match_dt_addr = out_addr;
1862 } else {
1863 dtb_size = dt_entry.size;
1864 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001865 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001866 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001867 {
1868 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1869 return -1;
1870 }
1871
Amol Jadicb524072012-08-09 16:40:18 -07001872 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001873 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001874 } else
1875 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001876
1877 /* Everything looks fine. Return success. */
1878 return 0;
1879}
1880#endif
1881
Brian Swetland9c4c0752009-01-25 16:23:50 -08001882void cmd_boot(const char *arg, void *data, unsigned sz)
1883{
Amit Blayba53e6e2015-08-17 09:20:01 +03001884#ifdef MDTP_SUPPORT
1885 static bool is_mdtp_activated = 0;
1886#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08001887 unsigned kernel_actual;
1888 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001889 uint32_t image_actual;
1890 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001891 uint32_t sig_actual = SIGNATURE_SIZE;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001892 struct boot_img_hdr *hdr = NULL;
1893 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001894 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001895 int ret = 0;
1896 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001897 unsigned int out_len = 0;
1898 unsigned int out_avai_len = 0;
1899 unsigned char *out_addr = NULL;
1900 uint32_t dtb_offset = 0;
1901 unsigned char *kernel_start_addr = NULL;
1902 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04001903 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001904
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001905#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07001906 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001907 {
1908 fastboot_fail("unlock device to use this command");
1909 return;
1910 }
1911#endif
1912
Brian Swetland9c4c0752009-01-25 16:23:50 -08001913 if (sz < sizeof(hdr)) {
1914 fastboot_fail("invalid bootimage header");
1915 return;
1916 }
1917
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001918 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001919
1920 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001921 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001922
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001923 if(target_is_emmc_boot() && hdr->page_size) {
1924 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001925 page_mask = page_size - 1;
1926 }
1927
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001928 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1929 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001930#if DEVICE_TREE
1931 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1932#endif
1933
1934 image_actual = ADD_OF(page_size, kernel_actual);
1935 image_actual = ADD_OF(image_actual, ramdisk_actual);
1936 image_actual = ADD_OF(image_actual, dt_actual);
1937
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001938 if (target_use_signed_kernel() && (!device.is_unlocked))
1939 image_actual = ADD_OF(image_actual, sig_actual);
1940
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001941 /* sz should have atleast raw boot image */
1942 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001943 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001944 return;
1945 }
1946
1947 /* Verify the boot image
1948 * device & page_size are initialized in aboot_init
1949 */
1950 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001951 /* Pass size excluding signature size, otherwise we would try to
1952 * access signature beyond its length
1953 */
1954 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001955
Amit Blay4aa292f2015-04-28 21:55:59 +03001956#ifdef MDTP_SUPPORT
1957 else
1958 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03001959 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03001960 mdtp_ext_partition_verification_t ext_partition;
Amit Blayba53e6e2015-08-17 09:20:01 +03001961
1962 if (!is_mdtp_activated) {
1963 ext_partition.partition = MDTP_PARTITION_NONE;
1964 mdtp_fwlock_verify_lock(&ext_partition);
1965 }
Amit Blay4aa292f2015-04-28 21:55:59 +03001966 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03001967
Shay Nachmani062aa3f2015-05-17 17:28:44 +03001968 mdtp_activated(&is_mdtp_activated);
1969 if(is_mdtp_activated){
1970 dprintf(CRITICAL, "fastboot boot command is not available.\n");
1971 return;
1972 }
Amit Blay4aa292f2015-04-28 21:55:59 +03001973#endif /* MDTP_SUPPORT */
1974
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001975 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001976 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1977 * If not, continue booting.
1978 */
1979 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
1980 {
1981 out_addr = (unsigned char *)target_get_scratch_address();
1982 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
1983 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001984 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001985 ret = decompress((unsigned char *)(ptr + page_size),
1986 hdr->kernel_size, out_addr, out_avai_len,
1987 &dtb_offset, &out_len);
1988 if (ret)
1989 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001990 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001991 ASSERT(0);
1992 }
1993
Matthew Qin0b15b322015-05-19 05:20:54 -04001994 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001995 kptr = (struct kernel64_hdr *)out_addr;
1996 kernel_start_addr = out_addr;
1997 kernel_size = out_len;
1998 } else {
1999 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2000 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2001 kernel_size = hdr->kernel_size;
2002 }
2003
2004 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002005 * Update the kernel/ramdisk/tags address if the boot image header
2006 * has default values, these default values come from mkbootimg when
2007 * the boot image is flashed using fastboot flash:raw
2008 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002009 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002010
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002011 /* Get virtual addresses since the hdr saves physical addresses. */
2012 hdr->kernel_addr = VA(hdr->kernel_addr);
2013 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2014 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002015
Matthew Qinbb7923d2015-02-09 10:56:09 +08002016 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002017 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002018 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002019 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2020 {
2021 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002022 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002023 }
2024
Amol Jadicb524072012-08-09 16:40:18 -07002025#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002026 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002027 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002028 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002029
2030 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002031#else
2032 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2033 {
2034 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002035 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002036 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002037#endif
2038
2039 /* Load ramdisk & kernel */
2040 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002041 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002042
2043#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002044 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2045 {
2046 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2047 return;
2048 }
2049
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002050 /*
2051 * If dtb is not found look for appended DTB in the kernel.
2052 * If appended dev tree is found, update the atags with
2053 * memory address to the DTB appended location on RAM.
2054 * Else update with the atags address in the kernel header
2055 */
2056 if (!dtb_copied) {
2057 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002058 dtb = dev_tree_appended((void*)(ptr + page_size),
2059 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002060 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002061 if (!dtb) {
2062 fastboot_fail("dtb not found");
2063 return;
2064 }
Amol Jadicb524072012-08-09 16:40:18 -07002065 }
2066#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002067
2068 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002069 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002070
Dima Zavin77e41f32013-03-06 16:10:43 -08002071 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002072 (const char*) hdr->cmdline, board_machtype(),
2073 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002074}
2075
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002076void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002077{
2078 struct ptentry *ptn;
2079 struct ptable *ptable;
2080
2081 ptable = flash_get_ptable();
2082 if (ptable == NULL) {
2083 fastboot_fail("partition table doesn't exist");
2084 return;
2085 }
2086
2087 ptn = ptable_find(ptable, arg);
2088 if (ptn == NULL) {
2089 fastboot_fail("unknown partition name");
2090 return;
2091 }
2092
2093 if (flash_erase(ptn)) {
2094 fastboot_fail("failed to erase partition");
2095 return;
2096 }
2097 fastboot_okay("");
2098}
2099
Bikas Gurungd48bd242010-09-04 19:54:32 -07002100
2101void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2102{
2103 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002104 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002105 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002106 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002107
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002108#if VERIFIED_BOOT
2109 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2110 {
2111 if(!device.is_unlocked)
2112 {
2113 fastboot_fail("unlock device to erase keystore");
2114 return;
2115 }
2116 }
2117#endif
2118
Kinson Chikf1a43512011-07-14 11:28:39 -07002119 index = partition_get_index(arg);
2120 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002121 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002122
Kinson Chikf1a43512011-07-14 11:28:39 -07002123 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002124 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002125 return;
2126 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002127
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002128 lun = partition_get_lun(index);
2129 mmc_set_lun(lun);
2130
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002131 if (platform_boot_dev_isemmc())
2132 {
2133 if (mmc_erase_card(ptn, size)) {
2134 fastboot_fail("failed to erase partition\n");
2135 return;
2136 }
2137 } else {
2138 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2139 size = partition_get_size(index);
2140 if (size > DEFAULT_ERASE_SIZE)
2141 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002142
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002143 /* Simple inefficient version of erase. Just writing
2144 0 in first several blocks */
2145 if (mmc_write(ptn , size, (unsigned int *)out)) {
2146 fastboot_fail("failed to erase partition");
2147 return;
2148 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002149 }
2150 fastboot_okay("");
2151}
2152
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002153void cmd_erase(const char *arg, void *data, unsigned sz)
2154{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002155#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002156 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002157 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002158 if(!device.is_unlocked && !device.is_verified)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002159 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002160 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002161 return;
2162 }
Channagoud Kadabi35297672015-06-13 11:09:49 -07002163 if(!device.is_unlocked && device.is_verified)
2164 {
2165 if(!boot_verify_flash_allowed(arg))
2166 {
2167 fastboot_fail("cannot flash this partition in verified state");
2168 return;
2169 }
2170 }
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002171 }
2172#endif
2173
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002174 if(target_is_emmc_boot())
2175 cmd_erase_mmc(arg, data, sz);
2176 else
2177 cmd_erase_nand(arg, data, sz);
2178}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002179
Channagoud Kadabi35227322015-05-29 11:14:17 -07002180static uint32_t aboot_get_secret_key()
2181{
2182 /* 0 is invalid secret key, update this implementation to return
2183 * device specific unique secret key
2184 */
2185 return 0;
2186}
2187
Ajay Dudani5c761132011-04-07 20:19:04 -07002188void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002189{
2190 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002191 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002192 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002193 char *token = NULL;
2194 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002195 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002196 uint8_t lun = 0;
2197 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002198
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002199 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002200 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002201 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002202 if(token)
2203 {
2204 lun = atoi(token);
2205 mmc_set_lun(lun);
2206 lun_set = true;
2207 }
2208
Mao Jinlong226f33a2014-07-04 17:24:10 +08002209 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002210 {
Channagoud Kadabi35227322015-05-29 11:14:17 -07002211 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2212 {
2213 if (!aboot_frp_unlock(pname, data, sz))
2214 {
2215 fastboot_info("FRP unlock successful");
2216 fastboot_okay("");
2217 }
2218 else
2219 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2220
2221 return;
2222 }
2223
Mao Jinlong226f33a2014-07-04 17:24:10 +08002224 if (!strcmp(pname, "partition"))
2225 {
2226 dprintf(INFO, "Attempt to write partition image.\n");
2227 if (write_partition(sz, (unsigned char *) data)) {
2228 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002229 return;
2230 }
2231 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002232 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002233 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002234#if VERIFIED_BOOT
2235 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2236 {
2237 if(!device.is_unlocked)
2238 {
2239 fastboot_fail("unlock device to flash keystore");
2240 return;
2241 }
2242 if(!boot_verify_validate_keystore((unsigned char *)data))
2243 {
2244 fastboot_fail("image is not a keystore file");
2245 return;
2246 }
2247 }
2248#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002249 index = partition_get_index(pname);
2250 ptn = partition_get_offset(index);
2251 if(ptn == 0) {
2252 fastboot_fail("partition table doesn't exist");
2253 return;
2254 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002255
Mao Jinlong226f33a2014-07-04 17:24:10 +08002256 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2257 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2258 fastboot_fail("image is not a boot image");
2259 return;
2260 }
2261 }
2262
2263 if(!lun_set)
2264 {
2265 lun = partition_get_lun(index);
2266 mmc_set_lun(lun);
2267 }
2268
2269 size = partition_get_size(index);
2270 if (ROUND_TO_PAGE(sz,511) > size) {
2271 fastboot_fail("size too large");
2272 return;
2273 }
2274 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2275 fastboot_fail("flash write failure");
2276 return;
2277 }
Greg Grisco6e754772011-06-23 12:19:39 -07002278 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002279 }
2280 fastboot_okay("");
2281 return;
2282}
2283
Ajay Dudanide984792015-03-02 09:57:41 -08002284void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2285{
2286 int i, images;
2287 meta_header_t *meta_header;
2288 img_header_entry_t *img_header_entry;
2289
2290 meta_header = (meta_header_t*) data;
2291 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2292
2293 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2294
2295 for (i=0; i<images; i++) {
2296
2297 if((img_header_entry[i].ptn_name == NULL) ||
2298 (img_header_entry[i].start_offset == 0) ||
2299 (img_header_entry[i].size == 0))
2300 break;
2301
2302 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2303 (void *) data + img_header_entry[i].start_offset,
2304 img_header_entry[i].size);
2305 }
2306
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002307 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2308 {
2309 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2310 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2311 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2312 }
2313 else
2314 {
2315 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2316 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2317 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2318 }
2319
2320 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002321 fastboot_okay("");
2322 return;
2323}
2324
Ajay Dudani5c761132011-04-07 20:19:04 -07002325void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2326{
2327 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002328 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002329 uint32_t *fill_buf = NULL;
2330 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002331 sparse_header_t *sparse_header;
2332 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002333 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002334 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302335 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002336 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302337 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002338 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302339 /*End of the sparse image address*/
2340 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002341
Kinson Chikf1a43512011-07-14 11:28:39 -07002342 index = partition_get_index(arg);
2343 ptn = partition_get_offset(index);
2344 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002345 fastboot_fail("partition table doesn't exist");
2346 return;
2347 }
2348
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302349 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302350
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002351 lun = partition_get_lun(index);
2352 mmc_set_lun(lun);
2353
vijay kumar800255e2015-04-24 20:26:19 +05302354 if (sz < sizeof(sparse_header_t)) {
2355 fastboot_fail("size too low");
2356 return;
2357 }
2358
Ajay Dudani5c761132011-04-07 20:19:04 -07002359 /* Read and skip over sparse image header */
2360 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302361
vijay kumar1321f342015-03-27 12:13:42 +05302362 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002363 fastboot_fail("size too large");
2364 return;
2365 }
2366
vijay kumarde4fcf62015-04-23 13:05:49 +05302367 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302368
2369 if (data_end < (uint32_t)data) {
2370 fastboot_fail("buffer overreads occured due to invalid sparse header");
2371 return;
2372 }
2373
vijay kumarde4fcf62015-04-23 13:05:49 +05302374 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002375 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302376 fastboot_fail("sparse header size mismatch");
2377 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002378 }
2379
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002380 dprintf (SPEW, "=== Sparse Image Header ===\n");
2381 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2382 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2383 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2384 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2385 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2386 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2387 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2388 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002389
2390 /* Start processing chunks */
2391 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2392 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302393 /* Make sure the total image size does not exceed the partition size */
2394 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2395 fastboot_fail("size too large");
2396 return;
2397 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002398 /* Read and skip over chunk header */
2399 chunk_header = (chunk_header_t *) data;
2400 data += sizeof(chunk_header_t);
2401
vijay kumar800255e2015-04-24 20:26:19 +05302402 if (data_end < (uint32_t)data) {
2403 fastboot_fail("buffer overreads occured due to invalid sparse header");
2404 return;
2405 }
2406
Ajay Dudani5c761132011-04-07 20:19:04 -07002407 dprintf (SPEW, "=== Chunk Header ===\n");
2408 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2409 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2410 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2411
vijay kumar800255e2015-04-24 20:26:19 +05302412 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002413 {
vijay kumar800255e2015-04-24 20:26:19 +05302414 fastboot_fail("chunk header size mismatch");
2415 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002416 }
2417
wufeng.jiang813dc352015-06-02 23:02:46 -04002418 if (!sparse_header->blk_sz ){
2419 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302420 return;
2421 }
2422
wufeng.jiang813dc352015-06-02 23:02:46 -04002423 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2424
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302425 /* Make sure that the chunk size calculated from sparse image does not
2426 * exceed partition size
2427 */
2428 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2429 {
2430 fastboot_fail("Chunk data size exceeds partition size");
2431 return;
2432 }
2433
Ajay Dudani5c761132011-04-07 20:19:04 -07002434 switch (chunk_header->chunk_type)
2435 {
2436 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002437 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002438 chunk_data_sz))
2439 {
2440 fastboot_fail("Bogus chunk size for chunk type Raw");
2441 return;
2442 }
2443
vijay kumar800255e2015-04-24 20:26:19 +05302444 if (data_end < (uint32_t)data + chunk_data_sz) {
2445 fastboot_fail("buffer overreads occured due to invalid sparse header");
2446 return;
2447 }
2448
wufeng.jiang813dc352015-06-02 23:02:46 -04002449 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2450 otherwise it will return in the line above
2451 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002452 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002453 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002454 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002455 {
2456 fastboot_fail("flash write failure");
2457 return;
2458 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302459 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2460 fastboot_fail("Bogus size for RAW chunk type");
2461 return;
2462 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002463 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002464 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002465 break;
2466
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002467 case CHUNK_TYPE_FILL:
2468 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2469 sizeof(uint32_t)))
2470 {
2471 fastboot_fail("Bogus chunk size for chunk type FILL");
2472 return;
2473 }
2474
2475 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2476 if (!fill_buf)
2477 {
2478 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2479 return;
2480 }
2481
vijay kumar800255e2015-04-24 20:26:19 +05302482 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2483 fastboot_fail("buffer overreads occured due to invalid sparse header");
2484 return;
2485 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002486 fill_val = *(uint32_t *)data;
2487 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002488
2489 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2490 {
2491 fill_buf[i] = fill_val;
2492 }
2493
wufeng.jiang813dc352015-06-02 23:02:46 -04002494 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002495 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302496 /* Make sure that the data written to partition does not exceed partition size */
2497 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2498 {
2499 fastboot_fail("Chunk data size for fill type exceeds partition size");
2500 return;
2501 }
2502
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002503 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2504 sparse_header->blk_sz,
2505 fill_buf))
2506 {
2507 fastboot_fail("flash write failure");
2508 free(fill_buf);
2509 return;
2510 }
2511
2512 total_blocks++;
2513 }
2514
2515 free(fill_buf);
2516 break;
2517
Ajay Dudani5c761132011-04-07 20:19:04 -07002518 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302519 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2520 fastboot_fail("bogus size for chunk DONT CARE type");
2521 return;
2522 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002523 total_blocks += chunk_header->chunk_sz;
2524 break;
2525
Ajay Dudani5c761132011-04-07 20:19:04 -07002526 case CHUNK_TYPE_CRC:
2527 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2528 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002529 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002530 return;
2531 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302532 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2533 fastboot_fail("bogus size for chunk CRC type");
2534 return;
2535 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002536 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302537 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2538 fastboot_fail("integer overflow occured");
2539 return;
2540 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002541 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302542 if (data_end < (uint32_t)data) {
2543 fastboot_fail("buffer overreads occured due to invalid sparse header");
2544 return;
2545 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002546 break;
2547
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002548 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002549 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002550 fastboot_fail("Unknown chunk type");
2551 return;
2552 }
2553 }
2554
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002555 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2556 total_blocks, sparse_header->total_blks);
2557
2558 if(total_blocks != sparse_header->total_blks)
2559 {
2560 fastboot_fail("sparse image write failure");
2561 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002562
2563 fastboot_okay("");
2564 return;
2565}
2566
2567void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2568{
2569 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002570 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002571
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002572#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002573 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2574 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002575 int ret=0;
2576 uint32 major_version=0;
2577 uint32 minor_version=0;
2578
2579 ret = scm_svc_version(&major_version,&minor_version);
2580 if(!ret)
2581 {
2582 if(major_version >= 2)
2583 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002584 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002585 {
2586 ret = encrypt_scm((uint32 **) &data, &sz);
2587 if (ret != 0) {
2588 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2589 return;
2590 }
2591
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002592 /* Protect only for SSD */
2593 if (!strcmp(arg, "ssd")) {
2594 ret = scm_protect_keystore((uint32 *) data, sz);
2595 if (ret != 0) {
2596 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2597 return;
2598 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002599 }
2600 }
2601 else
2602 {
2603 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2604 if(ret != 0)
2605 {
2606 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2607 return;
2608 }
2609 }
2610 }
2611 else
2612 {
2613 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2614 magic_number[1] == DECRYPT_MAGIC_1)
2615 {
2616 ret = decrypt_scm((uint32 **) &data, &sz);
2617 if (ret != 0) {
2618 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2619 return;
2620 }
2621 }
2622 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2623 magic_number[1] == ENCRYPT_MAGIC_1)
2624 {
2625 ret = encrypt_scm((uint32 **) &data, &sz);
2626 if (ret != 0) {
2627 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2628 return;
2629 }
2630 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002631 }
2632 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002633 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002634 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002635 dprintf(CRITICAL,"INVALID SVC Version\n");
2636 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002637 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002638#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002639
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002640#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002641 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002642 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002643 if(!device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002644 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002645 fastboot_fail("device is locked. Cannot flash images");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002646 return;
2647 }
Channagoud Kadabi35297672015-06-13 11:09:49 -07002648 if(!device.is_unlocked && device.is_verified)
2649 {
2650 if(!boot_verify_flash_allowed(arg))
2651 {
2652 fastboot_fail("cannot flash this partition in verified state");
2653 return;
2654 }
2655 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002656 }
2657#endif
2658
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002659 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002660 meta_header = (meta_header_t *) data;
2661 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002662 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002663 else if (meta_header->magic == META_HEADER_MAGIC)
2664 cmd_flash_meta_img(arg, data, sz);
2665 else
2666 cmd_flash_mmc_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002667 return;
2668}
2669
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002670void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2671{
2672 struct ptentry *sys_ptn;
2673 struct ptable *ptable;
2674
2675 ptable = flash_get_ptable();
2676 if (ptable == NULL) {
2677 fastboot_fail("partition table doesn't exist");
2678 return;
2679 }
2680
2681 sys_ptn = ptable_find(ptable, "system");
2682 if (sys_ptn == NULL) {
2683 fastboot_fail("system partition not found");
2684 return;
2685 }
2686
2687 sz = ROUND_TO_PAGE(sz, page_mask);
2688 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2689 fastboot_fail("update_ubi_vol failed");
2690 else
2691 fastboot_okay("");
2692}
2693
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002694void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002695{
2696 struct ptentry *ptn;
2697 struct ptable *ptable;
2698 unsigned extra = 0;
2699
2700 ptable = flash_get_ptable();
2701 if (ptable == NULL) {
2702 fastboot_fail("partition table doesn't exist");
2703 return;
2704 }
2705
2706 ptn = ptable_find(ptable, arg);
2707 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002708 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2709 arg);
2710 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002711 return;
2712 }
2713
2714 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2715 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2716 fastboot_fail("image is not a boot image");
2717 return;
2718 }
2719 }
2720
Amol Jadi5c61a952012-05-04 17:05:35 -07002721 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002722 || !strcmp(ptn->name, "userdata")
2723 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002724 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002725 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002726 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002727 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002728 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002729
2730 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002731 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2732 if (flash_ubi_img(ptn, data, sz)) {
2733 fastboot_fail("flash write failure");
2734 return;
2735 }
2736 } else {
2737 if (flash_write(ptn, extra, data, sz)) {
2738 fastboot_fail("flash write failure");
2739 return;
2740 }
Dima Zavin214cc642009-01-26 11:16:21 -08002741 }
2742 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2743 fastboot_okay("");
2744}
2745
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002746void cmd_flash(const char *arg, void *data, unsigned sz)
2747{
2748 if(target_is_emmc_boot())
2749 cmd_flash_mmc(arg, data, sz);
2750 else
2751 cmd_flash_nand(arg, data, sz);
2752}
2753
Dima Zavin214cc642009-01-26 11:16:21 -08002754void cmd_continue(const char *arg, void *data, unsigned sz)
2755{
2756 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002757 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002758
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002759 if (target_is_emmc_boot())
2760 {
2761 boot_linux_from_mmc();
2762 }
2763 else
2764 {
2765 boot_linux_from_flash();
2766 }
Dima Zavin214cc642009-01-26 11:16:21 -08002767}
2768
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002769void cmd_reboot(const char *arg, void *data, unsigned sz)
2770{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002771 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002772 fastboot_okay("");
2773 reboot_device(0);
2774}
2775
2776void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2777{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002778 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002779 fastboot_okay("");
2780 reboot_device(FASTBOOT_MODE);
2781}
2782
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002783void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2784{
2785 dprintf(INFO, "Enabling charger screen check\n");
2786 device.charger_screen_enabled = 1;
2787 write_device_info(&device);
2788 fastboot_okay("");
2789}
2790
2791void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2792{
2793 dprintf(INFO, "Disabling charger screen check\n");
2794 device.charger_screen_enabled = 0;
2795 write_device_info(&device);
2796 fastboot_okay("");
2797}
2798
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302799void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2800{
2801 dprintf(INFO, "Selecting display panel %s\n", arg);
2802 if (arg)
2803 strlcpy(device.display_panel, arg,
2804 sizeof(device.display_panel));
2805 write_device_info(&device);
2806 fastboot_okay("");
2807}
2808
Shashank Mittal162244e2011-08-08 19:01:25 -07002809void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2810{
vijay kumarc65876c2015-04-24 13:29:16 +05302811 if(!is_allow_unlock) {
2812 fastboot_fail("oem unlock is not allowed");
2813 return;
2814 }
2815
2816 display_fbcon_message("Oem Unlock requested");
2817 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2818}
2819
2820void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
2821{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002822 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002823 {
vijay kumarc65876c2015-04-24 13:29:16 +05302824 if(!is_allow_unlock) {
2825 fastboot_fail("oem unlock is not allowed");
2826 return;
2827 }
2828
Shashank Mittal162244e2011-08-08 19:01:25 -07002829 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002830 device.is_verified = 0;
2831 write_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05302832
2833 struct recovery_message msg;
2834 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2835 write_misc(0, &msg, sizeof(msg));
2836
2837 fastboot_okay("");
2838 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002839 }
2840 fastboot_okay("");
2841}
2842
Channagoud Kadabi35227322015-05-29 11:14:17 -07002843static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
2844{
2845 int ret = 1;
2846 uint32_t secret_key;
2847 char seckey_buffer[MAX_RSP_SIZE];
2848
2849 secret_key = aboot_get_secret_key();
2850 if (secret_key)
2851 {
2852 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
2853 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
2854 {
2855 is_allow_unlock = true;
2856 write_allow_oem_unlock(is_allow_unlock);
2857 ret = 0;
2858 }
2859 }
2860 return ret;
2861}
2862
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002863void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2864{
2865 /* TODO: Wipe user data */
2866 if(device.is_unlocked || device.is_verified)
2867 {
2868 device.is_unlocked = 0;
2869 device.is_verified = 0;
2870 write_device_info(&device);
2871 }
2872 fastboot_okay("");
2873}
2874
2875void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2876{
2877 /* TODO: Wipe user data */
2878 if(device.is_unlocked || !device.is_verified)
2879 {
2880 device.is_unlocked = 0;
2881 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002882 write_device_info(&device);
2883 }
2884 fastboot_okay("");
2885}
2886
Shashank Mittala0032282011-08-26 14:50:11 -07002887void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2888{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302889 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002890 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002891 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002892 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2893 fastboot_info(response);
2894 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002895 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302896 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2897 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002898 fastboot_okay("");
2899}
2900
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002901void cmd_preflash(const char *arg, void *data, unsigned sz)
2902{
2903 fastboot_okay("");
2904}
2905
Mao Flynn7b379f32015-04-20 00:28:30 +08002906static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302907
Mao Flynn7b379f32015-04-20 00:28:30 +08002908int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302909{
Mao Flynn7b379f32015-04-20 00:28:30 +08002910 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302911 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08002912 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302913 return -1;
2914 return 0;
2915}
2916
Mao Flynn7b379f32015-04-20 00:28:30 +08002917int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002918{
2919 struct ptentry *ptn;
2920 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08002921 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002922 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002923
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302924 ptable = flash_get_ptable();
2925 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002926 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2927 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302928 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002929
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302930 ptn = ptable_find(ptable, "splash");
2931 if (ptn == NULL) {
2932 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002933 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302934 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002935 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302936 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002937 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302938 }
2939
Mao Flynn7b379f32015-04-20 00:28:30 +08002940 header = (struct logo_img_header *)logo_header;
2941 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302942 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002943 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302944 }
2945
2946 fb_display = fbcon_display();
2947 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002948 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
2949 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
2950
2951 /* if the logo is full-screen size, remove "fbcon_clear()" */
2952 if ((header->width != fb_display->width)
2953 || (header->height != fb_display->height))
2954 fbcon_clear();
2955
2956 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
2957 (uint32_t *)base,
2958 (header->blocks * 512))) {
2959 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
2960 return -1;
2961 }
2962 fbcon_extract_to_screen(header, base);
2963 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002964 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002965
2966 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
2967 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
2968 return -1;
2969 }
2970
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302971 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08002972 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
2973 (uint32_t *)base,
2974 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302975 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302976 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002977 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002978 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302979 }
2980
Mao Flynn7b379f32015-04-20 00:28:30 +08002981 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302982}
2983
Mao Flynn7b379f32015-04-20 00:28:30 +08002984int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302985{
2986 int index = INVALID_PTN;
2987 unsigned long long ptn = 0;
2988 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08002989 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08002990 uint32_t blocksize, realsize, readsize;
2991 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302992
2993 index = partition_get_index("splash");
2994 if (index == 0) {
2995 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002996 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302997 }
2998
2999 ptn = partition_get_offset(index);
3000 if (ptn == 0) {
3001 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003002 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303003 }
3004
Mao Flynn1893a7f2015-06-03 12:03:36 +08003005 mmc_set_lun(partition_get_lun(index));
3006
3007 blocksize = mmc_get_device_blocksize();
3008 if (blocksize == 0) {
3009 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3010 return -1;
3011 }
3012
3013 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003014 if (!fb_display)
3015 {
3016 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3017 return -1;
3018 }
3019
Mao Flynn1893a7f2015-06-03 12:03:36 +08003020 base = (uint8_t *) fb_display->base;
3021
3022 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303023 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003024 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303025 }
3026
Mao Flynn1893a7f2015-06-03 12:03:36 +08003027 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003028 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303029 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003030 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303031 }
3032
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303033 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003034 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3035 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003036
Mao Flynn1893a7f2015-06-03 12:03:36 +08003037 realsize = header->blocks * 512;
3038 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3039
3040 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003041 if ((header->width != fb_display->width)
3042 || (header->height != fb_display->height))
3043 fbcon_clear();
3044
Mao Flynn1893a7f2015-06-03 12:03:36 +08003045 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003046 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3047 return -1;
3048 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003049
Mao Flynn1893a7f2015-06-03 12:03:36 +08003050 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3051 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303052
Mao Flynn1893a7f2015-06-03 12:03:36 +08003053 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3054 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3055 return -1;
3056 }
3057
3058 realsize = header->width * header->height * fb_display->bpp / 8;
3059 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3060
3061 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3062 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3063 fbcon_clear();
3064 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3065 return -1;
3066 }
3067 } else {
3068 if (mmc_read(ptn + blocksize ,
3069 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3070 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3071 return -1;
3072 }
3073 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3074 }
3075 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303076 }
3077
Mao Flynn7b379f32015-04-20 00:28:30 +08003078 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303079}
3080
Mao Flynn7b379f32015-04-20 00:28:30 +08003081int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303082{
3083 if (target_is_emmc_boot()) {
3084 return splash_screen_mmc();
3085 } else {
3086 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003087 }
3088}
3089
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003090/* Get the size from partiton name */
3091static void get_partition_size(const char *arg, char *response)
3092{
3093 uint64_t ptn = 0;
3094 uint64_t size;
3095 int index = INVALID_PTN;
3096
3097 index = partition_get_index(arg);
3098
3099 if (index == INVALID_PTN)
3100 {
3101 dprintf(CRITICAL, "Invalid partition index\n");
3102 return;
3103 }
3104
3105 ptn = partition_get_offset(index);
3106
3107 if(!ptn)
3108 {
3109 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3110 return;
3111 }
3112
3113 size = partition_get_size(index);
3114
3115 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3116 return;
3117}
3118
3119/*
3120 * Publish the partition type & size info
3121 * fastboot getvar will publish the required information.
3122 * fastboot getvar partition_size:<partition_name>: partition size in hex
3123 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3124 */
3125static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3126{
3127 uint8_t i;
3128
3129 for (i = 0; i < num_parts; i++) {
3130 get_partition_size(info[i].part_name, info[i].size_response);
3131
3132 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3133 {
3134 dprintf(CRITICAL, "partition size name truncated\n");
3135 return;
3136 }
3137 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3138 {
3139 dprintf(CRITICAL, "partition type name truncated\n");
3140 return;
3141 }
3142
3143 /* publish partition size & type info */
3144 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3145 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3146 }
3147}
3148
Amol Jadi5edf3552013-07-23 14:15:34 -07003149/* register commands and variables for fastboot */
3150void aboot_fastboot_register_commands(void)
3151{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003152 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07003153
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003154 struct fastboot_cmd_desc cmd_list[] = {
3155 /* By default the enabled list is empty. */
3156 {"", NULL},
3157 /* move commands enclosed within the below ifndef to here
3158 * if they need to be enabled in user build.
3159 */
3160#ifndef DISABLE_FASTBOOT_CMDS
3161 /* Register the following commands only for non-user builds */
3162 {"flash:", cmd_flash},
3163 {"erase:", cmd_erase},
3164 {"boot", cmd_boot},
3165 {"continue", cmd_continue},
3166 {"reboot", cmd_reboot},
3167 {"reboot-bootloader", cmd_reboot_bootloader},
3168 {"oem unlock", cmd_oem_unlock},
vijay kumarc65876c2015-04-24 13:29:16 +05303169 {"oem unlock-go", cmd_oem_unlock_go},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003170 {"oem lock", cmd_oem_lock},
3171 {"oem verified", cmd_oem_verified},
3172 {"oem device-info", cmd_oem_devinfo},
3173 {"preflash", cmd_preflash},
3174 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3175 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08003176 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003177#endif
3178 };
3179
3180 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3181 for (i = 1; i < fastboot_cmds_count; i++)
3182 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3183
Amol Jadi5edf3552013-07-23 14:15:34 -07003184 /* publish variables and their values */
3185 fastboot_publish("product", TARGET(BOARD));
3186 fastboot_publish("kernel", "lk");
3187 fastboot_publish("serialno", sn_buf);
3188
3189 /*
3190 * partition info is supported only for emmc partitions
3191 * Calling this for NAND prints some error messages which
3192 * is harmless but misleading. Avoid calling this for NAND
3193 * devices.
3194 */
3195 if (target_is_emmc_boot())
3196 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3197
3198 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003199 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3200 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003201 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003202 /* Is the charger screen check enabled */
3203 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3204 device.charger_screen_enabled);
3205 fastboot_publish("charger-screen-enabled",
3206 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303207 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3208 device.display_panel);
3209 fastboot_publish("display-panel",
3210 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003211 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3212 fastboot_publish("version-baseband", (const char *) device.radio_version);
Amol Jadi5edf3552013-07-23 14:15:34 -07003213}
3214
Brian Swetland9c4c0752009-01-25 16:23:50 -08003215void aboot_init(const struct app_descriptor *app)
3216{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003217 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03003218 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003219
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003220 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003221 if (target_is_emmc_boot())
3222 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003223 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003224 page_mask = page_size - 1;
3225 }
3226 else
3227 {
3228 page_size = flash_page_size();
3229 page_mask = page_size - 1;
3230 }
3231
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003232 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3233
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003234 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303235 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003236
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003237 /* Display splash screen if enabled */
3238#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003239#if NO_ALARM_DISPLAY
3240 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003241#endif
lijuang99c02d82015-02-13 19:04:34 +08003242 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003243#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003244 /* Wait if the display shutdown is in progress */
3245 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003246 if (!pm_appsbl_display_init_done())
3247 target_display_init(device.display_panel);
3248 else
3249 display_image_on_screen();
3250#else
lijuang99c02d82015-02-13 19:04:34 +08003251 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003252#endif
lijuang99c02d82015-02-13 19:04:34 +08003253 dprintf(SPEW, "Display Init: Done\n");
3254#if NO_ALARM_DISPLAY
3255 }
3256#endif
3257#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003258
Greg Griscod6250552011-06-29 14:40:23 -07003259 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003260 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003261
Dhaval Patel223ec952013-07-18 14:49:44 -07003262 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3263
Matthew Qindefd5562014-07-11 18:02:40 +08003264 /*
3265 * Check power off reason if user force reset,
3266 * if yes phone will do normal boot.
3267 */
3268 if (is_user_force_reset())
3269 goto normal_boot;
3270
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003271 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003272 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003273 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003274 dprintf(ALWAYS,"dload mode key sequence detected\n");
3275 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003276 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003277 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003278 }
3279 else
3280 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303281 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003282 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003283 }
3284 boot_into_fastboot = true;
3285 }
3286 if (!boot_into_fastboot)
3287 {
3288 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3289 boot_into_recovery = 1;
3290 if (!boot_into_recovery &&
3291 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003292 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003293 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003294 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003295 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003296 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003297 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003298
Channagoud Kadabib6e476b2015-08-05 16:23:42 -07003299#if USE_PON_REBOOT_REG
3300 reboot_mode = check_hard_reboot_mode();
3301#else
Ajay Dudani77421292010-10-27 19:34:06 -07003302 reboot_mode = check_reboot_mode();
Channagoud Kadabib6e476b2015-08-05 16:23:42 -07003303#endif
3304 if (reboot_mode == RECOVERY_MODE)
3305 {
Ajay Dudani77421292010-10-27 19:34:06 -07003306 boot_into_recovery = 1;
Channagoud Kadabib6e476b2015-08-05 16:23:42 -07003307 }
3308 else if(reboot_mode == FASTBOOT_MODE)
3309 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003310 boot_into_fastboot = true;
Channagoud Kadabib6e476b2015-08-05 16:23:42 -07003311 }
3312 else if(reboot_mode == ALARM_BOOT)
3313 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003314 boot_reason_alarm = true;
Ajay Dudani77421292010-10-27 19:34:06 -07003315 }
3316
Matthew Qindefd5562014-07-11 18:02:40 +08003317normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003318 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003319 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003320 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003321 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003322 if(emmc_recovery_init())
3323 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3324 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003325 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003326 if((device.is_unlocked) || (device.is_tampered))
3327 {
3328 #ifdef TZ_TAMPER_FUSE
3329 set_tamper_fuse_cmd();
3330 #endif
3331 #if USE_PCOM_SECBOOT
3332 set_tamper_flag(device.is_tampered);
3333 #endif
3334 }
Shashank Mittala0032282011-08-26 14:50:11 -07003335 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003336
Pavel Nedev5d91d412013-04-29 11:34:24 +03003337 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003338 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003339 else
3340 {
3341 recovery_init();
3342 #if USE_PCOM_SECBOOT
3343 if((device.is_unlocked) || (device.is_tampered))
3344 set_tamper_flag(device.is_tampered);
3345 #endif
3346 boot_linux_from_flash();
3347 }
3348 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3349 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003350 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003351
Amol Jadi5edf3552013-07-23 14:15:34 -07003352 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003353
Amol Jadi5edf3552013-07-23 14:15:34 -07003354 /* register aboot specific fastboot commands */
3355 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003356
Amol Jadi5edf3552013-07-23 14:15:34 -07003357 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003358 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003359
3360 /* initialize and start fastboot */
3361 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08003362}
3363
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003364uint32_t get_page_size()
3365{
3366 return page_size;
3367}
3368
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003369/*
3370 * Calculated and save hash (SHA256) for non-signed boot image.
3371 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003372 * @param image_addr - Boot image address
3373 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003374 *
3375 * @return int - 0 on success, negative value on failure.
3376 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003377static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003378{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003379 unsigned int digest[8];
3380#if IMAGE_VERIF_ALGO_SHA1
3381 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3382#else
3383 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3384#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003385
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003386 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003387 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003388
3389 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003390 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003391
3392 return 0;
3393}
3394
Brian Swetland9c4c0752009-01-25 16:23:50 -08003395APP_START(aboot)
3396 .init = aboot_init,
3397APP_END