blob: f6c4451e98239582e97ebbd5ea265ef180629ceb [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Kuogee Hsieh55e92c52013-12-20 08:34:10 -08005 * Copyright (c) 2009-2014, 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>
43#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080044#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080045#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080046#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070047#include <target.h>
48#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070049#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070050#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070051#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070052#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070053#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030054#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070055#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070056#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070057#include <boot_verifier.h>
Dima Zavin214cc642009-01-26 11:16:21 -080058
Neeti Desai17379b82012-06-04 18:42:53 -070059#if DEVICE_TREE
60#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070061#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070062#endif
63
Shashank Mittalcd98d472011-08-02 14:29:24 -070064#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080065#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080066#include "bootimg.h"
67#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070068#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070069#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070070#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070071#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070072#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070073
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070074extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070075extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070076extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070077extern int get_target_boot_params(const char *cmdline, const char *part,
78 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070079
80void write_device_info_mmc(device_info *dev);
81void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -070082static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070083
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070084#define EXPAND(NAME) #NAME
85#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070086
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080087#ifdef MEMBASE
88#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
89#else
David Ng183a7422009-12-07 14:55:21 -080090#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080091#endif
92
Deepa Dinamani0e163a42013-05-24 17:08:15 -070093#ifndef MEMSIZE
94#define MEMSIZE 1024*1024
95#endif
96
97#define MAX_TAGS_SIZE 1024
98
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080099#define RECOVERY_MODE 0x77665502
100#define FASTBOOT_MODE 0x77665500
Matthew Qind886f3c2014-01-17 16:52:01 +0800101#define ALARM_BOOT 0x77665503
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800102
Kun Liang2f1601a2013-08-12 16:29:54 +0800103/* make 4096 as default size to ensure EFS,EXT4's erasing */
104#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800105#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800106
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800107#define UBI_MAGIC "UBI#"
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700108#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800109#define UBI_MAGIC_SIZE 0x04
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700110#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800111
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800112#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
113
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700114#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
115
Smita Ghoshb4555da2014-09-18 14:14:56 -0700116#if UFS_SUPPORT || USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700117static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700118#else
David Ng183a7422009-12-07 14:55:21 -0800119static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700120#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800121static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300122static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800123static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800124static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800125static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700126static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300127static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800128
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800129static const char *baseband_apq = " androidboot.baseband=apq";
130static const char *baseband_msm = " androidboot.baseband=msm";
131static const char *baseband_csfb = " androidboot.baseband=csfb";
132static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700133static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800134static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700135static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800136static const char *baseband_dsda = " androidboot.baseband=dsda";
137static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800138static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800139static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800140
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700141static unsigned page_size = 0;
142static unsigned page_mask = 0;
143static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
144static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700145static char target_boot_params[64];
Matthew Qind886f3c2014-01-17 16:52:01 +0800146static bool boot_reason_alarm;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700147
Shashank Mittalcd98d472011-08-02 14:29:24 -0700148/* Assuming unauthorized kernel image by default */
149static int auth_kernel_img = 0;
150
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700151static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700152
Dima Zavin42168f22009-01-30 11:52:22 -0800153struct atag_ptbl_entry
154{
155 char name[16];
156 unsigned offset;
157 unsigned size;
158 unsigned flags;
159};
160
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700161/*
162 * Partition info, required to be published
163 * for fastboot
164 */
165struct getvar_partition_info {
166 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
167 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
168 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
169 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
170 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
171};
172
173/*
174 * Right now, we are publishing the info for only
175 * three partitions
176 */
177struct getvar_partition_info part_info[] =
178{
179 { "system" , "partition-size:", "partition-type:", "", "ext4" },
180 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
181 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
182};
183
184char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700185char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800186char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700187char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530188char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700189
Greg Griscod2471ef2011-07-14 13:00:42 -0700190extern int emmc_recovery_init(void);
191
Kinson Chik0b1c8162011-08-31 16:31:57 -0700192#if NO_KEYPAD_DRIVER
193extern int fastboot_trigger(void);
194#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700195
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800196static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700197{
198 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700199#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800200 if (is_arm64)
201 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
202 else
203 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700204 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
205 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700206#endif
207}
208
Dima Zavin42168f22009-01-30 11:52:22 -0800209static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
210{
211 struct atag_ptbl_entry atag_ptn;
212
213 memcpy(atag_ptn.name, ptn->name, 16);
214 atag_ptn.name[15] = '\0';
215 atag_ptn.offset = ptn->start;
216 atag_ptn.size = ptn->length;
217 atag_ptn.flags = ptn->flags;
218 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
219 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
220}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800221
Neeti Desaie245d492012-06-01 12:52:13 -0700222unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800223{
David Ng183a7422009-12-07 14:55:21 -0800224 int cmdline_len = 0;
225 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800226 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700227 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800228 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300229 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700230 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700231 char *boot_dev_buf = NULL;
Dima Zavin42168f22009-01-30 11:52:22 -0800232
Brian Swetland9c4c0752009-01-25 16:23:50 -0800233 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800234 cmdline_len = strlen(cmdline);
235 have_cmdline = 1;
236 }
237 if (target_is_emmc_boot()) {
238 cmdline_len += strlen(emmc_cmdline);
Smita Ghoshb4555da2014-09-18 14:14:56 -0700239#if UFS_SUPPORT || USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700240 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
241 ASSERT(boot_dev_buf);
242 platform_boot_dev_cmdline(boot_dev_buf);
243 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700244#endif
David Ng183a7422009-12-07 14:55:21 -0800245 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800246
247 cmdline_len += strlen(usb_sn_cmdline);
248 cmdline_len += strlen(sn_buf);
249
Pavel Nedev5614d222013-06-17 18:01:02 +0300250 if (boot_into_recovery && gpt_exists)
251 cmdline_len += strlen(secondary_gpt_enable);
252
Pavel Nedev328ac822013-04-05 15:25:11 +0300253 if (boot_into_ffbm) {
254 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700255 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800256 /* reduce kernel console messages to speed-up boot */
257 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800258 } else if (boot_reason_alarm) {
259 cmdline_len += strlen(alarmboot_cmdline);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700260 } else if (device.charger_screen_enabled &&
261 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700262 pause_at_bootup = 1;
263 cmdline_len += strlen(battchg_pause);
264 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800265
Shashank Mittalcd98d472011-08-02 14:29:24 -0700266 if(target_use_signed_kernel() && auth_kernel_img) {
267 cmdline_len += strlen(auth_kernel);
268 }
269
Joonwoo Park61112782013-10-02 19:50:39 -0700270 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
271 "system",
272 target_boot_params,
273 sizeof(target_boot_params)) == 0) {
274 have_target_boot_params = 1;
275 cmdline_len += strlen(target_boot_params);
276 }
277
Ajay Dudanid04110c2011-01-17 23:55:07 -0800278 /* Determine correct androidboot.baseband to use */
279 switch(target_baseband())
280 {
281 case BASEBAND_APQ:
282 cmdline_len += strlen(baseband_apq);
283 break;
284
285 case BASEBAND_MSM:
286 cmdline_len += strlen(baseband_msm);
287 break;
288
289 case BASEBAND_CSFB:
290 cmdline_len += strlen(baseband_csfb);
291 break;
292
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800293 case BASEBAND_SVLTE2A:
294 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800295 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700296
297 case BASEBAND_MDM:
298 cmdline_len += strlen(baseband_mdm);
299 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700300
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800301 case BASEBAND_MDM2:
302 cmdline_len += strlen(baseband_mdm2);
303 break;
304
Amol Jadi5c61a952012-05-04 17:05:35 -0700305 case BASEBAND_SGLTE:
306 cmdline_len += strlen(baseband_sglte);
307 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530308
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800309 case BASEBAND_SGLTE2:
310 cmdline_len += strlen(baseband_sglte2);
311 break;
312
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530313 case BASEBAND_DSDA:
314 cmdline_len += strlen(baseband_dsda);
315 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800316
317 case BASEBAND_DSDA2:
318 cmdline_len += strlen(baseband_dsda2);
319 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800320 }
321
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800322 if (cmdline) {
323 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
324 target_display_panel_node(device.display_panel,
325 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
326 strlen(display_panel_buf)) {
327 cmdline_len += strlen(display_panel_buf);
328 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700329 }
330
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800331 if (target_warm_boot()) {
332 warm_boot = true;
333 cmdline_len += strlen(warmboot_cmdline);
334 }
335
David Ng183a7422009-12-07 14:55:21 -0800336 if (cmdline_len > 0) {
337 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800338 unsigned char *dst;
339
340 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
341 ASSERT(cmdline_final != NULL);
342 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700343
Amol Jadi168b7712012-03-06 16:15:00 -0800344 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800345 if (have_cmdline) {
346 src = cmdline;
347 while ((*dst++ = *src++));
348 }
349 if (target_is_emmc_boot()) {
350 src = emmc_cmdline;
351 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700352 have_cmdline = 1;
353 while ((*dst++ = *src++));
Smita Ghoshb4555da2014-09-18 14:14:56 -0700354#if UFS_SUPPORT || USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700355 src = boot_dev_buf;
356 if (have_cmdline) --dst;
357 while ((*dst++ = *src++));
358#endif
David Ngf773dde2010-07-26 19:55:08 -0700359 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800360
361 src = usb_sn_cmdline;
362 if (have_cmdline) --dst;
363 have_cmdline = 1;
364 while ((*dst++ = *src++));
365 src = sn_buf;
366 if (have_cmdline) --dst;
367 have_cmdline = 1;
368 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800369 if (warm_boot) {
370 if (have_cmdline) --dst;
371 src = warmboot_cmdline;
372 while ((*dst++ = *src++));
373 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800374
Pavel Nedev5614d222013-06-17 18:01:02 +0300375 if (boot_into_recovery && gpt_exists) {
376 src = secondary_gpt_enable;
377 if (have_cmdline) --dst;
378 while ((*dst++ = *src++));
379 }
380
Pavel Nedev328ac822013-04-05 15:25:11 +0300381 if (boot_into_ffbm) {
382 src = androidboot_mode;
383 if (have_cmdline) --dst;
384 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700385 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300386 if (have_cmdline) --dst;
387 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800388 src = loglevel;
389 if (have_cmdline) --dst;
390 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800391 } else if (boot_reason_alarm) {
392 src = alarmboot_cmdline;
393 if (have_cmdline) --dst;
394 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300395 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700396 src = battchg_pause;
397 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800398 while ((*dst++ = *src++));
399 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800400
Shashank Mittalcd98d472011-08-02 14:29:24 -0700401 if(target_use_signed_kernel() && auth_kernel_img) {
402 src = auth_kernel;
403 if (have_cmdline) --dst;
404 while ((*dst++ = *src++));
405 }
406
Ajay Dudanid04110c2011-01-17 23:55:07 -0800407 switch(target_baseband())
408 {
409 case BASEBAND_APQ:
410 src = baseband_apq;
411 if (have_cmdline) --dst;
412 while ((*dst++ = *src++));
413 break;
414
415 case BASEBAND_MSM:
416 src = baseband_msm;
417 if (have_cmdline) --dst;
418 while ((*dst++ = *src++));
419 break;
420
421 case BASEBAND_CSFB:
422 src = baseband_csfb;
423 if (have_cmdline) --dst;
424 while ((*dst++ = *src++));
425 break;
426
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800427 case BASEBAND_SVLTE2A:
428 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800429 if (have_cmdline) --dst;
430 while ((*dst++ = *src++));
431 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700432
433 case BASEBAND_MDM:
434 src = baseband_mdm;
435 if (have_cmdline) --dst;
436 while ((*dst++ = *src++));
437 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700438
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800439 case BASEBAND_MDM2:
440 src = baseband_mdm2;
441 if (have_cmdline) --dst;
442 while ((*dst++ = *src++));
443 break;
444
Amol Jadi5c61a952012-05-04 17:05:35 -0700445 case BASEBAND_SGLTE:
446 src = baseband_sglte;
447 if (have_cmdline) --dst;
448 while ((*dst++ = *src++));
449 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530450
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800451 case BASEBAND_SGLTE2:
452 src = baseband_sglte2;
453 if (have_cmdline) --dst;
454 while ((*dst++ = *src++));
455 break;
456
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530457 case BASEBAND_DSDA:
458 src = baseband_dsda;
459 if (have_cmdline) --dst;
460 while ((*dst++ = *src++));
461 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800462
463 case BASEBAND_DSDA2:
464 src = baseband_dsda2;
465 if (have_cmdline) --dst;
466 while ((*dst++ = *src++));
467 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800468 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700469
470 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700471 src = display_panel_buf;
472 if (have_cmdline) --dst;
473 while ((*dst++ = *src++));
474 }
Joonwoo Park61112782013-10-02 19:50:39 -0700475
476 if (have_target_boot_params) {
477 if (have_cmdline) --dst;
478 src = target_boot_params;
479 while ((*dst++ = *src++));
480 }
Neeti Desaie245d492012-06-01 12:52:13 -0700481 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700482
483
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700484 if (boot_dev_buf)
485 free(boot_dev_buf);
486
Maria Yu9ca0c402014-07-07 14:49:34 +0800487 dprintf(INFO, "cmdline: %s\n", cmdline_final ? cmdline_final : "");
Neeti Desaie245d492012-06-01 12:52:13 -0700488 return cmdline_final;
489}
490
491unsigned *atag_core(unsigned *ptr)
492{
493 /* CORE */
494 *ptr++ = 2;
495 *ptr++ = 0x54410001;
496
497 return ptr;
498
499}
500
501unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
502 unsigned ramdisk_size)
503{
504 if (ramdisk_size) {
505 *ptr++ = 4;
506 *ptr++ = 0x54420005;
507 *ptr++ = (unsigned)ramdisk;
508 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800509 }
510
Neeti Desaie245d492012-06-01 12:52:13 -0700511 return ptr;
512}
513
514unsigned *atag_ptable(unsigned **ptr_addr)
515{
516 int i;
517 struct ptable *ptable;
518
519 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700520 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
521 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700522 *(*ptr_addr)++ = 0x4d534d70;
523 for (i = 0; i < ptable->count; ++i)
524 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
525 }
526
527 return (*ptr_addr);
528}
529
530unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
531{
532 int cmdline_length = 0;
533 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700534 char *dest;
535
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800536 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700537 n = (cmdline_length + 4) & (~3);
538
539 *ptr++ = (n / 4) + 2;
540 *ptr++ = 0x54410009;
541 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800542 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700543 ptr += (n / 4);
544
545 return ptr;
546}
547
548unsigned *atag_end(unsigned *ptr)
549{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800550 /* END */
551 *ptr++ = 0;
552 *ptr++ = 0;
553
Neeti Desaie245d492012-06-01 12:52:13 -0700554 return ptr;
555}
556
557void generate_atags(unsigned *ptr, const char *cmdline,
558 void *ramdisk, unsigned ramdisk_size)
559{
560
561 ptr = atag_core(ptr);
562 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
563 ptr = target_atag_mem(ptr);
564
565 /* Skip NAND partition ATAGS for eMMC boot */
566 if (!target_is_emmc_boot()){
567 ptr = atag_ptable(&ptr);
568 }
569
570 ptr = atag_cmdline(ptr, cmdline);
571 ptr = atag_end(ptr);
572}
573
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700574typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700575void boot_linux(void *kernel, unsigned *tags,
576 const char *cmdline, unsigned machtype,
577 void *ramdisk, unsigned ramdisk_size)
578{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800579 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800580#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700581 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800582#endif
583
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700584 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800585 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800586 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800587
588 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700589
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800590 final_cmdline = update_cmdline((const char*)cmdline);
591
Neeti Desai17379b82012-06-04 18:42:53 -0700592#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800593 dprintf(INFO, "Updating device tree: start\n");
594
Neeti Desai17379b82012-06-04 18:42:53 -0700595 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800596 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700597 if(ret)
598 {
599 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
600 ASSERT(0);
601 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800602 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700603#else
Neeti Desaie245d492012-06-01 12:52:13 -0700604 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800605 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700606#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700607
Maria Yu52254c02014-07-04 16:14:54 +0800608 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700609
610#if VERIFIED_BOOT
611 /* Write protect the device info */
612 if (mmc_write_protect("devinfo", 1))
613 {
614 dprintf(INFO, "Failed to write protect dev info\n");
615 ASSERT(0);
616 }
617#endif
618
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700619 /* Perform target specific cleanup */
620 target_uninit();
621
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800622 /* Turn off splash screen if enabled */
623#if DISPLAY_SPLASH_SCREEN
624 target_display_shutdown();
625#endif
626
627
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800628 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
629 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800630
631 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700632
Amol Jadi4421e652011-06-16 15:00:48 -0700633 /* do any platform specific cleanup before kernel entry */
634 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700635
Brian Swetland9c4c0752009-01-25 16:23:50 -0800636 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700637
Amol Jadi504f9fe2012-08-16 13:56:48 -0700638#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800639 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700640#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700641 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800642
643 if (IS_ARM64(kptr))
644 /* Jump to a 64bit kernel */
645 scm_elexec_call((paddr_t)kernel, tags_phys);
646 else
647 /* Jump to a 32bit kernel */
648 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800649}
650
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700651/* Function to check if the memory address range falls within the aboot
652 * boundaries.
653 * start: Start of the memory region
654 * size: Size of the memory region
655 */
656int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
657{
658 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800659 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700660 return -1;
661
662 /* Check for memory overlap. */
663 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
664 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700665 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700666 return 0;
667 else
668 return -1;
669}
670
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800671#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800672
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800673BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800674#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800675BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800676#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800677
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700678static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
679{
680 int ret;
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800681#if IMAGE_VERIF_ALGO_SHA1
682 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
683#else
684 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
685#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700686
687 /* Assume device is rooted at this time. */
688 device.is_tampered = 1;
689
690 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
691
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700692#if VERIFIED_BOOT
693 if(boot_into_recovery)
694 {
695 ret = boot_verify_image((unsigned char *)bootimg_addr,
696 bootimg_size, "recovery");
697 }
698 else
699 {
700 ret = boot_verify_image((unsigned char *)bootimg_addr,
701 bootimg_size, "boot");
702 }
703 boot_verify_print_state();
704#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700705 ret = image_verify((unsigned char *)bootimg_addr,
706 (unsigned char *)(bootimg_addr + bootimg_size),
707 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800708 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700709#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700710 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
711
712 if (ret)
713 {
714 /* Authorized kernel */
715 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700716 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700717 }
718
719#if USE_PCOM_SECBOOT
720 set_tamper_flag(device.is_tampered);
721#endif
722
723 if(device.is_tampered)
724 {
725 write_device_info_mmc(&device);
726 #ifdef TZ_TAMPER_FUSE
727 set_tamper_fuse_cmd();
728 #endif
729 #ifdef ASSERT_ON_TAMPER
730 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
731 ASSERT(0);
732 #endif
733 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700734
735#if VERIFIED_BOOT
736 if(boot_verify_get_state() == RED)
737 {
738 if(!boot_into_recovery)
739 {
740 dprintf(CRITICAL,
741 "Device verification failed. Rebooting into recovery.\n");
742 reboot_device(RECOVERY_MODE);
743 }
744 else
745 {
746 dprintf(CRITICAL,
747 "Recovery image verification failed. Asserting..\n");
748 ASSERT(0);
749 }
750 }
751#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700752}
753
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530754static bool check_format_bit()
755{
756 bool ret = false;
757 int index;
758 uint64_t offset;
759 struct boot_selection_info *in = NULL;
760 char *buf = NULL;
761
762 index = partition_get_index("bootselect");
763 if (index == INVALID_PTN)
764 {
765 dprintf(INFO, "Unable to locate /bootselect partition\n");
766 return ret;
767 }
768 offset = partition_get_offset(index);
769 if(!offset)
770 {
771 dprintf(INFO, "partition /bootselect doesn't exist\n");
772 return ret;
773 }
774 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
775 ASSERT(buf);
776 if (mmc_read(offset, (unsigned int *)buf, page_size))
777 {
778 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
779 free(buf);
780 return ret;
781 }
782 in = (struct boot_selection_info *) buf;
783 if ((in->signature == BOOTSELECT_SIGNATURE) &&
784 (in->version == BOOTSELECT_VERSION)) {
785 if ((in->state_info & BOOTSELECT_FORMAT) &&
786 !(in->state_info & BOOTSELECT_FACTORY))
787 ret = true;
788 } else {
789 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
790 in->signature, in->version);
791 ASSERT(0);
792 }
793 free(buf);
794 return ret;
795}
796
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700797void boot_verifier_init()
798{
799
800 uint32_t boot_state;
801 /* Check if device unlock */
802 if(device.is_unlocked)
803 {
804 boot_verify_send_event(DEV_UNLOCK);
805 boot_verify_print_state();
806 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
807 return;
808 }
809 else
810 {
811 boot_verify_send_event(BOOT_INIT);
812 }
813
814 /* Initialize keystore */
815 boot_state = boot_verify_keystore_init();
816 if(boot_state == YELLOW)
817 {
818 boot_verify_print_state();
819 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
820 }
821}
822
Shashank Mittal23b8f422010-04-16 19:27:21 -0700823int boot_linux_from_mmc(void)
824{
825 struct boot_img_hdr *hdr = (void*) buf;
826 struct boot_img_hdr *uhdr;
827 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700828 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700829 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700830 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700831
Shashank Mittalcd98d472011-08-02 14:29:24 -0700832 unsigned char *image_addr = 0;
833 unsigned kernel_actual;
834 unsigned ramdisk_actual;
835 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700836 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700837
838#if DEVICE_TREE
839 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700840 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700841 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800842 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700843 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700844#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800845 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
846 struct kernel64_hdr *kptr = (void*) kbuf;
847
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530848 if (check_format_bit())
849 boot_into_recovery = 1;
850
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700851 if (!boot_into_recovery) {
852 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
853 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
854 if (rcode <= 0) {
855 boot_into_ffbm = false;
856 if (rcode < 0)
857 dprintf(CRITICAL,"failed to get ffbm cookie");
858 } else
859 boot_into_ffbm = true;
860 } else
861 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700862 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
863 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
864 dprintf(INFO, "Unified boot method!\n");
865 hdr = uhdr;
866 goto unified_boot;
867 }
Greg Griscod6250552011-06-29 14:40:23 -0700868 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700869 index = partition_get_index("boot");
870 ptn = partition_get_offset(index);
871 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700872 dprintf(CRITICAL, "ERROR: No boot partition found\n");
873 return -1;
874 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700875 }
876 else {
877 index = partition_get_index("recovery");
878 ptn = partition_get_offset(index);
879 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700880 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
881 return -1;
882 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700883 }
884
Greg Griscod6250552011-06-29 14:40:23 -0700885 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700886 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
887 return -1;
888 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700889
890 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700891 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700892 return -1;
893 }
894
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700895 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530896
897 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
898 dprintf(CRITICAL, "ERROR: Invalid page size\n");
899 return -1;
900 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700901 page_size = hdr->page_size;
902 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700903 }
904
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800905 /* Read the next page to get kernel Image header
906 * which lives in the second page for arm64 targets.
907 */
908
909 if (mmc_read(ptn + page_size, (unsigned int *) kbuf, page_size)) {
910 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
911 return -1;
912 }
913
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700914 /*
915 * Update the kernel/ramdisk/tags address if the boot image header
916 * has default values, these default values come from mkbootimg when
917 * the boot image is flashed using fastboot flash:raw
918 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800919 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700920
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700921 /* Get virtual addresses since the hdr saves physical addresses. */
922 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
923 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
924 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700925
926 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
927 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
928
929 /* Check if the addresses in the header are valid. */
930 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
931 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
932 {
933 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
934 return -1;
935 }
936
937#ifndef DEVICE_TREE
938 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
939 {
940 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
941 return -1;
942 }
943#endif
944
Shashank Mittalcd98d472011-08-02 14:29:24 -0700945 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300946 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
947 (int) target_use_signed_kernel(),
948 device.is_unlocked,
949 device.is_tampered);
950
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700951#if VERIFIED_BOOT
952 boot_verifier_init();
953#endif
954
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700955 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800956 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700957 offset = 0;
958
Shashank Mittalcd98d472011-08-02 14:29:24 -0700959 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700960
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800961#if DEVICE_TREE
962 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
963 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700964
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700965 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700966 {
967 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
968 return -1;
969 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800970#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530971 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700972
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700973#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700974
Amol Jadib6be5c12012-11-14 13:39:51 -0800975 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700976 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800977
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700978 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
979 {
980 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
981 return -1;
982 }
983
Shashank Mittalcd98d472011-08-02 14:29:24 -0700984 /* Read image without signature */
985 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
986 {
987 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
988 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800989 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700990
Amol Jadib6be5c12012-11-14 13:39:51 -0800991 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700992 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800993
Shashank Mittalcd98d472011-08-02 14:29:24 -0700994 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700995
996 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
997 {
998 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
999 return -1;
1000 }
1001
Shashank Mittalcd98d472011-08-02 14:29:24 -07001002 /* Read signature */
1003 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1004 {
1005 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001006 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001007 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001008
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001009 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001010
Neeti Desai465491e2012-07-31 12:53:35 -07001011 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -07001012 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1013 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -07001014
Neeti Desai465491e2012-07-31 12:53:35 -07001015 #if DEVICE_TREE
1016 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001017 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -07001018 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001019
Deepa Dinamani19648b42013-09-05 17:05:55 -07001020 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001021 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1022 return -1;
1023 }
Deepa Dinamani19648b42013-09-05 17:05:55 -07001024
Neeti Desai465491e2012-07-31 12:53:35 -07001025 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -07001026 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001027 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
1028 return -1;
1029 }
1030
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001031 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001032 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001033 {
1034 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1035 return -1;
1036 }
1037
Joel Kingaa335dc2013-06-03 16:11:08 -07001038 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001039 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -08001040 /*
1041 * If appended dev tree is found, update the atags with
1042 * memory address to the DTB appended location on RAM.
1043 * Else update with the atags address in the kernel header
1044 */
1045 void *dtb;
1046 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001047 hdr->kernel_size,
1048 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001049 if (!dtb) {
1050 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1051 return -1;
1052 }
Neeti Desai465491e2012-07-31 12:53:35 -07001053 }
1054 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001055 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001056 else
1057 {
Amol Jadib6be5c12012-11-14 13:39:51 -08001058 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001059
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001060 image_addr = (unsigned char *)target_get_scratch_address();
1061#if DEVICE_TREE
1062 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1063 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1064
1065 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
1066 {
1067 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1068 return -1;
1069 }
1070#else
1071 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1072
1073#endif
1074 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
1075 {
1076 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1077 return -1;
1078 }
1079
Amol Jadib6be5c12012-11-14 13:39:51 -08001080 dprintf(INFO, "Loading boot image (%d): start\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001081 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001082 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001083
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001084 offset = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001085
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001086 /* Load the entire boot image */
1087 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual)) {
1088 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
Shashank Mittalcd98d472011-08-02 14:29:24 -07001089 return -1;
1090 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001091
1092 dprintf(INFO, "Loading boot image (%d): done\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001093 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001094 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -07001095
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001096 #ifdef TZ_SAVE_KERNEL_HASH
1097 aboot_save_boot_hash_mmc(image_addr, imagesize_actual);
1098 #endif /* TZ_SAVE_KERNEL_HASH */
1099
1100 /* Move kernel, ramdisk and device tree to correct address */
1101 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1102 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Neeti Desai465491e2012-07-31 12:53:35 -07001103
1104 #if DEVICE_TREE
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001105 if(hdr->dt_size) {
1106 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1107 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001108
Deepa Dinamani19648b42013-09-05 17:05:55 -07001109 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001110 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1111 return -1;
1112 }
1113
Joel Kingaa335dc2013-06-03 16:11:08 -07001114 /* Find index of device tree within device tree table */
1115 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001116 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1117 return -1;
1118 }
1119
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001120 /* Validate and Read device device tree in the tags_addr */
Joel Kingaa335dc2013-06-03 16:11:08 -07001121 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001122 {
1123 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1124 return -1;
1125 }
1126
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001127 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001128 } else {
Aparna Mallavarapu33127372014-06-03 22:15:54 +05301129 /* Validate the tags_addr */
1130 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1131 {
1132 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1133 return -1;
1134 }
Dima Zavin77e41f32013-03-06 16:10:43 -08001135 /*
1136 * If appended dev tree is found, update the atags with
1137 * memory address to the DTB appended location on RAM.
1138 * Else update with the atags address in the kernel header
1139 */
1140 void *dtb;
1141 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001142 kernel_actual,
1143 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001144 if (!dtb) {
1145 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1146 return -1;
1147 }
Neeti Desai465491e2012-07-31 12:53:35 -07001148 }
1149 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001150 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001151
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001152 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1153 target_load_ssd_keystore();
1154
Shashank Mittal23b8f422010-04-16 19:27:21 -07001155unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001156
Dima Zavin77e41f32013-03-06 16:10:43 -08001157 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001158 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001159 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1160
1161 return 0;
1162}
1163
Dima Zavin214cc642009-01-26 11:16:21 -08001164int boot_linux_from_flash(void)
1165{
1166 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001167 struct ptentry *ptn;
1168 struct ptable *ptable;
1169 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001170
Shashank Mittalcd98d472011-08-02 14:29:24 -07001171 unsigned char *image_addr = 0;
1172 unsigned kernel_actual;
1173 unsigned ramdisk_actual;
1174 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001175 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001176
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001177#if DEVICE_TREE
1178 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001179 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001180 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001181 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001182#endif
1183
David Ng183a7422009-12-07 14:55:21 -08001184 if (target_is_emmc_boot()) {
1185 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1186 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1187 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1188 return -1;
1189 }
1190 goto continue_boot;
1191 }
1192
Dima Zavin214cc642009-01-26 11:16:21 -08001193 ptable = flash_get_ptable();
1194 if (ptable == NULL) {
1195 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1196 return -1;
1197 }
1198
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001199 if(!boot_into_recovery)
1200 {
1201 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001202
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001203 if (ptn == NULL) {
1204 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1205 return -1;
1206 }
1207 }
1208 else
1209 {
1210 ptn = ptable_find(ptable, "recovery");
1211 if (ptn == NULL) {
1212 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1213 return -1;
1214 }
Dima Zavin214cc642009-01-26 11:16:21 -08001215 }
1216
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001217 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001218 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1219 return -1;
1220 }
Dima Zavin214cc642009-01-26 11:16:21 -08001221
1222 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001223 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001224 return -1;
1225 }
1226
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001227 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001228 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 -08001229 return -1;
1230 }
1231
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001232 /*
1233 * Update the kernel/ramdisk/tags address if the boot image header
1234 * has default values, these default values come from mkbootimg when
1235 * the boot image is flashed using fastboot flash:raw
1236 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001237 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001238
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001239 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001240 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1241 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1242 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1243
1244 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1245 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1246
1247 /* Check if the addresses in the header are valid. */
1248 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1249 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1250 {
1251 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1252 return -1;
1253 }
1254
1255#ifndef DEVICE_TREE
1256 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1257 {
1258 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1259 return -1;
1260 }
1261#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001262
Shashank Mittalcd98d472011-08-02 14:29:24 -07001263 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001264 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001265 {
1266 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001267 offset = 0;
1268
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001269#if DEVICE_TREE
1270 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1271 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001272
1273 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1274 {
1275 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1276 return -1;
1277 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001278#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001279 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001280#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001281
Amol Jadib6be5c12012-11-14 13:39:51 -08001282 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001283 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001284
Shashank Mittalcd98d472011-08-02 14:29:24 -07001285 /* Read image without signature */
1286 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1287 {
1288 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1289 return -1;
1290 }
Dima Zavin214cc642009-01-26 11:16:21 -08001291
Amol Jadib6be5c12012-11-14 13:39:51 -08001292 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001293 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001294
Shashank Mittalcd98d472011-08-02 14:29:24 -07001295 offset = imagesize_actual;
1296 /* Read signature */
1297 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1298 {
1299 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001300 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001301 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001302
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001303 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001304
1305 /* Move kernel and ramdisk to correct address */
1306 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1307 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001308#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001309 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001310 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001311 {
1312 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1313 return -1;
1314 }
1315
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001316 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1317#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001318
1319 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001320 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001321 {
1322 write_device_info_flash(&device);
1323 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301324#if USE_PCOM_SECBOOT
1325 set_tamper_flag(device.is_tampered);
1326#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001327 }
1328 else
1329 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001330 offset = page_size;
1331
Amol Jadib6be5c12012-11-14 13:39:51 -08001332 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1333 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1334 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1335
1336 dprintf(INFO, "Loading boot image (%d): start\n",
1337 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001338 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001339
1340 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001341 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1342 return -1;
1343 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001344 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001345
Amol Jadib6be5c12012-11-14 13:39:51 -08001346 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001347 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1348 return -1;
1349 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001350 offset += ramdisk_actual;
1351
1352 dprintf(INFO, "Loading boot image (%d): done\n",
1353 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001354 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001355
1356 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001357 offset += second_actual;
1358 /* Second image loading not implemented. */
1359 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001360 }
1361
1362#if DEVICE_TREE
1363 if(hdr->dt_size != 0) {
1364
1365 /* Read the device tree table into buffer */
1366 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1367 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1368 return -1;
1369 }
1370
1371 table = (struct dt_table*) dt_buf;
1372
Deepa Dinamani19648b42013-09-05 17:05:55 -07001373 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001374 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1375 return -1;
1376 }
1377
Deepa Dinamani19648b42013-09-05 17:05:55 -07001378 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1379 if (!table)
1380 return -1;
1381
1382 /* Read the entire device tree table into buffer */
1383 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1384 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1385 return -1;
1386 }
1387
1388
Joel Kingaa335dc2013-06-03 16:11:08 -07001389 /* Find index of device tree within device tree table */
1390 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001391 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1392 return -1;
1393 }
1394
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001395 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001396 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001397 {
1398 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1399 return -1;
1400 }
1401
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001402 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001403 if(flash_read(ptn, offset + dt_entry.offset,
1404 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001405 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1406 return -1;
1407 }
1408 }
1409#endif
1410
Shashank Mittalcd98d472011-08-02 14:29:24 -07001411 }
David Ng183a7422009-12-07 14:55:21 -08001412continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001413
Dima Zavin214cc642009-01-26 11:16:21 -08001414 /* TODO: create/pass atags to kernel */
1415
Ajay Dudanie28a6072011-07-01 13:59:46 -07001416 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001417 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001418 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1419
1420 return 0;
1421}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001422
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001423BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001424void write_device_info_mmc(device_info *dev)
1425{
1426 struct device_info *info = (void*) info_buf;
1427 unsigned long long ptn = 0;
1428 unsigned long long size;
1429 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001430 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001431 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001432
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001433#if VERIFIED_BOOT
1434 index = partition_get_index("devinfo");
1435#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001436 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001437#endif
1438
Shashank Mittal162244e2011-08-08 19:01:25 -07001439 ptn = partition_get_offset(index);
1440 if(ptn == 0)
1441 {
1442 return;
1443 }
1444
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001445 lun = partition_get_lun(index);
1446 mmc_set_lun(lun);
1447
Shashank Mittal162244e2011-08-08 19:01:25 -07001448 size = partition_get_size(index);
1449
1450 memcpy(info, dev, sizeof(device_info));
1451
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001452 blocksize = mmc_get_device_blocksize();
1453
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001454#if VERIFIED_BOOT
1455 if(mmc_write(ptn, blocksize, (void *)info_buf))
1456#else
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001457 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001458#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001459 {
1460 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1461 return;
1462 }
1463}
1464
1465void read_device_info_mmc(device_info *dev)
1466{
1467 struct device_info *info = (void*) info_buf;
1468 unsigned long long ptn = 0;
1469 unsigned long long size;
1470 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001471 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001472
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001473#if VERIFIED_BOOT
1474 index = partition_get_index("devinfo");
1475#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001476 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001477#endif
1478
Shashank Mittal162244e2011-08-08 19:01:25 -07001479 ptn = partition_get_offset(index);
1480 if(ptn == 0)
1481 {
1482 return;
1483 }
1484
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001485 mmc_set_lun(partition_get_lun(index));
1486
Shashank Mittal162244e2011-08-08 19:01:25 -07001487 size = partition_get_size(index);
1488
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001489 blocksize = mmc_get_device_blocksize();
1490
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001491#if VERIFIED_BOOT
1492 if(mmc_read(ptn, (void *)info_buf, blocksize))
1493#else
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001494 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001495#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001496 {
1497 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1498 return;
1499 }
1500
1501 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1502 {
1503 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
Shashank Mittal7e033062014-09-25 19:58:07 -07001504#if DEFAULT_UNLOCK
1505 info->is_unlocked = 1;
1506#else
Shashank Mittal162244e2011-08-08 19:01:25 -07001507 info->is_unlocked = 0;
Shashank Mittal7e033062014-09-25 19:58:07 -07001508#endif
1509 info->is_verified = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001510 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001511 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001512
1513 write_device_info_mmc(info);
1514 }
1515 memcpy(dev, info, sizeof(device_info));
1516}
1517
1518void write_device_info_flash(device_info *dev)
1519{
1520 struct device_info *info = (void *) info_buf;
1521 struct ptentry *ptn;
1522 struct ptable *ptable;
1523
1524 ptable = flash_get_ptable();
1525 if (ptable == NULL)
1526 {
1527 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1528 return;
1529 }
1530
1531 ptn = ptable_find(ptable, "devinfo");
1532 if (ptn == NULL)
1533 {
1534 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1535 return;
1536 }
1537
1538 memcpy(info, dev, sizeof(device_info));
1539
1540 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1541 {
1542 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1543 return;
1544 }
1545}
1546
1547void read_device_info_flash(device_info *dev)
1548{
1549 struct device_info *info = (void*) info_buf;
1550 struct ptentry *ptn;
1551 struct ptable *ptable;
1552
1553 ptable = flash_get_ptable();
1554 if (ptable == NULL)
1555 {
1556 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1557 return;
1558 }
1559
1560 ptn = ptable_find(ptable, "devinfo");
1561 if (ptn == NULL)
1562 {
1563 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1564 return;
1565 }
1566
1567 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1568 {
1569 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1570 return;
1571 }
1572
1573 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1574 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001575 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1576 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001577 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001578 write_device_info_flash(info);
1579 }
1580 memcpy(dev, info, sizeof(device_info));
1581}
1582
1583void write_device_info(device_info *dev)
1584{
1585 if(target_is_emmc_boot())
1586 {
1587 write_device_info_mmc(dev);
1588 }
1589 else
1590 {
1591 write_device_info_flash(dev);
1592 }
1593}
1594
1595void read_device_info(device_info *dev)
1596{
1597 if(target_is_emmc_boot())
1598 {
1599 read_device_info_mmc(dev);
1600 }
1601 else
1602 {
1603 read_device_info_flash(dev);
1604 }
1605}
1606
1607void reset_device_info()
1608{
1609 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001610 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001611 write_device_info(&device);
1612}
1613
1614void set_device_root()
1615{
1616 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001617 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001618 write_device_info(&device);
1619}
1620
Amol Jadicb524072012-08-09 16:40:18 -07001621#if DEVICE_TREE
1622int copy_dtb(uint8_t *boot_image_start)
1623{
1624 uint32 dt_image_offset = 0;
1625 uint32_t n;
1626 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001627 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001628 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001629
1630 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1631
Amol Jadicb524072012-08-09 16:40:18 -07001632 if(hdr->dt_size != 0) {
1633
1634 /* add kernel offset */
1635 dt_image_offset += page_size;
1636 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1637 dt_image_offset += n;
1638
1639 /* add ramdisk offset */
1640 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1641 dt_image_offset += n;
1642
1643 /* add second offset */
1644 if(hdr->second_size != 0) {
1645 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1646 dt_image_offset += n;
1647 }
1648
1649 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001650 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001651
Deepa Dinamani19648b42013-09-05 17:05:55 -07001652 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001653 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1654 return -1;
1655 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001656 /* Find index of device tree within device tree table */
1657 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001658 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1659 return -1;
1660 }
1661
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001662 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001663 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001664 {
1665 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1666 return -1;
1667 }
1668
Amol Jadicb524072012-08-09 16:40:18 -07001669 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001670 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001671 boot_image_start + dt_image_offset + dt_entry.offset,
1672 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001673 } else
1674 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001675
1676 /* Everything looks fine. Return success. */
1677 return 0;
1678}
1679#endif
1680
Brian Swetland9c4c0752009-01-25 16:23:50 -08001681void cmd_boot(const char *arg, void *data, unsigned sz)
1682{
1683 unsigned kernel_actual;
1684 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001685 uint32_t image_actual;
1686 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001687 uint32_t sig_actual = SIGNATURE_SIZE;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001688 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001689 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001690 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001691 int ret = 0;
1692 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001693
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001694#if VERIFIED_BOOT
1695 if(!device.is_unlocked)
1696 {
1697 fastboot_fail("unlock device to use this command");
1698 return;
1699 }
1700#endif
1701
Brian Swetland9c4c0752009-01-25 16:23:50 -08001702 if (sz < sizeof(hdr)) {
1703 fastboot_fail("invalid bootimage header");
1704 return;
1705 }
1706
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001707 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001708
1709 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001710 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001711
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001712 if(target_is_emmc_boot() && hdr->page_size) {
1713 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001714 page_mask = page_size - 1;
1715 }
1716
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001717 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1718 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001719#if DEVICE_TREE
1720 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1721#endif
1722
1723 image_actual = ADD_OF(page_size, kernel_actual);
1724 image_actual = ADD_OF(image_actual, ramdisk_actual);
1725 image_actual = ADD_OF(image_actual, dt_actual);
1726
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001727 if (target_use_signed_kernel() && (!device.is_unlocked))
1728 image_actual = ADD_OF(image_actual, sig_actual);
1729
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001730 /* sz should have atleast raw boot image */
1731 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001732 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001733 return;
1734 }
1735
1736 /* Verify the boot image
1737 * device & page_size are initialized in aboot_init
1738 */
1739 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001740 /* Pass size excluding signature size, otherwise we would try to
1741 * access signature beyond its length
1742 */
1743 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001744
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001745 /*
1746 * Update the kernel/ramdisk/tags address if the boot image header
1747 * has default values, these default values come from mkbootimg when
1748 * the boot image is flashed using fastboot flash:raw
1749 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001750 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1751 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001752
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001753 /* Get virtual addresses since the hdr saves physical addresses. */
1754 hdr->kernel_addr = VA(hdr->kernel_addr);
1755 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1756 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001757
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001758 /* Check if the addresses in the header are valid. */
1759 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1760 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1761 {
1762 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001763 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001764 }
1765
Amol Jadicb524072012-08-09 16:40:18 -07001766#if DEVICE_TREE
1767 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001768 ret = copy_dtb(data);
1769
1770 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001771#else
1772 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1773 {
1774 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001775 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001776 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001777#endif
1778
1779 /* Load ramdisk & kernel */
1780 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1781 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1782
1783#if DEVICE_TREE
1784 /*
1785 * If dtb is not found look for appended DTB in the kernel.
1786 * If appended dev tree is found, update the atags with
1787 * memory address to the DTB appended location on RAM.
1788 * Else update with the atags address in the kernel header
1789 */
1790 if (!dtb_copied) {
1791 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001792 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1793 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001794 if (!dtb) {
1795 fastboot_fail("dtb not found");
1796 return;
1797 }
Amol Jadicb524072012-08-09 16:40:18 -07001798 }
1799#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001800
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001801#ifndef DEVICE_TREE
1802 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1803 {
1804 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001805 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001806 }
1807#endif
1808
Brian Swetland9c4c0752009-01-25 16:23:50 -08001809 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001810 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001811
Dima Zavin77e41f32013-03-06 16:10:43 -08001812 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001813 (const char*) hdr->cmdline, board_machtype(),
1814 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001815}
1816
Dima Zavin214cc642009-01-26 11:16:21 -08001817void cmd_erase(const char *arg, void *data, unsigned sz)
1818{
1819 struct ptentry *ptn;
1820 struct ptable *ptable;
1821
1822 ptable = flash_get_ptable();
1823 if (ptable == NULL) {
1824 fastboot_fail("partition table doesn't exist");
1825 return;
1826 }
1827
1828 ptn = ptable_find(ptable, arg);
1829 if (ptn == NULL) {
1830 fastboot_fail("unknown partition name");
1831 return;
1832 }
1833
1834 if (flash_erase(ptn)) {
1835 fastboot_fail("failed to erase partition");
1836 return;
1837 }
1838 fastboot_okay("");
1839}
1840
Bikas Gurungd48bd242010-09-04 19:54:32 -07001841
1842void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1843{
Kun Liang2f1601a2013-08-12 16:29:54 +08001844 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001845 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001846 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001847 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001848 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001849
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001850#if VERIFIED_BOOT
1851 if(!strcmp(arg, KEYSTORE_PTN_NAME))
1852 {
1853 if(!device.is_unlocked)
1854 {
1855 fastboot_fail("unlock device to erase keystore");
1856 return;
1857 }
1858 }
1859#endif
1860
Kinson Chikf1a43512011-07-14 11:28:39 -07001861 index = partition_get_index(arg);
1862 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001863 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001864
Kinson Chikf1a43512011-07-14 11:28:39 -07001865 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001866 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001867 return;
1868 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001869
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001870 lun = partition_get_lun(index);
1871 mmc_set_lun(lun);
1872
Oliver Wangcee448d2013-10-22 18:40:13 +08001873#if MMC_SDHCI_SUPPORT
1874 if (mmc_erase_card(ptn, size)) {
1875 fastboot_fail("failed to erase partition\n");
1876 return;
1877 }
1878#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001879 size = partition_get_size(index);
1880 if (size > DEFAULT_ERASE_SIZE)
1881 size = DEFAULT_ERASE_SIZE;
1882
neetidb4b24d62012-01-20 12:13:09 -08001883 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001884 0 in first several blocks */
1885 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001886 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001887 return;
1888 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001889#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001890 fastboot_okay("");
1891}
1892
1893
Ajay Dudani5c761132011-04-07 20:19:04 -07001894void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001895{
1896 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001897 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001898 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001899 char *token = NULL;
1900 char *pname = NULL;
1901 uint8_t lun = 0;
1902 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001903
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001904 token = strtok(arg, ":");
1905 pname = token;
1906 token = strtok(NULL, ":");
1907 if(token)
1908 {
1909 lun = atoi(token);
1910 mmc_set_lun(lun);
1911 lun_set = true;
1912 }
1913
Mao Jinlong226f33a2014-07-04 17:24:10 +08001914 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07001915 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08001916 if (!strcmp(pname, "partition"))
1917 {
1918 dprintf(INFO, "Attempt to write partition image.\n");
1919 if (write_partition(sz, (unsigned char *) data)) {
1920 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07001921 return;
1922 }
1923 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08001924 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001925 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001926#if VERIFIED_BOOT
1927 if(!strcmp(pname, KEYSTORE_PTN_NAME))
1928 {
1929 if(!device.is_unlocked)
1930 {
1931 fastboot_fail("unlock device to flash keystore");
1932 return;
1933 }
1934 if(!boot_verify_validate_keystore((unsigned char *)data))
1935 {
1936 fastboot_fail("image is not a keystore file");
1937 return;
1938 }
1939 }
1940#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08001941 index = partition_get_index(pname);
1942 ptn = partition_get_offset(index);
1943 if(ptn == 0) {
1944 fastboot_fail("partition table doesn't exist");
1945 return;
1946 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001947
Mao Jinlong226f33a2014-07-04 17:24:10 +08001948 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
1949 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1950 fastboot_fail("image is not a boot image");
1951 return;
1952 }
1953 }
1954
1955 if(!lun_set)
1956 {
1957 lun = partition_get_lun(index);
1958 mmc_set_lun(lun);
1959 }
1960
1961 size = partition_get_size(index);
1962 if (ROUND_TO_PAGE(sz,511) > size) {
1963 fastboot_fail("size too large");
1964 return;
1965 }
1966 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1967 fastboot_fail("flash write failure");
1968 return;
1969 }
Greg Grisco6e754772011-06-23 12:19:39 -07001970 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001971 }
1972 fastboot_okay("");
1973 return;
1974}
1975
Ajay Dudani5c761132011-04-07 20:19:04 -07001976void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1977{
1978 unsigned int chunk;
1979 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001980 uint32_t *fill_buf = NULL;
1981 uint32_t fill_val;
1982 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001983 sparse_header_t *sparse_header;
1984 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001985 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001986 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301987 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001988 int index = INVALID_PTN;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001989 int i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001990 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001991
Kinson Chikf1a43512011-07-14 11:28:39 -07001992 index = partition_get_index(arg);
1993 ptn = partition_get_offset(index);
1994 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001995 fastboot_fail("partition table doesn't exist");
1996 return;
1997 }
1998
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301999 size = partition_get_size(index);
2000 if (ROUND_TO_PAGE(sz,511) > size) {
2001 fastboot_fail("size too large");
2002 return;
2003 }
2004
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002005 lun = partition_get_lun(index);
2006 mmc_set_lun(lun);
2007
Ajay Dudani5c761132011-04-07 20:19:04 -07002008 /* Read and skip over sparse image header */
2009 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08002010 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
2011 fastboot_fail("size too large");
2012 return;
2013 }
2014
Ajay Dudani5c761132011-04-07 20:19:04 -07002015 data += sparse_header->file_hdr_sz;
2016 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
2017 {
2018 /* Skip the remaining bytes in a header that is longer than
2019 * we expected.
2020 */
2021 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
2022 }
2023
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002024 dprintf (SPEW, "=== Sparse Image Header ===\n");
2025 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2026 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2027 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2028 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2029 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2030 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2031 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2032 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002033
2034 /* Start processing chunks */
2035 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2036 {
2037 /* Read and skip over chunk header */
2038 chunk_header = (chunk_header_t *) data;
2039 data += sizeof(chunk_header_t);
2040
2041 dprintf (SPEW, "=== Chunk Header ===\n");
2042 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2043 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2044 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2045
2046 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
2047 {
2048 /* Skip the remaining bytes in a header that is longer than
2049 * we expected.
2050 */
2051 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
2052 }
2053
2054 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
2055 switch (chunk_header->chunk_type)
2056 {
2057 case CHUNK_TYPE_RAW:
2058 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2059 chunk_data_sz))
2060 {
2061 fastboot_fail("Bogus chunk size for chunk type Raw");
2062 return;
2063 }
2064
Ajay Dudaniab18f022011-05-12 14:39:22 -07002065 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2066 chunk_data_sz,
2067 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002068 {
2069 fastboot_fail("flash write failure");
2070 return;
2071 }
2072 total_blocks += chunk_header->chunk_sz;
2073 data += chunk_data_sz;
2074 break;
2075
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002076 case CHUNK_TYPE_FILL:
2077 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2078 sizeof(uint32_t)))
2079 {
2080 fastboot_fail("Bogus chunk size for chunk type FILL");
2081 return;
2082 }
2083
2084 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2085 if (!fill_buf)
2086 {
2087 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2088 return;
2089 }
2090
2091 fill_val = *(uint32_t *)data;
2092 data = (char *) data + sizeof(uint32_t);
2093 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
2094
2095 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2096 {
2097 fill_buf[i] = fill_val;
2098 }
2099
2100 for (i = 0; i < chunk_blk_cnt; i++)
2101 {
2102 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2103 sparse_header->blk_sz,
2104 fill_buf))
2105 {
2106 fastboot_fail("flash write failure");
2107 free(fill_buf);
2108 return;
2109 }
2110
2111 total_blocks++;
2112 }
2113
2114 free(fill_buf);
2115 break;
2116
Ajay Dudani5c761132011-04-07 20:19:04 -07002117 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002118 total_blocks += chunk_header->chunk_sz;
2119 break;
2120
Ajay Dudani5c761132011-04-07 20:19:04 -07002121 case CHUNK_TYPE_CRC:
2122 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2123 {
2124 fastboot_fail("Bogus chunk size for chunk type Dont Care");
2125 return;
2126 }
2127 total_blocks += chunk_header->chunk_sz;
2128 data += chunk_data_sz;
2129 break;
2130
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002131 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002132 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002133 fastboot_fail("Unknown chunk type");
2134 return;
2135 }
2136 }
2137
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002138 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2139 total_blocks, sparse_header->total_blks);
2140
2141 if(total_blocks != sparse_header->total_blks)
2142 {
2143 fastboot_fail("sparse image write failure");
2144 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002145
2146 fastboot_okay("");
2147 return;
2148}
2149
2150void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2151{
2152 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002153 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2154 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002155
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002156#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002157 int ret=0;
2158 uint32 major_version=0;
2159 uint32 minor_version=0;
2160
2161 ret = scm_svc_version(&major_version,&minor_version);
2162 if(!ret)
2163 {
2164 if(major_version >= 2)
2165 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002166 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002167 {
2168 ret = encrypt_scm((uint32 **) &data, &sz);
2169 if (ret != 0) {
2170 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2171 return;
2172 }
2173
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002174 /* Protect only for SSD */
2175 if (!strcmp(arg, "ssd")) {
2176 ret = scm_protect_keystore((uint32 *) data, sz);
2177 if (ret != 0) {
2178 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2179 return;
2180 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002181 }
2182 }
2183 else
2184 {
2185 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2186 if(ret != 0)
2187 {
2188 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2189 return;
2190 }
2191 }
2192 }
2193 else
2194 {
2195 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2196 magic_number[1] == DECRYPT_MAGIC_1)
2197 {
2198 ret = decrypt_scm((uint32 **) &data, &sz);
2199 if (ret != 0) {
2200 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2201 return;
2202 }
2203 }
2204 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2205 magic_number[1] == ENCRYPT_MAGIC_1)
2206 {
2207 ret = encrypt_scm((uint32 **) &data, &sz);
2208 if (ret != 0) {
2209 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2210 return;
2211 }
2212 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002213 }
2214 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002215 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002216 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002217 dprintf(CRITICAL,"INVALID SVC Version\n");
2218 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002219 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002220#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002221
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002222#if VERIFIED_BOOT
2223 if(!device.is_unlocked && !device.is_verified)
2224 {
2225 fastboot_fail("device is locked. Cannot flash images");
2226 return;
2227 }
2228 if(!device.is_unlocked && device.is_verified)
2229 {
2230 if(!boot_verify_flash_allowed(arg))
2231 {
2232 fastboot_fail("cannot flash this partition in verified state");
2233 return;
2234 }
2235 }
2236#endif
2237
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002238 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002239 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
2240 cmd_flash_mmc_img(arg, data, sz);
2241 else
2242 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002243 return;
2244}
2245
Dima Zavin214cc642009-01-26 11:16:21 -08002246void cmd_flash(const char *arg, void *data, unsigned sz)
2247{
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"))
2276 {
2277 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07002278 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002279 else
2280 extra = 0;
2281 }
2282 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002283 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002284
2285 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2286 if (flash_write(ptn, extra, data, sz)) {
2287 fastboot_fail("flash write failure");
2288 return;
2289 }
2290 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2291 fastboot_okay("");
2292}
2293
2294void cmd_continue(const char *arg, void *data, unsigned sz)
2295{
2296 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002297 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002298
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002299 if (target_is_emmc_boot())
2300 {
2301 boot_linux_from_mmc();
2302 }
2303 else
2304 {
2305 boot_linux_from_flash();
2306 }
Dima Zavin214cc642009-01-26 11:16:21 -08002307}
2308
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002309void cmd_reboot(const char *arg, void *data, unsigned sz)
2310{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002311 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002312 fastboot_okay("");
2313 reboot_device(0);
2314}
2315
2316void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2317{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002318 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002319 fastboot_okay("");
2320 reboot_device(FASTBOOT_MODE);
2321}
2322
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002323void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2324{
2325 dprintf(INFO, "Enabling charger screen check\n");
2326 device.charger_screen_enabled = 1;
2327 write_device_info(&device);
2328 fastboot_okay("");
2329}
2330
2331void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2332{
2333 dprintf(INFO, "Disabling charger screen check\n");
2334 device.charger_screen_enabled = 0;
2335 write_device_info(&device);
2336 fastboot_okay("");
2337}
2338
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302339void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2340{
2341 dprintf(INFO, "Selecting display panel %s\n", arg);
2342 if (arg)
2343 strlcpy(device.display_panel, arg,
2344 sizeof(device.display_panel));
2345 write_device_info(&device);
2346 fastboot_okay("");
2347}
2348
Shashank Mittal162244e2011-08-08 19:01:25 -07002349void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2350{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002351 /* TODO: Wipe user data */
2352 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002353 {
2354 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002355 device.is_verified = 0;
2356 write_device_info(&device);
2357 }
2358 fastboot_okay("");
2359}
2360
2361void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2362{
2363 /* TODO: Wipe user data */
2364 if(device.is_unlocked || device.is_verified)
2365 {
2366 device.is_unlocked = 0;
2367 device.is_verified = 0;
2368 write_device_info(&device);
2369 }
2370 fastboot_okay("");
2371}
2372
2373void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2374{
2375 /* TODO: Wipe user data */
2376 if(device.is_unlocked || !device.is_verified)
2377 {
2378 device.is_unlocked = 0;
2379 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002380 write_device_info(&device);
2381 }
2382 fastboot_okay("");
2383}
2384
Shashank Mittala0032282011-08-26 14:50:11 -07002385void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2386{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302387 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002388 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002389 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002390 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2391 fastboot_info(response);
2392 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002393 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302394 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2395 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002396 fastboot_okay("");
2397}
2398
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002399void cmd_preflash(const char *arg, void *data, unsigned sz)
2400{
2401 fastboot_okay("");
2402}
2403
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302404static struct fbimage logo_header = {0};
2405struct fbimage* splash_screen_flash();
2406
2407int splash_screen_check_header(struct fbimage *logo)
2408{
2409 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2410 return -1;
2411 if (logo->header.width == 0 || logo->header.height == 0)
2412 return -1;
2413 return 0;
2414}
2415
2416struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002417{
2418 struct ptentry *ptn;
2419 struct ptable *ptable;
2420 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302421 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002422
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302423
2424 ptable = flash_get_ptable();
2425 if (ptable == NULL) {
2426 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2427 return NULL;
2428 }
2429 ptn = ptable_find(ptable, "splash");
2430 if (ptn == NULL) {
2431 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2432 return NULL;
2433 }
2434
2435 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2436 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2437 return NULL;
2438 }
2439
2440 if (splash_screen_check_header(logo)) {
2441 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2442 return NULL;
2443 }
2444
2445 fb_display = fbcon_display();
2446 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302447 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2448 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back to default logo\n");
2449 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002450 }
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302451 uint8_t *base = (uint8_t *) fb_display->base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302452 if (flash_read(ptn + sizeof(logo->header), 0,
2453 base,
2454 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2455 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302456 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302457 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002458 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302459 logo->image = base;
2460 }
2461
2462 return logo;
2463}
2464
2465struct fbimage* splash_screen_mmc()
2466{
2467 int index = INVALID_PTN;
2468 unsigned long long ptn = 0;
2469 struct fbcon_config *fb_display = NULL;
2470 struct fbimage *logo = &logo_header;
2471
2472 index = partition_get_index("splash");
2473 if (index == 0) {
2474 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2475 return NULL;
2476 }
2477
2478 ptn = partition_get_offset(index);
2479 if (ptn == 0) {
2480 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2481 return NULL;
2482 }
2483
2484 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2485 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2486 return NULL;
2487 }
2488
2489 if (splash_screen_check_header(logo)) {
2490 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2491 return NULL;
2492 }
2493
2494 fb_display = fbcon_display();
2495 if (fb_display) {
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302496 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2497 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
2498 return NULL;
2499 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302500 uint8_t *base = (uint8_t *) fb_display->base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302501 if (mmc_read(ptn + sizeof(logo->header),
2502 base,
2503 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2504 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05302505 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302506 return NULL;
2507 }
2508
2509 logo->image = base;
2510 }
2511
2512 return logo;
2513}
2514
2515
2516struct fbimage* fetch_image_from_partition()
2517{
2518 if (target_is_emmc_boot()) {
2519 return splash_screen_mmc();
2520 } else {
2521 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002522 }
2523}
2524
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002525/* Get the size from partiton name */
2526static void get_partition_size(const char *arg, char *response)
2527{
2528 uint64_t ptn = 0;
2529 uint64_t size;
2530 int index = INVALID_PTN;
2531
2532 index = partition_get_index(arg);
2533
2534 if (index == INVALID_PTN)
2535 {
2536 dprintf(CRITICAL, "Invalid partition index\n");
2537 return;
2538 }
2539
2540 ptn = partition_get_offset(index);
2541
2542 if(!ptn)
2543 {
2544 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2545 return;
2546 }
2547
2548 size = partition_get_size(index);
2549
2550 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2551 return;
2552}
2553
2554/*
2555 * Publish the partition type & size info
2556 * fastboot getvar will publish the required information.
2557 * fastboot getvar partition_size:<partition_name>: partition size in hex
2558 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2559 */
2560static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2561{
2562 uint8_t i;
2563
2564 for (i = 0; i < num_parts; i++) {
2565 get_partition_size(info[i].part_name, info[i].size_response);
2566
2567 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2568 {
2569 dprintf(CRITICAL, "partition size name truncated\n");
2570 return;
2571 }
2572 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2573 {
2574 dprintf(CRITICAL, "partition type name truncated\n");
2575 return;
2576 }
2577
2578 /* publish partition size & type info */
2579 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2580 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2581 }
2582}
2583
Amol Jadi5edf3552013-07-23 14:15:34 -07002584/* register commands and variables for fastboot */
2585void aboot_fastboot_register_commands(void)
2586{
2587 if (target_is_emmc_boot())
2588 {
2589 fastboot_register("flash:", cmd_flash_mmc);
2590 fastboot_register("erase:", cmd_erase_mmc);
2591 }
2592 else
2593 {
2594 fastboot_register("flash:", cmd_flash);
2595 fastboot_register("erase:", cmd_erase);
2596 }
2597
2598 fastboot_register("boot", cmd_boot);
2599 fastboot_register("continue", cmd_continue);
2600 fastboot_register("reboot", cmd_reboot);
2601 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2602 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002603 fastboot_register("oem lock", cmd_oem_lock);
2604 fastboot_register("oem verified", cmd_oem_verified);
Amol Jadi5edf3552013-07-23 14:15:34 -07002605 fastboot_register("oem device-info", cmd_oem_devinfo);
2606 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002607 fastboot_register("oem enable-charger-screen",
2608 cmd_oem_enable_charger_screen);
2609 fastboot_register("oem disable-charger-screen",
2610 cmd_oem_disable_charger_screen);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302611 fastboot_register("oem select-display-panel",
2612 cmd_oem_select_display_panel);
Amol Jadi5edf3552013-07-23 14:15:34 -07002613 /* publish variables and their values */
2614 fastboot_publish("product", TARGET(BOARD));
2615 fastboot_publish("kernel", "lk");
2616 fastboot_publish("serialno", sn_buf);
2617
2618 /*
2619 * partition info is supported only for emmc partitions
2620 * Calling this for NAND prints some error messages which
2621 * is harmless but misleading. Avoid calling this for NAND
2622 * devices.
2623 */
2624 if (target_is_emmc_boot())
2625 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2626
2627 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002628 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2629 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002630 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002631 /* Is the charger screen check enabled */
2632 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2633 device.charger_screen_enabled);
2634 fastboot_publish("charger-screen-enabled",
2635 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302636 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2637 device.display_panel);
2638 fastboot_publish("display-panel",
2639 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002640}
2641
Brian Swetland9c4c0752009-01-25 16:23:50 -08002642void aboot_init(const struct app_descriptor *app)
2643{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002644 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002645 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002646
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002647 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002648 if (target_is_emmc_boot())
2649 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002650 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002651 page_mask = page_size - 1;
2652 }
2653 else
2654 {
2655 page_size = flash_page_size();
2656 page_mask = page_size - 1;
2657 }
2658
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002659 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2660
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002661 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002662
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002663 /* Display splash screen if enabled */
2664#if DISPLAY_SPLASH_SCREEN
2665 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302666 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002667 dprintf(SPEW, "Display Init: Done\n");
2668#endif
2669
2670
Greg Griscod6250552011-06-29 14:40:23 -07002671 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002672 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002673
Dhaval Patel223ec952013-07-18 14:49:44 -07002674 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2675
Matthew Qindefd5562014-07-11 18:02:40 +08002676 /*
2677 * Check power off reason if user force reset,
2678 * if yes phone will do normal boot.
2679 */
2680 if (is_user_force_reset())
2681 goto normal_boot;
2682
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002683 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002684 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002685 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002686 dprintf(ALWAYS,"dload mode key sequence detected\n");
2687 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002688 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002689 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002690 }
2691 else
2692 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302693 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002694 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002695 }
2696 boot_into_fastboot = true;
2697 }
2698 if (!boot_into_fastboot)
2699 {
2700 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2701 boot_into_recovery = 1;
2702 if (!boot_into_recovery &&
2703 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002704 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002705 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002706 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002707 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002708 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002709 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002710
Ajay Dudani77421292010-10-27 19:34:06 -07002711 reboot_mode = check_reboot_mode();
2712 if (reboot_mode == RECOVERY_MODE) {
2713 boot_into_recovery = 1;
2714 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002715 boot_into_fastboot = true;
Matthew Qind886f3c2014-01-17 16:52:01 +08002716 } else if(reboot_mode == ALARM_BOOT) {
2717 boot_reason_alarm = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002718 }
2719
Matthew Qindefd5562014-07-11 18:02:40 +08002720normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03002721 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002722 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002723 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002724 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002725 if(emmc_recovery_init())
2726 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2727 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002728 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002729 if((device.is_unlocked) || (device.is_tampered))
2730 {
2731 #ifdef TZ_TAMPER_FUSE
2732 set_tamper_fuse_cmd();
2733 #endif
2734 #if USE_PCOM_SECBOOT
2735 set_tamper_flag(device.is_tampered);
2736 #endif
2737 }
Shashank Mittala0032282011-08-26 14:50:11 -07002738 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002739 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002740 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002741 else
2742 {
2743 recovery_init();
2744 #if USE_PCOM_SECBOOT
2745 if((device.is_unlocked) || (device.is_tampered))
2746 set_tamper_flag(device.is_tampered);
2747 #endif
2748 boot_linux_from_flash();
2749 }
2750 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2751 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002752 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002753
Amol Jadi5edf3552013-07-23 14:15:34 -07002754 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002755
Amol Jadi5edf3552013-07-23 14:15:34 -07002756 /* register aboot specific fastboot commands */
2757 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002758
Amol Jadi5edf3552013-07-23 14:15:34 -07002759 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002760 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002761
2762 /* initialize and start fastboot */
2763 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002764}
2765
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002766uint32_t get_page_size()
2767{
2768 return page_size;
2769}
2770
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002771/*
2772 * Calculated and save hash (SHA256) for non-signed boot image.
2773 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002774 * @param image_addr - Boot image address
2775 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002776 *
2777 * @return int - 0 on success, negative value on failure.
2778 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002779static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002780{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002781 unsigned int digest[8];
2782#if IMAGE_VERIF_ALGO_SHA1
2783 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
2784#else
2785 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
2786#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002787
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002788 target_crypto_init_params();
2789 hash_find(image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002790
2791 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002792 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002793
2794 return 0;
2795}
2796
Brian Swetland9c4c0752009-01-25 16:23:50 -08002797APP_START(aboot)
2798 .init = aboot_init,
2799APP_END