blob: 759bc2280324967b5f153f8c2e0afd423eba0cf0 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08005 * Copyright (c) 2009-2015, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080060#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053061#include <platform/timer.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080062#if USE_RPMB_FOR_DEVINFO
63#include <rpmb.h>
64#endif
Dima Zavin214cc642009-01-26 11:16:21 -080065
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070066#if ENABLE_WBC
67#include <pm_app_smbchg.h>
68#endif
69
Neeti Desai17379b82012-06-04 18:42:53 -070070#if DEVICE_TREE
71#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070072#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070073#endif
74
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053075#if WDOG_SUPPORT
76#include <wdog.h>
77#endif
78
Channagoud Kadabib6e476b2015-08-05 16:23:42 -070079#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070080#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080081#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080082#include "bootimg.h"
83#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070084#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080085#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070086#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070087#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070088#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070089#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020090#include "mdtp.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070091
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070092extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070093extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070094extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070095extern int get_target_boot_params(const char *cmdline, const char *part,
96 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070097
Sridhar Parasuram7e16d172015-07-05 11:35:23 -070098void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070099void write_device_info_mmc(device_info *dev);
100void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700101static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700102
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700103/* fastboot command function pointer */
104typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
105
106struct fastboot_cmd_desc {
107 char * name;
108 fastboot_cmd_fn cb;
109};
110
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700111#define EXPAND(NAME) #NAME
112#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700113
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800114#ifdef MEMBASE
115#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
116#else
David Ng183a7422009-12-07 14:55:21 -0800117#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800118#endif
119
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700120#ifndef MEMSIZE
121#define MEMSIZE 1024*1024
122#endif
123
124#define MAX_TAGS_SIZE 1024
125
Kun Liang2f1601a2013-08-12 16:29:54 +0800126/* make 4096 as default size to ensure EFS,EXT4's erasing */
127#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800128#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800129
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700130#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700131#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800132
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800133#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
134
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700135#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
136
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800137#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700138static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700139#else
David Ng183a7422009-12-07 14:55:21 -0800140static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700141#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800142static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300143static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800144static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800145static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800146static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700147static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300148static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200149static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800150
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800151static const char *baseband_apq = " androidboot.baseband=apq";
152static const char *baseband_msm = " androidboot.baseband=msm";
153static const char *baseband_csfb = " androidboot.baseband=csfb";
154static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700155static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800156static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700157static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800158static const char *baseband_dsda = " androidboot.baseband=dsda";
159static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800160static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800161static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800162
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700163static unsigned page_size = 0;
164static unsigned page_mask = 0;
165static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
166static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700167static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800168static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700169static bool devinfo_present = true;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700170
Shashank Mittalcd98d472011-08-02 14:29:24 -0700171/* Assuming unauthorized kernel image by default */
172static int auth_kernel_img = 0;
173
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -0800174static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
vijay kumarc65876c2015-04-24 13:29:16 +0530175static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700176
vijay kumarca2e6812015-07-08 20:28:25 +0530177static char frp_ptns[2][8] = {"config","frp"};
178
Dima Zavin42168f22009-01-30 11:52:22 -0800179struct atag_ptbl_entry
180{
181 char name[16];
182 unsigned offset;
183 unsigned size;
184 unsigned flags;
185};
186
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700187/*
188 * Partition info, required to be published
189 * for fastboot
190 */
191struct getvar_partition_info {
192 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
193 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
194 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
195 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
196 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
197};
198
199/*
200 * Right now, we are publishing the info for only
201 * three partitions
202 */
203struct getvar_partition_info part_info[] =
204{
205 { "system" , "partition-size:", "partition-type:", "", "ext4" },
206 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
207 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
208};
209
210char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700211char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800212char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700213char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530214char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700215
Greg Griscod2471ef2011-07-14 13:00:42 -0700216extern int emmc_recovery_init(void);
217
Kinson Chik0b1c8162011-08-31 16:31:57 -0700218#if NO_KEYPAD_DRIVER
219extern int fastboot_trigger(void);
220#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700221
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800222static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700223{
224 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700225#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800226 if (is_arm64)
227 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
228 else
229 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700230 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
231 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700232#endif
233}
234
Dima Zavin42168f22009-01-30 11:52:22 -0800235static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
236{
237 struct atag_ptbl_entry atag_ptn;
238
239 memcpy(atag_ptn.name, ptn->name, 16);
240 atag_ptn.name[15] = '\0';
241 atag_ptn.offset = ptn->start;
242 atag_ptn.size = ptn->length;
243 atag_ptn.flags = ptn->flags;
244 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
245 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
246}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800247
Neeti Desaie245d492012-06-01 12:52:13 -0700248unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800249{
David Ng183a7422009-12-07 14:55:21 -0800250 int cmdline_len = 0;
251 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800252 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700253 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800254 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300255 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700256 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700257 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200258 bool is_mdtp_activated = 0;
259#ifdef MDTP_SUPPORT
260 mdtp_activated(&is_mdtp_activated);
261#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800262
Brian Swetland9c4c0752009-01-25 16:23:50 -0800263 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800264 cmdline_len = strlen(cmdline);
265 have_cmdline = 1;
266 }
267 if (target_is_emmc_boot()) {
268 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800269#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700270 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
271 ASSERT(boot_dev_buf);
272 platform_boot_dev_cmdline(boot_dev_buf);
273 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700274#endif
David Ng183a7422009-12-07 14:55:21 -0800275 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800276
277 cmdline_len += strlen(usb_sn_cmdline);
278 cmdline_len += strlen(sn_buf);
279
Pavel Nedev5614d222013-06-17 18:01:02 +0300280 if (boot_into_recovery && gpt_exists)
281 cmdline_len += strlen(secondary_gpt_enable);
282
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200283 if(is_mdtp_activated)
284 cmdline_len += strlen(mdtp_activated_flag);
285
Pavel Nedev328ac822013-04-05 15:25:11 +0300286 if (boot_into_ffbm) {
287 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700288 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800289 /* reduce kernel console messages to speed-up boot */
290 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800291 } else if (boot_reason_alarm) {
292 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800293 } else if ((target_build_variant_user() || device.charger_screen_enabled)
294 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700295 pause_at_bootup = 1;
296 cmdline_len += strlen(battchg_pause);
297 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800298
Shashank Mittalcd98d472011-08-02 14:29:24 -0700299 if(target_use_signed_kernel() && auth_kernel_img) {
300 cmdline_len += strlen(auth_kernel);
301 }
302
Joonwoo Park61112782013-10-02 19:50:39 -0700303 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
304 "system",
305 target_boot_params,
306 sizeof(target_boot_params)) == 0) {
307 have_target_boot_params = 1;
308 cmdline_len += strlen(target_boot_params);
309 }
310
Ajay Dudanid04110c2011-01-17 23:55:07 -0800311 /* Determine correct androidboot.baseband to use */
312 switch(target_baseband())
313 {
314 case BASEBAND_APQ:
315 cmdline_len += strlen(baseband_apq);
316 break;
317
318 case BASEBAND_MSM:
319 cmdline_len += strlen(baseband_msm);
320 break;
321
322 case BASEBAND_CSFB:
323 cmdline_len += strlen(baseband_csfb);
324 break;
325
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800326 case BASEBAND_SVLTE2A:
327 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800328 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700329
330 case BASEBAND_MDM:
331 cmdline_len += strlen(baseband_mdm);
332 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700333
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800334 case BASEBAND_MDM2:
335 cmdline_len += strlen(baseband_mdm2);
336 break;
337
Amol Jadi5c61a952012-05-04 17:05:35 -0700338 case BASEBAND_SGLTE:
339 cmdline_len += strlen(baseband_sglte);
340 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530341
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800342 case BASEBAND_SGLTE2:
343 cmdline_len += strlen(baseband_sglte2);
344 break;
345
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530346 case BASEBAND_DSDA:
347 cmdline_len += strlen(baseband_dsda);
348 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800349
350 case BASEBAND_DSDA2:
351 cmdline_len += strlen(baseband_dsda2);
352 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800353 }
354
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800355 if (cmdline) {
356 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530357 target_display_panel_node(display_panel_buf,
358 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800359 strlen(display_panel_buf)) {
360 cmdline_len += strlen(display_panel_buf);
361 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700362 }
363
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800364 if (target_warm_boot()) {
365 warm_boot = true;
366 cmdline_len += strlen(warmboot_cmdline);
367 }
368
David Ng183a7422009-12-07 14:55:21 -0800369 if (cmdline_len > 0) {
370 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800371 unsigned char *dst;
372
373 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
374 ASSERT(cmdline_final != NULL);
375 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700376
Amol Jadi168b7712012-03-06 16:15:00 -0800377 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800378 if (have_cmdline) {
379 src = cmdline;
380 while ((*dst++ = *src++));
381 }
382 if (target_is_emmc_boot()) {
383 src = emmc_cmdline;
384 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700385 have_cmdline = 1;
386 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800387#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700388 src = boot_dev_buf;
389 if (have_cmdline) --dst;
390 while ((*dst++ = *src++));
391#endif
David Ngf773dde2010-07-26 19:55:08 -0700392 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800393
394 src = usb_sn_cmdline;
395 if (have_cmdline) --dst;
396 have_cmdline = 1;
397 while ((*dst++ = *src++));
398 src = sn_buf;
399 if (have_cmdline) --dst;
400 have_cmdline = 1;
401 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800402 if (warm_boot) {
403 if (have_cmdline) --dst;
404 src = warmboot_cmdline;
405 while ((*dst++ = *src++));
406 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800407
Pavel Nedev5614d222013-06-17 18:01:02 +0300408 if (boot_into_recovery && gpt_exists) {
409 src = secondary_gpt_enable;
410 if (have_cmdline) --dst;
411 while ((*dst++ = *src++));
412 }
413
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200414 if (is_mdtp_activated) {
415 src = mdtp_activated_flag;
416 if (have_cmdline) --dst;
417 while ((*dst++ = *src++));
418 }
419
Pavel Nedev328ac822013-04-05 15:25:11 +0300420 if (boot_into_ffbm) {
421 src = androidboot_mode;
422 if (have_cmdline) --dst;
423 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700424 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300425 if (have_cmdline) --dst;
426 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800427 src = loglevel;
428 if (have_cmdline) --dst;
429 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800430 } else if (boot_reason_alarm) {
431 src = alarmboot_cmdline;
432 if (have_cmdline) --dst;
433 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300434 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700435 src = battchg_pause;
436 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800437 while ((*dst++ = *src++));
438 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800439
Shashank Mittalcd98d472011-08-02 14:29:24 -0700440 if(target_use_signed_kernel() && auth_kernel_img) {
441 src = auth_kernel;
442 if (have_cmdline) --dst;
443 while ((*dst++ = *src++));
444 }
445
Ajay Dudanid04110c2011-01-17 23:55:07 -0800446 switch(target_baseband())
447 {
448 case BASEBAND_APQ:
449 src = baseband_apq;
450 if (have_cmdline) --dst;
451 while ((*dst++ = *src++));
452 break;
453
454 case BASEBAND_MSM:
455 src = baseband_msm;
456 if (have_cmdline) --dst;
457 while ((*dst++ = *src++));
458 break;
459
460 case BASEBAND_CSFB:
461 src = baseband_csfb;
462 if (have_cmdline) --dst;
463 while ((*dst++ = *src++));
464 break;
465
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800466 case BASEBAND_SVLTE2A:
467 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800468 if (have_cmdline) --dst;
469 while ((*dst++ = *src++));
470 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700471
472 case BASEBAND_MDM:
473 src = baseband_mdm;
474 if (have_cmdline) --dst;
475 while ((*dst++ = *src++));
476 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700477
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800478 case BASEBAND_MDM2:
479 src = baseband_mdm2;
480 if (have_cmdline) --dst;
481 while ((*dst++ = *src++));
482 break;
483
Amol Jadi5c61a952012-05-04 17:05:35 -0700484 case BASEBAND_SGLTE:
485 src = baseband_sglte;
486 if (have_cmdline) --dst;
487 while ((*dst++ = *src++));
488 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530489
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800490 case BASEBAND_SGLTE2:
491 src = baseband_sglte2;
492 if (have_cmdline) --dst;
493 while ((*dst++ = *src++));
494 break;
495
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530496 case BASEBAND_DSDA:
497 src = baseband_dsda;
498 if (have_cmdline) --dst;
499 while ((*dst++ = *src++));
500 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800501
502 case BASEBAND_DSDA2:
503 src = baseband_dsda2;
504 if (have_cmdline) --dst;
505 while ((*dst++ = *src++));
506 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800507 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700508
509 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700510 src = display_panel_buf;
511 if (have_cmdline) --dst;
512 while ((*dst++ = *src++));
513 }
Joonwoo Park61112782013-10-02 19:50:39 -0700514
515 if (have_target_boot_params) {
516 if (have_cmdline) --dst;
517 src = target_boot_params;
518 while ((*dst++ = *src++));
519 }
Neeti Desaie245d492012-06-01 12:52:13 -0700520 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700521
522
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700523 if (boot_dev_buf)
524 free(boot_dev_buf);
525
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800526 if (cmdline_final)
527 dprintf(INFO, "cmdline: %s\n", cmdline_final);
528 else
529 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700530 return cmdline_final;
531}
532
533unsigned *atag_core(unsigned *ptr)
534{
535 /* CORE */
536 *ptr++ = 2;
537 *ptr++ = 0x54410001;
538
539 return ptr;
540
541}
542
543unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
544 unsigned ramdisk_size)
545{
546 if (ramdisk_size) {
547 *ptr++ = 4;
548 *ptr++ = 0x54420005;
549 *ptr++ = (unsigned)ramdisk;
550 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800551 }
552
Neeti Desaie245d492012-06-01 12:52:13 -0700553 return ptr;
554}
555
556unsigned *atag_ptable(unsigned **ptr_addr)
557{
558 int i;
559 struct ptable *ptable;
560
561 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700562 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
563 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700564 *(*ptr_addr)++ = 0x4d534d70;
565 for (i = 0; i < ptable->count; ++i)
566 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
567 }
568
569 return (*ptr_addr);
570}
571
572unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
573{
574 int cmdline_length = 0;
575 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700576 char *dest;
577
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800578 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700579 n = (cmdline_length + 4) & (~3);
580
581 *ptr++ = (n / 4) + 2;
582 *ptr++ = 0x54410009;
583 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800584 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700585 ptr += (n / 4);
586
587 return ptr;
588}
589
590unsigned *atag_end(unsigned *ptr)
591{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800592 /* END */
593 *ptr++ = 0;
594 *ptr++ = 0;
595
Neeti Desaie245d492012-06-01 12:52:13 -0700596 return ptr;
597}
598
599void generate_atags(unsigned *ptr, const char *cmdline,
600 void *ramdisk, unsigned ramdisk_size)
601{
602
603 ptr = atag_core(ptr);
604 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
605 ptr = target_atag_mem(ptr);
606
607 /* Skip NAND partition ATAGS for eMMC boot */
608 if (!target_is_emmc_boot()){
609 ptr = atag_ptable(&ptr);
610 }
611
612 ptr = atag_cmdline(ptr, cmdline);
613 ptr = atag_end(ptr);
614}
615
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700616typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700617void boot_linux(void *kernel, unsigned *tags,
618 const char *cmdline, unsigned machtype,
619 void *ramdisk, unsigned ramdisk_size)
620{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800621 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800622#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700623 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800624#endif
625
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700626 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800627 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800628 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800629
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530630 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700631
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800632 final_cmdline = update_cmdline((const char*)cmdline);
633
Neeti Desai17379b82012-06-04 18:42:53 -0700634#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800635 dprintf(INFO, "Updating device tree: start\n");
636
Neeti Desai17379b82012-06-04 18:42:53 -0700637 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530638 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700639 if(ret)
640 {
641 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
642 ASSERT(0);
643 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800644 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700645#else
Neeti Desaie245d492012-06-01 12:52:13 -0700646 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800647 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700648#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700649
Maria Yu52254c02014-07-04 16:14:54 +0800650 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700651
652#if VERIFIED_BOOT
653 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700654 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700655 {
656 dprintf(INFO, "Failed to write protect dev info\n");
657 ASSERT(0);
658 }
659#endif
660
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700661 /* Perform target specific cleanup */
662 target_uninit();
663
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800664 /* Turn off splash screen if enabled */
665#if DISPLAY_SPLASH_SCREEN
666 target_display_shutdown();
667#endif
668
669
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800670 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530671 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800672
673 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700674
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +0530675 /* Initialise wdog to catch early kernel crashes */
676#if WDOG_SUPPORT
677 msm_wdog_init();
678#endif
Amol Jadi4421e652011-06-16 15:00:48 -0700679 /* do any platform specific cleanup before kernel entry */
680 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700681
Brian Swetland9c4c0752009-01-25 16:23:50 -0800682 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700683
Amol Jadi504f9fe2012-08-16 13:56:48 -0700684#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800685 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700686#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700687 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800688
689 if (IS_ARM64(kptr))
690 /* Jump to a 64bit kernel */
691 scm_elexec_call((paddr_t)kernel, tags_phys);
692 else
693 /* Jump to a 32bit kernel */
694 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800695}
696
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700697/* Function to check if the memory address range falls within the aboot
698 * boundaries.
699 * start: Start of the memory region
700 * size: Size of the memory region
701 */
702int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
703{
704 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800705 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700706 return -1;
707
708 /* Check for memory overlap. */
709 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
710 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700711 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700712 return 0;
713 else
714 return -1;
715}
716
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800717#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800718
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800719BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800720#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800721BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800722#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800723
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700724static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
725{
726 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800727
728#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800729#if IMAGE_VERIF_ALGO_SHA1
730 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
731#else
732 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
733#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800734#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700735
736 /* Assume device is rooted at this time. */
737 device.is_tampered = 1;
738
739 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
740
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700741#if VERIFIED_BOOT
742 if(boot_into_recovery)
743 {
744 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530745 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700746 }
747 else
748 {
749 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530750 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700751 }
752 boot_verify_print_state();
753#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700754 ret = image_verify((unsigned char *)bootimg_addr,
755 (unsigned char *)(bootimg_addr + bootimg_size),
756 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800757 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700758#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700759 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
760
761 if (ret)
762 {
763 /* Authorized kernel */
764 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700765 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700766 }
767
Amit Blay4aa292f2015-04-28 21:55:59 +0300768#ifdef MDTP_SUPPORT
769 {
770 /* Verify MDTP lock.
771 * For boot & recovery partitions, use aboot's verification result.
772 */
773 mdtp_ext_partition_verification_t ext_partition;
774 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
775 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
776 ext_partition.page_size = 0; /* Not needed since already validated */
777 ext_partition.image_addr = 0; /* Not needed since already validated */
778 ext_partition.image_size = 0; /* Not needed since already validated */
779 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
780 mdtp_fwlock_verify_lock(&ext_partition);
781 }
782#endif /* MDTP_SUPPORT */
783
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700784#if USE_PCOM_SECBOOT
785 set_tamper_flag(device.is_tampered);
786#endif
787
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700788#if VERIFIED_BOOT
789 if(boot_verify_get_state() == RED)
790 {
791 if(!boot_into_recovery)
792 {
793 dprintf(CRITICAL,
794 "Device verification failed. Rebooting into recovery.\n");
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +0530795 mdelay(1000);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700796 reboot_device(RECOVERY_MODE);
797 }
798 else
799 {
800 dprintf(CRITICAL,
801 "Recovery image verification failed. Asserting..\n");
802 ASSERT(0);
803 }
804 }
805#endif
Unnati Gandhi1be04752015-03-27 19:41:53 +0530806
807 if(device.is_tampered)
808 {
809 write_device_info_mmc(&device);
810 #ifdef TZ_TAMPER_FUSE
811 set_tamper_fuse_cmd();
812 #endif
813 #ifdef ASSERT_ON_TAMPER
814 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
815 ASSERT(0);
816 #endif
817 }
818
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700819}
820
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530821static bool check_format_bit()
822{
823 bool ret = false;
824 int index;
825 uint64_t offset;
826 struct boot_selection_info *in = NULL;
827 char *buf = NULL;
828
829 index = partition_get_index("bootselect");
830 if (index == INVALID_PTN)
831 {
832 dprintf(INFO, "Unable to locate /bootselect partition\n");
833 return ret;
834 }
835 offset = partition_get_offset(index);
836 if(!offset)
837 {
838 dprintf(INFO, "partition /bootselect doesn't exist\n");
839 return ret;
840 }
841 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
842 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530843 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530844 {
845 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
846 free(buf);
847 return ret;
848 }
849 in = (struct boot_selection_info *) buf;
850 if ((in->signature == BOOTSELECT_SIGNATURE) &&
851 (in->version == BOOTSELECT_VERSION)) {
852 if ((in->state_info & BOOTSELECT_FORMAT) &&
853 !(in->state_info & BOOTSELECT_FACTORY))
854 ret = true;
855 } else {
856 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
857 in->signature, in->version);
858 ASSERT(0);
859 }
860 free(buf);
861 return ret;
862}
863
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700864void boot_verifier_init()
865{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700866 uint32_t boot_state;
867 /* Check if device unlock */
868 if(device.is_unlocked)
869 {
870 boot_verify_send_event(DEV_UNLOCK);
871 boot_verify_print_state();
872 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
873 return;
874 }
875 else
876 {
877 boot_verify_send_event(BOOT_INIT);
878 }
879
880 /* Initialize keystore */
881 boot_state = boot_verify_keystore_init();
882 if(boot_state == YELLOW)
883 {
884 boot_verify_print_state();
885 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
886 }
887}
888
Shashank Mittal23b8f422010-04-16 19:27:21 -0700889int boot_linux_from_mmc(void)
890{
891 struct boot_img_hdr *hdr = (void*) buf;
892 struct boot_img_hdr *uhdr;
893 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700894 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700895 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700896 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700897
Shashank Mittalcd98d472011-08-02 14:29:24 -0700898 unsigned char *image_addr = 0;
899 unsigned kernel_actual;
900 unsigned ramdisk_actual;
901 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700902 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700903
Matthew Qin271927e2015-03-31 22:07:07 -0400904 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +0800905 unsigned int out_len = 0;
906 unsigned int out_avai_len = 0;
907 unsigned char *out_addr = NULL;
908 uint32_t dtb_offset = 0;
909 unsigned char *kernel_start_addr = NULL;
910 unsigned int kernel_size = 0;
911 int rc;
912
Neeti Desai465491e2012-07-31 12:53:35 -0700913#if DEVICE_TREE
914 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700915 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700916 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800917 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700918 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -0400919 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -0700920#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +0800921 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800922
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530923 if (check_format_bit())
924 boot_into_recovery = 1;
925
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700926 if (!boot_into_recovery) {
927 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
928 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
929 if (rcode <= 0) {
930 boot_into_ffbm = false;
931 if (rcode < 0)
932 dprintf(CRITICAL,"failed to get ffbm cookie");
933 } else
934 boot_into_ffbm = true;
935 } else
936 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700937 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
938 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
939 dprintf(INFO, "Unified boot method!\n");
940 hdr = uhdr;
941 goto unified_boot;
942 }
Greg Griscod6250552011-06-29 14:40:23 -0700943 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700944 index = partition_get_index("boot");
945 ptn = partition_get_offset(index);
946 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700947 dprintf(CRITICAL, "ERROR: No boot partition found\n");
948 return -1;
949 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700950 }
951 else {
952 index = partition_get_index("recovery");
953 ptn = partition_get_offset(index);
954 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700955 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
956 return -1;
957 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700958 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -0800959 /* Set Lun for boot & recovery partitions */
960 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -0700961
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530962 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700963 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
964 return -1;
965 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700966
967 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700968 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700969 return -1;
970 }
971
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700972 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530973
974 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
975 dprintf(CRITICAL, "ERROR: Invalid page size\n");
976 return -1;
977 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700978 page_size = hdr->page_size;
979 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700980 }
981
Matthew Qin49e51fa2015-02-09 10:40:45 +0800982 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
983 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800984
Matthew Qin49e51fa2015-02-09 10:40:45 +0800985 image_addr = (unsigned char *)target_get_scratch_address();
986
987#if DEVICE_TREE
988 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
989 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
990#else
991 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
992#endif
993
994#if VERIFIED_BOOT
995 boot_verifier_init();
996#endif
997
998 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
999 {
1000 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1001 return -1;
1002 }
1003
Matthew Qinbb7923d2015-02-09 10:56:09 +08001004 /*
1005 * Update loading flow of bootimage to support compressed/uncompressed
1006 * bootimage on both 64bit and 32bit platform.
1007 * 1. Load bootimage from emmc partition onto DDR.
1008 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1009 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1010 * platform accordingly.
1011 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1012 */
1013
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001014 dprintf(INFO, "Loading (%s) image (%d): start\n",
1015 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001016 bs_set_timestamp(BS_KERNEL_LOAD_START);
1017
Matthew Qinbb7923d2015-02-09 10:56:09 +08001018 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001019 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1020 {
1021 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1022 return -1;
1023 }
1024
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001025 dprintf(INFO, "Loading (%s) image (%d): done\n",
1026 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1027
Matthew Qin49e51fa2015-02-09 10:40:45 +08001028 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1029
1030 /* Authenticate Kernel */
1031 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1032 (int) target_use_signed_kernel(),
1033 device.is_unlocked,
1034 device.is_tampered);
1035
1036 if(target_use_signed_kernel() && (!device.is_unlocked))
1037 {
1038 offset = imagesize_actual;
1039 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1040 {
1041 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1042 return -1;
1043 }
1044
1045 /* Read signature */
1046 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1047 {
1048 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1049 return -1;
1050 }
1051
1052 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
1053 } else {
1054 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1055 #ifdef TZ_SAVE_KERNEL_HASH
1056 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1057 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001058
1059#ifdef MDTP_SUPPORT
1060 {
1061 /* Verify MDTP lock.
1062 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1063 * since verification was skipped in aboot. The signature is not part of the loaded image.
1064 */
1065 mdtp_ext_partition_verification_t ext_partition;
1066 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1067 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1068 ext_partition.page_size = page_size;
1069 ext_partition.image_addr = (uint32)image_addr;
1070 ext_partition.image_size = imagesize_actual;
1071 ext_partition.sig_avail = FALSE;
1072 mdtp_fwlock_verify_lock(&ext_partition);
1073 }
1074#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001075 }
1076
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001077 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001078 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1079 * If not, continue booting.
1080 */
1081 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1082 {
1083 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1084 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001085 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001086 rc = decompress((unsigned char *)(image_addr + page_size),
1087 hdr->kernel_size, out_addr, out_avai_len,
1088 &dtb_offset, &out_len);
1089 if (rc)
1090 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001091 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001092 ASSERT(0);
1093 }
1094
Matthew Qin0b15b322015-05-19 05:20:54 -04001095 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001096 kptr = (struct kernel64_hdr *)out_addr;
1097 kernel_start_addr = out_addr;
1098 kernel_size = out_len;
1099 } else {
1100 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1101 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1102 kernel_size = hdr->kernel_size;
1103 }
1104
1105 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001106 * Update the kernel/ramdisk/tags address if the boot image header
1107 * has default values, these default values come from mkbootimg when
1108 * the boot image is flashed using fastboot flash:raw
1109 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001110 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001111
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001112 /* Get virtual addresses since the hdr saves physical addresses. */
1113 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1114 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1115 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001116
Matthew Qinbb7923d2015-02-09 10:56:09 +08001117 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001118 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001119 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001120 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1121 {
1122 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1123 return -1;
1124 }
1125
1126#ifndef DEVICE_TREE
1127 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1128 {
1129 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1130 return -1;
1131 }
1132#endif
1133
Matthew Qin49e51fa2015-02-09 10:40:45 +08001134 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001135 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001136 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001137
Matthew Qin49e51fa2015-02-09 10:40:45 +08001138 #if DEVICE_TREE
1139 if(hdr->dt_size) {
1140 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1141 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001142
Matthew Qin49e51fa2015-02-09 10:40:45 +08001143 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1144 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1145 return -1;
1146 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001147
Matthew Qin49e51fa2015-02-09 10:40:45 +08001148 /* Find index of device tree within device tree table */
1149 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1150 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1151 return -1;
1152 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001153
Matthew Qin271927e2015-03-31 22:07:07 -04001154 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1155 {
1156 unsigned int compressed_size = 0;
1157 out_addr += out_len;
1158 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001159 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001160 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1161 dt_entry.size, out_addr, out_avai_len,
1162 &compressed_size, &dtb_size);
1163 if (rc)
1164 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001165 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001166 ASSERT(0);
1167 }
1168
Matthew Qin0b15b322015-05-19 05:20:54 -04001169 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001170 best_match_dt_addr = out_addr;
1171 } else {
1172 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1173 dtb_size = dt_entry.size;
1174 }
1175
Matthew Qin49e51fa2015-02-09 10:40:45 +08001176 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001177 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001178 {
1179 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1180 return -1;
1181 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001182
Matthew Qin271927e2015-03-31 22:07:07 -04001183 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001184 } else {
1185 /* Validate the tags_addr */
1186 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001187 {
1188 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1189 return -1;
1190 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001191 /*
1192 * If appended dev tree is found, update the atags with
1193 * memory address to the DTB appended location on RAM.
1194 * Else update with the atags address in the kernel header
1195 */
1196 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001197 dtb = dev_tree_appended((void*)(image_addr + page_size),
1198 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001199 (void *)hdr->tags_addr);
1200 if (!dtb) {
1201 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001202 return -1;
1203 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001204 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001205 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001206
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001207 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1208 target_load_ssd_keystore();
1209
Shashank Mittal23b8f422010-04-16 19:27:21 -07001210unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001211
Dima Zavin77e41f32013-03-06 16:10:43 -08001212 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001213 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001214 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1215
1216 return 0;
1217}
1218
Dima Zavin214cc642009-01-26 11:16:21 -08001219int boot_linux_from_flash(void)
1220{
1221 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001222 struct ptentry *ptn;
1223 struct ptable *ptable;
1224 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001225
Shashank Mittalcd98d472011-08-02 14:29:24 -07001226 unsigned char *image_addr = 0;
1227 unsigned kernel_actual;
1228 unsigned ramdisk_actual;
1229 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001230 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001231
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001232#if DEVICE_TREE
1233 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001234 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001235 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001236 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001237#endif
1238
David Ng183a7422009-12-07 14:55:21 -08001239 if (target_is_emmc_boot()) {
1240 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1241 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1242 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1243 return -1;
1244 }
1245 goto continue_boot;
1246 }
1247
Dima Zavin214cc642009-01-26 11:16:21 -08001248 ptable = flash_get_ptable();
1249 if (ptable == NULL) {
1250 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1251 return -1;
1252 }
1253
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001254 if(!boot_into_recovery)
1255 {
1256 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001257
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001258 if (ptn == NULL) {
1259 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1260 return -1;
1261 }
1262 }
1263 else
1264 {
1265 ptn = ptable_find(ptable, "recovery");
1266 if (ptn == NULL) {
1267 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1268 return -1;
1269 }
Dima Zavin214cc642009-01-26 11:16:21 -08001270 }
1271
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001272 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001273 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1274 return -1;
1275 }
Dima Zavin214cc642009-01-26 11:16:21 -08001276
1277 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001278 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001279 return -1;
1280 }
1281
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001282 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001283 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 -08001284 return -1;
1285 }
1286
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001287 /*
1288 * Update the kernel/ramdisk/tags address if the boot image header
1289 * has default values, these default values come from mkbootimg when
1290 * the boot image is flashed using fastboot flash:raw
1291 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001292 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001293
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001294 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001295 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1296 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1297 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1298
1299 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1300 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1301
1302 /* Check if the addresses in the header are valid. */
1303 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1304 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1305 {
1306 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1307 return -1;
1308 }
1309
1310#ifndef DEVICE_TREE
1311 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1312 {
1313 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1314 return -1;
1315 }
1316#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001317
Shashank Mittalcd98d472011-08-02 14:29:24 -07001318 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001319 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001320 {
1321 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001322 offset = 0;
1323
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001324#if DEVICE_TREE
1325 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1326 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001327
1328 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1329 {
1330 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1331 return -1;
1332 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001333#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001334 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001335#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001336
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001337 dprintf(INFO, "Loading (%s) image (%d): start\n",
1338 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001339 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001340
Shashank Mittalcd98d472011-08-02 14:29:24 -07001341 /* Read image without signature */
1342 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1343 {
1344 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1345 return -1;
1346 }
Dima Zavin214cc642009-01-26 11:16:21 -08001347
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001348 dprintf(INFO, "Loading (%s) image (%d): done\n",
1349 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001350 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001351
Shashank Mittalcd98d472011-08-02 14:29:24 -07001352 offset = imagesize_actual;
1353 /* Read signature */
1354 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1355 {
1356 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001357 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001358 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001359
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301360 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001361
1362 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001363 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1364 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001365#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001366 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001367 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001368 {
1369 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1370 return -1;
1371 }
1372
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001373 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1374#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001375
1376 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001377 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001378 {
1379 write_device_info_flash(&device);
1380 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301381#if USE_PCOM_SECBOOT
1382 set_tamper_flag(device.is_tampered);
1383#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001384 }
1385 else
1386 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001387 offset = page_size;
1388
Amol Jadib6be5c12012-11-14 13:39:51 -08001389 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1390 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1391 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1392
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001393 dprintf(INFO, "Loading (%s) image (%d): start\n",
1394 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1395
Amol Jadi492d5a52013-03-15 16:12:34 -07001396 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001397
1398 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001399 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1400 return -1;
1401 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001402 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001403
Amol Jadib6be5c12012-11-14 13:39:51 -08001404 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001405 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1406 return -1;
1407 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001408 offset += ramdisk_actual;
1409
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001410 dprintf(INFO, "Loading (%s) image (%d): done\n",
1411 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1412
Amol Jadi492d5a52013-03-15 16:12:34 -07001413 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001414
1415 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001416 offset += second_actual;
1417 /* Second image loading not implemented. */
1418 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001419 }
1420
1421#if DEVICE_TREE
1422 if(hdr->dt_size != 0) {
1423
1424 /* Read the device tree table into buffer */
1425 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1426 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1427 return -1;
1428 }
1429
1430 table = (struct dt_table*) dt_buf;
1431
Deepa Dinamani19648b42013-09-05 17:05:55 -07001432 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001433 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1434 return -1;
1435 }
1436
Deepa Dinamani19648b42013-09-05 17:05:55 -07001437 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1438 if (!table)
1439 return -1;
1440
1441 /* Read the entire device tree table into buffer */
1442 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1443 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1444 return -1;
1445 }
1446
1447
Joel Kingaa335dc2013-06-03 16:11:08 -07001448 /* Find index of device tree within device tree table */
1449 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001450 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1451 return -1;
1452 }
1453
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001454 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001455 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001456 {
1457 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1458 return -1;
1459 }
1460
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001461 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001462 if(flash_read(ptn, offset + dt_entry.offset,
1463 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001464 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1465 return -1;
1466 }
1467 }
1468#endif
1469
Shashank Mittalcd98d472011-08-02 14:29:24 -07001470 }
David Ng183a7422009-12-07 14:55:21 -08001471continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001472
Dima Zavin214cc642009-01-26 11:16:21 -08001473 /* TODO: create/pass atags to kernel */
1474
Ajay Dudanie28a6072011-07-01 13:59:46 -07001475 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001476 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001477 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1478
1479 return 0;
1480}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001481
Shashank Mittal162244e2011-08-08 19:01:25 -07001482void write_device_info_mmc(device_info *dev)
1483{
Shashank Mittal162244e2011-08-08 19:01:25 -07001484 unsigned long long ptn = 0;
1485 unsigned long long size;
1486 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001487 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001488 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001489 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001490
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001491 if (devinfo_present)
1492 index = partition_get_index("devinfo");
1493 else
1494 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001495
Shashank Mittal162244e2011-08-08 19:01:25 -07001496 ptn = partition_get_offset(index);
1497 if(ptn == 0)
1498 {
1499 return;
1500 }
1501
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001502 lun = partition_get_lun(index);
1503 mmc_set_lun(lun);
1504
Shashank Mittal162244e2011-08-08 19:01:25 -07001505 size = partition_get_size(index);
1506
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001507 blocksize = mmc_get_device_blocksize();
1508
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001509 if (devinfo_present)
1510 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1511 else
1512 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1513 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001514 {
1515 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001516 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001517 }
1518}
1519
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001520void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001521{
Shashank Mittal162244e2011-08-08 19:01:25 -07001522 unsigned long long ptn = 0;
1523 unsigned long long size;
1524 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001525 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001526 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001527
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001528 if ((index = partition_get_index("devinfo")) < 0)
1529 {
1530 devinfo_present = false;
1531 index = partition_get_index("aboot");
1532 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001533
Shashank Mittal162244e2011-08-08 19:01:25 -07001534 ptn = partition_get_offset(index);
1535 if(ptn == 0)
1536 {
1537 return;
1538 }
1539
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001540 mmc_set_lun(partition_get_lun(index));
1541
Shashank Mittal162244e2011-08-08 19:01:25 -07001542 size = partition_get_size(index);
1543
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001544 blocksize = mmc_get_device_blocksize();
1545
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001546 if (devinfo_present)
1547 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1548 else
1549 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1550 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001551 {
1552 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001553 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001554 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001555}
1556
1557void write_device_info_flash(device_info *dev)
1558{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001559 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001560 struct ptentry *ptn;
1561 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001562 if(info == NULL)
1563 {
1564 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1565 ASSERT(0);
1566 }
1567 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001568 ptable = flash_get_ptable();
1569 if (ptable == NULL)
1570 {
1571 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1572 return;
1573 }
1574
1575 ptn = ptable_find(ptable, "devinfo");
1576 if (ptn == NULL)
1577 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001578 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001579 return;
1580 }
1581
1582 memcpy(info, dev, sizeof(device_info));
1583
1584 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1585 {
1586 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1587 return;
1588 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001589 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001590}
1591
vijay kumarc65876c2015-04-24 13:29:16 +05301592static int read_allow_oem_unlock(device_info *dev)
1593{
vijay kumarc65876c2015-04-24 13:29:16 +05301594 unsigned offset;
1595 int index;
1596 unsigned long long ptn;
1597 unsigned long long ptn_size;
1598 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001599 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301600
vijay kumarca2e6812015-07-08 20:28:25 +05301601 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301602 if (index == INVALID_PTN)
1603 {
vijay kumarca2e6812015-07-08 20:28:25 +05301604 index = partition_get_index(frp_ptns[1]);
1605 if (index == INVALID_PTN)
1606 {
1607 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1608 return -1;
1609 }
vijay kumarc65876c2015-04-24 13:29:16 +05301610 }
1611
1612 ptn = partition_get_offset(index);
1613 ptn_size = partition_get_size(index);
1614 offset = ptn_size - blocksize;
1615
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001616 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301617 {
1618 dprintf(CRITICAL, "Reading MMC failed\n");
1619 return -1;
1620 }
1621
1622 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1623 is_allow_unlock = buf[blocksize-1] & 0x01;
1624 return 0;
1625}
1626
1627static int write_allow_oem_unlock(bool allow_unlock)
1628{
vijay kumarc65876c2015-04-24 13:29:16 +05301629 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301630 int index;
1631 unsigned long long ptn;
1632 unsigned long long ptn_size;
1633 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001634 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301635
vijay kumarca2e6812015-07-08 20:28:25 +05301636 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301637 if (index == INVALID_PTN)
1638 {
vijay kumarca2e6812015-07-08 20:28:25 +05301639 index = partition_get_index(frp_ptns[1]);
1640 if (index == INVALID_PTN)
1641 {
1642 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1643 return -1;
1644 }
vijay kumarc65876c2015-04-24 13:29:16 +05301645 }
1646
1647 ptn = partition_get_offset(index);
1648 ptn_size = partition_get_size(index);
1649 offset = ptn_size - blocksize;
1650
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001651 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301652 {
1653 dprintf(CRITICAL, "Reading MMC failed\n");
1654 return -1;
1655 }
1656
1657 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1658 buf[blocksize-1] = allow_unlock;
1659 if (mmc_write(ptn + offset, blocksize, buf))
1660 {
1661 dprintf(CRITICAL, "Writing MMC failed\n");
1662 return -1;
1663 }
1664
1665 return 0;
1666}
1667
Shashank Mittal162244e2011-08-08 19:01:25 -07001668void read_device_info_flash(device_info *dev)
1669{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001670 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001671 struct ptentry *ptn;
1672 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001673 if(info == NULL)
1674 {
1675 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1676 ASSERT(0);
1677 }
1678 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001679 ptable = flash_get_ptable();
1680 if (ptable == NULL)
1681 {
1682 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1683 return;
1684 }
1685
1686 ptn = ptable_find(ptable, "devinfo");
1687 if (ptn == NULL)
1688 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001689 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001690 return;
1691 }
1692
1693 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1694 {
1695 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1696 return;
1697 }
1698
1699 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1700 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001701 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1702 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001703 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001704 write_device_info_flash(info);
1705 }
1706 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001707 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001708}
1709
1710void write_device_info(device_info *dev)
1711{
1712 if(target_is_emmc_boot())
1713 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001714 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1715 if(info == NULL)
1716 {
1717 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1718 ASSERT(0);
1719 }
1720 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001721 memcpy(info, dev, sizeof(struct device_info));
1722
1723#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001724 if (is_secure_boot_enable()) {
1725 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1726 ASSERT(0);
1727 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001728 else
1729 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001730#else
1731 write_device_info_mmc(info);
1732#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001733 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001734 }
1735 else
1736 {
1737 write_device_info_flash(dev);
1738 }
1739}
1740
1741void read_device_info(device_info *dev)
1742{
1743 if(target_is_emmc_boot())
1744 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001745 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1746 if(info == NULL)
1747 {
1748 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1749 ASSERT(0);
1750 }
1751 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001752
1753#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001754 if (is_secure_boot_enable()) {
1755 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1756 ASSERT(0);
1757 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001758 else
1759 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001760#else
1761 read_device_info_mmc(info);
1762#endif
1763
1764 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1765 {
1766 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001767 if (is_secure_boot_enable())
1768 info->is_unlocked = 0;
1769 else
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001770 info->is_unlocked = 1;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001771 info->is_tampered = 0;
1772 info->charger_screen_enabled = 0;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001773 write_device_info(info);
1774 }
1775 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001776 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001777 }
1778 else
1779 {
1780 read_device_info_flash(dev);
1781 }
1782}
1783
1784void reset_device_info()
1785{
1786 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001787 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001788 write_device_info(&device);
1789}
1790
1791void set_device_root()
1792{
1793 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001794 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001795 write_device_info(&device);
1796}
1797
Amol Jadicb524072012-08-09 16:40:18 -07001798#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04001799int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
Amol Jadicb524072012-08-09 16:40:18 -07001800{
1801 uint32 dt_image_offset = 0;
1802 uint32_t n;
1803 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001804 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001805 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001806 unsigned int compressed_size = 0;
1807 unsigned int dtb_size = 0;
1808 unsigned int out_avai_len = 0;
1809 unsigned char *out_addr = NULL;
1810 unsigned char *best_match_dt_addr = NULL;
1811 int rc;
Amol Jadicb524072012-08-09 16:40:18 -07001812
1813 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1814
Amol Jadicb524072012-08-09 16:40:18 -07001815 if(hdr->dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001816 /* add kernel offset */
1817 dt_image_offset += page_size;
1818 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1819 dt_image_offset += n;
1820
1821 /* add ramdisk offset */
1822 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1823 dt_image_offset += n;
1824
1825 /* add second offset */
1826 if(hdr->second_size != 0) {
1827 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1828 dt_image_offset += n;
1829 }
1830
1831 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001832 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001833
Deepa Dinamani19648b42013-09-05 17:05:55 -07001834 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001835 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1836 return -1;
1837 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001838 /* Find index of device tree within device tree table */
1839 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001840 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1841 return -1;
1842 }
1843
Matthew Qin271927e2015-03-31 22:07:07 -04001844 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
1845 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
1846 {
1847 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
1848 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04001849 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001850 rc = decompress(best_match_dt_addr,
1851 dt_entry.size, out_addr, out_avai_len,
1852 &compressed_size, &dtb_size);
1853 if (rc)
1854 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001855 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001856 ASSERT(0);
1857 }
1858
Matthew Qin0b15b322015-05-19 05:20:54 -04001859 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001860 best_match_dt_addr = out_addr;
1861 } else {
1862 dtb_size = dt_entry.size;
1863 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001864 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001865 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001866 {
1867 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1868 return -1;
1869 }
1870
Amol Jadicb524072012-08-09 16:40:18 -07001871 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04001872 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001873 } else
1874 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001875
1876 /* Everything looks fine. Return success. */
1877 return 0;
1878}
1879#endif
1880
Brian Swetland9c4c0752009-01-25 16:23:50 -08001881void cmd_boot(const char *arg, void *data, unsigned sz)
1882{
Amit Blayba53e6e2015-08-17 09:20:01 +03001883#ifdef MDTP_SUPPORT
1884 static bool is_mdtp_activated = 0;
1885#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08001886 unsigned kernel_actual;
1887 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001888 uint32_t image_actual;
1889 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001890 uint32_t sig_actual = SIGNATURE_SIZE;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001891 struct boot_img_hdr *hdr = NULL;
1892 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001893 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001894 int ret = 0;
1895 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001896 unsigned int out_len = 0;
1897 unsigned int out_avai_len = 0;
1898 unsigned char *out_addr = NULL;
1899 uint32_t dtb_offset = 0;
1900 unsigned char *kernel_start_addr = NULL;
1901 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04001902 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001903
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001904#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07001905 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001906 {
1907 fastboot_fail("unlock device to use this command");
1908 return;
1909 }
1910#endif
1911
Brian Swetland9c4c0752009-01-25 16:23:50 -08001912 if (sz < sizeof(hdr)) {
1913 fastboot_fail("invalid bootimage header");
1914 return;
1915 }
1916
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001917 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001918
1919 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001920 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001921
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001922 if(target_is_emmc_boot() && hdr->page_size) {
1923 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001924 page_mask = page_size - 1;
1925 }
1926
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001927 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1928 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001929#if DEVICE_TREE
1930 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1931#endif
1932
1933 image_actual = ADD_OF(page_size, kernel_actual);
1934 image_actual = ADD_OF(image_actual, ramdisk_actual);
1935 image_actual = ADD_OF(image_actual, dt_actual);
1936
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001937 if (target_use_signed_kernel() && (!device.is_unlocked))
1938 image_actual = ADD_OF(image_actual, sig_actual);
1939
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001940 /* sz should have atleast raw boot image */
1941 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001942 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001943 return;
1944 }
1945
1946 /* Verify the boot image
1947 * device & page_size are initialized in aboot_init
1948 */
1949 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001950 /* Pass size excluding signature size, otherwise we would try to
1951 * access signature beyond its length
1952 */
1953 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001954
Amit Blay4aa292f2015-04-28 21:55:59 +03001955#ifdef MDTP_SUPPORT
1956 else
1957 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03001958 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03001959 mdtp_ext_partition_verification_t ext_partition;
Amit Blayba53e6e2015-08-17 09:20:01 +03001960
1961 if (!is_mdtp_activated) {
1962 ext_partition.partition = MDTP_PARTITION_NONE;
1963 mdtp_fwlock_verify_lock(&ext_partition);
1964 }
Amit Blay4aa292f2015-04-28 21:55:59 +03001965 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03001966
Shay Nachmani062aa3f2015-05-17 17:28:44 +03001967 mdtp_activated(&is_mdtp_activated);
1968 if(is_mdtp_activated){
1969 dprintf(CRITICAL, "fastboot boot command is not available.\n");
1970 return;
1971 }
Amit Blay4aa292f2015-04-28 21:55:59 +03001972#endif /* MDTP_SUPPORT */
1973
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001974 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001975 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1976 * If not, continue booting.
1977 */
1978 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
1979 {
1980 out_addr = (unsigned char *)target_get_scratch_address();
1981 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
1982 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001983 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001984 ret = decompress((unsigned char *)(ptr + page_size),
1985 hdr->kernel_size, out_addr, out_avai_len,
1986 &dtb_offset, &out_len);
1987 if (ret)
1988 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001989 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001990 ASSERT(0);
1991 }
1992
Matthew Qin0b15b322015-05-19 05:20:54 -04001993 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001994 kptr = (struct kernel64_hdr *)out_addr;
1995 kernel_start_addr = out_addr;
1996 kernel_size = out_len;
1997 } else {
1998 kptr = (struct kernel64_hdr*)((char *)data + page_size);
1999 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2000 kernel_size = hdr->kernel_size;
2001 }
2002
2003 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002004 * Update the kernel/ramdisk/tags address if the boot image header
2005 * has default values, these default values come from mkbootimg when
2006 * the boot image is flashed using fastboot flash:raw
2007 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002008 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002009
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002010 /* Get virtual addresses since the hdr saves physical addresses. */
2011 hdr->kernel_addr = VA(hdr->kernel_addr);
2012 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2013 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002014
Matthew Qinbb7923d2015-02-09 10:56:09 +08002015 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002016 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002017 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002018 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2019 {
2020 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002021 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002022 }
2023
Amol Jadicb524072012-08-09 16:40:18 -07002024#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002025 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002026 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002027 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002028
2029 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002030#else
2031 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2032 {
2033 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002034 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002035 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002036#endif
2037
2038 /* Load ramdisk & kernel */
2039 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002040 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002041
2042#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002043 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2044 {
2045 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2046 return;
2047 }
2048
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002049 /*
2050 * If dtb is not found look for appended DTB in the kernel.
2051 * If appended dev tree is found, update the atags with
2052 * memory address to the DTB appended location on RAM.
2053 * Else update with the atags address in the kernel header
2054 */
2055 if (!dtb_copied) {
2056 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002057 dtb = dev_tree_appended((void*)(ptr + page_size),
2058 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002059 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002060 if (!dtb) {
2061 fastboot_fail("dtb not found");
2062 return;
2063 }
Amol Jadicb524072012-08-09 16:40:18 -07002064 }
2065#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002066
2067 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002068 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002069
Dima Zavin77e41f32013-03-06 16:10:43 -08002070 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002071 (const char*) hdr->cmdline, board_machtype(),
2072 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002073}
2074
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002075void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002076{
2077 struct ptentry *ptn;
2078 struct ptable *ptable;
2079
2080 ptable = flash_get_ptable();
2081 if (ptable == NULL) {
2082 fastboot_fail("partition table doesn't exist");
2083 return;
2084 }
2085
2086 ptn = ptable_find(ptable, arg);
2087 if (ptn == NULL) {
2088 fastboot_fail("unknown partition name");
2089 return;
2090 }
2091
2092 if (flash_erase(ptn)) {
2093 fastboot_fail("failed to erase partition");
2094 return;
2095 }
2096 fastboot_okay("");
2097}
2098
Bikas Gurungd48bd242010-09-04 19:54:32 -07002099
2100void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2101{
2102 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002103 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002104 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002105 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002106
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002107#if VERIFIED_BOOT
2108 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2109 {
2110 if(!device.is_unlocked)
2111 {
2112 fastboot_fail("unlock device to erase keystore");
2113 return;
2114 }
2115 }
2116#endif
2117
Kinson Chikf1a43512011-07-14 11:28:39 -07002118 index = partition_get_index(arg);
2119 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002120 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002121
Kinson Chikf1a43512011-07-14 11:28:39 -07002122 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002123 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002124 return;
2125 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002126
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002127 lun = partition_get_lun(index);
2128 mmc_set_lun(lun);
2129
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002130 if (platform_boot_dev_isemmc())
2131 {
2132 if (mmc_erase_card(ptn, size)) {
2133 fastboot_fail("failed to erase partition\n");
2134 return;
2135 }
2136 } else {
2137 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2138 size = partition_get_size(index);
2139 if (size > DEFAULT_ERASE_SIZE)
2140 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002141
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002142 /* Simple inefficient version of erase. Just writing
2143 0 in first several blocks */
2144 if (mmc_write(ptn , size, (unsigned int *)out)) {
2145 fastboot_fail("failed to erase partition");
2146 return;
2147 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002148 }
2149 fastboot_okay("");
2150}
2151
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002152void cmd_erase(const char *arg, void *data, unsigned sz)
2153{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002154#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002155 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002156 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002157 if(!device.is_unlocked && !device.is_verified)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002158 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002159 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002160 return;
2161 }
Channagoud Kadabi35297672015-06-13 11:09:49 -07002162 if(!device.is_unlocked && device.is_verified)
2163 {
2164 if(!boot_verify_flash_allowed(arg))
2165 {
2166 fastboot_fail("cannot flash this partition in verified state");
2167 return;
2168 }
2169 }
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002170 }
2171#endif
2172
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002173 if(target_is_emmc_boot())
2174 cmd_erase_mmc(arg, data, sz);
2175 else
2176 cmd_erase_nand(arg, data, sz);
2177}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002178
Ajay Dudani5c761132011-04-07 20:19:04 -07002179void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002180{
2181 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002182 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002183 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002184 char *token = NULL;
2185 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002186 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002187 uint8_t lun = 0;
2188 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002189
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002190 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002191 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002192 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002193 if(token)
2194 {
2195 lun = atoi(token);
2196 mmc_set_lun(lun);
2197 lun_set = true;
2198 }
2199
Mao Jinlong226f33a2014-07-04 17:24:10 +08002200 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002201 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002202 if (!strcmp(pname, "partition"))
2203 {
2204 dprintf(INFO, "Attempt to write partition image.\n");
2205 if (write_partition(sz, (unsigned char *) data)) {
2206 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002207 return;
2208 }
2209 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002210 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002211 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002212#if VERIFIED_BOOT
2213 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2214 {
2215 if(!device.is_unlocked)
2216 {
2217 fastboot_fail("unlock device to flash keystore");
2218 return;
2219 }
2220 if(!boot_verify_validate_keystore((unsigned char *)data))
2221 {
2222 fastboot_fail("image is not a keystore file");
2223 return;
2224 }
2225 }
2226#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002227 index = partition_get_index(pname);
2228 ptn = partition_get_offset(index);
2229 if(ptn == 0) {
2230 fastboot_fail("partition table doesn't exist");
2231 return;
2232 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002233
Mao Jinlong226f33a2014-07-04 17:24:10 +08002234 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2235 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2236 fastboot_fail("image is not a boot image");
2237 return;
2238 }
2239 }
2240
2241 if(!lun_set)
2242 {
2243 lun = partition_get_lun(index);
2244 mmc_set_lun(lun);
2245 }
2246
2247 size = partition_get_size(index);
2248 if (ROUND_TO_PAGE(sz,511) > size) {
2249 fastboot_fail("size too large");
2250 return;
2251 }
2252 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2253 fastboot_fail("flash write failure");
2254 return;
2255 }
Greg Grisco6e754772011-06-23 12:19:39 -07002256 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002257 }
2258 fastboot_okay("");
2259 return;
2260}
2261
Ajay Dudanide984792015-03-02 09:57:41 -08002262void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2263{
2264 int i, images;
2265 meta_header_t *meta_header;
2266 img_header_entry_t *img_header_entry;
2267
2268 meta_header = (meta_header_t*) data;
2269 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2270
2271 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2272
2273 for (i=0; i<images; i++) {
2274
2275 if((img_header_entry[i].ptn_name == NULL) ||
2276 (img_header_entry[i].start_offset == 0) ||
2277 (img_header_entry[i].size == 0))
2278 break;
2279
2280 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2281 (void *) data + img_header_entry[i].start_offset,
2282 img_header_entry[i].size);
2283 }
2284
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002285 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2286 {
2287 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2288 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2289 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2290 }
2291 else
2292 {
2293 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2294 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2295 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2296 }
2297
2298 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002299 fastboot_okay("");
2300 return;
2301}
2302
Ajay Dudani5c761132011-04-07 20:19:04 -07002303void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2304{
2305 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002306 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002307 uint32_t *fill_buf = NULL;
2308 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002309 sparse_header_t *sparse_header;
2310 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002311 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002312 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302313 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002314 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302315 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002316 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302317 /*End of the sparse image address*/
2318 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002319
Kinson Chikf1a43512011-07-14 11:28:39 -07002320 index = partition_get_index(arg);
2321 ptn = partition_get_offset(index);
2322 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002323 fastboot_fail("partition table doesn't exist");
2324 return;
2325 }
2326
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302327 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302328
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002329 lun = partition_get_lun(index);
2330 mmc_set_lun(lun);
2331
vijay kumar800255e2015-04-24 20:26:19 +05302332 if (sz < sizeof(sparse_header_t)) {
2333 fastboot_fail("size too low");
2334 return;
2335 }
2336
Ajay Dudani5c761132011-04-07 20:19:04 -07002337 /* Read and skip over sparse image header */
2338 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302339
vijay kumar1321f342015-03-27 12:13:42 +05302340 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002341 fastboot_fail("size too large");
2342 return;
2343 }
2344
vijay kumarde4fcf62015-04-23 13:05:49 +05302345 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302346
2347 if (data_end < (uint32_t)data) {
2348 fastboot_fail("buffer overreads occured due to invalid sparse header");
2349 return;
2350 }
2351
vijay kumarde4fcf62015-04-23 13:05:49 +05302352 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002353 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302354 fastboot_fail("sparse header size mismatch");
2355 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002356 }
2357
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002358 dprintf (SPEW, "=== Sparse Image Header ===\n");
2359 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2360 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2361 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2362 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2363 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2364 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2365 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2366 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002367
2368 /* Start processing chunks */
2369 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2370 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302371 /* Make sure the total image size does not exceed the partition size */
2372 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2373 fastboot_fail("size too large");
2374 return;
2375 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002376 /* Read and skip over chunk header */
2377 chunk_header = (chunk_header_t *) data;
2378 data += sizeof(chunk_header_t);
2379
vijay kumar800255e2015-04-24 20:26:19 +05302380 if (data_end < (uint32_t)data) {
2381 fastboot_fail("buffer overreads occured due to invalid sparse header");
2382 return;
2383 }
2384
Ajay Dudani5c761132011-04-07 20:19:04 -07002385 dprintf (SPEW, "=== Chunk Header ===\n");
2386 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2387 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2388 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2389
vijay kumar800255e2015-04-24 20:26:19 +05302390 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002391 {
vijay kumar800255e2015-04-24 20:26:19 +05302392 fastboot_fail("chunk header size mismatch");
2393 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002394 }
2395
wufeng.jiang813dc352015-06-02 23:02:46 -04002396 if (!sparse_header->blk_sz ){
2397 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302398 return;
2399 }
2400
wufeng.jiang813dc352015-06-02 23:02:46 -04002401 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2402
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302403 /* Make sure that the chunk size calculated from sparse image does not
2404 * exceed partition size
2405 */
2406 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2407 {
2408 fastboot_fail("Chunk data size exceeds partition size");
2409 return;
2410 }
2411
Ajay Dudani5c761132011-04-07 20:19:04 -07002412 switch (chunk_header->chunk_type)
2413 {
2414 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002415 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002416 chunk_data_sz))
2417 {
2418 fastboot_fail("Bogus chunk size for chunk type Raw");
2419 return;
2420 }
2421
vijay kumar800255e2015-04-24 20:26:19 +05302422 if (data_end < (uint32_t)data + chunk_data_sz) {
2423 fastboot_fail("buffer overreads occured due to invalid sparse header");
2424 return;
2425 }
2426
wufeng.jiang813dc352015-06-02 23:02:46 -04002427 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2428 otherwise it will return in the line above
2429 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002430 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002431 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002432 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002433 {
2434 fastboot_fail("flash write failure");
2435 return;
2436 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302437 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2438 fastboot_fail("Bogus size for RAW chunk type");
2439 return;
2440 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002441 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002442 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002443 break;
2444
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002445 case CHUNK_TYPE_FILL:
2446 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2447 sizeof(uint32_t)))
2448 {
2449 fastboot_fail("Bogus chunk size for chunk type FILL");
2450 return;
2451 }
2452
2453 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2454 if (!fill_buf)
2455 {
2456 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2457 return;
2458 }
2459
vijay kumar800255e2015-04-24 20:26:19 +05302460 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2461 fastboot_fail("buffer overreads occured due to invalid sparse header");
2462 return;
2463 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002464 fill_val = *(uint32_t *)data;
2465 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002466
2467 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2468 {
2469 fill_buf[i] = fill_val;
2470 }
2471
wufeng.jiang813dc352015-06-02 23:02:46 -04002472 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002473 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302474 /* Make sure that the data written to partition does not exceed partition size */
2475 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2476 {
2477 fastboot_fail("Chunk data size for fill type exceeds partition size");
2478 return;
2479 }
2480
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002481 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2482 sparse_header->blk_sz,
2483 fill_buf))
2484 {
2485 fastboot_fail("flash write failure");
2486 free(fill_buf);
2487 return;
2488 }
2489
2490 total_blocks++;
2491 }
2492
2493 free(fill_buf);
2494 break;
2495
Ajay Dudani5c761132011-04-07 20:19:04 -07002496 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302497 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2498 fastboot_fail("bogus size for chunk DONT CARE type");
2499 return;
2500 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002501 total_blocks += chunk_header->chunk_sz;
2502 break;
2503
Ajay Dudani5c761132011-04-07 20:19:04 -07002504 case CHUNK_TYPE_CRC:
2505 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2506 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002507 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002508 return;
2509 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302510 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2511 fastboot_fail("bogus size for chunk CRC type");
2512 return;
2513 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002514 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302515 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2516 fastboot_fail("integer overflow occured");
2517 return;
2518 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002519 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302520 if (data_end < (uint32_t)data) {
2521 fastboot_fail("buffer overreads occured due to invalid sparse header");
2522 return;
2523 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002524 break;
2525
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002526 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002527 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002528 fastboot_fail("Unknown chunk type");
2529 return;
2530 }
2531 }
2532
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002533 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2534 total_blocks, sparse_header->total_blks);
2535
2536 if(total_blocks != sparse_header->total_blks)
2537 {
2538 fastboot_fail("sparse image write failure");
2539 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002540
2541 fastboot_okay("");
2542 return;
2543}
2544
2545void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2546{
2547 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002548 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002549
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002550#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002551 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2552 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002553 int ret=0;
2554 uint32 major_version=0;
2555 uint32 minor_version=0;
2556
2557 ret = scm_svc_version(&major_version,&minor_version);
2558 if(!ret)
2559 {
2560 if(major_version >= 2)
2561 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002562 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002563 {
2564 ret = encrypt_scm((uint32 **) &data, &sz);
2565 if (ret != 0) {
2566 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2567 return;
2568 }
2569
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002570 /* Protect only for SSD */
2571 if (!strcmp(arg, "ssd")) {
2572 ret = scm_protect_keystore((uint32 *) data, sz);
2573 if (ret != 0) {
2574 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2575 return;
2576 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002577 }
2578 }
2579 else
2580 {
2581 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2582 if(ret != 0)
2583 {
2584 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2585 return;
2586 }
2587 }
2588 }
2589 else
2590 {
2591 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2592 magic_number[1] == DECRYPT_MAGIC_1)
2593 {
2594 ret = decrypt_scm((uint32 **) &data, &sz);
2595 if (ret != 0) {
2596 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2597 return;
2598 }
2599 }
2600 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2601 magic_number[1] == ENCRYPT_MAGIC_1)
2602 {
2603 ret = encrypt_scm((uint32 **) &data, &sz);
2604 if (ret != 0) {
2605 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2606 return;
2607 }
2608 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002609 }
2610 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002611 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002612 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002613 dprintf(CRITICAL,"INVALID SVC Version\n");
2614 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002615 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002616#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002617
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002618#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002619 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002620 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002621 if(!device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002622 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002623 fastboot_fail("device is locked. Cannot flash images");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002624 return;
2625 }
Channagoud Kadabi35297672015-06-13 11:09:49 -07002626 if(!device.is_unlocked && device.is_verified)
2627 {
2628 if(!boot_verify_flash_allowed(arg))
2629 {
2630 fastboot_fail("cannot flash this partition in verified state");
2631 return;
2632 }
2633 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002634 }
2635#endif
2636
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002637 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002638 meta_header = (meta_header_t *) data;
2639 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002640 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002641 else if (meta_header->magic == META_HEADER_MAGIC)
2642 cmd_flash_meta_img(arg, data, sz);
2643 else
2644 cmd_flash_mmc_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002645 return;
2646}
2647
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002648void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2649{
2650 struct ptentry *sys_ptn;
2651 struct ptable *ptable;
2652
2653 ptable = flash_get_ptable();
2654 if (ptable == NULL) {
2655 fastboot_fail("partition table doesn't exist");
2656 return;
2657 }
2658
2659 sys_ptn = ptable_find(ptable, "system");
2660 if (sys_ptn == NULL) {
2661 fastboot_fail("system partition not found");
2662 return;
2663 }
2664
2665 sz = ROUND_TO_PAGE(sz, page_mask);
2666 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
2667 fastboot_fail("update_ubi_vol failed");
2668 else
2669 fastboot_okay("");
2670}
2671
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002672void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002673{
2674 struct ptentry *ptn;
2675 struct ptable *ptable;
2676 unsigned extra = 0;
2677
2678 ptable = flash_get_ptable();
2679 if (ptable == NULL) {
2680 fastboot_fail("partition table doesn't exist");
2681 return;
2682 }
2683
2684 ptn = ptable_find(ptable, arg);
2685 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002686 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
2687 arg);
2688 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08002689 return;
2690 }
2691
2692 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2693 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2694 fastboot_fail("image is not a boot image");
2695 return;
2696 }
2697 }
2698
Amol Jadi5c61a952012-05-04 17:05:35 -07002699 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002700 || !strcmp(ptn->name, "userdata")
2701 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002702 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002703 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002704 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002705 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002706 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002707
2708 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002709 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2710 if (flash_ubi_img(ptn, data, sz)) {
2711 fastboot_fail("flash write failure");
2712 return;
2713 }
2714 } else {
2715 if (flash_write(ptn, extra, data, sz)) {
2716 fastboot_fail("flash write failure");
2717 return;
2718 }
Dima Zavin214cc642009-01-26 11:16:21 -08002719 }
2720 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2721 fastboot_okay("");
2722}
2723
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002724void cmd_flash(const char *arg, void *data, unsigned sz)
2725{
2726 if(target_is_emmc_boot())
2727 cmd_flash_mmc(arg, data, sz);
2728 else
2729 cmd_flash_nand(arg, data, sz);
2730}
2731
Dima Zavin214cc642009-01-26 11:16:21 -08002732void cmd_continue(const char *arg, void *data, unsigned sz)
2733{
2734 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002735 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002736
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002737 if (target_is_emmc_boot())
2738 {
2739 boot_linux_from_mmc();
2740 }
2741 else
2742 {
2743 boot_linux_from_flash();
2744 }
Dima Zavin214cc642009-01-26 11:16:21 -08002745}
2746
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002747void cmd_reboot(const char *arg, void *data, unsigned sz)
2748{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002749 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002750 fastboot_okay("");
2751 reboot_device(0);
2752}
2753
2754void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2755{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002756 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002757 fastboot_okay("");
2758 reboot_device(FASTBOOT_MODE);
2759}
2760
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002761void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2762{
2763 dprintf(INFO, "Enabling charger screen check\n");
2764 device.charger_screen_enabled = 1;
2765 write_device_info(&device);
2766 fastboot_okay("");
2767}
2768
2769void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2770{
2771 dprintf(INFO, "Disabling charger screen check\n");
2772 device.charger_screen_enabled = 0;
2773 write_device_info(&device);
2774 fastboot_okay("");
2775}
2776
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302777void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2778{
2779 dprintf(INFO, "Selecting display panel %s\n", arg);
2780 if (arg)
2781 strlcpy(device.display_panel, arg,
2782 sizeof(device.display_panel));
2783 write_device_info(&device);
2784 fastboot_okay("");
2785}
2786
Shashank Mittal162244e2011-08-08 19:01:25 -07002787void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2788{
vijay kumarc65876c2015-04-24 13:29:16 +05302789 if(!is_allow_unlock) {
2790 fastboot_fail("oem unlock is not allowed");
2791 return;
2792 }
2793
2794 display_fbcon_message("Oem Unlock requested");
2795 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2796}
2797
2798void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
2799{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002800 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002801 {
vijay kumarc65876c2015-04-24 13:29:16 +05302802 if(!is_allow_unlock) {
2803 fastboot_fail("oem unlock is not allowed");
2804 return;
2805 }
2806
Shashank Mittal162244e2011-08-08 19:01:25 -07002807 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002808 device.is_verified = 0;
2809 write_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05302810
2811 struct recovery_message msg;
2812 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2813 write_misc(0, &msg, sizeof(msg));
2814
2815 fastboot_okay("");
2816 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002817 }
2818 fastboot_okay("");
2819}
2820
2821void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2822{
2823 /* TODO: Wipe user data */
2824 if(device.is_unlocked || device.is_verified)
2825 {
2826 device.is_unlocked = 0;
2827 device.is_verified = 0;
2828 write_device_info(&device);
2829 }
2830 fastboot_okay("");
2831}
2832
2833void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2834{
2835 /* TODO: Wipe user data */
2836 if(device.is_unlocked || !device.is_verified)
2837 {
2838 device.is_unlocked = 0;
2839 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002840 write_device_info(&device);
2841 }
2842 fastboot_okay("");
2843}
2844
Shashank Mittala0032282011-08-26 14:50:11 -07002845void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2846{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302847 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002848 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002849 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002850 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2851 fastboot_info(response);
2852 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002853 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302854 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2855 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002856 fastboot_okay("");
2857}
2858
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002859void cmd_preflash(const char *arg, void *data, unsigned sz)
2860{
2861 fastboot_okay("");
2862}
2863
Mao Flynn7b379f32015-04-20 00:28:30 +08002864static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302865
Mao Flynn7b379f32015-04-20 00:28:30 +08002866int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302867{
Mao Flynn7b379f32015-04-20 00:28:30 +08002868 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302869 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08002870 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302871 return -1;
2872 return 0;
2873}
2874
Mao Flynn7b379f32015-04-20 00:28:30 +08002875int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002876{
2877 struct ptentry *ptn;
2878 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08002879 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002880 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002881
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302882 ptable = flash_get_ptable();
2883 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002884 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2885 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302886 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002887
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302888 ptn = ptable_find(ptable, "splash");
2889 if (ptn == NULL) {
2890 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002891 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302892 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002893 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302894 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002895 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302896 }
2897
Mao Flynn7b379f32015-04-20 00:28:30 +08002898 header = (struct logo_img_header *)logo_header;
2899 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302900 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002901 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302902 }
2903
2904 fb_display = fbcon_display();
2905 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08002906 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
2907 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
2908
2909 /* if the logo is full-screen size, remove "fbcon_clear()" */
2910 if ((header->width != fb_display->width)
2911 || (header->height != fb_display->height))
2912 fbcon_clear();
2913
2914 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
2915 (uint32_t *)base,
2916 (header->blocks * 512))) {
2917 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
2918 return -1;
2919 }
2920 fbcon_extract_to_screen(header, base);
2921 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002922 }
Mao Flynn7b379f32015-04-20 00:28:30 +08002923
2924 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
2925 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
2926 return -1;
2927 }
2928
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302929 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08002930 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
2931 (uint32_t *)base,
2932 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302933 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302934 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002935 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002936 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302937 }
2938
Mao Flynn7b379f32015-04-20 00:28:30 +08002939 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302940}
2941
Mao Flynn7b379f32015-04-20 00:28:30 +08002942int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302943{
2944 int index = INVALID_PTN;
2945 unsigned long long ptn = 0;
2946 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08002947 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08002948 uint32_t blocksize, realsize, readsize;
2949 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302950
2951 index = partition_get_index("splash");
2952 if (index == 0) {
2953 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002954 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302955 }
2956
2957 ptn = partition_get_offset(index);
2958 if (ptn == 0) {
2959 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002960 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302961 }
2962
Mao Flynn1893a7f2015-06-03 12:03:36 +08002963 mmc_set_lun(partition_get_lun(index));
2964
2965 blocksize = mmc_get_device_blocksize();
2966 if (blocksize == 0) {
2967 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
2968 return -1;
2969 }
2970
2971 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07002972 if (!fb_display)
2973 {
2974 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
2975 return -1;
2976 }
2977
Mao Flynn1893a7f2015-06-03 12:03:36 +08002978 base = (uint8_t *) fb_display->base;
2979
2980 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302981 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002982 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302983 }
2984
Mao Flynn1893a7f2015-06-03 12:03:36 +08002985 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08002986 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302987 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08002988 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302989 }
2990
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302991 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08002992 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
2993 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08002994
Mao Flynn1893a7f2015-06-03 12:03:36 +08002995 realsize = header->blocks * 512;
2996 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
2997
2998 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08002999 if ((header->width != fb_display->width)
3000 || (header->height != fb_display->height))
3001 fbcon_clear();
3002
Mao Flynn1893a7f2015-06-03 12:03:36 +08003003 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003004 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3005 return -1;
3006 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003007
Mao Flynn1893a7f2015-06-03 12:03:36 +08003008 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3009 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303010
Mao Flynn1893a7f2015-06-03 12:03:36 +08003011 if ((header->width != fb_display->width) || (header->height != fb_display->height)) {
3012 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
3013 return -1;
3014 }
3015
3016 realsize = header->width * header->height * fb_display->bpp / 8;
3017 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3018
3019 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3020 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3021 fbcon_clear();
3022 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3023 return -1;
3024 }
3025 } else {
3026 if (mmc_read(ptn + blocksize ,
3027 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3028 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3029 return -1;
3030 }
3031 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3032 }
3033 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303034 }
3035
Mao Flynn7b379f32015-04-20 00:28:30 +08003036 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303037}
3038
Mao Flynn7b379f32015-04-20 00:28:30 +08003039int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303040{
3041 if (target_is_emmc_boot()) {
3042 return splash_screen_mmc();
3043 } else {
3044 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003045 }
3046}
3047
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003048/* Get the size from partiton name */
3049static void get_partition_size(const char *arg, char *response)
3050{
3051 uint64_t ptn = 0;
3052 uint64_t size;
3053 int index = INVALID_PTN;
3054
3055 index = partition_get_index(arg);
3056
3057 if (index == INVALID_PTN)
3058 {
3059 dprintf(CRITICAL, "Invalid partition index\n");
3060 return;
3061 }
3062
3063 ptn = partition_get_offset(index);
3064
3065 if(!ptn)
3066 {
3067 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3068 return;
3069 }
3070
3071 size = partition_get_size(index);
3072
3073 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3074 return;
3075}
3076
3077/*
3078 * Publish the partition type & size info
3079 * fastboot getvar will publish the required information.
3080 * fastboot getvar partition_size:<partition_name>: partition size in hex
3081 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3082 */
3083static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3084{
3085 uint8_t i;
3086
3087 for (i = 0; i < num_parts; i++) {
3088 get_partition_size(info[i].part_name, info[i].size_response);
3089
3090 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3091 {
3092 dprintf(CRITICAL, "partition size name truncated\n");
3093 return;
3094 }
3095 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3096 {
3097 dprintf(CRITICAL, "partition type name truncated\n");
3098 return;
3099 }
3100
3101 /* publish partition size & type info */
3102 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3103 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3104 }
3105}
3106
Amol Jadi5edf3552013-07-23 14:15:34 -07003107/* register commands and variables for fastboot */
3108void aboot_fastboot_register_commands(void)
3109{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003110 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07003111
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003112 struct fastboot_cmd_desc cmd_list[] = {
3113 /* By default the enabled list is empty. */
3114 {"", NULL},
3115 /* move commands enclosed within the below ifndef to here
3116 * if they need to be enabled in user build.
3117 */
3118#ifndef DISABLE_FASTBOOT_CMDS
3119 /* Register the following commands only for non-user builds */
3120 {"flash:", cmd_flash},
3121 {"erase:", cmd_erase},
3122 {"boot", cmd_boot},
3123 {"continue", cmd_continue},
3124 {"reboot", cmd_reboot},
3125 {"reboot-bootloader", cmd_reboot_bootloader},
3126 {"oem unlock", cmd_oem_unlock},
vijay kumarc65876c2015-04-24 13:29:16 +05303127 {"oem unlock-go", cmd_oem_unlock_go},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003128 {"oem lock", cmd_oem_lock},
3129 {"oem verified", cmd_oem_verified},
3130 {"oem device-info", cmd_oem_devinfo},
3131 {"preflash", cmd_preflash},
3132 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3133 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08003134 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003135#endif
3136 };
3137
3138 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3139 for (i = 1; i < fastboot_cmds_count; i++)
3140 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3141
Amol Jadi5edf3552013-07-23 14:15:34 -07003142 /* publish variables and their values */
3143 fastboot_publish("product", TARGET(BOARD));
3144 fastboot_publish("kernel", "lk");
3145 fastboot_publish("serialno", sn_buf);
3146
3147 /*
3148 * partition info is supported only for emmc partitions
3149 * Calling this for NAND prints some error messages which
3150 * is harmless but misleading. Avoid calling this for NAND
3151 * devices.
3152 */
3153 if (target_is_emmc_boot())
3154 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3155
3156 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003157 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3158 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003159 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003160 /* Is the charger screen check enabled */
3161 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3162 device.charger_screen_enabled);
3163 fastboot_publish("charger-screen-enabled",
3164 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303165 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3166 device.display_panel);
3167 fastboot_publish("display-panel",
3168 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003169 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3170 fastboot_publish("version-baseband", (const char *) device.radio_version);
Amol Jadi5edf3552013-07-23 14:15:34 -07003171}
3172
Brian Swetland9c4c0752009-01-25 16:23:50 -08003173void aboot_init(const struct app_descriptor *app)
3174{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003175 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03003176 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003177
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003178 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003179 if (target_is_emmc_boot())
3180 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003181 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003182 page_mask = page_size - 1;
3183 }
3184 else
3185 {
3186 page_size = flash_page_size();
3187 page_mask = page_size - 1;
3188 }
3189
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003190 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3191
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003192 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303193 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003194
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003195 /* Display splash screen if enabled */
3196#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003197#if NO_ALARM_DISPLAY
3198 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003199#endif
lijuang99c02d82015-02-13 19:04:34 +08003200 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003201#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003202 /* Wait if the display shutdown is in progress */
3203 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003204 if (!pm_appsbl_display_init_done())
3205 target_display_init(device.display_panel);
3206 else
3207 display_image_on_screen();
3208#else
lijuang99c02d82015-02-13 19:04:34 +08003209 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003210#endif
lijuang99c02d82015-02-13 19:04:34 +08003211 dprintf(SPEW, "Display Init: Done\n");
3212#if NO_ALARM_DISPLAY
3213 }
3214#endif
3215#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003216
Greg Griscod6250552011-06-29 14:40:23 -07003217 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003218 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003219
Dhaval Patel223ec952013-07-18 14:49:44 -07003220 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3221
Matthew Qindefd5562014-07-11 18:02:40 +08003222 /*
3223 * Check power off reason if user force reset,
3224 * if yes phone will do normal boot.
3225 */
3226 if (is_user_force_reset())
3227 goto normal_boot;
3228
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003229 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003230 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003231 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003232 dprintf(ALWAYS,"dload mode key sequence detected\n");
3233 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003234 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003235 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003236 }
3237 else
3238 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05303239 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003240 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003241 }
3242 boot_into_fastboot = true;
3243 }
3244 if (!boot_into_fastboot)
3245 {
3246 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3247 boot_into_recovery = 1;
3248 if (!boot_into_recovery &&
3249 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003250 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003251 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003252 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003253 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003254 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003255 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003256
Channagoud Kadabib6e476b2015-08-05 16:23:42 -07003257#if USE_PON_REBOOT_REG
3258 reboot_mode = check_hard_reboot_mode();
3259#else
Ajay Dudani77421292010-10-27 19:34:06 -07003260 reboot_mode = check_reboot_mode();
Channagoud Kadabib6e476b2015-08-05 16:23:42 -07003261#endif
3262 if (reboot_mode == RECOVERY_MODE)
3263 {
Ajay Dudani77421292010-10-27 19:34:06 -07003264 boot_into_recovery = 1;
Channagoud Kadabib6e476b2015-08-05 16:23:42 -07003265 }
3266 else if(reboot_mode == FASTBOOT_MODE)
3267 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003268 boot_into_fastboot = true;
Channagoud Kadabib6e476b2015-08-05 16:23:42 -07003269 }
3270 else if(reboot_mode == ALARM_BOOT)
3271 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003272 boot_reason_alarm = true;
Ajay Dudani77421292010-10-27 19:34:06 -07003273 }
3274
Matthew Qindefd5562014-07-11 18:02:40 +08003275normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003276 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003277 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003278 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003279 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003280 if(emmc_recovery_init())
3281 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3282 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003283 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003284 if((device.is_unlocked) || (device.is_tampered))
3285 {
3286 #ifdef TZ_TAMPER_FUSE
3287 set_tamper_fuse_cmd();
3288 #endif
3289 #if USE_PCOM_SECBOOT
3290 set_tamper_flag(device.is_tampered);
3291 #endif
3292 }
Shashank Mittala0032282011-08-26 14:50:11 -07003293 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003294
Pavel Nedev5d91d412013-04-29 11:34:24 +03003295 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003296 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003297 else
3298 {
3299 recovery_init();
3300 #if USE_PCOM_SECBOOT
3301 if((device.is_unlocked) || (device.is_tampered))
3302 set_tamper_flag(device.is_tampered);
3303 #endif
3304 boot_linux_from_flash();
3305 }
3306 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3307 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003308 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003309
Amol Jadi5edf3552013-07-23 14:15:34 -07003310 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003311
Amol Jadi5edf3552013-07-23 14:15:34 -07003312 /* register aboot specific fastboot commands */
3313 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003314
Amol Jadi5edf3552013-07-23 14:15:34 -07003315 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003316 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003317
3318 /* initialize and start fastboot */
3319 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08003320}
3321
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003322uint32_t get_page_size()
3323{
3324 return page_size;
3325}
3326
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003327/*
3328 * Calculated and save hash (SHA256) for non-signed boot image.
3329 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003330 * @param image_addr - Boot image address
3331 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003332 *
3333 * @return int - 0 on success, negative value on failure.
3334 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003335static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003336{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003337 unsigned int digest[8];
3338#if IMAGE_VERIF_ALGO_SHA1
3339 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3340#else
3341 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3342#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003343
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003344 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003345 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003346
3347 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003348 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003349
3350 return 0;
3351}
3352
Brian Swetland9c4c0752009-01-25 16:23:50 -08003353APP_START(aboot)
3354 .init = aboot_init,
3355APP_END