blob: 416d9e2a8e98a3811b6e4c819af592285e51b4ac [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
Shashank Mittalcd98d472011-08-02 14:29:24 -070079#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080080#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080081#include "bootimg.h"
82#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070083#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080084#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070085#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070086#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070087#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070088#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020089#include "mdtp.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070090
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070091extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070092extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070093extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070094extern int get_target_boot_params(const char *cmdline, const char *part,
95 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070096
Sridhar Parasuram7e16d172015-07-05 11:35:23 -070097void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070098void write_device_info_mmc(device_info *dev);
99void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700100static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700101
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700102/* fastboot command function pointer */
103typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
104
105struct fastboot_cmd_desc {
106 char * name;
107 fastboot_cmd_fn cb;
108};
109
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700110#define EXPAND(NAME) #NAME
111#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700112
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800113#ifdef MEMBASE
114#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
115#else
David Ng183a7422009-12-07 14:55:21 -0800116#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800117#endif
118
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700119#ifndef MEMSIZE
120#define MEMSIZE 1024*1024
121#endif
122
123#define MAX_TAGS_SIZE 1024
124
lijuang72875802015-02-10 10:35:12 +0800125#define RECOVERY_HARD_RESET_MODE 0x01
126#define FASTBOOT_HARD_RESET_MODE 0x02
127#define RTC_HARD_RESET_MODE 0x03
128
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800129#define RECOVERY_MODE 0x77665502
130#define FASTBOOT_MODE 0x77665500
Matthew Qind886f3c2014-01-17 16:52:01 +0800131#define ALARM_BOOT 0x77665503
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800132
Kun Liang2f1601a2013-08-12 16:29:54 +0800133/* make 4096 as default size to ensure EFS,EXT4's erasing */
134#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800135#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800136
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700137#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700138#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800139
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800140#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
141
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700142#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
143
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800144#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700145static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700146#else
David Ng183a7422009-12-07 14:55:21 -0800147static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700148#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800149static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300150static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800151static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800152static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800153static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700154static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300155static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200156static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800157
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800158static const char *baseband_apq = " androidboot.baseband=apq";
159static const char *baseband_msm = " androidboot.baseband=msm";
160static const char *baseband_csfb = " androidboot.baseband=csfb";
161static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700162static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800163static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700164static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800165static const char *baseband_dsda = " androidboot.baseband=dsda";
166static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800167static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800168static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800169
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700170static unsigned page_size = 0;
171static unsigned page_mask = 0;
172static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
173static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700174static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800175static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700176static bool devinfo_present = true;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700177
Shashank Mittalcd98d472011-08-02 14:29:24 -0700178/* Assuming unauthorized kernel image by default */
179static int auth_kernel_img = 0;
180
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -0800181static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
vijay kumarc65876c2015-04-24 13:29:16 +0530182static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700183
vijay kumarca2e6812015-07-08 20:28:25 +0530184static char frp_ptns[2][8] = {"config","frp"};
185
Dima Zavin42168f22009-01-30 11:52:22 -0800186struct atag_ptbl_entry
187{
188 char name[16];
189 unsigned offset;
190 unsigned size;
191 unsigned flags;
192};
193
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700194/*
195 * Partition info, required to be published
196 * for fastboot
197 */
198struct getvar_partition_info {
199 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
200 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
201 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
202 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
203 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
204};
205
206/*
207 * Right now, we are publishing the info for only
208 * three partitions
209 */
210struct getvar_partition_info part_info[] =
211{
212 { "system" , "partition-size:", "partition-type:", "", "ext4" },
213 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
214 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
215};
216
217char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700218char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800219char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700220char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530221char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700222
Greg Griscod2471ef2011-07-14 13:00:42 -0700223extern int emmc_recovery_init(void);
224
Kinson Chik0b1c8162011-08-31 16:31:57 -0700225#if NO_KEYPAD_DRIVER
226extern int fastboot_trigger(void);
227#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700228
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800229static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700230{
231 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700232#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800233 if (is_arm64)
234 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
235 else
236 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700237 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
238 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700239#endif
240}
241
Dima Zavin42168f22009-01-30 11:52:22 -0800242static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
243{
244 struct atag_ptbl_entry atag_ptn;
245
246 memcpy(atag_ptn.name, ptn->name, 16);
247 atag_ptn.name[15] = '\0';
248 atag_ptn.offset = ptn->start;
249 atag_ptn.size = ptn->length;
250 atag_ptn.flags = ptn->flags;
251 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
252 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
253}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800254
Neeti Desaie245d492012-06-01 12:52:13 -0700255unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800256{
David Ng183a7422009-12-07 14:55:21 -0800257 int cmdline_len = 0;
258 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800259 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700260 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800261 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300262 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700263 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700264 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200265 bool is_mdtp_activated = 0;
266#ifdef MDTP_SUPPORT
267 mdtp_activated(&is_mdtp_activated);
268#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800269
Brian Swetland9c4c0752009-01-25 16:23:50 -0800270 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800271 cmdline_len = strlen(cmdline);
272 have_cmdline = 1;
273 }
274 if (target_is_emmc_boot()) {
275 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800276#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700277 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
278 ASSERT(boot_dev_buf);
279 platform_boot_dev_cmdline(boot_dev_buf);
280 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700281#endif
David Ng183a7422009-12-07 14:55:21 -0800282 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800283
284 cmdline_len += strlen(usb_sn_cmdline);
285 cmdline_len += strlen(sn_buf);
286
Pavel Nedev5614d222013-06-17 18:01:02 +0300287 if (boot_into_recovery && gpt_exists)
288 cmdline_len += strlen(secondary_gpt_enable);
289
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200290 if(is_mdtp_activated)
291 cmdline_len += strlen(mdtp_activated_flag);
292
Pavel Nedev328ac822013-04-05 15:25:11 +0300293 if (boot_into_ffbm) {
294 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700295 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800296 /* reduce kernel console messages to speed-up boot */
297 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800298 } else if (boot_reason_alarm) {
299 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800300 } else if ((target_build_variant_user() || device.charger_screen_enabled)
301 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700302 pause_at_bootup = 1;
303 cmdline_len += strlen(battchg_pause);
304 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800305
Shashank Mittalcd98d472011-08-02 14:29:24 -0700306 if(target_use_signed_kernel() && auth_kernel_img) {
307 cmdline_len += strlen(auth_kernel);
308 }
309
Joonwoo Park61112782013-10-02 19:50:39 -0700310 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
311 "system",
312 target_boot_params,
313 sizeof(target_boot_params)) == 0) {
314 have_target_boot_params = 1;
315 cmdline_len += strlen(target_boot_params);
316 }
317
Ajay Dudanid04110c2011-01-17 23:55:07 -0800318 /* Determine correct androidboot.baseband to use */
319 switch(target_baseband())
320 {
321 case BASEBAND_APQ:
322 cmdline_len += strlen(baseband_apq);
323 break;
324
325 case BASEBAND_MSM:
326 cmdline_len += strlen(baseband_msm);
327 break;
328
329 case BASEBAND_CSFB:
330 cmdline_len += strlen(baseband_csfb);
331 break;
332
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800333 case BASEBAND_SVLTE2A:
334 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800335 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700336
337 case BASEBAND_MDM:
338 cmdline_len += strlen(baseband_mdm);
339 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700340
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800341 case BASEBAND_MDM2:
342 cmdline_len += strlen(baseband_mdm2);
343 break;
344
Amol Jadi5c61a952012-05-04 17:05:35 -0700345 case BASEBAND_SGLTE:
346 cmdline_len += strlen(baseband_sglte);
347 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530348
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800349 case BASEBAND_SGLTE2:
350 cmdline_len += strlen(baseband_sglte2);
351 break;
352
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530353 case BASEBAND_DSDA:
354 cmdline_len += strlen(baseband_dsda);
355 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800356
357 case BASEBAND_DSDA2:
358 cmdline_len += strlen(baseband_dsda2);
359 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800360 }
361
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800362 if (cmdline) {
363 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530364 target_display_panel_node(display_panel_buf,
365 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800366 strlen(display_panel_buf)) {
367 cmdline_len += strlen(display_panel_buf);
368 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700369 }
370
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800371 if (target_warm_boot()) {
372 warm_boot = true;
373 cmdline_len += strlen(warmboot_cmdline);
374 }
375
David Ng183a7422009-12-07 14:55:21 -0800376 if (cmdline_len > 0) {
377 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800378 unsigned char *dst;
379
380 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
381 ASSERT(cmdline_final != NULL);
382 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700383
Amol Jadi168b7712012-03-06 16:15:00 -0800384 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800385 if (have_cmdline) {
386 src = cmdline;
387 while ((*dst++ = *src++));
388 }
389 if (target_is_emmc_boot()) {
390 src = emmc_cmdline;
391 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700392 have_cmdline = 1;
393 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800394#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700395 src = boot_dev_buf;
396 if (have_cmdline) --dst;
397 while ((*dst++ = *src++));
398#endif
David Ngf773dde2010-07-26 19:55:08 -0700399 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800400
401 src = usb_sn_cmdline;
402 if (have_cmdline) --dst;
403 have_cmdline = 1;
404 while ((*dst++ = *src++));
405 src = sn_buf;
406 if (have_cmdline) --dst;
407 have_cmdline = 1;
408 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800409 if (warm_boot) {
410 if (have_cmdline) --dst;
411 src = warmboot_cmdline;
412 while ((*dst++ = *src++));
413 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800414
Pavel Nedev5614d222013-06-17 18:01:02 +0300415 if (boot_into_recovery && gpt_exists) {
416 src = secondary_gpt_enable;
417 if (have_cmdline) --dst;
418 while ((*dst++ = *src++));
419 }
420
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200421 if (is_mdtp_activated) {
422 src = mdtp_activated_flag;
423 if (have_cmdline) --dst;
424 while ((*dst++ = *src++));
425 }
426
Pavel Nedev328ac822013-04-05 15:25:11 +0300427 if (boot_into_ffbm) {
428 src = androidboot_mode;
429 if (have_cmdline) --dst;
430 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700431 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300432 if (have_cmdline) --dst;
433 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800434 src = loglevel;
435 if (have_cmdline) --dst;
436 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800437 } else if (boot_reason_alarm) {
438 src = alarmboot_cmdline;
439 if (have_cmdline) --dst;
440 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300441 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700442 src = battchg_pause;
443 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800444 while ((*dst++ = *src++));
445 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800446
Shashank Mittalcd98d472011-08-02 14:29:24 -0700447 if(target_use_signed_kernel() && auth_kernel_img) {
448 src = auth_kernel;
449 if (have_cmdline) --dst;
450 while ((*dst++ = *src++));
451 }
452
Ajay Dudanid04110c2011-01-17 23:55:07 -0800453 switch(target_baseband())
454 {
455 case BASEBAND_APQ:
456 src = baseband_apq;
457 if (have_cmdline) --dst;
458 while ((*dst++ = *src++));
459 break;
460
461 case BASEBAND_MSM:
462 src = baseband_msm;
463 if (have_cmdline) --dst;
464 while ((*dst++ = *src++));
465 break;
466
467 case BASEBAND_CSFB:
468 src = baseband_csfb;
469 if (have_cmdline) --dst;
470 while ((*dst++ = *src++));
471 break;
472
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800473 case BASEBAND_SVLTE2A:
474 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800475 if (have_cmdline) --dst;
476 while ((*dst++ = *src++));
477 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700478
479 case BASEBAND_MDM:
480 src = baseband_mdm;
481 if (have_cmdline) --dst;
482 while ((*dst++ = *src++));
483 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700484
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800485 case BASEBAND_MDM2:
486 src = baseband_mdm2;
487 if (have_cmdline) --dst;
488 while ((*dst++ = *src++));
489 break;
490
Amol Jadi5c61a952012-05-04 17:05:35 -0700491 case BASEBAND_SGLTE:
492 src = baseband_sglte;
493 if (have_cmdline) --dst;
494 while ((*dst++ = *src++));
495 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530496
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800497 case BASEBAND_SGLTE2:
498 src = baseband_sglte2;
499 if (have_cmdline) --dst;
500 while ((*dst++ = *src++));
501 break;
502
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530503 case BASEBAND_DSDA:
504 src = baseband_dsda;
505 if (have_cmdline) --dst;
506 while ((*dst++ = *src++));
507 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800508
509 case BASEBAND_DSDA2:
510 src = baseband_dsda2;
511 if (have_cmdline) --dst;
512 while ((*dst++ = *src++));
513 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800514 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700515
516 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700517 src = display_panel_buf;
518 if (have_cmdline) --dst;
519 while ((*dst++ = *src++));
520 }
Joonwoo Park61112782013-10-02 19:50:39 -0700521
522 if (have_target_boot_params) {
523 if (have_cmdline) --dst;
524 src = target_boot_params;
525 while ((*dst++ = *src++));
526 }
Neeti Desaie245d492012-06-01 12:52:13 -0700527 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700528
529
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700530 if (boot_dev_buf)
531 free(boot_dev_buf);
532
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800533 if (cmdline_final)
534 dprintf(INFO, "cmdline: %s\n", cmdline_final);
535 else
536 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700537 return cmdline_final;
538}
539
540unsigned *atag_core(unsigned *ptr)
541{
542 /* CORE */
543 *ptr++ = 2;
544 *ptr++ = 0x54410001;
545
546 return ptr;
547
548}
549
550unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
551 unsigned ramdisk_size)
552{
553 if (ramdisk_size) {
554 *ptr++ = 4;
555 *ptr++ = 0x54420005;
556 *ptr++ = (unsigned)ramdisk;
557 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800558 }
559
Neeti Desaie245d492012-06-01 12:52:13 -0700560 return ptr;
561}
562
563unsigned *atag_ptable(unsigned **ptr_addr)
564{
565 int i;
566 struct ptable *ptable;
567
568 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700569 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
570 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700571 *(*ptr_addr)++ = 0x4d534d70;
572 for (i = 0; i < ptable->count; ++i)
573 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
574 }
575
576 return (*ptr_addr);
577}
578
579unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
580{
581 int cmdline_length = 0;
582 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700583 char *dest;
584
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800585 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700586 n = (cmdline_length + 4) & (~3);
587
588 *ptr++ = (n / 4) + 2;
589 *ptr++ = 0x54410009;
590 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800591 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700592 ptr += (n / 4);
593
594 return ptr;
595}
596
597unsigned *atag_end(unsigned *ptr)
598{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800599 /* END */
600 *ptr++ = 0;
601 *ptr++ = 0;
602
Neeti Desaie245d492012-06-01 12:52:13 -0700603 return ptr;
604}
605
606void generate_atags(unsigned *ptr, const char *cmdline,
607 void *ramdisk, unsigned ramdisk_size)
608{
609
610 ptr = atag_core(ptr);
611 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
612 ptr = target_atag_mem(ptr);
613
614 /* Skip NAND partition ATAGS for eMMC boot */
615 if (!target_is_emmc_boot()){
616 ptr = atag_ptable(&ptr);
617 }
618
619 ptr = atag_cmdline(ptr, cmdline);
620 ptr = atag_end(ptr);
621}
622
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700623typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700624void boot_linux(void *kernel, unsigned *tags,
625 const char *cmdline, unsigned machtype,
626 void *ramdisk, unsigned ramdisk_size)
627{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800628 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800629#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700630 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800631#endif
632
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700633 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800634 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800635 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800636
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530637 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700638
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800639 final_cmdline = update_cmdline((const char*)cmdline);
640
Neeti Desai17379b82012-06-04 18:42:53 -0700641#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800642 dprintf(INFO, "Updating device tree: start\n");
643
Neeti Desai17379b82012-06-04 18:42:53 -0700644 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530645 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700646 if(ret)
647 {
648 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
649 ASSERT(0);
650 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800651 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700652#else
Neeti Desaie245d492012-06-01 12:52:13 -0700653 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800654 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700655#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700656
Maria Yu52254c02014-07-04 16:14:54 +0800657 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700658
659#if VERIFIED_BOOT
660 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700661 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700662 {
663 dprintf(INFO, "Failed to write protect dev info\n");
664 ASSERT(0);
665 }
666#endif
667
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700668 /* Perform target specific cleanup */
669 target_uninit();
670
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800671 /* Turn off splash screen if enabled */
672#if DISPLAY_SPLASH_SCREEN
673 target_display_shutdown();
674#endif
675
676
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800677 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530678 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800679
680 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700681
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +0530682 /* Initialise wdog to catch early kernel crashes */
683#if WDOG_SUPPORT
684 msm_wdog_init();
685#endif
Amol Jadi4421e652011-06-16 15:00:48 -0700686 /* do any platform specific cleanup before kernel entry */
687 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700688
Brian Swetland9c4c0752009-01-25 16:23:50 -0800689 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700690
Amol Jadi504f9fe2012-08-16 13:56:48 -0700691#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800692 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700693#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700694 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800695
696 if (IS_ARM64(kptr))
697 /* Jump to a 64bit kernel */
698 scm_elexec_call((paddr_t)kernel, tags_phys);
699 else
700 /* Jump to a 32bit kernel */
701 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800702}
703
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700704/* Function to check if the memory address range falls within the aboot
705 * boundaries.
706 * start: Start of the memory region
707 * size: Size of the memory region
708 */
709int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
710{
711 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800712 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700713 return -1;
714
715 /* Check for memory overlap. */
716 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
717 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700718 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700719 return 0;
720 else
721 return -1;
722}
723
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800724#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800725
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800726BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800727#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800728BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800729#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800730
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700731static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
732{
733 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800734
735#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800736#if IMAGE_VERIF_ALGO_SHA1
737 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
738#else
739 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
740#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800741#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700742
743 /* Assume device is rooted at this time. */
744 device.is_tampered = 1;
745
746 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
747
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700748#if VERIFIED_BOOT
749 if(boot_into_recovery)
750 {
751 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530752 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700753 }
754 else
755 {
756 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530757 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700758 }
759 boot_verify_print_state();
760#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700761 ret = image_verify((unsigned char *)bootimg_addr,
762 (unsigned char *)(bootimg_addr + bootimg_size),
763 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800764 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700765#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700766 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
767
768 if (ret)
769 {
770 /* Authorized kernel */
771 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700772 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700773 }
774
Amit Blay4aa292f2015-04-28 21:55:59 +0300775#ifdef MDTP_SUPPORT
776 {
777 /* Verify MDTP lock.
778 * For boot & recovery partitions, use aboot's verification result.
779 */
780 mdtp_ext_partition_verification_t ext_partition;
781 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
782 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
783 ext_partition.page_size = 0; /* Not needed since already validated */
784 ext_partition.image_addr = 0; /* Not needed since already validated */
785 ext_partition.image_size = 0; /* Not needed since already validated */
786 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
787 mdtp_fwlock_verify_lock(&ext_partition);
788 }
789#endif /* MDTP_SUPPORT */
790
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700791#if USE_PCOM_SECBOOT
792 set_tamper_flag(device.is_tampered);
793#endif
794
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700795#if VERIFIED_BOOT
796 if(boot_verify_get_state() == RED)
797 {
798 if(!boot_into_recovery)
799 {
800 dprintf(CRITICAL,
801 "Device verification failed. Rebooting into recovery.\n");
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +0530802 mdelay(1000);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700803 reboot_device(RECOVERY_MODE);
804 }
805 else
806 {
807 dprintf(CRITICAL,
808 "Recovery image verification failed. Asserting..\n");
809 ASSERT(0);
810 }
811 }
812#endif
Unnati Gandhi1be04752015-03-27 19:41:53 +0530813
814 if(device.is_tampered)
815 {
816 write_device_info_mmc(&device);
817 #ifdef TZ_TAMPER_FUSE
818 set_tamper_fuse_cmd();
819 #endif
820 #ifdef ASSERT_ON_TAMPER
821 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
822 ASSERT(0);
823 #endif
824 }
825
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700826}
827
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530828static bool check_format_bit()
829{
830 bool ret = false;
831 int index;
832 uint64_t offset;
833 struct boot_selection_info *in = NULL;
834 char *buf = NULL;
835
836 index = partition_get_index("bootselect");
837 if (index == INVALID_PTN)
838 {
839 dprintf(INFO, "Unable to locate /bootselect partition\n");
840 return ret;
841 }
842 offset = partition_get_offset(index);
843 if(!offset)
844 {
845 dprintf(INFO, "partition /bootselect doesn't exist\n");
846 return ret;
847 }
848 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
849 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530850 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530851 {
852 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
853 free(buf);
854 return ret;
855 }
856 in = (struct boot_selection_info *) buf;
857 if ((in->signature == BOOTSELECT_SIGNATURE) &&
858 (in->version == BOOTSELECT_VERSION)) {
859 if ((in->state_info & BOOTSELECT_FORMAT) &&
860 !(in->state_info & BOOTSELECT_FACTORY))
861 ret = true;
862 } else {
863 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
864 in->signature, in->version);
865 ASSERT(0);
866 }
867 free(buf);
868 return ret;
869}
870
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700871void boot_verifier_init()
872{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700873 uint32_t boot_state;
874 /* Check if device unlock */
875 if(device.is_unlocked)
876 {
877 boot_verify_send_event(DEV_UNLOCK);
878 boot_verify_print_state();
879 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
880 return;
881 }
882 else
883 {
884 boot_verify_send_event(BOOT_INIT);
885 }
886
887 /* Initialize keystore */
888 boot_state = boot_verify_keystore_init();
889 if(boot_state == YELLOW)
890 {
891 boot_verify_print_state();
892 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
893 }
894}
895
Shashank Mittal23b8f422010-04-16 19:27:21 -0700896int boot_linux_from_mmc(void)
897{
898 struct boot_img_hdr *hdr = (void*) buf;
899 struct boot_img_hdr *uhdr;
900 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700901 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700902 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700903 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700904
Shashank Mittalcd98d472011-08-02 14:29:24 -0700905 unsigned char *image_addr = 0;
906 unsigned kernel_actual;
907 unsigned ramdisk_actual;
908 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700909 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700910
Matthew Qin271927e2015-03-31 22:07:07 -0400911 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +0800912 unsigned int out_len = 0;
913 unsigned int out_avai_len = 0;
914 unsigned char *out_addr = NULL;
915 uint32_t dtb_offset = 0;
916 unsigned char *kernel_start_addr = NULL;
917 unsigned int kernel_size = 0;
918 int rc;
919
Neeti Desai465491e2012-07-31 12:53:35 -0700920#if DEVICE_TREE
921 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700922 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700923 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800924 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700925 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -0400926 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -0700927#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +0800928 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800929
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530930 if (check_format_bit())
931 boot_into_recovery = 1;
932
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700933 if (!boot_into_recovery) {
934 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
935 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
936 if (rcode <= 0) {
937 boot_into_ffbm = false;
938 if (rcode < 0)
939 dprintf(CRITICAL,"failed to get ffbm cookie");
940 } else
941 boot_into_ffbm = true;
942 } else
943 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700944 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
945 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
946 dprintf(INFO, "Unified boot method!\n");
947 hdr = uhdr;
948 goto unified_boot;
949 }
Greg Griscod6250552011-06-29 14:40:23 -0700950 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700951 index = partition_get_index("boot");
952 ptn = partition_get_offset(index);
953 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700954 dprintf(CRITICAL, "ERROR: No boot partition found\n");
955 return -1;
956 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700957 }
958 else {
959 index = partition_get_index("recovery");
960 ptn = partition_get_offset(index);
961 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700962 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
963 return -1;
964 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700965 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -0800966 /* Set Lun for boot & recovery partitions */
967 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -0700968
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530969 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700970 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
971 return -1;
972 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700973
974 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700975 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700976 return -1;
977 }
978
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700979 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530980
981 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
982 dprintf(CRITICAL, "ERROR: Invalid page size\n");
983 return -1;
984 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700985 page_size = hdr->page_size;
986 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700987 }
988
Matthew Qin49e51fa2015-02-09 10:40:45 +0800989 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
990 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800991
Matthew Qin49e51fa2015-02-09 10:40:45 +0800992 image_addr = (unsigned char *)target_get_scratch_address();
993
994#if DEVICE_TREE
995 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
996 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
997#else
998 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
999#endif
1000
1001#if VERIFIED_BOOT
1002 boot_verifier_init();
1003#endif
1004
1005 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
1006 {
1007 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1008 return -1;
1009 }
1010
Matthew Qinbb7923d2015-02-09 10:56:09 +08001011 /*
1012 * Update loading flow of bootimage to support compressed/uncompressed
1013 * bootimage on both 64bit and 32bit platform.
1014 * 1. Load bootimage from emmc partition onto DDR.
1015 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1016 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1017 * platform accordingly.
1018 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1019 */
1020
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001021 dprintf(INFO, "Loading (%s) image (%d): start\n",
1022 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001023 bs_set_timestamp(BS_KERNEL_LOAD_START);
1024
Matthew Qinbb7923d2015-02-09 10:56:09 +08001025 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001026 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1027 {
1028 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1029 return -1;
1030 }
1031
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001032 dprintf(INFO, "Loading (%s) image (%d): done\n",
1033 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1034
Matthew Qin49e51fa2015-02-09 10:40:45 +08001035 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1036
1037 /* Authenticate Kernel */
1038 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1039 (int) target_use_signed_kernel(),
1040 device.is_unlocked,
1041 device.is_tampered);
1042
1043 if(target_use_signed_kernel() && (!device.is_unlocked))
1044 {
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);
1060 } else {
1061 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1062 #ifdef TZ_SAVE_KERNEL_HASH
1063 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1064 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001065
1066#ifdef MDTP_SUPPORT
1067 {
1068 /* Verify MDTP lock.
1069 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1070 * since verification was skipped in aboot. The signature is not part of the loaded image.
1071 */
1072 mdtp_ext_partition_verification_t ext_partition;
1073 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1074 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1075 ext_partition.page_size = page_size;
1076 ext_partition.image_addr = (uint32)image_addr;
1077 ext_partition.image_size = imagesize_actual;
1078 ext_partition.sig_avail = FALSE;
1079 mdtp_fwlock_verify_lock(&ext_partition);
1080 }
1081#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001082 }
1083
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001084 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001085 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1086 * If not, continue booting.
1087 */
1088 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1089 {
1090 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1091 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001092 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001093 rc = decompress((unsigned char *)(image_addr + page_size),
1094 hdr->kernel_size, out_addr, out_avai_len,
1095 &dtb_offset, &out_len);
1096 if (rc)
1097 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001098 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001099 ASSERT(0);
1100 }
1101
Matthew Qin0b15b322015-05-19 05:20:54 -04001102 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001103 kptr = (struct kernel64_hdr *)out_addr;
1104 kernel_start_addr = out_addr;
1105 kernel_size = out_len;
1106 } else {
1107 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1108 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1109 kernel_size = hdr->kernel_size;
1110 }
1111
1112 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001113 * Update the kernel/ramdisk/tags address if the boot image header
1114 * has default values, these default values come from mkbootimg when
1115 * the boot image is flashed using fastboot flash:raw
1116 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001117 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001118
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001119 /* Get virtual addresses since the hdr saves physical addresses. */
1120 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1121 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1122 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001123
Matthew Qinbb7923d2015-02-09 10:56:09 +08001124 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001125 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001126 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001127 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1128 {
1129 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1130 return -1;
1131 }
1132
1133#ifndef DEVICE_TREE
1134 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1135 {
1136 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1137 return -1;
1138 }
1139#endif
1140
Matthew Qin49e51fa2015-02-09 10:40:45 +08001141 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001142 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001143 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001144
Matthew Qin49e51fa2015-02-09 10:40:45 +08001145 #if DEVICE_TREE
1146 if(hdr->dt_size) {
1147 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1148 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001149
Matthew Qin49e51fa2015-02-09 10:40:45 +08001150 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1151 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1152 return -1;
1153 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001154
Matthew Qin49e51fa2015-02-09 10:40:45 +08001155 /* Find index of device tree within device tree table */
1156 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1157 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1158 return -1;
1159 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001160
Matthew Qin271927e2015-03-31 22:07:07 -04001161 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1162 {
1163 unsigned int compressed_size = 0;
1164 out_addr += out_len;
1165 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001166 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001167 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1168 dt_entry.size, out_addr, out_avai_len,
1169 &compressed_size, &dtb_size);
1170 if (rc)
1171 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001172 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001173 ASSERT(0);
1174 }
1175
Matthew Qin0b15b322015-05-19 05:20:54 -04001176 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001177 best_match_dt_addr = out_addr;
1178 } else {
1179 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1180 dtb_size = dt_entry.size;
1181 }
1182
Matthew Qin49e51fa2015-02-09 10:40:45 +08001183 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001184 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001185 {
1186 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1187 return -1;
1188 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001189
Matthew Qin271927e2015-03-31 22:07:07 -04001190 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001191 } else {
1192 /* Validate the tags_addr */
1193 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001194 {
1195 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1196 return -1;
1197 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001198 /*
1199 * If appended dev tree is found, update the atags with
1200 * memory address to the DTB appended location on RAM.
1201 * Else update with the atags address in the kernel header
1202 */
1203 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001204 dtb = dev_tree_appended((void*)(image_addr + page_size),
1205 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001206 (void *)hdr->tags_addr);
1207 if (!dtb) {
1208 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001209 return -1;
1210 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001211 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001212 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001213
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001214 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1215 target_load_ssd_keystore();
1216
Shashank Mittal23b8f422010-04-16 19:27:21 -07001217unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001218
Dima Zavin77e41f32013-03-06 16:10:43 -08001219 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001220 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001221 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1222
1223 return 0;
1224}
1225
Dima Zavin214cc642009-01-26 11:16:21 -08001226int boot_linux_from_flash(void)
1227{
1228 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001229 struct ptentry *ptn;
1230 struct ptable *ptable;
1231 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001232
Shashank Mittalcd98d472011-08-02 14:29:24 -07001233 unsigned char *image_addr = 0;
1234 unsigned kernel_actual;
1235 unsigned ramdisk_actual;
1236 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001237 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001238
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001239#if DEVICE_TREE
1240 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001241 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001242 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001243 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001244#endif
1245
David Ng183a7422009-12-07 14:55:21 -08001246 if (target_is_emmc_boot()) {
1247 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1248 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1249 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1250 return -1;
1251 }
1252 goto continue_boot;
1253 }
1254
Dima Zavin214cc642009-01-26 11:16:21 -08001255 ptable = flash_get_ptable();
1256 if (ptable == NULL) {
1257 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1258 return -1;
1259 }
1260
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001261 if(!boot_into_recovery)
1262 {
1263 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001264
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001265 if (ptn == NULL) {
1266 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1267 return -1;
1268 }
1269 }
1270 else
1271 {
1272 ptn = ptable_find(ptable, "recovery");
1273 if (ptn == NULL) {
1274 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1275 return -1;
1276 }
Dima Zavin214cc642009-01-26 11:16:21 -08001277 }
1278
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001279 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001280 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1281 return -1;
1282 }
Dima Zavin214cc642009-01-26 11:16:21 -08001283
1284 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001285 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001286 return -1;
1287 }
1288
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001289 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001290 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 -08001291 return -1;
1292 }
1293
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001294 /*
1295 * Update the kernel/ramdisk/tags address if the boot image header
1296 * has default values, these default values come from mkbootimg when
1297 * the boot image is flashed using fastboot flash:raw
1298 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001299 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001300
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001301 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001302 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1303 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1304 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1305
1306 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1307 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1308
1309 /* Check if the addresses in the header are valid. */
1310 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1311 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1312 {
1313 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1314 return -1;
1315 }
1316
1317#ifndef DEVICE_TREE
1318 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1319 {
1320 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1321 return -1;
1322 }
1323#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001324
Shashank Mittalcd98d472011-08-02 14:29:24 -07001325 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001326 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001327 {
1328 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001329 offset = 0;
1330
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001331#if DEVICE_TREE
1332 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1333 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001334
1335 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1336 {
1337 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1338 return -1;
1339 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001340#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001341 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001342#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001343
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001344 dprintf(INFO, "Loading (%s) image (%d): start\n",
1345 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001346 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001347
Shashank Mittalcd98d472011-08-02 14:29:24 -07001348 /* Read image without signature */
1349 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1350 {
1351 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1352 return -1;
1353 }
Dima Zavin214cc642009-01-26 11:16:21 -08001354
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001355 dprintf(INFO, "Loading (%s) image (%d): done\n",
1356 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001357 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001358
Shashank Mittalcd98d472011-08-02 14:29:24 -07001359 offset = imagesize_actual;
1360 /* Read signature */
1361 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1362 {
1363 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001364 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001365 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001366
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301367 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001368
1369 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001370 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1371 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001372#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001373 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001374 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001375 {
1376 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1377 return -1;
1378 }
1379
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001380 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1381#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001382
1383 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001384 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001385 {
1386 write_device_info_flash(&device);
1387 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301388#if USE_PCOM_SECBOOT
1389 set_tamper_flag(device.is_tampered);
1390#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001391 }
1392 else
1393 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001394 offset = page_size;
1395
Amol Jadib6be5c12012-11-14 13:39:51 -08001396 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1397 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1398 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1399
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001400 dprintf(INFO, "Loading (%s) image (%d): start\n",
1401 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1402
Amol Jadi492d5a52013-03-15 16:12:34 -07001403 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001404
1405 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001406 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1407 return -1;
1408 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001409 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001410
Amol Jadib6be5c12012-11-14 13:39:51 -08001411 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001412 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1413 return -1;
1414 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001415 offset += ramdisk_actual;
1416
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001417 dprintf(INFO, "Loading (%s) image (%d): done\n",
1418 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1419
Amol Jadi492d5a52013-03-15 16:12:34 -07001420 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001421
1422 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001423 offset += second_actual;
1424 /* Second image loading not implemented. */
1425 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001426 }
1427
1428#if DEVICE_TREE
1429 if(hdr->dt_size != 0) {
1430
1431 /* Read the device tree table into buffer */
1432 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1433 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1434 return -1;
1435 }
1436
1437 table = (struct dt_table*) dt_buf;
1438
Deepa Dinamani19648b42013-09-05 17:05:55 -07001439 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001440 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1441 return -1;
1442 }
1443
Deepa Dinamani19648b42013-09-05 17:05:55 -07001444 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1445 if (!table)
1446 return -1;
1447
1448 /* Read the entire device tree table into buffer */
1449 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1450 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1451 return -1;
1452 }
1453
1454
Joel Kingaa335dc2013-06-03 16:11:08 -07001455 /* Find index of device tree within device tree table */
1456 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001457 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1458 return -1;
1459 }
1460
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001461 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001462 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001463 {
1464 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1465 return -1;
1466 }
1467
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001468 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001469 if(flash_read(ptn, offset + dt_entry.offset,
1470 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001471 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1472 return -1;
1473 }
1474 }
1475#endif
1476
Shashank Mittalcd98d472011-08-02 14:29:24 -07001477 }
David Ng183a7422009-12-07 14:55:21 -08001478continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001479
Dima Zavin214cc642009-01-26 11:16:21 -08001480 /* TODO: create/pass atags to kernel */
1481
Ajay Dudanie28a6072011-07-01 13:59:46 -07001482 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001483 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001484 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1485
1486 return 0;
1487}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001488
Shashank Mittal162244e2011-08-08 19:01:25 -07001489void write_device_info_mmc(device_info *dev)
1490{
Shashank Mittal162244e2011-08-08 19:01:25 -07001491 unsigned long long ptn = 0;
1492 unsigned long long size;
1493 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001494 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001495 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001496 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001497
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001498 if (devinfo_present)
1499 index = partition_get_index("devinfo");
1500 else
1501 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001502
Shashank Mittal162244e2011-08-08 19:01:25 -07001503 ptn = partition_get_offset(index);
1504 if(ptn == 0)
1505 {
1506 return;
1507 }
1508
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001509 lun = partition_get_lun(index);
1510 mmc_set_lun(lun);
1511
Shashank Mittal162244e2011-08-08 19:01:25 -07001512 size = partition_get_size(index);
1513
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001514 blocksize = mmc_get_device_blocksize();
1515
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001516 if (devinfo_present)
1517 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1518 else
1519 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1520 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001521 {
1522 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001523 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001524 }
1525}
1526
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001527void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001528{
Shashank Mittal162244e2011-08-08 19:01:25 -07001529 unsigned long long ptn = 0;
1530 unsigned long long size;
1531 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001532 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001533 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001534
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001535 if ((index = partition_get_index("devinfo")) < 0)
1536 {
1537 devinfo_present = false;
1538 index = partition_get_index("aboot");
1539 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001540
Shashank Mittal162244e2011-08-08 19:01:25 -07001541 ptn = partition_get_offset(index);
1542 if(ptn == 0)
1543 {
1544 return;
1545 }
1546
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001547 mmc_set_lun(partition_get_lun(index));
1548
Shashank Mittal162244e2011-08-08 19:01:25 -07001549 size = partition_get_size(index);
1550
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001551 blocksize = mmc_get_device_blocksize();
1552
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001553 if (devinfo_present)
1554 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1555 else
1556 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1557 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001558 {
1559 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001560 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001561 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001562}
1563
1564void write_device_info_flash(device_info *dev)
1565{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001566 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001567 struct ptentry *ptn;
1568 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001569 if(info == NULL)
1570 {
1571 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1572 ASSERT(0);
1573 }
1574 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001575 ptable = flash_get_ptable();
1576 if (ptable == NULL)
1577 {
1578 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1579 return;
1580 }
1581
1582 ptn = ptable_find(ptable, "devinfo");
1583 if (ptn == NULL)
1584 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001585 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001586 return;
1587 }
1588
1589 memcpy(info, dev, sizeof(device_info));
1590
1591 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1592 {
1593 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1594 return;
1595 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001596 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001597}
1598
vijay kumarc65876c2015-04-24 13:29:16 +05301599static int read_allow_oem_unlock(device_info *dev)
1600{
vijay kumarc65876c2015-04-24 13:29:16 +05301601 unsigned offset;
1602 int index;
1603 unsigned long long ptn;
1604 unsigned long long ptn_size;
1605 unsigned blocksize = mmc_get_device_blocksize();
1606 char buf[blocksize];
1607
vijay kumarca2e6812015-07-08 20:28:25 +05301608 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301609 if (index == INVALID_PTN)
1610 {
vijay kumarca2e6812015-07-08 20:28:25 +05301611 index = partition_get_index(frp_ptns[1]);
1612 if (index == INVALID_PTN)
1613 {
1614 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1615 return -1;
1616 }
vijay kumarc65876c2015-04-24 13:29:16 +05301617 }
1618
1619 ptn = partition_get_offset(index);
1620 ptn_size = partition_get_size(index);
1621 offset = ptn_size - blocksize;
1622
1623 if (mmc_read(ptn + offset, (void *)buf, sizeof(buf)))
1624 {
1625 dprintf(CRITICAL, "Reading MMC failed\n");
1626 return -1;
1627 }
1628
1629 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1630 is_allow_unlock = buf[blocksize-1] & 0x01;
1631 return 0;
1632}
1633
1634static int write_allow_oem_unlock(bool allow_unlock)
1635{
vijay kumarc65876c2015-04-24 13:29:16 +05301636 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301637 int index;
1638 unsigned long long ptn;
1639 unsigned long long ptn_size;
1640 unsigned blocksize = mmc_get_device_blocksize();
1641 char buf[blocksize];
1642
vijay kumarca2e6812015-07-08 20:28:25 +05301643 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301644 if (index == INVALID_PTN)
1645 {
vijay kumarca2e6812015-07-08 20:28:25 +05301646 index = partition_get_index(frp_ptns[1]);
1647 if (index == INVALID_PTN)
1648 {
1649 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1650 return -1;
1651 }
vijay kumarc65876c2015-04-24 13:29:16 +05301652 }
1653
1654 ptn = partition_get_offset(index);
1655 ptn_size = partition_get_size(index);
1656 offset = ptn_size - blocksize;
1657
1658 if (mmc_read(ptn + offset, (void *)buf, sizeof(buf)))
1659 {
1660 dprintf(CRITICAL, "Reading MMC failed\n");
1661 return -1;
1662 }
1663
1664 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1665 buf[blocksize-1] = allow_unlock;
1666 if (mmc_write(ptn + offset, blocksize, buf))
1667 {
1668 dprintf(CRITICAL, "Writing MMC failed\n");
1669 return -1;
1670 }
1671
1672 return 0;
1673}
1674
Shashank Mittal162244e2011-08-08 19:01:25 -07001675void read_device_info_flash(device_info *dev)
1676{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001677 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001678 struct ptentry *ptn;
1679 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001680 if(info == NULL)
1681 {
1682 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1683 ASSERT(0);
1684 }
1685 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001686 ptable = flash_get_ptable();
1687 if (ptable == NULL)
1688 {
1689 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1690 return;
1691 }
1692
1693 ptn = ptable_find(ptable, "devinfo");
1694 if (ptn == NULL)
1695 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001696 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001697 return;
1698 }
1699
1700 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1701 {
1702 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1703 return;
1704 }
1705
1706 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1707 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001708 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1709 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001710 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001711 write_device_info_flash(info);
1712 }
1713 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001714 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001715}
1716
1717void write_device_info(device_info *dev)
1718{
1719 if(target_is_emmc_boot())
1720 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001721 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1722 if(info == NULL)
1723 {
1724 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1725 ASSERT(0);
1726 }
1727 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001728 memcpy(info, dev, sizeof(struct device_info));
1729
1730#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001731 if (is_secure_boot_enable()) {
1732 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1733 ASSERT(0);
1734 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001735 else
1736 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001737#else
1738 write_device_info_mmc(info);
1739#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001740 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001741 }
1742 else
1743 {
1744 write_device_info_flash(dev);
1745 }
1746}
1747
1748void read_device_info(device_info *dev)
1749{
1750 if(target_is_emmc_boot())
1751 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001752 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1753 if(info == NULL)
1754 {
1755 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1756 ASSERT(0);
1757 }
1758 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001759
1760#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001761 if (is_secure_boot_enable()) {
1762 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1763 ASSERT(0);
1764 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001765 else
1766 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001767#else
1768 read_device_info_mmc(info);
1769#endif
1770
1771 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1772 {
1773 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001774 if (is_secure_boot_enable())
1775 info->is_unlocked = 0;
1776 else
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001777 info->is_unlocked = 1;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001778 info->is_tampered = 0;
1779 info->charger_screen_enabled = 0;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001780 write_device_info(info);
1781 }
1782 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001783 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001784 }
1785 else
1786 {
1787 read_device_info_flash(dev);
1788 }
1789}
1790
1791void reset_device_info()
1792{
1793 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001794 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001795 write_device_info(&device);
1796}
1797
1798void set_device_root()
1799{
1800 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001801 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001802 write_device_info(&device);
1803}
1804
Amol Jadicb524072012-08-09 16:40:18 -07001805#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001806int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
Amol Jadicb524072012-08-09 16:40:18 -07001807{
1808 uint32 dt_image_offset = 0;
1809 uint32_t n;
1810 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001811 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001812 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001813 unsigned int compressed_size = 0;
1814 unsigned int dtb_size = 0;
1815 unsigned int out_avai_len = 0;
1816 unsigned char *out_addr = NULL;
1817 unsigned char *best_match_dt_addr = NULL;
1818 int rc;
Amol Jadicb524072012-08-09 16:40:18 -07001819
1820 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1821
Amol Jadicb524072012-08-09 16:40:18 -07001822 if(hdr->dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001823 /* add kernel offset */
1824 dt_image_offset += page_size;
1825 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1826 dt_image_offset += n;
1827
1828 /* add ramdisk offset */
1829 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1830 dt_image_offset += n;
1831
1832 /* add second offset */
1833 if(hdr->second_size != 0) {
1834 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1835 dt_image_offset += n;
1836 }
1837
1838 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001839 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001840
Deepa Dinamani19648b42013-09-05 17:05:55 -07001841 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001842 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1843 return -1;
1844 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001845 /* Find index of device tree within device tree table */
1846 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001847 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1848 return -1;
1849 }
1850
Matthew Qin271927e2015-03-31 22:07:07 -04001851 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
1852 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
1853 {
1854 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
1855 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04001856 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001857 rc = decompress(best_match_dt_addr,
1858 dt_entry.size, out_addr, out_avai_len,
1859 &compressed_size, &dtb_size);
1860 if (rc)
1861 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001862 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001863 ASSERT(0);
1864 }
1865
Matthew Qin0b15b322015-05-19 05:20:54 -04001866 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001867 best_match_dt_addr = out_addr;
1868 } else {
1869 dtb_size = dt_entry.size;
1870 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001871 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001872 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001873 {
1874 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1875 return -1;
1876 }
1877
Amol Jadicb524072012-08-09 16:40:18 -07001878 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001879 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001880 } else
1881 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001882
1883 /* Everything looks fine. Return success. */
1884 return 0;
1885}
1886#endif
1887
Brian Swetland9c4c0752009-01-25 16:23:50 -08001888void cmd_boot(const char *arg, void *data, unsigned sz)
1889{
1890 unsigned kernel_actual;
1891 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001892 uint32_t image_actual;
1893 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001894 uint32_t sig_actual = SIGNATURE_SIZE;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001895 struct boot_img_hdr *hdr = NULL;
1896 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001897 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001898 int ret = 0;
1899 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001900 unsigned int out_len = 0;
1901 unsigned int out_avai_len = 0;
1902 unsigned char *out_addr = NULL;
1903 uint32_t dtb_offset = 0;
1904 unsigned char *kernel_start_addr = NULL;
1905 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04001906 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001907
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001908#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07001909 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001910 {
1911 fastboot_fail("unlock device to use this command");
1912 return;
1913 }
1914#endif
1915
Brian Swetland9c4c0752009-01-25 16:23:50 -08001916 if (sz < sizeof(hdr)) {
1917 fastboot_fail("invalid bootimage header");
1918 return;
1919 }
1920
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001921 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001922
1923 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001924 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001925
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001926 if(target_is_emmc_boot() && hdr->page_size) {
1927 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001928 page_mask = page_size - 1;
1929 }
1930
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001931 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1932 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001933#if DEVICE_TREE
1934 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1935#endif
1936
1937 image_actual = ADD_OF(page_size, kernel_actual);
1938 image_actual = ADD_OF(image_actual, ramdisk_actual);
1939 image_actual = ADD_OF(image_actual, dt_actual);
1940
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001941 if (target_use_signed_kernel() && (!device.is_unlocked))
1942 image_actual = ADD_OF(image_actual, sig_actual);
1943
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001944 /* sz should have atleast raw boot image */
1945 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001946 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001947 return;
1948 }
1949
1950 /* Verify the boot image
1951 * device & page_size are initialized in aboot_init
1952 */
1953 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001954 /* Pass size excluding signature size, otherwise we would try to
1955 * access signature beyond its length
1956 */
1957 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001958
Amit Blay4aa292f2015-04-28 21:55:59 +03001959#ifdef MDTP_SUPPORT
1960 else
1961 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03001962 /* fastboot boot is not allowed when MDTP is activated */
1963
Amit Blay4aa292f2015-04-28 21:55:59 +03001964 mdtp_ext_partition_verification_t ext_partition;
1965 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1966 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1967 ext_partition.page_size = page_size;
1968 ext_partition.image_addr = (uint32_t)data;
1969 ext_partition.image_size = image_actual - sig_actual;
1970 ext_partition.sig_avail = TRUE;
1971 mdtp_fwlock_verify_lock(&ext_partition);
1972 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03001973
1974 bool is_mdtp_activated = 0;
1975 mdtp_activated(&is_mdtp_activated);
1976 if(is_mdtp_activated){
1977 dprintf(CRITICAL, "fastboot boot command is not available.\n");
1978 return;
1979 }
Amit Blay4aa292f2015-04-28 21:55:59 +03001980#endif /* MDTP_SUPPORT */
1981
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001982 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001983 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1984 * If not, continue booting.
1985 */
1986 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
1987 {
1988 out_addr = (unsigned char *)target_get_scratch_address();
1989 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
1990 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001991 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001992 ret = decompress((unsigned char *)(ptr + page_size),
1993 hdr->kernel_size, out_addr, out_avai_len,
1994 &dtb_offset, &out_len);
1995 if (ret)
1996 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001997 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001998 ASSERT(0);
1999 }
2000
Matthew Qin0b15b322015-05-19 05:20:54 -04002001 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002002 kptr = (struct kernel64_hdr *)out_addr;
2003 kernel_start_addr = out_addr;
2004 kernel_size = out_len;
2005 } else {
2006 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2007 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2008 kernel_size = hdr->kernel_size;
2009 }
2010
2011 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002012 * Update the kernel/ramdisk/tags address if the boot image header
2013 * has default values, these default values come from mkbootimg when
2014 * the boot image is flashed using fastboot flash:raw
2015 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002016 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002017
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002018 /* Get virtual addresses since the hdr saves physical addresses. */
2019 hdr->kernel_addr = VA(hdr->kernel_addr);
2020 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2021 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002022
Matthew Qinbb7923d2015-02-09 10:56:09 +08002023 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002024 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002025 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002026 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2027 {
2028 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002029 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002030 }
2031
Amol Jadicb524072012-08-09 16:40:18 -07002032#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002033 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002034 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002035 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002036
2037 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002038#else
2039 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2040 {
2041 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002042 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002043 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002044#endif
2045
2046 /* Load ramdisk & kernel */
2047 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002048 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002049
2050#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002051 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2052 {
2053 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2054 return;
2055 }
2056
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002057 /*
2058 * If dtb is not found look for appended DTB in the kernel.
2059 * If appended dev tree is found, update the atags with
2060 * memory address to the DTB appended location on RAM.
2061 * Else update with the atags address in the kernel header
2062 */
2063 if (!dtb_copied) {
2064 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002065 dtb = dev_tree_appended((void*)(ptr + page_size),
2066 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002067 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002068 if (!dtb) {
2069 fastboot_fail("dtb not found");
2070 return;
2071 }
Amol Jadicb524072012-08-09 16:40:18 -07002072 }
2073#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002074
2075 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002076 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002077
Dima Zavin77e41f32013-03-06 16:10:43 -08002078 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002079 (const char*) hdr->cmdline, board_machtype(),
2080 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002081}
2082
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002083void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002084{
2085 struct ptentry *ptn;
2086 struct ptable *ptable;
2087
2088 ptable = flash_get_ptable();
2089 if (ptable == NULL) {
2090 fastboot_fail("partition table doesn't exist");
2091 return;
2092 }
2093
2094 ptn = ptable_find(ptable, arg);
2095 if (ptn == NULL) {
2096 fastboot_fail("unknown partition name");
2097 return;
2098 }
2099
2100 if (flash_erase(ptn)) {
2101 fastboot_fail("failed to erase partition");
2102 return;
2103 }
2104 fastboot_okay("");
2105}
2106
Bikas Gurungd48bd242010-09-04 19:54:32 -07002107
2108void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2109{
2110 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002111 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002112 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002113 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002114
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002115#if VERIFIED_BOOT
2116 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2117 {
2118 if(!device.is_unlocked)
2119 {
2120 fastboot_fail("unlock device to erase keystore");
2121 return;
2122 }
2123 }
2124#endif
2125
Kinson Chikf1a43512011-07-14 11:28:39 -07002126 index = partition_get_index(arg);
2127 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002128 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002129
Kinson Chikf1a43512011-07-14 11:28:39 -07002130 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002131 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002132 return;
2133 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002134
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002135 lun = partition_get_lun(index);
2136 mmc_set_lun(lun);
2137
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002138 if (platform_boot_dev_isemmc())
2139 {
2140 if (mmc_erase_card(ptn, size)) {
2141 fastboot_fail("failed to erase partition\n");
2142 return;
2143 }
2144 } else {
2145 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2146 size = partition_get_size(index);
2147 if (size > DEFAULT_ERASE_SIZE)
2148 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002149
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002150 /* Simple inefficient version of erase. Just writing
2151 0 in first several blocks */
2152 if (mmc_write(ptn , size, (unsigned int *)out)) {
2153 fastboot_fail("failed to erase partition");
2154 return;
2155 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002156 }
2157 fastboot_okay("");
2158}
2159
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002160void cmd_erase(const char *arg, void *data, unsigned sz)
2161{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002162#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002163 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002164 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002165 if(!device.is_unlocked && !device.is_verified)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002166 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002167 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002168 return;
2169 }
Channagoud Kadabi35297672015-06-13 11:09:49 -07002170 if(!device.is_unlocked && device.is_verified)
2171 {
2172 if(!boot_verify_flash_allowed(arg))
2173 {
2174 fastboot_fail("cannot flash this partition in verified state");
2175 return;
2176 }
2177 }
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002178 }
2179#endif
2180
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002181 if(target_is_emmc_boot())
2182 cmd_erase_mmc(arg, data, sz);
2183 else
2184 cmd_erase_nand(arg, data, sz);
2185}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002186
Ajay Dudani5c761132011-04-07 20:19:04 -07002187void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002188{
2189 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002190 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002191 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002192 char *token = NULL;
2193 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002194 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002195 uint8_t lun = 0;
2196 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002197
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002198 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002199 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002200 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002201 if(token)
2202 {
2203 lun = atoi(token);
2204 mmc_set_lun(lun);
2205 lun_set = true;
2206 }
2207
Mao Jinlong226f33a2014-07-04 17:24:10 +08002208 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002209 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002210 if (!strcmp(pname, "partition"))
2211 {
2212 dprintf(INFO, "Attempt to write partition image.\n");
2213 if (write_partition(sz, (unsigned char *) data)) {
2214 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002215 return;
2216 }
2217 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002218 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002219 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002220#if VERIFIED_BOOT
2221 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2222 {
2223 if(!device.is_unlocked)
2224 {
2225 fastboot_fail("unlock device to flash keystore");
2226 return;
2227 }
2228 if(!boot_verify_validate_keystore((unsigned char *)data))
2229 {
2230 fastboot_fail("image is not a keystore file");
2231 return;
2232 }
2233 }
2234#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002235 index = partition_get_index(pname);
2236 ptn = partition_get_offset(index);
2237 if(ptn == 0) {
2238 fastboot_fail("partition table doesn't exist");
2239 return;
2240 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002241
Mao Jinlong226f33a2014-07-04 17:24:10 +08002242 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2243 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2244 fastboot_fail("image is not a boot image");
2245 return;
2246 }
2247 }
2248
2249 if(!lun_set)
2250 {
2251 lun = partition_get_lun(index);
2252 mmc_set_lun(lun);
2253 }
2254
2255 size = partition_get_size(index);
2256 if (ROUND_TO_PAGE(sz,511) > size) {
2257 fastboot_fail("size too large");
2258 return;
2259 }
2260 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2261 fastboot_fail("flash write failure");
2262 return;
2263 }
Greg Grisco6e754772011-06-23 12:19:39 -07002264 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002265 }
2266 fastboot_okay("");
2267 return;
2268}
2269
Ajay Dudanide984792015-03-02 09:57:41 -08002270void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2271{
2272 int i, images;
2273 meta_header_t *meta_header;
2274 img_header_entry_t *img_header_entry;
2275
2276 meta_header = (meta_header_t*) data;
2277 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2278
2279 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2280
2281 for (i=0; i<images; i++) {
2282
2283 if((img_header_entry[i].ptn_name == NULL) ||
2284 (img_header_entry[i].start_offset == 0) ||
2285 (img_header_entry[i].size == 0))
2286 break;
2287
2288 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2289 (void *) data + img_header_entry[i].start_offset,
2290 img_header_entry[i].size);
2291 }
2292
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002293 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2294 {
2295 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2296 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2297 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2298 }
2299 else
2300 {
2301 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2302 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2303 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2304 }
2305
2306 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002307 fastboot_okay("");
2308 return;
2309}
2310
Ajay Dudani5c761132011-04-07 20:19:04 -07002311void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2312{
2313 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002314 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002315 uint32_t *fill_buf = NULL;
2316 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002317 sparse_header_t *sparse_header;
2318 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002319 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002320 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302321 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002322 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302323 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002324 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302325 /*End of the sparse image address*/
2326 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002327
Kinson Chikf1a43512011-07-14 11:28:39 -07002328 index = partition_get_index(arg);
2329 ptn = partition_get_offset(index);
2330 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002331 fastboot_fail("partition table doesn't exist");
2332 return;
2333 }
2334
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302335 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302336
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002337 lun = partition_get_lun(index);
2338 mmc_set_lun(lun);
2339
vijay kumar800255e2015-04-24 20:26:19 +05302340 if (sz < sizeof(sparse_header_t)) {
2341 fastboot_fail("size too low");
2342 return;
2343 }
2344
Ajay Dudani5c761132011-04-07 20:19:04 -07002345 /* Read and skip over sparse image header */
2346 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302347
vijay kumar1321f342015-03-27 12:13:42 +05302348 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002349 fastboot_fail("size too large");
2350 return;
2351 }
2352
vijay kumarde4fcf62015-04-23 13:05:49 +05302353 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302354
2355 if (data_end < (uint32_t)data) {
2356 fastboot_fail("buffer overreads occured due to invalid sparse header");
2357 return;
2358 }
2359
vijay kumarde4fcf62015-04-23 13:05:49 +05302360 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002361 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302362 fastboot_fail("sparse header size mismatch");
2363 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002364 }
2365
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002366 dprintf (SPEW, "=== Sparse Image Header ===\n");
2367 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2368 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2369 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2370 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2371 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2372 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2373 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2374 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002375
2376 /* Start processing chunks */
2377 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2378 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302379 /* Make sure the total image size does not exceed the partition size */
2380 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2381 fastboot_fail("size too large");
2382 return;
2383 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002384 /* Read and skip over chunk header */
2385 chunk_header = (chunk_header_t *) data;
2386 data += sizeof(chunk_header_t);
2387
vijay kumar800255e2015-04-24 20:26:19 +05302388 if (data_end < (uint32_t)data) {
2389 fastboot_fail("buffer overreads occured due to invalid sparse header");
2390 return;
2391 }
2392
Ajay Dudani5c761132011-04-07 20:19:04 -07002393 dprintf (SPEW, "=== Chunk Header ===\n");
2394 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2395 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2396 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2397
vijay kumar800255e2015-04-24 20:26:19 +05302398 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002399 {
vijay kumar800255e2015-04-24 20:26:19 +05302400 fastboot_fail("chunk header size mismatch");
2401 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002402 }
2403
wufeng.jiang813dc352015-06-02 23:02:46 -04002404 if (!sparse_header->blk_sz ){
2405 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302406 return;
2407 }
2408
wufeng.jiang813dc352015-06-02 23:02:46 -04002409 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2410
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302411 /* Make sure that the chunk size calculated from sparse image does not
2412 * exceed partition size
2413 */
2414 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2415 {
2416 fastboot_fail("Chunk data size exceeds partition size");
2417 return;
2418 }
2419
Ajay Dudani5c761132011-04-07 20:19:04 -07002420 switch (chunk_header->chunk_type)
2421 {
2422 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002423 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002424 chunk_data_sz))
2425 {
2426 fastboot_fail("Bogus chunk size for chunk type Raw");
2427 return;
2428 }
2429
vijay kumar800255e2015-04-24 20:26:19 +05302430 if (data_end < (uint32_t)data + chunk_data_sz) {
2431 fastboot_fail("buffer overreads occured due to invalid sparse header");
2432 return;
2433 }
2434
wufeng.jiang813dc352015-06-02 23:02:46 -04002435 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2436 otherwise it will return in the line above
2437 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002438 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002439 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002440 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002441 {
2442 fastboot_fail("flash write failure");
2443 return;
2444 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302445 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2446 fastboot_fail("Bogus size for RAW chunk type");
2447 return;
2448 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002449 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002450 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002451 break;
2452
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002453 case CHUNK_TYPE_FILL:
2454 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2455 sizeof(uint32_t)))
2456 {
2457 fastboot_fail("Bogus chunk size for chunk type FILL");
2458 return;
2459 }
2460
2461 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2462 if (!fill_buf)
2463 {
2464 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2465 return;
2466 }
2467
vijay kumar800255e2015-04-24 20:26:19 +05302468 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2469 fastboot_fail("buffer overreads occured due to invalid sparse header");
2470 return;
2471 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002472 fill_val = *(uint32_t *)data;
2473 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002474
2475 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2476 {
2477 fill_buf[i] = fill_val;
2478 }
2479
wufeng.jiang813dc352015-06-02 23:02:46 -04002480 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002481 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302482 /* Make sure that the data written to partition does not exceed partition size */
2483 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2484 {
2485 fastboot_fail("Chunk data size for fill type exceeds partition size");
2486 return;
2487 }
2488
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002489 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2490 sparse_header->blk_sz,
2491 fill_buf))
2492 {
2493 fastboot_fail("flash write failure");
2494 free(fill_buf);
2495 return;
2496 }
2497
2498 total_blocks++;
2499 }
2500
2501 free(fill_buf);
2502 break;
2503
Ajay Dudani5c761132011-04-07 20:19:04 -07002504 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302505 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2506 fastboot_fail("bogus size for chunk DONT CARE type");
2507 return;
2508 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002509 total_blocks += chunk_header->chunk_sz;
2510 break;
2511
Ajay Dudani5c761132011-04-07 20:19:04 -07002512 case CHUNK_TYPE_CRC:
2513 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2514 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002515 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002516 return;
2517 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302518 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2519 fastboot_fail("bogus size for chunk CRC type");
2520 return;
2521 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002522 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302523 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2524 fastboot_fail("integer overflow occured");
2525 return;
2526 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002527 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302528 if (data_end < (uint32_t)data) {
2529 fastboot_fail("buffer overreads occured due to invalid sparse header");
2530 return;
2531 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002532 break;
2533
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002534 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002535 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002536 fastboot_fail("Unknown chunk type");
2537 return;
2538 }
2539 }
2540
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002541 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2542 total_blocks, sparse_header->total_blks);
2543
2544 if(total_blocks != sparse_header->total_blks)
2545 {
2546 fastboot_fail("sparse image write failure");
2547 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002548
2549 fastboot_okay("");
2550 return;
2551}
2552
2553void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2554{
2555 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002556 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002557
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002558#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002559 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2560 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002561 int ret=0;
2562 uint32 major_version=0;
2563 uint32 minor_version=0;
2564
2565 ret = scm_svc_version(&major_version,&minor_version);
2566 if(!ret)
2567 {
2568 if(major_version >= 2)
2569 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002570 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002571 {
2572 ret = encrypt_scm((uint32 **) &data, &sz);
2573 if (ret != 0) {
2574 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2575 return;
2576 }
2577
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002578 /* Protect only for SSD */
2579 if (!strcmp(arg, "ssd")) {
2580 ret = scm_protect_keystore((uint32 *) data, sz);
2581 if (ret != 0) {
2582 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2583 return;
2584 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002585 }
2586 }
2587 else
2588 {
2589 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2590 if(ret != 0)
2591 {
2592 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2593 return;
2594 }
2595 }
2596 }
2597 else
2598 {
2599 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2600 magic_number[1] == DECRYPT_MAGIC_1)
2601 {
2602 ret = decrypt_scm((uint32 **) &data, &sz);
2603 if (ret != 0) {
2604 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2605 return;
2606 }
2607 }
2608 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2609 magic_number[1] == ENCRYPT_MAGIC_1)
2610 {
2611 ret = encrypt_scm((uint32 **) &data, &sz);
2612 if (ret != 0) {
2613 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2614 return;
2615 }
2616 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002617 }
2618 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002619 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002620 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002621 dprintf(CRITICAL,"INVALID SVC Version\n");
2622 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002623 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002624#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002625
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002626#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002627 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002628 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002629 if(!device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002630 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002631 fastboot_fail("device is locked. Cannot flash images");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002632 return;
2633 }
Channagoud Kadabi35297672015-06-13 11:09:49 -07002634 if(!device.is_unlocked && device.is_verified)
2635 {
2636 if(!boot_verify_flash_allowed(arg))
2637 {
2638 fastboot_fail("cannot flash this partition in verified state");
2639 return;
2640 }
2641 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002642 }
2643#endif
2644
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002645 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002646 meta_header = (meta_header_t *) data;
2647 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002648 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002649 else if (meta_header->magic == META_HEADER_MAGIC)
2650 cmd_flash_meta_img(arg, data, sz);
2651 else
2652 cmd_flash_mmc_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002653 return;
2654}
2655
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002656void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2657{
2658 struct ptentry *sys_ptn;
2659 struct ptable *ptable;
2660
2661 ptable = flash_get_ptable();
2662 if (ptable == NULL) {
2663 fastboot_fail("partition table doesn't exist");
2664 return;
2665 }
2666
2667 sys_ptn = ptable_find(ptable, "system");
2668 if (sys_ptn == NULL) {
2669 fastboot_fail("system partition not found");
2670 return;
2671 }
2672
2673 sz = ROUND_TO_PAGE(sz, page_mask);
2674 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2675 fastboot_fail("update_ubi_vol failed");
2676 else
2677 fastboot_okay("");
2678}
2679
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002680void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002681{
2682 struct ptentry *ptn;
2683 struct ptable *ptable;
2684 unsigned extra = 0;
2685
2686 ptable = flash_get_ptable();
2687 if (ptable == NULL) {
2688 fastboot_fail("partition table doesn't exist");
2689 return;
2690 }
2691
2692 ptn = ptable_find(ptable, arg);
2693 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002694 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2695 arg);
2696 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002697 return;
2698 }
2699
2700 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2701 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2702 fastboot_fail("image is not a boot image");
2703 return;
2704 }
2705 }
2706
Amol Jadi5c61a952012-05-04 17:05:35 -07002707 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002708 || !strcmp(ptn->name, "userdata")
2709 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002710 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002711 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002712 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002713 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002714 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002715
2716 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002717 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2718 if (flash_ubi_img(ptn, data, sz)) {
2719 fastboot_fail("flash write failure");
2720 return;
2721 }
2722 } else {
2723 if (flash_write(ptn, extra, data, sz)) {
2724 fastboot_fail("flash write failure");
2725 return;
2726 }
Dima Zavin214cc642009-01-26 11:16:21 -08002727 }
2728 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2729 fastboot_okay("");
2730}
2731
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002732void cmd_flash(const char *arg, void *data, unsigned sz)
2733{
2734 if(target_is_emmc_boot())
2735 cmd_flash_mmc(arg, data, sz);
2736 else
2737 cmd_flash_nand(arg, data, sz);
2738}
2739
Dima Zavin214cc642009-01-26 11:16:21 -08002740void cmd_continue(const char *arg, void *data, unsigned sz)
2741{
2742 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002743 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002744
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002745 if (target_is_emmc_boot())
2746 {
2747 boot_linux_from_mmc();
2748 }
2749 else
2750 {
2751 boot_linux_from_flash();
2752 }
Dima Zavin214cc642009-01-26 11:16:21 -08002753}
2754
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002755void cmd_reboot(const char *arg, void *data, unsigned sz)
2756{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002757 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002758 fastboot_okay("");
2759 reboot_device(0);
2760}
2761
2762void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2763{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002764 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002765 fastboot_okay("");
2766 reboot_device(FASTBOOT_MODE);
2767}
2768
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002769void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2770{
2771 dprintf(INFO, "Enabling charger screen check\n");
2772 device.charger_screen_enabled = 1;
2773 write_device_info(&device);
2774 fastboot_okay("");
2775}
2776
2777void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2778{
2779 dprintf(INFO, "Disabling charger screen check\n");
2780 device.charger_screen_enabled = 0;
2781 write_device_info(&device);
2782 fastboot_okay("");
2783}
2784
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302785void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2786{
2787 dprintf(INFO, "Selecting display panel %s\n", arg);
2788 if (arg)
2789 strlcpy(device.display_panel, arg,
2790 sizeof(device.display_panel));
2791 write_device_info(&device);
2792 fastboot_okay("");
2793}
2794
Shashank Mittal162244e2011-08-08 19:01:25 -07002795void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2796{
vijay kumarc65876c2015-04-24 13:29:16 +05302797 if(!is_allow_unlock) {
2798 fastboot_fail("oem unlock is not allowed");
2799 return;
2800 }
2801
2802 display_fbcon_message("Oem Unlock requested");
2803 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2804}
2805
2806void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
2807{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002808 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002809 {
vijay kumarc65876c2015-04-24 13:29:16 +05302810 if(!is_allow_unlock) {
2811 fastboot_fail("oem unlock is not allowed");
2812 return;
2813 }
2814
Shashank Mittal162244e2011-08-08 19:01:25 -07002815 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002816 device.is_verified = 0;
2817 write_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05302818
2819 struct recovery_message msg;
2820 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2821 write_misc(0, &msg, sizeof(msg));
2822
2823 fastboot_okay("");
2824 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002825 }
2826 fastboot_okay("");
2827}
2828
2829void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2830{
2831 /* TODO: Wipe user data */
2832 if(device.is_unlocked || device.is_verified)
2833 {
2834 device.is_unlocked = 0;
2835 device.is_verified = 0;
2836 write_device_info(&device);
2837 }
2838 fastboot_okay("");
2839}
2840
2841void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2842{
2843 /* TODO: Wipe user data */
2844 if(device.is_unlocked || !device.is_verified)
2845 {
2846 device.is_unlocked = 0;
2847 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002848 write_device_info(&device);
2849 }
2850 fastboot_okay("");
2851}
2852
Shashank Mittala0032282011-08-26 14:50:11 -07002853void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2854{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302855 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002856 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002857 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002858 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2859 fastboot_info(response);
2860 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002861 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302862 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2863 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002864 fastboot_okay("");
2865}
2866
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002867void cmd_preflash(const char *arg, void *data, unsigned sz)
2868{
2869 fastboot_okay("");
2870}
2871
Mao Flynn7b379f32015-04-20 00:28:30 +08002872static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302873
Mao Flynn7b379f32015-04-20 00:28:30 +08002874int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302875{
Mao Flynn7b379f32015-04-20 00:28:30 +08002876 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302877 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08002878 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302879 return -1;
2880 return 0;
2881}
2882
Mao Flynn7b379f32015-04-20 00:28:30 +08002883int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002884{
2885 struct ptentry *ptn;
2886 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08002887 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002888 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002889
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302890 ptable = flash_get_ptable();
2891 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002892 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2893 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302894 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002895
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302896 ptn = ptable_find(ptable, "splash");
2897 if (ptn == NULL) {
2898 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002899 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302900 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002901 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302902 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002903 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302904 }
2905
Mao Flynn7b379f32015-04-20 00:28:30 +08002906 header = (struct logo_img_header *)logo_header;
2907 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302908 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002909 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302910 }
2911
2912 fb_display = fbcon_display();
2913 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002914 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
2915 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
2916
2917 /* if the logo is full-screen size, remove "fbcon_clear()" */
2918 if ((header->width != fb_display->width)
2919 || (header->height != fb_display->height))
2920 fbcon_clear();
2921
2922 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
2923 (uint32_t *)base,
2924 (header->blocks * 512))) {
2925 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
2926 return -1;
2927 }
2928 fbcon_extract_to_screen(header, base);
2929 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002930 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002931
2932 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
2933 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
2934 return -1;
2935 }
2936
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302937 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08002938 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
2939 (uint32_t *)base,
2940 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302941 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302942 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002943 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002944 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302945 }
2946
Mao Flynn7b379f32015-04-20 00:28:30 +08002947 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302948}
2949
Mao Flynn7b379f32015-04-20 00:28:30 +08002950int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302951{
2952 int index = INVALID_PTN;
2953 unsigned long long ptn = 0;
2954 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08002955 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08002956 uint32_t blocksize, realsize, readsize;
2957 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302958
2959 index = partition_get_index("splash");
2960 if (index == 0) {
2961 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002962 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302963 }
2964
2965 ptn = partition_get_offset(index);
2966 if (ptn == 0) {
2967 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002968 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302969 }
2970
Mao Flynn1893a7f2015-06-03 12:03:36 +08002971 mmc_set_lun(partition_get_lun(index));
2972
2973 blocksize = mmc_get_device_blocksize();
2974 if (blocksize == 0) {
2975 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
2976 return -1;
2977 }
2978
2979 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07002980 if (!fb_display)
2981 {
2982 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
2983 return -1;
2984 }
2985
Mao Flynn1893a7f2015-06-03 12:03:36 +08002986 base = (uint8_t *) fb_display->base;
2987
2988 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302989 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002990 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302991 }
2992
Mao Flynn1893a7f2015-06-03 12:03:36 +08002993 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08002994 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302995 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002996 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302997 }
2998
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302999 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003000 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3001 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003002
Mao Flynn1893a7f2015-06-03 12:03:36 +08003003 realsize = header->blocks * 512;
3004 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3005
3006 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003007 if ((header->width != fb_display->width)
3008 || (header->height != fb_display->height))
3009 fbcon_clear();
3010
Mao Flynn1893a7f2015-06-03 12:03:36 +08003011 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003012 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3013 return -1;
3014 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003015
Mao Flynn1893a7f2015-06-03 12:03:36 +08003016 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3017 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303018
Mao Flynn1893a7f2015-06-03 12:03:36 +08003019 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3020 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3021 return -1;
3022 }
3023
3024 realsize = header->width * header->height * fb_display->bpp / 8;
3025 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3026
3027 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3028 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3029 fbcon_clear();
3030 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3031 return -1;
3032 }
3033 } else {
3034 if (mmc_read(ptn + blocksize ,
3035 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3036 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3037 return -1;
3038 }
3039 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3040 }
3041 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303042 }
3043
Mao Flynn7b379f32015-04-20 00:28:30 +08003044 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303045}
3046
Mao Flynn7b379f32015-04-20 00:28:30 +08003047int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303048{
3049 if (target_is_emmc_boot()) {
3050 return splash_screen_mmc();
3051 } else {
3052 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003053 }
3054}
3055
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003056/* Get the size from partiton name */
3057static void get_partition_size(const char *arg, char *response)
3058{
3059 uint64_t ptn = 0;
3060 uint64_t size;
3061 int index = INVALID_PTN;
3062
3063 index = partition_get_index(arg);
3064
3065 if (index == INVALID_PTN)
3066 {
3067 dprintf(CRITICAL, "Invalid partition index\n");
3068 return;
3069 }
3070
3071 ptn = partition_get_offset(index);
3072
3073 if(!ptn)
3074 {
3075 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3076 return;
3077 }
3078
3079 size = partition_get_size(index);
3080
3081 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3082 return;
3083}
3084
3085/*
3086 * Publish the partition type & size info
3087 * fastboot getvar will publish the required information.
3088 * fastboot getvar partition_size:<partition_name>: partition size in hex
3089 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3090 */
3091static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3092{
3093 uint8_t i;
3094
3095 for (i = 0; i < num_parts; i++) {
3096 get_partition_size(info[i].part_name, info[i].size_response);
3097
3098 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3099 {
3100 dprintf(CRITICAL, "partition size name truncated\n");
3101 return;
3102 }
3103 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3104 {
3105 dprintf(CRITICAL, "partition type name truncated\n");
3106 return;
3107 }
3108
3109 /* publish partition size & type info */
3110 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3111 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3112 }
3113}
3114
Amol Jadi5edf3552013-07-23 14:15:34 -07003115/* register commands and variables for fastboot */
3116void aboot_fastboot_register_commands(void)
3117{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003118 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07003119
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003120 struct fastboot_cmd_desc cmd_list[] = {
3121 /* By default the enabled list is empty. */
3122 {"", NULL},
3123 /* move commands enclosed within the below ifndef to here
3124 * if they need to be enabled in user build.
3125 */
3126#ifndef DISABLE_FASTBOOT_CMDS
3127 /* Register the following commands only for non-user builds */
3128 {"flash:", cmd_flash},
3129 {"erase:", cmd_erase},
3130 {"boot", cmd_boot},
3131 {"continue", cmd_continue},
3132 {"reboot", cmd_reboot},
3133 {"reboot-bootloader", cmd_reboot_bootloader},
3134 {"oem unlock", cmd_oem_unlock},
vijay kumarc65876c2015-04-24 13:29:16 +05303135 {"oem unlock-go", cmd_oem_unlock_go},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003136 {"oem lock", cmd_oem_lock},
3137 {"oem verified", cmd_oem_verified},
3138 {"oem device-info", cmd_oem_devinfo},
3139 {"preflash", cmd_preflash},
3140 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3141 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08003142 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003143#endif
3144 };
3145
3146 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3147 for (i = 1; i < fastboot_cmds_count; i++)
3148 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3149
Amol Jadi5edf3552013-07-23 14:15:34 -07003150 /* publish variables and their values */
3151 fastboot_publish("product", TARGET(BOARD));
3152 fastboot_publish("kernel", "lk");
3153 fastboot_publish("serialno", sn_buf);
3154
3155 /*
3156 * partition info is supported only for emmc partitions
3157 * Calling this for NAND prints some error messages which
3158 * is harmless but misleading. Avoid calling this for NAND
3159 * devices.
3160 */
3161 if (target_is_emmc_boot())
3162 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3163
3164 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003165 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3166 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003167 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003168 /* Is the charger screen check enabled */
3169 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3170 device.charger_screen_enabled);
3171 fastboot_publish("charger-screen-enabled",
3172 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303173 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3174 device.display_panel);
3175 fastboot_publish("display-panel",
3176 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003177 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3178 fastboot_publish("version-baseband", (const char *) device.radio_version);
Amol Jadi5edf3552013-07-23 14:15:34 -07003179}
3180
Brian Swetland9c4c0752009-01-25 16:23:50 -08003181void aboot_init(const struct app_descriptor *app)
3182{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003183 unsigned reboot_mode = 0;
lijuang72875802015-02-10 10:35:12 +08003184 unsigned hard_reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03003185 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003186
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003187 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003188 if (target_is_emmc_boot())
3189 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003190 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003191 page_mask = page_size - 1;
3192 }
3193 else
3194 {
3195 page_size = flash_page_size();
3196 page_mask = page_size - 1;
3197 }
3198
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003199 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3200
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003201 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303202 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003203
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003204 /* Display splash screen if enabled */
3205#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003206#if NO_ALARM_DISPLAY
3207 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003208#endif
lijuang99c02d82015-02-13 19:04:34 +08003209 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003210#if ENABLE_WBC
3211 if (!pm_appsbl_display_init_done())
3212 target_display_init(device.display_panel);
3213 else
3214 display_image_on_screen();
3215#else
lijuang99c02d82015-02-13 19:04:34 +08003216 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003217#endif
lijuang99c02d82015-02-13 19:04:34 +08003218 dprintf(SPEW, "Display Init: Done\n");
3219#if NO_ALARM_DISPLAY
3220 }
3221#endif
3222#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003223
Greg Griscod6250552011-06-29 14:40:23 -07003224 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003225 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003226
Dhaval Patel223ec952013-07-18 14:49:44 -07003227 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3228
Matthew Qindefd5562014-07-11 18:02:40 +08003229 /*
3230 * Check power off reason if user force reset,
3231 * if yes phone will do normal boot.
3232 */
3233 if (is_user_force_reset())
3234 goto normal_boot;
3235
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003236 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003237 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003238 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003239 dprintf(ALWAYS,"dload mode key sequence detected\n");
3240 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003241 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003242 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003243 }
3244 else
3245 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303246 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003247 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003248 }
3249 boot_into_fastboot = true;
3250 }
3251 if (!boot_into_fastboot)
3252 {
3253 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3254 boot_into_recovery = 1;
3255 if (!boot_into_recovery &&
3256 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003257 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003258 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003259 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003260 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003261 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003262 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003263
Ajay Dudani77421292010-10-27 19:34:06 -07003264 reboot_mode = check_reboot_mode();
lijuang72875802015-02-10 10:35:12 +08003265 hard_reboot_mode = check_hard_reboot_mode();
3266 if (reboot_mode == RECOVERY_MODE ||
3267 hard_reboot_mode == RECOVERY_HARD_RESET_MODE) {
Ajay Dudani77421292010-10-27 19:34:06 -07003268 boot_into_recovery = 1;
lijuang72875802015-02-10 10:35:12 +08003269 } else if(reboot_mode == FASTBOOT_MODE ||
3270 hard_reboot_mode == FASTBOOT_HARD_RESET_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003271 boot_into_fastboot = true;
lijuang72875802015-02-10 10:35:12 +08003272 } else if(reboot_mode == ALARM_BOOT ||
3273 hard_reboot_mode == RTC_HARD_RESET_MODE) {
Matthew Qind886f3c2014-01-17 16:52:01 +08003274 boot_reason_alarm = true;
Ajay Dudani77421292010-10-27 19:34:06 -07003275 }
3276
Matthew Qindefd5562014-07-11 18:02:40 +08003277normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003278 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003279 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003280 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003281 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003282 if(emmc_recovery_init())
3283 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3284 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003285 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003286 if((device.is_unlocked) || (device.is_tampered))
3287 {
3288 #ifdef TZ_TAMPER_FUSE
3289 set_tamper_fuse_cmd();
3290 #endif
3291 #if USE_PCOM_SECBOOT
3292 set_tamper_flag(device.is_tampered);
3293 #endif
3294 }
Shashank Mittala0032282011-08-26 14:50:11 -07003295 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003296
Pavel Nedev5d91d412013-04-29 11:34:24 +03003297 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003298 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003299 else
3300 {
3301 recovery_init();
3302 #if USE_PCOM_SECBOOT
3303 if((device.is_unlocked) || (device.is_tampered))
3304 set_tamper_flag(device.is_tampered);
3305 #endif
3306 boot_linux_from_flash();
3307 }
3308 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3309 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003310 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003311
Amol Jadi5edf3552013-07-23 14:15:34 -07003312 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003313
Amol Jadi5edf3552013-07-23 14:15:34 -07003314 /* register aboot specific fastboot commands */
3315 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003316
Amol Jadi5edf3552013-07-23 14:15:34 -07003317 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003318 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003319
3320 /* initialize and start fastboot */
3321 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08003322}
3323
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003324uint32_t get_page_size()
3325{
3326 return page_size;
3327}
3328
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003329/*
3330 * Calculated and save hash (SHA256) for non-signed boot image.
3331 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003332 * @param image_addr - Boot image address
3333 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003334 *
3335 * @return int - 0 on success, negative value on failure.
3336 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003337static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003338{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003339 unsigned int digest[8];
3340#if IMAGE_VERIF_ALGO_SHA1
3341 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3342#else
3343 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3344#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003345
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003346 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003347 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003348
3349 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003350 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003351
3352 return 0;
3353}
3354
Brian Swetland9c4c0752009-01-25 16:23:50 -08003355APP_START(aboot)
3356 .init = aboot_init,
3357APP_END