blob: 3ea7a44342880c908d2f730329da18233dffbb52 [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>
Dima Zavin214cc642009-01-26 11:16:21 -080057
Neeti Desai17379b82012-06-04 18:42:53 -070058#if DEVICE_TREE
59#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070060#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070061#endif
62
Shashank Mittalcd98d472011-08-02 14:29:24 -070063#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080064#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080065#include "bootimg.h"
66#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070067#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070068#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070069#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070070#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070071#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070072
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070073extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070074extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070075extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070076extern int get_target_boot_params(const char *cmdline, const char *part,
77 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070078
79void write_device_info_mmc(device_info *dev);
80void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -070081static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070082
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
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530147static device_info device = {DEVICE_MAGIC, 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
672 ret = image_verify((unsigned char *)bootimg_addr,
673 (unsigned char *)(bootimg_addr + bootimg_size),
674 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800675 auth_algo);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700676
677 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
678
679 if (ret)
680 {
681 /* Authorized kernel */
682 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700683 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700684 }
685
686#if USE_PCOM_SECBOOT
687 set_tamper_flag(device.is_tampered);
688#endif
689
690 if(device.is_tampered)
691 {
692 write_device_info_mmc(&device);
693 #ifdef TZ_TAMPER_FUSE
694 set_tamper_fuse_cmd();
695 #endif
696 #ifdef ASSERT_ON_TAMPER
697 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
698 ASSERT(0);
699 #endif
700 }
701}
702
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530703static bool check_format_bit()
704{
705 bool ret = false;
706 int index;
707 uint64_t offset;
708 struct boot_selection_info *in = NULL;
709 char *buf = NULL;
710
711 index = partition_get_index("bootselect");
712 if (index == INVALID_PTN)
713 {
714 dprintf(INFO, "Unable to locate /bootselect partition\n");
715 return ret;
716 }
717 offset = partition_get_offset(index);
718 if(!offset)
719 {
720 dprintf(INFO, "partition /bootselect doesn't exist\n");
721 return ret;
722 }
723 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
724 ASSERT(buf);
725 if (mmc_read(offset, (unsigned int *)buf, page_size))
726 {
727 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
728 free(buf);
729 return ret;
730 }
731 in = (struct boot_selection_info *) buf;
732 if ((in->signature == BOOTSELECT_SIGNATURE) &&
733 (in->version == BOOTSELECT_VERSION)) {
734 if ((in->state_info & BOOTSELECT_FORMAT) &&
735 !(in->state_info & BOOTSELECT_FACTORY))
736 ret = true;
737 } else {
738 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
739 in->signature, in->version);
740 ASSERT(0);
741 }
742 free(buf);
743 return ret;
744}
745
Shashank Mittal23b8f422010-04-16 19:27:21 -0700746int boot_linux_from_mmc(void)
747{
748 struct boot_img_hdr *hdr = (void*) buf;
749 struct boot_img_hdr *uhdr;
750 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700751 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700752 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700753 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700754
Shashank Mittalcd98d472011-08-02 14:29:24 -0700755 unsigned char *image_addr = 0;
756 unsigned kernel_actual;
757 unsigned ramdisk_actual;
758 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700759 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700760
761#if DEVICE_TREE
762 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700763 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700764 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800765 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700766 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700767#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800768 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
769 struct kernel64_hdr *kptr = (void*) kbuf;
770
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530771 if (check_format_bit())
772 boot_into_recovery = 1;
773
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700774 if (!boot_into_recovery) {
775 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
776 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
777 if (rcode <= 0) {
778 boot_into_ffbm = false;
779 if (rcode < 0)
780 dprintf(CRITICAL,"failed to get ffbm cookie");
781 } else
782 boot_into_ffbm = true;
783 } else
784 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700785 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
786 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
787 dprintf(INFO, "Unified boot method!\n");
788 hdr = uhdr;
789 goto unified_boot;
790 }
Greg Griscod6250552011-06-29 14:40:23 -0700791 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700792 index = partition_get_index("boot");
793 ptn = partition_get_offset(index);
794 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700795 dprintf(CRITICAL, "ERROR: No boot partition found\n");
796 return -1;
797 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700798 }
799 else {
800 index = partition_get_index("recovery");
801 ptn = partition_get_offset(index);
802 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700803 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
804 return -1;
805 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700806 }
807
Greg Griscod6250552011-06-29 14:40:23 -0700808 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700809 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
810 return -1;
811 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700812
813 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700814 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700815 return -1;
816 }
817
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700818 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530819
820 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
821 dprintf(CRITICAL, "ERROR: Invalid page size\n");
822 return -1;
823 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700824 page_size = hdr->page_size;
825 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700826 }
827
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800828 /* Read the next page to get kernel Image header
829 * which lives in the second page for arm64 targets.
830 */
831
832 if (mmc_read(ptn + page_size, (unsigned int *) kbuf, page_size)) {
833 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
834 return -1;
835 }
836
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700837 /*
838 * Update the kernel/ramdisk/tags address if the boot image header
839 * has default values, these default values come from mkbootimg when
840 * the boot image is flashed using fastboot flash:raw
841 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800842 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700843
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700844 /* Get virtual addresses since the hdr saves physical addresses. */
845 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
846 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
847 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700848
849 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
850 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
851
852 /* Check if the addresses in the header are valid. */
853 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
854 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
855 {
856 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
857 return -1;
858 }
859
860#ifndef DEVICE_TREE
861 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
862 {
863 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
864 return -1;
865 }
866#endif
867
Shashank Mittalcd98d472011-08-02 14:29:24 -0700868 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300869 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
870 (int) target_use_signed_kernel(),
871 device.is_unlocked,
872 device.is_tampered);
873
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700874 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800875 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700876 offset = 0;
877
Shashank Mittalcd98d472011-08-02 14:29:24 -0700878 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700879
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800880#if DEVICE_TREE
881 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
882 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700883
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700884 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700885 {
886 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
887 return -1;
888 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800889#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530890 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700891
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700892#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700893
Amol Jadib6be5c12012-11-14 13:39:51 -0800894 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700895 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800896
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700897 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
898 {
899 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
900 return -1;
901 }
902
Shashank Mittalcd98d472011-08-02 14:29:24 -0700903 /* Read image without signature */
904 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
905 {
906 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
907 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800908 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700909
Amol Jadib6be5c12012-11-14 13:39:51 -0800910 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700911 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800912
Shashank Mittalcd98d472011-08-02 14:29:24 -0700913 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700914
915 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
916 {
917 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
918 return -1;
919 }
920
Shashank Mittalcd98d472011-08-02 14:29:24 -0700921 /* Read signature */
922 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
923 {
924 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700925 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700926 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800927
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700928 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700929
Neeti Desai465491e2012-07-31 12:53:35 -0700930 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700931 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
932 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700933
Neeti Desai465491e2012-07-31 12:53:35 -0700934 #if DEVICE_TREE
935 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700936 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700937 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700938
Deepa Dinamani19648b42013-09-05 17:05:55 -0700939 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700940 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
941 return -1;
942 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700943
Neeti Desai465491e2012-07-31 12:53:35 -0700944 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700945 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700946 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
947 return -1;
948 }
949
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700950 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700951 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700952 {
953 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
954 return -1;
955 }
956
Joel Kingaa335dc2013-06-03 16:11:08 -0700957 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800958 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800959 /*
960 * If appended dev tree is found, update the atags with
961 * memory address to the DTB appended location on RAM.
962 * Else update with the atags address in the kernel header
963 */
964 void *dtb;
965 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700966 hdr->kernel_size,
967 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800968 if (!dtb) {
969 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
970 return -1;
971 }
Neeti Desai465491e2012-07-31 12:53:35 -0700972 }
973 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700974 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700975 else
976 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800977 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700978
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700979 image_addr = (unsigned char *)target_get_scratch_address();
980#if DEVICE_TREE
981 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
982 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
983
984 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
985 {
986 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
987 return -1;
988 }
989#else
990 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
991
992#endif
993 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
994 {
995 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
996 return -1;
997 }
998
Amol Jadib6be5c12012-11-14 13:39:51 -0800999 dprintf(INFO, "Loading boot image (%d): start\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001000 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001001 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001002
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001003 offset = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001004
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001005 /* Load the entire boot image */
1006 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual)) {
1007 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
Shashank Mittalcd98d472011-08-02 14:29:24 -07001008 return -1;
1009 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001010
1011 dprintf(INFO, "Loading boot image (%d): done\n",
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001012 imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001013 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -07001014
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001015 #ifdef TZ_SAVE_KERNEL_HASH
1016 aboot_save_boot_hash_mmc(image_addr, imagesize_actual);
1017 #endif /* TZ_SAVE_KERNEL_HASH */
1018
1019 /* Move kernel, ramdisk and device tree to correct address */
1020 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1021 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Neeti Desai465491e2012-07-31 12:53:35 -07001022
1023 #if DEVICE_TREE
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001024 if(hdr->dt_size) {
1025 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1026 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -07001027
Deepa Dinamani19648b42013-09-05 17:05:55 -07001028 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001029 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1030 return -1;
1031 }
1032
Joel Kingaa335dc2013-06-03 16:11:08 -07001033 /* Find index of device tree within device tree table */
1034 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001035 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1036 return -1;
1037 }
1038
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001039 /* Validate and Read device device tree in the tags_addr */
Joel Kingaa335dc2013-06-03 16:11:08 -07001040 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001041 {
1042 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1043 return -1;
1044 }
1045
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001046 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001047 } else {
Aparna Mallavarapu33127372014-06-03 22:15:54 +05301048 /* Validate the tags_addr */
1049 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1050 {
1051 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1052 return -1;
1053 }
Dima Zavin77e41f32013-03-06 16:10:43 -08001054 /*
1055 * If appended dev tree is found, update the atags with
1056 * memory address to the DTB appended location on RAM.
1057 * Else update with the atags address in the kernel header
1058 */
1059 void *dtb;
1060 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001061 kernel_actual,
1062 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001063 if (!dtb) {
1064 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1065 return -1;
1066 }
Neeti Desai465491e2012-07-31 12:53:35 -07001067 }
1068 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001069 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001070
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001071 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1072 target_load_ssd_keystore();
1073
Shashank Mittal23b8f422010-04-16 19:27:21 -07001074unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001075
Dima Zavin77e41f32013-03-06 16:10:43 -08001076 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001077 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001078 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1079
1080 return 0;
1081}
1082
Dima Zavin214cc642009-01-26 11:16:21 -08001083int boot_linux_from_flash(void)
1084{
1085 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001086 struct ptentry *ptn;
1087 struct ptable *ptable;
1088 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001089
Shashank Mittalcd98d472011-08-02 14:29:24 -07001090 unsigned char *image_addr = 0;
1091 unsigned kernel_actual;
1092 unsigned ramdisk_actual;
1093 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001094 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001095
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001096#if DEVICE_TREE
1097 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001098 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001099 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001100 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001101#endif
1102
David Ng183a7422009-12-07 14:55:21 -08001103 if (target_is_emmc_boot()) {
1104 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1105 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1106 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1107 return -1;
1108 }
1109 goto continue_boot;
1110 }
1111
Dima Zavin214cc642009-01-26 11:16:21 -08001112 ptable = flash_get_ptable();
1113 if (ptable == NULL) {
1114 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1115 return -1;
1116 }
1117
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001118 if(!boot_into_recovery)
1119 {
1120 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001121
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001122 if (ptn == NULL) {
1123 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1124 return -1;
1125 }
1126 }
1127 else
1128 {
1129 ptn = ptable_find(ptable, "recovery");
1130 if (ptn == NULL) {
1131 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1132 return -1;
1133 }
Dima Zavin214cc642009-01-26 11:16:21 -08001134 }
1135
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001136 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001137 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1138 return -1;
1139 }
Dima Zavin214cc642009-01-26 11:16:21 -08001140
1141 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001142 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001143 return -1;
1144 }
1145
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001146 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001147 dprintf(CRITICAL, "ERROR: Invalid boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001148 return -1;
1149 }
1150
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001151 /*
1152 * Update the kernel/ramdisk/tags address if the boot image header
1153 * has default values, these default values come from mkbootimg when
1154 * the boot image is flashed using fastboot flash:raw
1155 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001156 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001157
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001158 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001159 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1160 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1161 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1162
1163 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1164 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1165
1166 /* Check if the addresses in the header are valid. */
1167 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1168 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1169 {
1170 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1171 return -1;
1172 }
1173
1174#ifndef DEVICE_TREE
1175 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1176 {
1177 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1178 return -1;
1179 }
1180#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001181
Shashank Mittalcd98d472011-08-02 14:29:24 -07001182 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001183 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001184 {
1185 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001186 offset = 0;
1187
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001188#if DEVICE_TREE
1189 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1190 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001191
1192 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1193 {
1194 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1195 return -1;
1196 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001197#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001198 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001199#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001200
Amol Jadib6be5c12012-11-14 13:39:51 -08001201 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001202 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001203
Shashank Mittalcd98d472011-08-02 14:29:24 -07001204 /* Read image without signature */
1205 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1206 {
1207 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1208 return -1;
1209 }
Dima Zavin214cc642009-01-26 11:16:21 -08001210
Amol Jadib6be5c12012-11-14 13:39:51 -08001211 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001212 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001213
Shashank Mittalcd98d472011-08-02 14:29:24 -07001214 offset = imagesize_actual;
1215 /* Read signature */
1216 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1217 {
1218 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001219 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001220 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001221
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001222 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001223
1224 /* Move kernel and ramdisk to correct address */
1225 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1226 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001227#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001228 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001229 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001230 {
1231 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1232 return -1;
1233 }
1234
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001235 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1236#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001237
1238 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001239 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001240 {
1241 write_device_info_flash(&device);
1242 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301243#if USE_PCOM_SECBOOT
1244 set_tamper_flag(device.is_tampered);
1245#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001246 }
1247 else
1248 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001249 offset = page_size;
1250
Amol Jadib6be5c12012-11-14 13:39:51 -08001251 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1252 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1253 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1254
1255 dprintf(INFO, "Loading boot image (%d): start\n",
1256 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001257 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001258
1259 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001260 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1261 return -1;
1262 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001263 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001264
Amol Jadib6be5c12012-11-14 13:39:51 -08001265 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001266 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1267 return -1;
1268 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001269 offset += ramdisk_actual;
1270
1271 dprintf(INFO, "Loading boot image (%d): done\n",
1272 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001273 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001274
1275 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001276 offset += second_actual;
1277 /* Second image loading not implemented. */
1278 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001279 }
1280
1281#if DEVICE_TREE
1282 if(hdr->dt_size != 0) {
1283
1284 /* Read the device tree table into buffer */
1285 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1286 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1287 return -1;
1288 }
1289
1290 table = (struct dt_table*) dt_buf;
1291
Deepa Dinamani19648b42013-09-05 17:05:55 -07001292 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001293 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1294 return -1;
1295 }
1296
Deepa Dinamani19648b42013-09-05 17:05:55 -07001297 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1298 if (!table)
1299 return -1;
1300
1301 /* Read the entire device tree table into buffer */
1302 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1303 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1304 return -1;
1305 }
1306
1307
Joel Kingaa335dc2013-06-03 16:11:08 -07001308 /* Find index of device tree within device tree table */
1309 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001310 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1311 return -1;
1312 }
1313
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001314 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001315 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001316 {
1317 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1318 return -1;
1319 }
1320
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001321 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001322 if(flash_read(ptn, offset + dt_entry.offset,
1323 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001324 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1325 return -1;
1326 }
1327 }
1328#endif
1329
Shashank Mittalcd98d472011-08-02 14:29:24 -07001330 }
David Ng183a7422009-12-07 14:55:21 -08001331continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001332
Dima Zavin214cc642009-01-26 11:16:21 -08001333 /* TODO: create/pass atags to kernel */
1334
Ajay Dudanie28a6072011-07-01 13:59:46 -07001335 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001336 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001337 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1338
1339 return 0;
1340}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001341
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001342BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001343void write_device_info_mmc(device_info *dev)
1344{
1345 struct device_info *info = (void*) info_buf;
1346 unsigned long long ptn = 0;
1347 unsigned long long size;
1348 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001349 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001350 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001351
1352 index = partition_get_index("aboot");
1353 ptn = partition_get_offset(index);
1354 if(ptn == 0)
1355 {
1356 return;
1357 }
1358
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001359 lun = partition_get_lun(index);
1360 mmc_set_lun(lun);
1361
Shashank Mittal162244e2011-08-08 19:01:25 -07001362 size = partition_get_size(index);
1363
1364 memcpy(info, dev, sizeof(device_info));
1365
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001366 blocksize = mmc_get_device_blocksize();
1367
1368 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Shashank Mittal162244e2011-08-08 19:01:25 -07001369 {
1370 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1371 return;
1372 }
1373}
1374
1375void read_device_info_mmc(device_info *dev)
1376{
1377 struct device_info *info = (void*) info_buf;
1378 unsigned long long ptn = 0;
1379 unsigned long long size;
1380 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001381 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001382
1383 index = partition_get_index("aboot");
1384 ptn = partition_get_offset(index);
1385 if(ptn == 0)
1386 {
1387 return;
1388 }
1389
1390 size = partition_get_size(index);
1391
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001392 blocksize = mmc_get_device_blocksize();
1393
1394 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Shashank Mittal162244e2011-08-08 19:01:25 -07001395 {
1396 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1397 return;
1398 }
1399
1400 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1401 {
1402 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1403 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001404 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001405 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001406
1407 write_device_info_mmc(info);
1408 }
1409 memcpy(dev, info, sizeof(device_info));
1410}
1411
1412void write_device_info_flash(device_info *dev)
1413{
1414 struct device_info *info = (void *) info_buf;
1415 struct ptentry *ptn;
1416 struct ptable *ptable;
1417
1418 ptable = flash_get_ptable();
1419 if (ptable == NULL)
1420 {
1421 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1422 return;
1423 }
1424
1425 ptn = ptable_find(ptable, "devinfo");
1426 if (ptn == NULL)
1427 {
1428 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1429 return;
1430 }
1431
1432 memcpy(info, dev, sizeof(device_info));
1433
1434 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1435 {
1436 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1437 return;
1438 }
1439}
1440
1441void read_device_info_flash(device_info *dev)
1442{
1443 struct device_info *info = (void*) info_buf;
1444 struct ptentry *ptn;
1445 struct ptable *ptable;
1446
1447 ptable = flash_get_ptable();
1448 if (ptable == NULL)
1449 {
1450 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1451 return;
1452 }
1453
1454 ptn = ptable_find(ptable, "devinfo");
1455 if (ptn == NULL)
1456 {
1457 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1458 return;
1459 }
1460
1461 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1462 {
1463 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1464 return;
1465 }
1466
1467 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1468 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001469 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1470 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001471 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001472 write_device_info_flash(info);
1473 }
1474 memcpy(dev, info, sizeof(device_info));
1475}
1476
1477void write_device_info(device_info *dev)
1478{
1479 if(target_is_emmc_boot())
1480 {
1481 write_device_info_mmc(dev);
1482 }
1483 else
1484 {
1485 write_device_info_flash(dev);
1486 }
1487}
1488
1489void read_device_info(device_info *dev)
1490{
1491 if(target_is_emmc_boot())
1492 {
1493 read_device_info_mmc(dev);
1494 }
1495 else
1496 {
1497 read_device_info_flash(dev);
1498 }
1499}
1500
1501void reset_device_info()
1502{
1503 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001504 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001505 write_device_info(&device);
1506}
1507
1508void set_device_root()
1509{
1510 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001511 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001512 write_device_info(&device);
1513}
1514
Amol Jadicb524072012-08-09 16:40:18 -07001515#if DEVICE_TREE
1516int copy_dtb(uint8_t *boot_image_start)
1517{
1518 uint32 dt_image_offset = 0;
1519 uint32_t n;
1520 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001521 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001522 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001523
1524 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1525
Amol Jadicb524072012-08-09 16:40:18 -07001526 if(hdr->dt_size != 0) {
1527
1528 /* add kernel offset */
1529 dt_image_offset += page_size;
1530 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1531 dt_image_offset += n;
1532
1533 /* add ramdisk offset */
1534 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1535 dt_image_offset += n;
1536
1537 /* add second offset */
1538 if(hdr->second_size != 0) {
1539 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1540 dt_image_offset += n;
1541 }
1542
1543 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001544 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001545
Deepa Dinamani19648b42013-09-05 17:05:55 -07001546 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001547 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1548 return -1;
1549 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001550 /* Find index of device tree within device tree table */
1551 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001552 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1553 return -1;
1554 }
1555
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001556 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001557 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001558 {
1559 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1560 return -1;
1561 }
1562
Amol Jadicb524072012-08-09 16:40:18 -07001563 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001564 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001565 boot_image_start + dt_image_offset + dt_entry.offset,
1566 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001567 } else
1568 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001569
1570 /* Everything looks fine. Return success. */
1571 return 0;
1572}
1573#endif
1574
Brian Swetland9c4c0752009-01-25 16:23:50 -08001575void cmd_boot(const char *arg, void *data, unsigned sz)
1576{
1577 unsigned kernel_actual;
1578 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001579 uint32_t image_actual;
1580 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001581 uint32_t sig_actual = SIGNATURE_SIZE;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001582 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001583 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001584 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001585 int ret = 0;
1586 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001587
1588 if (sz < sizeof(hdr)) {
1589 fastboot_fail("invalid bootimage header");
1590 return;
1591 }
1592
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001593 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001594
1595 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001596 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001597
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001598 if(target_is_emmc_boot() && hdr->page_size) {
1599 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001600 page_mask = page_size - 1;
1601 }
1602
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001603 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1604 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001605#if DEVICE_TREE
1606 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1607#endif
1608
1609 image_actual = ADD_OF(page_size, kernel_actual);
1610 image_actual = ADD_OF(image_actual, ramdisk_actual);
1611 image_actual = ADD_OF(image_actual, dt_actual);
1612
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001613 if (target_use_signed_kernel() && (!device.is_unlocked))
1614 image_actual = ADD_OF(image_actual, sig_actual);
1615
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001616 /* sz should have atleast raw boot image */
1617 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001618 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001619 return;
1620 }
1621
1622 /* Verify the boot image
1623 * device & page_size are initialized in aboot_init
1624 */
1625 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001626 /* Pass size excluding signature size, otherwise we would try to
1627 * access signature beyond its length
1628 */
1629 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001630
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001631 /*
1632 * Update the kernel/ramdisk/tags address if the boot image header
1633 * has default values, these default values come from mkbootimg when
1634 * the boot image is flashed using fastboot flash:raw
1635 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001636 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1637 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001638
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001639 /* Get virtual addresses since the hdr saves physical addresses. */
1640 hdr->kernel_addr = VA(hdr->kernel_addr);
1641 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1642 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001643
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001644 /* Check if the addresses in the header are valid. */
1645 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1646 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1647 {
1648 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001649 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001650 }
1651
Amol Jadicb524072012-08-09 16:40:18 -07001652#if DEVICE_TREE
1653 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001654 ret = copy_dtb(data);
1655
1656 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001657#else
1658 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1659 {
1660 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001661 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001662 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001663#endif
1664
1665 /* Load ramdisk & kernel */
1666 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1667 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1668
1669#if DEVICE_TREE
1670 /*
1671 * If dtb is not found look for appended DTB in the kernel.
1672 * If appended dev tree is found, update the atags with
1673 * memory address to the DTB appended location on RAM.
1674 * Else update with the atags address in the kernel header
1675 */
1676 if (!dtb_copied) {
1677 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001678 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1679 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001680 if (!dtb) {
1681 fastboot_fail("dtb not found");
1682 return;
1683 }
Amol Jadicb524072012-08-09 16:40:18 -07001684 }
1685#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001686
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001687#ifndef DEVICE_TREE
1688 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1689 {
1690 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001691 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001692 }
1693#endif
1694
Brian Swetland9c4c0752009-01-25 16:23:50 -08001695 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001696 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001697
Dima Zavin77e41f32013-03-06 16:10:43 -08001698 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001699 (const char*) hdr->cmdline, board_machtype(),
1700 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001701}
1702
Dima Zavin214cc642009-01-26 11:16:21 -08001703void cmd_erase(const char *arg, void *data, unsigned sz)
1704{
1705 struct ptentry *ptn;
1706 struct ptable *ptable;
1707
1708 ptable = flash_get_ptable();
1709 if (ptable == NULL) {
1710 fastboot_fail("partition table doesn't exist");
1711 return;
1712 }
1713
1714 ptn = ptable_find(ptable, arg);
1715 if (ptn == NULL) {
1716 fastboot_fail("unknown partition name");
1717 return;
1718 }
1719
1720 if (flash_erase(ptn)) {
1721 fastboot_fail("failed to erase partition");
1722 return;
1723 }
1724 fastboot_okay("");
1725}
1726
Bikas Gurungd48bd242010-09-04 19:54:32 -07001727
1728void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1729{
Kun Liang2f1601a2013-08-12 16:29:54 +08001730 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001731 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001732 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001733 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001734 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001735
Kinson Chikf1a43512011-07-14 11:28:39 -07001736 index = partition_get_index(arg);
1737 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001738 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001739
Kinson Chikf1a43512011-07-14 11:28:39 -07001740 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001741 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001742 return;
1743 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001744
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001745 lun = partition_get_lun(index);
1746 mmc_set_lun(lun);
1747
Oliver Wangcee448d2013-10-22 18:40:13 +08001748#if MMC_SDHCI_SUPPORT
1749 if (mmc_erase_card(ptn, size)) {
1750 fastboot_fail("failed to erase partition\n");
1751 return;
1752 }
1753#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001754 size = partition_get_size(index);
1755 if (size > DEFAULT_ERASE_SIZE)
1756 size = DEFAULT_ERASE_SIZE;
1757
neetidb4b24d62012-01-20 12:13:09 -08001758 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001759 0 in first several blocks */
1760 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001761 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001762 return;
1763 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001764#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001765 fastboot_okay("");
1766}
1767
1768
Ajay Dudani5c761132011-04-07 20:19:04 -07001769void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001770{
1771 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001772 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001773 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001774 char *token = NULL;
1775 char *pname = NULL;
1776 uint8_t lun = 0;
1777 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001778
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001779 token = strtok(arg, ":");
1780 pname = token;
1781 token = strtok(NULL, ":");
1782 if(token)
1783 {
1784 lun = atoi(token);
1785 mmc_set_lun(lun);
1786 lun_set = true;
1787 }
1788
Mao Jinlong226f33a2014-07-04 17:24:10 +08001789 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07001790 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08001791 if (!strcmp(pname, "partition"))
1792 {
1793 dprintf(INFO, "Attempt to write partition image.\n");
1794 if (write_partition(sz, (unsigned char *) data)) {
1795 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07001796 return;
1797 }
1798 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08001799 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001800 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08001801 index = partition_get_index(pname);
1802 ptn = partition_get_offset(index);
1803 if(ptn == 0) {
1804 fastboot_fail("partition table doesn't exist");
1805 return;
1806 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001807
Mao Jinlong226f33a2014-07-04 17:24:10 +08001808 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
1809 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1810 fastboot_fail("image is not a boot image");
1811 return;
1812 }
1813 }
1814
1815 if(!lun_set)
1816 {
1817 lun = partition_get_lun(index);
1818 mmc_set_lun(lun);
1819 }
1820
1821 size = partition_get_size(index);
1822 if (ROUND_TO_PAGE(sz,511) > size) {
1823 fastboot_fail("size too large");
1824 return;
1825 }
1826 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1827 fastboot_fail("flash write failure");
1828 return;
1829 }
Greg Grisco6e754772011-06-23 12:19:39 -07001830 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001831 }
1832 fastboot_okay("");
1833 return;
1834}
1835
Ajay Dudani5c761132011-04-07 20:19:04 -07001836void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1837{
1838 unsigned int chunk;
1839 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001840 uint32_t *fill_buf = NULL;
1841 uint32_t fill_val;
1842 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001843 sparse_header_t *sparse_header;
1844 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001845 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001846 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301847 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001848 int index = INVALID_PTN;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001849 int i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001850 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001851
Kinson Chikf1a43512011-07-14 11:28:39 -07001852 index = partition_get_index(arg);
1853 ptn = partition_get_offset(index);
1854 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001855 fastboot_fail("partition table doesn't exist");
1856 return;
1857 }
1858
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301859 size = partition_get_size(index);
1860 if (ROUND_TO_PAGE(sz,511) > size) {
1861 fastboot_fail("size too large");
1862 return;
1863 }
1864
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001865 lun = partition_get_lun(index);
1866 mmc_set_lun(lun);
1867
Ajay Dudani5c761132011-04-07 20:19:04 -07001868 /* Read and skip over sparse image header */
1869 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001870 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1871 fastboot_fail("size too large");
1872 return;
1873 }
1874
Ajay Dudani5c761132011-04-07 20:19:04 -07001875 data += sparse_header->file_hdr_sz;
1876 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1877 {
1878 /* Skip the remaining bytes in a header that is longer than
1879 * we expected.
1880 */
1881 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1882 }
1883
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001884 dprintf (SPEW, "=== Sparse Image Header ===\n");
1885 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1886 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1887 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1888 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1889 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1890 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1891 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1892 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001893
1894 /* Start processing chunks */
1895 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1896 {
1897 /* Read and skip over chunk header */
1898 chunk_header = (chunk_header_t *) data;
1899 data += sizeof(chunk_header_t);
1900
1901 dprintf (SPEW, "=== Chunk Header ===\n");
1902 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1903 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1904 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1905
1906 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1907 {
1908 /* Skip the remaining bytes in a header that is longer than
1909 * we expected.
1910 */
1911 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1912 }
1913
1914 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1915 switch (chunk_header->chunk_type)
1916 {
1917 case CHUNK_TYPE_RAW:
1918 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1919 chunk_data_sz))
1920 {
1921 fastboot_fail("Bogus chunk size for chunk type Raw");
1922 return;
1923 }
1924
Ajay Dudaniab18f022011-05-12 14:39:22 -07001925 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1926 chunk_data_sz,
1927 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001928 {
1929 fastboot_fail("flash write failure");
1930 return;
1931 }
1932 total_blocks += chunk_header->chunk_sz;
1933 data += chunk_data_sz;
1934 break;
1935
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001936 case CHUNK_TYPE_FILL:
1937 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1938 sizeof(uint32_t)))
1939 {
1940 fastboot_fail("Bogus chunk size for chunk type FILL");
1941 return;
1942 }
1943
1944 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
1945 if (!fill_buf)
1946 {
1947 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
1948 return;
1949 }
1950
1951 fill_val = *(uint32_t *)data;
1952 data = (char *) data + sizeof(uint32_t);
1953 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
1954
1955 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
1956 {
1957 fill_buf[i] = fill_val;
1958 }
1959
1960 for (i = 0; i < chunk_blk_cnt; i++)
1961 {
1962 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1963 sparse_header->blk_sz,
1964 fill_buf))
1965 {
1966 fastboot_fail("flash write failure");
1967 free(fill_buf);
1968 return;
1969 }
1970
1971 total_blocks++;
1972 }
1973
1974 free(fill_buf);
1975 break;
1976
Ajay Dudani5c761132011-04-07 20:19:04 -07001977 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001978 total_blocks += chunk_header->chunk_sz;
1979 break;
1980
Ajay Dudani5c761132011-04-07 20:19:04 -07001981 case CHUNK_TYPE_CRC:
1982 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1983 {
1984 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1985 return;
1986 }
1987 total_blocks += chunk_header->chunk_sz;
1988 data += chunk_data_sz;
1989 break;
1990
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001991 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001992 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07001993 fastboot_fail("Unknown chunk type");
1994 return;
1995 }
1996 }
1997
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001998 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1999 total_blocks, sparse_header->total_blks);
2000
2001 if(total_blocks != sparse_header->total_blks)
2002 {
2003 fastboot_fail("sparse image write failure");
2004 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002005
2006 fastboot_okay("");
2007 return;
2008}
2009
2010void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2011{
2012 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002013 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2014 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002015
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002016#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002017 int ret=0;
2018 uint32 major_version=0;
2019 uint32 minor_version=0;
2020
2021 ret = scm_svc_version(&major_version,&minor_version);
2022 if(!ret)
2023 {
2024 if(major_version >= 2)
2025 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002026 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002027 {
2028 ret = encrypt_scm((uint32 **) &data, &sz);
2029 if (ret != 0) {
2030 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2031 return;
2032 }
2033
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002034 /* Protect only for SSD */
2035 if (!strcmp(arg, "ssd")) {
2036 ret = scm_protect_keystore((uint32 *) data, sz);
2037 if (ret != 0) {
2038 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2039 return;
2040 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002041 }
2042 }
2043 else
2044 {
2045 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2046 if(ret != 0)
2047 {
2048 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2049 return;
2050 }
2051 }
2052 }
2053 else
2054 {
2055 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2056 magic_number[1] == DECRYPT_MAGIC_1)
2057 {
2058 ret = decrypt_scm((uint32 **) &data, &sz);
2059 if (ret != 0) {
2060 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2061 return;
2062 }
2063 }
2064 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2065 magic_number[1] == ENCRYPT_MAGIC_1)
2066 {
2067 ret = encrypt_scm((uint32 **) &data, &sz);
2068 if (ret != 0) {
2069 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2070 return;
2071 }
2072 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002073 }
2074 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002075 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002076 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002077 dprintf(CRITICAL,"INVALID SVC Version\n");
2078 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002079 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002080#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002081
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002082 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002083 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
2084 cmd_flash_mmc_img(arg, data, sz);
2085 else
2086 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002087 return;
2088}
2089
Dima Zavin214cc642009-01-26 11:16:21 -08002090void cmd_flash(const char *arg, void *data, unsigned sz)
2091{
2092 struct ptentry *ptn;
2093 struct ptable *ptable;
2094 unsigned extra = 0;
2095
2096 ptable = flash_get_ptable();
2097 if (ptable == NULL) {
2098 fastboot_fail("partition table doesn't exist");
2099 return;
2100 }
2101
2102 ptn = ptable_find(ptable, arg);
2103 if (ptn == NULL) {
2104 fastboot_fail("unknown partition name");
2105 return;
2106 }
2107
2108 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2109 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2110 fastboot_fail("image is not a boot image");
2111 return;
2112 }
2113 }
2114
Amol Jadi5c61a952012-05-04 17:05:35 -07002115 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002116 || !strcmp(ptn->name, "userdata")
2117 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002118 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002119 || !strcmp(ptn->name, "modem"))
2120 {
2121 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07002122 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002123 else
2124 extra = 0;
2125 }
2126 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002127 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002128
2129 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2130 if (flash_write(ptn, extra, data, sz)) {
2131 fastboot_fail("flash write failure");
2132 return;
2133 }
2134 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2135 fastboot_okay("");
2136}
2137
2138void cmd_continue(const char *arg, void *data, unsigned sz)
2139{
2140 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002141 fastboot_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002142 if (target_is_emmc_boot())
2143 {
2144 boot_linux_from_mmc();
2145 }
2146 else
2147 {
2148 boot_linux_from_flash();
2149 }
Dima Zavin214cc642009-01-26 11:16:21 -08002150}
2151
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002152void cmd_reboot(const char *arg, void *data, unsigned sz)
2153{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002154 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002155 fastboot_okay("");
2156 reboot_device(0);
2157}
2158
2159void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2160{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002161 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002162 fastboot_okay("");
2163 reboot_device(FASTBOOT_MODE);
2164}
2165
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002166void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2167{
2168 dprintf(INFO, "Enabling charger screen check\n");
2169 device.charger_screen_enabled = 1;
2170 write_device_info(&device);
2171 fastboot_okay("");
2172}
2173
2174void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2175{
2176 dprintf(INFO, "Disabling charger screen check\n");
2177 device.charger_screen_enabled = 0;
2178 write_device_info(&device);
2179 fastboot_okay("");
2180}
2181
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302182void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2183{
2184 dprintf(INFO, "Selecting display panel %s\n", arg);
2185 if (arg)
2186 strlcpy(device.display_panel, arg,
2187 sizeof(device.display_panel));
2188 write_device_info(&device);
2189 fastboot_okay("");
2190}
2191
Shashank Mittal162244e2011-08-08 19:01:25 -07002192void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2193{
2194 if(!device.is_unlocked)
2195 {
2196 device.is_unlocked = 1;
2197 write_device_info(&device);
2198 }
2199 fastboot_okay("");
2200}
2201
Shashank Mittala0032282011-08-26 14:50:11 -07002202void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2203{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302204 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002205 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002206 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002207 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2208 fastboot_info(response);
2209 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002210 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302211 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2212 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002213 fastboot_okay("");
2214}
2215
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002216void cmd_preflash(const char *arg, void *data, unsigned sz)
2217{
2218 fastboot_okay("");
2219}
2220
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302221static struct fbimage logo_header = {0};
2222struct fbimage* splash_screen_flash();
2223
2224int splash_screen_check_header(struct fbimage *logo)
2225{
2226 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2227 return -1;
2228 if (logo->header.width == 0 || logo->header.height == 0)
2229 return -1;
2230 return 0;
2231}
2232
2233struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002234{
2235 struct ptentry *ptn;
2236 struct ptable *ptable;
2237 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302238 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002239
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302240
2241 ptable = flash_get_ptable();
2242 if (ptable == NULL) {
2243 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2244 return NULL;
2245 }
2246 ptn = ptable_find(ptable, "splash");
2247 if (ptn == NULL) {
2248 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2249 return NULL;
2250 }
2251
2252 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2253 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2254 return NULL;
2255 }
2256
2257 if (splash_screen_check_header(logo)) {
2258 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2259 return NULL;
2260 }
2261
2262 fb_display = fbcon_display();
2263 if (fb_display) {
2264 uint8_t *base = (uint8_t *) fb_display->base;
2265 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
2266 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002267 }
2268
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302269 if (flash_read(ptn + sizeof(logo->header), 0,
2270 base,
2271 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2272 fbcon_clear();
2273 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2274 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002275 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302276 logo->image = base;
2277 }
2278
2279 return logo;
2280}
2281
2282struct fbimage* splash_screen_mmc()
2283{
2284 int index = INVALID_PTN;
2285 unsigned long long ptn = 0;
2286 struct fbcon_config *fb_display = NULL;
2287 struct fbimage *logo = &logo_header;
2288
2289 index = partition_get_index("splash");
2290 if (index == 0) {
2291 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2292 return NULL;
2293 }
2294
2295 ptn = partition_get_offset(index);
2296 if (ptn == 0) {
2297 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2298 return NULL;
2299 }
2300
2301 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2302 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2303 return NULL;
2304 }
2305
2306 if (splash_screen_check_header(logo)) {
2307 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2308 return NULL;
2309 }
2310
2311 fb_display = fbcon_display();
2312 if (fb_display) {
2313 uint8_t *base = (uint8_t *) fb_display->base;
2314 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2315 base += LOGO_IMG_OFFSET;
2316
2317 if (mmc_read(ptn + sizeof(logo->header),
2318 base,
2319 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2320 fbcon_clear();
2321 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2322 return NULL;
2323 }
2324
2325 logo->image = base;
2326 }
2327
2328 return logo;
2329}
2330
2331
2332struct fbimage* fetch_image_from_partition()
2333{
2334 if (target_is_emmc_boot()) {
2335 return splash_screen_mmc();
2336 } else {
2337 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002338 }
2339}
2340
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002341/* Get the size from partiton name */
2342static void get_partition_size(const char *arg, char *response)
2343{
2344 uint64_t ptn = 0;
2345 uint64_t size;
2346 int index = INVALID_PTN;
2347
2348 index = partition_get_index(arg);
2349
2350 if (index == INVALID_PTN)
2351 {
2352 dprintf(CRITICAL, "Invalid partition index\n");
2353 return;
2354 }
2355
2356 ptn = partition_get_offset(index);
2357
2358 if(!ptn)
2359 {
2360 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2361 return;
2362 }
2363
2364 size = partition_get_size(index);
2365
2366 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2367 return;
2368}
2369
2370/*
2371 * Publish the partition type & size info
2372 * fastboot getvar will publish the required information.
2373 * fastboot getvar partition_size:<partition_name>: partition size in hex
2374 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2375 */
2376static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2377{
2378 uint8_t i;
2379
2380 for (i = 0; i < num_parts; i++) {
2381 get_partition_size(info[i].part_name, info[i].size_response);
2382
2383 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2384 {
2385 dprintf(CRITICAL, "partition size name truncated\n");
2386 return;
2387 }
2388 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2389 {
2390 dprintf(CRITICAL, "partition type name truncated\n");
2391 return;
2392 }
2393
2394 /* publish partition size & type info */
2395 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2396 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2397 }
2398}
2399
Amol Jadi5edf3552013-07-23 14:15:34 -07002400/* register commands and variables for fastboot */
2401void aboot_fastboot_register_commands(void)
2402{
2403 if (target_is_emmc_boot())
2404 {
2405 fastboot_register("flash:", cmd_flash_mmc);
2406 fastboot_register("erase:", cmd_erase_mmc);
2407 }
2408 else
2409 {
2410 fastboot_register("flash:", cmd_flash);
2411 fastboot_register("erase:", cmd_erase);
2412 }
2413
2414 fastboot_register("boot", cmd_boot);
2415 fastboot_register("continue", cmd_continue);
2416 fastboot_register("reboot", cmd_reboot);
2417 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2418 fastboot_register("oem unlock", cmd_oem_unlock);
2419 fastboot_register("oem device-info", cmd_oem_devinfo);
2420 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002421 fastboot_register("oem enable-charger-screen",
2422 cmd_oem_enable_charger_screen);
2423 fastboot_register("oem disable-charger-screen",
2424 cmd_oem_disable_charger_screen);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302425 fastboot_register("oem select-display-panel",
2426 cmd_oem_select_display_panel);
Amol Jadi5edf3552013-07-23 14:15:34 -07002427 /* publish variables and their values */
2428 fastboot_publish("product", TARGET(BOARD));
2429 fastboot_publish("kernel", "lk");
2430 fastboot_publish("serialno", sn_buf);
2431
2432 /*
2433 * partition info is supported only for emmc partitions
2434 * Calling this for NAND prints some error messages which
2435 * is harmless but misleading. Avoid calling this for NAND
2436 * devices.
2437 */
2438 if (target_is_emmc_boot())
2439 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2440
2441 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002442 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2443 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002444 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002445 /* Is the charger screen check enabled */
2446 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2447 device.charger_screen_enabled);
2448 fastboot_publish("charger-screen-enabled",
2449 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302450 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2451 device.display_panel);
2452 fastboot_publish("display-panel",
2453 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002454}
2455
Brian Swetland9c4c0752009-01-25 16:23:50 -08002456void aboot_init(const struct app_descriptor *app)
2457{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002458 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002459 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002460
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002461 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002462 if (target_is_emmc_boot())
2463 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002464 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002465 page_mask = page_size - 1;
2466 }
2467 else
2468 {
2469 page_size = flash_page_size();
2470 page_mask = page_size - 1;
2471 }
2472
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002473 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2474
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002475 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002476
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002477 /* Display splash screen if enabled */
2478#if DISPLAY_SPLASH_SCREEN
2479 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302480 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002481 dprintf(SPEW, "Display Init: Done\n");
2482#endif
2483
2484
Greg Griscod6250552011-06-29 14:40:23 -07002485 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002486 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002487
Dhaval Patel223ec952013-07-18 14:49:44 -07002488 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2489
Matthew Qindefd5562014-07-11 18:02:40 +08002490 /*
2491 * Check power off reason if user force reset,
2492 * if yes phone will do normal boot.
2493 */
2494 if (is_user_force_reset())
2495 goto normal_boot;
2496
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002497 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002498 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002499 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002500 dprintf(ALWAYS,"dload mode key sequence detected\n");
2501 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002502 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002503 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002504 }
2505 else
2506 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302507 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002508 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002509 }
2510 boot_into_fastboot = true;
2511 }
2512 if (!boot_into_fastboot)
2513 {
2514 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2515 boot_into_recovery = 1;
2516 if (!boot_into_recovery &&
2517 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002518 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002519 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002520 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002521 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002522 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002523 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002524
Ajay Dudani77421292010-10-27 19:34:06 -07002525 reboot_mode = check_reboot_mode();
2526 if (reboot_mode == RECOVERY_MODE) {
2527 boot_into_recovery = 1;
2528 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002529 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002530 }
2531
Matthew Qindefd5562014-07-11 18:02:40 +08002532normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03002533 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002534 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002535 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002536 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002537 if(emmc_recovery_init())
2538 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2539 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002540 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002541 if((device.is_unlocked) || (device.is_tampered))
2542 {
2543 #ifdef TZ_TAMPER_FUSE
2544 set_tamper_fuse_cmd();
2545 #endif
2546 #if USE_PCOM_SECBOOT
2547 set_tamper_flag(device.is_tampered);
2548 #endif
2549 }
Shashank Mittala0032282011-08-26 14:50:11 -07002550 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002551 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002552 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002553 else
2554 {
2555 recovery_init();
2556 #if USE_PCOM_SECBOOT
2557 if((device.is_unlocked) || (device.is_tampered))
2558 set_tamper_flag(device.is_tampered);
2559 #endif
2560 boot_linux_from_flash();
2561 }
2562 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2563 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002564 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002565
Amol Jadi5edf3552013-07-23 14:15:34 -07002566 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002567
Amol Jadi5edf3552013-07-23 14:15:34 -07002568 /* register aboot specific fastboot commands */
2569 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002570
Amol Jadi5edf3552013-07-23 14:15:34 -07002571 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002572 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002573
2574 /* initialize and start fastboot */
2575 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002576}
2577
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002578uint32_t get_page_size()
2579{
2580 return page_size;
2581}
2582
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002583/*
2584 * Calculated and save hash (SHA256) for non-signed boot image.
2585 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002586 * @param image_addr - Boot image address
2587 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002588 *
2589 * @return int - 0 on success, negative value on failure.
2590 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002591static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002592{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002593 unsigned int digest[8];
2594#if IMAGE_VERIF_ALGO_SHA1
2595 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
2596#else
2597 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
2598#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002599
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002600 target_crypto_init_params();
2601 hash_find(image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002602
2603 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002604 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002605
2606 return 0;
2607}
2608
Brian Swetland9c4c0752009-01-25 16:23:50 -08002609APP_START(aboot)
2610 .init = aboot_init,
2611APP_END