blob: 56872e70c7dc8a714331912c28a9d212cf446ae9 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Kuogee Hsieh55e92c52013-12-20 08:34:10 -08005 * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080038#include <kernel/thread.h>
39#include <arch/ops.h>
40
Dima Zavin214cc642009-01-26 11:16:21 -080041#include <dev/flash.h>
42#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080043#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080044#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080045#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070046#include <target.h>
47#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070048#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070049#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070050#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070051#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070052#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030053#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070054#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070055#include <boot_device.h>
Dima Zavin214cc642009-01-26 11:16:21 -080056
Neeti Desai17379b82012-06-04 18:42:53 -070057#if DEVICE_TREE
58#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070059#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070060#endif
61
Shashank Mittalcd98d472011-08-02 14:29:24 -070062#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080063#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080064#include "bootimg.h"
65#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070066#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070067#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070068#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070069#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070070#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070071
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070072extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070073extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070074extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070075extern int get_target_boot_params(const char *cmdline, const char *part,
76 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070077
78void write_device_info_mmc(device_info *dev);
79void write_device_info_flash(device_info *dev);
80
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070081#define EXPAND(NAME) #NAME
82#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070083
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080084#ifdef MEMBASE
85#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
86#else
David Ng183a7422009-12-07 14:55:21 -080087#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080088#endif
89
Deepa Dinamani0e163a42013-05-24 17:08:15 -070090#ifndef MEMSIZE
91#define MEMSIZE 1024*1024
92#endif
93
94#define MAX_TAGS_SIZE 1024
95
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080096#define RECOVERY_MODE 0x77665502
97#define FASTBOOT_MODE 0x77665500
98
Kun Liang2f1601a2013-08-12 16:29:54 +080099/* make 4096 as default size to ensure EFS,EXT4's erasing */
100#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800101#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800102
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800103#define UBI_MAGIC "UBI#"
104#define UBI_MAGIC_SIZE 0x04
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700105#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800106
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800107#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
108
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700109#if UFS_SUPPORT
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700110static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700111#else
David Ng183a7422009-12-07 14:55:21 -0800112static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700113#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800114static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300115static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800116static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800117static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700118static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300119static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800120
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800121static const char *baseband_apq = " androidboot.baseband=apq";
122static const char *baseband_msm = " androidboot.baseband=msm";
123static const char *baseband_csfb = " androidboot.baseband=csfb";
124static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700125static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800126static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700127static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800128static const char *baseband_dsda = " androidboot.baseband=dsda";
129static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800130static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800131static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800132
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700133static unsigned page_size = 0;
134static unsigned page_mask = 0;
135static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
136static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700137static char target_boot_params[64];
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700138
Shashank Mittalcd98d472011-08-02 14:29:24 -0700139/* Assuming unauthorized kernel image by default */
140static int auth_kernel_img = 0;
141
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530142static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700143
Dima Zavin42168f22009-01-30 11:52:22 -0800144struct atag_ptbl_entry
145{
146 char name[16];
147 unsigned offset;
148 unsigned size;
149 unsigned flags;
150};
151
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700152/*
153 * Partition info, required to be published
154 * for fastboot
155 */
156struct getvar_partition_info {
157 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
158 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
159 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
160 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
161 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
162};
163
164/*
165 * Right now, we are publishing the info for only
166 * three partitions
167 */
168struct getvar_partition_info part_info[] =
169{
170 { "system" , "partition-size:", "partition-type:", "", "ext4" },
171 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
172 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
173};
174
175char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700176char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800177char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700178char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530179char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700180
Greg Griscod2471ef2011-07-14 13:00:42 -0700181extern int emmc_recovery_init(void);
182
Kinson Chik0b1c8162011-08-31 16:31:57 -0700183#if NO_KEYPAD_DRIVER
184extern int fastboot_trigger(void);
185#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700186
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800187static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700188{
189 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700190#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800191 if (is_arm64)
192 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
193 else
194 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700195 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
196 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700197#endif
198}
199
Dima Zavin42168f22009-01-30 11:52:22 -0800200static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
201{
202 struct atag_ptbl_entry atag_ptn;
203
204 memcpy(atag_ptn.name, ptn->name, 16);
205 atag_ptn.name[15] = '\0';
206 atag_ptn.offset = ptn->start;
207 atag_ptn.size = ptn->length;
208 atag_ptn.flags = ptn->flags;
209 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
210 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
211}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800212
Neeti Desaie245d492012-06-01 12:52:13 -0700213unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800214{
David Ng183a7422009-12-07 14:55:21 -0800215 int cmdline_len = 0;
216 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800217 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700218 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800219 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300220 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700221 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700222 char *boot_dev_buf = NULL;
Dima Zavin42168f22009-01-30 11:52:22 -0800223
Brian Swetland9c4c0752009-01-25 16:23:50 -0800224 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800225 cmdline_len = strlen(cmdline);
226 have_cmdline = 1;
227 }
228 if (target_is_emmc_boot()) {
229 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700230#if UFS_SUPPORT
231 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
232 ASSERT(boot_dev_buf);
233 platform_boot_dev_cmdline(boot_dev_buf);
234 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700235#endif
David Ng183a7422009-12-07 14:55:21 -0800236 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800237
238 cmdline_len += strlen(usb_sn_cmdline);
239 cmdline_len += strlen(sn_buf);
240
Pavel Nedev5614d222013-06-17 18:01:02 +0300241 if (boot_into_recovery && gpt_exists)
242 cmdline_len += strlen(secondary_gpt_enable);
243
Pavel Nedev328ac822013-04-05 15:25:11 +0300244 if (boot_into_ffbm) {
245 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700246 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800247 /* reduce kernel console messages to speed-up boot */
248 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700249 } else if (device.charger_screen_enabled &&
250 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700251 pause_at_bootup = 1;
252 cmdline_len += strlen(battchg_pause);
253 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800254
Shashank Mittalcd98d472011-08-02 14:29:24 -0700255 if(target_use_signed_kernel() && auth_kernel_img) {
256 cmdline_len += strlen(auth_kernel);
257 }
258
Joonwoo Park61112782013-10-02 19:50:39 -0700259 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
260 "system",
261 target_boot_params,
262 sizeof(target_boot_params)) == 0) {
263 have_target_boot_params = 1;
264 cmdline_len += strlen(target_boot_params);
265 }
266
Ajay Dudanid04110c2011-01-17 23:55:07 -0800267 /* Determine correct androidboot.baseband to use */
268 switch(target_baseband())
269 {
270 case BASEBAND_APQ:
271 cmdline_len += strlen(baseband_apq);
272 break;
273
274 case BASEBAND_MSM:
275 cmdline_len += strlen(baseband_msm);
276 break;
277
278 case BASEBAND_CSFB:
279 cmdline_len += strlen(baseband_csfb);
280 break;
281
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800282 case BASEBAND_SVLTE2A:
283 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800284 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700285
286 case BASEBAND_MDM:
287 cmdline_len += strlen(baseband_mdm);
288 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700289
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800290 case BASEBAND_MDM2:
291 cmdline_len += strlen(baseband_mdm2);
292 break;
293
Amol Jadi5c61a952012-05-04 17:05:35 -0700294 case BASEBAND_SGLTE:
295 cmdline_len += strlen(baseband_sglte);
296 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530297
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800298 case BASEBAND_SGLTE2:
299 cmdline_len += strlen(baseband_sglte2);
300 break;
301
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530302 case BASEBAND_DSDA:
303 cmdline_len += strlen(baseband_dsda);
304 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800305
306 case BASEBAND_DSDA2:
307 cmdline_len += strlen(baseband_dsda2);
308 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800309 }
310
Ajay Singh Parmar430bbb32014-03-18 15:26:30 -0700311 if (target_display_panel_node(device.display_panel,
312 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
313 strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700314 cmdline_len += strlen(display_panel_buf);
315 }
316
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800317 if (target_warm_boot()) {
318 warm_boot = true;
319 cmdline_len += strlen(warmboot_cmdline);
320 }
321
David Ng183a7422009-12-07 14:55:21 -0800322 if (cmdline_len > 0) {
323 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700324 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
325 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700326
Amol Jadi168b7712012-03-06 16:15:00 -0800327 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700328 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800329 if (have_cmdline) {
330 src = cmdline;
331 while ((*dst++ = *src++));
332 }
333 if (target_is_emmc_boot()) {
334 src = emmc_cmdline;
335 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700336 have_cmdline = 1;
337 while ((*dst++ = *src++));
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700338#if UFS_SUPPORT
339 src = boot_dev_buf;
340 if (have_cmdline) --dst;
341 while ((*dst++ = *src++));
342#endif
David Ngf773dde2010-07-26 19:55:08 -0700343 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800344
345 src = usb_sn_cmdline;
346 if (have_cmdline) --dst;
347 have_cmdline = 1;
348 while ((*dst++ = *src++));
349 src = sn_buf;
350 if (have_cmdline) --dst;
351 have_cmdline = 1;
352 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800353 if (warm_boot) {
354 if (have_cmdline) --dst;
355 src = warmboot_cmdline;
356 while ((*dst++ = *src++));
357 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800358
Pavel Nedev5614d222013-06-17 18:01:02 +0300359 if (boot_into_recovery && gpt_exists) {
360 src = secondary_gpt_enable;
361 if (have_cmdline) --dst;
362 while ((*dst++ = *src++));
363 }
364
Pavel Nedev328ac822013-04-05 15:25:11 +0300365 if (boot_into_ffbm) {
366 src = androidboot_mode;
367 if (have_cmdline) --dst;
368 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700369 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300370 if (have_cmdline) --dst;
371 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800372 src = loglevel;
373 if (have_cmdline) --dst;
374 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300375 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700376 src = battchg_pause;
377 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800378 while ((*dst++ = *src++));
379 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800380
Shashank Mittalcd98d472011-08-02 14:29:24 -0700381 if(target_use_signed_kernel() && auth_kernel_img) {
382 src = auth_kernel;
383 if (have_cmdline) --dst;
384 while ((*dst++ = *src++));
385 }
386
Ajay Dudanid04110c2011-01-17 23:55:07 -0800387 switch(target_baseband())
388 {
389 case BASEBAND_APQ:
390 src = baseband_apq;
391 if (have_cmdline) --dst;
392 while ((*dst++ = *src++));
393 break;
394
395 case BASEBAND_MSM:
396 src = baseband_msm;
397 if (have_cmdline) --dst;
398 while ((*dst++ = *src++));
399 break;
400
401 case BASEBAND_CSFB:
402 src = baseband_csfb;
403 if (have_cmdline) --dst;
404 while ((*dst++ = *src++));
405 break;
406
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800407 case BASEBAND_SVLTE2A:
408 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800409 if (have_cmdline) --dst;
410 while ((*dst++ = *src++));
411 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700412
413 case BASEBAND_MDM:
414 src = baseband_mdm;
415 if (have_cmdline) --dst;
416 while ((*dst++ = *src++));
417 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700418
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800419 case BASEBAND_MDM2:
420 src = baseband_mdm2;
421 if (have_cmdline) --dst;
422 while ((*dst++ = *src++));
423 break;
424
Amol Jadi5c61a952012-05-04 17:05:35 -0700425 case BASEBAND_SGLTE:
426 src = baseband_sglte;
427 if (have_cmdline) --dst;
428 while ((*dst++ = *src++));
429 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530430
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800431 case BASEBAND_SGLTE2:
432 src = baseband_sglte2;
433 if (have_cmdline) --dst;
434 while ((*dst++ = *src++));
435 break;
436
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530437 case BASEBAND_DSDA:
438 src = baseband_dsda;
439 if (have_cmdline) --dst;
440 while ((*dst++ = *src++));
441 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800442
443 case BASEBAND_DSDA2:
444 src = baseband_dsda2;
445 if (have_cmdline) --dst;
446 while ((*dst++ = *src++));
447 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800448 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700449
450 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700451 src = display_panel_buf;
452 if (have_cmdline) --dst;
453 while ((*dst++ = *src++));
454 }
Joonwoo Park61112782013-10-02 19:50:39 -0700455
456 if (have_target_boot_params) {
457 if (have_cmdline) --dst;
458 src = target_boot_params;
459 while ((*dst++ = *src++));
460 }
Neeti Desaie245d492012-06-01 12:52:13 -0700461 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700462
463
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700464 if (boot_dev_buf)
465 free(boot_dev_buf);
466
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800467 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700468 return cmdline_final;
469}
470
471unsigned *atag_core(unsigned *ptr)
472{
473 /* CORE */
474 *ptr++ = 2;
475 *ptr++ = 0x54410001;
476
477 return ptr;
478
479}
480
481unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
482 unsigned ramdisk_size)
483{
484 if (ramdisk_size) {
485 *ptr++ = 4;
486 *ptr++ = 0x54420005;
487 *ptr++ = (unsigned)ramdisk;
488 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800489 }
490
Neeti Desaie245d492012-06-01 12:52:13 -0700491 return ptr;
492}
493
494unsigned *atag_ptable(unsigned **ptr_addr)
495{
496 int i;
497 struct ptable *ptable;
498
499 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700500 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
501 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700502 *(*ptr_addr)++ = 0x4d534d70;
503 for (i = 0; i < ptable->count; ++i)
504 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
505 }
506
507 return (*ptr_addr);
508}
509
510unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
511{
512 int cmdline_length = 0;
513 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700514 char *dest;
515
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800516 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700517 n = (cmdline_length + 4) & (~3);
518
519 *ptr++ = (n / 4) + 2;
520 *ptr++ = 0x54410009;
521 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800522 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700523 ptr += (n / 4);
524
525 return ptr;
526}
527
528unsigned *atag_end(unsigned *ptr)
529{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800530 /* END */
531 *ptr++ = 0;
532 *ptr++ = 0;
533
Neeti Desaie245d492012-06-01 12:52:13 -0700534 return ptr;
535}
536
537void generate_atags(unsigned *ptr, const char *cmdline,
538 void *ramdisk, unsigned ramdisk_size)
539{
540
541 ptr = atag_core(ptr);
542 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
543 ptr = target_atag_mem(ptr);
544
545 /* Skip NAND partition ATAGS for eMMC boot */
546 if (!target_is_emmc_boot()){
547 ptr = atag_ptable(&ptr);
548 }
549
550 ptr = atag_cmdline(ptr, cmdline);
551 ptr = atag_end(ptr);
552}
553
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700554typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700555void boot_linux(void *kernel, unsigned *tags,
556 const char *cmdline, unsigned machtype,
557 void *ramdisk, unsigned ramdisk_size)
558{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800559 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800560#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700561 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800562#endif
563
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700564 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800565 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800566 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800567
568 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700569
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800570 final_cmdline = update_cmdline((const char*)cmdline);
571
Neeti Desai17379b82012-06-04 18:42:53 -0700572#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800573 dprintf(INFO, "Updating device tree: start\n");
574
Neeti Desai17379b82012-06-04 18:42:53 -0700575 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800576 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700577 if(ret)
578 {
579 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
580 ASSERT(0);
581 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800582 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700583#else
Neeti Desaie245d492012-06-01 12:52:13 -0700584 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800585 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700586#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700587
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700588 /* Perform target specific cleanup */
589 target_uninit();
590
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800591 /* Turn off splash screen if enabled */
592#if DISPLAY_SPLASH_SCREEN
593 target_display_shutdown();
594#endif
595
596
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800597 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
598 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800599
600 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700601
Amol Jadi4421e652011-06-16 15:00:48 -0700602 /* do any platform specific cleanup before kernel entry */
603 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700604
Brian Swetland9c4c0752009-01-25 16:23:50 -0800605 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700606
Amol Jadi504f9fe2012-08-16 13:56:48 -0700607#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800608 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700609#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700610 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800611
612 if (IS_ARM64(kptr))
613 /* Jump to a 64bit kernel */
614 scm_elexec_call((paddr_t)kernel, tags_phys);
615 else
616 /* Jump to a 32bit kernel */
617 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800618}
619
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700620/* Function to check if the memory address range falls within the aboot
621 * boundaries.
622 * start: Start of the memory region
623 * size: Size of the memory region
624 */
625int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
626{
627 /* Check for boundary conditions. */
628 if ((start + size) < start)
629 return -1;
630
631 /* Check for memory overlap. */
632 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
633 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700634 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700635 return 0;
636 else
637 return -1;
638}
639
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800640#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800641
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800642BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800643#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800644BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800645#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800646
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700647static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
648{
649 int ret;
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800650#if IMAGE_VERIF_ALGO_SHA1
651 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
652#else
653 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
654#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700655
656 /* Assume device is rooted at this time. */
657 device.is_tampered = 1;
658
659 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
660
661 ret = image_verify((unsigned char *)bootimg_addr,
662 (unsigned char *)(bootimg_addr + bootimg_size),
663 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800664 auth_algo);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700665
666 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
667
668 if (ret)
669 {
670 /* Authorized kernel */
671 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700672 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700673 }
674
675#if USE_PCOM_SECBOOT
676 set_tamper_flag(device.is_tampered);
677#endif
678
679 if(device.is_tampered)
680 {
681 write_device_info_mmc(&device);
682 #ifdef TZ_TAMPER_FUSE
683 set_tamper_fuse_cmd();
684 #endif
685 #ifdef ASSERT_ON_TAMPER
686 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
687 ASSERT(0);
688 #endif
689 }
690}
691
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530692static bool check_format_bit()
693{
694 bool ret = false;
695 int index;
696 uint64_t offset;
697 struct boot_selection_info *in = NULL;
698 char *buf = NULL;
699
700 index = partition_get_index("bootselect");
701 if (index == INVALID_PTN)
702 {
703 dprintf(INFO, "Unable to locate /bootselect partition\n");
704 return ret;
705 }
706 offset = partition_get_offset(index);
707 if(!offset)
708 {
709 dprintf(INFO, "partition /bootselect doesn't exist\n");
710 return ret;
711 }
712 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
713 ASSERT(buf);
714 if (mmc_read(offset, (unsigned int *)buf, page_size))
715 {
716 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
717 free(buf);
718 return ret;
719 }
720 in = (struct boot_selection_info *) buf;
721 if ((in->signature == BOOTSELECT_SIGNATURE) &&
722 (in->version == BOOTSELECT_VERSION)) {
723 if ((in->state_info & BOOTSELECT_FORMAT) &&
724 !(in->state_info & BOOTSELECT_FACTORY))
725 ret = true;
726 } else {
727 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
728 in->signature, in->version);
729 ASSERT(0);
730 }
731 free(buf);
732 return ret;
733}
734
Shashank Mittal23b8f422010-04-16 19:27:21 -0700735int boot_linux_from_mmc(void)
736{
737 struct boot_img_hdr *hdr = (void*) buf;
738 struct boot_img_hdr *uhdr;
739 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700740 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700741 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700742 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700743
Shashank Mittalcd98d472011-08-02 14:29:24 -0700744 unsigned char *image_addr = 0;
745 unsigned kernel_actual;
746 unsigned ramdisk_actual;
747 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700748 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700749
750#if DEVICE_TREE
751 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700752 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700753 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800754 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700755 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700756#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800757 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
758 struct kernel64_hdr *kptr = (void*) kbuf;
759
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530760 if (check_format_bit())
761 boot_into_recovery = 1;
762
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700763 if (!boot_into_recovery) {
764 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
765 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
766 if (rcode <= 0) {
767 boot_into_ffbm = false;
768 if (rcode < 0)
769 dprintf(CRITICAL,"failed to get ffbm cookie");
770 } else
771 boot_into_ffbm = true;
772 } else
773 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700774 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
775 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
776 dprintf(INFO, "Unified boot method!\n");
777 hdr = uhdr;
778 goto unified_boot;
779 }
Greg Griscod6250552011-06-29 14:40:23 -0700780 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700781 index = partition_get_index("boot");
782 ptn = partition_get_offset(index);
783 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700784 dprintf(CRITICAL, "ERROR: No boot partition found\n");
785 return -1;
786 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700787 }
788 else {
789 index = partition_get_index("recovery");
790 ptn = partition_get_offset(index);
791 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700792 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
793 return -1;
794 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700795 }
796
Greg Griscod6250552011-06-29 14:40:23 -0700797 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700798 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
799 return -1;
800 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700801
802 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700803 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700804 return -1;
805 }
806
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700807 if (hdr->page_size && (hdr->page_size != page_size)) {
808 page_size = hdr->page_size;
809 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700810 }
811
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800812 /* Read the next page to get kernel Image header
813 * which lives in the second page for arm64 targets.
814 */
815
816 if (mmc_read(ptn + page_size, (unsigned int *) kbuf, page_size)) {
817 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
818 return -1;
819 }
820
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700821 /*
822 * Update the kernel/ramdisk/tags address if the boot image header
823 * has default values, these default values come from mkbootimg when
824 * the boot image is flashed using fastboot flash:raw
825 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800826 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700827
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700828 /* Get virtual addresses since the hdr saves physical addresses. */
829 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
830 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
831 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700832
833 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
834 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
835
836 /* Check if the addresses in the header are valid. */
837 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
838 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
839 {
840 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
841 return -1;
842 }
843
844#ifndef DEVICE_TREE
845 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
846 {
847 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
848 return -1;
849 }
850#endif
851
Shashank Mittalcd98d472011-08-02 14:29:24 -0700852 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300853 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
854 (int) target_use_signed_kernel(),
855 device.is_unlocked,
856 device.is_tampered);
857
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700858 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800859 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700860 offset = 0;
861
Shashank Mittalcd98d472011-08-02 14:29:24 -0700862 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700863
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800864#if DEVICE_TREE
865 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
866 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700867
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700868 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700869 {
870 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
871 return -1;
872 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800873#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530874 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700875
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700876#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700877
Amol Jadib6be5c12012-11-14 13:39:51 -0800878 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700879 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800880
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700881 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
882 {
883 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
884 return -1;
885 }
886
Shashank Mittalcd98d472011-08-02 14:29:24 -0700887 /* Read image without signature */
888 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
889 {
890 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
891 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800892 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700893
Amol Jadib6be5c12012-11-14 13:39:51 -0800894 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700895 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800896
Shashank Mittalcd98d472011-08-02 14:29:24 -0700897 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700898
899 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
900 {
901 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
902 return -1;
903 }
904
Shashank Mittalcd98d472011-08-02 14:29:24 -0700905 /* Read signature */
906 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
907 {
908 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700909 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700910 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800911
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700912 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700913
Neeti Desai465491e2012-07-31 12:53:35 -0700914 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700915 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
916 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700917
Neeti Desai465491e2012-07-31 12:53:35 -0700918 #if DEVICE_TREE
919 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700920 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700921 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700922
Deepa Dinamani19648b42013-09-05 17:05:55 -0700923 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700924 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
925 return -1;
926 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700927
Neeti Desai465491e2012-07-31 12:53:35 -0700928 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700929 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700930 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
931 return -1;
932 }
933
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700934 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700935 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700936 {
937 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
938 return -1;
939 }
940
Joel Kingaa335dc2013-06-03 16:11:08 -0700941 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800942 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800943 /*
944 * If appended dev tree is found, update the atags with
945 * memory address to the DTB appended location on RAM.
946 * Else update with the atags address in the kernel header
947 */
948 void *dtb;
949 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700950 hdr->kernel_size,
951 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800952 if (!dtb) {
953 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
954 return -1;
955 }
Neeti Desai465491e2012-07-31 12:53:35 -0700956 }
957 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700958 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700959 else
960 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800961 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700962
Amol Jadib6be5c12012-11-14 13:39:51 -0800963 dprintf(INFO, "Loading boot image (%d): start\n",
964 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700965 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800966
967 offset = page_size;
968
969 /* Load kernel */
970 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700971 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
972 return -1;
973 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800974 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700975
Amol Jadib6be5c12012-11-14 13:39:51 -0800976 /* Load ramdisk */
977 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700978 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800979 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700980 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
981 return -1;
982 }
983 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800984 offset += ramdisk_actual;
985
986 dprintf(INFO, "Loading boot image (%d): done\n",
987 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700988 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700989
990 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800991 offset += second_actual;
992 /* Second image loading not implemented. */
993 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700994 }
995
996 #if DEVICE_TREE
997 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -0700998 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -0700999 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
1000 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1001 return -1;
1002 }
1003 table = (struct dt_table*) dt_buf;
1004
Deepa Dinamani19648b42013-09-05 17:05:55 -07001005 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001006 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1007 return -1;
1008 }
1009
Deepa Dinamani19648b42013-09-05 17:05:55 -07001010 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1011 if (!table)
1012 return -1;
1013
1014 /* Read the entire device tree table into buffer */
1015 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
1016 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1017 return -1;
1018 }
1019
Joel Kingaa335dc2013-06-03 16:11:08 -07001020 /* Find index of device tree within device tree table */
1021 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001022 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1023 return -1;
1024 }
1025
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001026 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001027 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001028 {
1029 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1030 return -1;
1031 }
1032
Joel Kingaa335dc2013-06-03 16:11:08 -07001033 if(mmc_read(ptn + offset + dt_entry.offset,
1034 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -07001035 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1036 return -1;
1037 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +03001038 #ifdef TZ_SAVE_KERNEL_HASH
1039 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
1040 hdr->ramdisk_addr, ramdisk_actual,
1041 ptn, offset, hdr->dt_size);
1042 #endif /* TZ_SAVE_KERNEL_HASH */
1043
Channagoud Kadabi35095622013-03-01 13:53:05 -08001044 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -08001045 /*
1046 * If appended dev tree is found, update the atags with
1047 * memory address to the DTB appended location on RAM.
1048 * Else update with the atags address in the kernel header
1049 */
1050 void *dtb;
1051 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001052 kernel_actual,
1053 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001054 if (!dtb) {
1055 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1056 return -1;
1057 }
Neeti Desai465491e2012-07-31 12:53:35 -07001058 }
1059 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001060 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001061
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001062 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1063 target_load_ssd_keystore();
1064
Shashank Mittal23b8f422010-04-16 19:27:21 -07001065unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001066
Dima Zavin77e41f32013-03-06 16:10:43 -08001067 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001068 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001069 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1070
1071 return 0;
1072}
1073
Dima Zavin214cc642009-01-26 11:16:21 -08001074int boot_linux_from_flash(void)
1075{
1076 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001077 struct ptentry *ptn;
1078 struct ptable *ptable;
1079 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001080
Shashank Mittalcd98d472011-08-02 14:29:24 -07001081 unsigned char *image_addr = 0;
1082 unsigned kernel_actual;
1083 unsigned ramdisk_actual;
1084 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001085 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001086
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001087#if DEVICE_TREE
1088 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001089 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001090 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001091 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001092#endif
1093
David Ng183a7422009-12-07 14:55:21 -08001094 if (target_is_emmc_boot()) {
1095 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1096 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1097 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1098 return -1;
1099 }
1100 goto continue_boot;
1101 }
1102
Dima Zavin214cc642009-01-26 11:16:21 -08001103 ptable = flash_get_ptable();
1104 if (ptable == NULL) {
1105 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1106 return -1;
1107 }
1108
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001109 if(!boot_into_recovery)
1110 {
1111 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001112
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001113 if (ptn == NULL) {
1114 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1115 return -1;
1116 }
1117 }
1118 else
1119 {
1120 ptn = ptable_find(ptable, "recovery");
1121 if (ptn == NULL) {
1122 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1123 return -1;
1124 }
Dima Zavin214cc642009-01-26 11:16:21 -08001125 }
1126
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001127 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001128 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1129 return -1;
1130 }
Dima Zavin214cc642009-01-26 11:16:21 -08001131
1132 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001133 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001134 return -1;
1135 }
1136
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001137 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001138 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 -08001139 return -1;
1140 }
1141
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001142 /*
1143 * Update the kernel/ramdisk/tags address if the boot image header
1144 * has default values, these default values come from mkbootimg when
1145 * the boot image is flashed using fastboot flash:raw
1146 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001147 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001148
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001149 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001150 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1151 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1152 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1153
1154 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1155 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1156
1157 /* Check if the addresses in the header are valid. */
1158 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1159 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1160 {
1161 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1162 return -1;
1163 }
1164
1165#ifndef DEVICE_TREE
1166 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1167 {
1168 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1169 return -1;
1170 }
1171#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001172
Shashank Mittalcd98d472011-08-02 14:29:24 -07001173 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001174 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001175 {
1176 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001177 offset = 0;
1178
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001179#if DEVICE_TREE
1180 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1181 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001182
1183 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1184 {
1185 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1186 return -1;
1187 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001188#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001189 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001190#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001191
Amol Jadib6be5c12012-11-14 13:39:51 -08001192 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001193 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001194
Shashank Mittalcd98d472011-08-02 14:29:24 -07001195 /* Read image without signature */
1196 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1197 {
1198 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1199 return -1;
1200 }
Dima Zavin214cc642009-01-26 11:16:21 -08001201
Amol Jadib6be5c12012-11-14 13:39:51 -08001202 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001203 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001204
Shashank Mittalcd98d472011-08-02 14:29:24 -07001205 offset = imagesize_actual;
1206 /* Read signature */
1207 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1208 {
1209 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001210 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001211 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001212
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001213 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001214
1215 /* Move kernel and ramdisk to correct address */
1216 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1217 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001218#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001219 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001220 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001221 {
1222 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1223 return -1;
1224 }
1225
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001226 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1227#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001228
1229 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001230 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001231 {
1232 write_device_info_flash(&device);
1233 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301234#if USE_PCOM_SECBOOT
1235 set_tamper_flag(device.is_tampered);
1236#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001237 }
1238 else
1239 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001240 offset = page_size;
1241
Amol Jadib6be5c12012-11-14 13:39:51 -08001242 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1243 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1244 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1245
1246 dprintf(INFO, "Loading boot image (%d): start\n",
1247 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001248 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001249
1250 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001251 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1252 return -1;
1253 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001254 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001255
Amol Jadib6be5c12012-11-14 13:39:51 -08001256 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001257 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1258 return -1;
1259 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001260 offset += ramdisk_actual;
1261
1262 dprintf(INFO, "Loading boot image (%d): done\n",
1263 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001264 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001265
1266 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001267 offset += second_actual;
1268 /* Second image loading not implemented. */
1269 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001270 }
1271
1272#if DEVICE_TREE
1273 if(hdr->dt_size != 0) {
1274
1275 /* Read the device tree table into buffer */
1276 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1277 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1278 return -1;
1279 }
1280
1281 table = (struct dt_table*) dt_buf;
1282
Deepa Dinamani19648b42013-09-05 17:05:55 -07001283 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001284 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1285 return -1;
1286 }
1287
Deepa Dinamani19648b42013-09-05 17:05:55 -07001288 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1289 if (!table)
1290 return -1;
1291
1292 /* Read the entire device tree table into buffer */
1293 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1294 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1295 return -1;
1296 }
1297
1298
Joel Kingaa335dc2013-06-03 16:11:08 -07001299 /* Find index of device tree within device tree table */
1300 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001301 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1302 return -1;
1303 }
1304
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001305 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001306 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001307 {
1308 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1309 return -1;
1310 }
1311
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001312 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001313 if(flash_read(ptn, offset + dt_entry.offset,
1314 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001315 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1316 return -1;
1317 }
1318 }
1319#endif
1320
Shashank Mittalcd98d472011-08-02 14:29:24 -07001321 }
David Ng183a7422009-12-07 14:55:21 -08001322continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001323
Dima Zavin214cc642009-01-26 11:16:21 -08001324 /* TODO: create/pass atags to kernel */
1325
Ajay Dudanie28a6072011-07-01 13:59:46 -07001326 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001327 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001328 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1329
1330 return 0;
1331}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001332
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001333BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001334void write_device_info_mmc(device_info *dev)
1335{
1336 struct device_info *info = (void*) info_buf;
1337 unsigned long long ptn = 0;
1338 unsigned long long size;
1339 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001340 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001341 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001342
1343 index = partition_get_index("aboot");
1344 ptn = partition_get_offset(index);
1345 if(ptn == 0)
1346 {
1347 return;
1348 }
1349
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001350 lun = partition_get_lun(index);
1351 mmc_set_lun(lun);
1352
Shashank Mittal162244e2011-08-08 19:01:25 -07001353 size = partition_get_size(index);
1354
1355 memcpy(info, dev, sizeof(device_info));
1356
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001357 blocksize = mmc_get_device_blocksize();
1358
1359 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Shashank Mittal162244e2011-08-08 19:01:25 -07001360 {
1361 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1362 return;
1363 }
1364}
1365
1366void read_device_info_mmc(device_info *dev)
1367{
1368 struct device_info *info = (void*) info_buf;
1369 unsigned long long ptn = 0;
1370 unsigned long long size;
1371 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001372 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001373
1374 index = partition_get_index("aboot");
1375 ptn = partition_get_offset(index);
1376 if(ptn == 0)
1377 {
1378 return;
1379 }
1380
1381 size = partition_get_size(index);
1382
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001383 blocksize = mmc_get_device_blocksize();
1384
1385 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Shashank Mittal162244e2011-08-08 19:01:25 -07001386 {
1387 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1388 return;
1389 }
1390
1391 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1392 {
1393 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1394 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001395 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001396 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001397
1398 write_device_info_mmc(info);
1399 }
1400 memcpy(dev, info, sizeof(device_info));
1401}
1402
1403void write_device_info_flash(device_info *dev)
1404{
1405 struct device_info *info = (void *) info_buf;
1406 struct ptentry *ptn;
1407 struct ptable *ptable;
1408
1409 ptable = flash_get_ptable();
1410 if (ptable == NULL)
1411 {
1412 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1413 return;
1414 }
1415
1416 ptn = ptable_find(ptable, "devinfo");
1417 if (ptn == NULL)
1418 {
1419 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1420 return;
1421 }
1422
1423 memcpy(info, dev, sizeof(device_info));
1424
1425 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1426 {
1427 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1428 return;
1429 }
1430}
1431
1432void read_device_info_flash(device_info *dev)
1433{
1434 struct device_info *info = (void*) info_buf;
1435 struct ptentry *ptn;
1436 struct ptable *ptable;
1437
1438 ptable = flash_get_ptable();
1439 if (ptable == NULL)
1440 {
1441 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1442 return;
1443 }
1444
1445 ptn = ptable_find(ptable, "devinfo");
1446 if (ptn == NULL)
1447 {
1448 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1449 return;
1450 }
1451
1452 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1453 {
1454 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1455 return;
1456 }
1457
1458 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1459 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001460 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1461 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001462 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001463 write_device_info_flash(info);
1464 }
1465 memcpy(dev, info, sizeof(device_info));
1466}
1467
1468void write_device_info(device_info *dev)
1469{
1470 if(target_is_emmc_boot())
1471 {
1472 write_device_info_mmc(dev);
1473 }
1474 else
1475 {
1476 write_device_info_flash(dev);
1477 }
1478}
1479
1480void read_device_info(device_info *dev)
1481{
1482 if(target_is_emmc_boot())
1483 {
1484 read_device_info_mmc(dev);
1485 }
1486 else
1487 {
1488 read_device_info_flash(dev);
1489 }
1490}
1491
1492void reset_device_info()
1493{
1494 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001495 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001496 write_device_info(&device);
1497}
1498
1499void set_device_root()
1500{
1501 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001502 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001503 write_device_info(&device);
1504}
1505
Amol Jadicb524072012-08-09 16:40:18 -07001506#if DEVICE_TREE
1507int copy_dtb(uint8_t *boot_image_start)
1508{
1509 uint32 dt_image_offset = 0;
1510 uint32_t n;
1511 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001512 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001513 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001514
1515 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1516
Amol Jadicb524072012-08-09 16:40:18 -07001517 if(hdr->dt_size != 0) {
1518
1519 /* add kernel offset */
1520 dt_image_offset += page_size;
1521 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1522 dt_image_offset += n;
1523
1524 /* add ramdisk offset */
1525 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1526 dt_image_offset += n;
1527
1528 /* add second offset */
1529 if(hdr->second_size != 0) {
1530 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1531 dt_image_offset += n;
1532 }
1533
1534 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001535 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001536
Deepa Dinamani19648b42013-09-05 17:05:55 -07001537 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001538 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1539 return -1;
1540 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001541 /* Find index of device tree within device tree table */
1542 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001543 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1544 return -1;
1545 }
1546
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001547 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001548 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001549 {
1550 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1551 return -1;
1552 }
1553
Amol Jadicb524072012-08-09 16:40:18 -07001554 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001555 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001556 boot_image_start + dt_image_offset + dt_entry.offset,
1557 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001558 } else
1559 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001560
1561 /* Everything looks fine. Return success. */
1562 return 0;
1563}
1564#endif
1565
Brian Swetland9c4c0752009-01-25 16:23:50 -08001566void cmd_boot(const char *arg, void *data, unsigned sz)
1567{
1568 unsigned kernel_actual;
1569 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001570 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001571 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001572 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001573 int ret = 0;
1574 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001575
1576 if (sz < sizeof(hdr)) {
1577 fastboot_fail("invalid bootimage header");
1578 return;
1579 }
1580
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001581 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001582
1583 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001584 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001585
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001586 if(target_is_emmc_boot() && hdr->page_size) {
1587 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001588 page_mask = page_size - 1;
1589 }
1590
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001591 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1592 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1593
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001594 /*
1595 * Update the kernel/ramdisk/tags address if the boot image header
1596 * has default values, these default values come from mkbootimg when
1597 * the boot image is flashed using fastboot flash:raw
1598 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001599 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1600 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001601
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001602 /* Get virtual addresses since the hdr saves physical addresses. */
1603 hdr->kernel_addr = VA(hdr->kernel_addr);
1604 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1605 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001606
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001607 /* Check if the addresses in the header are valid. */
1608 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1609 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1610 {
1611 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001612 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001613 }
1614
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001615 /* sz should have atleast raw boot image */
1616 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001617 fastboot_fail("incomplete bootimage");
1618 return;
1619 }
1620
Amol Jadicb524072012-08-09 16:40:18 -07001621#if DEVICE_TREE
1622 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001623 ret = copy_dtb(data);
1624
1625 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001626#else
1627 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1628 {
1629 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001630 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001631 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001632#endif
1633
1634 /* Load ramdisk & kernel */
1635 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1636 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1637
1638#if DEVICE_TREE
1639 /*
1640 * If dtb is not found look for appended DTB in the kernel.
1641 * If appended dev tree is found, update the atags with
1642 * memory address to the DTB appended location on RAM.
1643 * Else update with the atags address in the kernel header
1644 */
1645 if (!dtb_copied) {
1646 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001647 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1648 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001649 if (!dtb) {
1650 fastboot_fail("dtb not found");
1651 return;
1652 }
Amol Jadicb524072012-08-09 16:40:18 -07001653 }
1654#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001655
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001656#ifndef DEVICE_TREE
1657 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1658 {
1659 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001660 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001661 }
1662#endif
1663
Brian Swetland9c4c0752009-01-25 16:23:50 -08001664 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001665 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001666
Dima Zavin77e41f32013-03-06 16:10:43 -08001667 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001668 (const char*) hdr->cmdline, board_machtype(),
1669 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001670}
1671
Dima Zavin214cc642009-01-26 11:16:21 -08001672void cmd_erase(const char *arg, void *data, unsigned sz)
1673{
1674 struct ptentry *ptn;
1675 struct ptable *ptable;
1676
1677 ptable = flash_get_ptable();
1678 if (ptable == NULL) {
1679 fastboot_fail("partition table doesn't exist");
1680 return;
1681 }
1682
1683 ptn = ptable_find(ptable, arg);
1684 if (ptn == NULL) {
1685 fastboot_fail("unknown partition name");
1686 return;
1687 }
1688
1689 if (flash_erase(ptn)) {
1690 fastboot_fail("failed to erase partition");
1691 return;
1692 }
1693 fastboot_okay("");
1694}
1695
Bikas Gurungd48bd242010-09-04 19:54:32 -07001696
1697void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1698{
Kun Liang2f1601a2013-08-12 16:29:54 +08001699 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001700 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001701 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001702 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001703 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001704
Kinson Chikf1a43512011-07-14 11:28:39 -07001705 index = partition_get_index(arg);
1706 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001707 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001708
Kinson Chikf1a43512011-07-14 11:28:39 -07001709 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001710 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001711 return;
1712 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001713
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001714 lun = partition_get_lun(index);
1715 mmc_set_lun(lun);
1716
Oliver Wangcee448d2013-10-22 18:40:13 +08001717#if MMC_SDHCI_SUPPORT
1718 if (mmc_erase_card(ptn, size)) {
1719 fastboot_fail("failed to erase partition\n");
1720 return;
1721 }
1722#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001723 size = partition_get_size(index);
1724 if (size > DEFAULT_ERASE_SIZE)
1725 size = DEFAULT_ERASE_SIZE;
1726
neetidb4b24d62012-01-20 12:13:09 -08001727 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001728 0 in first several blocks */
1729 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001730 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001731 return;
1732 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001733#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001734 fastboot_okay("");
1735}
1736
1737
Ajay Dudani5c761132011-04-07 20:19:04 -07001738void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001739{
1740 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001741 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001742 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001743 char *token = NULL;
1744 char *pname = NULL;
1745 uint8_t lun = 0;
1746 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001747
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001748 token = strtok(arg, ":");
1749 pname = token;
1750 token = strtok(NULL, ":");
1751 if(token)
1752 {
1753 lun = atoi(token);
1754 mmc_set_lun(lun);
1755 lun_set = true;
1756 }
1757
1758 if (!strcmp(pname, "partition"))
Greg Grisco6e754772011-06-23 12:19:39 -07001759 {
1760 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001761 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001762 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001763 return;
1764 }
1765 }
Greg Grisco6e754772011-06-23 12:19:39 -07001766 else
1767 {
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001768 index = partition_get_index(pname);
Kinson Chikf1a43512011-07-14 11:28:39 -07001769 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001770 if(ptn == 0) {
1771 fastboot_fail("partition table doesn't exist");
1772 return;
1773 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001774
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001775 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
Greg Grisco6e754772011-06-23 12:19:39 -07001776 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1777 fastboot_fail("image is not a boot image");
1778 return;
1779 }
1780 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001781
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001782 if(!lun_set)
1783 {
1784 lun = partition_get_lun(index);
1785 mmc_set_lun(lun);
1786 }
1787
Kinson Chikf1a43512011-07-14 11:28:39 -07001788 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001789 if (ROUND_TO_PAGE(sz,511) > size) {
1790 fastboot_fail("size too large");
1791 return;
1792 }
1793 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1794 fastboot_fail("flash write failure");
1795 return;
1796 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001797 }
1798 fastboot_okay("");
1799 return;
1800}
1801
Ajay Dudani5c761132011-04-07 20:19:04 -07001802void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1803{
1804 unsigned int chunk;
1805 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001806 uint32_t *fill_buf = NULL;
1807 uint32_t fill_val;
1808 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001809 sparse_header_t *sparse_header;
1810 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001811 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001812 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301813 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001814 int index = INVALID_PTN;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001815 int i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001816 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001817
Kinson Chikf1a43512011-07-14 11:28:39 -07001818 index = partition_get_index(arg);
1819 ptn = partition_get_offset(index);
1820 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001821 fastboot_fail("partition table doesn't exist");
1822 return;
1823 }
1824
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301825 size = partition_get_size(index);
1826 if (ROUND_TO_PAGE(sz,511) > size) {
1827 fastboot_fail("size too large");
1828 return;
1829 }
1830
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001831 lun = partition_get_lun(index);
1832 mmc_set_lun(lun);
1833
Ajay Dudani5c761132011-04-07 20:19:04 -07001834 /* Read and skip over sparse image header */
1835 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001836 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1837 fastboot_fail("size too large");
1838 return;
1839 }
1840
Ajay Dudani5c761132011-04-07 20:19:04 -07001841 data += sparse_header->file_hdr_sz;
1842 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1843 {
1844 /* Skip the remaining bytes in a header that is longer than
1845 * we expected.
1846 */
1847 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1848 }
1849
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001850 dprintf (SPEW, "=== Sparse Image Header ===\n");
1851 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1852 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1853 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1854 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1855 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1856 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1857 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1858 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001859
1860 /* Start processing chunks */
1861 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1862 {
1863 /* Read and skip over chunk header */
1864 chunk_header = (chunk_header_t *) data;
1865 data += sizeof(chunk_header_t);
1866
1867 dprintf (SPEW, "=== Chunk Header ===\n");
1868 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1869 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1870 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1871
1872 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1873 {
1874 /* Skip the remaining bytes in a header that is longer than
1875 * we expected.
1876 */
1877 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1878 }
1879
1880 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1881 switch (chunk_header->chunk_type)
1882 {
1883 case CHUNK_TYPE_RAW:
1884 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1885 chunk_data_sz))
1886 {
1887 fastboot_fail("Bogus chunk size for chunk type Raw");
1888 return;
1889 }
1890
Ajay Dudaniab18f022011-05-12 14:39:22 -07001891 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1892 chunk_data_sz,
1893 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001894 {
1895 fastboot_fail("flash write failure");
1896 return;
1897 }
1898 total_blocks += chunk_header->chunk_sz;
1899 data += chunk_data_sz;
1900 break;
1901
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001902 case CHUNK_TYPE_FILL:
1903 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1904 sizeof(uint32_t)))
1905 {
1906 fastboot_fail("Bogus chunk size for chunk type FILL");
1907 return;
1908 }
1909
1910 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
1911 if (!fill_buf)
1912 {
1913 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
1914 return;
1915 }
1916
1917 fill_val = *(uint32_t *)data;
1918 data = (char *) data + sizeof(uint32_t);
1919 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
1920
1921 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
1922 {
1923 fill_buf[i] = fill_val;
1924 }
1925
1926 for (i = 0; i < chunk_blk_cnt; i++)
1927 {
1928 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1929 sparse_header->blk_sz,
1930 fill_buf))
1931 {
1932 fastboot_fail("flash write failure");
1933 free(fill_buf);
1934 return;
1935 }
1936
1937 total_blocks++;
1938 }
1939
1940 free(fill_buf);
1941 break;
1942
Ajay Dudani5c761132011-04-07 20:19:04 -07001943 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001944 total_blocks += chunk_header->chunk_sz;
1945 break;
1946
Ajay Dudani5c761132011-04-07 20:19:04 -07001947 case CHUNK_TYPE_CRC:
1948 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1949 {
1950 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1951 return;
1952 }
1953 total_blocks += chunk_header->chunk_sz;
1954 data += chunk_data_sz;
1955 break;
1956
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001957 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001958 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07001959 fastboot_fail("Unknown chunk type");
1960 return;
1961 }
1962 }
1963
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001964 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1965 total_blocks, sparse_header->total_blks);
1966
1967 if(total_blocks != sparse_header->total_blks)
1968 {
1969 fastboot_fail("sparse image write failure");
1970 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001971
1972 fastboot_okay("");
1973 return;
1974}
1975
1976void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1977{
1978 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001979 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1980 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001981
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001982#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001983 int ret=0;
1984 uint32 major_version=0;
1985 uint32 minor_version=0;
1986
1987 ret = scm_svc_version(&major_version,&minor_version);
1988 if(!ret)
1989 {
1990 if(major_version >= 2)
1991 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001992 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001993 {
1994 ret = encrypt_scm((uint32 **) &data, &sz);
1995 if (ret != 0) {
1996 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1997 return;
1998 }
1999
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002000 /* Protect only for SSD */
2001 if (!strcmp(arg, "ssd")) {
2002 ret = scm_protect_keystore((uint32 *) data, sz);
2003 if (ret != 0) {
2004 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2005 return;
2006 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002007 }
2008 }
2009 else
2010 {
2011 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2012 if(ret != 0)
2013 {
2014 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2015 return;
2016 }
2017 }
2018 }
2019 else
2020 {
2021 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2022 magic_number[1] == DECRYPT_MAGIC_1)
2023 {
2024 ret = decrypt_scm((uint32 **) &data, &sz);
2025 if (ret != 0) {
2026 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2027 return;
2028 }
2029 }
2030 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2031 magic_number[1] == ENCRYPT_MAGIC_1)
2032 {
2033 ret = encrypt_scm((uint32 **) &data, &sz);
2034 if (ret != 0) {
2035 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2036 return;
2037 }
2038 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002039 }
2040 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002041 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002042 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002043 dprintf(CRITICAL,"INVALID SVC Version\n");
2044 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002045 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002046#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002047
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002048 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002049 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
2050 cmd_flash_mmc_img(arg, data, sz);
2051 else
2052 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002053 return;
2054}
2055
Dima Zavin214cc642009-01-26 11:16:21 -08002056void cmd_flash(const char *arg, void *data, unsigned sz)
2057{
2058 struct ptentry *ptn;
2059 struct ptable *ptable;
2060 unsigned extra = 0;
2061
2062 ptable = flash_get_ptable();
2063 if (ptable == NULL) {
2064 fastboot_fail("partition table doesn't exist");
2065 return;
2066 }
2067
2068 ptn = ptable_find(ptable, arg);
2069 if (ptn == NULL) {
2070 fastboot_fail("unknown partition name");
2071 return;
2072 }
2073
2074 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2075 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2076 fastboot_fail("image is not a boot image");
2077 return;
2078 }
2079 }
2080
Amol Jadi5c61a952012-05-04 17:05:35 -07002081 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002082 || !strcmp(ptn->name, "userdata")
2083 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002084 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002085 || !strcmp(ptn->name, "modem"))
2086 {
2087 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07002088 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002089 else
2090 extra = 0;
2091 }
2092 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002093 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002094
2095 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2096 if (flash_write(ptn, extra, data, sz)) {
2097 fastboot_fail("flash write failure");
2098 return;
2099 }
2100 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2101 fastboot_okay("");
2102}
2103
2104void cmd_continue(const char *arg, void *data, unsigned sz)
2105{
2106 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002107 fastboot_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002108 if (target_is_emmc_boot())
2109 {
2110 boot_linux_from_mmc();
2111 }
2112 else
2113 {
2114 boot_linux_from_flash();
2115 }
Dima Zavin214cc642009-01-26 11:16:21 -08002116}
2117
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002118void cmd_reboot(const char *arg, void *data, unsigned sz)
2119{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002120 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002121 fastboot_okay("");
2122 reboot_device(0);
2123}
2124
2125void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2126{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002127 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002128 fastboot_okay("");
2129 reboot_device(FASTBOOT_MODE);
2130}
2131
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002132void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2133{
2134 dprintf(INFO, "Enabling charger screen check\n");
2135 device.charger_screen_enabled = 1;
2136 write_device_info(&device);
2137 fastboot_okay("");
2138}
2139
2140void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2141{
2142 dprintf(INFO, "Disabling charger screen check\n");
2143 device.charger_screen_enabled = 0;
2144 write_device_info(&device);
2145 fastboot_okay("");
2146}
2147
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302148void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2149{
2150 dprintf(INFO, "Selecting display panel %s\n", arg);
2151 if (arg)
2152 strlcpy(device.display_panel, arg,
2153 sizeof(device.display_panel));
2154 write_device_info(&device);
2155 fastboot_okay("");
2156}
2157
Shashank Mittal162244e2011-08-08 19:01:25 -07002158void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2159{
2160 if(!device.is_unlocked)
2161 {
2162 device.is_unlocked = 1;
2163 write_device_info(&device);
2164 }
2165 fastboot_okay("");
2166}
2167
Shashank Mittala0032282011-08-26 14:50:11 -07002168void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2169{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302170 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002171 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002172 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002173 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2174 fastboot_info(response);
2175 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002176 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302177 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2178 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002179 fastboot_okay("");
2180}
2181
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002182void cmd_preflash(const char *arg, void *data, unsigned sz)
2183{
2184 fastboot_okay("");
2185}
2186
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302187static struct fbimage logo_header = {0};
2188struct fbimage* splash_screen_flash();
2189
2190int splash_screen_check_header(struct fbimage *logo)
2191{
2192 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2193 return -1;
2194 if (logo->header.width == 0 || logo->header.height == 0)
2195 return -1;
2196 return 0;
2197}
2198
2199struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002200{
2201 struct ptentry *ptn;
2202 struct ptable *ptable;
2203 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302204 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002205
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302206
2207 ptable = flash_get_ptable();
2208 if (ptable == NULL) {
2209 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2210 return NULL;
2211 }
2212 ptn = ptable_find(ptable, "splash");
2213 if (ptn == NULL) {
2214 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2215 return NULL;
2216 }
2217
2218 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2219 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2220 return NULL;
2221 }
2222
2223 if (splash_screen_check_header(logo)) {
2224 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2225 return NULL;
2226 }
2227
2228 fb_display = fbcon_display();
2229 if (fb_display) {
2230 uint8_t *base = (uint8_t *) fb_display->base;
2231 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
2232 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002233 }
2234
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302235 if (flash_read(ptn + sizeof(logo->header), 0,
2236 base,
2237 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2238 fbcon_clear();
2239 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2240 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002241 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302242 logo->image = base;
2243 }
2244
2245 return logo;
2246}
2247
2248struct fbimage* splash_screen_mmc()
2249{
2250 int index = INVALID_PTN;
2251 unsigned long long ptn = 0;
2252 struct fbcon_config *fb_display = NULL;
2253 struct fbimage *logo = &logo_header;
2254
2255 index = partition_get_index("splash");
2256 if (index == 0) {
2257 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2258 return NULL;
2259 }
2260
2261 ptn = partition_get_offset(index);
2262 if (ptn == 0) {
2263 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2264 return NULL;
2265 }
2266
2267 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2268 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2269 return NULL;
2270 }
2271
2272 if (splash_screen_check_header(logo)) {
2273 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2274 return NULL;
2275 }
2276
2277 fb_display = fbcon_display();
2278 if (fb_display) {
2279 uint8_t *base = (uint8_t *) fb_display->base;
2280 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2281 base += LOGO_IMG_OFFSET;
2282
2283 if (mmc_read(ptn + sizeof(logo->header),
2284 base,
2285 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2286 fbcon_clear();
2287 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2288 return NULL;
2289 }
2290
2291 logo->image = base;
2292 }
2293
2294 return logo;
2295}
2296
2297
2298struct fbimage* fetch_image_from_partition()
2299{
2300 if (target_is_emmc_boot()) {
2301 return splash_screen_mmc();
2302 } else {
2303 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002304 }
2305}
2306
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002307/* Get the size from partiton name */
2308static void get_partition_size(const char *arg, char *response)
2309{
2310 uint64_t ptn = 0;
2311 uint64_t size;
2312 int index = INVALID_PTN;
2313
2314 index = partition_get_index(arg);
2315
2316 if (index == INVALID_PTN)
2317 {
2318 dprintf(CRITICAL, "Invalid partition index\n");
2319 return;
2320 }
2321
2322 ptn = partition_get_offset(index);
2323
2324 if(!ptn)
2325 {
2326 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2327 return;
2328 }
2329
2330 size = partition_get_size(index);
2331
2332 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2333 return;
2334}
2335
2336/*
2337 * Publish the partition type & size info
2338 * fastboot getvar will publish the required information.
2339 * fastboot getvar partition_size:<partition_name>: partition size in hex
2340 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2341 */
2342static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2343{
2344 uint8_t i;
2345
2346 for (i = 0; i < num_parts; i++) {
2347 get_partition_size(info[i].part_name, info[i].size_response);
2348
2349 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2350 {
2351 dprintf(CRITICAL, "partition size name truncated\n");
2352 return;
2353 }
2354 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2355 {
2356 dprintf(CRITICAL, "partition type name truncated\n");
2357 return;
2358 }
2359
2360 /* publish partition size & type info */
2361 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2362 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2363 }
2364}
2365
Amol Jadi5edf3552013-07-23 14:15:34 -07002366/* register commands and variables for fastboot */
2367void aboot_fastboot_register_commands(void)
2368{
2369 if (target_is_emmc_boot())
2370 {
2371 fastboot_register("flash:", cmd_flash_mmc);
2372 fastboot_register("erase:", cmd_erase_mmc);
2373 }
2374 else
2375 {
2376 fastboot_register("flash:", cmd_flash);
2377 fastboot_register("erase:", cmd_erase);
2378 }
2379
2380 fastboot_register("boot", cmd_boot);
2381 fastboot_register("continue", cmd_continue);
2382 fastboot_register("reboot", cmd_reboot);
2383 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2384 fastboot_register("oem unlock", cmd_oem_unlock);
2385 fastboot_register("oem device-info", cmd_oem_devinfo);
2386 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002387 fastboot_register("oem enable-charger-screen",
2388 cmd_oem_enable_charger_screen);
2389 fastboot_register("oem disable-charger-screen",
2390 cmd_oem_disable_charger_screen);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302391 fastboot_register("oem select-display-panel",
2392 cmd_oem_select_display_panel);
Amol Jadi5edf3552013-07-23 14:15:34 -07002393 /* publish variables and their values */
2394 fastboot_publish("product", TARGET(BOARD));
2395 fastboot_publish("kernel", "lk");
2396 fastboot_publish("serialno", sn_buf);
2397
2398 /*
2399 * partition info is supported only for emmc partitions
2400 * Calling this for NAND prints some error messages which
2401 * is harmless but misleading. Avoid calling this for NAND
2402 * devices.
2403 */
2404 if (target_is_emmc_boot())
2405 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2406
2407 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002408 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2409 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002410 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002411 /* Is the charger screen check enabled */
2412 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2413 device.charger_screen_enabled);
2414 fastboot_publish("charger-screen-enabled",
2415 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302416 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2417 device.display_panel);
2418 fastboot_publish("display-panel",
2419 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002420}
2421
Brian Swetland9c4c0752009-01-25 16:23:50 -08002422void aboot_init(const struct app_descriptor *app)
2423{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002424 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002425 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002426
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002427 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002428 if (target_is_emmc_boot())
2429 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002430 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002431 page_mask = page_size - 1;
2432 }
2433 else
2434 {
2435 page_size = flash_page_size();
2436 page_mask = page_size - 1;
2437 }
2438
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002439 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2440
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002441 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002442
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002443 /* Display splash screen if enabled */
2444#if DISPLAY_SPLASH_SCREEN
2445 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302446 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002447 dprintf(SPEW, "Display Init: Done\n");
2448#endif
2449
2450
Greg Griscod6250552011-06-29 14:40:23 -07002451 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002452 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002453
Dhaval Patel223ec952013-07-18 14:49:44 -07002454 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2455
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002456 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002457 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002458 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002459 dprintf(ALWAYS,"dload mode key sequence detected\n");
2460 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002461 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002462 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002463 }
2464 else
2465 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302466 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002467 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002468 }
2469 boot_into_fastboot = true;
2470 }
2471 if (!boot_into_fastboot)
2472 {
2473 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2474 boot_into_recovery = 1;
2475 if (!boot_into_recovery &&
2476 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002477 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002478 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002479 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002480 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002481 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002482 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002483
Ajay Dudani77421292010-10-27 19:34:06 -07002484 reboot_mode = check_reboot_mode();
2485 if (reboot_mode == RECOVERY_MODE) {
2486 boot_into_recovery = 1;
2487 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002488 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002489 }
2490
Pavel Nedev5d91d412013-04-29 11:34:24 +03002491 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002492 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002493 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002494 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002495 if(emmc_recovery_init())
2496 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2497 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002498 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002499 if((device.is_unlocked) || (device.is_tampered))
2500 {
2501 #ifdef TZ_TAMPER_FUSE
2502 set_tamper_fuse_cmd();
2503 #endif
2504 #if USE_PCOM_SECBOOT
2505 set_tamper_flag(device.is_tampered);
2506 #endif
2507 }
Shashank Mittala0032282011-08-26 14:50:11 -07002508 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002509 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002510 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002511 else
2512 {
2513 recovery_init();
2514 #if USE_PCOM_SECBOOT
2515 if((device.is_unlocked) || (device.is_tampered))
2516 set_tamper_flag(device.is_tampered);
2517 #endif
2518 boot_linux_from_flash();
2519 }
2520 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2521 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002522 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002523
Amol Jadi5edf3552013-07-23 14:15:34 -07002524 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002525
Amol Jadi5edf3552013-07-23 14:15:34 -07002526 /* register aboot specific fastboot commands */
2527 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002528
Amol Jadi5edf3552013-07-23 14:15:34 -07002529 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002530 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002531
2532 /* initialize and start fastboot */
2533 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002534}
2535
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002536uint32_t get_page_size()
2537{
2538 return page_size;
2539}
2540
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002541/*
2542 * Calculated and save hash (SHA256) for non-signed boot image.
2543 *
2544 * Hash the same data that is checked on the signed boot image.
2545 * Kernel and Ramdisk are already read to memory buffers.
2546 * Need to read the entire device-tree from mmc
2547 * since non-signed image only read the DT tags of the relevant platform.
2548 *
2549 * @param kernel_addr - kernel bufer
2550 * @param kernel_actual - kernel size in bytes
2551 * @param ramdisk_addr - ramdisk buffer
2552 * @param ramdisk_actual - ramdisk size
2553 * @param ptn - partition
2554 * @param dt_offset - device tree offset on mmc partition
2555 * @param dt_size
2556 *
2557 * @return int - 0 on success, negative value on failure.
2558 */
2559int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2560 void *ramdisk_addr, unsigned ramdisk_actual,
2561 unsigned long long ptn,
2562 unsigned dt_offset, unsigned dt_size)
2563{
2564 SHA256_CTX sha256_ctx;
2565 char digest[32]={0};
2566 char *buf = (char *)target_get_scratch_address();
2567 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2568 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2569
2570 SHA256_Init(&sha256_ctx);
2571
2572 /* Read Boot Header */
2573 if (mmc_read(ptn, buf, page_size))
2574 {
2575 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2576 return -1;
2577 }
2578 /* Read entire Device Tree */
2579 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2580 {
2581 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2582 return -1;
2583 }
2584 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2585 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2586 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2587 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2588
2589 SHA256_Final(digest, &sha256_ctx);
2590
2591 save_kernel_hash_cmd(digest);
2592 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2593
2594 return 0;
2595}
2596
Brian Swetland9c4c0752009-01-25 16:23:50 -08002597APP_START(aboot)
2598 .init = aboot_init,
2599APP_END