blob: 5fd12d60387226c41971c7993eb4dfadff24c7fe [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)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530808
809 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
810 dprintf(CRITICAL, "ERROR: Invalid page size\n");
811 return -1;
812 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700813 page_size = hdr->page_size;
814 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700815 }
816
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800817 /* Read the next page to get kernel Image header
818 * which lives in the second page for arm64 targets.
819 */
820
821 if (mmc_read(ptn + page_size, (unsigned int *) kbuf, page_size)) {
822 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
823 return -1;
824 }
825
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700826 /*
827 * Update the kernel/ramdisk/tags address if the boot image header
828 * has default values, these default values come from mkbootimg when
829 * the boot image is flashed using fastboot flash:raw
830 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800831 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700832
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700833 /* Get virtual addresses since the hdr saves physical addresses. */
834 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
835 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
836 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700837
838 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
839 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
840
841 /* Check if the addresses in the header are valid. */
842 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
843 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
844 {
845 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
846 return -1;
847 }
848
849#ifndef DEVICE_TREE
850 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
851 {
852 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
853 return -1;
854 }
855#endif
856
Shashank Mittalcd98d472011-08-02 14:29:24 -0700857 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300858 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
859 (int) target_use_signed_kernel(),
860 device.is_unlocked,
861 device.is_tampered);
862
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700863 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800864 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700865 offset = 0;
866
Shashank Mittalcd98d472011-08-02 14:29:24 -0700867 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700868
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800869#if DEVICE_TREE
870 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
871 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700872
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700873 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700874 {
875 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
876 return -1;
877 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800878#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530879 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700880
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700881#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700882
Amol Jadib6be5c12012-11-14 13:39:51 -0800883 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700884 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800885
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700886 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
887 {
888 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
889 return -1;
890 }
891
Shashank Mittalcd98d472011-08-02 14:29:24 -0700892 /* Read image without signature */
893 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
894 {
895 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
896 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800897 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700898
Amol Jadib6be5c12012-11-14 13:39:51 -0800899 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700900 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800901
Shashank Mittalcd98d472011-08-02 14:29:24 -0700902 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700903
904 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
905 {
906 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
907 return -1;
908 }
909
Shashank Mittalcd98d472011-08-02 14:29:24 -0700910 /* Read signature */
911 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
912 {
913 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700914 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700915 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800916
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700917 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700918
Neeti Desai465491e2012-07-31 12:53:35 -0700919 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700920 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
921 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700922
Neeti Desai465491e2012-07-31 12:53:35 -0700923 #if DEVICE_TREE
924 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700925 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700926 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700927
Deepa Dinamani19648b42013-09-05 17:05:55 -0700928 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700929 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
930 return -1;
931 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700932
Neeti Desai465491e2012-07-31 12:53:35 -0700933 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700934 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700935 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
936 return -1;
937 }
938
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700939 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700940 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700941 {
942 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
943 return -1;
944 }
945
Joel Kingaa335dc2013-06-03 16:11:08 -0700946 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800947 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800948 /*
949 * If appended dev tree is found, update the atags with
950 * memory address to the DTB appended location on RAM.
951 * Else update with the atags address in the kernel header
952 */
953 void *dtb;
954 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700955 hdr->kernel_size,
956 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800957 if (!dtb) {
958 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
959 return -1;
960 }
Neeti Desai465491e2012-07-31 12:53:35 -0700961 }
962 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700963 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700964 else
965 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800966 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700967
Amol Jadib6be5c12012-11-14 13:39:51 -0800968 dprintf(INFO, "Loading boot image (%d): start\n",
969 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700970 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800971
972 offset = page_size;
973
974 /* Load kernel */
975 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700976 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
977 return -1;
978 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800979 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700980
Amol Jadib6be5c12012-11-14 13:39:51 -0800981 /* Load ramdisk */
982 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700983 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800984 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700985 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
986 return -1;
987 }
988 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800989 offset += ramdisk_actual;
990
991 dprintf(INFO, "Loading boot image (%d): done\n",
992 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700993 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700994
995 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800996 offset += second_actual;
997 /* Second image loading not implemented. */
998 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700999 }
1000
1001 #if DEVICE_TREE
1002 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -07001003 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -07001004 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
1005 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1006 return -1;
1007 }
1008 table = (struct dt_table*) dt_buf;
1009
Deepa Dinamani19648b42013-09-05 17:05:55 -07001010 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001011 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1012 return -1;
1013 }
1014
Deepa Dinamani19648b42013-09-05 17:05:55 -07001015 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1016 if (!table)
1017 return -1;
1018
1019 /* Read the entire device tree table into buffer */
1020 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
1021 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1022 return -1;
1023 }
1024
Joel Kingaa335dc2013-06-03 16:11:08 -07001025 /* Find index of device tree within device tree table */
1026 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001027 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1028 return -1;
1029 }
1030
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001031 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001032 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001033 {
1034 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1035 return -1;
1036 }
1037
Joel Kingaa335dc2013-06-03 16:11:08 -07001038 if(mmc_read(ptn + offset + dt_entry.offset,
1039 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -07001040 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1041 return -1;
1042 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +03001043 #ifdef TZ_SAVE_KERNEL_HASH
1044 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
1045 hdr->ramdisk_addr, ramdisk_actual,
1046 ptn, offset, hdr->dt_size);
1047 #endif /* TZ_SAVE_KERNEL_HASH */
1048
Channagoud Kadabi35095622013-03-01 13:53:05 -08001049 } else {
Aparna Mallavarapu33127372014-06-03 22:15:54 +05301050
1051 /* Validate the tags_addr */
1052 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1053 {
1054 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1055 return -1;
1056 }
Dima Zavin77e41f32013-03-06 16:10:43 -08001057 /*
1058 * If appended dev tree is found, update the atags with
1059 * memory address to the DTB appended location on RAM.
1060 * Else update with the atags address in the kernel header
1061 */
1062 void *dtb;
1063 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001064 kernel_actual,
1065 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001066 if (!dtb) {
1067 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1068 return -1;
1069 }
Neeti Desai465491e2012-07-31 12:53:35 -07001070 }
1071 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001072 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001073
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001074 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1075 target_load_ssd_keystore();
1076
Shashank Mittal23b8f422010-04-16 19:27:21 -07001077unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001078
Dima Zavin77e41f32013-03-06 16:10:43 -08001079 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001080 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001081 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1082
1083 return 0;
1084}
1085
Dima Zavin214cc642009-01-26 11:16:21 -08001086int boot_linux_from_flash(void)
1087{
1088 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001089 struct ptentry *ptn;
1090 struct ptable *ptable;
1091 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001092
Shashank Mittalcd98d472011-08-02 14:29:24 -07001093 unsigned char *image_addr = 0;
1094 unsigned kernel_actual;
1095 unsigned ramdisk_actual;
1096 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001097 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001098
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001099#if DEVICE_TREE
1100 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001101 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001102 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001103 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001104#endif
1105
David Ng183a7422009-12-07 14:55:21 -08001106 if (target_is_emmc_boot()) {
1107 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1108 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1109 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1110 return -1;
1111 }
1112 goto continue_boot;
1113 }
1114
Dima Zavin214cc642009-01-26 11:16:21 -08001115 ptable = flash_get_ptable();
1116 if (ptable == NULL) {
1117 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1118 return -1;
1119 }
1120
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001121 if(!boot_into_recovery)
1122 {
1123 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001124
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001125 if (ptn == NULL) {
1126 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1127 return -1;
1128 }
1129 }
1130 else
1131 {
1132 ptn = ptable_find(ptable, "recovery");
1133 if (ptn == NULL) {
1134 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1135 return -1;
1136 }
Dima Zavin214cc642009-01-26 11:16:21 -08001137 }
1138
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001139 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001140 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1141 return -1;
1142 }
Dima Zavin214cc642009-01-26 11:16:21 -08001143
1144 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001145 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001146 return -1;
1147 }
1148
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001149 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001150 dprintf(CRITICAL, "ERROR: Invalid boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001151 return -1;
1152 }
1153
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001154 /*
1155 * Update the kernel/ramdisk/tags address if the boot image header
1156 * has default values, these default values come from mkbootimg when
1157 * the boot image is flashed using fastboot flash:raw
1158 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001159 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001160
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001161 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001162 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1163 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1164 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1165
1166 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1167 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1168
1169 /* Check if the addresses in the header are valid. */
1170 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1171 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1172 {
1173 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1174 return -1;
1175 }
1176
1177#ifndef DEVICE_TREE
1178 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1179 {
1180 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1181 return -1;
1182 }
1183#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001184
Shashank Mittalcd98d472011-08-02 14:29:24 -07001185 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001186 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001187 {
1188 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001189 offset = 0;
1190
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001191#if DEVICE_TREE
1192 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1193 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001194
1195 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1196 {
1197 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1198 return -1;
1199 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001200#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001201 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001202#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001203
Amol Jadib6be5c12012-11-14 13:39:51 -08001204 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001205 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001206
Shashank Mittalcd98d472011-08-02 14:29:24 -07001207 /* Read image without signature */
1208 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1209 {
1210 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1211 return -1;
1212 }
Dima Zavin214cc642009-01-26 11:16:21 -08001213
Amol Jadib6be5c12012-11-14 13:39:51 -08001214 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001215 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001216
Shashank Mittalcd98d472011-08-02 14:29:24 -07001217 offset = imagesize_actual;
1218 /* Read signature */
1219 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1220 {
1221 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001222 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001223 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001224
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001225 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001226
1227 /* Move kernel and ramdisk to correct address */
1228 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1229 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001230#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001231 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001232 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001233 {
1234 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1235 return -1;
1236 }
1237
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001238 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1239#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001240
1241 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001242 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001243 {
1244 write_device_info_flash(&device);
1245 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301246#if USE_PCOM_SECBOOT
1247 set_tamper_flag(device.is_tampered);
1248#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001249 }
1250 else
1251 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001252 offset = page_size;
1253
Amol Jadib6be5c12012-11-14 13:39:51 -08001254 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1255 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1256 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1257
1258 dprintf(INFO, "Loading boot image (%d): start\n",
1259 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001260 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001261
1262 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001263 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1264 return -1;
1265 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001266 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001267
Amol Jadib6be5c12012-11-14 13:39:51 -08001268 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001269 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1270 return -1;
1271 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001272 offset += ramdisk_actual;
1273
1274 dprintf(INFO, "Loading boot image (%d): done\n",
1275 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001276 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001277
1278 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001279 offset += second_actual;
1280 /* Second image loading not implemented. */
1281 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001282 }
1283
1284#if DEVICE_TREE
1285 if(hdr->dt_size != 0) {
1286
1287 /* Read the device tree table into buffer */
1288 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1289 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1290 return -1;
1291 }
1292
1293 table = (struct dt_table*) dt_buf;
1294
Deepa Dinamani19648b42013-09-05 17:05:55 -07001295 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001296 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1297 return -1;
1298 }
1299
Deepa Dinamani19648b42013-09-05 17:05:55 -07001300 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1301 if (!table)
1302 return -1;
1303
1304 /* Read the entire device tree table into buffer */
1305 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1306 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1307 return -1;
1308 }
1309
1310
Joel Kingaa335dc2013-06-03 16:11:08 -07001311 /* Find index of device tree within device tree table */
1312 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001313 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1314 return -1;
1315 }
1316
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001317 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001318 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001319 {
1320 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1321 return -1;
1322 }
1323
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001324 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001325 if(flash_read(ptn, offset + dt_entry.offset,
1326 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001327 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1328 return -1;
1329 }
1330 }
1331#endif
1332
Shashank Mittalcd98d472011-08-02 14:29:24 -07001333 }
David Ng183a7422009-12-07 14:55:21 -08001334continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001335
Dima Zavin214cc642009-01-26 11:16:21 -08001336 /* TODO: create/pass atags to kernel */
1337
Ajay Dudanie28a6072011-07-01 13:59:46 -07001338 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001339 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001340 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1341
1342 return 0;
1343}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001344
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001345BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001346void write_device_info_mmc(device_info *dev)
1347{
1348 struct device_info *info = (void*) info_buf;
1349 unsigned long long ptn = 0;
1350 unsigned long long size;
1351 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001352 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001353 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001354
1355 index = partition_get_index("aboot");
1356 ptn = partition_get_offset(index);
1357 if(ptn == 0)
1358 {
1359 return;
1360 }
1361
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001362 lun = partition_get_lun(index);
1363 mmc_set_lun(lun);
1364
Shashank Mittal162244e2011-08-08 19:01:25 -07001365 size = partition_get_size(index);
1366
1367 memcpy(info, dev, sizeof(device_info));
1368
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001369 blocksize = mmc_get_device_blocksize();
1370
1371 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Shashank Mittal162244e2011-08-08 19:01:25 -07001372 {
1373 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1374 return;
1375 }
1376}
1377
1378void read_device_info_mmc(device_info *dev)
1379{
1380 struct device_info *info = (void*) info_buf;
1381 unsigned long long ptn = 0;
1382 unsigned long long size;
1383 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001384 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001385
1386 index = partition_get_index("aboot");
1387 ptn = partition_get_offset(index);
1388 if(ptn == 0)
1389 {
1390 return;
1391 }
1392
1393 size = partition_get_size(index);
1394
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001395 blocksize = mmc_get_device_blocksize();
1396
1397 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Shashank Mittal162244e2011-08-08 19:01:25 -07001398 {
1399 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1400 return;
1401 }
1402
1403 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1404 {
1405 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1406 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001407 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001408 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001409
1410 write_device_info_mmc(info);
1411 }
1412 memcpy(dev, info, sizeof(device_info));
1413}
1414
1415void write_device_info_flash(device_info *dev)
1416{
1417 struct device_info *info = (void *) info_buf;
1418 struct ptentry *ptn;
1419 struct ptable *ptable;
1420
1421 ptable = flash_get_ptable();
1422 if (ptable == NULL)
1423 {
1424 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1425 return;
1426 }
1427
1428 ptn = ptable_find(ptable, "devinfo");
1429 if (ptn == NULL)
1430 {
1431 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1432 return;
1433 }
1434
1435 memcpy(info, dev, sizeof(device_info));
1436
1437 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1438 {
1439 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1440 return;
1441 }
1442}
1443
1444void read_device_info_flash(device_info *dev)
1445{
1446 struct device_info *info = (void*) info_buf;
1447 struct ptentry *ptn;
1448 struct ptable *ptable;
1449
1450 ptable = flash_get_ptable();
1451 if (ptable == NULL)
1452 {
1453 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1454 return;
1455 }
1456
1457 ptn = ptable_find(ptable, "devinfo");
1458 if (ptn == NULL)
1459 {
1460 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1461 return;
1462 }
1463
1464 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1465 {
1466 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1467 return;
1468 }
1469
1470 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1471 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001472 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1473 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001474 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001475 write_device_info_flash(info);
1476 }
1477 memcpy(dev, info, sizeof(device_info));
1478}
1479
1480void write_device_info(device_info *dev)
1481{
1482 if(target_is_emmc_boot())
1483 {
1484 write_device_info_mmc(dev);
1485 }
1486 else
1487 {
1488 write_device_info_flash(dev);
1489 }
1490}
1491
1492void read_device_info(device_info *dev)
1493{
1494 if(target_is_emmc_boot())
1495 {
1496 read_device_info_mmc(dev);
1497 }
1498 else
1499 {
1500 read_device_info_flash(dev);
1501 }
1502}
1503
1504void reset_device_info()
1505{
1506 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001507 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001508 write_device_info(&device);
1509}
1510
1511void set_device_root()
1512{
1513 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001514 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001515 write_device_info(&device);
1516}
1517
Amol Jadicb524072012-08-09 16:40:18 -07001518#if DEVICE_TREE
1519int copy_dtb(uint8_t *boot_image_start)
1520{
1521 uint32 dt_image_offset = 0;
1522 uint32_t n;
1523 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001524 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001525 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001526
1527 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1528
Amol Jadicb524072012-08-09 16:40:18 -07001529 if(hdr->dt_size != 0) {
1530
1531 /* add kernel offset */
1532 dt_image_offset += page_size;
1533 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1534 dt_image_offset += n;
1535
1536 /* add ramdisk offset */
1537 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1538 dt_image_offset += n;
1539
1540 /* add second offset */
1541 if(hdr->second_size != 0) {
1542 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1543 dt_image_offset += n;
1544 }
1545
1546 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001547 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001548
Deepa Dinamani19648b42013-09-05 17:05:55 -07001549 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001550 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1551 return -1;
1552 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001553 /* Find index of device tree within device tree table */
1554 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001555 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1556 return -1;
1557 }
1558
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001559 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001560 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001561 {
1562 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1563 return -1;
1564 }
1565
Amol Jadicb524072012-08-09 16:40:18 -07001566 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001567 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001568 boot_image_start + dt_image_offset + dt_entry.offset,
1569 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001570 } else
1571 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001572
1573 /* Everything looks fine. Return success. */
1574 return 0;
1575}
1576#endif
1577
Brian Swetland9c4c0752009-01-25 16:23:50 -08001578void cmd_boot(const char *arg, void *data, unsigned sz)
1579{
1580 unsigned kernel_actual;
1581 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001582 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001583 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001584 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001585 int ret = 0;
1586 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001587
1588 if (sz < sizeof(hdr)) {
1589 fastboot_fail("invalid bootimage header");
1590 return;
1591 }
1592
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001593 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001594
1595 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001596 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001597
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001598 if(target_is_emmc_boot() && hdr->page_size) {
1599 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001600 page_mask = page_size - 1;
1601 }
1602
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001603 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1604 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1605
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001606 /*
1607 * Update the kernel/ramdisk/tags address if the boot image header
1608 * has default values, these default values come from mkbootimg when
1609 * the boot image is flashed using fastboot flash:raw
1610 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001611 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1612 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001613
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001614 /* Get virtual addresses since the hdr saves physical addresses. */
1615 hdr->kernel_addr = VA(hdr->kernel_addr);
1616 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1617 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001618
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001619 /* Check if the addresses in the header are valid. */
1620 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1621 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1622 {
1623 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001624 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001625 }
1626
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001627 /* sz should have atleast raw boot image */
1628 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001629 fastboot_fail("incomplete bootimage");
1630 return;
1631 }
1632
Amol Jadicb524072012-08-09 16:40:18 -07001633#if DEVICE_TREE
1634 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001635 ret = copy_dtb(data);
1636
1637 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001638#else
1639 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1640 {
1641 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001642 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001643 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001644#endif
1645
1646 /* Load ramdisk & kernel */
1647 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1648 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1649
1650#if DEVICE_TREE
1651 /*
1652 * If dtb is not found look for appended DTB in the kernel.
1653 * If appended dev tree is found, update the atags with
1654 * memory address to the DTB appended location on RAM.
1655 * Else update with the atags address in the kernel header
1656 */
1657 if (!dtb_copied) {
1658 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001659 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1660 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001661 if (!dtb) {
1662 fastboot_fail("dtb not found");
1663 return;
1664 }
Amol Jadicb524072012-08-09 16:40:18 -07001665 }
1666#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001667
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001668#ifndef DEVICE_TREE
1669 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1670 {
1671 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001672 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001673 }
1674#endif
1675
Brian Swetland9c4c0752009-01-25 16:23:50 -08001676 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001677 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001678
Dima Zavin77e41f32013-03-06 16:10:43 -08001679 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001680 (const char*) hdr->cmdline, board_machtype(),
1681 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001682}
1683
Dima Zavin214cc642009-01-26 11:16:21 -08001684void cmd_erase(const char *arg, void *data, unsigned sz)
1685{
1686 struct ptentry *ptn;
1687 struct ptable *ptable;
1688
1689 ptable = flash_get_ptable();
1690 if (ptable == NULL) {
1691 fastboot_fail("partition table doesn't exist");
1692 return;
1693 }
1694
1695 ptn = ptable_find(ptable, arg);
1696 if (ptn == NULL) {
1697 fastboot_fail("unknown partition name");
1698 return;
1699 }
1700
1701 if (flash_erase(ptn)) {
1702 fastboot_fail("failed to erase partition");
1703 return;
1704 }
1705 fastboot_okay("");
1706}
1707
Bikas Gurungd48bd242010-09-04 19:54:32 -07001708
1709void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1710{
Kun Liang2f1601a2013-08-12 16:29:54 +08001711 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001712 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001713 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001714 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001715 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001716
Kinson Chikf1a43512011-07-14 11:28:39 -07001717 index = partition_get_index(arg);
1718 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001719 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001720
Kinson Chikf1a43512011-07-14 11:28:39 -07001721 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001722 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001723 return;
1724 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001725
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001726 lun = partition_get_lun(index);
1727 mmc_set_lun(lun);
1728
Oliver Wangcee448d2013-10-22 18:40:13 +08001729#if MMC_SDHCI_SUPPORT
1730 if (mmc_erase_card(ptn, size)) {
1731 fastboot_fail("failed to erase partition\n");
1732 return;
1733 }
1734#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001735 size = partition_get_size(index);
1736 if (size > DEFAULT_ERASE_SIZE)
1737 size = DEFAULT_ERASE_SIZE;
1738
neetidb4b24d62012-01-20 12:13:09 -08001739 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001740 0 in first several blocks */
1741 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001742 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001743 return;
1744 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001745#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001746 fastboot_okay("");
1747}
1748
1749
Ajay Dudani5c761132011-04-07 20:19:04 -07001750void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001751{
1752 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001753 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001754 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001755 char *token = NULL;
1756 char *pname = NULL;
1757 uint8_t lun = 0;
1758 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001759
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001760 token = strtok(arg, ":");
1761 pname = token;
1762 token = strtok(NULL, ":");
1763 if(token)
1764 {
1765 lun = atoi(token);
1766 mmc_set_lun(lun);
1767 lun_set = true;
1768 }
1769
1770 if (!strcmp(pname, "partition"))
Greg Grisco6e754772011-06-23 12:19:39 -07001771 {
1772 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001773 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001774 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001775 return;
1776 }
1777 }
Greg Grisco6e754772011-06-23 12:19:39 -07001778 else
1779 {
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001780 index = partition_get_index(pname);
Kinson Chikf1a43512011-07-14 11:28:39 -07001781 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001782 if(ptn == 0) {
1783 fastboot_fail("partition table doesn't exist");
1784 return;
1785 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001786
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001787 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
Greg Grisco6e754772011-06-23 12:19:39 -07001788 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1789 fastboot_fail("image is not a boot image");
1790 return;
1791 }
1792 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001793
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001794 if(!lun_set)
1795 {
1796 lun = partition_get_lun(index);
1797 mmc_set_lun(lun);
1798 }
1799
Kinson Chikf1a43512011-07-14 11:28:39 -07001800 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001801 if (ROUND_TO_PAGE(sz,511) > size) {
1802 fastboot_fail("size too large");
1803 return;
1804 }
1805 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1806 fastboot_fail("flash write failure");
1807 return;
1808 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001809 }
1810 fastboot_okay("");
1811 return;
1812}
1813
Ajay Dudani5c761132011-04-07 20:19:04 -07001814void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1815{
1816 unsigned int chunk;
1817 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001818 uint32_t *fill_buf = NULL;
1819 uint32_t fill_val;
1820 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001821 sparse_header_t *sparse_header;
1822 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001823 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001824 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301825 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001826 int index = INVALID_PTN;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001827 int i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001828 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001829
Kinson Chikf1a43512011-07-14 11:28:39 -07001830 index = partition_get_index(arg);
1831 ptn = partition_get_offset(index);
1832 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001833 fastboot_fail("partition table doesn't exist");
1834 return;
1835 }
1836
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301837 size = partition_get_size(index);
1838 if (ROUND_TO_PAGE(sz,511) > size) {
1839 fastboot_fail("size too large");
1840 return;
1841 }
1842
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001843 lun = partition_get_lun(index);
1844 mmc_set_lun(lun);
1845
Ajay Dudani5c761132011-04-07 20:19:04 -07001846 /* Read and skip over sparse image header */
1847 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001848 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1849 fastboot_fail("size too large");
1850 return;
1851 }
1852
Ajay Dudani5c761132011-04-07 20:19:04 -07001853 data += sparse_header->file_hdr_sz;
1854 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1855 {
1856 /* Skip the remaining bytes in a header that is longer than
1857 * we expected.
1858 */
1859 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1860 }
1861
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001862 dprintf (SPEW, "=== Sparse Image Header ===\n");
1863 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1864 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1865 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1866 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1867 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1868 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1869 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1870 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001871
1872 /* Start processing chunks */
1873 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1874 {
1875 /* Read and skip over chunk header */
1876 chunk_header = (chunk_header_t *) data;
1877 data += sizeof(chunk_header_t);
1878
1879 dprintf (SPEW, "=== Chunk Header ===\n");
1880 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1881 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1882 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1883
1884 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1885 {
1886 /* Skip the remaining bytes in a header that is longer than
1887 * we expected.
1888 */
1889 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1890 }
1891
1892 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1893 switch (chunk_header->chunk_type)
1894 {
1895 case CHUNK_TYPE_RAW:
1896 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1897 chunk_data_sz))
1898 {
1899 fastboot_fail("Bogus chunk size for chunk type Raw");
1900 return;
1901 }
1902
Ajay Dudaniab18f022011-05-12 14:39:22 -07001903 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1904 chunk_data_sz,
1905 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001906 {
1907 fastboot_fail("flash write failure");
1908 return;
1909 }
1910 total_blocks += chunk_header->chunk_sz;
1911 data += chunk_data_sz;
1912 break;
1913
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001914 case CHUNK_TYPE_FILL:
1915 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1916 sizeof(uint32_t)))
1917 {
1918 fastboot_fail("Bogus chunk size for chunk type FILL");
1919 return;
1920 }
1921
1922 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
1923 if (!fill_buf)
1924 {
1925 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
1926 return;
1927 }
1928
1929 fill_val = *(uint32_t *)data;
1930 data = (char *) data + sizeof(uint32_t);
1931 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
1932
1933 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
1934 {
1935 fill_buf[i] = fill_val;
1936 }
1937
1938 for (i = 0; i < chunk_blk_cnt; i++)
1939 {
1940 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1941 sparse_header->blk_sz,
1942 fill_buf))
1943 {
1944 fastboot_fail("flash write failure");
1945 free(fill_buf);
1946 return;
1947 }
1948
1949 total_blocks++;
1950 }
1951
1952 free(fill_buf);
1953 break;
1954
Ajay Dudani5c761132011-04-07 20:19:04 -07001955 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001956 total_blocks += chunk_header->chunk_sz;
1957 break;
1958
Ajay Dudani5c761132011-04-07 20:19:04 -07001959 case CHUNK_TYPE_CRC:
1960 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1961 {
1962 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1963 return;
1964 }
1965 total_blocks += chunk_header->chunk_sz;
1966 data += chunk_data_sz;
1967 break;
1968
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001969 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001970 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07001971 fastboot_fail("Unknown chunk type");
1972 return;
1973 }
1974 }
1975
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001976 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1977 total_blocks, sparse_header->total_blks);
1978
1979 if(total_blocks != sparse_header->total_blks)
1980 {
1981 fastboot_fail("sparse image write failure");
1982 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001983
1984 fastboot_okay("");
1985 return;
1986}
1987
1988void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1989{
1990 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001991 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1992 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001993
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001994#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001995 int ret=0;
1996 uint32 major_version=0;
1997 uint32 minor_version=0;
1998
1999 ret = scm_svc_version(&major_version,&minor_version);
2000 if(!ret)
2001 {
2002 if(major_version >= 2)
2003 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002004 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002005 {
2006 ret = encrypt_scm((uint32 **) &data, &sz);
2007 if (ret != 0) {
2008 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2009 return;
2010 }
2011
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002012 /* Protect only for SSD */
2013 if (!strcmp(arg, "ssd")) {
2014 ret = scm_protect_keystore((uint32 *) data, sz);
2015 if (ret != 0) {
2016 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2017 return;
2018 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002019 }
2020 }
2021 else
2022 {
2023 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2024 if(ret != 0)
2025 {
2026 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2027 return;
2028 }
2029 }
2030 }
2031 else
2032 {
2033 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2034 magic_number[1] == DECRYPT_MAGIC_1)
2035 {
2036 ret = decrypt_scm((uint32 **) &data, &sz);
2037 if (ret != 0) {
2038 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2039 return;
2040 }
2041 }
2042 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2043 magic_number[1] == ENCRYPT_MAGIC_1)
2044 {
2045 ret = encrypt_scm((uint32 **) &data, &sz);
2046 if (ret != 0) {
2047 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2048 return;
2049 }
2050 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002051 }
2052 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002053 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002054 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002055 dprintf(CRITICAL,"INVALID SVC Version\n");
2056 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002057 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002058#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002059
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002060 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002061 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
2062 cmd_flash_mmc_img(arg, data, sz);
2063 else
2064 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002065 return;
2066}
2067
Dima Zavin214cc642009-01-26 11:16:21 -08002068void cmd_flash(const char *arg, void *data, unsigned sz)
2069{
2070 struct ptentry *ptn;
2071 struct ptable *ptable;
2072 unsigned extra = 0;
2073
2074 ptable = flash_get_ptable();
2075 if (ptable == NULL) {
2076 fastboot_fail("partition table doesn't exist");
2077 return;
2078 }
2079
2080 ptn = ptable_find(ptable, arg);
2081 if (ptn == NULL) {
2082 fastboot_fail("unknown partition name");
2083 return;
2084 }
2085
2086 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2087 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2088 fastboot_fail("image is not a boot image");
2089 return;
2090 }
2091 }
2092
Amol Jadi5c61a952012-05-04 17:05:35 -07002093 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002094 || !strcmp(ptn->name, "userdata")
2095 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002096 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002097 || !strcmp(ptn->name, "modem"))
2098 {
2099 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07002100 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002101 else
2102 extra = 0;
2103 }
2104 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002105 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002106
2107 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2108 if (flash_write(ptn, extra, data, sz)) {
2109 fastboot_fail("flash write failure");
2110 return;
2111 }
2112 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2113 fastboot_okay("");
2114}
2115
2116void cmd_continue(const char *arg, void *data, unsigned sz)
2117{
2118 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002119 fastboot_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002120 if (target_is_emmc_boot())
2121 {
2122 boot_linux_from_mmc();
2123 }
2124 else
2125 {
2126 boot_linux_from_flash();
2127 }
Dima Zavin214cc642009-01-26 11:16:21 -08002128}
2129
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002130void cmd_reboot(const char *arg, void *data, unsigned sz)
2131{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002132 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002133 fastboot_okay("");
2134 reboot_device(0);
2135}
2136
2137void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2138{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002139 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002140 fastboot_okay("");
2141 reboot_device(FASTBOOT_MODE);
2142}
2143
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002144void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2145{
2146 dprintf(INFO, "Enabling charger screen check\n");
2147 device.charger_screen_enabled = 1;
2148 write_device_info(&device);
2149 fastboot_okay("");
2150}
2151
2152void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2153{
2154 dprintf(INFO, "Disabling charger screen check\n");
2155 device.charger_screen_enabled = 0;
2156 write_device_info(&device);
2157 fastboot_okay("");
2158}
2159
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302160void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2161{
2162 dprintf(INFO, "Selecting display panel %s\n", arg);
2163 if (arg)
2164 strlcpy(device.display_panel, arg,
2165 sizeof(device.display_panel));
2166 write_device_info(&device);
2167 fastboot_okay("");
2168}
2169
Shashank Mittal162244e2011-08-08 19:01:25 -07002170void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2171{
2172 if(!device.is_unlocked)
2173 {
2174 device.is_unlocked = 1;
2175 write_device_info(&device);
2176 }
2177 fastboot_okay("");
2178}
2179
Shashank Mittala0032282011-08-26 14:50:11 -07002180void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2181{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302182 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002183 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002184 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002185 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2186 fastboot_info(response);
2187 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002188 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302189 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2190 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002191 fastboot_okay("");
2192}
2193
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002194void cmd_preflash(const char *arg, void *data, unsigned sz)
2195{
2196 fastboot_okay("");
2197}
2198
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302199static struct fbimage logo_header = {0};
2200struct fbimage* splash_screen_flash();
2201
2202int splash_screen_check_header(struct fbimage *logo)
2203{
2204 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2205 return -1;
2206 if (logo->header.width == 0 || logo->header.height == 0)
2207 return -1;
2208 return 0;
2209}
2210
2211struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002212{
2213 struct ptentry *ptn;
2214 struct ptable *ptable;
2215 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302216 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002217
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302218
2219 ptable = flash_get_ptable();
2220 if (ptable == NULL) {
2221 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2222 return NULL;
2223 }
2224 ptn = ptable_find(ptable, "splash");
2225 if (ptn == NULL) {
2226 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2227 return NULL;
2228 }
2229
2230 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2231 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2232 return NULL;
2233 }
2234
2235 if (splash_screen_check_header(logo)) {
2236 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2237 return NULL;
2238 }
2239
2240 fb_display = fbcon_display();
2241 if (fb_display) {
2242 uint8_t *base = (uint8_t *) fb_display->base;
2243 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
2244 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002245 }
2246
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302247 if (flash_read(ptn + sizeof(logo->header), 0,
2248 base,
2249 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2250 fbcon_clear();
2251 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2252 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002253 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302254 logo->image = base;
2255 }
2256
2257 return logo;
2258}
2259
2260struct fbimage* splash_screen_mmc()
2261{
2262 int index = INVALID_PTN;
2263 unsigned long long ptn = 0;
2264 struct fbcon_config *fb_display = NULL;
2265 struct fbimage *logo = &logo_header;
2266
2267 index = partition_get_index("splash");
2268 if (index == 0) {
2269 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2270 return NULL;
2271 }
2272
2273 ptn = partition_get_offset(index);
2274 if (ptn == 0) {
2275 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2276 return NULL;
2277 }
2278
2279 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2280 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2281 return NULL;
2282 }
2283
2284 if (splash_screen_check_header(logo)) {
2285 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2286 return NULL;
2287 }
2288
2289 fb_display = fbcon_display();
2290 if (fb_display) {
2291 uint8_t *base = (uint8_t *) fb_display->base;
2292 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2293 base += LOGO_IMG_OFFSET;
2294
2295 if (mmc_read(ptn + sizeof(logo->header),
2296 base,
2297 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2298 fbcon_clear();
2299 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2300 return NULL;
2301 }
2302
2303 logo->image = base;
2304 }
2305
2306 return logo;
2307}
2308
2309
2310struct fbimage* fetch_image_from_partition()
2311{
2312 if (target_is_emmc_boot()) {
2313 return splash_screen_mmc();
2314 } else {
2315 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002316 }
2317}
2318
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002319/* Get the size from partiton name */
2320static void get_partition_size(const char *arg, char *response)
2321{
2322 uint64_t ptn = 0;
2323 uint64_t size;
2324 int index = INVALID_PTN;
2325
2326 index = partition_get_index(arg);
2327
2328 if (index == INVALID_PTN)
2329 {
2330 dprintf(CRITICAL, "Invalid partition index\n");
2331 return;
2332 }
2333
2334 ptn = partition_get_offset(index);
2335
2336 if(!ptn)
2337 {
2338 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2339 return;
2340 }
2341
2342 size = partition_get_size(index);
2343
2344 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2345 return;
2346}
2347
2348/*
2349 * Publish the partition type & size info
2350 * fastboot getvar will publish the required information.
2351 * fastboot getvar partition_size:<partition_name>: partition size in hex
2352 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2353 */
2354static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2355{
2356 uint8_t i;
2357
2358 for (i = 0; i < num_parts; i++) {
2359 get_partition_size(info[i].part_name, info[i].size_response);
2360
2361 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2362 {
2363 dprintf(CRITICAL, "partition size name truncated\n");
2364 return;
2365 }
2366 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2367 {
2368 dprintf(CRITICAL, "partition type name truncated\n");
2369 return;
2370 }
2371
2372 /* publish partition size & type info */
2373 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2374 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2375 }
2376}
2377
Amol Jadi5edf3552013-07-23 14:15:34 -07002378/* register commands and variables for fastboot */
2379void aboot_fastboot_register_commands(void)
2380{
2381 if (target_is_emmc_boot())
2382 {
2383 fastboot_register("flash:", cmd_flash_mmc);
2384 fastboot_register("erase:", cmd_erase_mmc);
2385 }
2386 else
2387 {
2388 fastboot_register("flash:", cmd_flash);
2389 fastboot_register("erase:", cmd_erase);
2390 }
2391
2392 fastboot_register("boot", cmd_boot);
2393 fastboot_register("continue", cmd_continue);
2394 fastboot_register("reboot", cmd_reboot);
2395 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2396 fastboot_register("oem unlock", cmd_oem_unlock);
2397 fastboot_register("oem device-info", cmd_oem_devinfo);
2398 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002399 fastboot_register("oem enable-charger-screen",
2400 cmd_oem_enable_charger_screen);
2401 fastboot_register("oem disable-charger-screen",
2402 cmd_oem_disable_charger_screen);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302403 fastboot_register("oem select-display-panel",
2404 cmd_oem_select_display_panel);
Amol Jadi5edf3552013-07-23 14:15:34 -07002405 /* publish variables and their values */
2406 fastboot_publish("product", TARGET(BOARD));
2407 fastboot_publish("kernel", "lk");
2408 fastboot_publish("serialno", sn_buf);
2409
2410 /*
2411 * partition info is supported only for emmc partitions
2412 * Calling this for NAND prints some error messages which
2413 * is harmless but misleading. Avoid calling this for NAND
2414 * devices.
2415 */
2416 if (target_is_emmc_boot())
2417 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2418
2419 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002420 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2421 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002422 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002423 /* Is the charger screen check enabled */
2424 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2425 device.charger_screen_enabled);
2426 fastboot_publish("charger-screen-enabled",
2427 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302428 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2429 device.display_panel);
2430 fastboot_publish("display-panel",
2431 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002432}
2433
Brian Swetland9c4c0752009-01-25 16:23:50 -08002434void aboot_init(const struct app_descriptor *app)
2435{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002436 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002437 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002438
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002439 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002440 if (target_is_emmc_boot())
2441 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002442 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002443 page_mask = page_size - 1;
2444 }
2445 else
2446 {
2447 page_size = flash_page_size();
2448 page_mask = page_size - 1;
2449 }
2450
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002451 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2452
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002453 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002454
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002455 /* Display splash screen if enabled */
2456#if DISPLAY_SPLASH_SCREEN
2457 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302458 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002459 dprintf(SPEW, "Display Init: Done\n");
2460#endif
2461
2462
Greg Griscod6250552011-06-29 14:40:23 -07002463 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002464 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002465
Dhaval Patel223ec952013-07-18 14:49:44 -07002466 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2467
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002468 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002469 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002470 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002471 dprintf(ALWAYS,"dload mode key sequence detected\n");
2472 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002473 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002474 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002475 }
2476 else
2477 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302478 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002479 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002480 }
2481 boot_into_fastboot = true;
2482 }
2483 if (!boot_into_fastboot)
2484 {
2485 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2486 boot_into_recovery = 1;
2487 if (!boot_into_recovery &&
2488 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002489 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002490 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002491 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002492 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002493 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002494 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002495
Ajay Dudani77421292010-10-27 19:34:06 -07002496 reboot_mode = check_reboot_mode();
2497 if (reboot_mode == RECOVERY_MODE) {
2498 boot_into_recovery = 1;
2499 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002500 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002501 }
2502
Pavel Nedev5d91d412013-04-29 11:34:24 +03002503 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002504 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002505 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002506 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002507 if(emmc_recovery_init())
2508 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2509 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002510 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002511 if((device.is_unlocked) || (device.is_tampered))
2512 {
2513 #ifdef TZ_TAMPER_FUSE
2514 set_tamper_fuse_cmd();
2515 #endif
2516 #if USE_PCOM_SECBOOT
2517 set_tamper_flag(device.is_tampered);
2518 #endif
2519 }
Shashank Mittala0032282011-08-26 14:50:11 -07002520 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002521 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002522 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002523 else
2524 {
2525 recovery_init();
2526 #if USE_PCOM_SECBOOT
2527 if((device.is_unlocked) || (device.is_tampered))
2528 set_tamper_flag(device.is_tampered);
2529 #endif
2530 boot_linux_from_flash();
2531 }
2532 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2533 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002534 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002535
Amol Jadi5edf3552013-07-23 14:15:34 -07002536 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002537
Amol Jadi5edf3552013-07-23 14:15:34 -07002538 /* register aboot specific fastboot commands */
2539 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002540
Amol Jadi5edf3552013-07-23 14:15:34 -07002541 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002542 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002543
2544 /* initialize and start fastboot */
2545 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002546}
2547
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002548uint32_t get_page_size()
2549{
2550 return page_size;
2551}
2552
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002553/*
2554 * Calculated and save hash (SHA256) for non-signed boot image.
2555 *
2556 * Hash the same data that is checked on the signed boot image.
2557 * Kernel and Ramdisk are already read to memory buffers.
2558 * Need to read the entire device-tree from mmc
2559 * since non-signed image only read the DT tags of the relevant platform.
2560 *
2561 * @param kernel_addr - kernel bufer
2562 * @param kernel_actual - kernel size in bytes
2563 * @param ramdisk_addr - ramdisk buffer
2564 * @param ramdisk_actual - ramdisk size
2565 * @param ptn - partition
2566 * @param dt_offset - device tree offset on mmc partition
2567 * @param dt_size
2568 *
2569 * @return int - 0 on success, negative value on failure.
2570 */
2571int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2572 void *ramdisk_addr, unsigned ramdisk_actual,
2573 unsigned long long ptn,
2574 unsigned dt_offset, unsigned dt_size)
2575{
2576 SHA256_CTX sha256_ctx;
2577 char digest[32]={0};
2578 char *buf = (char *)target_get_scratch_address();
2579 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2580 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2581
2582 SHA256_Init(&sha256_ctx);
2583
2584 /* Read Boot Header */
2585 if (mmc_read(ptn, buf, page_size))
2586 {
2587 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2588 return -1;
2589 }
2590 /* Read entire Device Tree */
2591 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2592 {
2593 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2594 return -1;
2595 }
2596 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2597 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2598 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2599 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2600
2601 SHA256_Final(digest, &sha256_ctx);
2602
2603 save_kernel_hash_cmd(digest);
2604 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2605
2606 return 0;
2607}
2608
Brian Swetland9c4c0752009-01-25 16:23:50 -08002609APP_START(aboot)
2610 .init = aboot_init,
2611APP_END