blob: 21b0c413bd44206af3bc349772d53f2e4dca01c7 [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>
Brian Swetland9c4c0752009-01-25 16:23:50 -080038#include <kernel/thread.h>
39#include <arch/ops.h>
40
Dima Zavin214cc642009-01-26 11:16:21 -080041#include <dev/flash.h>
42#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080043#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080044#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080045#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070046#include <target.h>
47#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070048#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070049#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070050#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070051#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070052#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030053#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070054#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070055#include <boot_device.h>
Dima Zavin214cc642009-01-26 11:16:21 -080056
Neeti Desai17379b82012-06-04 18:42:53 -070057#if DEVICE_TREE
58#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070059#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070060#endif
61
Shashank Mittalcd98d472011-08-02 14:29:24 -070062#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080063#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080064#include "bootimg.h"
65#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070066#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070067#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070068#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070069#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070070#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070071
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070072extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070073extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070074extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070075extern int get_target_boot_params(const char *cmdline, const char *part,
76 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070077
78void write_device_info_mmc(device_info *dev);
79void write_device_info_flash(device_info *dev);
80
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070081#define EXPAND(NAME) #NAME
82#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070083
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080084#ifdef MEMBASE
85#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
86#else
David Ng183a7422009-12-07 14:55:21 -080087#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080088#endif
89
Deepa Dinamani0e163a42013-05-24 17:08:15 -070090#ifndef MEMSIZE
91#define MEMSIZE 1024*1024
92#endif
93
94#define MAX_TAGS_SIZE 1024
95
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080096#define RECOVERY_MODE 0x77665502
97#define FASTBOOT_MODE 0x77665500
98
Kun Liang2f1601a2013-08-12 16:29:54 +080099/* make 4096 as default size to ensure EFS,EXT4's erasing */
100#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800101#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800102
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800103#define UBI_MAGIC "UBI#"
104#define UBI_MAGIC_SIZE 0x04
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700105#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800106
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800107#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
108
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700109#if UFS_SUPPORT
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700110static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700111#else
David Ng183a7422009-12-07 14:55:21 -0800112static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700113#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800114static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300115static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800116static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800117static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700118static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300119static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800120
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800121static const char *baseband_apq = " androidboot.baseband=apq";
122static const char *baseband_msm = " androidboot.baseband=msm";
123static const char *baseband_csfb = " androidboot.baseband=csfb";
124static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700125static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800126static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700127static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800128static const char *baseband_dsda = " androidboot.baseband=dsda";
129static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800130static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800131static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800132
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700133static unsigned page_size = 0;
134static unsigned page_mask = 0;
135static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
136static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700137static char target_boot_params[64];
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700138
Shashank Mittalcd98d472011-08-02 14:29:24 -0700139/* Assuming unauthorized kernel image by default */
140static int auth_kernel_img = 0;
141
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530142static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700143
Dima Zavin42168f22009-01-30 11:52:22 -0800144struct atag_ptbl_entry
145{
146 char name[16];
147 unsigned offset;
148 unsigned size;
149 unsigned flags;
150};
151
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700152/*
153 * Partition info, required to be published
154 * for fastboot
155 */
156struct getvar_partition_info {
157 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
158 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
159 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
160 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
161 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
162};
163
164/*
165 * Right now, we are publishing the info for only
166 * three partitions
167 */
168struct getvar_partition_info part_info[] =
169{
170 { "system" , "partition-size:", "partition-type:", "", "ext4" },
171 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
172 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
173};
174
175char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700176char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800177char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700178char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530179char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700180
Greg Griscod2471ef2011-07-14 13:00:42 -0700181extern int emmc_recovery_init(void);
182
Kinson Chik0b1c8162011-08-31 16:31:57 -0700183#if NO_KEYPAD_DRIVER
184extern int fastboot_trigger(void);
185#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700186
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800187static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700188{
189 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700190#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800191 if (is_arm64)
192 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
193 else
194 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700195 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
196 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700197#endif
198}
199
Dima Zavin42168f22009-01-30 11:52:22 -0800200static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
201{
202 struct atag_ptbl_entry atag_ptn;
203
204 memcpy(atag_ptn.name, ptn->name, 16);
205 atag_ptn.name[15] = '\0';
206 atag_ptn.offset = ptn->start;
207 atag_ptn.size = ptn->length;
208 atag_ptn.flags = ptn->flags;
209 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
210 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
211}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800212
Neeti Desaie245d492012-06-01 12:52:13 -0700213unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800214{
David Ng183a7422009-12-07 14:55:21 -0800215 int cmdline_len = 0;
216 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800217 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700218 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800219 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300220 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700221 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700222 char *boot_dev_buf = NULL;
Dima Zavin42168f22009-01-30 11:52:22 -0800223
Brian Swetland9c4c0752009-01-25 16:23:50 -0800224 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800225 cmdline_len = strlen(cmdline);
226 have_cmdline = 1;
227 }
228 if (target_is_emmc_boot()) {
229 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700230#if UFS_SUPPORT
231 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
232 ASSERT(boot_dev_buf);
233 platform_boot_dev_cmdline(boot_dev_buf);
234 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700235#endif
David Ng183a7422009-12-07 14:55:21 -0800236 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800237
238 cmdline_len += strlen(usb_sn_cmdline);
239 cmdline_len += strlen(sn_buf);
240
Pavel Nedev5614d222013-06-17 18:01:02 +0300241 if (boot_into_recovery && gpt_exists)
242 cmdline_len += strlen(secondary_gpt_enable);
243
Pavel Nedev328ac822013-04-05 15:25:11 +0300244 if (boot_into_ffbm) {
245 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700246 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800247 /* reduce kernel console messages to speed-up boot */
248 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700249 } else if (device.charger_screen_enabled &&
250 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700251 pause_at_bootup = 1;
252 cmdline_len += strlen(battchg_pause);
253 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800254
Shashank Mittalcd98d472011-08-02 14:29:24 -0700255 if(target_use_signed_kernel() && auth_kernel_img) {
256 cmdline_len += strlen(auth_kernel);
257 }
258
Joonwoo Park61112782013-10-02 19:50:39 -0700259 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
260 "system",
261 target_boot_params,
262 sizeof(target_boot_params)) == 0) {
263 have_target_boot_params = 1;
264 cmdline_len += strlen(target_boot_params);
265 }
266
Ajay Dudanid04110c2011-01-17 23:55:07 -0800267 /* Determine correct androidboot.baseband to use */
268 switch(target_baseband())
269 {
270 case BASEBAND_APQ:
271 cmdline_len += strlen(baseband_apq);
272 break;
273
274 case BASEBAND_MSM:
275 cmdline_len += strlen(baseband_msm);
276 break;
277
278 case BASEBAND_CSFB:
279 cmdline_len += strlen(baseband_csfb);
280 break;
281
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800282 case BASEBAND_SVLTE2A:
283 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800284 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700285
286 case BASEBAND_MDM:
287 cmdline_len += strlen(baseband_mdm);
288 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700289
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800290 case BASEBAND_MDM2:
291 cmdline_len += strlen(baseband_mdm2);
292 break;
293
Amol Jadi5c61a952012-05-04 17:05:35 -0700294 case BASEBAND_SGLTE:
295 cmdline_len += strlen(baseband_sglte);
296 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530297
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800298 case BASEBAND_SGLTE2:
299 cmdline_len += strlen(baseband_sglte2);
300 break;
301
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530302 case BASEBAND_DSDA:
303 cmdline_len += strlen(baseband_dsda);
304 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800305
306 case BASEBAND_DSDA2:
307 cmdline_len += strlen(baseband_dsda2);
308 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800309 }
310
Ajay Singh Parmar430bbb32014-03-18 15:26:30 -0700311 if (target_display_panel_node(device.display_panel,
312 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
313 strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700314 cmdline_len += strlen(display_panel_buf);
315 }
316
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800317 if (target_warm_boot()) {
318 warm_boot = true;
319 cmdline_len += strlen(warmboot_cmdline);
320 }
321
David Ng183a7422009-12-07 14:55:21 -0800322 if (cmdline_len > 0) {
323 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700324 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
325 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700326
Amol Jadi168b7712012-03-06 16:15:00 -0800327 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700328 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800329 if (have_cmdline) {
330 src = cmdline;
331 while ((*dst++ = *src++));
332 }
333 if (target_is_emmc_boot()) {
334 src = emmc_cmdline;
335 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700336 have_cmdline = 1;
337 while ((*dst++ = *src++));
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700338#if UFS_SUPPORT
339 src = boot_dev_buf;
340 if (have_cmdline) --dst;
341 while ((*dst++ = *src++));
342#endif
David Ngf773dde2010-07-26 19:55:08 -0700343 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800344
345 src = usb_sn_cmdline;
346 if (have_cmdline) --dst;
347 have_cmdline = 1;
348 while ((*dst++ = *src++));
349 src = sn_buf;
350 if (have_cmdline) --dst;
351 have_cmdline = 1;
352 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800353 if (warm_boot) {
354 if (have_cmdline) --dst;
355 src = warmboot_cmdline;
356 while ((*dst++ = *src++));
357 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800358
Pavel Nedev5614d222013-06-17 18:01:02 +0300359 if (boot_into_recovery && gpt_exists) {
360 src = secondary_gpt_enable;
361 if (have_cmdline) --dst;
362 while ((*dst++ = *src++));
363 }
364
Pavel Nedev328ac822013-04-05 15:25:11 +0300365 if (boot_into_ffbm) {
366 src = androidboot_mode;
367 if (have_cmdline) --dst;
368 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700369 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300370 if (have_cmdline) --dst;
371 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800372 src = loglevel;
373 if (have_cmdline) --dst;
374 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300375 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700376 src = battchg_pause;
377 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800378 while ((*dst++ = *src++));
379 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800380
Shashank Mittalcd98d472011-08-02 14:29:24 -0700381 if(target_use_signed_kernel() && auth_kernel_img) {
382 src = auth_kernel;
383 if (have_cmdline) --dst;
384 while ((*dst++ = *src++));
385 }
386
Ajay Dudanid04110c2011-01-17 23:55:07 -0800387 switch(target_baseband())
388 {
389 case BASEBAND_APQ:
390 src = baseband_apq;
391 if (have_cmdline) --dst;
392 while ((*dst++ = *src++));
393 break;
394
395 case BASEBAND_MSM:
396 src = baseband_msm;
397 if (have_cmdline) --dst;
398 while ((*dst++ = *src++));
399 break;
400
401 case BASEBAND_CSFB:
402 src = baseband_csfb;
403 if (have_cmdline) --dst;
404 while ((*dst++ = *src++));
405 break;
406
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800407 case BASEBAND_SVLTE2A:
408 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800409 if (have_cmdline) --dst;
410 while ((*dst++ = *src++));
411 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700412
413 case BASEBAND_MDM:
414 src = baseband_mdm;
415 if (have_cmdline) --dst;
416 while ((*dst++ = *src++));
417 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700418
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800419 case BASEBAND_MDM2:
420 src = baseband_mdm2;
421 if (have_cmdline) --dst;
422 while ((*dst++ = *src++));
423 break;
424
Amol Jadi5c61a952012-05-04 17:05:35 -0700425 case BASEBAND_SGLTE:
426 src = baseband_sglte;
427 if (have_cmdline) --dst;
428 while ((*dst++ = *src++));
429 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530430
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800431 case BASEBAND_SGLTE2:
432 src = baseband_sglte2;
433 if (have_cmdline) --dst;
434 while ((*dst++ = *src++));
435 break;
436
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530437 case BASEBAND_DSDA:
438 src = baseband_dsda;
439 if (have_cmdline) --dst;
440 while ((*dst++ = *src++));
441 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800442
443 case BASEBAND_DSDA2:
444 src = baseband_dsda2;
445 if (have_cmdline) --dst;
446 while ((*dst++ = *src++));
447 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800448 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700449
450 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700451 src = display_panel_buf;
452 if (have_cmdline) --dst;
453 while ((*dst++ = *src++));
454 }
Joonwoo Park61112782013-10-02 19:50:39 -0700455
456 if (have_target_boot_params) {
457 if (have_cmdline) --dst;
458 src = target_boot_params;
459 while ((*dst++ = *src++));
460 }
Neeti Desaie245d492012-06-01 12:52:13 -0700461 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700462
463
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700464 if (boot_dev_buf)
465 free(boot_dev_buf);
466
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800467 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700468 return cmdline_final;
469}
470
471unsigned *atag_core(unsigned *ptr)
472{
473 /* CORE */
474 *ptr++ = 2;
475 *ptr++ = 0x54410001;
476
477 return ptr;
478
479}
480
481unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
482 unsigned ramdisk_size)
483{
484 if (ramdisk_size) {
485 *ptr++ = 4;
486 *ptr++ = 0x54420005;
487 *ptr++ = (unsigned)ramdisk;
488 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800489 }
490
Neeti Desaie245d492012-06-01 12:52:13 -0700491 return ptr;
492}
493
494unsigned *atag_ptable(unsigned **ptr_addr)
495{
496 int i;
497 struct ptable *ptable;
498
499 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700500 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
501 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700502 *(*ptr_addr)++ = 0x4d534d70;
503 for (i = 0; i < ptable->count; ++i)
504 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
505 }
506
507 return (*ptr_addr);
508}
509
510unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
511{
512 int cmdline_length = 0;
513 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700514 char *dest;
515
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800516 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700517 n = (cmdline_length + 4) & (~3);
518
519 *ptr++ = (n / 4) + 2;
520 *ptr++ = 0x54410009;
521 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800522 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700523 ptr += (n / 4);
524
525 return ptr;
526}
527
528unsigned *atag_end(unsigned *ptr)
529{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800530 /* END */
531 *ptr++ = 0;
532 *ptr++ = 0;
533
Neeti Desaie245d492012-06-01 12:52:13 -0700534 return ptr;
535}
536
537void generate_atags(unsigned *ptr, const char *cmdline,
538 void *ramdisk, unsigned ramdisk_size)
539{
540
541 ptr = atag_core(ptr);
542 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
543 ptr = target_atag_mem(ptr);
544
545 /* Skip NAND partition ATAGS for eMMC boot */
546 if (!target_is_emmc_boot()){
547 ptr = atag_ptable(&ptr);
548 }
549
550 ptr = atag_cmdline(ptr, cmdline);
551 ptr = atag_end(ptr);
552}
553
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700554typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700555void boot_linux(void *kernel, unsigned *tags,
556 const char *cmdline, unsigned machtype,
557 void *ramdisk, unsigned ramdisk_size)
558{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800559 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800560#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700561 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800562#endif
563
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700564 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800565 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800566 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800567
568 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700569
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800570 final_cmdline = update_cmdline((const char*)cmdline);
571
Neeti Desai17379b82012-06-04 18:42:53 -0700572#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800573 dprintf(INFO, "Updating device tree: start\n");
574
Neeti Desai17379b82012-06-04 18:42:53 -0700575 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800576 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700577 if(ret)
578 {
579 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
580 ASSERT(0);
581 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800582 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700583#else
Neeti Desaie245d492012-06-01 12:52:13 -0700584 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800585 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700586#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700587
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700588 /* Perform target specific cleanup */
589 target_uninit();
590
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800591 /* Turn off splash screen if enabled */
592#if DISPLAY_SPLASH_SCREEN
593 target_display_shutdown();
594#endif
595
596
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800597 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
598 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800599
600 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700601
Amol Jadi4421e652011-06-16 15:00:48 -0700602 /* do any platform specific cleanup before kernel entry */
603 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700604
Brian Swetland9c4c0752009-01-25 16:23:50 -0800605 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700606
Amol Jadi504f9fe2012-08-16 13:56:48 -0700607#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800608 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700609#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700610 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800611
612 if (IS_ARM64(kptr))
613 /* Jump to a 64bit kernel */
614 scm_elexec_call((paddr_t)kernel, tags_phys);
615 else
616 /* Jump to a 32bit kernel */
617 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800618}
619
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700620/* Function to check if the memory address range falls within the aboot
621 * boundaries.
622 * start: Start of the memory region
623 * size: Size of the memory region
624 */
625int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
626{
627 /* Check for boundary conditions. */
628 if ((start + size) < start)
629 return -1;
630
631 /* Check for memory overlap. */
632 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
633 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700634 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700635 return 0;
636 else
637 return -1;
638}
639
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800640#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800641
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800642BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800643#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800644BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800645#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800646
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700647static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
648{
649 int ret;
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800650#if IMAGE_VERIF_ALGO_SHA1
651 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
652#else
653 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
654#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700655
656 /* Assume device is rooted at this time. */
657 device.is_tampered = 1;
658
659 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
660
661 ret = image_verify((unsigned char *)bootimg_addr,
662 (unsigned char *)(bootimg_addr + bootimg_size),
663 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800664 auth_algo);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700665
666 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
667
668 if (ret)
669 {
670 /* Authorized kernel */
671 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700672 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700673 }
674
675#if USE_PCOM_SECBOOT
676 set_tamper_flag(device.is_tampered);
677#endif
678
679 if(device.is_tampered)
680 {
681 write_device_info_mmc(&device);
682 #ifdef TZ_TAMPER_FUSE
683 set_tamper_fuse_cmd();
684 #endif
685 #ifdef ASSERT_ON_TAMPER
686 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
687 ASSERT(0);
688 #endif
689 }
690}
691
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530692static bool check_format_bit()
693{
694 bool ret = false;
695 int index;
696 uint64_t offset;
697 struct boot_selection_info *in = NULL;
698 char *buf = NULL;
699
700 index = partition_get_index("bootselect");
701 if (index == INVALID_PTN)
702 {
703 dprintf(INFO, "Unable to locate /bootselect partition\n");
704 return ret;
705 }
706 offset = partition_get_offset(index);
707 if(!offset)
708 {
709 dprintf(INFO, "partition /bootselect doesn't exist\n");
710 return ret;
711 }
712 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
713 ASSERT(buf);
714 if (mmc_read(offset, (unsigned int *)buf, page_size))
715 {
716 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
717 free(buf);
718 return ret;
719 }
720 in = (struct boot_selection_info *) buf;
721 if ((in->signature == BOOTSELECT_SIGNATURE) &&
722 (in->version == BOOTSELECT_VERSION)) {
723 if ((in->state_info & BOOTSELECT_FORMAT) &&
724 !(in->state_info & BOOTSELECT_FACTORY))
725 ret = true;
726 } else {
727 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
728 in->signature, in->version);
729 ASSERT(0);
730 }
731 free(buf);
732 return ret;
733}
734
Shashank Mittal23b8f422010-04-16 19:27:21 -0700735int boot_linux_from_mmc(void)
736{
737 struct boot_img_hdr *hdr = (void*) buf;
738 struct boot_img_hdr *uhdr;
739 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700740 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700741 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700742 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700743
Shashank Mittalcd98d472011-08-02 14:29:24 -0700744 unsigned char *image_addr = 0;
745 unsigned kernel_actual;
746 unsigned ramdisk_actual;
747 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700748 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700749
750#if DEVICE_TREE
751 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700752 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700753 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800754 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700755 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700756#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800757 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
758 struct kernel64_hdr *kptr = (void*) kbuf;
759
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530760 if (check_format_bit())
761 boot_into_recovery = 1;
762
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700763 if (!boot_into_recovery) {
764 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
765 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
766 if (rcode <= 0) {
767 boot_into_ffbm = false;
768 if (rcode < 0)
769 dprintf(CRITICAL,"failed to get ffbm cookie");
770 } else
771 boot_into_ffbm = true;
772 } else
773 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700774 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
775 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
776 dprintf(INFO, "Unified boot method!\n");
777 hdr = uhdr;
778 goto unified_boot;
779 }
Greg Griscod6250552011-06-29 14:40:23 -0700780 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700781 index = partition_get_index("boot");
782 ptn = partition_get_offset(index);
783 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700784 dprintf(CRITICAL, "ERROR: No boot partition found\n");
785 return -1;
786 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700787 }
788 else {
789 index = partition_get_index("recovery");
790 ptn = partition_get_offset(index);
791 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700792 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
793 return -1;
794 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700795 }
796
Greg Griscod6250552011-06-29 14:40:23 -0700797 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700798 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
799 return -1;
800 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700801
802 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700803 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700804 return -1;
805 }
806
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700807 if (hdr->page_size && (hdr->page_size != page_size)) {
808 page_size = hdr->page_size;
809 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700810 }
811
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800812 /* Read the next page to get kernel Image header
813 * which lives in the second page for arm64 targets.
814 */
815
816 if (mmc_read(ptn + page_size, (unsigned int *) kbuf, page_size)) {
817 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
818 return -1;
819 }
820
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700821 /*
822 * Update the kernel/ramdisk/tags address if the boot image header
823 * has default values, these default values come from mkbootimg when
824 * the boot image is flashed using fastboot flash:raw
825 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800826 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700827
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700828 /* Get virtual addresses since the hdr saves physical addresses. */
829 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
830 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
831 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700832
833 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
834 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
835
836 /* Check if the addresses in the header are valid. */
837 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
838 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
839 {
840 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
841 return -1;
842 }
843
844#ifndef DEVICE_TREE
845 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
846 {
847 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
848 return -1;
849 }
850#endif
851
Shashank Mittalcd98d472011-08-02 14:29:24 -0700852 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300853 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
854 (int) target_use_signed_kernel(),
855 device.is_unlocked,
856 device.is_tampered);
857
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700858 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800859 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700860 offset = 0;
861
Shashank Mittalcd98d472011-08-02 14:29:24 -0700862 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700863
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800864#if DEVICE_TREE
865 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
866 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700867
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700868 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700869 {
870 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
871 return -1;
872 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800873#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530874 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700875
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700876#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700877
Amol Jadib6be5c12012-11-14 13:39:51 -0800878 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700879 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800880
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700881 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
882 {
883 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
884 return -1;
885 }
886
Shashank Mittalcd98d472011-08-02 14:29:24 -0700887 /* Read image without signature */
888 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
889 {
890 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
891 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800892 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700893
Amol Jadib6be5c12012-11-14 13:39:51 -0800894 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700895 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800896
Shashank Mittalcd98d472011-08-02 14:29:24 -0700897 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700898
899 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
900 {
901 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
902 return -1;
903 }
904
Shashank Mittalcd98d472011-08-02 14:29:24 -0700905 /* Read signature */
906 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
907 {
908 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700909 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700910 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800911
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700912 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700913
Neeti Desai465491e2012-07-31 12:53:35 -0700914 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700915 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
916 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700917
Neeti Desai465491e2012-07-31 12:53:35 -0700918 #if DEVICE_TREE
919 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700920 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700921 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700922
Deepa Dinamani19648b42013-09-05 17:05:55 -0700923 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700924 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
925 return -1;
926 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700927
Neeti Desai465491e2012-07-31 12:53:35 -0700928 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700929 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700930 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
931 return -1;
932 }
933
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700934 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700935 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700936 {
937 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
938 return -1;
939 }
940
Joel Kingaa335dc2013-06-03 16:11:08 -0700941 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800942 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800943 /*
944 * If appended dev tree is found, update the atags with
945 * memory address to the DTB appended location on RAM.
946 * Else update with the atags address in the kernel header
947 */
948 void *dtb;
949 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700950 hdr->kernel_size,
951 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800952 if (!dtb) {
953 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
954 return -1;
955 }
Neeti Desai465491e2012-07-31 12:53:35 -0700956 }
957 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700958 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700959 else
960 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800961 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700962
Amol Jadib6be5c12012-11-14 13:39:51 -0800963 dprintf(INFO, "Loading boot image (%d): start\n",
964 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700965 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800966
967 offset = page_size;
968
969 /* Load kernel */
970 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700971 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
972 return -1;
973 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800974 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700975
Amol Jadib6be5c12012-11-14 13:39:51 -0800976 /* Load ramdisk */
977 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700978 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800979 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700980 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
981 return -1;
982 }
983 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800984 offset += ramdisk_actual;
985
986 dprintf(INFO, "Loading boot image (%d): done\n",
987 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700988 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700989
990 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800991 offset += second_actual;
992 /* Second image loading not implemented. */
993 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700994 }
995
996 #if DEVICE_TREE
997 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -0700998 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -0700999 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
1000 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1001 return -1;
1002 }
1003 table = (struct dt_table*) dt_buf;
1004
Deepa Dinamani19648b42013-09-05 17:05:55 -07001005 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001006 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1007 return -1;
1008 }
1009
Deepa Dinamani19648b42013-09-05 17:05:55 -07001010 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1011 if (!table)
1012 return -1;
1013
1014 /* Read the entire device tree table into buffer */
1015 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
1016 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1017 return -1;
1018 }
1019
Joel Kingaa335dc2013-06-03 16:11:08 -07001020 /* Find index of device tree within device tree table */
1021 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001022 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1023 return -1;
1024 }
1025
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001026 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001027 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001028 {
1029 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1030 return -1;
1031 }
1032
Joel Kingaa335dc2013-06-03 16:11:08 -07001033 if(mmc_read(ptn + offset + dt_entry.offset,
1034 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -07001035 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1036 return -1;
1037 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +03001038 #ifdef TZ_SAVE_KERNEL_HASH
1039 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
1040 hdr->ramdisk_addr, ramdisk_actual,
1041 ptn, offset, hdr->dt_size);
1042 #endif /* TZ_SAVE_KERNEL_HASH */
1043
Channagoud Kadabi35095622013-03-01 13:53:05 -08001044 } else {
Aparna Mallavarapu33127372014-06-03 22:15:54 +05301045
1046 /* Validate the tags_addr */
1047 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1048 {
1049 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1050 return -1;
1051 }
Dima Zavin77e41f32013-03-06 16:10:43 -08001052 /*
1053 * If appended dev tree is found, update the atags with
1054 * memory address to the DTB appended location on RAM.
1055 * Else update with the atags address in the kernel header
1056 */
1057 void *dtb;
1058 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001059 kernel_actual,
1060 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001061 if (!dtb) {
1062 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1063 return -1;
1064 }
Neeti Desai465491e2012-07-31 12:53:35 -07001065 }
1066 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001067 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001068
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001069 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1070 target_load_ssd_keystore();
1071
Shashank Mittal23b8f422010-04-16 19:27:21 -07001072unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001073
Dima Zavin77e41f32013-03-06 16:10:43 -08001074 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001075 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001076 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1077
1078 return 0;
1079}
1080
Dima Zavin214cc642009-01-26 11:16:21 -08001081int boot_linux_from_flash(void)
1082{
1083 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001084 struct ptentry *ptn;
1085 struct ptable *ptable;
1086 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001087
Shashank Mittalcd98d472011-08-02 14:29:24 -07001088 unsigned char *image_addr = 0;
1089 unsigned kernel_actual;
1090 unsigned ramdisk_actual;
1091 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001092 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001093
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001094#if DEVICE_TREE
1095 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001096 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001097 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001098 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001099#endif
1100
David Ng183a7422009-12-07 14:55:21 -08001101 if (target_is_emmc_boot()) {
1102 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1103 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1104 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1105 return -1;
1106 }
1107 goto continue_boot;
1108 }
1109
Dima Zavin214cc642009-01-26 11:16:21 -08001110 ptable = flash_get_ptable();
1111 if (ptable == NULL) {
1112 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1113 return -1;
1114 }
1115
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001116 if(!boot_into_recovery)
1117 {
1118 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001119
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001120 if (ptn == NULL) {
1121 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1122 return -1;
1123 }
1124 }
1125 else
1126 {
1127 ptn = ptable_find(ptable, "recovery");
1128 if (ptn == NULL) {
1129 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1130 return -1;
1131 }
Dima Zavin214cc642009-01-26 11:16:21 -08001132 }
1133
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001134 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001135 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1136 return -1;
1137 }
Dima Zavin214cc642009-01-26 11:16:21 -08001138
1139 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001140 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001141 return -1;
1142 }
1143
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001144 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001145 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 -08001146 return -1;
1147 }
1148
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001149 /*
1150 * Update the kernel/ramdisk/tags address if the boot image header
1151 * has default values, these default values come from mkbootimg when
1152 * the boot image is flashed using fastboot flash:raw
1153 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001154 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001155
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001156 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001157 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1158 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1159 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1160
1161 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1162 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1163
1164 /* Check if the addresses in the header are valid. */
1165 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1166 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1167 {
1168 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1169 return -1;
1170 }
1171
1172#ifndef DEVICE_TREE
1173 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1174 {
1175 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1176 return -1;
1177 }
1178#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001179
Shashank Mittalcd98d472011-08-02 14:29:24 -07001180 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001181 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001182 {
1183 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001184 offset = 0;
1185
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001186#if DEVICE_TREE
1187 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1188 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001189
1190 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1191 {
1192 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1193 return -1;
1194 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001195#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001196 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001197#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001198
Amol Jadib6be5c12012-11-14 13:39:51 -08001199 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001200 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001201
Shashank Mittalcd98d472011-08-02 14:29:24 -07001202 /* Read image without signature */
1203 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1204 {
1205 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1206 return -1;
1207 }
Dima Zavin214cc642009-01-26 11:16:21 -08001208
Amol Jadib6be5c12012-11-14 13:39:51 -08001209 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001210 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001211
Shashank Mittalcd98d472011-08-02 14:29:24 -07001212 offset = imagesize_actual;
1213 /* Read signature */
1214 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1215 {
1216 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001217 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001218 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001219
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001220 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001221
1222 /* Move kernel and ramdisk to correct address */
1223 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1224 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001225#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001226 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001227 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001228 {
1229 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1230 return -1;
1231 }
1232
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001233 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1234#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001235
1236 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001237 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001238 {
1239 write_device_info_flash(&device);
1240 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301241#if USE_PCOM_SECBOOT
1242 set_tamper_flag(device.is_tampered);
1243#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001244 }
1245 else
1246 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001247 offset = page_size;
1248
Amol Jadib6be5c12012-11-14 13:39:51 -08001249 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1250 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1251 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1252
1253 dprintf(INFO, "Loading boot image (%d): start\n",
1254 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001255 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001256
1257 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001258 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1259 return -1;
1260 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001261 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001262
Amol Jadib6be5c12012-11-14 13:39:51 -08001263 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001264 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1265 return -1;
1266 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001267 offset += ramdisk_actual;
1268
1269 dprintf(INFO, "Loading boot image (%d): done\n",
1270 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001271 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001272
1273 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001274 offset += second_actual;
1275 /* Second image loading not implemented. */
1276 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001277 }
1278
1279#if DEVICE_TREE
1280 if(hdr->dt_size != 0) {
1281
1282 /* Read the device tree table into buffer */
1283 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1284 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1285 return -1;
1286 }
1287
1288 table = (struct dt_table*) dt_buf;
1289
Deepa Dinamani19648b42013-09-05 17:05:55 -07001290 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001291 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1292 return -1;
1293 }
1294
Deepa Dinamani19648b42013-09-05 17:05:55 -07001295 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1296 if (!table)
1297 return -1;
1298
1299 /* Read the entire device tree table into buffer */
1300 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1301 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1302 return -1;
1303 }
1304
1305
Joel Kingaa335dc2013-06-03 16:11:08 -07001306 /* Find index of device tree within device tree table */
1307 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001308 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1309 return -1;
1310 }
1311
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001312 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001313 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001314 {
1315 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1316 return -1;
1317 }
1318
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001319 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001320 if(flash_read(ptn, offset + dt_entry.offset,
1321 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001322 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1323 return -1;
1324 }
1325 }
1326#endif
1327
Shashank Mittalcd98d472011-08-02 14:29:24 -07001328 }
David Ng183a7422009-12-07 14:55:21 -08001329continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001330
Dima Zavin214cc642009-01-26 11:16:21 -08001331 /* TODO: create/pass atags to kernel */
1332
Ajay Dudanie28a6072011-07-01 13:59:46 -07001333 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001334 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001335 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1336
1337 return 0;
1338}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001339
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001340BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001341void write_device_info_mmc(device_info *dev)
1342{
1343 struct device_info *info = (void*) info_buf;
1344 unsigned long long ptn = 0;
1345 unsigned long long size;
1346 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001347 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001348 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001349
1350 index = partition_get_index("aboot");
1351 ptn = partition_get_offset(index);
1352 if(ptn == 0)
1353 {
1354 return;
1355 }
1356
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001357 lun = partition_get_lun(index);
1358 mmc_set_lun(lun);
1359
Shashank Mittal162244e2011-08-08 19:01:25 -07001360 size = partition_get_size(index);
1361
1362 memcpy(info, dev, sizeof(device_info));
1363
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001364 blocksize = mmc_get_device_blocksize();
1365
1366 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Shashank Mittal162244e2011-08-08 19:01:25 -07001367 {
1368 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1369 return;
1370 }
1371}
1372
1373void read_device_info_mmc(device_info *dev)
1374{
1375 struct device_info *info = (void*) info_buf;
1376 unsigned long long ptn = 0;
1377 unsigned long long size;
1378 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001379 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001380
1381 index = partition_get_index("aboot");
1382 ptn = partition_get_offset(index);
1383 if(ptn == 0)
1384 {
1385 return;
1386 }
1387
1388 size = partition_get_size(index);
1389
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001390 blocksize = mmc_get_device_blocksize();
1391
1392 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Shashank Mittal162244e2011-08-08 19:01:25 -07001393 {
1394 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1395 return;
1396 }
1397
1398 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1399 {
1400 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1401 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001402 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001403 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001404
1405 write_device_info_mmc(info);
1406 }
1407 memcpy(dev, info, sizeof(device_info));
1408}
1409
1410void write_device_info_flash(device_info *dev)
1411{
1412 struct device_info *info = (void *) info_buf;
1413 struct ptentry *ptn;
1414 struct ptable *ptable;
1415
1416 ptable = flash_get_ptable();
1417 if (ptable == NULL)
1418 {
1419 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1420 return;
1421 }
1422
1423 ptn = ptable_find(ptable, "devinfo");
1424 if (ptn == NULL)
1425 {
1426 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1427 return;
1428 }
1429
1430 memcpy(info, dev, sizeof(device_info));
1431
1432 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1433 {
1434 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1435 return;
1436 }
1437}
1438
1439void read_device_info_flash(device_info *dev)
1440{
1441 struct device_info *info = (void*) info_buf;
1442 struct ptentry *ptn;
1443 struct ptable *ptable;
1444
1445 ptable = flash_get_ptable();
1446 if (ptable == NULL)
1447 {
1448 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1449 return;
1450 }
1451
1452 ptn = ptable_find(ptable, "devinfo");
1453 if (ptn == NULL)
1454 {
1455 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1456 return;
1457 }
1458
1459 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1460 {
1461 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1462 return;
1463 }
1464
1465 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1466 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001467 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1468 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001469 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001470 write_device_info_flash(info);
1471 }
1472 memcpy(dev, info, sizeof(device_info));
1473}
1474
1475void write_device_info(device_info *dev)
1476{
1477 if(target_is_emmc_boot())
1478 {
1479 write_device_info_mmc(dev);
1480 }
1481 else
1482 {
1483 write_device_info_flash(dev);
1484 }
1485}
1486
1487void read_device_info(device_info *dev)
1488{
1489 if(target_is_emmc_boot())
1490 {
1491 read_device_info_mmc(dev);
1492 }
1493 else
1494 {
1495 read_device_info_flash(dev);
1496 }
1497}
1498
1499void reset_device_info()
1500{
1501 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001502 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001503 write_device_info(&device);
1504}
1505
1506void set_device_root()
1507{
1508 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001509 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001510 write_device_info(&device);
1511}
1512
Amol Jadicb524072012-08-09 16:40:18 -07001513#if DEVICE_TREE
1514int copy_dtb(uint8_t *boot_image_start)
1515{
1516 uint32 dt_image_offset = 0;
1517 uint32_t n;
1518 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001519 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001520 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001521
1522 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1523
Amol Jadicb524072012-08-09 16:40:18 -07001524 if(hdr->dt_size != 0) {
1525
1526 /* add kernel offset */
1527 dt_image_offset += page_size;
1528 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1529 dt_image_offset += n;
1530
1531 /* add ramdisk offset */
1532 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1533 dt_image_offset += n;
1534
1535 /* add second offset */
1536 if(hdr->second_size != 0) {
1537 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1538 dt_image_offset += n;
1539 }
1540
1541 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001542 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001543
Deepa Dinamani19648b42013-09-05 17:05:55 -07001544 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001545 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1546 return -1;
1547 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001548 /* Find index of device tree within device tree table */
1549 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001550 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1551 return -1;
1552 }
1553
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001554 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001555 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001556 {
1557 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1558 return -1;
1559 }
1560
Amol Jadicb524072012-08-09 16:40:18 -07001561 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001562 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001563 boot_image_start + dt_image_offset + dt_entry.offset,
1564 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001565 } else
1566 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001567
1568 /* Everything looks fine. Return success. */
1569 return 0;
1570}
1571#endif
1572
Brian Swetland9c4c0752009-01-25 16:23:50 -08001573void cmd_boot(const char *arg, void *data, unsigned sz)
1574{
1575 unsigned kernel_actual;
1576 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001577 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001578 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001579 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001580 int ret = 0;
1581 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001582
1583 if (sz < sizeof(hdr)) {
1584 fastboot_fail("invalid bootimage header");
1585 return;
1586 }
1587
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001588 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001589
1590 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001591 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001592
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001593 if(target_is_emmc_boot() && hdr->page_size) {
1594 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001595 page_mask = page_size - 1;
1596 }
1597
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001598 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1599 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1600
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001601 /*
1602 * Update the kernel/ramdisk/tags address if the boot image header
1603 * has default values, these default values come from mkbootimg when
1604 * the boot image is flashed using fastboot flash:raw
1605 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001606 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1607 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001608
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001609 /* Get virtual addresses since the hdr saves physical addresses. */
1610 hdr->kernel_addr = VA(hdr->kernel_addr);
1611 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1612 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001613
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001614 /* Check if the addresses in the header are valid. */
1615 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1616 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1617 {
1618 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001619 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001620 }
1621
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001622 /* sz should have atleast raw boot image */
1623 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001624 fastboot_fail("incomplete bootimage");
1625 return;
1626 }
1627
Amol Jadicb524072012-08-09 16:40:18 -07001628#if DEVICE_TREE
1629 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001630 ret = copy_dtb(data);
1631
1632 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001633#else
1634 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1635 {
1636 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001637 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001638 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001639#endif
1640
1641 /* Load ramdisk & kernel */
1642 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1643 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1644
1645#if DEVICE_TREE
1646 /*
1647 * If dtb is not found look for appended DTB in the kernel.
1648 * If appended dev tree is found, update the atags with
1649 * memory address to the DTB appended location on RAM.
1650 * Else update with the atags address in the kernel header
1651 */
1652 if (!dtb_copied) {
1653 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001654 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1655 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001656 if (!dtb) {
1657 fastboot_fail("dtb not found");
1658 return;
1659 }
Amol Jadicb524072012-08-09 16:40:18 -07001660 }
1661#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001662
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001663#ifndef DEVICE_TREE
1664 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1665 {
1666 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001667 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001668 }
1669#endif
1670
Brian Swetland9c4c0752009-01-25 16:23:50 -08001671 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001672 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001673
Dima Zavin77e41f32013-03-06 16:10:43 -08001674 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001675 (const char*) hdr->cmdline, board_machtype(),
1676 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001677}
1678
Dima Zavin214cc642009-01-26 11:16:21 -08001679void cmd_erase(const char *arg, void *data, unsigned sz)
1680{
1681 struct ptentry *ptn;
1682 struct ptable *ptable;
1683
1684 ptable = flash_get_ptable();
1685 if (ptable == NULL) {
1686 fastboot_fail("partition table doesn't exist");
1687 return;
1688 }
1689
1690 ptn = ptable_find(ptable, arg);
1691 if (ptn == NULL) {
1692 fastboot_fail("unknown partition name");
1693 return;
1694 }
1695
1696 if (flash_erase(ptn)) {
1697 fastboot_fail("failed to erase partition");
1698 return;
1699 }
1700 fastboot_okay("");
1701}
1702
Bikas Gurungd48bd242010-09-04 19:54:32 -07001703
1704void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1705{
Kun Liang2f1601a2013-08-12 16:29:54 +08001706 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001707 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001708 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001709 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001710 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001711
Kinson Chikf1a43512011-07-14 11:28:39 -07001712 index = partition_get_index(arg);
1713 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001714 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001715
Kinson Chikf1a43512011-07-14 11:28:39 -07001716 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001717 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001718 return;
1719 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001720
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001721 lun = partition_get_lun(index);
1722 mmc_set_lun(lun);
1723
Oliver Wangcee448d2013-10-22 18:40:13 +08001724#if MMC_SDHCI_SUPPORT
1725 if (mmc_erase_card(ptn, size)) {
1726 fastboot_fail("failed to erase partition\n");
1727 return;
1728 }
1729#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001730 size = partition_get_size(index);
1731 if (size > DEFAULT_ERASE_SIZE)
1732 size = DEFAULT_ERASE_SIZE;
1733
neetidb4b24d62012-01-20 12:13:09 -08001734 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001735 0 in first several blocks */
1736 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001737 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001738 return;
1739 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001740#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001741 fastboot_okay("");
1742}
1743
1744
Ajay Dudani5c761132011-04-07 20:19:04 -07001745void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001746{
1747 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001748 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001749 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001750 char *token = NULL;
1751 char *pname = NULL;
1752 uint8_t lun = 0;
1753 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001754
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001755 token = strtok(arg, ":");
1756 pname = token;
1757 token = strtok(NULL, ":");
1758 if(token)
1759 {
1760 lun = atoi(token);
1761 mmc_set_lun(lun);
1762 lun_set = true;
1763 }
1764
1765 if (!strcmp(pname, "partition"))
Greg Grisco6e754772011-06-23 12:19:39 -07001766 {
1767 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001768 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001769 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001770 return;
1771 }
1772 }
Greg Grisco6e754772011-06-23 12:19:39 -07001773 else
1774 {
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001775 index = partition_get_index(pname);
Kinson Chikf1a43512011-07-14 11:28:39 -07001776 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001777 if(ptn == 0) {
1778 fastboot_fail("partition table doesn't exist");
1779 return;
1780 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001781
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001782 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
Greg Grisco6e754772011-06-23 12:19:39 -07001783 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1784 fastboot_fail("image is not a boot image");
1785 return;
1786 }
1787 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001788
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001789 if(!lun_set)
1790 {
1791 lun = partition_get_lun(index);
1792 mmc_set_lun(lun);
1793 }
1794
Kinson Chikf1a43512011-07-14 11:28:39 -07001795 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001796 if (ROUND_TO_PAGE(sz,511) > size) {
1797 fastboot_fail("size too large");
1798 return;
1799 }
1800 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1801 fastboot_fail("flash write failure");
1802 return;
1803 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001804 }
1805 fastboot_okay("");
1806 return;
1807}
1808
Ajay Dudani5c761132011-04-07 20:19:04 -07001809void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1810{
1811 unsigned int chunk;
1812 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001813 uint32_t *fill_buf = NULL;
1814 uint32_t fill_val;
1815 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001816 sparse_header_t *sparse_header;
1817 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001818 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001819 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301820 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001821 int index = INVALID_PTN;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001822 int i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001823 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001824
Kinson Chikf1a43512011-07-14 11:28:39 -07001825 index = partition_get_index(arg);
1826 ptn = partition_get_offset(index);
1827 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001828 fastboot_fail("partition table doesn't exist");
1829 return;
1830 }
1831
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301832 size = partition_get_size(index);
1833 if (ROUND_TO_PAGE(sz,511) > size) {
1834 fastboot_fail("size too large");
1835 return;
1836 }
1837
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001838 lun = partition_get_lun(index);
1839 mmc_set_lun(lun);
1840
Ajay Dudani5c761132011-04-07 20:19:04 -07001841 /* Read and skip over sparse image header */
1842 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001843 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1844 fastboot_fail("size too large");
1845 return;
1846 }
1847
Ajay Dudani5c761132011-04-07 20:19:04 -07001848 data += sparse_header->file_hdr_sz;
1849 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1850 {
1851 /* Skip the remaining bytes in a header that is longer than
1852 * we expected.
1853 */
1854 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1855 }
1856
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001857 dprintf (SPEW, "=== Sparse Image Header ===\n");
1858 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1859 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1860 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1861 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1862 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1863 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1864 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1865 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001866
1867 /* Start processing chunks */
1868 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1869 {
1870 /* Read and skip over chunk header */
1871 chunk_header = (chunk_header_t *) data;
1872 data += sizeof(chunk_header_t);
1873
1874 dprintf (SPEW, "=== Chunk Header ===\n");
1875 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1876 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1877 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1878
1879 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1880 {
1881 /* Skip the remaining bytes in a header that is longer than
1882 * we expected.
1883 */
1884 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1885 }
1886
1887 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1888 switch (chunk_header->chunk_type)
1889 {
1890 case CHUNK_TYPE_RAW:
1891 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1892 chunk_data_sz))
1893 {
1894 fastboot_fail("Bogus chunk size for chunk type Raw");
1895 return;
1896 }
1897
Ajay Dudaniab18f022011-05-12 14:39:22 -07001898 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1899 chunk_data_sz,
1900 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001901 {
1902 fastboot_fail("flash write failure");
1903 return;
1904 }
1905 total_blocks += chunk_header->chunk_sz;
1906 data += chunk_data_sz;
1907 break;
1908
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001909 case CHUNK_TYPE_FILL:
1910 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1911 sizeof(uint32_t)))
1912 {
1913 fastboot_fail("Bogus chunk size for chunk type FILL");
1914 return;
1915 }
1916
1917 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
1918 if (!fill_buf)
1919 {
1920 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
1921 return;
1922 }
1923
1924 fill_val = *(uint32_t *)data;
1925 data = (char *) data + sizeof(uint32_t);
1926 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
1927
1928 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
1929 {
1930 fill_buf[i] = fill_val;
1931 }
1932
1933 for (i = 0; i < chunk_blk_cnt; i++)
1934 {
1935 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1936 sparse_header->blk_sz,
1937 fill_buf))
1938 {
1939 fastboot_fail("flash write failure");
1940 free(fill_buf);
1941 return;
1942 }
1943
1944 total_blocks++;
1945 }
1946
1947 free(fill_buf);
1948 break;
1949
Ajay Dudani5c761132011-04-07 20:19:04 -07001950 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001951 total_blocks += chunk_header->chunk_sz;
1952 break;
1953
Ajay Dudani5c761132011-04-07 20:19:04 -07001954 case CHUNK_TYPE_CRC:
1955 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1956 {
1957 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1958 return;
1959 }
1960 total_blocks += chunk_header->chunk_sz;
1961 data += chunk_data_sz;
1962 break;
1963
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001964 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001965 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07001966 fastboot_fail("Unknown chunk type");
1967 return;
1968 }
1969 }
1970
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001971 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1972 total_blocks, sparse_header->total_blks);
1973
1974 if(total_blocks != sparse_header->total_blks)
1975 {
1976 fastboot_fail("sparse image write failure");
1977 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001978
1979 fastboot_okay("");
1980 return;
1981}
1982
1983void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1984{
1985 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001986 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1987 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001988
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001989#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001990 int ret=0;
1991 uint32 major_version=0;
1992 uint32 minor_version=0;
1993
1994 ret = scm_svc_version(&major_version,&minor_version);
1995 if(!ret)
1996 {
1997 if(major_version >= 2)
1998 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001999 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002000 {
2001 ret = encrypt_scm((uint32 **) &data, &sz);
2002 if (ret != 0) {
2003 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2004 return;
2005 }
2006
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002007 /* Protect only for SSD */
2008 if (!strcmp(arg, "ssd")) {
2009 ret = scm_protect_keystore((uint32 *) data, sz);
2010 if (ret != 0) {
2011 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2012 return;
2013 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002014 }
2015 }
2016 else
2017 {
2018 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2019 if(ret != 0)
2020 {
2021 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2022 return;
2023 }
2024 }
2025 }
2026 else
2027 {
2028 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2029 magic_number[1] == DECRYPT_MAGIC_1)
2030 {
2031 ret = decrypt_scm((uint32 **) &data, &sz);
2032 if (ret != 0) {
2033 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2034 return;
2035 }
2036 }
2037 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2038 magic_number[1] == ENCRYPT_MAGIC_1)
2039 {
2040 ret = encrypt_scm((uint32 **) &data, &sz);
2041 if (ret != 0) {
2042 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2043 return;
2044 }
2045 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002046 }
2047 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002048 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002049 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002050 dprintf(CRITICAL,"INVALID SVC Version\n");
2051 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002052 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002053#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002054
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002055 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002056 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
2057 cmd_flash_mmc_img(arg, data, sz);
2058 else
2059 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002060 return;
2061}
2062
Dima Zavin214cc642009-01-26 11:16:21 -08002063void cmd_flash(const char *arg, void *data, unsigned sz)
2064{
2065 struct ptentry *ptn;
2066 struct ptable *ptable;
2067 unsigned extra = 0;
2068
2069 ptable = flash_get_ptable();
2070 if (ptable == NULL) {
2071 fastboot_fail("partition table doesn't exist");
2072 return;
2073 }
2074
2075 ptn = ptable_find(ptable, arg);
2076 if (ptn == NULL) {
2077 fastboot_fail("unknown partition name");
2078 return;
2079 }
2080
2081 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2082 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2083 fastboot_fail("image is not a boot image");
2084 return;
2085 }
2086 }
2087
Amol Jadi5c61a952012-05-04 17:05:35 -07002088 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002089 || !strcmp(ptn->name, "userdata")
2090 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002091 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002092 || !strcmp(ptn->name, "modem"))
2093 {
2094 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07002095 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002096 else
2097 extra = 0;
2098 }
2099 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002100 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002101
2102 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2103 if (flash_write(ptn, extra, data, sz)) {
2104 fastboot_fail("flash write failure");
2105 return;
2106 }
2107 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2108 fastboot_okay("");
2109}
2110
2111void cmd_continue(const char *arg, void *data, unsigned sz)
2112{
2113 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002114 fastboot_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002115 if (target_is_emmc_boot())
2116 {
2117 boot_linux_from_mmc();
2118 }
2119 else
2120 {
2121 boot_linux_from_flash();
2122 }
Dima Zavin214cc642009-01-26 11:16:21 -08002123}
2124
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002125void cmd_reboot(const char *arg, void *data, unsigned sz)
2126{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002127 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002128 fastboot_okay("");
2129 reboot_device(0);
2130}
2131
2132void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2133{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002134 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002135 fastboot_okay("");
2136 reboot_device(FASTBOOT_MODE);
2137}
2138
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002139void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2140{
2141 dprintf(INFO, "Enabling charger screen check\n");
2142 device.charger_screen_enabled = 1;
2143 write_device_info(&device);
2144 fastboot_okay("");
2145}
2146
2147void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2148{
2149 dprintf(INFO, "Disabling charger screen check\n");
2150 device.charger_screen_enabled = 0;
2151 write_device_info(&device);
2152 fastboot_okay("");
2153}
2154
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302155void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2156{
2157 dprintf(INFO, "Selecting display panel %s\n", arg);
2158 if (arg)
2159 strlcpy(device.display_panel, arg,
2160 sizeof(device.display_panel));
2161 write_device_info(&device);
2162 fastboot_okay("");
2163}
2164
Shashank Mittal162244e2011-08-08 19:01:25 -07002165void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2166{
2167 if(!device.is_unlocked)
2168 {
2169 device.is_unlocked = 1;
2170 write_device_info(&device);
2171 }
2172 fastboot_okay("");
2173}
2174
Shashank Mittala0032282011-08-26 14:50:11 -07002175void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2176{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302177 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002178 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002179 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002180 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2181 fastboot_info(response);
2182 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002183 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302184 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2185 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002186 fastboot_okay("");
2187}
2188
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002189void cmd_preflash(const char *arg, void *data, unsigned sz)
2190{
2191 fastboot_okay("");
2192}
2193
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302194static struct fbimage logo_header = {0};
2195struct fbimage* splash_screen_flash();
2196
2197int splash_screen_check_header(struct fbimage *logo)
2198{
2199 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2200 return -1;
2201 if (logo->header.width == 0 || logo->header.height == 0)
2202 return -1;
2203 return 0;
2204}
2205
2206struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002207{
2208 struct ptentry *ptn;
2209 struct ptable *ptable;
2210 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302211 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002212
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302213
2214 ptable = flash_get_ptable();
2215 if (ptable == NULL) {
2216 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2217 return NULL;
2218 }
2219 ptn = ptable_find(ptable, "splash");
2220 if (ptn == NULL) {
2221 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2222 return NULL;
2223 }
2224
2225 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2226 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2227 return NULL;
2228 }
2229
2230 if (splash_screen_check_header(logo)) {
2231 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2232 return NULL;
2233 }
2234
2235 fb_display = fbcon_display();
2236 if (fb_display) {
2237 uint8_t *base = (uint8_t *) fb_display->base;
2238 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
2239 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002240 }
2241
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302242 if (flash_read(ptn + sizeof(logo->header), 0,
2243 base,
2244 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2245 fbcon_clear();
2246 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2247 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002248 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302249 logo->image = base;
2250 }
2251
2252 return logo;
2253}
2254
2255struct fbimage* splash_screen_mmc()
2256{
2257 int index = INVALID_PTN;
2258 unsigned long long ptn = 0;
2259 struct fbcon_config *fb_display = NULL;
2260 struct fbimage *logo = &logo_header;
2261
2262 index = partition_get_index("splash");
2263 if (index == 0) {
2264 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2265 return NULL;
2266 }
2267
2268 ptn = partition_get_offset(index);
2269 if (ptn == 0) {
2270 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2271 return NULL;
2272 }
2273
2274 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2275 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2276 return NULL;
2277 }
2278
2279 if (splash_screen_check_header(logo)) {
2280 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2281 return NULL;
2282 }
2283
2284 fb_display = fbcon_display();
2285 if (fb_display) {
2286 uint8_t *base = (uint8_t *) fb_display->base;
2287 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2288 base += LOGO_IMG_OFFSET;
2289
2290 if (mmc_read(ptn + sizeof(logo->header),
2291 base,
2292 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2293 fbcon_clear();
2294 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2295 return NULL;
2296 }
2297
2298 logo->image = base;
2299 }
2300
2301 return logo;
2302}
2303
2304
2305struct fbimage* fetch_image_from_partition()
2306{
2307 if (target_is_emmc_boot()) {
2308 return splash_screen_mmc();
2309 } else {
2310 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002311 }
2312}
2313
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002314/* Get the size from partiton name */
2315static void get_partition_size(const char *arg, char *response)
2316{
2317 uint64_t ptn = 0;
2318 uint64_t size;
2319 int index = INVALID_PTN;
2320
2321 index = partition_get_index(arg);
2322
2323 if (index == INVALID_PTN)
2324 {
2325 dprintf(CRITICAL, "Invalid partition index\n");
2326 return;
2327 }
2328
2329 ptn = partition_get_offset(index);
2330
2331 if(!ptn)
2332 {
2333 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2334 return;
2335 }
2336
2337 size = partition_get_size(index);
2338
2339 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2340 return;
2341}
2342
2343/*
2344 * Publish the partition type & size info
2345 * fastboot getvar will publish the required information.
2346 * fastboot getvar partition_size:<partition_name>: partition size in hex
2347 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2348 */
2349static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2350{
2351 uint8_t i;
2352
2353 for (i = 0; i < num_parts; i++) {
2354 get_partition_size(info[i].part_name, info[i].size_response);
2355
2356 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2357 {
2358 dprintf(CRITICAL, "partition size name truncated\n");
2359 return;
2360 }
2361 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2362 {
2363 dprintf(CRITICAL, "partition type name truncated\n");
2364 return;
2365 }
2366
2367 /* publish partition size & type info */
2368 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2369 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2370 }
2371}
2372
Amol Jadi5edf3552013-07-23 14:15:34 -07002373/* register commands and variables for fastboot */
2374void aboot_fastboot_register_commands(void)
2375{
2376 if (target_is_emmc_boot())
2377 {
2378 fastboot_register("flash:", cmd_flash_mmc);
2379 fastboot_register("erase:", cmd_erase_mmc);
2380 }
2381 else
2382 {
2383 fastboot_register("flash:", cmd_flash);
2384 fastboot_register("erase:", cmd_erase);
2385 }
2386
2387 fastboot_register("boot", cmd_boot);
2388 fastboot_register("continue", cmd_continue);
2389 fastboot_register("reboot", cmd_reboot);
2390 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2391 fastboot_register("oem unlock", cmd_oem_unlock);
2392 fastboot_register("oem device-info", cmd_oem_devinfo);
2393 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002394 fastboot_register("oem enable-charger-screen",
2395 cmd_oem_enable_charger_screen);
2396 fastboot_register("oem disable-charger-screen",
2397 cmd_oem_disable_charger_screen);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302398 fastboot_register("oem select-display-panel",
2399 cmd_oem_select_display_panel);
Amol Jadi5edf3552013-07-23 14:15:34 -07002400 /* publish variables and their values */
2401 fastboot_publish("product", TARGET(BOARD));
2402 fastboot_publish("kernel", "lk");
2403 fastboot_publish("serialno", sn_buf);
2404
2405 /*
2406 * partition info is supported only for emmc partitions
2407 * Calling this for NAND prints some error messages which
2408 * is harmless but misleading. Avoid calling this for NAND
2409 * devices.
2410 */
2411 if (target_is_emmc_boot())
2412 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2413
2414 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002415 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2416 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002417 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002418 /* Is the charger screen check enabled */
2419 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2420 device.charger_screen_enabled);
2421 fastboot_publish("charger-screen-enabled",
2422 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302423 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2424 device.display_panel);
2425 fastboot_publish("display-panel",
2426 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002427}
2428
Brian Swetland9c4c0752009-01-25 16:23:50 -08002429void aboot_init(const struct app_descriptor *app)
2430{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002431 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002432 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002433
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002434 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002435 if (target_is_emmc_boot())
2436 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002437 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002438 page_mask = page_size - 1;
2439 }
2440 else
2441 {
2442 page_size = flash_page_size();
2443 page_mask = page_size - 1;
2444 }
2445
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002446 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2447
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002448 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002449
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002450 /* Display splash screen if enabled */
2451#if DISPLAY_SPLASH_SCREEN
2452 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302453 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002454 dprintf(SPEW, "Display Init: Done\n");
2455#endif
2456
2457
Greg Griscod6250552011-06-29 14:40:23 -07002458 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002459 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002460
Dhaval Patel223ec952013-07-18 14:49:44 -07002461 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2462
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002463 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002464 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002465 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002466 dprintf(ALWAYS,"dload mode key sequence detected\n");
2467 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002468 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002469 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002470 }
2471 else
2472 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302473 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002474 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002475 }
2476 boot_into_fastboot = true;
2477 }
2478 if (!boot_into_fastboot)
2479 {
2480 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2481 boot_into_recovery = 1;
2482 if (!boot_into_recovery &&
2483 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002484 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002485 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002486 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002487 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002488 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002489 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002490
Ajay Dudani77421292010-10-27 19:34:06 -07002491 reboot_mode = check_reboot_mode();
2492 if (reboot_mode == RECOVERY_MODE) {
2493 boot_into_recovery = 1;
2494 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002495 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002496 }
2497
Pavel Nedev5d91d412013-04-29 11:34:24 +03002498 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002499 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002500 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002501 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002502 if(emmc_recovery_init())
2503 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2504 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002505 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002506 if((device.is_unlocked) || (device.is_tampered))
2507 {
2508 #ifdef TZ_TAMPER_FUSE
2509 set_tamper_fuse_cmd();
2510 #endif
2511 #if USE_PCOM_SECBOOT
2512 set_tamper_flag(device.is_tampered);
2513 #endif
2514 }
Shashank Mittala0032282011-08-26 14:50:11 -07002515 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002516 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002517 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002518 else
2519 {
2520 recovery_init();
2521 #if USE_PCOM_SECBOOT
2522 if((device.is_unlocked) || (device.is_tampered))
2523 set_tamper_flag(device.is_tampered);
2524 #endif
2525 boot_linux_from_flash();
2526 }
2527 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2528 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002529 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002530
Amol Jadi5edf3552013-07-23 14:15:34 -07002531 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002532
Amol Jadi5edf3552013-07-23 14:15:34 -07002533 /* register aboot specific fastboot commands */
2534 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002535
Amol Jadi5edf3552013-07-23 14:15:34 -07002536 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002537 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002538
2539 /* initialize and start fastboot */
2540 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002541}
2542
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002543uint32_t get_page_size()
2544{
2545 return page_size;
2546}
2547
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002548/*
2549 * Calculated and save hash (SHA256) for non-signed boot image.
2550 *
2551 * Hash the same data that is checked on the signed boot image.
2552 * Kernel and Ramdisk are already read to memory buffers.
2553 * Need to read the entire device-tree from mmc
2554 * since non-signed image only read the DT tags of the relevant platform.
2555 *
2556 * @param kernel_addr - kernel bufer
2557 * @param kernel_actual - kernel size in bytes
2558 * @param ramdisk_addr - ramdisk buffer
2559 * @param ramdisk_actual - ramdisk size
2560 * @param ptn - partition
2561 * @param dt_offset - device tree offset on mmc partition
2562 * @param dt_size
2563 *
2564 * @return int - 0 on success, negative value on failure.
2565 */
2566int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2567 void *ramdisk_addr, unsigned ramdisk_actual,
2568 unsigned long long ptn,
2569 unsigned dt_offset, unsigned dt_size)
2570{
2571 SHA256_CTX sha256_ctx;
2572 char digest[32]={0};
2573 char *buf = (char *)target_get_scratch_address();
2574 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2575 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2576
2577 SHA256_Init(&sha256_ctx);
2578
2579 /* Read Boot Header */
2580 if (mmc_read(ptn, buf, page_size))
2581 {
2582 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2583 return -1;
2584 }
2585 /* Read entire Device Tree */
2586 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2587 {
2588 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2589 return -1;
2590 }
2591 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2592 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2593 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2594 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2595
2596 SHA256_Final(digest, &sha256_ctx);
2597
2598 save_kernel_hash_cmd(digest);
2599 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2600
2601 return 0;
2602}
2603
Brian Swetland9c4c0752009-01-25 16:23:50 -08002604APP_START(aboot)
2605 .init = aboot_init,
2606APP_END