blob: e8021d881bf9e46356dfd4ef099bc52a0ba51df7 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08005 * Copyright (c) 2009-2015, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080060#if USE_RPMB_FOR_DEVINFO
61#include <rpmb.h>
62#endif
Dima Zavin214cc642009-01-26 11:16:21 -080063
Neeti Desai17379b82012-06-04 18:42:53 -070064#if DEVICE_TREE
65#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070066#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070067#endif
68
Shashank Mittalcd98d472011-08-02 14:29:24 -070069#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080070#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080071#include "bootimg.h"
72#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070073#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080074#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070075#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070076#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070077#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070078#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020079#include "mdtp.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070080
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070081extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070082extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070083extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070084extern int get_target_boot_params(const char *cmdline, const char *part,
85 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070086
87void write_device_info_mmc(device_info *dev);
88void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -070089static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070090
Sridhar Parasurame94e8152014-10-24 14:06:03 -070091/* fastboot command function pointer */
92typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
93
94struct fastboot_cmd_desc {
95 char * name;
96 fastboot_cmd_fn cb;
97};
98
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070099#define EXPAND(NAME) #NAME
100#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700101
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800102#ifdef MEMBASE
103#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
104#else
David Ng183a7422009-12-07 14:55:21 -0800105#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800106#endif
107
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700108#ifndef MEMSIZE
109#define MEMSIZE 1024*1024
110#endif
111
112#define MAX_TAGS_SIZE 1024
113
lijuang72875802015-02-10 10:35:12 +0800114#define RECOVERY_HARD_RESET_MODE 0x01
115#define FASTBOOT_HARD_RESET_MODE 0x02
116#define RTC_HARD_RESET_MODE 0x03
117
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800118#define RECOVERY_MODE 0x77665502
119#define FASTBOOT_MODE 0x77665500
Matthew Qind886f3c2014-01-17 16:52:01 +0800120#define ALARM_BOOT 0x77665503
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800121
Kun Liang2f1601a2013-08-12 16:29:54 +0800122/* make 4096 as default size to ensure EFS,EXT4's erasing */
123#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800124#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800125
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700126#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700127#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800128
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800129#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
130
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700131#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
132
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800133#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700134static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700135#else
David Ng183a7422009-12-07 14:55:21 -0800136static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700137#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800138static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300139static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800140static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800141static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800142static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700143static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300144static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800145
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800146static const char *baseband_apq = " androidboot.baseband=apq";
147static const char *baseband_msm = " androidboot.baseband=msm";
148static const char *baseband_csfb = " androidboot.baseband=csfb";
149static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700150static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800151static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700152static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800153static const char *baseband_dsda = " androidboot.baseband=dsda";
154static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800155static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800156static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800157
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700158static unsigned page_size = 0;
159static unsigned page_mask = 0;
160static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
161static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700162static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800163static bool boot_reason_alarm;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700164
Shashank Mittalcd98d472011-08-02 14:29:24 -0700165/* Assuming unauthorized kernel image by default */
166static int auth_kernel_img = 0;
167
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -0800168static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
Shashank Mittal162244e2011-08-08 19:01:25 -0700169
Dima Zavin42168f22009-01-30 11:52:22 -0800170struct atag_ptbl_entry
171{
172 char name[16];
173 unsigned offset;
174 unsigned size;
175 unsigned flags;
176};
177
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700178/*
179 * Partition info, required to be published
180 * for fastboot
181 */
182struct getvar_partition_info {
183 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
184 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
185 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
186 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
187 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
188};
189
190/*
191 * Right now, we are publishing the info for only
192 * three partitions
193 */
194struct getvar_partition_info part_info[] =
195{
196 { "system" , "partition-size:", "partition-type:", "", "ext4" },
197 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
198 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
199};
200
201char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700202char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800203char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700204char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530205char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700206
Greg Griscod2471ef2011-07-14 13:00:42 -0700207extern int emmc_recovery_init(void);
208
Kinson Chik0b1c8162011-08-31 16:31:57 -0700209#if NO_KEYPAD_DRIVER
210extern int fastboot_trigger(void);
211#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700212
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800213static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700214{
215 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700216#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800217 if (is_arm64)
218 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
219 else
220 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700221 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
222 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700223#endif
224}
225
Dima Zavin42168f22009-01-30 11:52:22 -0800226static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
227{
228 struct atag_ptbl_entry atag_ptn;
229
230 memcpy(atag_ptn.name, ptn->name, 16);
231 atag_ptn.name[15] = '\0';
232 atag_ptn.offset = ptn->start;
233 atag_ptn.size = ptn->length;
234 atag_ptn.flags = ptn->flags;
235 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
236 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
237}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800238
Neeti Desaie245d492012-06-01 12:52:13 -0700239unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800240{
David Ng183a7422009-12-07 14:55:21 -0800241 int cmdline_len = 0;
242 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800243 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700244 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800245 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300246 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700247 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700248 char *boot_dev_buf = NULL;
Dima Zavin42168f22009-01-30 11:52:22 -0800249
Brian Swetland9c4c0752009-01-25 16:23:50 -0800250 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800251 cmdline_len = strlen(cmdline);
252 have_cmdline = 1;
253 }
254 if (target_is_emmc_boot()) {
255 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800256#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700257 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
258 ASSERT(boot_dev_buf);
259 platform_boot_dev_cmdline(boot_dev_buf);
260 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700261#endif
David Ng183a7422009-12-07 14:55:21 -0800262 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800263
264 cmdline_len += strlen(usb_sn_cmdline);
265 cmdline_len += strlen(sn_buf);
266
Pavel Nedev5614d222013-06-17 18:01:02 +0300267 if (boot_into_recovery && gpt_exists)
268 cmdline_len += strlen(secondary_gpt_enable);
269
Pavel Nedev328ac822013-04-05 15:25:11 +0300270 if (boot_into_ffbm) {
271 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700272 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800273 /* reduce kernel console messages to speed-up boot */
274 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800275 } else if (boot_reason_alarm) {
276 cmdline_len += strlen(alarmboot_cmdline);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700277 } else if (device.charger_screen_enabled &&
278 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700279 pause_at_bootup = 1;
280 cmdline_len += strlen(battchg_pause);
281 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800282
Shashank Mittalcd98d472011-08-02 14:29:24 -0700283 if(target_use_signed_kernel() && auth_kernel_img) {
284 cmdline_len += strlen(auth_kernel);
285 }
286
Joonwoo Park61112782013-10-02 19:50:39 -0700287 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
288 "system",
289 target_boot_params,
290 sizeof(target_boot_params)) == 0) {
291 have_target_boot_params = 1;
292 cmdline_len += strlen(target_boot_params);
293 }
294
Ajay Dudanid04110c2011-01-17 23:55:07 -0800295 /* Determine correct androidboot.baseband to use */
296 switch(target_baseband())
297 {
298 case BASEBAND_APQ:
299 cmdline_len += strlen(baseband_apq);
300 break;
301
302 case BASEBAND_MSM:
303 cmdline_len += strlen(baseband_msm);
304 break;
305
306 case BASEBAND_CSFB:
307 cmdline_len += strlen(baseband_csfb);
308 break;
309
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800310 case BASEBAND_SVLTE2A:
311 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800312 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700313
314 case BASEBAND_MDM:
315 cmdline_len += strlen(baseband_mdm);
316 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700317
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800318 case BASEBAND_MDM2:
319 cmdline_len += strlen(baseband_mdm2);
320 break;
321
Amol Jadi5c61a952012-05-04 17:05:35 -0700322 case BASEBAND_SGLTE:
323 cmdline_len += strlen(baseband_sglte);
324 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530325
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800326 case BASEBAND_SGLTE2:
327 cmdline_len += strlen(baseband_sglte2);
328 break;
329
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530330 case BASEBAND_DSDA:
331 cmdline_len += strlen(baseband_dsda);
332 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800333
334 case BASEBAND_DSDA2:
335 cmdline_len += strlen(baseband_dsda2);
336 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800337 }
338
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800339 if (cmdline) {
340 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
341 target_display_panel_node(device.display_panel,
342 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
343 strlen(display_panel_buf)) {
344 cmdline_len += strlen(display_panel_buf);
345 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700346 }
347
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800348 if (target_warm_boot()) {
349 warm_boot = true;
350 cmdline_len += strlen(warmboot_cmdline);
351 }
352
David Ng183a7422009-12-07 14:55:21 -0800353 if (cmdline_len > 0) {
354 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800355 unsigned char *dst;
356
357 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
358 ASSERT(cmdline_final != NULL);
359 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700360
Amol Jadi168b7712012-03-06 16:15:00 -0800361 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800362 if (have_cmdline) {
363 src = cmdline;
364 while ((*dst++ = *src++));
365 }
366 if (target_is_emmc_boot()) {
367 src = emmc_cmdline;
368 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700369 have_cmdline = 1;
370 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800371#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700372 src = boot_dev_buf;
373 if (have_cmdline) --dst;
374 while ((*dst++ = *src++));
375#endif
David Ngf773dde2010-07-26 19:55:08 -0700376 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800377
378 src = usb_sn_cmdline;
379 if (have_cmdline) --dst;
380 have_cmdline = 1;
381 while ((*dst++ = *src++));
382 src = sn_buf;
383 if (have_cmdline) --dst;
384 have_cmdline = 1;
385 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800386 if (warm_boot) {
387 if (have_cmdline) --dst;
388 src = warmboot_cmdline;
389 while ((*dst++ = *src++));
390 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800391
Pavel Nedev5614d222013-06-17 18:01:02 +0300392 if (boot_into_recovery && gpt_exists) {
393 src = secondary_gpt_enable;
394 if (have_cmdline) --dst;
395 while ((*dst++ = *src++));
396 }
397
Pavel Nedev328ac822013-04-05 15:25:11 +0300398 if (boot_into_ffbm) {
399 src = androidboot_mode;
400 if (have_cmdline) --dst;
401 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700402 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300403 if (have_cmdline) --dst;
404 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800405 src = loglevel;
406 if (have_cmdline) --dst;
407 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800408 } else if (boot_reason_alarm) {
409 src = alarmboot_cmdline;
410 if (have_cmdline) --dst;
411 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300412 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700413 src = battchg_pause;
414 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800415 while ((*dst++ = *src++));
416 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800417
Shashank Mittalcd98d472011-08-02 14:29:24 -0700418 if(target_use_signed_kernel() && auth_kernel_img) {
419 src = auth_kernel;
420 if (have_cmdline) --dst;
421 while ((*dst++ = *src++));
422 }
423
Ajay Dudanid04110c2011-01-17 23:55:07 -0800424 switch(target_baseband())
425 {
426 case BASEBAND_APQ:
427 src = baseband_apq;
428 if (have_cmdline) --dst;
429 while ((*dst++ = *src++));
430 break;
431
432 case BASEBAND_MSM:
433 src = baseband_msm;
434 if (have_cmdline) --dst;
435 while ((*dst++ = *src++));
436 break;
437
438 case BASEBAND_CSFB:
439 src = baseband_csfb;
440 if (have_cmdline) --dst;
441 while ((*dst++ = *src++));
442 break;
443
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800444 case BASEBAND_SVLTE2A:
445 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800446 if (have_cmdline) --dst;
447 while ((*dst++ = *src++));
448 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700449
450 case BASEBAND_MDM:
451 src = baseband_mdm;
452 if (have_cmdline) --dst;
453 while ((*dst++ = *src++));
454 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700455
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800456 case BASEBAND_MDM2:
457 src = baseband_mdm2;
458 if (have_cmdline) --dst;
459 while ((*dst++ = *src++));
460 break;
461
Amol Jadi5c61a952012-05-04 17:05:35 -0700462 case BASEBAND_SGLTE:
463 src = baseband_sglte;
464 if (have_cmdline) --dst;
465 while ((*dst++ = *src++));
466 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530467
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800468 case BASEBAND_SGLTE2:
469 src = baseband_sglte2;
470 if (have_cmdline) --dst;
471 while ((*dst++ = *src++));
472 break;
473
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530474 case BASEBAND_DSDA:
475 src = baseband_dsda;
476 if (have_cmdline) --dst;
477 while ((*dst++ = *src++));
478 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800479
480 case BASEBAND_DSDA2:
481 src = baseband_dsda2;
482 if (have_cmdline) --dst;
483 while ((*dst++ = *src++));
484 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800485 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700486
487 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700488 src = display_panel_buf;
489 if (have_cmdline) --dst;
490 while ((*dst++ = *src++));
491 }
Joonwoo Park61112782013-10-02 19:50:39 -0700492
493 if (have_target_boot_params) {
494 if (have_cmdline) --dst;
495 src = target_boot_params;
496 while ((*dst++ = *src++));
497 }
Neeti Desaie245d492012-06-01 12:52:13 -0700498 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700499
500
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700501 if (boot_dev_buf)
502 free(boot_dev_buf);
503
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800504 if (cmdline_final)
505 dprintf(INFO, "cmdline: %s\n", cmdline_final);
506 else
507 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700508 return cmdline_final;
509}
510
511unsigned *atag_core(unsigned *ptr)
512{
513 /* CORE */
514 *ptr++ = 2;
515 *ptr++ = 0x54410001;
516
517 return ptr;
518
519}
520
521unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
522 unsigned ramdisk_size)
523{
524 if (ramdisk_size) {
525 *ptr++ = 4;
526 *ptr++ = 0x54420005;
527 *ptr++ = (unsigned)ramdisk;
528 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800529 }
530
Neeti Desaie245d492012-06-01 12:52:13 -0700531 return ptr;
532}
533
534unsigned *atag_ptable(unsigned **ptr_addr)
535{
536 int i;
537 struct ptable *ptable;
538
539 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700540 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
541 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700542 *(*ptr_addr)++ = 0x4d534d70;
543 for (i = 0; i < ptable->count; ++i)
544 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
545 }
546
547 return (*ptr_addr);
548}
549
550unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
551{
552 int cmdline_length = 0;
553 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700554 char *dest;
555
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800556 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700557 n = (cmdline_length + 4) & (~3);
558
559 *ptr++ = (n / 4) + 2;
560 *ptr++ = 0x54410009;
561 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800562 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700563 ptr += (n / 4);
564
565 return ptr;
566}
567
568unsigned *atag_end(unsigned *ptr)
569{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800570 /* END */
571 *ptr++ = 0;
572 *ptr++ = 0;
573
Neeti Desaie245d492012-06-01 12:52:13 -0700574 return ptr;
575}
576
577void generate_atags(unsigned *ptr, const char *cmdline,
578 void *ramdisk, unsigned ramdisk_size)
579{
580
581 ptr = atag_core(ptr);
582 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
583 ptr = target_atag_mem(ptr);
584
585 /* Skip NAND partition ATAGS for eMMC boot */
586 if (!target_is_emmc_boot()){
587 ptr = atag_ptable(&ptr);
588 }
589
590 ptr = atag_cmdline(ptr, cmdline);
591 ptr = atag_end(ptr);
592}
593
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700594typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700595void boot_linux(void *kernel, unsigned *tags,
596 const char *cmdline, unsigned machtype,
597 void *ramdisk, unsigned ramdisk_size)
598{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800599 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800600#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700601 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800602#endif
603
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700604 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800605 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800606 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800607
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530608 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700609
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800610 final_cmdline = update_cmdline((const char*)cmdline);
611
Neeti Desai17379b82012-06-04 18:42:53 -0700612#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800613 dprintf(INFO, "Updating device tree: start\n");
614
Neeti Desai17379b82012-06-04 18:42:53 -0700615 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530616 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700617 if(ret)
618 {
619 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
620 ASSERT(0);
621 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800622 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700623#else
Neeti Desaie245d492012-06-01 12:52:13 -0700624 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800625 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700626#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700627
Maria Yu52254c02014-07-04 16:14:54 +0800628 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700629
630#if VERIFIED_BOOT
631 /* Write protect the device info */
Channagoud Kadabibc4f15f2015-02-25 12:09:51 -0800632 if (target_build_variant_user() && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700633 {
634 dprintf(INFO, "Failed to write protect dev info\n");
635 ASSERT(0);
636 }
637#endif
638
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700639 /* Perform target specific cleanup */
640 target_uninit();
641
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800642 /* Turn off splash screen if enabled */
643#if DISPLAY_SPLASH_SCREEN
644 target_display_shutdown();
645#endif
646
647
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800648 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530649 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800650
651 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700652
Amol Jadi4421e652011-06-16 15:00:48 -0700653 /* do any platform specific cleanup before kernel entry */
654 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700655
Brian Swetland9c4c0752009-01-25 16:23:50 -0800656 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700657
Amol Jadi504f9fe2012-08-16 13:56:48 -0700658#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800659 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700660#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700661 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800662
663 if (IS_ARM64(kptr))
664 /* Jump to a 64bit kernel */
665 scm_elexec_call((paddr_t)kernel, tags_phys);
666 else
667 /* Jump to a 32bit kernel */
668 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800669}
670
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700671/* Function to check if the memory address range falls within the aboot
672 * boundaries.
673 * start: Start of the memory region
674 * size: Size of the memory region
675 */
676int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
677{
678 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800679 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700680 return -1;
681
682 /* Check for memory overlap. */
683 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
684 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700685 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700686 return 0;
687 else
688 return -1;
689}
690
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800691#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800692
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800693BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800694#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800695BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800696#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800697
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700698static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
699{
700 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800701
702#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800703#if IMAGE_VERIF_ALGO_SHA1
704 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
705#else
706 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
707#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800708#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700709
710 /* Assume device is rooted at this time. */
711 device.is_tampered = 1;
712
713 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
714
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700715#if VERIFIED_BOOT
716 if(boot_into_recovery)
717 {
718 ret = boot_verify_image((unsigned char *)bootimg_addr,
719 bootimg_size, "recovery");
720 }
721 else
722 {
723 ret = boot_verify_image((unsigned char *)bootimg_addr,
724 bootimg_size, "boot");
725 }
726 boot_verify_print_state();
727#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700728 ret = image_verify((unsigned char *)bootimg_addr,
729 (unsigned char *)(bootimg_addr + bootimg_size),
730 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800731 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700732#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700733 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
734
735 if (ret)
736 {
737 /* Authorized kernel */
738 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700739 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700740 }
741
742#if USE_PCOM_SECBOOT
743 set_tamper_flag(device.is_tampered);
744#endif
745
746 if(device.is_tampered)
747 {
748 write_device_info_mmc(&device);
749 #ifdef TZ_TAMPER_FUSE
750 set_tamper_fuse_cmd();
751 #endif
752 #ifdef ASSERT_ON_TAMPER
753 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
754 ASSERT(0);
755 #endif
756 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700757
758#if VERIFIED_BOOT
759 if(boot_verify_get_state() == RED)
760 {
761 if(!boot_into_recovery)
762 {
763 dprintf(CRITICAL,
764 "Device verification failed. Rebooting into recovery.\n");
765 reboot_device(RECOVERY_MODE);
766 }
767 else
768 {
769 dprintf(CRITICAL,
770 "Recovery image verification failed. Asserting..\n");
771 ASSERT(0);
772 }
773 }
774#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700775}
776
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530777static bool check_format_bit()
778{
779 bool ret = false;
780 int index;
781 uint64_t offset;
782 struct boot_selection_info *in = NULL;
783 char *buf = NULL;
784
785 index = partition_get_index("bootselect");
786 if (index == INVALID_PTN)
787 {
788 dprintf(INFO, "Unable to locate /bootselect partition\n");
789 return ret;
790 }
791 offset = partition_get_offset(index);
792 if(!offset)
793 {
794 dprintf(INFO, "partition /bootselect doesn't exist\n");
795 return ret;
796 }
797 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
798 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530799 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530800 {
801 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
802 free(buf);
803 return ret;
804 }
805 in = (struct boot_selection_info *) buf;
806 if ((in->signature == BOOTSELECT_SIGNATURE) &&
807 (in->version == BOOTSELECT_VERSION)) {
808 if ((in->state_info & BOOTSELECT_FORMAT) &&
809 !(in->state_info & BOOTSELECT_FACTORY))
810 ret = true;
811 } else {
812 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
813 in->signature, in->version);
814 ASSERT(0);
815 }
816 free(buf);
817 return ret;
818}
819
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700820void boot_verifier_init()
821{
822
823 uint32_t boot_state;
824 /* Check if device unlock */
825 if(device.is_unlocked)
826 {
827 boot_verify_send_event(DEV_UNLOCK);
828 boot_verify_print_state();
829 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
830 return;
831 }
832 else
833 {
834 boot_verify_send_event(BOOT_INIT);
835 }
836
837 /* Initialize keystore */
838 boot_state = boot_verify_keystore_init();
839 if(boot_state == YELLOW)
840 {
841 boot_verify_print_state();
842 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
843 }
844}
845
Shashank Mittal23b8f422010-04-16 19:27:21 -0700846int boot_linux_from_mmc(void)
847{
848 struct boot_img_hdr *hdr = (void*) buf;
849 struct boot_img_hdr *uhdr;
850 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700851 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700852 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700853 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700854
Shashank Mittalcd98d472011-08-02 14:29:24 -0700855 unsigned char *image_addr = 0;
856 unsigned kernel_actual;
857 unsigned ramdisk_actual;
858 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700859 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700860
861#if DEVICE_TREE
862 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700863 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700864 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800865 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700866 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700867#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +0800868 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800869
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530870 if (check_format_bit())
871 boot_into_recovery = 1;
872
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700873 if (!boot_into_recovery) {
874 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
875 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
876 if (rcode <= 0) {
877 boot_into_ffbm = false;
878 if (rcode < 0)
879 dprintf(CRITICAL,"failed to get ffbm cookie");
880 } else
881 boot_into_ffbm = true;
882 } else
883 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700884 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
885 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
886 dprintf(INFO, "Unified boot method!\n");
887 hdr = uhdr;
888 goto unified_boot;
889 }
Greg Griscod6250552011-06-29 14:40:23 -0700890 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700891 index = partition_get_index("boot");
892 ptn = partition_get_offset(index);
893 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700894 dprintf(CRITICAL, "ERROR: No boot partition found\n");
895 return -1;
896 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700897 }
898 else {
899 index = partition_get_index("recovery");
900 ptn = partition_get_offset(index);
901 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700902 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
903 return -1;
904 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700905 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -0800906 /* Set Lun for boot & recovery partitions */
907 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -0700908
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530909 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700910 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
911 return -1;
912 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700913
914 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700915 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700916 return -1;
917 }
918
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700919 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530920
921 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
922 dprintf(CRITICAL, "ERROR: Invalid page size\n");
923 return -1;
924 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700925 page_size = hdr->page_size;
926 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700927 }
928
Matthew Qin49e51fa2015-02-09 10:40:45 +0800929 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
930 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800931
Matthew Qin49e51fa2015-02-09 10:40:45 +0800932 image_addr = (unsigned char *)target_get_scratch_address();
933
934#if DEVICE_TREE
935 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
936 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
937#else
938 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
939#endif
940
941#if VERIFIED_BOOT
942 boot_verifier_init();
943#endif
944
945 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
946 {
947 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
948 return -1;
949 }
950
951 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
952 bs_set_timestamp(BS_KERNEL_LOAD_START);
953
954 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
955 {
956 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
957 return -1;
958 }
959
960 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
961 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
962
963 /* Authenticate Kernel */
964 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
965 (int) target_use_signed_kernel(),
966 device.is_unlocked,
967 device.is_tampered);
968
969 if(target_use_signed_kernel() && (!device.is_unlocked))
970 {
971 offset = imagesize_actual;
972 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
973 {
974 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
975 return -1;
976 }
977
978 /* Read signature */
979 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
980 {
981 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
982 return -1;
983 }
984
985 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
986 } else {
987 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
988 #ifdef TZ_SAVE_KERNEL_HASH
989 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
990 #endif /* TZ_SAVE_KERNEL_HASH */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800991 }
992
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700993 /*
994 * Update the kernel/ramdisk/tags address if the boot image header
995 * has default values, these default values come from mkbootimg when
996 * the boot image is flashed using fastboot flash:raw
997 */
Matthew Qin49e51fa2015-02-09 10:40:45 +0800998 kptr = (void *)(image_addr + page_size);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800999 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001000
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001001 /* Get virtual addresses since the hdr saves physical addresses. */
1002 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1003 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1004 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001005
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001006 /* Check if the addresses in the header are valid. */
1007 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1008 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1009 {
1010 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1011 return -1;
1012 }
1013
1014#ifndef DEVICE_TREE
1015 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1016 {
1017 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1018 return -1;
1019 }
1020#endif
1021
Amir Samuelov57a6fa22013-06-05 16:36:43 +03001022
Matthew Qin49e51fa2015-02-09 10:40:45 +08001023 /* Move kernel, ramdisk and device tree to correct address */
1024 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1025 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001026
Matthew Qin49e51fa2015-02-09 10:40:45 +08001027 #if DEVICE_TREE
1028 if(hdr->dt_size) {
1029 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1030 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001031
Matthew Qin49e51fa2015-02-09 10:40:45 +08001032 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1033 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1034 return -1;
1035 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001036
Matthew Qin49e51fa2015-02-09 10:40:45 +08001037 /* Find index of device tree within device tree table */
1038 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1039 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1040 return -1;
1041 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001042
Matthew Qin49e51fa2015-02-09 10:40:45 +08001043 /* Validate and Read device device tree in the tags_addr */
1044 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001045 {
1046 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1047 return -1;
1048 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001049
Matthew Qin49e51fa2015-02-09 10:40:45 +08001050 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
1051 } else {
1052 /* Validate the tags_addr */
1053 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001054 {
1055 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1056 return -1;
1057 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001058 /*
1059 * If appended dev tree is found, update the atags with
1060 * memory address to the DTB appended location on RAM.
1061 * Else update with the atags address in the kernel header
1062 */
1063 void *dtb;
1064 dtb = dev_tree_appended((void*) hdr->kernel_addr,
1065 kernel_actual,
1066 (void *)hdr->tags_addr);
1067 if (!dtb) {
1068 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001069 return -1;
1070 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001071 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001072 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001073
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001074 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1075 target_load_ssd_keystore();
1076
Shashank Mittal23b8f422010-04-16 19:27:21 -07001077unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001078
Dima Zavin77e41f32013-03-06 16:10:43 -08001079 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001080 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001081 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1082
1083 return 0;
1084}
1085
Dima Zavin214cc642009-01-26 11:16:21 -08001086int boot_linux_from_flash(void)
1087{
1088 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001089 struct ptentry *ptn;
1090 struct ptable *ptable;
1091 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001092
Shashank Mittalcd98d472011-08-02 14:29:24 -07001093 unsigned char *image_addr = 0;
1094 unsigned kernel_actual;
1095 unsigned ramdisk_actual;
1096 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001097 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001098
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001099#if DEVICE_TREE
1100 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001101 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001102 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001103 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001104#endif
1105
David Ng183a7422009-12-07 14:55:21 -08001106 if (target_is_emmc_boot()) {
1107 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1108 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1109 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1110 return -1;
1111 }
1112 goto continue_boot;
1113 }
1114
Dima Zavin214cc642009-01-26 11:16:21 -08001115 ptable = flash_get_ptable();
1116 if (ptable == NULL) {
1117 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1118 return -1;
1119 }
1120
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001121 if(!boot_into_recovery)
1122 {
1123 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001124
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001125 if (ptn == NULL) {
1126 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1127 return -1;
1128 }
1129 }
1130 else
1131 {
1132 ptn = ptable_find(ptable, "recovery");
1133 if (ptn == NULL) {
1134 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1135 return -1;
1136 }
Dima Zavin214cc642009-01-26 11:16:21 -08001137 }
1138
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001139 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001140 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1141 return -1;
1142 }
Dima Zavin214cc642009-01-26 11:16:21 -08001143
1144 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001145 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001146 return -1;
1147 }
1148
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001149 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001150 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 -08001151 return -1;
1152 }
1153
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001154 /*
1155 * Update the kernel/ramdisk/tags address if the boot image header
1156 * has default values, these default values come from mkbootimg when
1157 * the boot image is flashed using fastboot flash:raw
1158 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001159 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001160
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001161 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001162 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1163 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1164 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1165
1166 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1167 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1168
1169 /* Check if the addresses in the header are valid. */
1170 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1171 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1172 {
1173 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1174 return -1;
1175 }
1176
1177#ifndef DEVICE_TREE
1178 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1179 {
1180 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1181 return -1;
1182 }
1183#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001184
Shashank Mittalcd98d472011-08-02 14:29:24 -07001185 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001186 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001187 {
1188 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001189 offset = 0;
1190
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001191#if DEVICE_TREE
1192 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1193 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001194
1195 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1196 {
1197 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1198 return -1;
1199 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001200#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001201 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001202#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001203
Amol Jadib6be5c12012-11-14 13:39:51 -08001204 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001205 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001206
Shashank Mittalcd98d472011-08-02 14:29:24 -07001207 /* Read image without signature */
1208 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1209 {
1210 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1211 return -1;
1212 }
Dima Zavin214cc642009-01-26 11:16:21 -08001213
Amol Jadib6be5c12012-11-14 13:39:51 -08001214 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001215 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001216
Shashank Mittalcd98d472011-08-02 14:29:24 -07001217 offset = imagesize_actual;
1218 /* Read signature */
1219 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1220 {
1221 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001222 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001223 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001224
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301225 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001226
1227 /* Move kernel and ramdisk to correct address */
1228 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1229 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001230#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001231 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001232 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001233 {
1234 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1235 return -1;
1236 }
1237
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001238 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1239#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001240
1241 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001242 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001243 {
1244 write_device_info_flash(&device);
1245 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301246#if USE_PCOM_SECBOOT
1247 set_tamper_flag(device.is_tampered);
1248#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001249 }
1250 else
1251 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001252 offset = page_size;
1253
Amol Jadib6be5c12012-11-14 13:39:51 -08001254 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1255 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1256 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1257
1258 dprintf(INFO, "Loading boot image (%d): start\n",
1259 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001260 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001261
1262 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001263 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1264 return -1;
1265 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001266 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001267
Amol Jadib6be5c12012-11-14 13:39:51 -08001268 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001269 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1270 return -1;
1271 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001272 offset += ramdisk_actual;
1273
1274 dprintf(INFO, "Loading boot image (%d): done\n",
1275 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001276 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001277
1278 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001279 offset += second_actual;
1280 /* Second image loading not implemented. */
1281 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001282 }
1283
1284#if DEVICE_TREE
1285 if(hdr->dt_size != 0) {
1286
1287 /* Read the device tree table into buffer */
1288 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1289 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1290 return -1;
1291 }
1292
1293 table = (struct dt_table*) dt_buf;
1294
Deepa Dinamani19648b42013-09-05 17:05:55 -07001295 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001296 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1297 return -1;
1298 }
1299
Deepa Dinamani19648b42013-09-05 17:05:55 -07001300 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1301 if (!table)
1302 return -1;
1303
1304 /* Read the entire device tree table into buffer */
1305 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1306 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1307 return -1;
1308 }
1309
1310
Joel Kingaa335dc2013-06-03 16:11:08 -07001311 /* Find index of device tree within device tree table */
1312 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001313 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1314 return -1;
1315 }
1316
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001317 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001318 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001319 {
1320 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1321 return -1;
1322 }
1323
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001324 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001325 if(flash_read(ptn, offset + dt_entry.offset,
1326 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001327 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1328 return -1;
1329 }
1330 }
1331#endif
1332
Shashank Mittalcd98d472011-08-02 14:29:24 -07001333 }
David Ng183a7422009-12-07 14:55:21 -08001334continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001335
Dima Zavin214cc642009-01-26 11:16:21 -08001336 /* TODO: create/pass atags to kernel */
1337
Ajay Dudanie28a6072011-07-01 13:59:46 -07001338 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001339 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001340 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1341
1342 return 0;
1343}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001344
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001345BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001346void write_device_info_mmc(device_info *dev)
1347{
Shashank Mittal162244e2011-08-08 19:01:25 -07001348 unsigned long long ptn = 0;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001349#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001350 unsigned long long size;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001351#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001352 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001353 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001354 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001355
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001356#if VERIFIED_BOOT
1357 index = partition_get_index("devinfo");
1358#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001359 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001360#endif
1361
Shashank Mittal162244e2011-08-08 19:01:25 -07001362 ptn = partition_get_offset(index);
1363 if(ptn == 0)
1364 {
1365 return;
1366 }
1367
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001368 lun = partition_get_lun(index);
1369 mmc_set_lun(lun);
1370
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001371#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001372 size = partition_get_size(index);
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001373#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001374
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001375 blocksize = mmc_get_device_blocksize();
1376
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001377#if VERIFIED_BOOT
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001378 if(mmc_write(ptn, blocksize, (void *)dev))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001379#else
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001380 if(mmc_write((ptn + size - blocksize), blocksize, (void *)dev))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001381#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001382 {
1383 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1384 return;
1385 }
1386}
1387
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001388void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001389{
Shashank Mittal162244e2011-08-08 19:01:25 -07001390 unsigned long long ptn = 0;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001391#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001392 unsigned long long size;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001393#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001394 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001395 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001396
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001397#if VERIFIED_BOOT
1398 index = partition_get_index("devinfo");
1399#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001400 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001401#endif
1402
Shashank Mittal162244e2011-08-08 19:01:25 -07001403 ptn = partition_get_offset(index);
1404 if(ptn == 0)
1405 {
1406 return;
1407 }
1408
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001409 mmc_set_lun(partition_get_lun(index));
1410
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001411#if !VERIFIED_BOOT
Shashank Mittal162244e2011-08-08 19:01:25 -07001412 size = partition_get_size(index);
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001413#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001414
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001415 blocksize = mmc_get_device_blocksize();
1416
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001417#if VERIFIED_BOOT
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001418 if(mmc_read(ptn, (void *)info, blocksize))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001419#else
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001420 if(mmc_read((ptn + size - blocksize), (void *)info, blocksize))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001421#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001422 {
1423 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1424 return;
1425 }
1426
Shashank Mittal162244e2011-08-08 19:01:25 -07001427}
1428
1429void write_device_info_flash(device_info *dev)
1430{
1431 struct device_info *info = (void *) info_buf;
1432 struct ptentry *ptn;
1433 struct ptable *ptable;
1434
1435 ptable = flash_get_ptable();
1436 if (ptable == NULL)
1437 {
1438 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1439 return;
1440 }
1441
1442 ptn = ptable_find(ptable, "devinfo");
1443 if (ptn == NULL)
1444 {
1445 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1446 return;
1447 }
1448
1449 memcpy(info, dev, sizeof(device_info));
1450
1451 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1452 {
1453 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1454 return;
1455 }
1456}
1457
1458void read_device_info_flash(device_info *dev)
1459{
1460 struct device_info *info = (void*) info_buf;
1461 struct ptentry *ptn;
1462 struct ptable *ptable;
1463
1464 ptable = flash_get_ptable();
1465 if (ptable == NULL)
1466 {
1467 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1468 return;
1469 }
1470
1471 ptn = ptable_find(ptable, "devinfo");
1472 if (ptn == NULL)
1473 {
1474 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1475 return;
1476 }
1477
1478 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1479 {
1480 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1481 return;
1482 }
1483
1484 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1485 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001486 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1487 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001488 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001489 write_device_info_flash(info);
1490 }
1491 memcpy(dev, info, sizeof(device_info));
1492}
1493
1494void write_device_info(device_info *dev)
1495{
1496 if(target_is_emmc_boot())
1497 {
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001498 struct device_info *info = (void*) info_buf;
1499 memcpy(info, dev, sizeof(struct device_info));
1500
1501#if USE_RPMB_FOR_DEVINFO
1502 if (is_secure_boot_enable())
1503 write_device_info_rpmb((void*) info, mmc_get_device_blocksize());
1504#else
1505 write_device_info_mmc(info);
1506#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001507 }
1508 else
1509 {
1510 write_device_info_flash(dev);
1511 }
1512}
1513
1514void read_device_info(device_info *dev)
1515{
1516 if(target_is_emmc_boot())
1517 {
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001518 struct device_info *info = (void*) info_buf;
1519
1520#if USE_RPMB_FOR_DEVINFO
1521 if (is_secure_boot_enable())
1522 read_device_info_rpmb((void*) info, mmc_get_device_blocksize());
1523#else
1524 read_device_info_mmc(info);
1525#endif
1526
1527 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1528 {
1529 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1530 if (is_secure_boot_enable())
1531 info->is_unlocked = 0;
1532 else
1533 info->is_unlocked = 1;
1534 info->is_verified = 0;
1535 info->is_tampered = 0;
1536 info->charger_screen_enabled = 0;
1537
1538 write_device_info(info);
1539 }
1540 memcpy(dev, info, sizeof(device_info));
Shashank Mittal162244e2011-08-08 19:01:25 -07001541 }
1542 else
1543 {
1544 read_device_info_flash(dev);
1545 }
1546}
1547
1548void reset_device_info()
1549{
1550 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001551 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001552 write_device_info(&device);
1553}
1554
1555void set_device_root()
1556{
1557 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001558 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001559 write_device_info(&device);
1560}
1561
Amol Jadicb524072012-08-09 16:40:18 -07001562#if DEVICE_TREE
1563int copy_dtb(uint8_t *boot_image_start)
1564{
1565 uint32 dt_image_offset = 0;
1566 uint32_t n;
1567 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001568 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001569 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001570
1571 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1572
Amol Jadicb524072012-08-09 16:40:18 -07001573 if(hdr->dt_size != 0) {
1574
1575 /* add kernel offset */
1576 dt_image_offset += page_size;
1577 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1578 dt_image_offset += n;
1579
1580 /* add ramdisk offset */
1581 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1582 dt_image_offset += n;
1583
1584 /* add second offset */
1585 if(hdr->second_size != 0) {
1586 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1587 dt_image_offset += n;
1588 }
1589
1590 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001591 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001592
Deepa Dinamani19648b42013-09-05 17:05:55 -07001593 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001594 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1595 return -1;
1596 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001597 /* Find index of device tree within device tree table */
1598 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001599 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1600 return -1;
1601 }
1602
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001603 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001604 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001605 {
1606 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1607 return -1;
1608 }
1609
Amol Jadicb524072012-08-09 16:40:18 -07001610 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001611 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001612 boot_image_start + dt_image_offset + dt_entry.offset,
1613 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001614 } else
1615 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001616
1617 /* Everything looks fine. Return success. */
1618 return 0;
1619}
1620#endif
1621
Brian Swetland9c4c0752009-01-25 16:23:50 -08001622void cmd_boot(const char *arg, void *data, unsigned sz)
1623{
1624 unsigned kernel_actual;
1625 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001626 uint32_t image_actual;
1627 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001628 uint32_t sig_actual = SIGNATURE_SIZE;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001629 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001630 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001631 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001632 int ret = 0;
1633 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001634
Amit Blay6281ebc2015-01-11 14:44:08 +02001635#ifdef MDTP_SUPPORT
1636 /* Go through Firmware Lock verification before continue with boot process */
1637 mdtp_fwlock_verify_lock();
Reut Zysman18411272015-02-09 13:47:27 +02001638 display_image_on_screen();
Amit Blay6281ebc2015-01-11 14:44:08 +02001639#endif /* MDTP_SUPPORT */
1640
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001641#if VERIFIED_BOOT
1642 if(!device.is_unlocked)
1643 {
1644 fastboot_fail("unlock device to use this command");
1645 return;
1646 }
1647#endif
1648
Brian Swetland9c4c0752009-01-25 16:23:50 -08001649 if (sz < sizeof(hdr)) {
1650 fastboot_fail("invalid bootimage header");
1651 return;
1652 }
1653
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001654 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001655
1656 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001657 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001658
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001659 if(target_is_emmc_boot() && hdr->page_size) {
1660 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001661 page_mask = page_size - 1;
1662 }
1663
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001664 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1665 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001666#if DEVICE_TREE
1667 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1668#endif
1669
1670 image_actual = ADD_OF(page_size, kernel_actual);
1671 image_actual = ADD_OF(image_actual, ramdisk_actual);
1672 image_actual = ADD_OF(image_actual, dt_actual);
1673
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001674 if (target_use_signed_kernel() && (!device.is_unlocked))
1675 image_actual = ADD_OF(image_actual, sig_actual);
1676
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001677 /* sz should have atleast raw boot image */
1678 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001679 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001680 return;
1681 }
1682
1683 /* Verify the boot image
1684 * device & page_size are initialized in aboot_init
1685 */
1686 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001687 /* Pass size excluding signature size, otherwise we would try to
1688 * access signature beyond its length
1689 */
1690 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001691
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001692 /*
1693 * Update the kernel/ramdisk/tags address if the boot image header
1694 * has default values, these default values come from mkbootimg when
1695 * the boot image is flashed using fastboot flash:raw
1696 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001697 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1698 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001699
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001700 /* Get virtual addresses since the hdr saves physical addresses. */
1701 hdr->kernel_addr = VA(hdr->kernel_addr);
1702 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1703 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001704
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001705 /* Check if the addresses in the header are valid. */
1706 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1707 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1708 {
1709 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001710 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001711 }
1712
Amol Jadicb524072012-08-09 16:40:18 -07001713#if DEVICE_TREE
1714 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001715 ret = copy_dtb(data);
1716
1717 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001718#else
1719 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1720 {
1721 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001722 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001723 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001724#endif
1725
1726 /* Load ramdisk & kernel */
1727 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1728 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1729
1730#if DEVICE_TREE
1731 /*
1732 * If dtb is not found look for appended DTB in the kernel.
1733 * If appended dev tree is found, update the atags with
1734 * memory address to the DTB appended location on RAM.
1735 * Else update with the atags address in the kernel header
1736 */
1737 if (!dtb_copied) {
1738 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001739 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1740 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001741 if (!dtb) {
1742 fastboot_fail("dtb not found");
1743 return;
1744 }
Amol Jadicb524072012-08-09 16:40:18 -07001745 }
1746#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001747
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001748#ifndef DEVICE_TREE
1749 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1750 {
1751 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001752 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001753 }
1754#endif
1755
Brian Swetland9c4c0752009-01-25 16:23:50 -08001756 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001757 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001758
Dima Zavin77e41f32013-03-06 16:10:43 -08001759 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001760 (const char*) hdr->cmdline, board_machtype(),
1761 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001762}
1763
Sridhar Parasurame94e8152014-10-24 14:06:03 -07001764void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08001765{
1766 struct ptentry *ptn;
1767 struct ptable *ptable;
1768
1769 ptable = flash_get_ptable();
1770 if (ptable == NULL) {
1771 fastboot_fail("partition table doesn't exist");
1772 return;
1773 }
1774
1775 ptn = ptable_find(ptable, arg);
1776 if (ptn == NULL) {
1777 fastboot_fail("unknown partition name");
1778 return;
1779 }
1780
1781 if (flash_erase(ptn)) {
1782 fastboot_fail("failed to erase partition");
1783 return;
1784 }
1785 fastboot_okay("");
1786}
1787
Bikas Gurungd48bd242010-09-04 19:54:32 -07001788
1789void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1790{
1791 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001792 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001793 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001794 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001795
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001796#if VERIFIED_BOOT
1797 if(!strcmp(arg, KEYSTORE_PTN_NAME))
1798 {
1799 if(!device.is_unlocked)
1800 {
1801 fastboot_fail("unlock device to erase keystore");
1802 return;
1803 }
1804 }
1805#endif
1806
Kinson Chikf1a43512011-07-14 11:28:39 -07001807 index = partition_get_index(arg);
1808 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001809 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001810
Kinson Chikf1a43512011-07-14 11:28:39 -07001811 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001812 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001813 return;
1814 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001815
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001816 lun = partition_get_lun(index);
1817 mmc_set_lun(lun);
1818
Sridhar Parasuramd7957122015-02-27 11:33:40 -08001819 if (platform_boot_dev_isemmc())
1820 {
1821 if (mmc_erase_card(ptn, size)) {
1822 fastboot_fail("failed to erase partition\n");
1823 return;
1824 }
1825 } else {
1826 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
1827 size = partition_get_size(index);
1828 if (size > DEFAULT_ERASE_SIZE)
1829 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08001830
Sridhar Parasuramd7957122015-02-27 11:33:40 -08001831 /* Simple inefficient version of erase. Just writing
1832 0 in first several blocks */
1833 if (mmc_write(ptn , size, (unsigned int *)out)) {
1834 fastboot_fail("failed to erase partition");
1835 return;
1836 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07001837 }
1838 fastboot_okay("");
1839}
1840
Sridhar Parasurame94e8152014-10-24 14:06:03 -07001841void cmd_erase(const char *arg, void *data, unsigned sz)
1842{
1843 if(target_is_emmc_boot())
1844 cmd_erase_mmc(arg, data, sz);
1845 else
1846 cmd_erase_nand(arg, data, sz);
1847}
Bikas Gurungd48bd242010-09-04 19:54:32 -07001848
Ajay Dudani5c761132011-04-07 20:19:04 -07001849void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001850{
1851 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001852 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001853 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001854 char *token = NULL;
1855 char *pname = NULL;
1856 uint8_t lun = 0;
1857 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001858
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301859 token = strtok((char *)arg, ":");
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001860 pname = token;
1861 token = strtok(NULL, ":");
1862 if(token)
1863 {
1864 lun = atoi(token);
1865 mmc_set_lun(lun);
1866 lun_set = true;
1867 }
1868
Mao Jinlong226f33a2014-07-04 17:24:10 +08001869 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07001870 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08001871 if (!strcmp(pname, "partition"))
1872 {
1873 dprintf(INFO, "Attempt to write partition image.\n");
1874 if (write_partition(sz, (unsigned char *) data)) {
1875 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07001876 return;
1877 }
1878 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08001879 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001880 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001881#if VERIFIED_BOOT
1882 if(!strcmp(pname, KEYSTORE_PTN_NAME))
1883 {
1884 if(!device.is_unlocked)
1885 {
1886 fastboot_fail("unlock device to flash keystore");
1887 return;
1888 }
1889 if(!boot_verify_validate_keystore((unsigned char *)data))
1890 {
1891 fastboot_fail("image is not a keystore file");
1892 return;
1893 }
1894 }
1895#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08001896 index = partition_get_index(pname);
1897 ptn = partition_get_offset(index);
1898 if(ptn == 0) {
1899 fastboot_fail("partition table doesn't exist");
1900 return;
1901 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001902
Mao Jinlong226f33a2014-07-04 17:24:10 +08001903 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
1904 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1905 fastboot_fail("image is not a boot image");
1906 return;
1907 }
1908 }
1909
1910 if(!lun_set)
1911 {
1912 lun = partition_get_lun(index);
1913 mmc_set_lun(lun);
1914 }
1915
1916 size = partition_get_size(index);
1917 if (ROUND_TO_PAGE(sz,511) > size) {
1918 fastboot_fail("size too large");
1919 return;
1920 }
1921 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1922 fastboot_fail("flash write failure");
1923 return;
1924 }
Greg Grisco6e754772011-06-23 12:19:39 -07001925 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001926 }
1927 fastboot_okay("");
1928 return;
1929}
1930
Ajay Dudanide984792015-03-02 09:57:41 -08001931void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
1932{
1933 int i, images;
1934 meta_header_t *meta_header;
1935 img_header_entry_t *img_header_entry;
1936
1937 meta_header = (meta_header_t*) data;
1938 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
1939
1940 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
1941
1942 for (i=0; i<images; i++) {
1943
1944 if((img_header_entry[i].ptn_name == NULL) ||
1945 (img_header_entry[i].start_offset == 0) ||
1946 (img_header_entry[i].size == 0))
1947 break;
1948
1949 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
1950 (void *) data + img_header_entry[i].start_offset,
1951 img_header_entry[i].size);
1952 }
1953
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08001954 if (!strncmp(arg, "bootloader", strlen("bootloader")))
1955 {
1956 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
1957 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
1958 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
1959 }
1960 else
1961 {
1962 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
1963 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
1964 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
1965 }
1966
1967 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08001968 fastboot_okay("");
1969 return;
1970}
1971
Ajay Dudani5c761132011-04-07 20:19:04 -07001972void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1973{
1974 unsigned int chunk;
1975 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001976 uint32_t *fill_buf = NULL;
1977 uint32_t fill_val;
1978 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001979 sparse_header_t *sparse_header;
1980 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001981 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001982 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301983 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001984 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301985 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001986 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001987
Kinson Chikf1a43512011-07-14 11:28:39 -07001988 index = partition_get_index(arg);
1989 ptn = partition_get_offset(index);
1990 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001991 fastboot_fail("partition table doesn't exist");
1992 return;
1993 }
1994
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301995 size = partition_get_size(index);
1996 if (ROUND_TO_PAGE(sz,511) > size) {
1997 fastboot_fail("size too large");
1998 return;
1999 }
2000
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002001 lun = partition_get_lun(index);
2002 mmc_set_lun(lun);
2003
Ajay Dudani5c761132011-04-07 20:19:04 -07002004 /* Read and skip over sparse image header */
2005 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08002006 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
2007 fastboot_fail("size too large");
2008 return;
2009 }
2010
Ajay Dudani5c761132011-04-07 20:19:04 -07002011 data += sparse_header->file_hdr_sz;
2012 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
2013 {
2014 /* Skip the remaining bytes in a header that is longer than
2015 * we expected.
2016 */
2017 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
2018 }
2019
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002020 dprintf (SPEW, "=== Sparse Image Header ===\n");
2021 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2022 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2023 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2024 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2025 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2026 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2027 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2028 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002029
2030 /* Start processing chunks */
2031 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2032 {
2033 /* Read and skip over chunk header */
2034 chunk_header = (chunk_header_t *) data;
2035 data += sizeof(chunk_header_t);
2036
2037 dprintf (SPEW, "=== Chunk Header ===\n");
2038 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2039 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2040 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2041
2042 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
2043 {
2044 /* Skip the remaining bytes in a header that is longer than
2045 * we expected.
2046 */
2047 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
2048 }
2049
2050 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
2051 switch (chunk_header->chunk_type)
2052 {
2053 case CHUNK_TYPE_RAW:
2054 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2055 chunk_data_sz))
2056 {
2057 fastboot_fail("Bogus chunk size for chunk type Raw");
2058 return;
2059 }
2060
Ajay Dudaniab18f022011-05-12 14:39:22 -07002061 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2062 chunk_data_sz,
2063 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002064 {
2065 fastboot_fail("flash write failure");
2066 return;
2067 }
2068 total_blocks += chunk_header->chunk_sz;
2069 data += chunk_data_sz;
2070 break;
2071
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002072 case CHUNK_TYPE_FILL:
2073 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2074 sizeof(uint32_t)))
2075 {
2076 fastboot_fail("Bogus chunk size for chunk type FILL");
2077 return;
2078 }
2079
2080 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2081 if (!fill_buf)
2082 {
2083 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2084 return;
2085 }
2086
2087 fill_val = *(uint32_t *)data;
2088 data = (char *) data + sizeof(uint32_t);
2089 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
2090
2091 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2092 {
2093 fill_buf[i] = fill_val;
2094 }
2095
2096 for (i = 0; i < chunk_blk_cnt; i++)
2097 {
2098 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2099 sparse_header->blk_sz,
2100 fill_buf))
2101 {
2102 fastboot_fail("flash write failure");
2103 free(fill_buf);
2104 return;
2105 }
2106
2107 total_blocks++;
2108 }
2109
2110 free(fill_buf);
2111 break;
2112
Ajay Dudani5c761132011-04-07 20:19:04 -07002113 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002114 total_blocks += chunk_header->chunk_sz;
2115 break;
2116
Ajay Dudani5c761132011-04-07 20:19:04 -07002117 case CHUNK_TYPE_CRC:
2118 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2119 {
2120 fastboot_fail("Bogus chunk size for chunk type Dont Care");
2121 return;
2122 }
2123 total_blocks += chunk_header->chunk_sz;
2124 data += chunk_data_sz;
2125 break;
2126
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002127 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002128 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002129 fastboot_fail("Unknown chunk type");
2130 return;
2131 }
2132 }
2133
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002134 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2135 total_blocks, sparse_header->total_blks);
2136
2137 if(total_blocks != sparse_header->total_blks)
2138 {
2139 fastboot_fail("sparse image write failure");
2140 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002141
2142 fastboot_okay("");
2143 return;
2144}
2145
2146void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2147{
2148 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002149 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002150
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002151#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002152 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2153 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002154 int ret=0;
2155 uint32 major_version=0;
2156 uint32 minor_version=0;
2157
2158 ret = scm_svc_version(&major_version,&minor_version);
2159 if(!ret)
2160 {
2161 if(major_version >= 2)
2162 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002163 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002164 {
2165 ret = encrypt_scm((uint32 **) &data, &sz);
2166 if (ret != 0) {
2167 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2168 return;
2169 }
2170
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002171 /* Protect only for SSD */
2172 if (!strcmp(arg, "ssd")) {
2173 ret = scm_protect_keystore((uint32 *) data, sz);
2174 if (ret != 0) {
2175 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2176 return;
2177 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002178 }
2179 }
2180 else
2181 {
2182 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2183 if(ret != 0)
2184 {
2185 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2186 return;
2187 }
2188 }
2189 }
2190 else
2191 {
2192 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2193 magic_number[1] == DECRYPT_MAGIC_1)
2194 {
2195 ret = decrypt_scm((uint32 **) &data, &sz);
2196 if (ret != 0) {
2197 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2198 return;
2199 }
2200 }
2201 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2202 magic_number[1] == ENCRYPT_MAGIC_1)
2203 {
2204 ret = encrypt_scm((uint32 **) &data, &sz);
2205 if (ret != 0) {
2206 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2207 return;
2208 }
2209 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002210 }
2211 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002212 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002213 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002214 dprintf(CRITICAL,"INVALID SVC Version\n");
2215 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002216 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002217#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002218
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002219#if VERIFIED_BOOT
2220 if(!device.is_unlocked && !device.is_verified)
2221 {
2222 fastboot_fail("device is locked. Cannot flash images");
2223 return;
2224 }
2225 if(!device.is_unlocked && device.is_verified)
2226 {
2227 if(!boot_verify_flash_allowed(arg))
2228 {
2229 fastboot_fail("cannot flash this partition in verified state");
2230 return;
2231 }
2232 }
2233#endif
2234
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002235 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002236 meta_header = (meta_header_t *) data;
2237 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002238 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002239 else if (meta_header->magic == META_HEADER_MAGIC)
2240 cmd_flash_meta_img(arg, data, sz);
2241 else
2242 cmd_flash_mmc_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002243 return;
2244}
2245
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002246void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002247{
2248 struct ptentry *ptn;
2249 struct ptable *ptable;
2250 unsigned extra = 0;
2251
2252 ptable = flash_get_ptable();
2253 if (ptable == NULL) {
2254 fastboot_fail("partition table doesn't exist");
2255 return;
2256 }
2257
2258 ptn = ptable_find(ptable, arg);
2259 if (ptn == NULL) {
2260 fastboot_fail("unknown partition name");
2261 return;
2262 }
2263
2264 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2265 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2266 fastboot_fail("image is not a boot image");
2267 return;
2268 }
2269 }
2270
Amol Jadi5c61a952012-05-04 17:05:35 -07002271 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002272 || !strcmp(ptn->name, "userdata")
2273 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002274 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002275 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002276 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002277 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002278 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002279
2280 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02002281 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
2282 if (flash_ubi_img(ptn, data, sz)) {
2283 fastboot_fail("flash write failure");
2284 return;
2285 }
2286 } else {
2287 if (flash_write(ptn, extra, data, sz)) {
2288 fastboot_fail("flash write failure");
2289 return;
2290 }
Dima Zavin214cc642009-01-26 11:16:21 -08002291 }
2292 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2293 fastboot_okay("");
2294}
2295
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002296void cmd_flash(const char *arg, void *data, unsigned sz)
2297{
2298 if(target_is_emmc_boot())
2299 cmd_flash_mmc(arg, data, sz);
2300 else
2301 cmd_flash_nand(arg, data, sz);
2302}
2303
Dima Zavin214cc642009-01-26 11:16:21 -08002304void cmd_continue(const char *arg, void *data, unsigned sz)
2305{
2306 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002307 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002308
Amit Blay6281ebc2015-01-11 14:44:08 +02002309#ifdef MDTP_SUPPORT
2310 /* Go through Firmware Lock verification before continue with boot process */
2311 mdtp_fwlock_verify_lock();
Reut Zysman18411272015-02-09 13:47:27 +02002312 display_image_on_screen();
Amit Blay6281ebc2015-01-11 14:44:08 +02002313#endif /* MDTP_SUPPORT */
2314
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002315 if (target_is_emmc_boot())
2316 {
2317 boot_linux_from_mmc();
2318 }
2319 else
2320 {
2321 boot_linux_from_flash();
2322 }
Dima Zavin214cc642009-01-26 11:16:21 -08002323}
2324
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002325void cmd_reboot(const char *arg, void *data, unsigned sz)
2326{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002327 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002328 fastboot_okay("");
2329 reboot_device(0);
2330}
2331
2332void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2333{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002334 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002335 fastboot_okay("");
2336 reboot_device(FASTBOOT_MODE);
2337}
2338
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002339void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2340{
2341 dprintf(INFO, "Enabling charger screen check\n");
2342 device.charger_screen_enabled = 1;
2343 write_device_info(&device);
2344 fastboot_okay("");
2345}
2346
2347void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2348{
2349 dprintf(INFO, "Disabling charger screen check\n");
2350 device.charger_screen_enabled = 0;
2351 write_device_info(&device);
2352 fastboot_okay("");
2353}
2354
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302355void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2356{
2357 dprintf(INFO, "Selecting display panel %s\n", arg);
2358 if (arg)
2359 strlcpy(device.display_panel, arg,
2360 sizeof(device.display_panel));
2361 write_device_info(&device);
2362 fastboot_okay("");
2363}
2364
Shashank Mittal162244e2011-08-08 19:01:25 -07002365void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2366{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002367 /* TODO: Wipe user data */
2368 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002369 {
2370 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002371 device.is_verified = 0;
2372 write_device_info(&device);
2373 }
2374 fastboot_okay("");
2375}
2376
2377void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2378{
2379 /* TODO: Wipe user data */
2380 if(device.is_unlocked || device.is_verified)
2381 {
2382 device.is_unlocked = 0;
2383 device.is_verified = 0;
2384 write_device_info(&device);
2385 }
2386 fastboot_okay("");
2387}
2388
2389void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2390{
2391 /* TODO: Wipe user data */
2392 if(device.is_unlocked || !device.is_verified)
2393 {
2394 device.is_unlocked = 0;
2395 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002396 write_device_info(&device);
2397 }
2398 fastboot_okay("");
2399}
2400
Shashank Mittala0032282011-08-26 14:50:11 -07002401void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2402{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302403 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002404 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002405 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002406 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2407 fastboot_info(response);
2408 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002409 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302410 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2411 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002412 fastboot_okay("");
2413}
2414
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002415void cmd_preflash(const char *arg, void *data, unsigned sz)
2416{
2417 fastboot_okay("");
2418}
2419
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302420struct fbimage* splash_screen_flash();
2421
2422int splash_screen_check_header(struct fbimage *logo)
2423{
2424 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2425 return -1;
2426 if (logo->header.width == 0 || logo->header.height == 0)
2427 return -1;
2428 return 0;
2429}
2430
2431struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002432{
2433 struct ptentry *ptn;
2434 struct ptable *ptable;
2435 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002436 struct fbimage *logo = NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002437
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302438
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002439 logo = (struct fbimage *) malloc(ROUNDUP(page_size, sizeof(struct fbimage)));
2440 ASSERT(logo);
2441
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302442 ptable = flash_get_ptable();
2443 if (ptable == NULL) {
2444 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002445 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302446 }
2447 ptn = ptable_find(ptable, "splash");
2448 if (ptn == NULL) {
2449 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002450 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302451 }
2452
2453 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2454 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002455 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302456 }
2457
2458 if (splash_screen_check_header(logo)) {
2459 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002460 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302461 }
2462
2463 fb_display = fbcon_display();
2464 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302465 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2466 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back to default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002467 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002468 }
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302469 uint8_t *base = (uint8_t *) fb_display->base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302470 if (flash_read(ptn + sizeof(logo->header), 0,
2471 base,
2472 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2473 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302474 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002475 goto err;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002476 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302477 logo->image = base;
2478 }
2479
2480 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002481
2482err:
2483 free(logo);
2484 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302485}
2486
2487struct fbimage* splash_screen_mmc()
2488{
2489 int index = INVALID_PTN;
2490 unsigned long long ptn = 0;
2491 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002492 struct fbimage *logo = NULL;
Channagoud Kadabida117392014-11-24 11:01:12 -08002493 uint32_t blocksize;
2494 uint32_t readsize;
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002495 uint32_t logosize;
Channagoud Kadabida117392014-11-24 11:01:12 -08002496 uint32_t ptn_size;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302497
2498 index = partition_get_index("splash");
2499 if (index == 0) {
2500 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2501 return NULL;
2502 }
2503
2504 ptn = partition_get_offset(index);
2505 if (ptn == 0) {
2506 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2507 return NULL;
2508 }
2509
Channagoud Kadabida117392014-11-24 11:01:12 -08002510 ptn_size = partition_get_size(index);
2511 blocksize = mmc_get_device_blocksize();
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002512 logosize = ROUNDUP(sizeof(logo->header), blocksize);
Channagoud Kadabida117392014-11-24 11:01:12 -08002513
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002514 logo = (struct fbimage *)memalign(CACHE_LINE, ROUNDUP(logosize, CACHE_LINE));
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002515 ASSERT(logo);
2516
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002517 if (mmc_read(ptn, (uint32_t *) logo, logosize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302518 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002519 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302520 }
2521
2522 if (splash_screen_check_header(logo)) {
2523 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002524 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302525 }
2526
2527 fb_display = fbcon_display();
2528 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302529 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2530 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002531 goto err;
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302532 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302533 uint8_t *base = (uint8_t *) fb_display->base;
Channagoud Kadabida117392014-11-24 11:01:12 -08002534 readsize = ROUNDUP((logo->header.width * logo->header.height * fb_display->bpp/8), blocksize);
2535
2536 if (readsize > ptn_size)
2537 {
2538 dprintf(CRITICAL, "@%d:Invalid logo header readsize:%u exceeds ptn_size:%u\n", __LINE__, readsize,ptn_size);
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002539 goto err;
Channagoud Kadabida117392014-11-24 11:01:12 -08002540 }
2541
Channagoud Kadabicccd02c2015-02-19 17:51:40 -08002542 if (mmc_read(ptn + logosize,(uint32_t *)base, readsize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302543 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302544 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002545 goto err;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302546 }
2547
2548 logo->image = base;
2549 }
2550
2551 return logo;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08002552
2553err:
2554 free(logo);
2555 return NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302556}
2557
2558
2559struct fbimage* fetch_image_from_partition()
2560{
2561 if (target_is_emmc_boot()) {
2562 return splash_screen_mmc();
2563 } else {
2564 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002565 }
2566}
2567
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002568/* Get the size from partiton name */
2569static void get_partition_size(const char *arg, char *response)
2570{
2571 uint64_t ptn = 0;
2572 uint64_t size;
2573 int index = INVALID_PTN;
2574
2575 index = partition_get_index(arg);
2576
2577 if (index == INVALID_PTN)
2578 {
2579 dprintf(CRITICAL, "Invalid partition index\n");
2580 return;
2581 }
2582
2583 ptn = partition_get_offset(index);
2584
2585 if(!ptn)
2586 {
2587 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2588 return;
2589 }
2590
2591 size = partition_get_size(index);
2592
2593 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2594 return;
2595}
2596
2597/*
2598 * Publish the partition type & size info
2599 * fastboot getvar will publish the required information.
2600 * fastboot getvar partition_size:<partition_name>: partition size in hex
2601 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2602 */
2603static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2604{
2605 uint8_t i;
2606
2607 for (i = 0; i < num_parts; i++) {
2608 get_partition_size(info[i].part_name, info[i].size_response);
2609
2610 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2611 {
2612 dprintf(CRITICAL, "partition size name truncated\n");
2613 return;
2614 }
2615 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2616 {
2617 dprintf(CRITICAL, "partition type name truncated\n");
2618 return;
2619 }
2620
2621 /* publish partition size & type info */
2622 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2623 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2624 }
2625}
2626
Amol Jadi5edf3552013-07-23 14:15:34 -07002627/* register commands and variables for fastboot */
2628void aboot_fastboot_register_commands(void)
2629{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002630 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07002631
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002632 struct fastboot_cmd_desc cmd_list[] = {
2633 /* By default the enabled list is empty. */
2634 {"", NULL},
2635 /* move commands enclosed within the below ifndef to here
2636 * if they need to be enabled in user build.
2637 */
2638#ifndef DISABLE_FASTBOOT_CMDS
2639 /* Register the following commands only for non-user builds */
2640 {"flash:", cmd_flash},
2641 {"erase:", cmd_erase},
2642 {"boot", cmd_boot},
2643 {"continue", cmd_continue},
2644 {"reboot", cmd_reboot},
2645 {"reboot-bootloader", cmd_reboot_bootloader},
2646 {"oem unlock", cmd_oem_unlock},
2647 {"oem lock", cmd_oem_lock},
2648 {"oem verified", cmd_oem_verified},
2649 {"oem device-info", cmd_oem_devinfo},
2650 {"preflash", cmd_preflash},
2651 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
2652 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
Aravind Venkateswaranbcca9b12014-11-04 17:04:01 -08002653 {"oem select-display-panel", cmd_oem_select_display_panel},
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002654#endif
2655 };
2656
2657 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
2658 for (i = 1; i < fastboot_cmds_count; i++)
2659 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
2660
Amol Jadi5edf3552013-07-23 14:15:34 -07002661 /* publish variables and their values */
2662 fastboot_publish("product", TARGET(BOARD));
2663 fastboot_publish("kernel", "lk");
2664 fastboot_publish("serialno", sn_buf);
2665
2666 /*
2667 * partition info is supported only for emmc partitions
2668 * Calling this for NAND prints some error messages which
2669 * is harmless but misleading. Avoid calling this for NAND
2670 * devices.
2671 */
2672 if (target_is_emmc_boot())
2673 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2674
2675 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002676 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2677 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002678 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002679 /* Is the charger screen check enabled */
2680 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2681 device.charger_screen_enabled);
2682 fastboot_publish("charger-screen-enabled",
2683 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302684 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2685 device.display_panel);
2686 fastboot_publish("display-panel",
2687 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002688 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
2689 fastboot_publish("version-baseband", (const char *) device.radio_version);
Amol Jadi5edf3552013-07-23 14:15:34 -07002690}
2691
Brian Swetland9c4c0752009-01-25 16:23:50 -08002692void aboot_init(const struct app_descriptor *app)
2693{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002694 unsigned reboot_mode = 0;
lijuang72875802015-02-10 10:35:12 +08002695 unsigned hard_reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002696 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002697
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002698 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002699 if (target_is_emmc_boot())
2700 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002701 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002702 page_mask = page_size - 1;
2703 }
2704 else
2705 {
2706 page_size = flash_page_size();
2707 page_mask = page_size - 1;
2708 }
2709
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002710 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2711
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002712 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002713
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002714 /* Display splash screen if enabled */
2715#if DISPLAY_SPLASH_SCREEN
2716 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302717 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002718 dprintf(SPEW, "Display Init: Done\n");
2719#endif
2720
2721
Greg Griscod6250552011-06-29 14:40:23 -07002722 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002723 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002724
Dhaval Patel223ec952013-07-18 14:49:44 -07002725 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2726
Matthew Qindefd5562014-07-11 18:02:40 +08002727 /*
2728 * Check power off reason if user force reset,
2729 * if yes phone will do normal boot.
2730 */
2731 if (is_user_force_reset())
2732 goto normal_boot;
2733
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002734 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002735 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002736 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002737 dprintf(ALWAYS,"dload mode key sequence detected\n");
2738 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002739 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002740 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002741 }
2742 else
2743 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302744 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002745 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002746 }
2747 boot_into_fastboot = true;
2748 }
2749 if (!boot_into_fastboot)
2750 {
2751 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2752 boot_into_recovery = 1;
2753 if (!boot_into_recovery &&
2754 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002755 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002756 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002757 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002758 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002759 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002760 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002761
Ajay Dudani77421292010-10-27 19:34:06 -07002762 reboot_mode = check_reboot_mode();
lijuang72875802015-02-10 10:35:12 +08002763 hard_reboot_mode = check_hard_reboot_mode();
2764 if (reboot_mode == RECOVERY_MODE ||
2765 hard_reboot_mode == RECOVERY_HARD_RESET_MODE) {
Ajay Dudani77421292010-10-27 19:34:06 -07002766 boot_into_recovery = 1;
lijuang72875802015-02-10 10:35:12 +08002767 } else if(reboot_mode == FASTBOOT_MODE ||
2768 hard_reboot_mode == FASTBOOT_HARD_RESET_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002769 boot_into_fastboot = true;
lijuang72875802015-02-10 10:35:12 +08002770 } else if(reboot_mode == ALARM_BOOT ||
2771 hard_reboot_mode == RTC_HARD_RESET_MODE) {
Matthew Qind886f3c2014-01-17 16:52:01 +08002772 boot_reason_alarm = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002773 }
2774
Matthew Qindefd5562014-07-11 18:02:40 +08002775normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03002776 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002777 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002778 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002779 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002780 if(emmc_recovery_init())
2781 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2782 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002783 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002784 if((device.is_unlocked) || (device.is_tampered))
2785 {
2786 #ifdef TZ_TAMPER_FUSE
2787 set_tamper_fuse_cmd();
2788 #endif
2789 #if USE_PCOM_SECBOOT
2790 set_tamper_flag(device.is_tampered);
2791 #endif
2792 }
Shashank Mittala0032282011-08-26 14:50:11 -07002793 }
Amit Blay6281ebc2015-01-11 14:44:08 +02002794
2795#ifdef MDTP_SUPPORT
2796 /* Go through Firmware Lock verification before continue with boot process */
2797 mdtp_fwlock_verify_lock();
Reut Zysman18411272015-02-09 13:47:27 +02002798 display_image_on_screen();
Amit Blay6281ebc2015-01-11 14:44:08 +02002799#endif /* MDTP_SUPPORT */
2800
Pavel Nedev5d91d412013-04-29 11:34:24 +03002801 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002802 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002803 else
2804 {
2805 recovery_init();
2806 #if USE_PCOM_SECBOOT
2807 if((device.is_unlocked) || (device.is_tampered))
2808 set_tamper_flag(device.is_tampered);
2809 #endif
2810 boot_linux_from_flash();
2811 }
2812 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2813 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002814 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002815
Amol Jadi5edf3552013-07-23 14:15:34 -07002816 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002817
Amol Jadi5edf3552013-07-23 14:15:34 -07002818 /* register aboot specific fastboot commands */
2819 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002820
Amol Jadi5edf3552013-07-23 14:15:34 -07002821 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002822 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002823
2824 /* initialize and start fastboot */
2825 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002826}
2827
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002828uint32_t get_page_size()
2829{
2830 return page_size;
2831}
2832
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002833/*
2834 * Calculated and save hash (SHA256) for non-signed boot image.
2835 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002836 * @param image_addr - Boot image address
2837 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002838 *
2839 * @return int - 0 on success, negative value on failure.
2840 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002841static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002842{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002843 unsigned int digest[8];
2844#if IMAGE_VERIF_ALGO_SHA1
2845 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
2846#else
2847 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
2848#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002849
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002850 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002851 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002852
2853 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002854 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002855
2856 return 0;
2857}
2858
Brian Swetland9c4c0752009-01-25 16:23:50 -08002859APP_START(aboot)
2860 .init = aboot_init,
2861APP_END