blob: 0432b3c20ff0fcdd6d45581feaf5cc43c19b1385 [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#"
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700104#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800105#define UBI_MAGIC_SIZE 0x04
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700106#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800107
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800108#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
109
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700110#if UFS_SUPPORT
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700111static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700112#else
David Ng183a7422009-12-07 14:55:21 -0800113static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700114#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800115static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300116static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800117static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800118static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700119static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300120static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800121
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800122static const char *baseband_apq = " androidboot.baseband=apq";
123static const char *baseband_msm = " androidboot.baseband=msm";
124static const char *baseband_csfb = " androidboot.baseband=csfb";
125static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700126static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800127static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700128static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800129static const char *baseband_dsda = " androidboot.baseband=dsda";
130static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800131static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800132static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800133
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700134static unsigned page_size = 0;
135static unsigned page_mask = 0;
136static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
137static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700138static char target_boot_params[64];
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700139
Shashank Mittalcd98d472011-08-02 14:29:24 -0700140/* Assuming unauthorized kernel image by default */
141static int auth_kernel_img = 0;
142
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530143static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700144
Dima Zavin42168f22009-01-30 11:52:22 -0800145struct atag_ptbl_entry
146{
147 char name[16];
148 unsigned offset;
149 unsigned size;
150 unsigned flags;
151};
152
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700153/*
154 * Partition info, required to be published
155 * for fastboot
156 */
157struct getvar_partition_info {
158 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
159 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
160 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
161 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
162 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
163};
164
165/*
166 * Right now, we are publishing the info for only
167 * three partitions
168 */
169struct getvar_partition_info part_info[] =
170{
171 { "system" , "partition-size:", "partition-type:", "", "ext4" },
172 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
173 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
174};
175
176char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700177char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800178char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700179char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530180char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700181
Greg Griscod2471ef2011-07-14 13:00:42 -0700182extern int emmc_recovery_init(void);
183
Kinson Chik0b1c8162011-08-31 16:31:57 -0700184#if NO_KEYPAD_DRIVER
185extern int fastboot_trigger(void);
186#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700187
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800188static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700189{
190 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700191#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800192 if (is_arm64)
193 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
194 else
195 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700196 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
197 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700198#endif
199}
200
Dima Zavin42168f22009-01-30 11:52:22 -0800201static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
202{
203 struct atag_ptbl_entry atag_ptn;
204
205 memcpy(atag_ptn.name, ptn->name, 16);
206 atag_ptn.name[15] = '\0';
207 atag_ptn.offset = ptn->start;
208 atag_ptn.size = ptn->length;
209 atag_ptn.flags = ptn->flags;
210 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
211 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
212}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800213
Neeti Desaie245d492012-06-01 12:52:13 -0700214unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800215{
David Ng183a7422009-12-07 14:55:21 -0800216 int cmdline_len = 0;
217 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800218 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700219 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800220 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300221 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700222 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700223 char *boot_dev_buf = NULL;
Dima Zavin42168f22009-01-30 11:52:22 -0800224
Brian Swetland9c4c0752009-01-25 16:23:50 -0800225 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800226 cmdline_len = strlen(cmdline);
227 have_cmdline = 1;
228 }
229 if (target_is_emmc_boot()) {
230 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700231#if UFS_SUPPORT
232 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
233 ASSERT(boot_dev_buf);
234 platform_boot_dev_cmdline(boot_dev_buf);
235 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700236#endif
David Ng183a7422009-12-07 14:55:21 -0800237 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800238
239 cmdline_len += strlen(usb_sn_cmdline);
240 cmdline_len += strlen(sn_buf);
241
Pavel Nedev5614d222013-06-17 18:01:02 +0300242 if (boot_into_recovery && gpt_exists)
243 cmdline_len += strlen(secondary_gpt_enable);
244
Pavel Nedev328ac822013-04-05 15:25:11 +0300245 if (boot_into_ffbm) {
246 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700247 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800248 /* reduce kernel console messages to speed-up boot */
249 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700250 } else if (device.charger_screen_enabled &&
251 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700252 pause_at_bootup = 1;
253 cmdline_len += strlen(battchg_pause);
254 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800255
Shashank Mittalcd98d472011-08-02 14:29:24 -0700256 if(target_use_signed_kernel() && auth_kernel_img) {
257 cmdline_len += strlen(auth_kernel);
258 }
259
Joonwoo Park61112782013-10-02 19:50:39 -0700260 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
261 "system",
262 target_boot_params,
263 sizeof(target_boot_params)) == 0) {
264 have_target_boot_params = 1;
265 cmdline_len += strlen(target_boot_params);
266 }
267
Ajay Dudanid04110c2011-01-17 23:55:07 -0800268 /* Determine correct androidboot.baseband to use */
269 switch(target_baseband())
270 {
271 case BASEBAND_APQ:
272 cmdline_len += strlen(baseband_apq);
273 break;
274
275 case BASEBAND_MSM:
276 cmdline_len += strlen(baseband_msm);
277 break;
278
279 case BASEBAND_CSFB:
280 cmdline_len += strlen(baseband_csfb);
281 break;
282
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800283 case BASEBAND_SVLTE2A:
284 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800285 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700286
287 case BASEBAND_MDM:
288 cmdline_len += strlen(baseband_mdm);
289 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700290
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800291 case BASEBAND_MDM2:
292 cmdline_len += strlen(baseband_mdm2);
293 break;
294
Amol Jadi5c61a952012-05-04 17:05:35 -0700295 case BASEBAND_SGLTE:
296 cmdline_len += strlen(baseband_sglte);
297 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530298
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800299 case BASEBAND_SGLTE2:
300 cmdline_len += strlen(baseband_sglte2);
301 break;
302
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530303 case BASEBAND_DSDA:
304 cmdline_len += strlen(baseband_dsda);
305 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800306
307 case BASEBAND_DSDA2:
308 cmdline_len += strlen(baseband_dsda2);
309 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800310 }
311
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700312 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
313 target_display_panel_node(device.display_panel,
Ajay Singh Parmar430bbb32014-03-18 15:26:30 -0700314 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
315 strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700316 cmdline_len += strlen(display_panel_buf);
317 }
318
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800319 if (target_warm_boot()) {
320 warm_boot = true;
321 cmdline_len += strlen(warmboot_cmdline);
322 }
323
David Ng183a7422009-12-07 14:55:21 -0800324 if (cmdline_len > 0) {
325 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700326 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
327 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700328
Amol Jadi168b7712012-03-06 16:15:00 -0800329 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700330 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800331 if (have_cmdline) {
332 src = cmdline;
333 while ((*dst++ = *src++));
334 }
335 if (target_is_emmc_boot()) {
336 src = emmc_cmdline;
337 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700338 have_cmdline = 1;
339 while ((*dst++ = *src++));
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700340#if UFS_SUPPORT
341 src = boot_dev_buf;
342 if (have_cmdline) --dst;
343 while ((*dst++ = *src++));
344#endif
David Ngf773dde2010-07-26 19:55:08 -0700345 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800346
347 src = usb_sn_cmdline;
348 if (have_cmdline) --dst;
349 have_cmdline = 1;
350 while ((*dst++ = *src++));
351 src = sn_buf;
352 if (have_cmdline) --dst;
353 have_cmdline = 1;
354 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800355 if (warm_boot) {
356 if (have_cmdline) --dst;
357 src = warmboot_cmdline;
358 while ((*dst++ = *src++));
359 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800360
Pavel Nedev5614d222013-06-17 18:01:02 +0300361 if (boot_into_recovery && gpt_exists) {
362 src = secondary_gpt_enable;
363 if (have_cmdline) --dst;
364 while ((*dst++ = *src++));
365 }
366
Pavel Nedev328ac822013-04-05 15:25:11 +0300367 if (boot_into_ffbm) {
368 src = androidboot_mode;
369 if (have_cmdline) --dst;
370 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700371 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300372 if (have_cmdline) --dst;
373 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800374 src = loglevel;
375 if (have_cmdline) --dst;
376 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300377 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700378 src = battchg_pause;
379 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800380 while ((*dst++ = *src++));
381 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800382
Shashank Mittalcd98d472011-08-02 14:29:24 -0700383 if(target_use_signed_kernel() && auth_kernel_img) {
384 src = auth_kernel;
385 if (have_cmdline) --dst;
386 while ((*dst++ = *src++));
387 }
388
Ajay Dudanid04110c2011-01-17 23:55:07 -0800389 switch(target_baseband())
390 {
391 case BASEBAND_APQ:
392 src = baseband_apq;
393 if (have_cmdline) --dst;
394 while ((*dst++ = *src++));
395 break;
396
397 case BASEBAND_MSM:
398 src = baseband_msm;
399 if (have_cmdline) --dst;
400 while ((*dst++ = *src++));
401 break;
402
403 case BASEBAND_CSFB:
404 src = baseband_csfb;
405 if (have_cmdline) --dst;
406 while ((*dst++ = *src++));
407 break;
408
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800409 case BASEBAND_SVLTE2A:
410 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800411 if (have_cmdline) --dst;
412 while ((*dst++ = *src++));
413 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700414
415 case BASEBAND_MDM:
416 src = baseband_mdm;
417 if (have_cmdline) --dst;
418 while ((*dst++ = *src++));
419 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700420
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800421 case BASEBAND_MDM2:
422 src = baseband_mdm2;
423 if (have_cmdline) --dst;
424 while ((*dst++ = *src++));
425 break;
426
Amol Jadi5c61a952012-05-04 17:05:35 -0700427 case BASEBAND_SGLTE:
428 src = baseband_sglte;
429 if (have_cmdline) --dst;
430 while ((*dst++ = *src++));
431 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530432
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800433 case BASEBAND_SGLTE2:
434 src = baseband_sglte2;
435 if (have_cmdline) --dst;
436 while ((*dst++ = *src++));
437 break;
438
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530439 case BASEBAND_DSDA:
440 src = baseband_dsda;
441 if (have_cmdline) --dst;
442 while ((*dst++ = *src++));
443 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800444
445 case BASEBAND_DSDA2:
446 src = baseband_dsda2;
447 if (have_cmdline) --dst;
448 while ((*dst++ = *src++));
449 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800450 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700451
452 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700453 src = display_panel_buf;
454 if (have_cmdline) --dst;
455 while ((*dst++ = *src++));
456 }
Joonwoo Park61112782013-10-02 19:50:39 -0700457
458 if (have_target_boot_params) {
459 if (have_cmdline) --dst;
460 src = target_boot_params;
461 while ((*dst++ = *src++));
462 }
Neeti Desaie245d492012-06-01 12:52:13 -0700463 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700464
465
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700466 if (boot_dev_buf)
467 free(boot_dev_buf);
468
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800469 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700470 return cmdline_final;
471}
472
473unsigned *atag_core(unsigned *ptr)
474{
475 /* CORE */
476 *ptr++ = 2;
477 *ptr++ = 0x54410001;
478
479 return ptr;
480
481}
482
483unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
484 unsigned ramdisk_size)
485{
486 if (ramdisk_size) {
487 *ptr++ = 4;
488 *ptr++ = 0x54420005;
489 *ptr++ = (unsigned)ramdisk;
490 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800491 }
492
Neeti Desaie245d492012-06-01 12:52:13 -0700493 return ptr;
494}
495
496unsigned *atag_ptable(unsigned **ptr_addr)
497{
498 int i;
499 struct ptable *ptable;
500
501 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700502 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
503 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700504 *(*ptr_addr)++ = 0x4d534d70;
505 for (i = 0; i < ptable->count; ++i)
506 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
507 }
508
509 return (*ptr_addr);
510}
511
512unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
513{
514 int cmdline_length = 0;
515 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700516 char *dest;
517
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800518 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700519 n = (cmdline_length + 4) & (~3);
520
521 *ptr++ = (n / 4) + 2;
522 *ptr++ = 0x54410009;
523 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800524 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700525 ptr += (n / 4);
526
527 return ptr;
528}
529
530unsigned *atag_end(unsigned *ptr)
531{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800532 /* END */
533 *ptr++ = 0;
534 *ptr++ = 0;
535
Neeti Desaie245d492012-06-01 12:52:13 -0700536 return ptr;
537}
538
539void generate_atags(unsigned *ptr, const char *cmdline,
540 void *ramdisk, unsigned ramdisk_size)
541{
542
543 ptr = atag_core(ptr);
544 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
545 ptr = target_atag_mem(ptr);
546
547 /* Skip NAND partition ATAGS for eMMC boot */
548 if (!target_is_emmc_boot()){
549 ptr = atag_ptable(&ptr);
550 }
551
552 ptr = atag_cmdline(ptr, cmdline);
553 ptr = atag_end(ptr);
554}
555
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700556typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700557void boot_linux(void *kernel, unsigned *tags,
558 const char *cmdline, unsigned machtype,
559 void *ramdisk, unsigned ramdisk_size)
560{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800561 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800562#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700563 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800564#endif
565
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700566 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800567 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800568 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800569
570 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700571
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800572 final_cmdline = update_cmdline((const char*)cmdline);
573
Neeti Desai17379b82012-06-04 18:42:53 -0700574#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800575 dprintf(INFO, "Updating device tree: start\n");
576
Neeti Desai17379b82012-06-04 18:42:53 -0700577 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800578 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700579 if(ret)
580 {
581 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
582 ASSERT(0);
583 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800584 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700585#else
Neeti Desaie245d492012-06-01 12:52:13 -0700586 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800587 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700588#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700589
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700590 /* Perform target specific cleanup */
591 target_uninit();
592
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800593 /* Turn off splash screen if enabled */
594#if DISPLAY_SPLASH_SCREEN
595 target_display_shutdown();
596#endif
597
598
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800599 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
600 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800601
602 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700603
Amol Jadi4421e652011-06-16 15:00:48 -0700604 /* do any platform specific cleanup before kernel entry */
605 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700606
Brian Swetland9c4c0752009-01-25 16:23:50 -0800607 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700608
Amol Jadi504f9fe2012-08-16 13:56:48 -0700609#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800610 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700611#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700612 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800613
614 if (IS_ARM64(kptr))
615 /* Jump to a 64bit kernel */
616 scm_elexec_call((paddr_t)kernel, tags_phys);
617 else
618 /* Jump to a 32bit kernel */
619 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800620}
621
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700622/* Function to check if the memory address range falls within the aboot
623 * boundaries.
624 * start: Start of the memory region
625 * size: Size of the memory region
626 */
627int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
628{
629 /* Check for boundary conditions. */
630 if ((start + size) < start)
631 return -1;
632
633 /* Check for memory overlap. */
634 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
635 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700636 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700637 return 0;
638 else
639 return -1;
640}
641
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800642#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800643
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800644BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800645#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800646BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800647#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800648
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700649static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
650{
651 int ret;
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800652#if IMAGE_VERIF_ALGO_SHA1
653 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
654#else
655 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
656#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700657
658 /* Assume device is rooted at this time. */
659 device.is_tampered = 1;
660
661 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
662
663 ret = image_verify((unsigned char *)bootimg_addr,
664 (unsigned char *)(bootimg_addr + bootimg_size),
665 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800666 auth_algo);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700667
668 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
669
670 if (ret)
671 {
672 /* Authorized kernel */
673 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700674 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700675 }
676
677#if USE_PCOM_SECBOOT
678 set_tamper_flag(device.is_tampered);
679#endif
680
681 if(device.is_tampered)
682 {
683 write_device_info_mmc(&device);
684 #ifdef TZ_TAMPER_FUSE
685 set_tamper_fuse_cmd();
686 #endif
687 #ifdef ASSERT_ON_TAMPER
688 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
689 ASSERT(0);
690 #endif
691 }
692}
693
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530694static bool check_format_bit()
695{
696 bool ret = false;
697 int index;
698 uint64_t offset;
699 struct boot_selection_info *in = NULL;
700 char *buf = NULL;
701
702 index = partition_get_index("bootselect");
703 if (index == INVALID_PTN)
704 {
705 dprintf(INFO, "Unable to locate /bootselect partition\n");
706 return ret;
707 }
708 offset = partition_get_offset(index);
709 if(!offset)
710 {
711 dprintf(INFO, "partition /bootselect doesn't exist\n");
712 return ret;
713 }
714 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
715 ASSERT(buf);
716 if (mmc_read(offset, (unsigned int *)buf, page_size))
717 {
718 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
719 free(buf);
720 return ret;
721 }
722 in = (struct boot_selection_info *) buf;
723 if ((in->signature == BOOTSELECT_SIGNATURE) &&
724 (in->version == BOOTSELECT_VERSION)) {
725 if ((in->state_info & BOOTSELECT_FORMAT) &&
726 !(in->state_info & BOOTSELECT_FACTORY))
727 ret = true;
728 } else {
729 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
730 in->signature, in->version);
731 ASSERT(0);
732 }
733 free(buf);
734 return ret;
735}
736
Shashank Mittal23b8f422010-04-16 19:27:21 -0700737int boot_linux_from_mmc(void)
738{
739 struct boot_img_hdr *hdr = (void*) buf;
740 struct boot_img_hdr *uhdr;
741 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700742 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700743 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700744 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700745
Shashank Mittalcd98d472011-08-02 14:29:24 -0700746 unsigned char *image_addr = 0;
747 unsigned kernel_actual;
748 unsigned ramdisk_actual;
749 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700750 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700751
752#if DEVICE_TREE
753 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700754 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700755 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800756 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700757 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700758#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800759 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
760 struct kernel64_hdr *kptr = (void*) kbuf;
761
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530762 if (check_format_bit())
763 boot_into_recovery = 1;
764
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700765 if (!boot_into_recovery) {
766 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
767 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
768 if (rcode <= 0) {
769 boot_into_ffbm = false;
770 if (rcode < 0)
771 dprintf(CRITICAL,"failed to get ffbm cookie");
772 } else
773 boot_into_ffbm = true;
774 } else
775 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700776 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
777 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
778 dprintf(INFO, "Unified boot method!\n");
779 hdr = uhdr;
780 goto unified_boot;
781 }
Greg Griscod6250552011-06-29 14:40:23 -0700782 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700783 index = partition_get_index("boot");
784 ptn = partition_get_offset(index);
785 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700786 dprintf(CRITICAL, "ERROR: No boot partition found\n");
787 return -1;
788 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700789 }
790 else {
791 index = partition_get_index("recovery");
792 ptn = partition_get_offset(index);
793 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700794 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
795 return -1;
796 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700797 }
798
Greg Griscod6250552011-06-29 14:40:23 -0700799 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700800 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
801 return -1;
802 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700803
804 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700805 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700806 return -1;
807 }
808
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700809 if (hdr->page_size && (hdr->page_size != page_size)) {
810 page_size = hdr->page_size;
811 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700812 }
813
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800814 /* Read the next page to get kernel Image header
815 * which lives in the second page for arm64 targets.
816 */
817
818 if (mmc_read(ptn + page_size, (unsigned int *) kbuf, page_size)) {
819 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
820 return -1;
821 }
822
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700823 /*
824 * Update the kernel/ramdisk/tags address if the boot image header
825 * has default values, these default values come from mkbootimg when
826 * the boot image is flashed using fastboot flash:raw
827 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800828 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700829
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700830 /* Get virtual addresses since the hdr saves physical addresses. */
831 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
832 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
833 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700834
835 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
836 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
837
838 /* Check if the addresses in the header are valid. */
839 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
840 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
841 {
842 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
843 return -1;
844 }
845
846#ifndef DEVICE_TREE
847 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
848 {
849 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
850 return -1;
851 }
852#endif
853
Shashank Mittalcd98d472011-08-02 14:29:24 -0700854 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300855 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
856 (int) target_use_signed_kernel(),
857 device.is_unlocked,
858 device.is_tampered);
859
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700860 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800861 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700862 offset = 0;
863
Shashank Mittalcd98d472011-08-02 14:29:24 -0700864 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700865
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800866#if DEVICE_TREE
867 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
868 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700869
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700870 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700871 {
872 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
873 return -1;
874 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800875#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530876 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700877
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700878#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700879
Amol Jadib6be5c12012-11-14 13:39:51 -0800880 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700881 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800882
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700883 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
884 {
885 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
886 return -1;
887 }
888
Shashank Mittalcd98d472011-08-02 14:29:24 -0700889 /* Read image without signature */
890 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
891 {
892 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
893 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800894 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700895
Amol Jadib6be5c12012-11-14 13:39:51 -0800896 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700897 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800898
Shashank Mittalcd98d472011-08-02 14:29:24 -0700899 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700900
901 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
902 {
903 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
904 return -1;
905 }
906
Shashank Mittalcd98d472011-08-02 14:29:24 -0700907 /* Read signature */
908 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
909 {
910 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700911 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700912 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800913
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700914 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700915
Neeti Desai465491e2012-07-31 12:53:35 -0700916 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700917 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
918 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700919
Neeti Desai465491e2012-07-31 12:53:35 -0700920 #if DEVICE_TREE
921 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700922 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700923 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700924
Deepa Dinamani19648b42013-09-05 17:05:55 -0700925 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700926 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
927 return -1;
928 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700929
Neeti Desai465491e2012-07-31 12:53:35 -0700930 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700931 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700932 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
933 return -1;
934 }
935
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700936 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700937 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700938 {
939 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
940 return -1;
941 }
942
Joel Kingaa335dc2013-06-03 16:11:08 -0700943 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800944 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800945 /*
946 * If appended dev tree is found, update the atags with
947 * memory address to the DTB appended location on RAM.
948 * Else update with the atags address in the kernel header
949 */
950 void *dtb;
951 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700952 hdr->kernel_size,
953 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800954 if (!dtb) {
955 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
956 return -1;
957 }
Neeti Desai465491e2012-07-31 12:53:35 -0700958 }
959 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700960 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700961 else
962 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800963 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700964
Amol Jadib6be5c12012-11-14 13:39:51 -0800965 dprintf(INFO, "Loading boot image (%d): start\n",
966 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700967 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800968
969 offset = page_size;
970
971 /* Load kernel */
972 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700973 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
974 return -1;
975 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800976 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700977
Amol Jadib6be5c12012-11-14 13:39:51 -0800978 /* Load ramdisk */
979 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700980 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800981 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700982 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
983 return -1;
984 }
985 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800986 offset += ramdisk_actual;
987
988 dprintf(INFO, "Loading boot image (%d): done\n",
989 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700990 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700991
992 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800993 offset += second_actual;
994 /* Second image loading not implemented. */
995 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700996 }
997
998 #if DEVICE_TREE
999 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -07001000 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -07001001 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
1002 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1003 return -1;
1004 }
1005 table = (struct dt_table*) dt_buf;
1006
Deepa Dinamani19648b42013-09-05 17:05:55 -07001007 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001008 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1009 return -1;
1010 }
1011
Deepa Dinamani19648b42013-09-05 17:05:55 -07001012 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1013 if (!table)
1014 return -1;
1015
1016 /* Read the entire device tree table into buffer */
1017 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
1018 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1019 return -1;
1020 }
1021
Joel Kingaa335dc2013-06-03 16:11:08 -07001022 /* Find index of device tree within device tree table */
1023 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001024 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1025 return -1;
1026 }
1027
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001028 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001029 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001030 {
1031 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1032 return -1;
1033 }
1034
Joel Kingaa335dc2013-06-03 16:11:08 -07001035 if(mmc_read(ptn + offset + dt_entry.offset,
1036 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -07001037 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1038 return -1;
1039 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +03001040 #ifdef TZ_SAVE_KERNEL_HASH
1041 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
1042 hdr->ramdisk_addr, ramdisk_actual,
1043 ptn, offset, hdr->dt_size);
1044 #endif /* TZ_SAVE_KERNEL_HASH */
1045
Channagoud Kadabi35095622013-03-01 13:53:05 -08001046 } else {
Aparna Mallavarapu33127372014-06-03 22:15:54 +05301047
1048 /* Validate the tags_addr */
1049 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1050 {
1051 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1052 return -1;
1053 }
Dima Zavin77e41f32013-03-06 16:10:43 -08001054 /*
1055 * If appended dev tree is found, update the atags with
1056 * memory address to the DTB appended location on RAM.
1057 * Else update with the atags address in the kernel header
1058 */
1059 void *dtb;
1060 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001061 kernel_actual,
1062 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001063 if (!dtb) {
1064 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1065 return -1;
1066 }
Neeti Desai465491e2012-07-31 12:53:35 -07001067 }
1068 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001069 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001070
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001071 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1072 target_load_ssd_keystore();
1073
Shashank Mittal23b8f422010-04-16 19:27:21 -07001074unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001075
Dima Zavin77e41f32013-03-06 16:10:43 -08001076 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001077 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001078 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1079
1080 return 0;
1081}
1082
Dima Zavin214cc642009-01-26 11:16:21 -08001083int boot_linux_from_flash(void)
1084{
1085 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001086 struct ptentry *ptn;
1087 struct ptable *ptable;
1088 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001089
Shashank Mittalcd98d472011-08-02 14:29:24 -07001090 unsigned char *image_addr = 0;
1091 unsigned kernel_actual;
1092 unsigned ramdisk_actual;
1093 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001094 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001095
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001096#if DEVICE_TREE
1097 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001098 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001099 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001100 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001101#endif
1102
David Ng183a7422009-12-07 14:55:21 -08001103 if (target_is_emmc_boot()) {
1104 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1105 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1106 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1107 return -1;
1108 }
1109 goto continue_boot;
1110 }
1111
Dima Zavin214cc642009-01-26 11:16:21 -08001112 ptable = flash_get_ptable();
1113 if (ptable == NULL) {
1114 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1115 return -1;
1116 }
1117
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001118 if(!boot_into_recovery)
1119 {
1120 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001121
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001122 if (ptn == NULL) {
1123 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1124 return -1;
1125 }
1126 }
1127 else
1128 {
1129 ptn = ptable_find(ptable, "recovery");
1130 if (ptn == NULL) {
1131 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1132 return -1;
1133 }
Dima Zavin214cc642009-01-26 11:16:21 -08001134 }
1135
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001136 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001137 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1138 return -1;
1139 }
Dima Zavin214cc642009-01-26 11:16:21 -08001140
1141 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001142 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001143 return -1;
1144 }
1145
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001146 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001147 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 -08001148 return -1;
1149 }
1150
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001151 /*
1152 * Update the kernel/ramdisk/tags address if the boot image header
1153 * has default values, these default values come from mkbootimg when
1154 * the boot image is flashed using fastboot flash:raw
1155 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001156 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001157
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001158 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001159 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1160 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1161 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1162
1163 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1164 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1165
1166 /* Check if the addresses in the header are valid. */
1167 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1168 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1169 {
1170 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1171 return -1;
1172 }
1173
1174#ifndef DEVICE_TREE
1175 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1176 {
1177 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1178 return -1;
1179 }
1180#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001181
Shashank Mittalcd98d472011-08-02 14:29:24 -07001182 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001183 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001184 {
1185 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001186 offset = 0;
1187
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001188#if DEVICE_TREE
1189 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1190 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001191
1192 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1193 {
1194 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1195 return -1;
1196 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001197#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001198 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001199#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001200
Amol Jadib6be5c12012-11-14 13:39:51 -08001201 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001202 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001203
Shashank Mittalcd98d472011-08-02 14:29:24 -07001204 /* Read image without signature */
1205 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1206 {
1207 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1208 return -1;
1209 }
Dima Zavin214cc642009-01-26 11:16:21 -08001210
Amol Jadib6be5c12012-11-14 13:39:51 -08001211 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001212 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001213
Shashank Mittalcd98d472011-08-02 14:29:24 -07001214 offset = imagesize_actual;
1215 /* Read signature */
1216 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1217 {
1218 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001219 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001220 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001221
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001222 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001223
1224 /* Move kernel and ramdisk to correct address */
1225 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1226 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001227#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001228 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001229 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001230 {
1231 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1232 return -1;
1233 }
1234
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001235 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1236#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001237
1238 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001239 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001240 {
1241 write_device_info_flash(&device);
1242 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301243#if USE_PCOM_SECBOOT
1244 set_tamper_flag(device.is_tampered);
1245#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001246 }
1247 else
1248 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001249 offset = page_size;
1250
Amol Jadib6be5c12012-11-14 13:39:51 -08001251 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1252 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1253 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1254
1255 dprintf(INFO, "Loading boot image (%d): start\n",
1256 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001257 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001258
1259 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001260 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1261 return -1;
1262 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001263 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001264
Amol Jadib6be5c12012-11-14 13:39:51 -08001265 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001266 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1267 return -1;
1268 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001269 offset += ramdisk_actual;
1270
1271 dprintf(INFO, "Loading boot image (%d): done\n",
1272 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001273 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001274
1275 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001276 offset += second_actual;
1277 /* Second image loading not implemented. */
1278 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001279 }
1280
1281#if DEVICE_TREE
1282 if(hdr->dt_size != 0) {
1283
1284 /* Read the device tree table into buffer */
1285 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1286 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1287 return -1;
1288 }
1289
1290 table = (struct dt_table*) dt_buf;
1291
Deepa Dinamani19648b42013-09-05 17:05:55 -07001292 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001293 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1294 return -1;
1295 }
1296
Deepa Dinamani19648b42013-09-05 17:05:55 -07001297 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1298 if (!table)
1299 return -1;
1300
1301 /* Read the entire device tree table into buffer */
1302 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1303 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1304 return -1;
1305 }
1306
1307
Joel Kingaa335dc2013-06-03 16:11:08 -07001308 /* Find index of device tree within device tree table */
1309 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001310 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1311 return -1;
1312 }
1313
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001314 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001315 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001316 {
1317 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1318 return -1;
1319 }
1320
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001321 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001322 if(flash_read(ptn, offset + dt_entry.offset,
1323 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001324 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1325 return -1;
1326 }
1327 }
1328#endif
1329
Shashank Mittalcd98d472011-08-02 14:29:24 -07001330 }
David Ng183a7422009-12-07 14:55:21 -08001331continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001332
Dima Zavin214cc642009-01-26 11:16:21 -08001333 /* TODO: create/pass atags to kernel */
1334
Ajay Dudanie28a6072011-07-01 13:59:46 -07001335 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001336 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001337 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1338
1339 return 0;
1340}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001341
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001342BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001343void write_device_info_mmc(device_info *dev)
1344{
1345 struct device_info *info = (void*) info_buf;
1346 unsigned long long ptn = 0;
1347 unsigned long long size;
1348 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001349 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001350 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001351
1352 index = partition_get_index("aboot");
1353 ptn = partition_get_offset(index);
1354 if(ptn == 0)
1355 {
1356 return;
1357 }
1358
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001359 lun = partition_get_lun(index);
1360 mmc_set_lun(lun);
1361
Shashank Mittal162244e2011-08-08 19:01:25 -07001362 size = partition_get_size(index);
1363
1364 memcpy(info, dev, sizeof(device_info));
1365
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001366 blocksize = mmc_get_device_blocksize();
1367
1368 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Shashank Mittal162244e2011-08-08 19:01:25 -07001369 {
1370 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1371 return;
1372 }
1373}
1374
1375void read_device_info_mmc(device_info *dev)
1376{
1377 struct device_info *info = (void*) info_buf;
1378 unsigned long long ptn = 0;
1379 unsigned long long size;
1380 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001381 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001382
1383 index = partition_get_index("aboot");
1384 ptn = partition_get_offset(index);
1385 if(ptn == 0)
1386 {
1387 return;
1388 }
1389
1390 size = partition_get_size(index);
1391
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001392 blocksize = mmc_get_device_blocksize();
1393
1394 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Shashank Mittal162244e2011-08-08 19:01:25 -07001395 {
1396 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1397 return;
1398 }
1399
1400 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1401 {
1402 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1403 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001404 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001405 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001406
1407 write_device_info_mmc(info);
1408 }
1409 memcpy(dev, info, sizeof(device_info));
1410}
1411
1412void write_device_info_flash(device_info *dev)
1413{
1414 struct device_info *info = (void *) info_buf;
1415 struct ptentry *ptn;
1416 struct ptable *ptable;
1417
1418 ptable = flash_get_ptable();
1419 if (ptable == NULL)
1420 {
1421 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1422 return;
1423 }
1424
1425 ptn = ptable_find(ptable, "devinfo");
1426 if (ptn == NULL)
1427 {
1428 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1429 return;
1430 }
1431
1432 memcpy(info, dev, sizeof(device_info));
1433
1434 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1435 {
1436 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1437 return;
1438 }
1439}
1440
1441void read_device_info_flash(device_info *dev)
1442{
1443 struct device_info *info = (void*) info_buf;
1444 struct ptentry *ptn;
1445 struct ptable *ptable;
1446
1447 ptable = flash_get_ptable();
1448 if (ptable == NULL)
1449 {
1450 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1451 return;
1452 }
1453
1454 ptn = ptable_find(ptable, "devinfo");
1455 if (ptn == NULL)
1456 {
1457 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1458 return;
1459 }
1460
1461 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1462 {
1463 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1464 return;
1465 }
1466
1467 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1468 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001469 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1470 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001471 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001472 write_device_info_flash(info);
1473 }
1474 memcpy(dev, info, sizeof(device_info));
1475}
1476
1477void write_device_info(device_info *dev)
1478{
1479 if(target_is_emmc_boot())
1480 {
1481 write_device_info_mmc(dev);
1482 }
1483 else
1484 {
1485 write_device_info_flash(dev);
1486 }
1487}
1488
1489void read_device_info(device_info *dev)
1490{
1491 if(target_is_emmc_boot())
1492 {
1493 read_device_info_mmc(dev);
1494 }
1495 else
1496 {
1497 read_device_info_flash(dev);
1498 }
1499}
1500
1501void reset_device_info()
1502{
1503 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001504 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001505 write_device_info(&device);
1506}
1507
1508void set_device_root()
1509{
1510 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001511 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001512 write_device_info(&device);
1513}
1514
Amol Jadicb524072012-08-09 16:40:18 -07001515#if DEVICE_TREE
1516int copy_dtb(uint8_t *boot_image_start)
1517{
1518 uint32 dt_image_offset = 0;
1519 uint32_t n;
1520 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001521 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001522 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001523
1524 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1525
Amol Jadicb524072012-08-09 16:40:18 -07001526 if(hdr->dt_size != 0) {
1527
1528 /* add kernel offset */
1529 dt_image_offset += page_size;
1530 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1531 dt_image_offset += n;
1532
1533 /* add ramdisk offset */
1534 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1535 dt_image_offset += n;
1536
1537 /* add second offset */
1538 if(hdr->second_size != 0) {
1539 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1540 dt_image_offset += n;
1541 }
1542
1543 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001544 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001545
Deepa Dinamani19648b42013-09-05 17:05:55 -07001546 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001547 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1548 return -1;
1549 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001550 /* Find index of device tree within device tree table */
1551 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001552 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1553 return -1;
1554 }
1555
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001556 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001557 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001558 {
1559 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1560 return -1;
1561 }
1562
Amol Jadicb524072012-08-09 16:40:18 -07001563 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001564 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001565 boot_image_start + dt_image_offset + dt_entry.offset,
1566 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001567 } else
1568 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001569
1570 /* Everything looks fine. Return success. */
1571 return 0;
1572}
1573#endif
1574
Brian Swetland9c4c0752009-01-25 16:23:50 -08001575void cmd_boot(const char *arg, void *data, unsigned sz)
1576{
1577 unsigned kernel_actual;
1578 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001579 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001580 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001581 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001582 int ret = 0;
1583 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001584
1585 if (sz < sizeof(hdr)) {
1586 fastboot_fail("invalid bootimage header");
1587 return;
1588 }
1589
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001590 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001591
1592 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001593 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001594
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001595 if(target_is_emmc_boot() && hdr->page_size) {
1596 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001597 page_mask = page_size - 1;
1598 }
1599
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001600 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1601 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1602
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001603 /*
1604 * Update the kernel/ramdisk/tags address if the boot image header
1605 * has default values, these default values come from mkbootimg when
1606 * the boot image is flashed using fastboot flash:raw
1607 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001608 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1609 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001610
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001611 /* Get virtual addresses since the hdr saves physical addresses. */
1612 hdr->kernel_addr = VA(hdr->kernel_addr);
1613 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1614 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001615
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001616 /* Check if the addresses in the header are valid. */
1617 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1618 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1619 {
1620 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001621 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001622 }
1623
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001624 /* sz should have atleast raw boot image */
1625 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001626 fastboot_fail("incomplete bootimage");
1627 return;
1628 }
1629
Amol Jadicb524072012-08-09 16:40:18 -07001630#if DEVICE_TREE
1631 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001632 ret = copy_dtb(data);
1633
1634 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001635#else
1636 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1637 {
1638 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001639 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001640 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001641#endif
1642
1643 /* Load ramdisk & kernel */
1644 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1645 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1646
1647#if DEVICE_TREE
1648 /*
1649 * If dtb is not found look for appended DTB in the kernel.
1650 * If appended dev tree is found, update the atags with
1651 * memory address to the DTB appended location on RAM.
1652 * Else update with the atags address in the kernel header
1653 */
1654 if (!dtb_copied) {
1655 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001656 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1657 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001658 if (!dtb) {
1659 fastboot_fail("dtb not found");
1660 return;
1661 }
Amol Jadicb524072012-08-09 16:40:18 -07001662 }
1663#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001664
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001665#ifndef DEVICE_TREE
1666 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1667 {
1668 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001669 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001670 }
1671#endif
1672
Brian Swetland9c4c0752009-01-25 16:23:50 -08001673 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001674 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001675
Dima Zavin77e41f32013-03-06 16:10:43 -08001676 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001677 (const char*) hdr->cmdline, board_machtype(),
1678 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001679}
1680
Dima Zavin214cc642009-01-26 11:16:21 -08001681void cmd_erase(const char *arg, void *data, unsigned sz)
1682{
1683 struct ptentry *ptn;
1684 struct ptable *ptable;
1685
1686 ptable = flash_get_ptable();
1687 if (ptable == NULL) {
1688 fastboot_fail("partition table doesn't exist");
1689 return;
1690 }
1691
1692 ptn = ptable_find(ptable, arg);
1693 if (ptn == NULL) {
1694 fastboot_fail("unknown partition name");
1695 return;
1696 }
1697
1698 if (flash_erase(ptn)) {
1699 fastboot_fail("failed to erase partition");
1700 return;
1701 }
1702 fastboot_okay("");
1703}
1704
Bikas Gurungd48bd242010-09-04 19:54:32 -07001705
1706void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1707{
Kun Liang2f1601a2013-08-12 16:29:54 +08001708 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001709 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001710 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001711 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001712 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001713
Kinson Chikf1a43512011-07-14 11:28:39 -07001714 index = partition_get_index(arg);
1715 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001716 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001717
Kinson Chikf1a43512011-07-14 11:28:39 -07001718 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001719 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001720 return;
1721 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001722
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001723 lun = partition_get_lun(index);
1724 mmc_set_lun(lun);
1725
Oliver Wangcee448d2013-10-22 18:40:13 +08001726#if MMC_SDHCI_SUPPORT
1727 if (mmc_erase_card(ptn, size)) {
1728 fastboot_fail("failed to erase partition\n");
1729 return;
1730 }
1731#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001732 size = partition_get_size(index);
1733 if (size > DEFAULT_ERASE_SIZE)
1734 size = DEFAULT_ERASE_SIZE;
1735
neetidb4b24d62012-01-20 12:13:09 -08001736 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001737 0 in first several blocks */
1738 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001739 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001740 return;
1741 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001742#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001743 fastboot_okay("");
1744}
1745
1746
Ajay Dudani5c761132011-04-07 20:19:04 -07001747void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001748{
1749 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001750 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001751 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001752 char *token = NULL;
1753 char *pname = NULL;
1754 uint8_t lun = 0;
1755 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001756
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001757 token = strtok(arg, ":");
1758 pname = token;
1759 token = strtok(NULL, ":");
1760 if(token)
1761 {
1762 lun = atoi(token);
1763 mmc_set_lun(lun);
1764 lun_set = true;
1765 }
1766
1767 if (!strcmp(pname, "partition"))
Greg Grisco6e754772011-06-23 12:19:39 -07001768 {
1769 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001770 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001771 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001772 return;
1773 }
1774 }
Greg Grisco6e754772011-06-23 12:19:39 -07001775 else
1776 {
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001777 index = partition_get_index(pname);
Kinson Chikf1a43512011-07-14 11:28:39 -07001778 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001779 if(ptn == 0) {
1780 fastboot_fail("partition table doesn't exist");
1781 return;
1782 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001783
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001784 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
Greg Grisco6e754772011-06-23 12:19:39 -07001785 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1786 fastboot_fail("image is not a boot image");
1787 return;
1788 }
1789 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001790
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001791 if(!lun_set)
1792 {
1793 lun = partition_get_lun(index);
1794 mmc_set_lun(lun);
1795 }
1796
Kinson Chikf1a43512011-07-14 11:28:39 -07001797 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001798 if (ROUND_TO_PAGE(sz,511) > size) {
1799 fastboot_fail("size too large");
1800 return;
1801 }
1802 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1803 fastboot_fail("flash write failure");
1804 return;
1805 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001806 }
1807 fastboot_okay("");
1808 return;
1809}
1810
Ajay Dudani5c761132011-04-07 20:19:04 -07001811void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1812{
1813 unsigned int chunk;
1814 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001815 uint32_t *fill_buf = NULL;
1816 uint32_t fill_val;
1817 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001818 sparse_header_t *sparse_header;
1819 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001820 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001821 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301822 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001823 int index = INVALID_PTN;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001824 int i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001825 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001826
Kinson Chikf1a43512011-07-14 11:28:39 -07001827 index = partition_get_index(arg);
1828 ptn = partition_get_offset(index);
1829 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001830 fastboot_fail("partition table doesn't exist");
1831 return;
1832 }
1833
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301834 size = partition_get_size(index);
1835 if (ROUND_TO_PAGE(sz,511) > size) {
1836 fastboot_fail("size too large");
1837 return;
1838 }
1839
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001840 lun = partition_get_lun(index);
1841 mmc_set_lun(lun);
1842
Ajay Dudani5c761132011-04-07 20:19:04 -07001843 /* Read and skip over sparse image header */
1844 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001845 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1846 fastboot_fail("size too large");
1847 return;
1848 }
1849
Ajay Dudani5c761132011-04-07 20:19:04 -07001850 data += sparse_header->file_hdr_sz;
1851 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1852 {
1853 /* Skip the remaining bytes in a header that is longer than
1854 * we expected.
1855 */
1856 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1857 }
1858
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001859 dprintf (SPEW, "=== Sparse Image Header ===\n");
1860 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1861 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1862 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1863 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1864 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1865 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1866 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1867 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001868
1869 /* Start processing chunks */
1870 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1871 {
1872 /* Read and skip over chunk header */
1873 chunk_header = (chunk_header_t *) data;
1874 data += sizeof(chunk_header_t);
1875
1876 dprintf (SPEW, "=== Chunk Header ===\n");
1877 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1878 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1879 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1880
1881 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1882 {
1883 /* Skip the remaining bytes in a header that is longer than
1884 * we expected.
1885 */
1886 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1887 }
1888
1889 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1890 switch (chunk_header->chunk_type)
1891 {
1892 case CHUNK_TYPE_RAW:
1893 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1894 chunk_data_sz))
1895 {
1896 fastboot_fail("Bogus chunk size for chunk type Raw");
1897 return;
1898 }
1899
Ajay Dudaniab18f022011-05-12 14:39:22 -07001900 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1901 chunk_data_sz,
1902 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001903 {
1904 fastboot_fail("flash write failure");
1905 return;
1906 }
1907 total_blocks += chunk_header->chunk_sz;
1908 data += chunk_data_sz;
1909 break;
1910
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001911 case CHUNK_TYPE_FILL:
1912 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1913 sizeof(uint32_t)))
1914 {
1915 fastboot_fail("Bogus chunk size for chunk type FILL");
1916 return;
1917 }
1918
1919 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
1920 if (!fill_buf)
1921 {
1922 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
1923 return;
1924 }
1925
1926 fill_val = *(uint32_t *)data;
1927 data = (char *) data + sizeof(uint32_t);
1928 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
1929
1930 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
1931 {
1932 fill_buf[i] = fill_val;
1933 }
1934
1935 for (i = 0; i < chunk_blk_cnt; i++)
1936 {
1937 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1938 sparse_header->blk_sz,
1939 fill_buf))
1940 {
1941 fastboot_fail("flash write failure");
1942 free(fill_buf);
1943 return;
1944 }
1945
1946 total_blocks++;
1947 }
1948
1949 free(fill_buf);
1950 break;
1951
Ajay Dudani5c761132011-04-07 20:19:04 -07001952 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001953 total_blocks += chunk_header->chunk_sz;
1954 break;
1955
Ajay Dudani5c761132011-04-07 20:19:04 -07001956 case CHUNK_TYPE_CRC:
1957 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1958 {
1959 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1960 return;
1961 }
1962 total_blocks += chunk_header->chunk_sz;
1963 data += chunk_data_sz;
1964 break;
1965
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001966 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001967 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07001968 fastboot_fail("Unknown chunk type");
1969 return;
1970 }
1971 }
1972
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001973 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1974 total_blocks, sparse_header->total_blks);
1975
1976 if(total_blocks != sparse_header->total_blks)
1977 {
1978 fastboot_fail("sparse image write failure");
1979 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001980
1981 fastboot_okay("");
1982 return;
1983}
1984
1985void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1986{
1987 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001988 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1989 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001990
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001991#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001992 int ret=0;
1993 uint32 major_version=0;
1994 uint32 minor_version=0;
1995
1996 ret = scm_svc_version(&major_version,&minor_version);
1997 if(!ret)
1998 {
1999 if(major_version >= 2)
2000 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002001 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002002 {
2003 ret = encrypt_scm((uint32 **) &data, &sz);
2004 if (ret != 0) {
2005 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2006 return;
2007 }
2008
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002009 /* Protect only for SSD */
2010 if (!strcmp(arg, "ssd")) {
2011 ret = scm_protect_keystore((uint32 *) data, sz);
2012 if (ret != 0) {
2013 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2014 return;
2015 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002016 }
2017 }
2018 else
2019 {
2020 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2021 if(ret != 0)
2022 {
2023 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2024 return;
2025 }
2026 }
2027 }
2028 else
2029 {
2030 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2031 magic_number[1] == DECRYPT_MAGIC_1)
2032 {
2033 ret = decrypt_scm((uint32 **) &data, &sz);
2034 if (ret != 0) {
2035 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2036 return;
2037 }
2038 }
2039 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2040 magic_number[1] == ENCRYPT_MAGIC_1)
2041 {
2042 ret = encrypt_scm((uint32 **) &data, &sz);
2043 if (ret != 0) {
2044 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2045 return;
2046 }
2047 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002048 }
2049 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002050 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002051 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002052 dprintf(CRITICAL,"INVALID SVC Version\n");
2053 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002054 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002055#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002056
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002057 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002058 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
2059 cmd_flash_mmc_img(arg, data, sz);
2060 else
2061 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002062 return;
2063}
2064
Dima Zavin214cc642009-01-26 11:16:21 -08002065void cmd_flash(const char *arg, void *data, unsigned sz)
2066{
2067 struct ptentry *ptn;
2068 struct ptable *ptable;
2069 unsigned extra = 0;
2070
2071 ptable = flash_get_ptable();
2072 if (ptable == NULL) {
2073 fastboot_fail("partition table doesn't exist");
2074 return;
2075 }
2076
2077 ptn = ptable_find(ptable, arg);
2078 if (ptn == NULL) {
2079 fastboot_fail("unknown partition name");
2080 return;
2081 }
2082
2083 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2084 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2085 fastboot_fail("image is not a boot image");
2086 return;
2087 }
2088 }
2089
Amol Jadi5c61a952012-05-04 17:05:35 -07002090 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002091 || !strcmp(ptn->name, "userdata")
2092 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002093 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002094 || !strcmp(ptn->name, "modem"))
2095 {
2096 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07002097 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002098 else
2099 extra = 0;
2100 }
2101 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002102 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002103
2104 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2105 if (flash_write(ptn, extra, data, sz)) {
2106 fastboot_fail("flash write failure");
2107 return;
2108 }
2109 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2110 fastboot_okay("");
2111}
2112
2113void cmd_continue(const char *arg, void *data, unsigned sz)
2114{
2115 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002116 fastboot_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002117 if (target_is_emmc_boot())
2118 {
2119 boot_linux_from_mmc();
2120 }
2121 else
2122 {
2123 boot_linux_from_flash();
2124 }
Dima Zavin214cc642009-01-26 11:16:21 -08002125}
2126
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002127void cmd_reboot(const char *arg, void *data, unsigned sz)
2128{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002129 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002130 fastboot_okay("");
2131 reboot_device(0);
2132}
2133
2134void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2135{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002136 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002137 fastboot_okay("");
2138 reboot_device(FASTBOOT_MODE);
2139}
2140
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002141void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2142{
2143 dprintf(INFO, "Enabling charger screen check\n");
2144 device.charger_screen_enabled = 1;
2145 write_device_info(&device);
2146 fastboot_okay("");
2147}
2148
2149void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2150{
2151 dprintf(INFO, "Disabling charger screen check\n");
2152 device.charger_screen_enabled = 0;
2153 write_device_info(&device);
2154 fastboot_okay("");
2155}
2156
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302157void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2158{
2159 dprintf(INFO, "Selecting display panel %s\n", arg);
2160 if (arg)
2161 strlcpy(device.display_panel, arg,
2162 sizeof(device.display_panel));
2163 write_device_info(&device);
2164 fastboot_okay("");
2165}
2166
Shashank Mittal162244e2011-08-08 19:01:25 -07002167void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2168{
2169 if(!device.is_unlocked)
2170 {
2171 device.is_unlocked = 1;
2172 write_device_info(&device);
2173 }
2174 fastboot_okay("");
2175}
2176
Shashank Mittala0032282011-08-26 14:50:11 -07002177void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2178{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302179 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002180 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002181 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002182 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2183 fastboot_info(response);
2184 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002185 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302186 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2187 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002188 fastboot_okay("");
2189}
2190
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002191void cmd_preflash(const char *arg, void *data, unsigned sz)
2192{
2193 fastboot_okay("");
2194}
2195
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302196static struct fbimage logo_header = {0};
2197struct fbimage* splash_screen_flash();
2198
2199int splash_screen_check_header(struct fbimage *logo)
2200{
2201 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2202 return -1;
2203 if (logo->header.width == 0 || logo->header.height == 0)
2204 return -1;
2205 return 0;
2206}
2207
2208struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002209{
2210 struct ptentry *ptn;
2211 struct ptable *ptable;
2212 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302213 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002214
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302215
2216 ptable = flash_get_ptable();
2217 if (ptable == NULL) {
2218 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2219 return NULL;
2220 }
2221 ptn = ptable_find(ptable, "splash");
2222 if (ptn == NULL) {
2223 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2224 return NULL;
2225 }
2226
2227 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2228 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2229 return NULL;
2230 }
2231
2232 if (splash_screen_check_header(logo)) {
2233 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2234 return NULL;
2235 }
2236
2237 fb_display = fbcon_display();
2238 if (fb_display) {
2239 uint8_t *base = (uint8_t *) fb_display->base;
2240 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
2241 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002242 }
2243
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302244 if (flash_read(ptn + sizeof(logo->header), 0,
2245 base,
2246 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2247 fbcon_clear();
2248 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2249 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002250 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302251 logo->image = base;
2252 }
2253
2254 return logo;
2255}
2256
2257struct fbimage* splash_screen_mmc()
2258{
2259 int index = INVALID_PTN;
2260 unsigned long long ptn = 0;
2261 struct fbcon_config *fb_display = NULL;
2262 struct fbimage *logo = &logo_header;
2263
2264 index = partition_get_index("splash");
2265 if (index == 0) {
2266 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2267 return NULL;
2268 }
2269
2270 ptn = partition_get_offset(index);
2271 if (ptn == 0) {
2272 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2273 return NULL;
2274 }
2275
2276 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2277 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2278 return NULL;
2279 }
2280
2281 if (splash_screen_check_header(logo)) {
2282 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2283 return NULL;
2284 }
2285
2286 fb_display = fbcon_display();
2287 if (fb_display) {
2288 uint8_t *base = (uint8_t *) fb_display->base;
2289 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2290 base += LOGO_IMG_OFFSET;
2291
2292 if (mmc_read(ptn + sizeof(logo->header),
2293 base,
2294 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2295 fbcon_clear();
2296 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2297 return NULL;
2298 }
2299
2300 logo->image = base;
2301 }
2302
2303 return logo;
2304}
2305
2306
2307struct fbimage* fetch_image_from_partition()
2308{
2309 if (target_is_emmc_boot()) {
2310 return splash_screen_mmc();
2311 } else {
2312 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002313 }
2314}
2315
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002316/* Get the size from partiton name */
2317static void get_partition_size(const char *arg, char *response)
2318{
2319 uint64_t ptn = 0;
2320 uint64_t size;
2321 int index = INVALID_PTN;
2322
2323 index = partition_get_index(arg);
2324
2325 if (index == INVALID_PTN)
2326 {
2327 dprintf(CRITICAL, "Invalid partition index\n");
2328 return;
2329 }
2330
2331 ptn = partition_get_offset(index);
2332
2333 if(!ptn)
2334 {
2335 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2336 return;
2337 }
2338
2339 size = partition_get_size(index);
2340
2341 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2342 return;
2343}
2344
2345/*
2346 * Publish the partition type & size info
2347 * fastboot getvar will publish the required information.
2348 * fastboot getvar partition_size:<partition_name>: partition size in hex
2349 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2350 */
2351static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2352{
2353 uint8_t i;
2354
2355 for (i = 0; i < num_parts; i++) {
2356 get_partition_size(info[i].part_name, info[i].size_response);
2357
2358 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2359 {
2360 dprintf(CRITICAL, "partition size name truncated\n");
2361 return;
2362 }
2363 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2364 {
2365 dprintf(CRITICAL, "partition type name truncated\n");
2366 return;
2367 }
2368
2369 /* publish partition size & type info */
2370 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2371 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2372 }
2373}
2374
Amol Jadi5edf3552013-07-23 14:15:34 -07002375/* register commands and variables for fastboot */
2376void aboot_fastboot_register_commands(void)
2377{
2378 if (target_is_emmc_boot())
2379 {
2380 fastboot_register("flash:", cmd_flash_mmc);
2381 fastboot_register("erase:", cmd_erase_mmc);
2382 }
2383 else
2384 {
2385 fastboot_register("flash:", cmd_flash);
2386 fastboot_register("erase:", cmd_erase);
2387 }
2388
2389 fastboot_register("boot", cmd_boot);
2390 fastboot_register("continue", cmd_continue);
2391 fastboot_register("reboot", cmd_reboot);
2392 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2393 fastboot_register("oem unlock", cmd_oem_unlock);
2394 fastboot_register("oem device-info", cmd_oem_devinfo);
2395 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002396 fastboot_register("oem enable-charger-screen",
2397 cmd_oem_enable_charger_screen);
2398 fastboot_register("oem disable-charger-screen",
2399 cmd_oem_disable_charger_screen);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302400 fastboot_register("oem select-display-panel",
2401 cmd_oem_select_display_panel);
Amol Jadi5edf3552013-07-23 14:15:34 -07002402 /* publish variables and their values */
2403 fastboot_publish("product", TARGET(BOARD));
2404 fastboot_publish("kernel", "lk");
2405 fastboot_publish("serialno", sn_buf);
2406
2407 /*
2408 * partition info is supported only for emmc partitions
2409 * Calling this for NAND prints some error messages which
2410 * is harmless but misleading. Avoid calling this for NAND
2411 * devices.
2412 */
2413 if (target_is_emmc_boot())
2414 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2415
2416 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002417 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2418 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002419 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002420 /* Is the charger screen check enabled */
2421 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2422 device.charger_screen_enabled);
2423 fastboot_publish("charger-screen-enabled",
2424 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302425 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2426 device.display_panel);
2427 fastboot_publish("display-panel",
2428 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002429}
2430
Brian Swetland9c4c0752009-01-25 16:23:50 -08002431void aboot_init(const struct app_descriptor *app)
2432{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002433 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002434 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002435
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002436 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002437 if (target_is_emmc_boot())
2438 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002439 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002440 page_mask = page_size - 1;
2441 }
2442 else
2443 {
2444 page_size = flash_page_size();
2445 page_mask = page_size - 1;
2446 }
2447
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002448 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2449
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002450 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002451
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002452 /* Display splash screen if enabled */
2453#if DISPLAY_SPLASH_SCREEN
2454 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302455 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002456 dprintf(SPEW, "Display Init: Done\n");
2457#endif
2458
2459
Greg Griscod6250552011-06-29 14:40:23 -07002460 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002461 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002462
Dhaval Patel223ec952013-07-18 14:49:44 -07002463 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2464
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002465 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002466 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002467 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002468 dprintf(ALWAYS,"dload mode key sequence detected\n");
2469 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002470 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002471 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002472 }
2473 else
2474 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302475 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002476 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002477 }
2478 boot_into_fastboot = true;
2479 }
2480 if (!boot_into_fastboot)
2481 {
2482 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2483 boot_into_recovery = 1;
2484 if (!boot_into_recovery &&
2485 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002486 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002487 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002488 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002489 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002490 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002491 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002492
Ajay Dudani77421292010-10-27 19:34:06 -07002493 reboot_mode = check_reboot_mode();
2494 if (reboot_mode == RECOVERY_MODE) {
2495 boot_into_recovery = 1;
2496 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002497 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002498 }
2499
Pavel Nedev5d91d412013-04-29 11:34:24 +03002500 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002501 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002502 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002503 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002504 if(emmc_recovery_init())
2505 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2506 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002507 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002508 if((device.is_unlocked) || (device.is_tampered))
2509 {
2510 #ifdef TZ_TAMPER_FUSE
2511 set_tamper_fuse_cmd();
2512 #endif
2513 #if USE_PCOM_SECBOOT
2514 set_tamper_flag(device.is_tampered);
2515 #endif
2516 }
Shashank Mittala0032282011-08-26 14:50:11 -07002517 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002518 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002519 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002520 else
2521 {
2522 recovery_init();
2523 #if USE_PCOM_SECBOOT
2524 if((device.is_unlocked) || (device.is_tampered))
2525 set_tamper_flag(device.is_tampered);
2526 #endif
2527 boot_linux_from_flash();
2528 }
2529 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2530 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002531 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002532
Amol Jadi5edf3552013-07-23 14:15:34 -07002533 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002534
Amol Jadi5edf3552013-07-23 14:15:34 -07002535 /* register aboot specific fastboot commands */
2536 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002537
Amol Jadi5edf3552013-07-23 14:15:34 -07002538 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002539 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002540
2541 /* initialize and start fastboot */
2542 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002543}
2544
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002545uint32_t get_page_size()
2546{
2547 return page_size;
2548}
2549
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002550/*
2551 * Calculated and save hash (SHA256) for non-signed boot image.
2552 *
2553 * Hash the same data that is checked on the signed boot image.
2554 * Kernel and Ramdisk are already read to memory buffers.
2555 * Need to read the entire device-tree from mmc
2556 * since non-signed image only read the DT tags of the relevant platform.
2557 *
2558 * @param kernel_addr - kernel bufer
2559 * @param kernel_actual - kernel size in bytes
2560 * @param ramdisk_addr - ramdisk buffer
2561 * @param ramdisk_actual - ramdisk size
2562 * @param ptn - partition
2563 * @param dt_offset - device tree offset on mmc partition
2564 * @param dt_size
2565 *
2566 * @return int - 0 on success, negative value on failure.
2567 */
2568int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2569 void *ramdisk_addr, unsigned ramdisk_actual,
2570 unsigned long long ptn,
2571 unsigned dt_offset, unsigned dt_size)
2572{
2573 SHA256_CTX sha256_ctx;
2574 char digest[32]={0};
2575 char *buf = (char *)target_get_scratch_address();
2576 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2577 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2578
2579 SHA256_Init(&sha256_ctx);
2580
2581 /* Read Boot Header */
2582 if (mmc_read(ptn, buf, page_size))
2583 {
2584 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2585 return -1;
2586 }
2587 /* Read entire Device Tree */
2588 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2589 {
2590 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2591 return -1;
2592 }
2593 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2594 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2595 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2596 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2597
2598 SHA256_Final(digest, &sha256_ctx);
2599
2600 save_kernel_hash_cmd(digest);
2601 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2602
2603 return 0;
2604}
2605
Brian Swetland9c4c0752009-01-25 16:23:50 -08002606APP_START(aboot)
2607 .init = aboot_init,
2608APP_END