blob: 7b7ba60992a75b87ee6dec02fef41f3543d11632 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Kuogee Hsieh55e92c52013-12-20 08:34:10 -08005 * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
43#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080044#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080045#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080046#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070047#include <target.h>
48#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070049#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070050#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070051#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070052#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070053#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030054#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070055#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070056#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070057#include <boot_verifier.h>
Dima Zavin214cc642009-01-26 11:16:21 -080058
Neeti Desai17379b82012-06-04 18:42:53 -070059#if DEVICE_TREE
60#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070061#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070062#endif
63
Shashank Mittalcd98d472011-08-02 14:29:24 -070064#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080065#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080066#include "bootimg.h"
67#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070068#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070069#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070070#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070071#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070072#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070073
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070074extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070075extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070076extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070077extern int get_target_boot_params(const char *cmdline, const char *part,
78 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070079
80void write_device_info_mmc(device_info *dev);
81void write_device_info_flash(device_info *dev);
82
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070083#define EXPAND(NAME) #NAME
84#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070085
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080086#ifdef MEMBASE
87#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
88#else
David Ng183a7422009-12-07 14:55:21 -080089#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080090#endif
91
Deepa Dinamani0e163a42013-05-24 17:08:15 -070092#ifndef MEMSIZE
93#define MEMSIZE 1024*1024
94#endif
95
96#define MAX_TAGS_SIZE 1024
97
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080098#define RECOVERY_MODE 0x77665502
99#define FASTBOOT_MODE 0x77665500
100
Kun Liang2f1601a2013-08-12 16:29:54 +0800101/* make 4096 as default size to ensure EFS,EXT4's erasing */
102#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800103#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800104
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800105#define UBI_MAGIC "UBI#"
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700106#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800107#define UBI_MAGIC_SIZE 0x04
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700108#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800109
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800110#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
111
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700112#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
113
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700114#if UFS_SUPPORT
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700115static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700116#else
David Ng183a7422009-12-07 14:55:21 -0800117static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700118#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800119static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300120static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800121static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800122static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700123static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300124static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800125
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800126static const char *baseband_apq = " androidboot.baseband=apq";
127static const char *baseband_msm = " androidboot.baseband=msm";
128static const char *baseband_csfb = " androidboot.baseband=csfb";
129static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700130static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800131static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700132static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800133static const char *baseband_dsda = " androidboot.baseband=dsda";
134static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800135static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800136static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800137
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700138static unsigned page_size = 0;
139static unsigned page_mask = 0;
140static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
141static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700142static char target_boot_params[64];
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700143
Shashank Mittalcd98d472011-08-02 14:29:24 -0700144/* Assuming unauthorized kernel image by default */
145static int auth_kernel_img = 0;
146
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700147static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700148
Dima Zavin42168f22009-01-30 11:52:22 -0800149struct atag_ptbl_entry
150{
151 char name[16];
152 unsigned offset;
153 unsigned size;
154 unsigned flags;
155};
156
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700157/*
158 * Partition info, required to be published
159 * for fastboot
160 */
161struct getvar_partition_info {
162 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
163 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
164 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
165 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
166 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
167};
168
169/*
170 * Right now, we are publishing the info for only
171 * three partitions
172 */
173struct getvar_partition_info part_info[] =
174{
175 { "system" , "partition-size:", "partition-type:", "", "ext4" },
176 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
177 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
178};
179
180char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700181char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800182char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700183char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530184char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700185
Greg Griscod2471ef2011-07-14 13:00:42 -0700186extern int emmc_recovery_init(void);
187
Kinson Chik0b1c8162011-08-31 16:31:57 -0700188#if NO_KEYPAD_DRIVER
189extern int fastboot_trigger(void);
190#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700191
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800192static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700193{
194 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700195#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800196 if (is_arm64)
197 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
198 else
199 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700200 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
201 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700202#endif
203}
204
Dima Zavin42168f22009-01-30 11:52:22 -0800205static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
206{
207 struct atag_ptbl_entry atag_ptn;
208
209 memcpy(atag_ptn.name, ptn->name, 16);
210 atag_ptn.name[15] = '\0';
211 atag_ptn.offset = ptn->start;
212 atag_ptn.size = ptn->length;
213 atag_ptn.flags = ptn->flags;
214 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
215 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
216}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800217
Neeti Desaie245d492012-06-01 12:52:13 -0700218unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800219{
David Ng183a7422009-12-07 14:55:21 -0800220 int cmdline_len = 0;
221 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800222 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700223 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800224 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300225 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700226 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700227 char *boot_dev_buf = NULL;
Dima Zavin42168f22009-01-30 11:52:22 -0800228
Brian Swetland9c4c0752009-01-25 16:23:50 -0800229 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800230 cmdline_len = strlen(cmdline);
231 have_cmdline = 1;
232 }
233 if (target_is_emmc_boot()) {
234 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700235#if UFS_SUPPORT
236 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
237 ASSERT(boot_dev_buf);
238 platform_boot_dev_cmdline(boot_dev_buf);
239 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700240#endif
David Ng183a7422009-12-07 14:55:21 -0800241 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800242
243 cmdline_len += strlen(usb_sn_cmdline);
244 cmdline_len += strlen(sn_buf);
245
Pavel Nedev5614d222013-06-17 18:01:02 +0300246 if (boot_into_recovery && gpt_exists)
247 cmdline_len += strlen(secondary_gpt_enable);
248
Pavel Nedev328ac822013-04-05 15:25:11 +0300249 if (boot_into_ffbm) {
250 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700251 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800252 /* reduce kernel console messages to speed-up boot */
253 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700254 } else if (device.charger_screen_enabled &&
255 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700256 pause_at_bootup = 1;
257 cmdline_len += strlen(battchg_pause);
258 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800259
Shashank Mittalcd98d472011-08-02 14:29:24 -0700260 if(target_use_signed_kernel() && auth_kernel_img) {
261 cmdline_len += strlen(auth_kernel);
262 }
263
Joonwoo Park61112782013-10-02 19:50:39 -0700264 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
265 "system",
266 target_boot_params,
267 sizeof(target_boot_params)) == 0) {
268 have_target_boot_params = 1;
269 cmdline_len += strlen(target_boot_params);
270 }
271
Ajay Dudanid04110c2011-01-17 23:55:07 -0800272 /* Determine correct androidboot.baseband to use */
273 switch(target_baseband())
274 {
275 case BASEBAND_APQ:
276 cmdline_len += strlen(baseband_apq);
277 break;
278
279 case BASEBAND_MSM:
280 cmdline_len += strlen(baseband_msm);
281 break;
282
283 case BASEBAND_CSFB:
284 cmdline_len += strlen(baseband_csfb);
285 break;
286
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800287 case BASEBAND_SVLTE2A:
288 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800289 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700290
291 case BASEBAND_MDM:
292 cmdline_len += strlen(baseband_mdm);
293 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700294
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800295 case BASEBAND_MDM2:
296 cmdline_len += strlen(baseband_mdm2);
297 break;
298
Amol Jadi5c61a952012-05-04 17:05:35 -0700299 case BASEBAND_SGLTE:
300 cmdline_len += strlen(baseband_sglte);
301 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530302
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800303 case BASEBAND_SGLTE2:
304 cmdline_len += strlen(baseband_sglte2);
305 break;
306
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530307 case BASEBAND_DSDA:
308 cmdline_len += strlen(baseband_dsda);
309 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800310
311 case BASEBAND_DSDA2:
312 cmdline_len += strlen(baseband_dsda2);
313 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800314 }
315
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800316 if (cmdline) {
317 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
318 target_display_panel_node(device.display_panel,
319 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
320 strlen(display_panel_buf)) {
321 cmdline_len += strlen(display_panel_buf);
322 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700323 }
324
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800325 if (target_warm_boot()) {
326 warm_boot = true;
327 cmdline_len += strlen(warmboot_cmdline);
328 }
329
David Ng183a7422009-12-07 14:55:21 -0800330 if (cmdline_len > 0) {
331 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800332 unsigned char *dst;
333
334 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
335 ASSERT(cmdline_final != NULL);
336 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700337
Amol Jadi168b7712012-03-06 16:15:00 -0800338 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800339 if (have_cmdline) {
340 src = cmdline;
341 while ((*dst++ = *src++));
342 }
343 if (target_is_emmc_boot()) {
344 src = emmc_cmdline;
345 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700346 have_cmdline = 1;
347 while ((*dst++ = *src++));
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700348#if UFS_SUPPORT
349 src = boot_dev_buf;
350 if (have_cmdline) --dst;
351 while ((*dst++ = *src++));
352#endif
David Ngf773dde2010-07-26 19:55:08 -0700353 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800354
355 src = usb_sn_cmdline;
356 if (have_cmdline) --dst;
357 have_cmdline = 1;
358 while ((*dst++ = *src++));
359 src = sn_buf;
360 if (have_cmdline) --dst;
361 have_cmdline = 1;
362 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800363 if (warm_boot) {
364 if (have_cmdline) --dst;
365 src = warmboot_cmdline;
366 while ((*dst++ = *src++));
367 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800368
Pavel Nedev5614d222013-06-17 18:01:02 +0300369 if (boot_into_recovery && gpt_exists) {
370 src = secondary_gpt_enable;
371 if (have_cmdline) --dst;
372 while ((*dst++ = *src++));
373 }
374
Pavel Nedev328ac822013-04-05 15:25:11 +0300375 if (boot_into_ffbm) {
376 src = androidboot_mode;
377 if (have_cmdline) --dst;
378 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700379 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300380 if (have_cmdline) --dst;
381 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800382 src = loglevel;
383 if (have_cmdline) --dst;
384 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300385 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700386 src = battchg_pause;
387 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800388 while ((*dst++ = *src++));
389 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800390
Shashank Mittalcd98d472011-08-02 14:29:24 -0700391 if(target_use_signed_kernel() && auth_kernel_img) {
392 src = auth_kernel;
393 if (have_cmdline) --dst;
394 while ((*dst++ = *src++));
395 }
396
Ajay Dudanid04110c2011-01-17 23:55:07 -0800397 switch(target_baseband())
398 {
399 case BASEBAND_APQ:
400 src = baseband_apq;
401 if (have_cmdline) --dst;
402 while ((*dst++ = *src++));
403 break;
404
405 case BASEBAND_MSM:
406 src = baseband_msm;
407 if (have_cmdline) --dst;
408 while ((*dst++ = *src++));
409 break;
410
411 case BASEBAND_CSFB:
412 src = baseband_csfb;
413 if (have_cmdline) --dst;
414 while ((*dst++ = *src++));
415 break;
416
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800417 case BASEBAND_SVLTE2A:
418 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800419 if (have_cmdline) --dst;
420 while ((*dst++ = *src++));
421 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700422
423 case BASEBAND_MDM:
424 src = baseband_mdm;
425 if (have_cmdline) --dst;
426 while ((*dst++ = *src++));
427 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700428
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800429 case BASEBAND_MDM2:
430 src = baseband_mdm2;
431 if (have_cmdline) --dst;
432 while ((*dst++ = *src++));
433 break;
434
Amol Jadi5c61a952012-05-04 17:05:35 -0700435 case BASEBAND_SGLTE:
436 src = baseband_sglte;
437 if (have_cmdline) --dst;
438 while ((*dst++ = *src++));
439 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530440
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800441 case BASEBAND_SGLTE2:
442 src = baseband_sglte2;
443 if (have_cmdline) --dst;
444 while ((*dst++ = *src++));
445 break;
446
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530447 case BASEBAND_DSDA:
448 src = baseband_dsda;
449 if (have_cmdline) --dst;
450 while ((*dst++ = *src++));
451 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800452
453 case BASEBAND_DSDA2:
454 src = baseband_dsda2;
455 if (have_cmdline) --dst;
456 while ((*dst++ = *src++));
457 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800458 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700459
460 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700461 src = display_panel_buf;
462 if (have_cmdline) --dst;
463 while ((*dst++ = *src++));
464 }
Joonwoo Park61112782013-10-02 19:50:39 -0700465
466 if (have_target_boot_params) {
467 if (have_cmdline) --dst;
468 src = target_boot_params;
469 while ((*dst++ = *src++));
470 }
Neeti Desaie245d492012-06-01 12:52:13 -0700471 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700472
473
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700474 if (boot_dev_buf)
475 free(boot_dev_buf);
476
Maria Yu9ca0c402014-07-07 14:49:34 +0800477 dprintf(INFO, "cmdline: %s\n", cmdline_final ? cmdline_final : "");
Neeti Desaie245d492012-06-01 12:52:13 -0700478 return cmdline_final;
479}
480
481unsigned *atag_core(unsigned *ptr)
482{
483 /* CORE */
484 *ptr++ = 2;
485 *ptr++ = 0x54410001;
486
487 return ptr;
488
489}
490
491unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
492 unsigned ramdisk_size)
493{
494 if (ramdisk_size) {
495 *ptr++ = 4;
496 *ptr++ = 0x54420005;
497 *ptr++ = (unsigned)ramdisk;
498 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800499 }
500
Neeti Desaie245d492012-06-01 12:52:13 -0700501 return ptr;
502}
503
504unsigned *atag_ptable(unsigned **ptr_addr)
505{
506 int i;
507 struct ptable *ptable;
508
509 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700510 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
511 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700512 *(*ptr_addr)++ = 0x4d534d70;
513 for (i = 0; i < ptable->count; ++i)
514 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
515 }
516
517 return (*ptr_addr);
518}
519
520unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
521{
522 int cmdline_length = 0;
523 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700524 char *dest;
525
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800526 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700527 n = (cmdline_length + 4) & (~3);
528
529 *ptr++ = (n / 4) + 2;
530 *ptr++ = 0x54410009;
531 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800532 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700533 ptr += (n / 4);
534
535 return ptr;
536}
537
538unsigned *atag_end(unsigned *ptr)
539{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800540 /* END */
541 *ptr++ = 0;
542 *ptr++ = 0;
543
Neeti Desaie245d492012-06-01 12:52:13 -0700544 return ptr;
545}
546
547void generate_atags(unsigned *ptr, const char *cmdline,
548 void *ramdisk, unsigned ramdisk_size)
549{
550
551 ptr = atag_core(ptr);
552 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
553 ptr = target_atag_mem(ptr);
554
555 /* Skip NAND partition ATAGS for eMMC boot */
556 if (!target_is_emmc_boot()){
557 ptr = atag_ptable(&ptr);
558 }
559
560 ptr = atag_cmdline(ptr, cmdline);
561 ptr = atag_end(ptr);
562}
563
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700564typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700565void boot_linux(void *kernel, unsigned *tags,
566 const char *cmdline, unsigned machtype,
567 void *ramdisk, unsigned ramdisk_size)
568{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800569 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800570#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700571 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800572#endif
573
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700574 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800575 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800576 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800577
578 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700579
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800580 final_cmdline = update_cmdline((const char*)cmdline);
581
Neeti Desai17379b82012-06-04 18:42:53 -0700582#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800583 dprintf(INFO, "Updating device tree: start\n");
584
Neeti Desai17379b82012-06-04 18:42:53 -0700585 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800586 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700587 if(ret)
588 {
589 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
590 ASSERT(0);
591 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800592 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700593#else
Neeti Desaie245d492012-06-01 12:52:13 -0700594 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800595 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700596#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700597
Maria Yu52254c02014-07-04 16:14:54 +0800598 free(final_cmdline);
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700599 /* Perform target specific cleanup */
600 target_uninit();
601
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800602 /* Turn off splash screen if enabled */
603#if DISPLAY_SPLASH_SCREEN
604 target_display_shutdown();
605#endif
606
607
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800608 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
609 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800610
611 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700612
Amol Jadi4421e652011-06-16 15:00:48 -0700613 /* do any platform specific cleanup before kernel entry */
614 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700615
Brian Swetland9c4c0752009-01-25 16:23:50 -0800616 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700617
Amol Jadi504f9fe2012-08-16 13:56:48 -0700618#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800619 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700620#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700621 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800622
623 if (IS_ARM64(kptr))
624 /* Jump to a 64bit kernel */
625 scm_elexec_call((paddr_t)kernel, tags_phys);
626 else
627 /* Jump to a 32bit kernel */
628 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800629}
630
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700631/* Function to check if the memory address range falls within the aboot
632 * boundaries.
633 * start: Start of the memory region
634 * size: Size of the memory region
635 */
636int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
637{
638 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800639 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700640 return -1;
641
642 /* Check for memory overlap. */
643 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
644 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700645 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700646 return 0;
647 else
648 return -1;
649}
650
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800651#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800652
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800653BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800654#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800655BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800656#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800657
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700658static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
659{
660 int ret;
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800661#if IMAGE_VERIF_ALGO_SHA1
662 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
663#else
664 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
665#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700666
667 /* Assume device is rooted at this time. */
668 device.is_tampered = 1;
669
670 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
671
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700672#if VERIFIED_BOOT
673 if(boot_into_recovery)
674 {
675 ret = boot_verify_image((unsigned char *)bootimg_addr,
676 bootimg_size, "recovery");
677 }
678 else
679 {
680 ret = boot_verify_image((unsigned char *)bootimg_addr,
681 bootimg_size, "boot");
682 }
683 boot_verify_print_state();
684#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700685 ret = image_verify((unsigned char *)bootimg_addr,
686 (unsigned char *)(bootimg_addr + bootimg_size),
687 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800688 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700689#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700690 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
691
692 if (ret)
693 {
694 /* Authorized kernel */
695 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700696 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700697 }
698
699#if USE_PCOM_SECBOOT
700 set_tamper_flag(device.is_tampered);
701#endif
702
703 if(device.is_tampered)
704 {
705 write_device_info_mmc(&device);
706 #ifdef TZ_TAMPER_FUSE
707 set_tamper_fuse_cmd();
708 #endif
709 #ifdef ASSERT_ON_TAMPER
710 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
711 ASSERT(0);
712 #endif
713 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700714
715#if VERIFIED_BOOT
716 if(boot_verify_get_state() == RED)
717 {
718 if(!boot_into_recovery)
719 {
720 dprintf(CRITICAL,
721 "Device verification failed. Rebooting into recovery.\n");
722 reboot_device(RECOVERY_MODE);
723 }
724 else
725 {
726 dprintf(CRITICAL,
727 "Recovery image verification failed. Asserting..\n");
728 ASSERT(0);
729 }
730 }
731#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700732}
733
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530734static bool check_format_bit()
735{
736 bool ret = false;
737 int index;
738 uint64_t offset;
739 struct boot_selection_info *in = NULL;
740 char *buf = NULL;
741
742 index = partition_get_index("bootselect");
743 if (index == INVALID_PTN)
744 {
745 dprintf(INFO, "Unable to locate /bootselect partition\n");
746 return ret;
747 }
748 offset = partition_get_offset(index);
749 if(!offset)
750 {
751 dprintf(INFO, "partition /bootselect doesn't exist\n");
752 return ret;
753 }
754 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
755 ASSERT(buf);
756 if (mmc_read(offset, (unsigned int *)buf, page_size))
757 {
758 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
759 free(buf);
760 return ret;
761 }
762 in = (struct boot_selection_info *) buf;
763 if ((in->signature == BOOTSELECT_SIGNATURE) &&
764 (in->version == BOOTSELECT_VERSION)) {
765 if ((in->state_info & BOOTSELECT_FORMAT) &&
766 !(in->state_info & BOOTSELECT_FACTORY))
767 ret = true;
768 } else {
769 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
770 in->signature, in->version);
771 ASSERT(0);
772 }
773 free(buf);
774 return ret;
775}
776
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700777void boot_verifier_init()
778{
779
780 uint32_t boot_state;
781 /* Check if device unlock */
782 if(device.is_unlocked)
783 {
784 boot_verify_send_event(DEV_UNLOCK);
785 boot_verify_print_state();
786 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
787 return;
788 }
789 else
790 {
791 boot_verify_send_event(BOOT_INIT);
792 }
793
794 /* Initialize keystore */
795 boot_state = boot_verify_keystore_init();
796 if(boot_state == YELLOW)
797 {
798 boot_verify_print_state();
799 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
800 }
801}
802
Shashank Mittal23b8f422010-04-16 19:27:21 -0700803int boot_linux_from_mmc(void)
804{
805 struct boot_img_hdr *hdr = (void*) buf;
806 struct boot_img_hdr *uhdr;
807 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700808 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700809 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700810 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700811
Shashank Mittalcd98d472011-08-02 14:29:24 -0700812 unsigned char *image_addr = 0;
813 unsigned kernel_actual;
814 unsigned ramdisk_actual;
815 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700816 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700817
818#if DEVICE_TREE
819 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700820 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700821 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800822 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700823 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700824#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800825 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
826 struct kernel64_hdr *kptr = (void*) kbuf;
827
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530828 if (check_format_bit())
829 boot_into_recovery = 1;
830
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700831 if (!boot_into_recovery) {
832 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
833 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
834 if (rcode <= 0) {
835 boot_into_ffbm = false;
836 if (rcode < 0)
837 dprintf(CRITICAL,"failed to get ffbm cookie");
838 } else
839 boot_into_ffbm = true;
840 } else
841 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700842 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
843 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
844 dprintf(INFO, "Unified boot method!\n");
845 hdr = uhdr;
846 goto unified_boot;
847 }
Greg Griscod6250552011-06-29 14:40:23 -0700848 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700849 index = partition_get_index("boot");
850 ptn = partition_get_offset(index);
851 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700852 dprintf(CRITICAL, "ERROR: No boot partition found\n");
853 return -1;
854 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700855 }
856 else {
857 index = partition_get_index("recovery");
858 ptn = partition_get_offset(index);
859 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700860 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
861 return -1;
862 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700863 }
864
Greg Griscod6250552011-06-29 14:40:23 -0700865 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700866 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
867 return -1;
868 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700869
870 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700871 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700872 return -1;
873 }
874
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700875 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530876
877 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
878 dprintf(CRITICAL, "ERROR: Invalid page size\n");
879 return -1;
880 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700881 page_size = hdr->page_size;
882 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700883 }
884
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800885 /* Read the next page to get kernel Image header
886 * which lives in the second page for arm64 targets.
887 */
888
889 if (mmc_read(ptn + page_size, (unsigned int *) kbuf, page_size)) {
890 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
891 return -1;
892 }
893
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700894 /*
895 * Update the kernel/ramdisk/tags address if the boot image header
896 * has default values, these default values come from mkbootimg when
897 * the boot image is flashed using fastboot flash:raw
898 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800899 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700900
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700901 /* Get virtual addresses since the hdr saves physical addresses. */
902 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
903 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
904 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700905
906 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
907 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
908
909 /* Check if the addresses in the header are valid. */
910 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
911 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
912 {
913 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
914 return -1;
915 }
916
917#ifndef DEVICE_TREE
918 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
919 {
920 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
921 return -1;
922 }
923#endif
924
Shashank Mittalcd98d472011-08-02 14:29:24 -0700925 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300926 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
927 (int) target_use_signed_kernel(),
928 device.is_unlocked,
929 device.is_tampered);
930
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700931#if VERIFIED_BOOT
932 boot_verifier_init();
933#endif
934
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700935 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800936 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700937 offset = 0;
938
Shashank Mittalcd98d472011-08-02 14:29:24 -0700939 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700940
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800941#if DEVICE_TREE
942 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
943 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700944
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700945 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700946 {
947 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
948 return -1;
949 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800950#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530951 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700952
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700953#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700954
Amol Jadib6be5c12012-11-14 13:39:51 -0800955 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700956 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800957
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700958 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
959 {
960 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
961 return -1;
962 }
963
Shashank Mittalcd98d472011-08-02 14:29:24 -0700964 /* Read image without signature */
965 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
966 {
967 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
968 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800969 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700970
Amol Jadib6be5c12012-11-14 13:39:51 -0800971 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700972 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800973
Shashank Mittalcd98d472011-08-02 14:29:24 -0700974 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700975
976 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
977 {
978 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
979 return -1;
980 }
981
Shashank Mittalcd98d472011-08-02 14:29:24 -0700982 /* Read signature */
983 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
984 {
985 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700986 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700987 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800988
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700989 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700990
Neeti Desai465491e2012-07-31 12:53:35 -0700991 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700992 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
993 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700994
Neeti Desai465491e2012-07-31 12:53:35 -0700995 #if DEVICE_TREE
996 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700997 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700998 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700999
Deepa Dinamani19648b42013-09-05 17:05:55 -07001000 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001001 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1002 return -1;
1003 }
Deepa Dinamani19648b42013-09-05 17:05:55 -07001004
Neeti Desai465491e2012-07-31 12:53:35 -07001005 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -07001006 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001007 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
1008 return -1;
1009 }
1010
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001011 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001012 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001013 {
1014 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1015 return -1;
1016 }
1017
Joel Kingaa335dc2013-06-03 16:11:08 -07001018 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001019 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -08001020 /*
1021 * If appended dev tree is found, update the atags with
1022 * memory address to the DTB appended location on RAM.
1023 * Else update with the atags address in the kernel header
1024 */
1025 void *dtb;
1026 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001027 hdr->kernel_size,
1028 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001029 if (!dtb) {
1030 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1031 return -1;
1032 }
Neeti Desai465491e2012-07-31 12:53:35 -07001033 }
1034 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001035 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001036 else
1037 {
Amol Jadib6be5c12012-11-14 13:39:51 -08001038 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001039
Amol Jadib6be5c12012-11-14 13:39:51 -08001040 dprintf(INFO, "Loading boot image (%d): start\n",
1041 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001042 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001043
1044 offset = page_size;
1045
1046 /* Load kernel */
1047 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001048 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1049 return -1;
1050 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001051 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001052
Amol Jadib6be5c12012-11-14 13:39:51 -08001053 /* Load ramdisk */
1054 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -07001055 {
Amol Jadib6be5c12012-11-14 13:39:51 -08001056 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001057 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1058 return -1;
1059 }
1060 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001061 offset += ramdisk_actual;
1062
1063 dprintf(INFO, "Loading boot image (%d): done\n",
1064 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001065 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -07001066
1067 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001068 offset += second_actual;
1069 /* Second image loading not implemented. */
1070 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -07001071 }
1072
1073 #if DEVICE_TREE
1074 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -07001075 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -07001076 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
1077 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1078 return -1;
1079 }
1080 table = (struct dt_table*) dt_buf;
1081
Deepa Dinamani19648b42013-09-05 17:05:55 -07001082 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001083 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1084 return -1;
1085 }
1086
Deepa Dinamani19648b42013-09-05 17:05:55 -07001087 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1088 if (!table)
1089 return -1;
1090
1091 /* Read the entire device tree table into buffer */
1092 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
1093 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1094 return -1;
1095 }
1096
Joel Kingaa335dc2013-06-03 16:11:08 -07001097 /* Find index of device tree within device tree table */
1098 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001099 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1100 return -1;
1101 }
1102
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001103 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001104 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001105 {
1106 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1107 return -1;
1108 }
1109
Joel Kingaa335dc2013-06-03 16:11:08 -07001110 if(mmc_read(ptn + offset + dt_entry.offset,
1111 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -07001112 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1113 return -1;
1114 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +03001115 #ifdef TZ_SAVE_KERNEL_HASH
1116 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
1117 hdr->ramdisk_addr, ramdisk_actual,
1118 ptn, offset, hdr->dt_size);
1119 #endif /* TZ_SAVE_KERNEL_HASH */
1120
Channagoud Kadabi35095622013-03-01 13:53:05 -08001121 } else {
Aparna Mallavarapu33127372014-06-03 22:15:54 +05301122
1123 /* Validate the tags_addr */
1124 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1125 {
1126 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1127 return -1;
1128 }
Dima Zavin77e41f32013-03-06 16:10:43 -08001129 /*
1130 * If appended dev tree is found, update the atags with
1131 * memory address to the DTB appended location on RAM.
1132 * Else update with the atags address in the kernel header
1133 */
1134 void *dtb;
1135 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001136 kernel_actual,
1137 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001138 if (!dtb) {
1139 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1140 return -1;
1141 }
Neeti Desai465491e2012-07-31 12:53:35 -07001142 }
1143 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001144 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001145
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001146 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1147 target_load_ssd_keystore();
1148
Shashank Mittal23b8f422010-04-16 19:27:21 -07001149unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001150
Dima Zavin77e41f32013-03-06 16:10:43 -08001151 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001152 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001153 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1154
1155 return 0;
1156}
1157
Dima Zavin214cc642009-01-26 11:16:21 -08001158int boot_linux_from_flash(void)
1159{
1160 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001161 struct ptentry *ptn;
1162 struct ptable *ptable;
1163 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001164
Shashank Mittalcd98d472011-08-02 14:29:24 -07001165 unsigned char *image_addr = 0;
1166 unsigned kernel_actual;
1167 unsigned ramdisk_actual;
1168 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001169 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001170
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001171#if DEVICE_TREE
1172 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001173 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001174 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001175 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001176#endif
1177
David Ng183a7422009-12-07 14:55:21 -08001178 if (target_is_emmc_boot()) {
1179 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1180 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1181 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1182 return -1;
1183 }
1184 goto continue_boot;
1185 }
1186
Dima Zavin214cc642009-01-26 11:16:21 -08001187 ptable = flash_get_ptable();
1188 if (ptable == NULL) {
1189 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1190 return -1;
1191 }
1192
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001193 if(!boot_into_recovery)
1194 {
1195 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001196
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001197 if (ptn == NULL) {
1198 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1199 return -1;
1200 }
1201 }
1202 else
1203 {
1204 ptn = ptable_find(ptable, "recovery");
1205 if (ptn == NULL) {
1206 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1207 return -1;
1208 }
Dima Zavin214cc642009-01-26 11:16:21 -08001209 }
1210
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001211 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001212 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1213 return -1;
1214 }
Dima Zavin214cc642009-01-26 11:16:21 -08001215
1216 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001217 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001218 return -1;
1219 }
1220
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001221 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001222 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 -08001223 return -1;
1224 }
1225
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001226 /*
1227 * Update the kernel/ramdisk/tags address if the boot image header
1228 * has default values, these default values come from mkbootimg when
1229 * the boot image is flashed using fastboot flash:raw
1230 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001231 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001232
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001233 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001234 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1235 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1236 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1237
1238 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1239 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1240
1241 /* Check if the addresses in the header are valid. */
1242 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1243 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1244 {
1245 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1246 return -1;
1247 }
1248
1249#ifndef DEVICE_TREE
1250 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1251 {
1252 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1253 return -1;
1254 }
1255#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001256
Shashank Mittalcd98d472011-08-02 14:29:24 -07001257 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001258 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001259 {
1260 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001261 offset = 0;
1262
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001263#if DEVICE_TREE
1264 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1265 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001266
1267 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1268 {
1269 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1270 return -1;
1271 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001272#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001273 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001274#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001275
Amol Jadib6be5c12012-11-14 13:39:51 -08001276 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001277 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001278
Shashank Mittalcd98d472011-08-02 14:29:24 -07001279 /* Read image without signature */
1280 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1281 {
1282 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1283 return -1;
1284 }
Dima Zavin214cc642009-01-26 11:16:21 -08001285
Amol Jadib6be5c12012-11-14 13:39:51 -08001286 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001287 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001288
Shashank Mittalcd98d472011-08-02 14:29:24 -07001289 offset = imagesize_actual;
1290 /* Read signature */
1291 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1292 {
1293 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001294 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001295 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001296
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001297 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001298
1299 /* Move kernel and ramdisk to correct address */
1300 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1301 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001302#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001303 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001304 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001305 {
1306 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1307 return -1;
1308 }
1309
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001310 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1311#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001312
1313 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001314 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001315 {
1316 write_device_info_flash(&device);
1317 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301318#if USE_PCOM_SECBOOT
1319 set_tamper_flag(device.is_tampered);
1320#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001321 }
1322 else
1323 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001324 offset = page_size;
1325
Amol Jadib6be5c12012-11-14 13:39:51 -08001326 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1327 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1328 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1329
1330 dprintf(INFO, "Loading boot image (%d): start\n",
1331 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001332 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001333
1334 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001335 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1336 return -1;
1337 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001338 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001339
Amol Jadib6be5c12012-11-14 13:39:51 -08001340 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001341 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1342 return -1;
1343 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001344 offset += ramdisk_actual;
1345
1346 dprintf(INFO, "Loading boot image (%d): done\n",
1347 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001348 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001349
1350 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001351 offset += second_actual;
1352 /* Second image loading not implemented. */
1353 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001354 }
1355
1356#if DEVICE_TREE
1357 if(hdr->dt_size != 0) {
1358
1359 /* Read the device tree table into buffer */
1360 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1361 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1362 return -1;
1363 }
1364
1365 table = (struct dt_table*) dt_buf;
1366
Deepa Dinamani19648b42013-09-05 17:05:55 -07001367 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001368 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1369 return -1;
1370 }
1371
Deepa Dinamani19648b42013-09-05 17:05:55 -07001372 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1373 if (!table)
1374 return -1;
1375
1376 /* Read the entire device tree table into buffer */
1377 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1378 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1379 return -1;
1380 }
1381
1382
Joel Kingaa335dc2013-06-03 16:11:08 -07001383 /* Find index of device tree within device tree table */
1384 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001385 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1386 return -1;
1387 }
1388
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001389 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001390 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001391 {
1392 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1393 return -1;
1394 }
1395
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001396 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001397 if(flash_read(ptn, offset + dt_entry.offset,
1398 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001399 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1400 return -1;
1401 }
1402 }
1403#endif
1404
Shashank Mittalcd98d472011-08-02 14:29:24 -07001405 }
David Ng183a7422009-12-07 14:55:21 -08001406continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001407
Dima Zavin214cc642009-01-26 11:16:21 -08001408 /* TODO: create/pass atags to kernel */
1409
Ajay Dudanie28a6072011-07-01 13:59:46 -07001410 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001411 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001412 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1413
1414 return 0;
1415}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001416
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001417BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001418void write_device_info_mmc(device_info *dev)
1419{
1420 struct device_info *info = (void*) info_buf;
1421 unsigned long long ptn = 0;
1422 unsigned long long size;
1423 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001424 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001425 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001426
1427 index = partition_get_index("aboot");
1428 ptn = partition_get_offset(index);
1429 if(ptn == 0)
1430 {
1431 return;
1432 }
1433
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001434 lun = partition_get_lun(index);
1435 mmc_set_lun(lun);
1436
Shashank Mittal162244e2011-08-08 19:01:25 -07001437 size = partition_get_size(index);
1438
1439 memcpy(info, dev, sizeof(device_info));
1440
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001441 blocksize = mmc_get_device_blocksize();
1442
1443 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Shashank Mittal162244e2011-08-08 19:01:25 -07001444 {
1445 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1446 return;
1447 }
1448}
1449
1450void read_device_info_mmc(device_info *dev)
1451{
1452 struct device_info *info = (void*) info_buf;
1453 unsigned long long ptn = 0;
1454 unsigned long long size;
1455 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001456 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001457
1458 index = partition_get_index("aboot");
1459 ptn = partition_get_offset(index);
1460 if(ptn == 0)
1461 {
1462 return;
1463 }
1464
1465 size = partition_get_size(index);
1466
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001467 blocksize = mmc_get_device_blocksize();
1468
1469 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Shashank Mittal162244e2011-08-08 19:01:25 -07001470 {
1471 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1472 return;
1473 }
1474
1475 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1476 {
1477 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1478 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001479 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001480 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001481
1482 write_device_info_mmc(info);
1483 }
1484 memcpy(dev, info, sizeof(device_info));
1485}
1486
1487void write_device_info_flash(device_info *dev)
1488{
1489 struct device_info *info = (void *) info_buf;
1490 struct ptentry *ptn;
1491 struct ptable *ptable;
1492
1493 ptable = flash_get_ptable();
1494 if (ptable == NULL)
1495 {
1496 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1497 return;
1498 }
1499
1500 ptn = ptable_find(ptable, "devinfo");
1501 if (ptn == NULL)
1502 {
1503 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1504 return;
1505 }
1506
1507 memcpy(info, dev, sizeof(device_info));
1508
1509 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1510 {
1511 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1512 return;
1513 }
1514}
1515
1516void read_device_info_flash(device_info *dev)
1517{
1518 struct device_info *info = (void*) info_buf;
1519 struct ptentry *ptn;
1520 struct ptable *ptable;
1521
1522 ptable = flash_get_ptable();
1523 if (ptable == NULL)
1524 {
1525 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1526 return;
1527 }
1528
1529 ptn = ptable_find(ptable, "devinfo");
1530 if (ptn == NULL)
1531 {
1532 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1533 return;
1534 }
1535
1536 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1537 {
1538 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1539 return;
1540 }
1541
1542 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1543 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001544 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1545 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001546 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001547 write_device_info_flash(info);
1548 }
1549 memcpy(dev, info, sizeof(device_info));
1550}
1551
1552void write_device_info(device_info *dev)
1553{
1554 if(target_is_emmc_boot())
1555 {
1556 write_device_info_mmc(dev);
1557 }
1558 else
1559 {
1560 write_device_info_flash(dev);
1561 }
1562}
1563
1564void read_device_info(device_info *dev)
1565{
1566 if(target_is_emmc_boot())
1567 {
1568 read_device_info_mmc(dev);
1569 }
1570 else
1571 {
1572 read_device_info_flash(dev);
1573 }
1574}
1575
1576void reset_device_info()
1577{
1578 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001579 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001580 write_device_info(&device);
1581}
1582
1583void set_device_root()
1584{
1585 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001586 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001587 write_device_info(&device);
1588}
1589
Amol Jadicb524072012-08-09 16:40:18 -07001590#if DEVICE_TREE
1591int copy_dtb(uint8_t *boot_image_start)
1592{
1593 uint32 dt_image_offset = 0;
1594 uint32_t n;
1595 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001596 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001597 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001598
1599 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1600
Amol Jadicb524072012-08-09 16:40:18 -07001601 if(hdr->dt_size != 0) {
1602
1603 /* add kernel offset */
1604 dt_image_offset += page_size;
1605 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1606 dt_image_offset += n;
1607
1608 /* add ramdisk offset */
1609 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1610 dt_image_offset += n;
1611
1612 /* add second offset */
1613 if(hdr->second_size != 0) {
1614 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1615 dt_image_offset += n;
1616 }
1617
1618 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001619 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001620
Deepa Dinamani19648b42013-09-05 17:05:55 -07001621 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001622 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1623 return -1;
1624 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001625 /* Find index of device tree within device tree table */
1626 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001627 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1628 return -1;
1629 }
1630
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001631 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001632 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001633 {
1634 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1635 return -1;
1636 }
1637
Amol Jadicb524072012-08-09 16:40:18 -07001638 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001639 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001640 boot_image_start + dt_image_offset + dt_entry.offset,
1641 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001642 } else
1643 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001644
1645 /* Everything looks fine. Return success. */
1646 return 0;
1647}
1648#endif
1649
Brian Swetland9c4c0752009-01-25 16:23:50 -08001650void cmd_boot(const char *arg, void *data, unsigned sz)
1651{
1652 unsigned kernel_actual;
1653 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001654 uint32_t image_actual;
1655 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001656 uint32_t sig_actual = SIGNATURE_SIZE;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001657 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001658 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001659 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001660 int ret = 0;
1661 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001662
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001663#if VERIFIED_BOOT
1664 if(!device.is_unlocked)
1665 {
1666 fastboot_fail("unlock device to use this command");
1667 return;
1668 }
1669#endif
1670
Brian Swetland9c4c0752009-01-25 16:23:50 -08001671 if (sz < sizeof(hdr)) {
1672 fastboot_fail("invalid bootimage header");
1673 return;
1674 }
1675
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001676 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001677
1678 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001679 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001680
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001681 if(target_is_emmc_boot() && hdr->page_size) {
1682 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001683 page_mask = page_size - 1;
1684 }
1685
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001686 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1687 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001688#if DEVICE_TREE
1689 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1690#endif
1691
1692 image_actual = ADD_OF(page_size, kernel_actual);
1693 image_actual = ADD_OF(image_actual, ramdisk_actual);
1694 image_actual = ADD_OF(image_actual, dt_actual);
1695
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001696 if (target_use_signed_kernel() && (!device.is_unlocked))
1697 image_actual = ADD_OF(image_actual, sig_actual);
1698
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001699 /* sz should have atleast raw boot image */
1700 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001701 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001702 return;
1703 }
1704
1705 /* Verify the boot image
1706 * device & page_size are initialized in aboot_init
1707 */
1708 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001709 /* Pass size excluding signature size, otherwise we would try to
1710 * access signature beyond its length
1711 */
1712 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001713
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001714 /*
1715 * Update the kernel/ramdisk/tags address if the boot image header
1716 * has default values, these default values come from mkbootimg when
1717 * the boot image is flashed using fastboot flash:raw
1718 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001719 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1720 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001721
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001722 /* Get virtual addresses since the hdr saves physical addresses. */
1723 hdr->kernel_addr = VA(hdr->kernel_addr);
1724 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1725 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001726
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001727 /* Check if the addresses in the header are valid. */
1728 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1729 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1730 {
1731 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001732 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001733 }
1734
Amol Jadicb524072012-08-09 16:40:18 -07001735#if DEVICE_TREE
1736 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001737 ret = copy_dtb(data);
1738
1739 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001740#else
1741 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1742 {
1743 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001744 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001745 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001746#endif
1747
1748 /* Load ramdisk & kernel */
1749 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1750 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1751
1752#if DEVICE_TREE
1753 /*
1754 * If dtb is not found look for appended DTB in the kernel.
1755 * If appended dev tree is found, update the atags with
1756 * memory address to the DTB appended location on RAM.
1757 * Else update with the atags address in the kernel header
1758 */
1759 if (!dtb_copied) {
1760 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001761 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1762 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001763 if (!dtb) {
1764 fastboot_fail("dtb not found");
1765 return;
1766 }
Amol Jadicb524072012-08-09 16:40:18 -07001767 }
1768#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001769
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001770#ifndef DEVICE_TREE
1771 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1772 {
1773 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001774 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001775 }
1776#endif
1777
Brian Swetland9c4c0752009-01-25 16:23:50 -08001778 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001779 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001780
Dima Zavin77e41f32013-03-06 16:10:43 -08001781 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001782 (const char*) hdr->cmdline, board_machtype(),
1783 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001784}
1785
Dima Zavin214cc642009-01-26 11:16:21 -08001786void cmd_erase(const char *arg, void *data, unsigned sz)
1787{
1788 struct ptentry *ptn;
1789 struct ptable *ptable;
1790
1791 ptable = flash_get_ptable();
1792 if (ptable == NULL) {
1793 fastboot_fail("partition table doesn't exist");
1794 return;
1795 }
1796
1797 ptn = ptable_find(ptable, arg);
1798 if (ptn == NULL) {
1799 fastboot_fail("unknown partition name");
1800 return;
1801 }
1802
1803 if (flash_erase(ptn)) {
1804 fastboot_fail("failed to erase partition");
1805 return;
1806 }
1807 fastboot_okay("");
1808}
1809
Bikas Gurungd48bd242010-09-04 19:54:32 -07001810
1811void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1812{
Kun Liang2f1601a2013-08-12 16:29:54 +08001813 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001814 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001815 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001816 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001817 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001818
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001819#if VERIFIED_BOOT
1820 if(!strcmp(arg, KEYSTORE_PTN_NAME))
1821 {
1822 if(!device.is_unlocked)
1823 {
1824 fastboot_fail("unlock device to erase keystore");
1825 return;
1826 }
1827 }
1828#endif
1829
Kinson Chikf1a43512011-07-14 11:28:39 -07001830 index = partition_get_index(arg);
1831 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001832 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001833
Kinson Chikf1a43512011-07-14 11:28:39 -07001834 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001835 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001836 return;
1837 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001838
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001839 lun = partition_get_lun(index);
1840 mmc_set_lun(lun);
1841
Oliver Wangcee448d2013-10-22 18:40:13 +08001842#if MMC_SDHCI_SUPPORT
1843 if (mmc_erase_card(ptn, size)) {
1844 fastboot_fail("failed to erase partition\n");
1845 return;
1846 }
1847#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001848 size = partition_get_size(index);
1849 if (size > DEFAULT_ERASE_SIZE)
1850 size = DEFAULT_ERASE_SIZE;
1851
neetidb4b24d62012-01-20 12:13:09 -08001852 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001853 0 in first several blocks */
1854 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001855 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001856 return;
1857 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001858#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001859 fastboot_okay("");
1860}
1861
1862
Ajay Dudani5c761132011-04-07 20:19:04 -07001863void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001864{
1865 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001866 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001867 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001868 char *token = NULL;
1869 char *pname = NULL;
1870 uint8_t lun = 0;
1871 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001872
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001873 token = strtok(arg, ":");
1874 pname = token;
1875 token = strtok(NULL, ":");
1876 if(token)
1877 {
1878 lun = atoi(token);
1879 mmc_set_lun(lun);
1880 lun_set = true;
1881 }
1882
Mao Jinlong226f33a2014-07-04 17:24:10 +08001883 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07001884 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08001885 if (!strcmp(pname, "partition"))
1886 {
1887 dprintf(INFO, "Attempt to write partition image.\n");
1888 if (write_partition(sz, (unsigned char *) data)) {
1889 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07001890 return;
1891 }
1892 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08001893 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001894 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001895#if VERIFIED_BOOT
1896 if(!strcmp(pname, KEYSTORE_PTN_NAME))
1897 {
1898 if(!device.is_unlocked)
1899 {
1900 fastboot_fail("unlock device to flash keystore");
1901 return;
1902 }
1903 if(!boot_verify_validate_keystore((unsigned char *)data))
1904 {
1905 fastboot_fail("image is not a keystore file");
1906 return;
1907 }
1908 }
1909#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08001910 index = partition_get_index(pname);
1911 ptn = partition_get_offset(index);
1912 if(ptn == 0) {
1913 fastboot_fail("partition table doesn't exist");
1914 return;
1915 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001916
Mao Jinlong226f33a2014-07-04 17:24:10 +08001917 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
1918 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1919 fastboot_fail("image is not a boot image");
1920 return;
1921 }
1922 }
1923
1924 if(!lun_set)
1925 {
1926 lun = partition_get_lun(index);
1927 mmc_set_lun(lun);
1928 }
1929
1930 size = partition_get_size(index);
1931 if (ROUND_TO_PAGE(sz,511) > size) {
1932 fastboot_fail("size too large");
1933 return;
1934 }
1935 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1936 fastboot_fail("flash write failure");
1937 return;
1938 }
Greg Grisco6e754772011-06-23 12:19:39 -07001939 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001940 }
1941 fastboot_okay("");
1942 return;
1943}
1944
Ajay Dudani5c761132011-04-07 20:19:04 -07001945void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1946{
1947 unsigned int chunk;
1948 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001949 uint32_t *fill_buf = NULL;
1950 uint32_t fill_val;
1951 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001952 sparse_header_t *sparse_header;
1953 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001954 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001955 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301956 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001957 int index = INVALID_PTN;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001958 int i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001959 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001960
Kinson Chikf1a43512011-07-14 11:28:39 -07001961 index = partition_get_index(arg);
1962 ptn = partition_get_offset(index);
1963 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001964 fastboot_fail("partition table doesn't exist");
1965 return;
1966 }
1967
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301968 size = partition_get_size(index);
1969 if (ROUND_TO_PAGE(sz,511) > size) {
1970 fastboot_fail("size too large");
1971 return;
1972 }
1973
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001974 lun = partition_get_lun(index);
1975 mmc_set_lun(lun);
1976
Ajay Dudani5c761132011-04-07 20:19:04 -07001977 /* Read and skip over sparse image header */
1978 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001979 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1980 fastboot_fail("size too large");
1981 return;
1982 }
1983
Ajay Dudani5c761132011-04-07 20:19:04 -07001984 data += sparse_header->file_hdr_sz;
1985 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1986 {
1987 /* Skip the remaining bytes in a header that is longer than
1988 * we expected.
1989 */
1990 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1991 }
1992
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001993 dprintf (SPEW, "=== Sparse Image Header ===\n");
1994 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1995 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1996 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1997 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1998 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1999 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2000 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2001 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002002
2003 /* Start processing chunks */
2004 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2005 {
2006 /* Read and skip over chunk header */
2007 chunk_header = (chunk_header_t *) data;
2008 data += sizeof(chunk_header_t);
2009
2010 dprintf (SPEW, "=== Chunk Header ===\n");
2011 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2012 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2013 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2014
2015 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
2016 {
2017 /* Skip the remaining bytes in a header that is longer than
2018 * we expected.
2019 */
2020 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
2021 }
2022
2023 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
2024 switch (chunk_header->chunk_type)
2025 {
2026 case CHUNK_TYPE_RAW:
2027 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2028 chunk_data_sz))
2029 {
2030 fastboot_fail("Bogus chunk size for chunk type Raw");
2031 return;
2032 }
2033
Ajay Dudaniab18f022011-05-12 14:39:22 -07002034 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2035 chunk_data_sz,
2036 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002037 {
2038 fastboot_fail("flash write failure");
2039 return;
2040 }
2041 total_blocks += chunk_header->chunk_sz;
2042 data += chunk_data_sz;
2043 break;
2044
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002045 case CHUNK_TYPE_FILL:
2046 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2047 sizeof(uint32_t)))
2048 {
2049 fastboot_fail("Bogus chunk size for chunk type FILL");
2050 return;
2051 }
2052
2053 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2054 if (!fill_buf)
2055 {
2056 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2057 return;
2058 }
2059
2060 fill_val = *(uint32_t *)data;
2061 data = (char *) data + sizeof(uint32_t);
2062 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
2063
2064 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2065 {
2066 fill_buf[i] = fill_val;
2067 }
2068
2069 for (i = 0; i < chunk_blk_cnt; i++)
2070 {
2071 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2072 sparse_header->blk_sz,
2073 fill_buf))
2074 {
2075 fastboot_fail("flash write failure");
2076 free(fill_buf);
2077 return;
2078 }
2079
2080 total_blocks++;
2081 }
2082
2083 free(fill_buf);
2084 break;
2085
Ajay Dudani5c761132011-04-07 20:19:04 -07002086 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002087 total_blocks += chunk_header->chunk_sz;
2088 break;
2089
Ajay Dudani5c761132011-04-07 20:19:04 -07002090 case CHUNK_TYPE_CRC:
2091 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2092 {
2093 fastboot_fail("Bogus chunk size for chunk type Dont Care");
2094 return;
2095 }
2096 total_blocks += chunk_header->chunk_sz;
2097 data += chunk_data_sz;
2098 break;
2099
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002100 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002101 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002102 fastboot_fail("Unknown chunk type");
2103 return;
2104 }
2105 }
2106
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002107 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2108 total_blocks, sparse_header->total_blks);
2109
2110 if(total_blocks != sparse_header->total_blks)
2111 {
2112 fastboot_fail("sparse image write failure");
2113 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002114
2115 fastboot_okay("");
2116 return;
2117}
2118
2119void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2120{
2121 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002122 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2123 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002124
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002125#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002126 int ret=0;
2127 uint32 major_version=0;
2128 uint32 minor_version=0;
2129
2130 ret = scm_svc_version(&major_version,&minor_version);
2131 if(!ret)
2132 {
2133 if(major_version >= 2)
2134 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002135 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002136 {
2137 ret = encrypt_scm((uint32 **) &data, &sz);
2138 if (ret != 0) {
2139 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2140 return;
2141 }
2142
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002143 /* Protect only for SSD */
2144 if (!strcmp(arg, "ssd")) {
2145 ret = scm_protect_keystore((uint32 *) data, sz);
2146 if (ret != 0) {
2147 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2148 return;
2149 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002150 }
2151 }
2152 else
2153 {
2154 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2155 if(ret != 0)
2156 {
2157 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2158 return;
2159 }
2160 }
2161 }
2162 else
2163 {
2164 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2165 magic_number[1] == DECRYPT_MAGIC_1)
2166 {
2167 ret = decrypt_scm((uint32 **) &data, &sz);
2168 if (ret != 0) {
2169 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2170 return;
2171 }
2172 }
2173 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2174 magic_number[1] == ENCRYPT_MAGIC_1)
2175 {
2176 ret = encrypt_scm((uint32 **) &data, &sz);
2177 if (ret != 0) {
2178 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2179 return;
2180 }
2181 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002182 }
2183 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002184 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002185 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002186 dprintf(CRITICAL,"INVALID SVC Version\n");
2187 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002188 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002189#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002190
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002191#if VERIFIED_BOOT
2192 if(!device.is_unlocked && !device.is_verified)
2193 {
2194 fastboot_fail("device is locked. Cannot flash images");
2195 return;
2196 }
2197 if(!device.is_unlocked && device.is_verified)
2198 {
2199 if(!boot_verify_flash_allowed(arg))
2200 {
2201 fastboot_fail("cannot flash this partition in verified state");
2202 return;
2203 }
2204 }
2205#endif
2206
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002207 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002208 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
2209 cmd_flash_mmc_img(arg, data, sz);
2210 else
2211 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002212 return;
2213}
2214
Dima Zavin214cc642009-01-26 11:16:21 -08002215void cmd_flash(const char *arg, void *data, unsigned sz)
2216{
2217 struct ptentry *ptn;
2218 struct ptable *ptable;
2219 unsigned extra = 0;
2220
2221 ptable = flash_get_ptable();
2222 if (ptable == NULL) {
2223 fastboot_fail("partition table doesn't exist");
2224 return;
2225 }
2226
2227 ptn = ptable_find(ptable, arg);
2228 if (ptn == NULL) {
2229 fastboot_fail("unknown partition name");
2230 return;
2231 }
2232
2233 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2234 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2235 fastboot_fail("image is not a boot image");
2236 return;
2237 }
2238 }
2239
Amol Jadi5c61a952012-05-04 17:05:35 -07002240 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002241 || !strcmp(ptn->name, "userdata")
2242 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002243 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002244 || !strcmp(ptn->name, "modem"))
2245 {
2246 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07002247 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002248 else
2249 extra = 0;
2250 }
2251 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002252 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002253
2254 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2255 if (flash_write(ptn, extra, data, sz)) {
2256 fastboot_fail("flash write failure");
2257 return;
2258 }
2259 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2260 fastboot_okay("");
2261}
2262
2263void cmd_continue(const char *arg, void *data, unsigned sz)
2264{
2265 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002266 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002267
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002268 if (target_is_emmc_boot())
2269 {
2270 boot_linux_from_mmc();
2271 }
2272 else
2273 {
2274 boot_linux_from_flash();
2275 }
Dima Zavin214cc642009-01-26 11:16:21 -08002276}
2277
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002278void cmd_reboot(const char *arg, void *data, unsigned sz)
2279{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002280 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002281 fastboot_okay("");
2282 reboot_device(0);
2283}
2284
2285void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2286{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002287 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002288 fastboot_okay("");
2289 reboot_device(FASTBOOT_MODE);
2290}
2291
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002292void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2293{
2294 dprintf(INFO, "Enabling charger screen check\n");
2295 device.charger_screen_enabled = 1;
2296 write_device_info(&device);
2297 fastboot_okay("");
2298}
2299
2300void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2301{
2302 dprintf(INFO, "Disabling charger screen check\n");
2303 device.charger_screen_enabled = 0;
2304 write_device_info(&device);
2305 fastboot_okay("");
2306}
2307
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302308void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2309{
2310 dprintf(INFO, "Selecting display panel %s\n", arg);
2311 if (arg)
2312 strlcpy(device.display_panel, arg,
2313 sizeof(device.display_panel));
2314 write_device_info(&device);
2315 fastboot_okay("");
2316}
2317
Shashank Mittal162244e2011-08-08 19:01:25 -07002318void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2319{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002320 /* TODO: Wipe user data */
2321 if(!device.is_unlocked || device.is_verified)
Shashank Mittal162244e2011-08-08 19:01:25 -07002322 {
2323 device.is_unlocked = 1;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002324 device.is_verified = 0;
2325 write_device_info(&device);
2326 }
2327 fastboot_okay("");
2328}
2329
2330void cmd_oem_lock(const char *arg, void *data, unsigned sz)
2331{
2332 /* TODO: Wipe user data */
2333 if(device.is_unlocked || device.is_verified)
2334 {
2335 device.is_unlocked = 0;
2336 device.is_verified = 0;
2337 write_device_info(&device);
2338 }
2339 fastboot_okay("");
2340}
2341
2342void cmd_oem_verified(const char *arg, void *data, unsigned sz)
2343{
2344 /* TODO: Wipe user data */
2345 if(device.is_unlocked || !device.is_verified)
2346 {
2347 device.is_unlocked = 0;
2348 device.is_verified = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002349 write_device_info(&device);
2350 }
2351 fastboot_okay("");
2352}
2353
Shashank Mittala0032282011-08-26 14:50:11 -07002354void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2355{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302356 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002357 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002358 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002359 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2360 fastboot_info(response);
2361 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002362 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302363 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2364 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002365 fastboot_okay("");
2366}
2367
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002368void cmd_preflash(const char *arg, void *data, unsigned sz)
2369{
2370 fastboot_okay("");
2371}
2372
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302373static struct fbimage logo_header = {0};
2374struct fbimage* splash_screen_flash();
2375
2376int splash_screen_check_header(struct fbimage *logo)
2377{
2378 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2379 return -1;
2380 if (logo->header.width == 0 || logo->header.height == 0)
2381 return -1;
2382 return 0;
2383}
2384
2385struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002386{
2387 struct ptentry *ptn;
2388 struct ptable *ptable;
2389 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302390 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002391
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302392
2393 ptable = flash_get_ptable();
2394 if (ptable == NULL) {
2395 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2396 return NULL;
2397 }
2398 ptn = ptable_find(ptable, "splash");
2399 if (ptn == NULL) {
2400 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2401 return NULL;
2402 }
2403
2404 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2405 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2406 return NULL;
2407 }
2408
2409 if (splash_screen_check_header(logo)) {
2410 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2411 return NULL;
2412 }
2413
2414 fb_display = fbcon_display();
2415 if (fb_display) {
2416 uint8_t *base = (uint8_t *) fb_display->base;
2417 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
2418 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002419 }
2420
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302421 if (flash_read(ptn + sizeof(logo->header), 0,
2422 base,
2423 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2424 fbcon_clear();
2425 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2426 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002427 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302428 logo->image = base;
2429 }
2430
2431 return logo;
2432}
2433
2434struct fbimage* splash_screen_mmc()
2435{
2436 int index = INVALID_PTN;
2437 unsigned long long ptn = 0;
2438 struct fbcon_config *fb_display = NULL;
2439 struct fbimage *logo = &logo_header;
2440
2441 index = partition_get_index("splash");
2442 if (index == 0) {
2443 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2444 return NULL;
2445 }
2446
2447 ptn = partition_get_offset(index);
2448 if (ptn == 0) {
2449 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2450 return NULL;
2451 }
2452
2453 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2454 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2455 return NULL;
2456 }
2457
2458 if (splash_screen_check_header(logo)) {
2459 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2460 return NULL;
2461 }
2462
2463 fb_display = fbcon_display();
2464 if (fb_display) {
2465 uint8_t *base = (uint8_t *) fb_display->base;
2466 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2467 base += LOGO_IMG_OFFSET;
2468
2469 if (mmc_read(ptn + sizeof(logo->header),
2470 base,
2471 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2472 fbcon_clear();
2473 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2474 return NULL;
2475 }
2476
2477 logo->image = base;
2478 }
2479
2480 return logo;
2481}
2482
2483
2484struct fbimage* fetch_image_from_partition()
2485{
2486 if (target_is_emmc_boot()) {
2487 return splash_screen_mmc();
2488 } else {
2489 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002490 }
2491}
2492
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002493/* Get the size from partiton name */
2494static void get_partition_size(const char *arg, char *response)
2495{
2496 uint64_t ptn = 0;
2497 uint64_t size;
2498 int index = INVALID_PTN;
2499
2500 index = partition_get_index(arg);
2501
2502 if (index == INVALID_PTN)
2503 {
2504 dprintf(CRITICAL, "Invalid partition index\n");
2505 return;
2506 }
2507
2508 ptn = partition_get_offset(index);
2509
2510 if(!ptn)
2511 {
2512 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2513 return;
2514 }
2515
2516 size = partition_get_size(index);
2517
2518 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2519 return;
2520}
2521
2522/*
2523 * Publish the partition type & size info
2524 * fastboot getvar will publish the required information.
2525 * fastboot getvar partition_size:<partition_name>: partition size in hex
2526 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2527 */
2528static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2529{
2530 uint8_t i;
2531
2532 for (i = 0; i < num_parts; i++) {
2533 get_partition_size(info[i].part_name, info[i].size_response);
2534
2535 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2536 {
2537 dprintf(CRITICAL, "partition size name truncated\n");
2538 return;
2539 }
2540 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2541 {
2542 dprintf(CRITICAL, "partition type name truncated\n");
2543 return;
2544 }
2545
2546 /* publish partition size & type info */
2547 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2548 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2549 }
2550}
2551
Amol Jadi5edf3552013-07-23 14:15:34 -07002552/* register commands and variables for fastboot */
2553void aboot_fastboot_register_commands(void)
2554{
2555 if (target_is_emmc_boot())
2556 {
2557 fastboot_register("flash:", cmd_flash_mmc);
2558 fastboot_register("erase:", cmd_erase_mmc);
2559 }
2560 else
2561 {
2562 fastboot_register("flash:", cmd_flash);
2563 fastboot_register("erase:", cmd_erase);
2564 }
2565
2566 fastboot_register("boot", cmd_boot);
2567 fastboot_register("continue", cmd_continue);
2568 fastboot_register("reboot", cmd_reboot);
2569 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2570 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002571 fastboot_register("oem lock", cmd_oem_lock);
2572 fastboot_register("oem verified", cmd_oem_verified);
Amol Jadi5edf3552013-07-23 14:15:34 -07002573 fastboot_register("oem device-info", cmd_oem_devinfo);
2574 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002575 fastboot_register("oem enable-charger-screen",
2576 cmd_oem_enable_charger_screen);
2577 fastboot_register("oem disable-charger-screen",
2578 cmd_oem_disable_charger_screen);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302579 fastboot_register("oem select-display-panel",
2580 cmd_oem_select_display_panel);
Amol Jadi5edf3552013-07-23 14:15:34 -07002581 /* publish variables and their values */
2582 fastboot_publish("product", TARGET(BOARD));
2583 fastboot_publish("kernel", "lk");
2584 fastboot_publish("serialno", sn_buf);
2585
2586 /*
2587 * partition info is supported only for emmc partitions
2588 * Calling this for NAND prints some error messages which
2589 * is harmless but misleading. Avoid calling this for NAND
2590 * devices.
2591 */
2592 if (target_is_emmc_boot())
2593 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2594
2595 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002596 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2597 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002598 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002599 /* Is the charger screen check enabled */
2600 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2601 device.charger_screen_enabled);
2602 fastboot_publish("charger-screen-enabled",
2603 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302604 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2605 device.display_panel);
2606 fastboot_publish("display-panel",
2607 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002608}
2609
Brian Swetland9c4c0752009-01-25 16:23:50 -08002610void aboot_init(const struct app_descriptor *app)
2611{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002612 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002613 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002614
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002615 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002616 if (target_is_emmc_boot())
2617 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002618 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002619 page_mask = page_size - 1;
2620 }
2621 else
2622 {
2623 page_size = flash_page_size();
2624 page_mask = page_size - 1;
2625 }
2626
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002627 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2628
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002629 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002630
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002631 /* Display splash screen if enabled */
2632#if DISPLAY_SPLASH_SCREEN
2633 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302634 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002635 dprintf(SPEW, "Display Init: Done\n");
2636#endif
2637
2638
Greg Griscod6250552011-06-29 14:40:23 -07002639 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002640 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002641
Dhaval Patel223ec952013-07-18 14:49:44 -07002642 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2643
Matthew Qindefd5562014-07-11 18:02:40 +08002644 /*
2645 * Check power off reason if user force reset,
2646 * if yes phone will do normal boot.
2647 */
2648 if (is_user_force_reset())
2649 goto normal_boot;
2650
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002651 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002652 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002653 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002654 dprintf(ALWAYS,"dload mode key sequence detected\n");
2655 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002656 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002657 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002658 }
2659 else
2660 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302661 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002662 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002663 }
2664 boot_into_fastboot = true;
2665 }
2666 if (!boot_into_fastboot)
2667 {
2668 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2669 boot_into_recovery = 1;
2670 if (!boot_into_recovery &&
2671 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002672 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002673 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002674 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002675 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002676 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002677 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002678
Ajay Dudani77421292010-10-27 19:34:06 -07002679 reboot_mode = check_reboot_mode();
2680 if (reboot_mode == RECOVERY_MODE) {
2681 boot_into_recovery = 1;
2682 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002683 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002684 }
2685
Matthew Qindefd5562014-07-11 18:02:40 +08002686normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03002687 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002688 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002689 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002690 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002691 if(emmc_recovery_init())
2692 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2693 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002694 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002695 if((device.is_unlocked) || (device.is_tampered))
2696 {
2697 #ifdef TZ_TAMPER_FUSE
2698 set_tamper_fuse_cmd();
2699 #endif
2700 #if USE_PCOM_SECBOOT
2701 set_tamper_flag(device.is_tampered);
2702 #endif
2703 }
Shashank Mittala0032282011-08-26 14:50:11 -07002704 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002705 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002706 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002707 else
2708 {
2709 recovery_init();
2710 #if USE_PCOM_SECBOOT
2711 if((device.is_unlocked) || (device.is_tampered))
2712 set_tamper_flag(device.is_tampered);
2713 #endif
2714 boot_linux_from_flash();
2715 }
2716 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2717 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002718 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002719
Amol Jadi5edf3552013-07-23 14:15:34 -07002720 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002721
Amol Jadi5edf3552013-07-23 14:15:34 -07002722 /* register aboot specific fastboot commands */
2723 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002724
Amol Jadi5edf3552013-07-23 14:15:34 -07002725 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002726 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002727
2728 /* initialize and start fastboot */
2729 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002730}
2731
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002732uint32_t get_page_size()
2733{
2734 return page_size;
2735}
2736
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002737/*
2738 * Calculated and save hash (SHA256) for non-signed boot image.
2739 *
2740 * Hash the same data that is checked on the signed boot image.
2741 * Kernel and Ramdisk are already read to memory buffers.
2742 * Need to read the entire device-tree from mmc
2743 * since non-signed image only read the DT tags of the relevant platform.
2744 *
2745 * @param kernel_addr - kernel bufer
2746 * @param kernel_actual - kernel size in bytes
2747 * @param ramdisk_addr - ramdisk buffer
2748 * @param ramdisk_actual - ramdisk size
2749 * @param ptn - partition
2750 * @param dt_offset - device tree offset on mmc partition
2751 * @param dt_size
2752 *
2753 * @return int - 0 on success, negative value on failure.
2754 */
2755int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2756 void *ramdisk_addr, unsigned ramdisk_actual,
2757 unsigned long long ptn,
2758 unsigned dt_offset, unsigned dt_size)
2759{
2760 SHA256_CTX sha256_ctx;
2761 char digest[32]={0};
2762 char *buf = (char *)target_get_scratch_address();
2763 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2764 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2765
2766 SHA256_Init(&sha256_ctx);
2767
2768 /* Read Boot Header */
2769 if (mmc_read(ptn, buf, page_size))
2770 {
2771 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2772 return -1;
2773 }
2774 /* Read entire Device Tree */
2775 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2776 {
2777 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2778 return -1;
2779 }
2780 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2781 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2782 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2783 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2784
2785 SHA256_Final(digest, &sha256_ctx);
2786
2787 save_kernel_hash_cmd(digest);
2788 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2789
2790 return 0;
2791}
2792
Brian Swetland9c4c0752009-01-25 16:23:50 -08002793APP_START(aboot)
2794 .init = aboot_init,
2795APP_END