blob: afb6c757ff8ae77cc4d36645711f67b820e1f3ec [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"
Channagoud Kadabi736c4962015-08-21 11:56:52 -070091#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070092
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070093extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070094extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070095extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070096extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +053097 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070098
Sridhar Parasuram7e16d172015-07-05 11:35:23 -070099void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700100void write_device_info_mmc(device_info *dev);
101void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700102static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabi35227322015-05-29 11:14:17 -0700103static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700104
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700105/* fastboot command function pointer */
106typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
107
108struct fastboot_cmd_desc {
109 char * name;
110 fastboot_cmd_fn cb;
111};
112
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700113#define EXPAND(NAME) #NAME
114#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700115
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800116#ifdef MEMBASE
117#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
118#else
David Ng183a7422009-12-07 14:55:21 -0800119#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800120#endif
121
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700122#ifndef MEMSIZE
123#define MEMSIZE 1024*1024
124#endif
125
126#define MAX_TAGS_SIZE 1024
127
Kun Liang2f1601a2013-08-12 16:29:54 +0800128/* make 4096 as default size to ensure EFS,EXT4's erasing */
129#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800130#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800131
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700132#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700133#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800134
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800135#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
136
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700137#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
138
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800139#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700140static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700141#else
David Ng183a7422009-12-07 14:55:21 -0800142static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700143#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800144static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300145static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800146static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800147static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800148static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700149static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300150static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200151static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800152
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800153static const char *baseband_apq = " androidboot.baseband=apq";
154static const char *baseband_msm = " androidboot.baseband=msm";
155static const char *baseband_csfb = " androidboot.baseband=csfb";
156static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700157static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800158static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700159static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800160static const char *baseband_dsda = " androidboot.baseband=dsda";
161static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800162static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800163static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800164
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700165static unsigned page_size = 0;
166static unsigned page_mask = 0;
167static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
168static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530169static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800170static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700171static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700172bool boot_into_fastboot = false;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700173
Shashank Mittalcd98d472011-08-02 14:29:24 -0700174/* Assuming unauthorized kernel image by default */
175static int auth_kernel_img = 0;
176
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -0800177static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
vijay kumarc65876c2015-04-24 13:29:16 +0530178static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700179
vijay kumarca2e6812015-07-08 20:28:25 +0530180static char frp_ptns[2][8] = {"config","frp"};
181
Dima Zavin42168f22009-01-30 11:52:22 -0800182struct atag_ptbl_entry
183{
184 char name[16];
185 unsigned offset;
186 unsigned size;
187 unsigned flags;
188};
189
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700190/*
191 * Partition info, required to be published
192 * for fastboot
193 */
194struct getvar_partition_info {
195 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
196 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
197 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
198 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
199 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
200};
201
202/*
203 * Right now, we are publishing the info for only
204 * three partitions
205 */
206struct getvar_partition_info part_info[] =
207{
208 { "system" , "partition-size:", "partition-type:", "", "ext4" },
209 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
210 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
211};
212
213char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700214char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800215char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700216char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530217char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700218
Greg Griscod2471ef2011-07-14 13:00:42 -0700219extern int emmc_recovery_init(void);
220
Kinson Chik0b1c8162011-08-31 16:31:57 -0700221#if NO_KEYPAD_DRIVER
222extern int fastboot_trigger(void);
223#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700224
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800225static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700226{
227 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700228#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800229 if (is_arm64)
230 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
231 else
232 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700233 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
234 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700235#endif
236}
237
Dima Zavin42168f22009-01-30 11:52:22 -0800238static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
239{
240 struct atag_ptbl_entry atag_ptn;
241
242 memcpy(atag_ptn.name, ptn->name, 16);
243 atag_ptn.name[15] = '\0';
244 atag_ptn.offset = ptn->start;
245 atag_ptn.size = ptn->length;
246 atag_ptn.flags = ptn->flags;
247 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
248 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
249}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800250
Neeti Desaie245d492012-06-01 12:52:13 -0700251unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800252{
David Ng183a7422009-12-07 14:55:21 -0800253 int cmdline_len = 0;
254 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800255 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700256 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800257 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300258 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700259 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700260 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200261 bool is_mdtp_activated = 0;
262#ifdef MDTP_SUPPORT
263 mdtp_activated(&is_mdtp_activated);
264#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800265
Brian Swetland9c4c0752009-01-25 16:23:50 -0800266 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800267 cmdline_len = strlen(cmdline);
268 have_cmdline = 1;
269 }
270 if (target_is_emmc_boot()) {
271 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800272#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700273 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
274 ASSERT(boot_dev_buf);
275 platform_boot_dev_cmdline(boot_dev_buf);
276 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700277#endif
David Ng183a7422009-12-07 14:55:21 -0800278 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800279
280 cmdline_len += strlen(usb_sn_cmdline);
281 cmdline_len += strlen(sn_buf);
282
Pavel Nedev5614d222013-06-17 18:01:02 +0300283 if (boot_into_recovery && gpt_exists)
284 cmdline_len += strlen(secondary_gpt_enable);
285
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200286 if(is_mdtp_activated)
287 cmdline_len += strlen(mdtp_activated_flag);
288
Pavel Nedev328ac822013-04-05 15:25:11 +0300289 if (boot_into_ffbm) {
290 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700291 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800292 /* reduce kernel console messages to speed-up boot */
293 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800294 } else if (boot_reason_alarm) {
295 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800296 } else if ((target_build_variant_user() || device.charger_screen_enabled)
297 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700298 pause_at_bootup = 1;
299 cmdline_len += strlen(battchg_pause);
300 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800301
Shashank Mittalcd98d472011-08-02 14:29:24 -0700302 if(target_use_signed_kernel() && auth_kernel_img) {
303 cmdline_len += strlen(auth_kernel);
304 }
305
Joonwoo Park61112782013-10-02 19:50:39 -0700306 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
307 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530308 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700309 have_target_boot_params = 1;
310 cmdline_len += strlen(target_boot_params);
311 }
312
Ajay Dudanid04110c2011-01-17 23:55:07 -0800313 /* Determine correct androidboot.baseband to use */
314 switch(target_baseband())
315 {
316 case BASEBAND_APQ:
317 cmdline_len += strlen(baseband_apq);
318 break;
319
320 case BASEBAND_MSM:
321 cmdline_len += strlen(baseband_msm);
322 break;
323
324 case BASEBAND_CSFB:
325 cmdline_len += strlen(baseband_csfb);
326 break;
327
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800328 case BASEBAND_SVLTE2A:
329 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800330 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700331
332 case BASEBAND_MDM:
333 cmdline_len += strlen(baseband_mdm);
334 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700335
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800336 case BASEBAND_MDM2:
337 cmdline_len += strlen(baseband_mdm2);
338 break;
339
Amol Jadi5c61a952012-05-04 17:05:35 -0700340 case BASEBAND_SGLTE:
341 cmdline_len += strlen(baseband_sglte);
342 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530343
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800344 case BASEBAND_SGLTE2:
345 cmdline_len += strlen(baseband_sglte2);
346 break;
347
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530348 case BASEBAND_DSDA:
349 cmdline_len += strlen(baseband_dsda);
350 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800351
352 case BASEBAND_DSDA2:
353 cmdline_len += strlen(baseband_dsda2);
354 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800355 }
356
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800357 if (cmdline) {
358 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530359 target_display_panel_node(display_panel_buf,
360 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800361 strlen(display_panel_buf)) {
362 cmdline_len += strlen(display_panel_buf);
363 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700364 }
365
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800366 if (target_warm_boot()) {
367 warm_boot = true;
368 cmdline_len += strlen(warmboot_cmdline);
369 }
370
David Ng183a7422009-12-07 14:55:21 -0800371 if (cmdline_len > 0) {
372 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800373 unsigned char *dst;
374
375 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
376 ASSERT(cmdline_final != NULL);
377 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700378
Amol Jadi168b7712012-03-06 16:15:00 -0800379 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800380 if (have_cmdline) {
381 src = cmdline;
382 while ((*dst++ = *src++));
383 }
384 if (target_is_emmc_boot()) {
385 src = emmc_cmdline;
386 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700387 have_cmdline = 1;
388 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800389#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700390 src = boot_dev_buf;
391 if (have_cmdline) --dst;
392 while ((*dst++ = *src++));
393#endif
David Ngf773dde2010-07-26 19:55:08 -0700394 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800395
396 src = usb_sn_cmdline;
397 if (have_cmdline) --dst;
398 have_cmdline = 1;
399 while ((*dst++ = *src++));
400 src = sn_buf;
401 if (have_cmdline) --dst;
402 have_cmdline = 1;
403 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800404 if (warm_boot) {
405 if (have_cmdline) --dst;
406 src = warmboot_cmdline;
407 while ((*dst++ = *src++));
408 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800409
Pavel Nedev5614d222013-06-17 18:01:02 +0300410 if (boot_into_recovery && gpt_exists) {
411 src = secondary_gpt_enable;
412 if (have_cmdline) --dst;
413 while ((*dst++ = *src++));
414 }
415
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200416 if (is_mdtp_activated) {
417 src = mdtp_activated_flag;
418 if (have_cmdline) --dst;
419 while ((*dst++ = *src++));
420 }
421
Pavel Nedev328ac822013-04-05 15:25:11 +0300422 if (boot_into_ffbm) {
423 src = androidboot_mode;
424 if (have_cmdline) --dst;
425 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700426 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300427 if (have_cmdline) --dst;
428 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800429 src = loglevel;
430 if (have_cmdline) --dst;
431 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800432 } else if (boot_reason_alarm) {
433 src = alarmboot_cmdline;
434 if (have_cmdline) --dst;
435 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300436 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700437 src = battchg_pause;
438 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800439 while ((*dst++ = *src++));
440 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800441
Shashank Mittalcd98d472011-08-02 14:29:24 -0700442 if(target_use_signed_kernel() && auth_kernel_img) {
443 src = auth_kernel;
444 if (have_cmdline) --dst;
445 while ((*dst++ = *src++));
446 }
447
Ajay Dudanid04110c2011-01-17 23:55:07 -0800448 switch(target_baseband())
449 {
450 case BASEBAND_APQ:
451 src = baseband_apq;
452 if (have_cmdline) --dst;
453 while ((*dst++ = *src++));
454 break;
455
456 case BASEBAND_MSM:
457 src = baseband_msm;
458 if (have_cmdline) --dst;
459 while ((*dst++ = *src++));
460 break;
461
462 case BASEBAND_CSFB:
463 src = baseband_csfb;
464 if (have_cmdline) --dst;
465 while ((*dst++ = *src++));
466 break;
467
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800468 case BASEBAND_SVLTE2A:
469 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800470 if (have_cmdline) --dst;
471 while ((*dst++ = *src++));
472 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700473
474 case BASEBAND_MDM:
475 src = baseband_mdm;
476 if (have_cmdline) --dst;
477 while ((*dst++ = *src++));
478 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700479
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800480 case BASEBAND_MDM2:
481 src = baseband_mdm2;
482 if (have_cmdline) --dst;
483 while ((*dst++ = *src++));
484 break;
485
Amol Jadi5c61a952012-05-04 17:05:35 -0700486 case BASEBAND_SGLTE:
487 src = baseband_sglte;
488 if (have_cmdline) --dst;
489 while ((*dst++ = *src++));
490 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530491
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800492 case BASEBAND_SGLTE2:
493 src = baseband_sglte2;
494 if (have_cmdline) --dst;
495 while ((*dst++ = *src++));
496 break;
497
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530498 case BASEBAND_DSDA:
499 src = baseband_dsda;
500 if (have_cmdline) --dst;
501 while ((*dst++ = *src++));
502 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800503
504 case BASEBAND_DSDA2:
505 src = baseband_dsda2;
506 if (have_cmdline) --dst;
507 while ((*dst++ = *src++));
508 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800509 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700510
511 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700512 src = display_panel_buf;
513 if (have_cmdline) --dst;
514 while ((*dst++ = *src++));
515 }
Joonwoo Park61112782013-10-02 19:50:39 -0700516
517 if (have_target_boot_params) {
518 if (have_cmdline) --dst;
519 src = target_boot_params;
520 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530521 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700522 }
Neeti Desaie245d492012-06-01 12:52:13 -0700523 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700524
525
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700526 if (boot_dev_buf)
527 free(boot_dev_buf);
528
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800529 if (cmdline_final)
530 dprintf(INFO, "cmdline: %s\n", cmdline_final);
531 else
532 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700533 return cmdline_final;
534}
535
536unsigned *atag_core(unsigned *ptr)
537{
538 /* CORE */
539 *ptr++ = 2;
540 *ptr++ = 0x54410001;
541
542 return ptr;
543
544}
545
546unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
547 unsigned ramdisk_size)
548{
549 if (ramdisk_size) {
550 *ptr++ = 4;
551 *ptr++ = 0x54420005;
552 *ptr++ = (unsigned)ramdisk;
553 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800554 }
555
Neeti Desaie245d492012-06-01 12:52:13 -0700556 return ptr;
557}
558
559unsigned *atag_ptable(unsigned **ptr_addr)
560{
561 int i;
562 struct ptable *ptable;
563
564 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700565 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
566 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700567 *(*ptr_addr)++ = 0x4d534d70;
568 for (i = 0; i < ptable->count; ++i)
569 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
570 }
571
572 return (*ptr_addr);
573}
574
575unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
576{
577 int cmdline_length = 0;
578 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700579 char *dest;
580
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800581 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700582 n = (cmdline_length + 4) & (~3);
583
584 *ptr++ = (n / 4) + 2;
585 *ptr++ = 0x54410009;
586 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800587 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700588 ptr += (n / 4);
589
590 return ptr;
591}
592
593unsigned *atag_end(unsigned *ptr)
594{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800595 /* END */
596 *ptr++ = 0;
597 *ptr++ = 0;
598
Neeti Desaie245d492012-06-01 12:52:13 -0700599 return ptr;
600}
601
602void generate_atags(unsigned *ptr, const char *cmdline,
603 void *ramdisk, unsigned ramdisk_size)
604{
605
606 ptr = atag_core(ptr);
607 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
608 ptr = target_atag_mem(ptr);
609
610 /* Skip NAND partition ATAGS for eMMC boot */
611 if (!target_is_emmc_boot()){
612 ptr = atag_ptable(&ptr);
613 }
614
615 ptr = atag_cmdline(ptr, cmdline);
616 ptr = atag_end(ptr);
617}
618
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700619typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700620void boot_linux(void *kernel, unsigned *tags,
621 const char *cmdline, unsigned machtype,
622 void *ramdisk, unsigned ramdisk_size)
623{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800624 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800625#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700626 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800627#endif
628
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700629 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800630 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800631 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800632
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530633 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700634
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800635 final_cmdline = update_cmdline((const char*)cmdline);
636
Neeti Desai17379b82012-06-04 18:42:53 -0700637#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800638 dprintf(INFO, "Updating device tree: start\n");
639
Neeti Desai17379b82012-06-04 18:42:53 -0700640 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530641 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700642 if(ret)
643 {
644 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
645 ASSERT(0);
646 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800647 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700648#else
Neeti Desaie245d492012-06-01 12:52:13 -0700649 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800650 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700651#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700652
Maria Yu52254c02014-07-04 16:14:54 +0800653 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700654
655#if VERIFIED_BOOT
656 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700657 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700658 {
659 dprintf(INFO, "Failed to write protect dev info\n");
660 ASSERT(0);
661 }
662#endif
663
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700664 /* Perform target specific cleanup */
665 target_uninit();
666
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800667 /* Turn off splash screen if enabled */
668#if DISPLAY_SPLASH_SCREEN
669 target_display_shutdown();
670#endif
671
672
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800673 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530674 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800675
676 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700677
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +0530678 /* Initialise wdog to catch early kernel crashes */
679#if WDOG_SUPPORT
680 msm_wdog_init();
681#endif
Amol Jadi4421e652011-06-16 15:00:48 -0700682 /* do any platform specific cleanup before kernel entry */
683 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700684
Brian Swetland9c4c0752009-01-25 16:23:50 -0800685 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700686
Amol Jadi504f9fe2012-08-16 13:56:48 -0700687#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800688 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700689#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700690 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800691
692 if (IS_ARM64(kptr))
693 /* Jump to a 64bit kernel */
694 scm_elexec_call((paddr_t)kernel, tags_phys);
695 else
696 /* Jump to a 32bit kernel */
697 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800698}
699
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700700/* Function to check if the memory address range falls within the aboot
701 * boundaries.
702 * start: Start of the memory region
703 * size: Size of the memory region
704 */
705int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
706{
707 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800708 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700709 return -1;
710
711 /* Check for memory overlap. */
712 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
713 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700714 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700715 return 0;
716 else
717 return -1;
718}
719
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800720#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800721
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800722BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800723#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800724BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800725#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800726
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700727static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
728{
729 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800730
731#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800732#if IMAGE_VERIF_ALGO_SHA1
733 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
734#else
735 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
736#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800737#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700738
739 /* Assume device is rooted at this time. */
740 device.is_tampered = 1;
741
742 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
743
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700744#if VERIFIED_BOOT
745 if(boot_into_recovery)
746 {
747 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530748 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700749 }
750 else
751 {
752 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530753 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700754 }
755 boot_verify_print_state();
756#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700757 ret = image_verify((unsigned char *)bootimg_addr,
758 (unsigned char *)(bootimg_addr + bootimg_size),
759 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800760 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700761#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700762 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
763
764 if (ret)
765 {
766 /* Authorized kernel */
767 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700768 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700769 }
770
Amit Blay4aa292f2015-04-28 21:55:59 +0300771#ifdef MDTP_SUPPORT
772 {
773 /* Verify MDTP lock.
774 * For boot & recovery partitions, use aboot's verification result.
775 */
776 mdtp_ext_partition_verification_t ext_partition;
777 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
778 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
779 ext_partition.page_size = 0; /* Not needed since already validated */
780 ext_partition.image_addr = 0; /* Not needed since already validated */
781 ext_partition.image_size = 0; /* Not needed since already validated */
782 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
783 mdtp_fwlock_verify_lock(&ext_partition);
784 }
785#endif /* MDTP_SUPPORT */
786
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700787#if USE_PCOM_SECBOOT
788 set_tamper_flag(device.is_tampered);
789#endif
790
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700791#if VERIFIED_BOOT
792 if(boot_verify_get_state() == RED)
793 {
794 if(!boot_into_recovery)
795 {
796 dprintf(CRITICAL,
797 "Device verification failed. Rebooting into recovery.\n");
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +0530798 mdelay(1000);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700799 reboot_device(RECOVERY_MODE);
800 }
801 else
802 {
803 dprintf(CRITICAL,
804 "Recovery image verification failed. Asserting..\n");
805 ASSERT(0);
806 }
807 }
808#endif
Unnati Gandhi1be04752015-03-27 19:41:53 +0530809
810 if(device.is_tampered)
811 {
812 write_device_info_mmc(&device);
813 #ifdef TZ_TAMPER_FUSE
814 set_tamper_fuse_cmd();
815 #endif
816 #ifdef ASSERT_ON_TAMPER
817 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
818 ASSERT(0);
819 #endif
820 }
821
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700822}
823
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530824static bool check_format_bit()
825{
826 bool ret = false;
827 int index;
828 uint64_t offset;
829 struct boot_selection_info *in = NULL;
830 char *buf = NULL;
831
832 index = partition_get_index("bootselect");
833 if (index == INVALID_PTN)
834 {
835 dprintf(INFO, "Unable to locate /bootselect partition\n");
836 return ret;
837 }
838 offset = partition_get_offset(index);
839 if(!offset)
840 {
841 dprintf(INFO, "partition /bootselect doesn't exist\n");
842 return ret;
843 }
844 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
845 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530846 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530847 {
848 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
849 free(buf);
850 return ret;
851 }
852 in = (struct boot_selection_info *) buf;
853 if ((in->signature == BOOTSELECT_SIGNATURE) &&
854 (in->version == BOOTSELECT_VERSION)) {
855 if ((in->state_info & BOOTSELECT_FORMAT) &&
856 !(in->state_info & BOOTSELECT_FACTORY))
857 ret = true;
858 } else {
859 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
860 in->signature, in->version);
861 ASSERT(0);
862 }
863 free(buf);
864 return ret;
865}
866
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700867void boot_verifier_init()
868{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700869 uint32_t boot_state;
870 /* Check if device unlock */
871 if(device.is_unlocked)
872 {
873 boot_verify_send_event(DEV_UNLOCK);
874 boot_verify_print_state();
875 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
876 return;
877 }
878 else
879 {
880 boot_verify_send_event(BOOT_INIT);
881 }
882
883 /* Initialize keystore */
884 boot_state = boot_verify_keystore_init();
885 if(boot_state == YELLOW)
886 {
887 boot_verify_print_state();
888 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
889 }
890}
891
Shashank Mittal23b8f422010-04-16 19:27:21 -0700892int boot_linux_from_mmc(void)
893{
894 struct boot_img_hdr *hdr = (void*) buf;
895 struct boot_img_hdr *uhdr;
896 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700897 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700898 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700899 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700900
Shashank Mittalcd98d472011-08-02 14:29:24 -0700901 unsigned char *image_addr = 0;
902 unsigned kernel_actual;
903 unsigned ramdisk_actual;
904 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700905 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700906
Matthew Qin271927e2015-03-31 22:07:07 -0400907 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +0800908 unsigned int out_len = 0;
909 unsigned int out_avai_len = 0;
910 unsigned char *out_addr = NULL;
911 uint32_t dtb_offset = 0;
912 unsigned char *kernel_start_addr = NULL;
913 unsigned int kernel_size = 0;
914 int rc;
915
Neeti Desai465491e2012-07-31 12:53:35 -0700916#if DEVICE_TREE
917 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700918 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700919 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800920 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700921 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -0400922 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -0700923#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +0800924 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800925
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530926 if (check_format_bit())
927 boot_into_recovery = 1;
928
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700929 if (!boot_into_recovery) {
930 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
931 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
932 if (rcode <= 0) {
933 boot_into_ffbm = false;
934 if (rcode < 0)
935 dprintf(CRITICAL,"failed to get ffbm cookie");
936 } else
937 boot_into_ffbm = true;
938 } else
939 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700940 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
941 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
942 dprintf(INFO, "Unified boot method!\n");
943 hdr = uhdr;
944 goto unified_boot;
945 }
Greg Griscod6250552011-06-29 14:40:23 -0700946 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700947 index = partition_get_index("boot");
948 ptn = partition_get_offset(index);
949 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700950 dprintf(CRITICAL, "ERROR: No boot partition found\n");
951 return -1;
952 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700953 }
954 else {
955 index = partition_get_index("recovery");
956 ptn = partition_get_offset(index);
957 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700958 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
959 return -1;
960 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700961 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -0800962 /* Set Lun for boot & recovery partitions */
963 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -0700964
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530965 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700966 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
967 return -1;
968 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700969
970 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700971 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700972 return -1;
973 }
974
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700975 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530976
977 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
978 dprintf(CRITICAL, "ERROR: Invalid page size\n");
979 return -1;
980 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700981 page_size = hdr->page_size;
982 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700983 }
984
Matthew Qin49e51fa2015-02-09 10:40:45 +0800985 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
986 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800987
Matthew Qin49e51fa2015-02-09 10:40:45 +0800988 image_addr = (unsigned char *)target_get_scratch_address();
989
990#if DEVICE_TREE
991 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
992 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
993#else
994 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
995#endif
996
997#if VERIFIED_BOOT
998 boot_verifier_init();
999#endif
1000
1001 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
1002 {
1003 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1004 return -1;
1005 }
1006
Matthew Qinbb7923d2015-02-09 10:56:09 +08001007 /*
1008 * Update loading flow of bootimage to support compressed/uncompressed
1009 * bootimage on both 64bit and 32bit platform.
1010 * 1. Load bootimage from emmc partition onto DDR.
1011 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1012 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1013 * platform accordingly.
1014 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1015 */
1016
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001017 dprintf(INFO, "Loading (%s) image (%d): start\n",
1018 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001019 bs_set_timestamp(BS_KERNEL_LOAD_START);
1020
Matthew Qinbb7923d2015-02-09 10:56:09 +08001021 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001022 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1023 {
1024 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1025 return -1;
1026 }
1027
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001028 dprintf(INFO, "Loading (%s) image (%d): done\n",
1029 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1030
Matthew Qin49e51fa2015-02-09 10:40:45 +08001031 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1032
1033 /* Authenticate Kernel */
1034 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1035 (int) target_use_signed_kernel(),
1036 device.is_unlocked,
1037 device.is_tampered);
1038
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001039 /* Change the condition a little bit to include the test framework support.
1040 * We would never reach this point if device is in fastboot mode, even if we did
1041 * that means we are in test mode, so execute kernel authentication part for the
1042 * tests */
1043 if((target_use_signed_kernel() && (!device.is_unlocked)) || boot_into_fastboot)
Matthew Qin49e51fa2015-02-09 10:40:45 +08001044 {
1045 offset = imagesize_actual;
1046 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1047 {
1048 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1049 return -1;
1050 }
1051
1052 /* Read signature */
1053 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1054 {
1055 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1056 return -1;
1057 }
1058
1059 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001060 /* The purpose of our test is done here */
1061 if (boot_into_fastboot && auth_kernel_img)
1062 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001063 } else {
1064 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1065 #ifdef TZ_SAVE_KERNEL_HASH
1066 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1067 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001068
1069#ifdef MDTP_SUPPORT
1070 {
1071 /* Verify MDTP lock.
1072 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1073 * since verification was skipped in aboot. The signature is not part of the loaded image.
1074 */
1075 mdtp_ext_partition_verification_t ext_partition;
1076 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1077 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1078 ext_partition.page_size = page_size;
1079 ext_partition.image_addr = (uint32)image_addr;
1080 ext_partition.image_size = imagesize_actual;
1081 ext_partition.sig_avail = FALSE;
1082 mdtp_fwlock_verify_lock(&ext_partition);
1083 }
1084#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001085 }
1086
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001087 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001088 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1089 * If not, continue booting.
1090 */
1091 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1092 {
1093 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1094 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001095 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001096 rc = decompress((unsigned char *)(image_addr + page_size),
1097 hdr->kernel_size, out_addr, out_avai_len,
1098 &dtb_offset, &out_len);
1099 if (rc)
1100 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001101 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001102 ASSERT(0);
1103 }
1104
Matthew Qin0b15b322015-05-19 05:20:54 -04001105 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001106 kptr = (struct kernel64_hdr *)out_addr;
1107 kernel_start_addr = out_addr;
1108 kernel_size = out_len;
1109 } else {
1110 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1111 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1112 kernel_size = hdr->kernel_size;
1113 }
1114
1115 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001116 * Update the kernel/ramdisk/tags address if the boot image header
1117 * has default values, these default values come from mkbootimg when
1118 * the boot image is flashed using fastboot flash:raw
1119 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001120 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001121
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001122 /* Get virtual addresses since the hdr saves physical addresses. */
1123 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1124 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1125 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001126
Matthew Qinbb7923d2015-02-09 10:56:09 +08001127 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001128 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001129 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001130 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1131 {
1132 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1133 return -1;
1134 }
1135
1136#ifndef DEVICE_TREE
1137 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1138 {
1139 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1140 return -1;
1141 }
1142#endif
1143
Matthew Qin49e51fa2015-02-09 10:40:45 +08001144 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001145 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001146 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001147
Matthew Qin49e51fa2015-02-09 10:40:45 +08001148 #if DEVICE_TREE
1149 if(hdr->dt_size) {
1150 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1151 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001152
Matthew Qin49e51fa2015-02-09 10:40:45 +08001153 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1154 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1155 return -1;
1156 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001157
Matthew Qin49e51fa2015-02-09 10:40:45 +08001158 /* Find index of device tree within device tree table */
1159 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1160 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1161 return -1;
1162 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001163
Matthew Qin271927e2015-03-31 22:07:07 -04001164 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1165 {
1166 unsigned int compressed_size = 0;
1167 out_addr += out_len;
1168 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001169 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001170 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1171 dt_entry.size, out_addr, out_avai_len,
1172 &compressed_size, &dtb_size);
1173 if (rc)
1174 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001175 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001176 ASSERT(0);
1177 }
1178
Matthew Qin0b15b322015-05-19 05:20:54 -04001179 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001180 best_match_dt_addr = out_addr;
1181 } else {
1182 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1183 dtb_size = dt_entry.size;
1184 }
1185
Matthew Qin49e51fa2015-02-09 10:40:45 +08001186 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001187 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001188 {
1189 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1190 return -1;
1191 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001192
Matthew Qin271927e2015-03-31 22:07:07 -04001193 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001194 } else {
1195 /* Validate the tags_addr */
1196 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001197 {
1198 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1199 return -1;
1200 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001201 /*
1202 * If appended dev tree is found, update the atags with
1203 * memory address to the DTB appended location on RAM.
1204 * Else update with the atags address in the kernel header
1205 */
1206 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001207 dtb = dev_tree_appended((void*)(image_addr + page_size),
1208 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001209 (void *)hdr->tags_addr);
1210 if (!dtb) {
1211 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001212 return -1;
1213 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001214 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001215 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001216
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001217 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1218 target_load_ssd_keystore();
1219
Shashank Mittal23b8f422010-04-16 19:27:21 -07001220unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001221
Dima Zavin77e41f32013-03-06 16:10:43 -08001222 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001223 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001224 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1225
1226 return 0;
1227}
1228
Dima Zavin214cc642009-01-26 11:16:21 -08001229int boot_linux_from_flash(void)
1230{
1231 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001232 struct ptentry *ptn;
1233 struct ptable *ptable;
1234 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001235
Shashank Mittalcd98d472011-08-02 14:29:24 -07001236 unsigned char *image_addr = 0;
1237 unsigned kernel_actual;
1238 unsigned ramdisk_actual;
1239 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001240 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001241
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001242#if DEVICE_TREE
1243 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001244 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001245 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001246 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001247#endif
1248
David Ng183a7422009-12-07 14:55:21 -08001249 if (target_is_emmc_boot()) {
1250 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1251 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1252 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1253 return -1;
1254 }
1255 goto continue_boot;
1256 }
1257
Dima Zavin214cc642009-01-26 11:16:21 -08001258 ptable = flash_get_ptable();
1259 if (ptable == NULL) {
1260 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1261 return -1;
1262 }
1263
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001264 if(!boot_into_recovery)
1265 {
1266 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001267
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001268 if (ptn == NULL) {
1269 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1270 return -1;
1271 }
1272 }
1273 else
1274 {
1275 ptn = ptable_find(ptable, "recovery");
1276 if (ptn == NULL) {
1277 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1278 return -1;
1279 }
Dima Zavin214cc642009-01-26 11:16:21 -08001280 }
1281
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001282 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001283 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1284 return -1;
1285 }
Dima Zavin214cc642009-01-26 11:16:21 -08001286
1287 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001288 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001289 return -1;
1290 }
1291
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001292 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001293 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 -08001294 return -1;
1295 }
1296
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001297 /*
1298 * Update the kernel/ramdisk/tags address if the boot image header
1299 * has default values, these default values come from mkbootimg when
1300 * the boot image is flashed using fastboot flash:raw
1301 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001302 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001303
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001304 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001305 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1306 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1307 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1308
1309 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1310 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1311
1312 /* Check if the addresses in the header are valid. */
1313 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1314 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1315 {
1316 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1317 return -1;
1318 }
1319
1320#ifndef DEVICE_TREE
1321 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1322 {
1323 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1324 return -1;
1325 }
1326#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001327
Shashank Mittalcd98d472011-08-02 14:29:24 -07001328 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001329 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001330 {
1331 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001332 offset = 0;
1333
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001334#if DEVICE_TREE
1335 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1336 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001337
1338 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1339 {
1340 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1341 return -1;
1342 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001343#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001344 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001345#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001346
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001347 dprintf(INFO, "Loading (%s) image (%d): start\n",
1348 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001349 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001350
Shashank Mittalcd98d472011-08-02 14:29:24 -07001351 /* Read image without signature */
1352 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1353 {
1354 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1355 return -1;
1356 }
Dima Zavin214cc642009-01-26 11:16:21 -08001357
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001358 dprintf(INFO, "Loading (%s) image (%d): done\n",
1359 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001360 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001361
Shashank Mittalcd98d472011-08-02 14:29:24 -07001362 offset = imagesize_actual;
1363 /* Read signature */
1364 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1365 {
1366 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001367 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001368 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001369
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301370 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001371
1372 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001373 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1374 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001375#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001376 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001377 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001378 {
1379 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1380 return -1;
1381 }
1382
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001383 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1384#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001385
1386 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001387 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001388 {
1389 write_device_info_flash(&device);
1390 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301391#if USE_PCOM_SECBOOT
1392 set_tamper_flag(device.is_tampered);
1393#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001394 }
1395 else
1396 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001397 offset = page_size;
1398
Amol Jadib6be5c12012-11-14 13:39:51 -08001399 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1400 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1401 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1402
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001403 dprintf(INFO, "Loading (%s) image (%d): start\n",
1404 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1405
Amol Jadi492d5a52013-03-15 16:12:34 -07001406 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001407
1408 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001409 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1410 return -1;
1411 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001412 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001413
Amol Jadib6be5c12012-11-14 13:39:51 -08001414 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001415 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1416 return -1;
1417 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001418 offset += ramdisk_actual;
1419
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001420 dprintf(INFO, "Loading (%s) image (%d): done\n",
1421 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1422
Amol Jadi492d5a52013-03-15 16:12:34 -07001423 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001424
1425 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001426 offset += second_actual;
1427 /* Second image loading not implemented. */
1428 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001429 }
1430
1431#if DEVICE_TREE
1432 if(hdr->dt_size != 0) {
1433
1434 /* Read the device tree table into buffer */
1435 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1436 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1437 return -1;
1438 }
1439
1440 table = (struct dt_table*) dt_buf;
1441
Deepa Dinamani19648b42013-09-05 17:05:55 -07001442 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001443 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1444 return -1;
1445 }
1446
Deepa Dinamani19648b42013-09-05 17:05:55 -07001447 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1448 if (!table)
1449 return -1;
1450
1451 /* Read the entire device tree table into buffer */
1452 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1453 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1454 return -1;
1455 }
1456
1457
Joel Kingaa335dc2013-06-03 16:11:08 -07001458 /* Find index of device tree within device tree table */
1459 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001460 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1461 return -1;
1462 }
1463
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001464 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001465 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001466 {
1467 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1468 return -1;
1469 }
1470
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001471 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001472 if(flash_read(ptn, offset + dt_entry.offset,
1473 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001474 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1475 return -1;
1476 }
1477 }
1478#endif
1479
Shashank Mittalcd98d472011-08-02 14:29:24 -07001480 }
David Ng183a7422009-12-07 14:55:21 -08001481continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001482
Dima Zavin214cc642009-01-26 11:16:21 -08001483 /* TODO: create/pass atags to kernel */
1484
Ajay Dudanie28a6072011-07-01 13:59:46 -07001485 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001486 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001487 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1488
1489 return 0;
1490}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001491
Shashank Mittal162244e2011-08-08 19:01:25 -07001492void write_device_info_mmc(device_info *dev)
1493{
Shashank Mittal162244e2011-08-08 19:01:25 -07001494 unsigned long long ptn = 0;
1495 unsigned long long size;
1496 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001497 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001498 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001499 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001500
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001501 if (devinfo_present)
1502 index = partition_get_index("devinfo");
1503 else
1504 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001505
Shashank Mittal162244e2011-08-08 19:01:25 -07001506 ptn = partition_get_offset(index);
1507 if(ptn == 0)
1508 {
1509 return;
1510 }
1511
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001512 lun = partition_get_lun(index);
1513 mmc_set_lun(lun);
1514
Shashank Mittal162244e2011-08-08 19:01:25 -07001515 size = partition_get_size(index);
1516
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001517 blocksize = mmc_get_device_blocksize();
1518
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001519 if (devinfo_present)
1520 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1521 else
1522 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1523 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001524 {
1525 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001526 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001527 }
1528}
1529
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001530void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001531{
Shashank Mittal162244e2011-08-08 19:01:25 -07001532 unsigned long long ptn = 0;
1533 unsigned long long size;
1534 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001535 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001536 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001537
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001538 if ((index = partition_get_index("devinfo")) < 0)
1539 {
1540 devinfo_present = false;
1541 index = partition_get_index("aboot");
1542 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001543
Shashank Mittal162244e2011-08-08 19:01:25 -07001544 ptn = partition_get_offset(index);
1545 if(ptn == 0)
1546 {
1547 return;
1548 }
1549
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001550 mmc_set_lun(partition_get_lun(index));
1551
Shashank Mittal162244e2011-08-08 19:01:25 -07001552 size = partition_get_size(index);
1553
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001554 blocksize = mmc_get_device_blocksize();
1555
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001556 if (devinfo_present)
1557 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1558 else
1559 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1560 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001561 {
1562 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001563 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001564 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001565}
1566
1567void write_device_info_flash(device_info *dev)
1568{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001569 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001570 struct ptentry *ptn;
1571 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001572 if(info == NULL)
1573 {
1574 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1575 ASSERT(0);
1576 }
1577 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001578 ptable = flash_get_ptable();
1579 if (ptable == NULL)
1580 {
1581 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1582 return;
1583 }
1584
1585 ptn = ptable_find(ptable, "devinfo");
1586 if (ptn == NULL)
1587 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001588 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001589 return;
1590 }
1591
1592 memcpy(info, dev, sizeof(device_info));
1593
1594 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1595 {
1596 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1597 return;
1598 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001599 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001600}
1601
vijay kumarc65876c2015-04-24 13:29:16 +05301602static int read_allow_oem_unlock(device_info *dev)
1603{
vijay kumarc65876c2015-04-24 13:29:16 +05301604 unsigned offset;
1605 int index;
1606 unsigned long long ptn;
1607 unsigned long long ptn_size;
1608 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001609 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301610
vijay kumarca2e6812015-07-08 20:28:25 +05301611 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301612 if (index == INVALID_PTN)
1613 {
vijay kumarca2e6812015-07-08 20:28:25 +05301614 index = partition_get_index(frp_ptns[1]);
1615 if (index == INVALID_PTN)
1616 {
1617 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1618 return -1;
1619 }
vijay kumarc65876c2015-04-24 13:29:16 +05301620 }
1621
1622 ptn = partition_get_offset(index);
1623 ptn_size = partition_get_size(index);
1624 offset = ptn_size - blocksize;
1625
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001626 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301627 {
1628 dprintf(CRITICAL, "Reading MMC failed\n");
1629 return -1;
1630 }
1631
1632 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1633 is_allow_unlock = buf[blocksize-1] & 0x01;
1634 return 0;
1635}
1636
1637static int write_allow_oem_unlock(bool allow_unlock)
1638{
vijay kumarc65876c2015-04-24 13:29:16 +05301639 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301640 int index;
1641 unsigned long long ptn;
1642 unsigned long long ptn_size;
1643 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001644 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301645
vijay kumarca2e6812015-07-08 20:28:25 +05301646 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301647 if (index == INVALID_PTN)
1648 {
vijay kumarca2e6812015-07-08 20:28:25 +05301649 index = partition_get_index(frp_ptns[1]);
1650 if (index == INVALID_PTN)
1651 {
1652 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1653 return -1;
1654 }
vijay kumarc65876c2015-04-24 13:29:16 +05301655 }
1656
1657 ptn = partition_get_offset(index);
1658 ptn_size = partition_get_size(index);
1659 offset = ptn_size - blocksize;
1660
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001661 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301662 {
1663 dprintf(CRITICAL, "Reading MMC failed\n");
1664 return -1;
1665 }
1666
1667 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1668 buf[blocksize-1] = allow_unlock;
1669 if (mmc_write(ptn + offset, blocksize, buf))
1670 {
1671 dprintf(CRITICAL, "Writing MMC failed\n");
1672 return -1;
1673 }
1674
1675 return 0;
1676}
1677
Shashank Mittal162244e2011-08-08 19:01:25 -07001678void read_device_info_flash(device_info *dev)
1679{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001680 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001681 struct ptentry *ptn;
1682 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001683 if(info == NULL)
1684 {
1685 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1686 ASSERT(0);
1687 }
1688 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001689 ptable = flash_get_ptable();
1690 if (ptable == NULL)
1691 {
1692 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1693 return;
1694 }
1695
1696 ptn = ptable_find(ptable, "devinfo");
1697 if (ptn == NULL)
1698 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001699 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001700 return;
1701 }
1702
1703 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1704 {
1705 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1706 return;
1707 }
1708
1709 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1710 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001711 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1712 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001713 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001714 write_device_info_flash(info);
1715 }
1716 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001717 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001718}
1719
1720void write_device_info(device_info *dev)
1721{
1722 if(target_is_emmc_boot())
1723 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001724 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1725 if(info == NULL)
1726 {
1727 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1728 ASSERT(0);
1729 }
1730 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001731 memcpy(info, dev, sizeof(struct device_info));
1732
1733#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001734 if (is_secure_boot_enable()) {
1735 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1736 ASSERT(0);
1737 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001738 else
1739 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001740#else
1741 write_device_info_mmc(info);
1742#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001743 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001744 }
1745 else
1746 {
1747 write_device_info_flash(dev);
1748 }
1749}
1750
1751void read_device_info(device_info *dev)
1752{
1753 if(target_is_emmc_boot())
1754 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001755 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1756 if(info == NULL)
1757 {
1758 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1759 ASSERT(0);
1760 }
1761 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001762
1763#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001764 if (is_secure_boot_enable()) {
1765 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1766 ASSERT(0);
1767 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001768 else
1769 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001770#else
1771 read_device_info_mmc(info);
1772#endif
1773
1774 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1775 {
1776 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001777 if (is_secure_boot_enable())
1778 info->is_unlocked = 0;
1779 else
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001780 info->is_unlocked = 1;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001781 info->is_tampered = 0;
1782 info->charger_screen_enabled = 0;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001783 write_device_info(info);
1784 }
1785 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001786 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001787 }
1788 else
1789 {
1790 read_device_info_flash(dev);
1791 }
1792}
1793
1794void reset_device_info()
1795{
1796 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001797 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001798 write_device_info(&device);
1799}
1800
1801void set_device_root()
1802{
1803 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001804 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001805 write_device_info(&device);
1806}
1807
Amol Jadicb524072012-08-09 16:40:18 -07001808#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001809int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
Amol Jadicb524072012-08-09 16:40:18 -07001810{
1811 uint32 dt_image_offset = 0;
1812 uint32_t n;
1813 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001814 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001815 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001816 unsigned int compressed_size = 0;
1817 unsigned int dtb_size = 0;
1818 unsigned int out_avai_len = 0;
1819 unsigned char *out_addr = NULL;
1820 unsigned char *best_match_dt_addr = NULL;
1821 int rc;
Amol Jadicb524072012-08-09 16:40:18 -07001822
1823 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1824
Amol Jadicb524072012-08-09 16:40:18 -07001825 if(hdr->dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001826 /* add kernel offset */
1827 dt_image_offset += page_size;
1828 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1829 dt_image_offset += n;
1830
1831 /* add ramdisk offset */
1832 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1833 dt_image_offset += n;
1834
1835 /* add second offset */
1836 if(hdr->second_size != 0) {
1837 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1838 dt_image_offset += n;
1839 }
1840
1841 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001842 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001843
Deepa Dinamani19648b42013-09-05 17:05:55 -07001844 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001845 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1846 return -1;
1847 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001848 /* Find index of device tree within device tree table */
1849 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001850 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1851 return -1;
1852 }
1853
Matthew Qin271927e2015-03-31 22:07:07 -04001854 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
1855 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
1856 {
1857 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
1858 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04001859 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001860 rc = decompress(best_match_dt_addr,
1861 dt_entry.size, out_addr, out_avai_len,
1862 &compressed_size, &dtb_size);
1863 if (rc)
1864 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001865 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001866 ASSERT(0);
1867 }
1868
Matthew Qin0b15b322015-05-19 05:20:54 -04001869 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001870 best_match_dt_addr = out_addr;
1871 } else {
1872 dtb_size = dt_entry.size;
1873 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001874 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001875 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001876 {
1877 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1878 return -1;
1879 }
1880
Amol Jadicb524072012-08-09 16:40:18 -07001881 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001882 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001883 } else
1884 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001885
1886 /* Everything looks fine. Return success. */
1887 return 0;
1888}
1889#endif
1890
Brian Swetland9c4c0752009-01-25 16:23:50 -08001891void cmd_boot(const char *arg, void *data, unsigned sz)
1892{
Amit Blayba53e6e2015-08-17 09:20:01 +03001893#ifdef MDTP_SUPPORT
1894 static bool is_mdtp_activated = 0;
1895#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08001896 unsigned kernel_actual;
1897 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001898 uint32_t image_actual;
1899 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001900 uint32_t sig_actual = SIGNATURE_SIZE;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001901 struct boot_img_hdr *hdr = NULL;
1902 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001903 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001904 int ret = 0;
1905 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001906 unsigned int out_len = 0;
1907 unsigned int out_avai_len = 0;
1908 unsigned char *out_addr = NULL;
1909 uint32_t dtb_offset = 0;
1910 unsigned char *kernel_start_addr = NULL;
1911 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04001912 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001913
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001914#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07001915 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001916 {
1917 fastboot_fail("unlock device to use this command");
1918 return;
1919 }
1920#endif
1921
Brian Swetland9c4c0752009-01-25 16:23:50 -08001922 if (sz < sizeof(hdr)) {
1923 fastboot_fail("invalid bootimage header");
1924 return;
1925 }
1926
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001927 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001928
1929 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001930 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001931
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001932 if(target_is_emmc_boot() && hdr->page_size) {
1933 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001934 page_mask = page_size - 1;
1935 }
1936
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001937 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1938 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001939#if DEVICE_TREE
1940 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1941#endif
1942
1943 image_actual = ADD_OF(page_size, kernel_actual);
1944 image_actual = ADD_OF(image_actual, ramdisk_actual);
1945 image_actual = ADD_OF(image_actual, dt_actual);
1946
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001947 if (target_use_signed_kernel() && (!device.is_unlocked))
1948 image_actual = ADD_OF(image_actual, sig_actual);
1949
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001950 /* sz should have atleast raw boot image */
1951 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001952 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001953 return;
1954 }
1955
1956 /* Verify the boot image
1957 * device & page_size are initialized in aboot_init
1958 */
1959 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001960 /* Pass size excluding signature size, otherwise we would try to
1961 * access signature beyond its length
1962 */
1963 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001964
Amit Blay4aa292f2015-04-28 21:55:59 +03001965#ifdef MDTP_SUPPORT
1966 else
1967 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03001968 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03001969 mdtp_ext_partition_verification_t ext_partition;
Amit Blayba53e6e2015-08-17 09:20:01 +03001970
1971 if (!is_mdtp_activated) {
1972 ext_partition.partition = MDTP_PARTITION_NONE;
1973 mdtp_fwlock_verify_lock(&ext_partition);
1974 }
Amit Blay4aa292f2015-04-28 21:55:59 +03001975 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03001976
Shay Nachmani062aa3f2015-05-17 17:28:44 +03001977 mdtp_activated(&is_mdtp_activated);
1978 if(is_mdtp_activated){
1979 dprintf(CRITICAL, "fastboot boot command is not available.\n");
1980 return;
1981 }
Amit Blay4aa292f2015-04-28 21:55:59 +03001982#endif /* MDTP_SUPPORT */
1983
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001984 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001985 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1986 * If not, continue booting.
1987 */
1988 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
1989 {
1990 out_addr = (unsigned char *)target_get_scratch_address();
1991 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
1992 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001993 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001994 ret = decompress((unsigned char *)(ptr + page_size),
1995 hdr->kernel_size, out_addr, out_avai_len,
1996 &dtb_offset, &out_len);
1997 if (ret)
1998 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001999 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002000 ASSERT(0);
2001 }
2002
Matthew Qin0b15b322015-05-19 05:20:54 -04002003 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002004 kptr = (struct kernel64_hdr *)out_addr;
2005 kernel_start_addr = out_addr;
2006 kernel_size = out_len;
2007 } else {
2008 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2009 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2010 kernel_size = hdr->kernel_size;
2011 }
2012
2013 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002014 * Update the kernel/ramdisk/tags address if the boot image header
2015 * has default values, these default values come from mkbootimg when
2016 * the boot image is flashed using fastboot flash:raw
2017 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002018 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002019
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002020 /* Get virtual addresses since the hdr saves physical addresses. */
2021 hdr->kernel_addr = VA(hdr->kernel_addr);
2022 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2023 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002024
Matthew Qinbb7923d2015-02-09 10:56:09 +08002025 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002026 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002027 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002028 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2029 {
2030 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002031 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002032 }
2033
Amol Jadicb524072012-08-09 16:40:18 -07002034#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002035 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002036 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002037 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002038
2039 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002040#else
2041 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2042 {
2043 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002044 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002045 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002046#endif
2047
2048 /* Load ramdisk & kernel */
2049 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002050 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002051
2052#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002053 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2054 {
2055 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2056 return;
2057 }
2058
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002059 /*
2060 * If dtb is not found look for appended DTB in the kernel.
2061 * If appended dev tree is found, update the atags with
2062 * memory address to the DTB appended location on RAM.
2063 * Else update with the atags address in the kernel header
2064 */
2065 if (!dtb_copied) {
2066 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002067 dtb = dev_tree_appended((void*)(ptr + page_size),
2068 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002069 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002070 if (!dtb) {
2071 fastboot_fail("dtb not found");
2072 return;
2073 }
Amol Jadicb524072012-08-09 16:40:18 -07002074 }
2075#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002076
2077 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002078 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002079
Dima Zavin77e41f32013-03-06 16:10:43 -08002080 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002081 (const char*) hdr->cmdline, board_machtype(),
2082 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002083}
2084
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002085void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002086{
2087 struct ptentry *ptn;
2088 struct ptable *ptable;
2089
2090 ptable = flash_get_ptable();
2091 if (ptable == NULL) {
2092 fastboot_fail("partition table doesn't exist");
2093 return;
2094 }
2095
2096 ptn = ptable_find(ptable, arg);
2097 if (ptn == NULL) {
2098 fastboot_fail("unknown partition name");
2099 return;
2100 }
2101
2102 if (flash_erase(ptn)) {
2103 fastboot_fail("failed to erase partition");
2104 return;
2105 }
2106 fastboot_okay("");
2107}
2108
Bikas Gurungd48bd242010-09-04 19:54:32 -07002109
2110void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2111{
2112 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002113 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002114 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002115 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002116
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002117#if VERIFIED_BOOT
2118 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2119 {
2120 if(!device.is_unlocked)
2121 {
2122 fastboot_fail("unlock device to erase keystore");
2123 return;
2124 }
2125 }
2126#endif
2127
Kinson Chikf1a43512011-07-14 11:28:39 -07002128 index = partition_get_index(arg);
2129 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002130 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002131
Kinson Chikf1a43512011-07-14 11:28:39 -07002132 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002133 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002134 return;
2135 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002136
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002137 lun = partition_get_lun(index);
2138 mmc_set_lun(lun);
2139
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002140 if (platform_boot_dev_isemmc())
2141 {
2142 if (mmc_erase_card(ptn, size)) {
2143 fastboot_fail("failed to erase partition\n");
2144 return;
2145 }
2146 } else {
2147 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2148 size = partition_get_size(index);
2149 if (size > DEFAULT_ERASE_SIZE)
2150 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002151
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002152 /* Simple inefficient version of erase. Just writing
2153 0 in first several blocks */
2154 if (mmc_write(ptn , size, (unsigned int *)out)) {
2155 fastboot_fail("failed to erase partition");
2156 return;
2157 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002158 }
2159 fastboot_okay("");
2160}
2161
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002162void cmd_erase(const char *arg, void *data, unsigned sz)
2163{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002164#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002165 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002166 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002167 if(!device.is_unlocked && !device.is_verified)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002168 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002169 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002170 return;
2171 }
Channagoud Kadabi35297672015-06-13 11:09:49 -07002172 if(!device.is_unlocked && device.is_verified)
2173 {
2174 if(!boot_verify_flash_allowed(arg))
2175 {
2176 fastboot_fail("cannot flash this partition in verified state");
2177 return;
2178 }
2179 }
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002180 }
2181#endif
2182
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002183 if(target_is_emmc_boot())
2184 cmd_erase_mmc(arg, data, sz);
2185 else
2186 cmd_erase_nand(arg, data, sz);
2187}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002188
Channagoud Kadabi35227322015-05-29 11:14:17 -07002189static uint32_t aboot_get_secret_key()
2190{
2191 /* 0 is invalid secret key, update this implementation to return
2192 * device specific unique secret key
2193 */
2194 return 0;
2195}
2196
Ajay Dudani5c761132011-04-07 20:19:04 -07002197void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002198{
2199 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002200 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002201 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002202 char *token = NULL;
2203 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002204 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002205 uint8_t lun = 0;
2206 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002207
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002208 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002209 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002210 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002211 if(token)
2212 {
2213 lun = atoi(token);
2214 mmc_set_lun(lun);
2215 lun_set = true;
2216 }
2217
Mao Jinlong226f33a2014-07-04 17:24:10 +08002218 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002219 {
Channagoud Kadabi35227322015-05-29 11:14:17 -07002220 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2221 {
2222 if (!aboot_frp_unlock(pname, data, sz))
2223 {
2224 fastboot_info("FRP unlock successful");
2225 fastboot_okay("");
2226 }
2227 else
2228 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2229
2230 return;
2231 }
2232
Mao Jinlong226f33a2014-07-04 17:24:10 +08002233 if (!strcmp(pname, "partition"))
2234 {
2235 dprintf(INFO, "Attempt to write partition image.\n");
2236 if (write_partition(sz, (unsigned char *) data)) {
2237 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002238 return;
2239 }
2240 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002241 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002242 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002243#if VERIFIED_BOOT
2244 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2245 {
2246 if(!device.is_unlocked)
2247 {
2248 fastboot_fail("unlock device to flash keystore");
2249 return;
2250 }
2251 if(!boot_verify_validate_keystore((unsigned char *)data))
2252 {
2253 fastboot_fail("image is not a keystore file");
2254 return;
2255 }
2256 }
2257#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002258 index = partition_get_index(pname);
2259 ptn = partition_get_offset(index);
2260 if(ptn == 0) {
2261 fastboot_fail("partition table doesn't exist");
2262 return;
2263 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002264
Mao Jinlong226f33a2014-07-04 17:24:10 +08002265 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2266 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2267 fastboot_fail("image is not a boot image");
2268 return;
2269 }
2270 }
2271
2272 if(!lun_set)
2273 {
2274 lun = partition_get_lun(index);
2275 mmc_set_lun(lun);
2276 }
2277
2278 size = partition_get_size(index);
2279 if (ROUND_TO_PAGE(sz,511) > size) {
2280 fastboot_fail("size too large");
2281 return;
2282 }
2283 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2284 fastboot_fail("flash write failure");
2285 return;
2286 }
Greg Grisco6e754772011-06-23 12:19:39 -07002287 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002288 }
2289 fastboot_okay("");
2290 return;
2291}
2292
Ajay Dudanide984792015-03-02 09:57:41 -08002293void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2294{
2295 int i, images;
2296 meta_header_t *meta_header;
2297 img_header_entry_t *img_header_entry;
2298
2299 meta_header = (meta_header_t*) data;
2300 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2301
2302 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2303
2304 for (i=0; i<images; i++) {
2305
2306 if((img_header_entry[i].ptn_name == NULL) ||
2307 (img_header_entry[i].start_offset == 0) ||
2308 (img_header_entry[i].size == 0))
2309 break;
2310
2311 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2312 (void *) data + img_header_entry[i].start_offset,
2313 img_header_entry[i].size);
2314 }
2315
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002316 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2317 {
2318 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2319 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2320 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2321 }
2322 else
2323 {
2324 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2325 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2326 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2327 }
2328
2329 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002330 fastboot_okay("");
2331 return;
2332}
2333
Ajay Dudani5c761132011-04-07 20:19:04 -07002334void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2335{
2336 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002337 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002338 uint32_t *fill_buf = NULL;
2339 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002340 sparse_header_t *sparse_header;
2341 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002342 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002343 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302344 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002345 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302346 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002347 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302348 /*End of the sparse image address*/
2349 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002350
Kinson Chikf1a43512011-07-14 11:28:39 -07002351 index = partition_get_index(arg);
2352 ptn = partition_get_offset(index);
2353 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002354 fastboot_fail("partition table doesn't exist");
2355 return;
2356 }
2357
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302358 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302359
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002360 lun = partition_get_lun(index);
2361 mmc_set_lun(lun);
2362
vijay kumar800255e2015-04-24 20:26:19 +05302363 if (sz < sizeof(sparse_header_t)) {
2364 fastboot_fail("size too low");
2365 return;
2366 }
2367
Ajay Dudani5c761132011-04-07 20:19:04 -07002368 /* Read and skip over sparse image header */
2369 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302370
vijay kumar1321f342015-03-27 12:13:42 +05302371 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002372 fastboot_fail("size too large");
2373 return;
2374 }
2375
vijay kumarde4fcf62015-04-23 13:05:49 +05302376 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302377
2378 if (data_end < (uint32_t)data) {
2379 fastboot_fail("buffer overreads occured due to invalid sparse header");
2380 return;
2381 }
2382
vijay kumarde4fcf62015-04-23 13:05:49 +05302383 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002384 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302385 fastboot_fail("sparse header size mismatch");
2386 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002387 }
2388
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002389 dprintf (SPEW, "=== Sparse Image Header ===\n");
2390 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2391 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2392 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2393 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2394 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2395 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2396 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2397 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002398
2399 /* Start processing chunks */
2400 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2401 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302402 /* Make sure the total image size does not exceed the partition size */
2403 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2404 fastboot_fail("size too large");
2405 return;
2406 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002407 /* Read and skip over chunk header */
2408 chunk_header = (chunk_header_t *) data;
2409 data += sizeof(chunk_header_t);
2410
vijay kumar800255e2015-04-24 20:26:19 +05302411 if (data_end < (uint32_t)data) {
2412 fastboot_fail("buffer overreads occured due to invalid sparse header");
2413 return;
2414 }
2415
Ajay Dudani5c761132011-04-07 20:19:04 -07002416 dprintf (SPEW, "=== Chunk Header ===\n");
2417 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2418 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2419 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2420
vijay kumar800255e2015-04-24 20:26:19 +05302421 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002422 {
vijay kumar800255e2015-04-24 20:26:19 +05302423 fastboot_fail("chunk header size mismatch");
2424 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002425 }
2426
wufeng.jiang813dc352015-06-02 23:02:46 -04002427 if (!sparse_header->blk_sz ){
2428 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302429 return;
2430 }
2431
wufeng.jiang813dc352015-06-02 23:02:46 -04002432 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2433
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302434 /* Make sure that the chunk size calculated from sparse image does not
2435 * exceed partition size
2436 */
2437 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2438 {
2439 fastboot_fail("Chunk data size exceeds partition size");
2440 return;
2441 }
2442
Ajay Dudani5c761132011-04-07 20:19:04 -07002443 switch (chunk_header->chunk_type)
2444 {
2445 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002446 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002447 chunk_data_sz))
2448 {
2449 fastboot_fail("Bogus chunk size for chunk type Raw");
2450 return;
2451 }
2452
vijay kumar800255e2015-04-24 20:26:19 +05302453 if (data_end < (uint32_t)data + chunk_data_sz) {
2454 fastboot_fail("buffer overreads occured due to invalid sparse header");
2455 return;
2456 }
2457
wufeng.jiang813dc352015-06-02 23:02:46 -04002458 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2459 otherwise it will return in the line above
2460 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002461 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002462 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002463 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002464 {
2465 fastboot_fail("flash write failure");
2466 return;
2467 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302468 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2469 fastboot_fail("Bogus size for RAW chunk type");
2470 return;
2471 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002472 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002473 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002474 break;
2475
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002476 case CHUNK_TYPE_FILL:
2477 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2478 sizeof(uint32_t)))
2479 {
2480 fastboot_fail("Bogus chunk size for chunk type FILL");
2481 return;
2482 }
2483
2484 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2485 if (!fill_buf)
2486 {
2487 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2488 return;
2489 }
2490
vijay kumar800255e2015-04-24 20:26:19 +05302491 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2492 fastboot_fail("buffer overreads occured due to invalid sparse header");
2493 return;
2494 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002495 fill_val = *(uint32_t *)data;
2496 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002497
2498 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2499 {
2500 fill_buf[i] = fill_val;
2501 }
2502
wufeng.jiang813dc352015-06-02 23:02:46 -04002503 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002504 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302505 /* Make sure that the data written to partition does not exceed partition size */
2506 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2507 {
2508 fastboot_fail("Chunk data size for fill type exceeds partition size");
2509 return;
2510 }
2511
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002512 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2513 sparse_header->blk_sz,
2514 fill_buf))
2515 {
2516 fastboot_fail("flash write failure");
2517 free(fill_buf);
2518 return;
2519 }
2520
2521 total_blocks++;
2522 }
2523
2524 free(fill_buf);
2525 break;
2526
Ajay Dudani5c761132011-04-07 20:19:04 -07002527 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302528 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2529 fastboot_fail("bogus size for chunk DONT CARE type");
2530 return;
2531 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002532 total_blocks += chunk_header->chunk_sz;
2533 break;
2534
Ajay Dudani5c761132011-04-07 20:19:04 -07002535 case CHUNK_TYPE_CRC:
2536 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2537 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002538 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002539 return;
2540 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302541 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2542 fastboot_fail("bogus size for chunk CRC type");
2543 return;
2544 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002545 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302546 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2547 fastboot_fail("integer overflow occured");
2548 return;
2549 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002550 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302551 if (data_end < (uint32_t)data) {
2552 fastboot_fail("buffer overreads occured due to invalid sparse header");
2553 return;
2554 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002555 break;
2556
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002557 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002558 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002559 fastboot_fail("Unknown chunk type");
2560 return;
2561 }
2562 }
2563
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002564 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2565 total_blocks, sparse_header->total_blks);
2566
2567 if(total_blocks != sparse_header->total_blks)
2568 {
2569 fastboot_fail("sparse image write failure");
2570 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002571
2572 fastboot_okay("");
2573 return;
2574}
2575
2576void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2577{
2578 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002579 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002580
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002581#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002582 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2583 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002584 int ret=0;
2585 uint32 major_version=0;
2586 uint32 minor_version=0;
2587
2588 ret = scm_svc_version(&major_version,&minor_version);
2589 if(!ret)
2590 {
2591 if(major_version >= 2)
2592 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002593 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002594 {
2595 ret = encrypt_scm((uint32 **) &data, &sz);
2596 if (ret != 0) {
2597 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2598 return;
2599 }
2600
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002601 /* Protect only for SSD */
2602 if (!strcmp(arg, "ssd")) {
2603 ret = scm_protect_keystore((uint32 *) data, sz);
2604 if (ret != 0) {
2605 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2606 return;
2607 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002608 }
2609 }
2610 else
2611 {
2612 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2613 if(ret != 0)
2614 {
2615 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2616 return;
2617 }
2618 }
2619 }
2620 else
2621 {
2622 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2623 magic_number[1] == DECRYPT_MAGIC_1)
2624 {
2625 ret = decrypt_scm((uint32 **) &data, &sz);
2626 if (ret != 0) {
2627 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2628 return;
2629 }
2630 }
2631 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2632 magic_number[1] == ENCRYPT_MAGIC_1)
2633 {
2634 ret = encrypt_scm((uint32 **) &data, &sz);
2635 if (ret != 0) {
2636 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2637 return;
2638 }
2639 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002640 }
2641 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002642 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002643 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002644 dprintf(CRITICAL,"INVALID SVC Version\n");
2645 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002646 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002647#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002648
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002649#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002650 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002651 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002652 if(!device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002653 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002654 fastboot_fail("device is locked. Cannot flash images");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002655 return;
2656 }
Channagoud Kadabi35297672015-06-13 11:09:49 -07002657 if(!device.is_unlocked && device.is_verified)
2658 {
2659 if(!boot_verify_flash_allowed(arg))
2660 {
2661 fastboot_fail("cannot flash this partition in verified state");
2662 return;
2663 }
2664 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002665 }
2666#endif
2667
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002668 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002669 meta_header = (meta_header_t *) data;
2670 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002671 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002672 else if (meta_header->magic == META_HEADER_MAGIC)
2673 cmd_flash_meta_img(arg, data, sz);
2674 else
2675 cmd_flash_mmc_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002676 return;
2677}
2678
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002679void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2680{
2681 struct ptentry *sys_ptn;
2682 struct ptable *ptable;
2683
2684 ptable = flash_get_ptable();
2685 if (ptable == NULL) {
2686 fastboot_fail("partition table doesn't exist");
2687 return;
2688 }
2689
2690 sys_ptn = ptable_find(ptable, "system");
2691 if (sys_ptn == NULL) {
2692 fastboot_fail("system partition not found");
2693 return;
2694 }
2695
2696 sz = ROUND_TO_PAGE(sz, page_mask);
2697 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2698 fastboot_fail("update_ubi_vol failed");
2699 else
2700 fastboot_okay("");
2701}
2702
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002703void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002704{
2705 struct ptentry *ptn;
2706 struct ptable *ptable;
2707 unsigned extra = 0;
2708
2709 ptable = flash_get_ptable();
2710 if (ptable == NULL) {
2711 fastboot_fail("partition table doesn't exist");
2712 return;
2713 }
2714
2715 ptn = ptable_find(ptable, arg);
2716 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002717 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2718 arg);
2719 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002720 return;
2721 }
2722
2723 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2724 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2725 fastboot_fail("image is not a boot image");
2726 return;
2727 }
2728 }
2729
Amol Jadi5c61a952012-05-04 17:05:35 -07002730 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002731 || !strcmp(ptn->name, "userdata")
2732 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002733 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002734 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002735 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002736 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002737 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002738
2739 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002740 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2741 if (flash_ubi_img(ptn, data, sz)) {
2742 fastboot_fail("flash write failure");
2743 return;
2744 }
2745 } else {
2746 if (flash_write(ptn, extra, data, sz)) {
2747 fastboot_fail("flash write failure");
2748 return;
2749 }
Dima Zavin214cc642009-01-26 11:16:21 -08002750 }
2751 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2752 fastboot_okay("");
2753}
2754
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002755void cmd_flash(const char *arg, void *data, unsigned sz)
2756{
2757 if(target_is_emmc_boot())
2758 cmd_flash_mmc(arg, data, sz);
2759 else
2760 cmd_flash_nand(arg, data, sz);
2761}
2762
Dima Zavin214cc642009-01-26 11:16:21 -08002763void cmd_continue(const char *arg, void *data, unsigned sz)
2764{
2765 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002766 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002767
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002768 if (target_is_emmc_boot())
2769 {
2770 boot_linux_from_mmc();
2771 }
2772 else
2773 {
2774 boot_linux_from_flash();
2775 }
Dima Zavin214cc642009-01-26 11:16:21 -08002776}
2777
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002778void cmd_reboot(const char *arg, void *data, unsigned sz)
2779{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002780 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002781 fastboot_okay("");
2782 reboot_device(0);
2783}
2784
2785void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2786{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002787 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002788 fastboot_okay("");
2789 reboot_device(FASTBOOT_MODE);
2790}
2791
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002792void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2793{
2794 dprintf(INFO, "Enabling charger screen check\n");
2795 device.charger_screen_enabled = 1;
2796 write_device_info(&device);
2797 fastboot_okay("");
2798}
2799
2800void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2801{
2802 dprintf(INFO, "Disabling charger screen check\n");
2803 device.charger_screen_enabled = 0;
2804 write_device_info(&device);
2805 fastboot_okay("");
2806}
2807
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302808void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2809{
2810 dprintf(INFO, "Selecting display panel %s\n", arg);
2811 if (arg)
2812 strlcpy(device.display_panel, arg,
2813 sizeof(device.display_panel));
2814 write_device_info(&device);
2815 fastboot_okay("");
2816}
2817
Shashank Mittal162244e2011-08-08 19:01:25 -07002818void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2819{
vijay kumarc65876c2015-04-24 13:29:16 +05302820 if(!is_allow_unlock) {
2821 fastboot_fail("oem unlock is not allowed");
2822 return;
2823 }
2824
2825 display_fbcon_message("Oem Unlock requested");
2826 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2827}
2828
2829void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
2830{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002831 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002832 {
vijay kumarc65876c2015-04-24 13:29:16 +05302833 if(!is_allow_unlock) {
2834 fastboot_fail("oem unlock is not allowed");
2835 return;
2836 }
2837
Shashank Mittal162244e2011-08-08 19:01:25 -07002838 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002839 device.is_verified = 0;
2840 write_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05302841
2842 struct recovery_message msg;
2843 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2844 write_misc(0, &msg, sizeof(msg));
2845
2846 fastboot_okay("");
2847 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002848 }
2849 fastboot_okay("");
2850}
2851
Channagoud Kadabi35227322015-05-29 11:14:17 -07002852static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
2853{
2854 int ret = 1;
2855 uint32_t secret_key;
2856 char seckey_buffer[MAX_RSP_SIZE];
2857
2858 secret_key = aboot_get_secret_key();
2859 if (secret_key)
2860 {
2861 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
2862 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
2863 {
2864 is_allow_unlock = true;
2865 write_allow_oem_unlock(is_allow_unlock);
2866 ret = 0;
2867 }
2868 }
2869 return ret;
2870}
2871
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002872void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2873{
2874 /* TODO: Wipe user data */
2875 if(device.is_unlocked || device.is_verified)
2876 {
2877 device.is_unlocked = 0;
2878 device.is_verified = 0;
2879 write_device_info(&device);
2880 }
2881 fastboot_okay("");
2882}
2883
2884void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2885{
2886 /* TODO: Wipe user data */
2887 if(device.is_unlocked || !device.is_verified)
2888 {
2889 device.is_unlocked = 0;
2890 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002891 write_device_info(&device);
2892 }
2893 fastboot_okay("");
2894}
2895
Shashank Mittala0032282011-08-26 14:50:11 -07002896void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2897{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302898 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002899 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002900 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002901 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2902 fastboot_info(response);
2903 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002904 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302905 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2906 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002907 fastboot_okay("");
2908}
2909
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002910void cmd_preflash(const char *arg, void *data, unsigned sz)
2911{
2912 fastboot_okay("");
2913}
2914
Mao Flynn7b379f32015-04-20 00:28:30 +08002915static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302916
Mao Flynn7b379f32015-04-20 00:28:30 +08002917int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302918{
Mao Flynn7b379f32015-04-20 00:28:30 +08002919 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302920 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08002921 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302922 return -1;
2923 return 0;
2924}
2925
Mao Flynn7b379f32015-04-20 00:28:30 +08002926int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002927{
2928 struct ptentry *ptn;
2929 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08002930 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002931 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002932
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302933 ptable = flash_get_ptable();
2934 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002935 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2936 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302937 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002938
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302939 ptn = ptable_find(ptable, "splash");
2940 if (ptn == NULL) {
2941 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002942 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302943 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002944 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302945 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002946 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302947 }
2948
Mao Flynn7b379f32015-04-20 00:28:30 +08002949 header = (struct logo_img_header *)logo_header;
2950 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302951 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002952 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302953 }
2954
2955 fb_display = fbcon_display();
2956 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002957 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
2958 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
2959
2960 /* if the logo is full-screen size, remove "fbcon_clear()" */
2961 if ((header->width != fb_display->width)
2962 || (header->height != fb_display->height))
2963 fbcon_clear();
2964
2965 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
2966 (uint32_t *)base,
2967 (header->blocks * 512))) {
2968 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
2969 return -1;
2970 }
2971 fbcon_extract_to_screen(header, base);
2972 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002973 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002974
2975 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
2976 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
2977 return -1;
2978 }
2979
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302980 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08002981 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
2982 (uint32_t *)base,
2983 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302984 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302985 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002986 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002987 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302988 }
2989
Mao Flynn7b379f32015-04-20 00:28:30 +08002990 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302991}
2992
Mao Flynn7b379f32015-04-20 00:28:30 +08002993int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302994{
2995 int index = INVALID_PTN;
2996 unsigned long long ptn = 0;
2997 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08002998 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08002999 uint32_t blocksize, realsize, readsize;
3000 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303001
3002 index = partition_get_index("splash");
3003 if (index == 0) {
3004 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003005 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303006 }
3007
3008 ptn = partition_get_offset(index);
3009 if (ptn == 0) {
3010 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003011 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303012 }
3013
Mao Flynn1893a7f2015-06-03 12:03:36 +08003014 mmc_set_lun(partition_get_lun(index));
3015
3016 blocksize = mmc_get_device_blocksize();
3017 if (blocksize == 0) {
3018 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3019 return -1;
3020 }
3021
3022 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003023 if (!fb_display)
3024 {
3025 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3026 return -1;
3027 }
3028
Mao Flynn1893a7f2015-06-03 12:03:36 +08003029 base = (uint8_t *) fb_display->base;
3030
3031 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303032 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003033 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303034 }
3035
Mao Flynn1893a7f2015-06-03 12:03:36 +08003036 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003037 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303038 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003039 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303040 }
3041
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303042 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003043 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3044 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003045
Mao Flynn1893a7f2015-06-03 12:03:36 +08003046 realsize = header->blocks * 512;
3047 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3048
3049 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003050 if ((header->width != fb_display->width)
3051 || (header->height != fb_display->height))
3052 fbcon_clear();
3053
Mao Flynn1893a7f2015-06-03 12:03:36 +08003054 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003055 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3056 return -1;
3057 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003058
Mao Flynn1893a7f2015-06-03 12:03:36 +08003059 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3060 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303061
Mao Flynn1893a7f2015-06-03 12:03:36 +08003062 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3063 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3064 return -1;
3065 }
3066
3067 realsize = header->width * header->height * fb_display->bpp / 8;
3068 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3069
3070 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3071 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3072 fbcon_clear();
3073 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3074 return -1;
3075 }
3076 } else {
3077 if (mmc_read(ptn + blocksize ,
3078 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3079 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3080 return -1;
3081 }
3082 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3083 }
3084 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303085 }
3086
Mao Flynn7b379f32015-04-20 00:28:30 +08003087 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303088}
3089
Mao Flynn7b379f32015-04-20 00:28:30 +08003090int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303091{
3092 if (target_is_emmc_boot()) {
3093 return splash_screen_mmc();
3094 } else {
3095 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003096 }
3097}
3098
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003099/* Get the size from partiton name */
3100static void get_partition_size(const char *arg, char *response)
3101{
3102 uint64_t ptn = 0;
3103 uint64_t size;
3104 int index = INVALID_PTN;
3105
3106 index = partition_get_index(arg);
3107
3108 if (index == INVALID_PTN)
3109 {
3110 dprintf(CRITICAL, "Invalid partition index\n");
3111 return;
3112 }
3113
3114 ptn = partition_get_offset(index);
3115
3116 if(!ptn)
3117 {
3118 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3119 return;
3120 }
3121
3122 size = partition_get_size(index);
3123
3124 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3125 return;
3126}
3127
3128/*
3129 * Publish the partition type & size info
3130 * fastboot getvar will publish the required information.
3131 * fastboot getvar partition_size:<partition_name>: partition size in hex
3132 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3133 */
3134static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3135{
3136 uint8_t i;
3137
3138 for (i = 0; i < num_parts; i++) {
3139 get_partition_size(info[i].part_name, info[i].size_response);
3140
3141 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3142 {
3143 dprintf(CRITICAL, "partition size name truncated\n");
3144 return;
3145 }
3146 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3147 {
3148 dprintf(CRITICAL, "partition type name truncated\n");
3149 return;
3150 }
3151
3152 /* publish partition size & type info */
3153 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3154 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3155 }
3156}
3157
Amol Jadi5edf3552013-07-23 14:15:34 -07003158/* register commands and variables for fastboot */
3159void aboot_fastboot_register_commands(void)
3160{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003161 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07003162
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003163 struct fastboot_cmd_desc cmd_list[] = {
3164 /* By default the enabled list is empty. */
3165 {"", NULL},
3166 /* move commands enclosed within the below ifndef to here
3167 * if they need to be enabled in user build.
3168 */
3169#ifndef DISABLE_FASTBOOT_CMDS
3170 /* Register the following commands only for non-user builds */
3171 {"flash:", cmd_flash},
3172 {"erase:", cmd_erase},
3173 {"boot", cmd_boot},
3174 {"continue", cmd_continue},
3175 {"reboot", cmd_reboot},
3176 {"reboot-bootloader", cmd_reboot_bootloader},
3177 {"oem unlock", cmd_oem_unlock},
vijay kumarc65876c2015-04-24 13:29:16 +05303178 {"oem unlock-go", cmd_oem_unlock_go},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003179 {"oem lock", cmd_oem_lock},
3180 {"oem verified", cmd_oem_verified},
3181 {"oem device-info", cmd_oem_devinfo},
3182 {"preflash", cmd_preflash},
3183 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3184 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08003185 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003186#if UNITTEST_FW_SUPPORT
3187 {"oem run-tests", cmd_oem_runtests},
3188#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003189#endif
3190 };
3191
3192 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3193 for (i = 1; i < fastboot_cmds_count; i++)
3194 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3195
Amol Jadi5edf3552013-07-23 14:15:34 -07003196 /* publish variables and their values */
3197 fastboot_publish("product", TARGET(BOARD));
3198 fastboot_publish("kernel", "lk");
3199 fastboot_publish("serialno", sn_buf);
3200
3201 /*
3202 * partition info is supported only for emmc partitions
3203 * Calling this for NAND prints some error messages which
3204 * is harmless but misleading. Avoid calling this for NAND
3205 * devices.
3206 */
3207 if (target_is_emmc_boot())
3208 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3209
3210 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003211 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3212 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003213 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003214 /* Is the charger screen check enabled */
3215 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3216 device.charger_screen_enabled);
3217 fastboot_publish("charger-screen-enabled",
3218 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303219 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3220 device.display_panel);
3221 fastboot_publish("display-panel",
3222 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003223 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3224 fastboot_publish("version-baseband", (const char *) device.radio_version);
Amol Jadi5edf3552013-07-23 14:15:34 -07003225}
3226
Brian Swetland9c4c0752009-01-25 16:23:50 -08003227void aboot_init(const struct app_descriptor *app)
3228{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003229 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003230
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003231 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003232 if (target_is_emmc_boot())
3233 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003234 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003235 page_mask = page_size - 1;
3236 }
3237 else
3238 {
3239 page_size = flash_page_size();
3240 page_mask = page_size - 1;
3241 }
3242
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003243 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3244
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003245 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303246 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003247
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003248 /* Display splash screen if enabled */
3249#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003250#if NO_ALARM_DISPLAY
3251 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003252#endif
lijuang99c02d82015-02-13 19:04:34 +08003253 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003254#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003255 /* Wait if the display shutdown is in progress */
3256 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003257 if (!pm_appsbl_display_init_done())
3258 target_display_init(device.display_panel);
3259 else
3260 display_image_on_screen();
3261#else
lijuang99c02d82015-02-13 19:04:34 +08003262 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003263#endif
lijuang99c02d82015-02-13 19:04:34 +08003264 dprintf(SPEW, "Display Init: Done\n");
3265#if NO_ALARM_DISPLAY
3266 }
3267#endif
3268#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003269
Greg Griscod6250552011-06-29 14:40:23 -07003270 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003271 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003272
Dhaval Patel223ec952013-07-18 14:49:44 -07003273 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3274
Matthew Qindefd5562014-07-11 18:02:40 +08003275 /*
3276 * Check power off reason if user force reset,
3277 * if yes phone will do normal boot.
3278 */
3279 if (is_user_force_reset())
3280 goto normal_boot;
3281
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003282 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003283 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003284 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003285 dprintf(ALWAYS,"dload mode key sequence detected\n");
3286 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003287 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003288 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003289 }
3290 else
3291 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303292 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003293 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003294 }
3295 boot_into_fastboot = true;
3296 }
3297 if (!boot_into_fastboot)
3298 {
3299 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3300 boot_into_recovery = 1;
3301 if (!boot_into_recovery &&
3302 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003303 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003304 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003305 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003306 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003307 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003308 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003309
Channagoud Kadabib6e476b2015-08-05 16:23:42 -07003310#if USE_PON_REBOOT_REG
3311 reboot_mode = check_hard_reboot_mode();
3312#else
Ajay Dudani77421292010-10-27 19:34:06 -07003313 reboot_mode = check_reboot_mode();
Channagoud Kadabib6e476b2015-08-05 16:23:42 -07003314#endif
3315 if (reboot_mode == RECOVERY_MODE)
3316 {
Ajay Dudani77421292010-10-27 19:34:06 -07003317 boot_into_recovery = 1;
Channagoud Kadabib6e476b2015-08-05 16:23:42 -07003318 }
3319 else if(reboot_mode == FASTBOOT_MODE)
3320 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003321 boot_into_fastboot = true;
Channagoud Kadabib6e476b2015-08-05 16:23:42 -07003322 }
3323 else if(reboot_mode == ALARM_BOOT)
3324 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003325 boot_reason_alarm = true;
Ajay Dudani77421292010-10-27 19:34:06 -07003326 }
3327
Matthew Qindefd5562014-07-11 18:02:40 +08003328normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003329 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003330 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003331 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003332 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003333 if(emmc_recovery_init())
3334 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3335 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003336 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003337 if((device.is_unlocked) || (device.is_tampered))
3338 {
3339 #ifdef TZ_TAMPER_FUSE
3340 set_tamper_fuse_cmd();
3341 #endif
3342 #if USE_PCOM_SECBOOT
3343 set_tamper_flag(device.is_tampered);
3344 #endif
3345 }
Shashank Mittala0032282011-08-26 14:50:11 -07003346 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003347
Pavel Nedev5d91d412013-04-29 11:34:24 +03003348 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003349 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003350 else
3351 {
3352 recovery_init();
3353 #if USE_PCOM_SECBOOT
3354 if((device.is_unlocked) || (device.is_tampered))
3355 set_tamper_flag(device.is_tampered);
3356 #endif
3357 boot_linux_from_flash();
3358 }
3359 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3360 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003361 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003362
Amol Jadi5edf3552013-07-23 14:15:34 -07003363 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003364
Amol Jadi5edf3552013-07-23 14:15:34 -07003365 /* register aboot specific fastboot commands */
3366 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003367
Amol Jadi5edf3552013-07-23 14:15:34 -07003368 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003369 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003370
3371 /* initialize and start fastboot */
3372 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08003373}
3374
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003375uint32_t get_page_size()
3376{
3377 return page_size;
3378}
3379
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003380/*
3381 * Calculated and save hash (SHA256) for non-signed boot image.
3382 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003383 * @param image_addr - Boot image address
3384 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003385 *
3386 * @return int - 0 on success, negative value on failure.
3387 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003388static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003389{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003390 unsigned int digest[8];
3391#if IMAGE_VERIF_ALGO_SHA1
3392 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3393#else
3394 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3395#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003396
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003397 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003398 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003399
3400 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003401 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003402
3403 return 0;
3404}
3405
Brian Swetland9c4c0752009-01-25 16:23:50 -08003406APP_START(aboot)
3407 .init = aboot_init,
3408APP_END