blob: 3fef1b5836e8d3f2e7e6ec73fdb192344468bdac [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);
81
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070082#define EXPAND(NAME) #NAME
83#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070084
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080085#ifdef MEMBASE
86#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
87#else
David Ng183a7422009-12-07 14:55:21 -080088#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080089#endif
90
Deepa Dinamani0e163a42013-05-24 17:08:15 -070091#ifndef MEMSIZE
92#define MEMSIZE 1024*1024
93#endif
94
95#define MAX_TAGS_SIZE 1024
96
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080097#define RECOVERY_MODE 0x77665502
98#define FASTBOOT_MODE 0x77665500
99
Kun Liang2f1601a2013-08-12 16:29:54 +0800100/* make 4096 as default size to ensure EFS,EXT4's erasing */
101#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800102#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800103
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800104#define UBI_MAGIC "UBI#"
105#define UBI_MAGIC_SIZE 0x04
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700106#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800107
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800108#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
109
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700110#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
111
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700112#if UFS_SUPPORT
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700113static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700114#else
David Ng183a7422009-12-07 14:55:21 -0800115static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700116#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800117static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300118static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800119static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800120static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700121static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300122static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800123
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800124static const char *baseband_apq = " androidboot.baseband=apq";
125static const char *baseband_msm = " androidboot.baseband=msm";
126static const char *baseband_csfb = " androidboot.baseband=csfb";
127static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700128static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800129static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700130static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800131static const char *baseband_dsda = " androidboot.baseband=dsda";
132static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800133static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800134static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800135
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700136static unsigned page_size = 0;
137static unsigned page_mask = 0;
138static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
139static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700140static char target_boot_params[64];
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700141
Shashank Mittalcd98d472011-08-02 14:29:24 -0700142/* Assuming unauthorized kernel image by default */
143static int auth_kernel_img = 0;
144
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530145static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700146
Dima Zavin42168f22009-01-30 11:52:22 -0800147struct atag_ptbl_entry
148{
149 char name[16];
150 unsigned offset;
151 unsigned size;
152 unsigned flags;
153};
154
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700155/*
156 * Partition info, required to be published
157 * for fastboot
158 */
159struct getvar_partition_info {
160 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
161 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
162 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
163 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
164 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
165};
166
167/*
168 * Right now, we are publishing the info for only
169 * three partitions
170 */
171struct getvar_partition_info part_info[] =
172{
173 { "system" , "partition-size:", "partition-type:", "", "ext4" },
174 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
175 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
176};
177
178char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700179char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800180char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700181char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530182char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700183
Greg Griscod2471ef2011-07-14 13:00:42 -0700184extern int emmc_recovery_init(void);
185
Kinson Chik0b1c8162011-08-31 16:31:57 -0700186#if NO_KEYPAD_DRIVER
187extern int fastboot_trigger(void);
188#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700189
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800190static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700191{
192 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700193#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800194 if (is_arm64)
195 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
196 else
197 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700198 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
199 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700200#endif
201}
202
Dima Zavin42168f22009-01-30 11:52:22 -0800203static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
204{
205 struct atag_ptbl_entry atag_ptn;
206
207 memcpy(atag_ptn.name, ptn->name, 16);
208 atag_ptn.name[15] = '\0';
209 atag_ptn.offset = ptn->start;
210 atag_ptn.size = ptn->length;
211 atag_ptn.flags = ptn->flags;
212 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
213 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
214}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800215
Neeti Desaie245d492012-06-01 12:52:13 -0700216unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800217{
David Ng183a7422009-12-07 14:55:21 -0800218 int cmdline_len = 0;
219 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800220 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700221 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800222 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300223 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700224 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700225 char *boot_dev_buf = NULL;
Dima Zavin42168f22009-01-30 11:52:22 -0800226
Brian Swetland9c4c0752009-01-25 16:23:50 -0800227 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800228 cmdline_len = strlen(cmdline);
229 have_cmdline = 1;
230 }
231 if (target_is_emmc_boot()) {
232 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700233#if UFS_SUPPORT
234 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
235 ASSERT(boot_dev_buf);
236 platform_boot_dev_cmdline(boot_dev_buf);
237 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700238#endif
David Ng183a7422009-12-07 14:55:21 -0800239 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800240
241 cmdline_len += strlen(usb_sn_cmdline);
242 cmdline_len += strlen(sn_buf);
243
Pavel Nedev5614d222013-06-17 18:01:02 +0300244 if (boot_into_recovery && gpt_exists)
245 cmdline_len += strlen(secondary_gpt_enable);
246
Pavel Nedev328ac822013-04-05 15:25:11 +0300247 if (boot_into_ffbm) {
248 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700249 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800250 /* reduce kernel console messages to speed-up boot */
251 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700252 } else if (device.charger_screen_enabled &&
253 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700254 pause_at_bootup = 1;
255 cmdline_len += strlen(battchg_pause);
256 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800257
Shashank Mittalcd98d472011-08-02 14:29:24 -0700258 if(target_use_signed_kernel() && auth_kernel_img) {
259 cmdline_len += strlen(auth_kernel);
260 }
261
Joonwoo Park61112782013-10-02 19:50:39 -0700262 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
263 "system",
264 target_boot_params,
265 sizeof(target_boot_params)) == 0) {
266 have_target_boot_params = 1;
267 cmdline_len += strlen(target_boot_params);
268 }
269
Ajay Dudanid04110c2011-01-17 23:55:07 -0800270 /* Determine correct androidboot.baseband to use */
271 switch(target_baseband())
272 {
273 case BASEBAND_APQ:
274 cmdline_len += strlen(baseband_apq);
275 break;
276
277 case BASEBAND_MSM:
278 cmdline_len += strlen(baseband_msm);
279 break;
280
281 case BASEBAND_CSFB:
282 cmdline_len += strlen(baseband_csfb);
283 break;
284
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800285 case BASEBAND_SVLTE2A:
286 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800287 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700288
289 case BASEBAND_MDM:
290 cmdline_len += strlen(baseband_mdm);
291 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700292
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800293 case BASEBAND_MDM2:
294 cmdline_len += strlen(baseband_mdm2);
295 break;
296
Amol Jadi5c61a952012-05-04 17:05:35 -0700297 case BASEBAND_SGLTE:
298 cmdline_len += strlen(baseband_sglte);
299 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530300
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800301 case BASEBAND_SGLTE2:
302 cmdline_len += strlen(baseband_sglte2);
303 break;
304
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530305 case BASEBAND_DSDA:
306 cmdline_len += strlen(baseband_dsda);
307 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800308
309 case BASEBAND_DSDA2:
310 cmdline_len += strlen(baseband_dsda2);
311 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800312 }
313
Ajay Singh Parmar430bbb32014-03-18 15:26:30 -0700314 if (target_display_panel_node(device.display_panel,
315 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
316 strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700317 cmdline_len += strlen(display_panel_buf);
318 }
319
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800320 if (target_warm_boot()) {
321 warm_boot = true;
322 cmdline_len += strlen(warmboot_cmdline);
323 }
324
David Ng183a7422009-12-07 14:55:21 -0800325 if (cmdline_len > 0) {
326 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700327 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
328 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700329
Amol Jadi168b7712012-03-06 16:15:00 -0800330 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700331 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800332 if (have_cmdline) {
333 src = cmdline;
334 while ((*dst++ = *src++));
335 }
336 if (target_is_emmc_boot()) {
337 src = emmc_cmdline;
338 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700339 have_cmdline = 1;
340 while ((*dst++ = *src++));
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700341#if UFS_SUPPORT
342 src = boot_dev_buf;
343 if (have_cmdline) --dst;
344 while ((*dst++ = *src++));
345#endif
David Ngf773dde2010-07-26 19:55:08 -0700346 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800347
348 src = usb_sn_cmdline;
349 if (have_cmdline) --dst;
350 have_cmdline = 1;
351 while ((*dst++ = *src++));
352 src = sn_buf;
353 if (have_cmdline) --dst;
354 have_cmdline = 1;
355 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800356 if (warm_boot) {
357 if (have_cmdline) --dst;
358 src = warmboot_cmdline;
359 while ((*dst++ = *src++));
360 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800361
Pavel Nedev5614d222013-06-17 18:01:02 +0300362 if (boot_into_recovery && gpt_exists) {
363 src = secondary_gpt_enable;
364 if (have_cmdline) --dst;
365 while ((*dst++ = *src++));
366 }
367
Pavel Nedev328ac822013-04-05 15:25:11 +0300368 if (boot_into_ffbm) {
369 src = androidboot_mode;
370 if (have_cmdline) --dst;
371 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700372 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300373 if (have_cmdline) --dst;
374 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800375 src = loglevel;
376 if (have_cmdline) --dst;
377 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300378 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700379 src = battchg_pause;
380 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800381 while ((*dst++ = *src++));
382 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800383
Shashank Mittalcd98d472011-08-02 14:29:24 -0700384 if(target_use_signed_kernel() && auth_kernel_img) {
385 src = auth_kernel;
386 if (have_cmdline) --dst;
387 while ((*dst++ = *src++));
388 }
389
Ajay Dudanid04110c2011-01-17 23:55:07 -0800390 switch(target_baseband())
391 {
392 case BASEBAND_APQ:
393 src = baseband_apq;
394 if (have_cmdline) --dst;
395 while ((*dst++ = *src++));
396 break;
397
398 case BASEBAND_MSM:
399 src = baseband_msm;
400 if (have_cmdline) --dst;
401 while ((*dst++ = *src++));
402 break;
403
404 case BASEBAND_CSFB:
405 src = baseband_csfb;
406 if (have_cmdline) --dst;
407 while ((*dst++ = *src++));
408 break;
409
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800410 case BASEBAND_SVLTE2A:
411 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800412 if (have_cmdline) --dst;
413 while ((*dst++ = *src++));
414 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700415
416 case BASEBAND_MDM:
417 src = baseband_mdm;
418 if (have_cmdline) --dst;
419 while ((*dst++ = *src++));
420 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700421
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800422 case BASEBAND_MDM2:
423 src = baseband_mdm2;
424 if (have_cmdline) --dst;
425 while ((*dst++ = *src++));
426 break;
427
Amol Jadi5c61a952012-05-04 17:05:35 -0700428 case BASEBAND_SGLTE:
429 src = baseband_sglte;
430 if (have_cmdline) --dst;
431 while ((*dst++ = *src++));
432 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530433
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800434 case BASEBAND_SGLTE2:
435 src = baseband_sglte2;
436 if (have_cmdline) --dst;
437 while ((*dst++ = *src++));
438 break;
439
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530440 case BASEBAND_DSDA:
441 src = baseband_dsda;
442 if (have_cmdline) --dst;
443 while ((*dst++ = *src++));
444 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800445
446 case BASEBAND_DSDA2:
447 src = baseband_dsda2;
448 if (have_cmdline) --dst;
449 while ((*dst++ = *src++));
450 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800451 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700452
453 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700454 src = display_panel_buf;
455 if (have_cmdline) --dst;
456 while ((*dst++ = *src++));
457 }
Joonwoo Park61112782013-10-02 19:50:39 -0700458
459 if (have_target_boot_params) {
460 if (have_cmdline) --dst;
461 src = target_boot_params;
462 while ((*dst++ = *src++));
463 }
Neeti Desaie245d492012-06-01 12:52:13 -0700464 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700465
466
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700467 if (boot_dev_buf)
468 free(boot_dev_buf);
469
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800470 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700471 return cmdline_final;
472}
473
474unsigned *atag_core(unsigned *ptr)
475{
476 /* CORE */
477 *ptr++ = 2;
478 *ptr++ = 0x54410001;
479
480 return ptr;
481
482}
483
484unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
485 unsigned ramdisk_size)
486{
487 if (ramdisk_size) {
488 *ptr++ = 4;
489 *ptr++ = 0x54420005;
490 *ptr++ = (unsigned)ramdisk;
491 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800492 }
493
Neeti Desaie245d492012-06-01 12:52:13 -0700494 return ptr;
495}
496
497unsigned *atag_ptable(unsigned **ptr_addr)
498{
499 int i;
500 struct ptable *ptable;
501
502 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700503 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
504 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700505 *(*ptr_addr)++ = 0x4d534d70;
506 for (i = 0; i < ptable->count; ++i)
507 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
508 }
509
510 return (*ptr_addr);
511}
512
513unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
514{
515 int cmdline_length = 0;
516 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700517 char *dest;
518
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800519 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700520 n = (cmdline_length + 4) & (~3);
521
522 *ptr++ = (n / 4) + 2;
523 *ptr++ = 0x54410009;
524 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800525 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700526 ptr += (n / 4);
527
528 return ptr;
529}
530
531unsigned *atag_end(unsigned *ptr)
532{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800533 /* END */
534 *ptr++ = 0;
535 *ptr++ = 0;
536
Neeti Desaie245d492012-06-01 12:52:13 -0700537 return ptr;
538}
539
540void generate_atags(unsigned *ptr, const char *cmdline,
541 void *ramdisk, unsigned ramdisk_size)
542{
543
544 ptr = atag_core(ptr);
545 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
546 ptr = target_atag_mem(ptr);
547
548 /* Skip NAND partition ATAGS for eMMC boot */
549 if (!target_is_emmc_boot()){
550 ptr = atag_ptable(&ptr);
551 }
552
553 ptr = atag_cmdline(ptr, cmdline);
554 ptr = atag_end(ptr);
555}
556
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700557typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700558void boot_linux(void *kernel, unsigned *tags,
559 const char *cmdline, unsigned machtype,
560 void *ramdisk, unsigned ramdisk_size)
561{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800562 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800563#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700564 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800565#endif
566
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700567 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800568 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800569 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800570
571 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700572
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800573 final_cmdline = update_cmdline((const char*)cmdline);
574
Neeti Desai17379b82012-06-04 18:42:53 -0700575#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800576 dprintf(INFO, "Updating device tree: start\n");
577
Neeti Desai17379b82012-06-04 18:42:53 -0700578 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800579 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700580 if(ret)
581 {
582 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
583 ASSERT(0);
584 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800585 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700586#else
Neeti Desaie245d492012-06-01 12:52:13 -0700587 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800588 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700589#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700590
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700591 /* Perform target specific cleanup */
592 target_uninit();
593
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800594 /* Turn off splash screen if enabled */
595#if DISPLAY_SPLASH_SCREEN
596 target_display_shutdown();
597#endif
598
599
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800600 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
601 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800602
603 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700604
Amol Jadi4421e652011-06-16 15:00:48 -0700605 /* do any platform specific cleanup before kernel entry */
606 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700607
Brian Swetland9c4c0752009-01-25 16:23:50 -0800608 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700609
Amol Jadi504f9fe2012-08-16 13:56:48 -0700610#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800611 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700612#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700613 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800614
615 if (IS_ARM64(kptr))
616 /* Jump to a 64bit kernel */
617 scm_elexec_call((paddr_t)kernel, tags_phys);
618 else
619 /* Jump to a 32bit kernel */
620 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800621}
622
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700623/* Function to check if the memory address range falls within the aboot
624 * boundaries.
625 * start: Start of the memory region
626 * size: Size of the memory region
627 */
628int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
629{
630 /* Check for boundary conditions. */
631 if ((start + size) < start)
632 return -1;
633
634 /* Check for memory overlap. */
635 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
636 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700637 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700638 return 0;
639 else
640 return -1;
641}
642
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800643#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800644
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800645BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800646#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800647BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800648#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800649
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700650static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
651{
652 int ret;
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800653#if IMAGE_VERIF_ALGO_SHA1
654 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
655#else
656 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
657#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700658
659 /* Assume device is rooted at this time. */
660 device.is_tampered = 1;
661
662 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
663
664 ret = image_verify((unsigned char *)bootimg_addr,
665 (unsigned char *)(bootimg_addr + bootimg_size),
666 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800667 auth_algo);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700668
669 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
670
671 if (ret)
672 {
673 /* Authorized kernel */
674 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700675 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700676 }
677
678#if USE_PCOM_SECBOOT
679 set_tamper_flag(device.is_tampered);
680#endif
681
682 if(device.is_tampered)
683 {
684 write_device_info_mmc(&device);
685 #ifdef TZ_TAMPER_FUSE
686 set_tamper_fuse_cmd();
687 #endif
688 #ifdef ASSERT_ON_TAMPER
689 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
690 ASSERT(0);
691 #endif
692 }
693}
694
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530695static bool check_format_bit()
696{
697 bool ret = false;
698 int index;
699 uint64_t offset;
700 struct boot_selection_info *in = NULL;
701 char *buf = NULL;
702
703 index = partition_get_index("bootselect");
704 if (index == INVALID_PTN)
705 {
706 dprintf(INFO, "Unable to locate /bootselect partition\n");
707 return ret;
708 }
709 offset = partition_get_offset(index);
710 if(!offset)
711 {
712 dprintf(INFO, "partition /bootselect doesn't exist\n");
713 return ret;
714 }
715 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
716 ASSERT(buf);
717 if (mmc_read(offset, (unsigned int *)buf, page_size))
718 {
719 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
720 free(buf);
721 return ret;
722 }
723 in = (struct boot_selection_info *) buf;
724 if ((in->signature == BOOTSELECT_SIGNATURE) &&
725 (in->version == BOOTSELECT_VERSION)) {
726 if ((in->state_info & BOOTSELECT_FORMAT) &&
727 !(in->state_info & BOOTSELECT_FACTORY))
728 ret = true;
729 } else {
730 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
731 in->signature, in->version);
732 ASSERT(0);
733 }
734 free(buf);
735 return ret;
736}
737
Shashank Mittal23b8f422010-04-16 19:27:21 -0700738int boot_linux_from_mmc(void)
739{
740 struct boot_img_hdr *hdr = (void*) buf;
741 struct boot_img_hdr *uhdr;
742 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700743 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700744 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700745 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700746
Shashank Mittalcd98d472011-08-02 14:29:24 -0700747 unsigned char *image_addr = 0;
748 unsigned kernel_actual;
749 unsigned ramdisk_actual;
750 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700751 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700752
753#if DEVICE_TREE
754 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700755 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700756 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800757 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700758 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700759#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800760 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
761 struct kernel64_hdr *kptr = (void*) kbuf;
762
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530763 if (check_format_bit())
764 boot_into_recovery = 1;
765
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700766 if (!boot_into_recovery) {
767 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
768 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
769 if (rcode <= 0) {
770 boot_into_ffbm = false;
771 if (rcode < 0)
772 dprintf(CRITICAL,"failed to get ffbm cookie");
773 } else
774 boot_into_ffbm = true;
775 } else
776 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700777 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
778 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
779 dprintf(INFO, "Unified boot method!\n");
780 hdr = uhdr;
781 goto unified_boot;
782 }
Greg Griscod6250552011-06-29 14:40:23 -0700783 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700784 index = partition_get_index("boot");
785 ptn = partition_get_offset(index);
786 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700787 dprintf(CRITICAL, "ERROR: No boot partition found\n");
788 return -1;
789 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700790 }
791 else {
792 index = partition_get_index("recovery");
793 ptn = partition_get_offset(index);
794 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700795 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
796 return -1;
797 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700798 }
799
Greg Griscod6250552011-06-29 14:40:23 -0700800 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700801 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
802 return -1;
803 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700804
805 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700806 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700807 return -1;
808 }
809
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700810 if (hdr->page_size && (hdr->page_size != page_size)) {
811 page_size = hdr->page_size;
812 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700813 }
814
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800815 /* Read the next page to get kernel Image header
816 * which lives in the second page for arm64 targets.
817 */
818
819 if (mmc_read(ptn + page_size, (unsigned int *) kbuf, page_size)) {
820 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
821 return -1;
822 }
823
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700824 /*
825 * Update the kernel/ramdisk/tags address if the boot image header
826 * has default values, these default values come from mkbootimg when
827 * the boot image is flashed using fastboot flash:raw
828 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800829 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700830
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700831 /* Get virtual addresses since the hdr saves physical addresses. */
832 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
833 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
834 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700835
836 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
837 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
838
839 /* Check if the addresses in the header are valid. */
840 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
841 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
842 {
843 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
844 return -1;
845 }
846
847#ifndef DEVICE_TREE
848 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
849 {
850 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
851 return -1;
852 }
853#endif
854
Shashank Mittalcd98d472011-08-02 14:29:24 -0700855 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300856 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
857 (int) target_use_signed_kernel(),
858 device.is_unlocked,
859 device.is_tampered);
860
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700861 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800862 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700863 offset = 0;
864
Shashank Mittalcd98d472011-08-02 14:29:24 -0700865 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700866
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800867#if DEVICE_TREE
868 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
869 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700870
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700871 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700872 {
873 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
874 return -1;
875 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800876#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530877 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700878
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700879#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700880
Amol Jadib6be5c12012-11-14 13:39:51 -0800881 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700882 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800883
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700884 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
885 {
886 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
887 return -1;
888 }
889
Shashank Mittalcd98d472011-08-02 14:29:24 -0700890 /* Read image without signature */
891 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
892 {
893 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
894 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800895 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700896
Amol Jadib6be5c12012-11-14 13:39:51 -0800897 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700898 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800899
Shashank Mittalcd98d472011-08-02 14:29:24 -0700900 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700901
902 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
903 {
904 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
905 return -1;
906 }
907
Shashank Mittalcd98d472011-08-02 14:29:24 -0700908 /* Read signature */
909 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
910 {
911 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700912 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700913 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800914
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700915 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700916
Neeti Desai465491e2012-07-31 12:53:35 -0700917 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700918 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
919 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700920
Neeti Desai465491e2012-07-31 12:53:35 -0700921 #if DEVICE_TREE
922 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700923 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700924 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700925
Deepa Dinamani19648b42013-09-05 17:05:55 -0700926 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700927 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
928 return -1;
929 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700930
Neeti Desai465491e2012-07-31 12:53:35 -0700931 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700932 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700933 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
934 return -1;
935 }
936
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700937 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700938 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700939 {
940 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
941 return -1;
942 }
943
Joel Kingaa335dc2013-06-03 16:11:08 -0700944 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800945 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800946 /*
947 * If appended dev tree is found, update the atags with
948 * memory address to the DTB appended location on RAM.
949 * Else update with the atags address in the kernel header
950 */
951 void *dtb;
952 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700953 hdr->kernel_size,
954 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800955 if (!dtb) {
956 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
957 return -1;
958 }
Neeti Desai465491e2012-07-31 12:53:35 -0700959 }
960 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700961 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700962 else
963 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800964 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700965
Amol Jadib6be5c12012-11-14 13:39:51 -0800966 dprintf(INFO, "Loading boot image (%d): start\n",
967 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700968 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800969
970 offset = page_size;
971
972 /* Load kernel */
973 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700974 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
975 return -1;
976 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800977 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700978
Amol Jadib6be5c12012-11-14 13:39:51 -0800979 /* Load ramdisk */
980 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700981 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800982 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700983 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
984 return -1;
985 }
986 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800987 offset += ramdisk_actual;
988
989 dprintf(INFO, "Loading boot image (%d): done\n",
990 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700991 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700992
993 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800994 offset += second_actual;
995 /* Second image loading not implemented. */
996 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700997 }
998
999 #if DEVICE_TREE
1000 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -07001001 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -07001002 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
1003 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1004 return -1;
1005 }
1006 table = (struct dt_table*) dt_buf;
1007
Deepa Dinamani19648b42013-09-05 17:05:55 -07001008 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001009 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1010 return -1;
1011 }
1012
Deepa Dinamani19648b42013-09-05 17:05:55 -07001013 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1014 if (!table)
1015 return -1;
1016
1017 /* Read the entire device tree table into buffer */
1018 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
1019 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1020 return -1;
1021 }
1022
Joel Kingaa335dc2013-06-03 16:11:08 -07001023 /* Find index of device tree within device tree table */
1024 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001025 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1026 return -1;
1027 }
1028
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001029 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001030 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001031 {
1032 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1033 return -1;
1034 }
1035
Joel Kingaa335dc2013-06-03 16:11:08 -07001036 if(mmc_read(ptn + offset + dt_entry.offset,
1037 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -07001038 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1039 return -1;
1040 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +03001041 #ifdef TZ_SAVE_KERNEL_HASH
1042 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
1043 hdr->ramdisk_addr, ramdisk_actual,
1044 ptn, offset, hdr->dt_size);
1045 #endif /* TZ_SAVE_KERNEL_HASH */
1046
Channagoud Kadabi35095622013-03-01 13:53:05 -08001047 } else {
Aparna Mallavarapu33127372014-06-03 22:15:54 +05301048
1049 /* Validate the tags_addr */
1050 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1051 {
1052 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1053 return -1;
1054 }
Dima Zavin77e41f32013-03-06 16:10:43 -08001055 /*
1056 * If appended dev tree is found, update the atags with
1057 * memory address to the DTB appended location on RAM.
1058 * Else update with the atags address in the kernel header
1059 */
1060 void *dtb;
1061 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001062 kernel_actual,
1063 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001064 if (!dtb) {
1065 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1066 return -1;
1067 }
Neeti Desai465491e2012-07-31 12:53:35 -07001068 }
1069 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001070 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001071
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001072 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1073 target_load_ssd_keystore();
1074
Shashank Mittal23b8f422010-04-16 19:27:21 -07001075unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001076
Dima Zavin77e41f32013-03-06 16:10:43 -08001077 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001078 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001079 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1080
1081 return 0;
1082}
1083
Dima Zavin214cc642009-01-26 11:16:21 -08001084int boot_linux_from_flash(void)
1085{
1086 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001087 struct ptentry *ptn;
1088 struct ptable *ptable;
1089 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001090
Shashank Mittalcd98d472011-08-02 14:29:24 -07001091 unsigned char *image_addr = 0;
1092 unsigned kernel_actual;
1093 unsigned ramdisk_actual;
1094 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001095 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001096
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001097#if DEVICE_TREE
1098 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001099 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001100 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001101 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001102#endif
1103
David Ng183a7422009-12-07 14:55:21 -08001104 if (target_is_emmc_boot()) {
1105 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1106 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1107 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1108 return -1;
1109 }
1110 goto continue_boot;
1111 }
1112
Dima Zavin214cc642009-01-26 11:16:21 -08001113 ptable = flash_get_ptable();
1114 if (ptable == NULL) {
1115 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1116 return -1;
1117 }
1118
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001119 if(!boot_into_recovery)
1120 {
1121 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001122
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001123 if (ptn == NULL) {
1124 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1125 return -1;
1126 }
1127 }
1128 else
1129 {
1130 ptn = ptable_find(ptable, "recovery");
1131 if (ptn == NULL) {
1132 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1133 return -1;
1134 }
Dima Zavin214cc642009-01-26 11:16:21 -08001135 }
1136
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001137 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001138 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1139 return -1;
1140 }
Dima Zavin214cc642009-01-26 11:16:21 -08001141
1142 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001143 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001144 return -1;
1145 }
1146
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001147 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001148 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 -08001149 return -1;
1150 }
1151
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001152 /*
1153 * Update the kernel/ramdisk/tags address if the boot image header
1154 * has default values, these default values come from mkbootimg when
1155 * the boot image is flashed using fastboot flash:raw
1156 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001157 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001158
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001159 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001160 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1161 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1162 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1163
1164 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1165 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1166
1167 /* Check if the addresses in the header are valid. */
1168 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1169 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1170 {
1171 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1172 return -1;
1173 }
1174
1175#ifndef DEVICE_TREE
1176 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1177 {
1178 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1179 return -1;
1180 }
1181#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001182
Shashank Mittalcd98d472011-08-02 14:29:24 -07001183 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001184 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001185 {
1186 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001187 offset = 0;
1188
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001189#if DEVICE_TREE
1190 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1191 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001192
1193 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1194 {
1195 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1196 return -1;
1197 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001198#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001199 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001200#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001201
Amol Jadib6be5c12012-11-14 13:39:51 -08001202 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001203 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001204
Shashank Mittalcd98d472011-08-02 14:29:24 -07001205 /* Read image without signature */
1206 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1207 {
1208 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1209 return -1;
1210 }
Dima Zavin214cc642009-01-26 11:16:21 -08001211
Amol Jadib6be5c12012-11-14 13:39:51 -08001212 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001213 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001214
Shashank Mittalcd98d472011-08-02 14:29:24 -07001215 offset = imagesize_actual;
1216 /* Read signature */
1217 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1218 {
1219 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001220 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001221 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001222
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001223 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001224
1225 /* Move kernel and ramdisk to correct address */
1226 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1227 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001228#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001229 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001230 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001231 {
1232 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1233 return -1;
1234 }
1235
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001236 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1237#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001238
1239 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001240 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001241 {
1242 write_device_info_flash(&device);
1243 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301244#if USE_PCOM_SECBOOT
1245 set_tamper_flag(device.is_tampered);
1246#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001247 }
1248 else
1249 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001250 offset = page_size;
1251
Amol Jadib6be5c12012-11-14 13:39:51 -08001252 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1253 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1254 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1255
1256 dprintf(INFO, "Loading boot image (%d): start\n",
1257 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001258 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001259
1260 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001261 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1262 return -1;
1263 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001264 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001265
Amol Jadib6be5c12012-11-14 13:39:51 -08001266 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001267 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1268 return -1;
1269 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001270 offset += ramdisk_actual;
1271
1272 dprintf(INFO, "Loading boot image (%d): done\n",
1273 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001274 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001275
1276 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001277 offset += second_actual;
1278 /* Second image loading not implemented. */
1279 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001280 }
1281
1282#if DEVICE_TREE
1283 if(hdr->dt_size != 0) {
1284
1285 /* Read the device tree table into buffer */
1286 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1287 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1288 return -1;
1289 }
1290
1291 table = (struct dt_table*) dt_buf;
1292
Deepa Dinamani19648b42013-09-05 17:05:55 -07001293 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001294 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1295 return -1;
1296 }
1297
Deepa Dinamani19648b42013-09-05 17:05:55 -07001298 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1299 if (!table)
1300 return -1;
1301
1302 /* Read the entire device tree table into buffer */
1303 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1304 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1305 return -1;
1306 }
1307
1308
Joel Kingaa335dc2013-06-03 16:11:08 -07001309 /* Find index of device tree within device tree table */
1310 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001311 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1312 return -1;
1313 }
1314
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001315 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001316 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001317 {
1318 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1319 return -1;
1320 }
1321
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001322 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001323 if(flash_read(ptn, offset + dt_entry.offset,
1324 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001325 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1326 return -1;
1327 }
1328 }
1329#endif
1330
Shashank Mittalcd98d472011-08-02 14:29:24 -07001331 }
David Ng183a7422009-12-07 14:55:21 -08001332continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001333
Dima Zavin214cc642009-01-26 11:16:21 -08001334 /* TODO: create/pass atags to kernel */
1335
Ajay Dudanie28a6072011-07-01 13:59:46 -07001336 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001337 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001338 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1339
1340 return 0;
1341}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001342
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001343BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001344void write_device_info_mmc(device_info *dev)
1345{
1346 struct device_info *info = (void*) info_buf;
1347 unsigned long long ptn = 0;
1348 unsigned long long size;
1349 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001350 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001351 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001352
1353 index = partition_get_index("aboot");
1354 ptn = partition_get_offset(index);
1355 if(ptn == 0)
1356 {
1357 return;
1358 }
1359
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001360 lun = partition_get_lun(index);
1361 mmc_set_lun(lun);
1362
Shashank Mittal162244e2011-08-08 19:01:25 -07001363 size = partition_get_size(index);
1364
1365 memcpy(info, dev, sizeof(device_info));
1366
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001367 blocksize = mmc_get_device_blocksize();
1368
1369 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Shashank Mittal162244e2011-08-08 19:01:25 -07001370 {
1371 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1372 return;
1373 }
1374}
1375
1376void read_device_info_mmc(device_info *dev)
1377{
1378 struct device_info *info = (void*) info_buf;
1379 unsigned long long ptn = 0;
1380 unsigned long long size;
1381 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001382 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001383
1384 index = partition_get_index("aboot");
1385 ptn = partition_get_offset(index);
1386 if(ptn == 0)
1387 {
1388 return;
1389 }
1390
1391 size = partition_get_size(index);
1392
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001393 blocksize = mmc_get_device_blocksize();
1394
1395 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Shashank Mittal162244e2011-08-08 19:01:25 -07001396 {
1397 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1398 return;
1399 }
1400
1401 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1402 {
1403 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1404 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001405 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001406 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001407
1408 write_device_info_mmc(info);
1409 }
1410 memcpy(dev, info, sizeof(device_info));
1411}
1412
1413void write_device_info_flash(device_info *dev)
1414{
1415 struct device_info *info = (void *) info_buf;
1416 struct ptentry *ptn;
1417 struct ptable *ptable;
1418
1419 ptable = flash_get_ptable();
1420 if (ptable == NULL)
1421 {
1422 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1423 return;
1424 }
1425
1426 ptn = ptable_find(ptable, "devinfo");
1427 if (ptn == NULL)
1428 {
1429 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1430 return;
1431 }
1432
1433 memcpy(info, dev, sizeof(device_info));
1434
1435 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1436 {
1437 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1438 return;
1439 }
1440}
1441
1442void read_device_info_flash(device_info *dev)
1443{
1444 struct device_info *info = (void*) info_buf;
1445 struct ptentry *ptn;
1446 struct ptable *ptable;
1447
1448 ptable = flash_get_ptable();
1449 if (ptable == NULL)
1450 {
1451 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1452 return;
1453 }
1454
1455 ptn = ptable_find(ptable, "devinfo");
1456 if (ptn == NULL)
1457 {
1458 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1459 return;
1460 }
1461
1462 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1463 {
1464 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1465 return;
1466 }
1467
1468 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1469 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001470 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1471 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001472 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001473 write_device_info_flash(info);
1474 }
1475 memcpy(dev, info, sizeof(device_info));
1476}
1477
1478void write_device_info(device_info *dev)
1479{
1480 if(target_is_emmc_boot())
1481 {
1482 write_device_info_mmc(dev);
1483 }
1484 else
1485 {
1486 write_device_info_flash(dev);
1487 }
1488}
1489
1490void read_device_info(device_info *dev)
1491{
1492 if(target_is_emmc_boot())
1493 {
1494 read_device_info_mmc(dev);
1495 }
1496 else
1497 {
1498 read_device_info_flash(dev);
1499 }
1500}
1501
1502void reset_device_info()
1503{
1504 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001505 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001506 write_device_info(&device);
1507}
1508
1509void set_device_root()
1510{
1511 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001512 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001513 write_device_info(&device);
1514}
1515
Amol Jadicb524072012-08-09 16:40:18 -07001516#if DEVICE_TREE
1517int copy_dtb(uint8_t *boot_image_start)
1518{
1519 uint32 dt_image_offset = 0;
1520 uint32_t n;
1521 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001522 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001523 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001524
1525 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1526
Amol Jadicb524072012-08-09 16:40:18 -07001527 if(hdr->dt_size != 0) {
1528
1529 /* add kernel offset */
1530 dt_image_offset += page_size;
1531 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1532 dt_image_offset += n;
1533
1534 /* add ramdisk offset */
1535 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1536 dt_image_offset += n;
1537
1538 /* add second offset */
1539 if(hdr->second_size != 0) {
1540 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1541 dt_image_offset += n;
1542 }
1543
1544 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001545 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001546
Deepa Dinamani19648b42013-09-05 17:05:55 -07001547 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001548 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1549 return -1;
1550 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001551 /* Find index of device tree within device tree table */
1552 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001553 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1554 return -1;
1555 }
1556
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001557 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001558 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001559 {
1560 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1561 return -1;
1562 }
1563
Amol Jadicb524072012-08-09 16:40:18 -07001564 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001565 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001566 boot_image_start + dt_image_offset + dt_entry.offset,
1567 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001568 } else
1569 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001570
1571 /* Everything looks fine. Return success. */
1572 return 0;
1573}
1574#endif
1575
Brian Swetland9c4c0752009-01-25 16:23:50 -08001576void cmd_boot(const char *arg, void *data, unsigned sz)
1577{
1578 unsigned kernel_actual;
1579 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001580 uint32_t image_actual;
1581 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001582 uint32_t sig_actual = SIGNATURE_SIZE;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001583 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001584 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001585 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001586 int ret = 0;
1587 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001588
1589 if (sz < sizeof(hdr)) {
1590 fastboot_fail("invalid bootimage header");
1591 return;
1592 }
1593
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001594 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001595
1596 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001597 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001598
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001599 if(target_is_emmc_boot() && hdr->page_size) {
1600 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001601 page_mask = page_size - 1;
1602 }
1603
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001604 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1605 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001606#if DEVICE_TREE
1607 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1608#endif
1609
1610 image_actual = ADD_OF(page_size, kernel_actual);
1611 image_actual = ADD_OF(image_actual, ramdisk_actual);
1612 image_actual = ADD_OF(image_actual, dt_actual);
1613
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001614 if (target_use_signed_kernel() && (!device.is_unlocked))
1615 image_actual = ADD_OF(image_actual, sig_actual);
1616
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001617 /* sz should have atleast raw boot image */
1618 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001619 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001620 return;
1621 }
1622
1623 /* Verify the boot image
1624 * device & page_size are initialized in aboot_init
1625 */
1626 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001627 /* Pass size excluding signature size, otherwise we would try to
1628 * access signature beyond its length
1629 */
1630 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001631
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001632 /*
1633 * Update the kernel/ramdisk/tags address if the boot image header
1634 * has default values, these default values come from mkbootimg when
1635 * the boot image is flashed using fastboot flash:raw
1636 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001637 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1638 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001639
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001640 /* Get virtual addresses since the hdr saves physical addresses. */
1641 hdr->kernel_addr = VA(hdr->kernel_addr);
1642 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1643 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001644
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001645 /* Check if the addresses in the header are valid. */
1646 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1647 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1648 {
1649 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001650 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001651 }
1652
Amol Jadicb524072012-08-09 16:40:18 -07001653#if DEVICE_TREE
1654 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001655 ret = copy_dtb(data);
1656
1657 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001658#else
1659 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1660 {
1661 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001662 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001663 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001664#endif
1665
1666 /* Load ramdisk & kernel */
1667 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1668 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1669
1670#if DEVICE_TREE
1671 /*
1672 * If dtb is not found look for appended DTB in the kernel.
1673 * If appended dev tree is found, update the atags with
1674 * memory address to the DTB appended location on RAM.
1675 * Else update with the atags address in the kernel header
1676 */
1677 if (!dtb_copied) {
1678 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001679 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1680 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001681 if (!dtb) {
1682 fastboot_fail("dtb not found");
1683 return;
1684 }
Amol Jadicb524072012-08-09 16:40:18 -07001685 }
1686#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001687
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001688#ifndef DEVICE_TREE
1689 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1690 {
1691 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001692 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001693 }
1694#endif
1695
Brian Swetland9c4c0752009-01-25 16:23:50 -08001696 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001697 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001698
Dima Zavin77e41f32013-03-06 16:10:43 -08001699 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001700 (const char*) hdr->cmdline, board_machtype(),
1701 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001702}
1703
Dima Zavin214cc642009-01-26 11:16:21 -08001704void cmd_erase(const char *arg, void *data, unsigned sz)
1705{
1706 struct ptentry *ptn;
1707 struct ptable *ptable;
1708
1709 ptable = flash_get_ptable();
1710 if (ptable == NULL) {
1711 fastboot_fail("partition table doesn't exist");
1712 return;
1713 }
1714
1715 ptn = ptable_find(ptable, arg);
1716 if (ptn == NULL) {
1717 fastboot_fail("unknown partition name");
1718 return;
1719 }
1720
1721 if (flash_erase(ptn)) {
1722 fastboot_fail("failed to erase partition");
1723 return;
1724 }
1725 fastboot_okay("");
1726}
1727
Bikas Gurungd48bd242010-09-04 19:54:32 -07001728
1729void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1730{
Kun Liang2f1601a2013-08-12 16:29:54 +08001731 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001732 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001733 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001734 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001735 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001736
Kinson Chikf1a43512011-07-14 11:28:39 -07001737 index = partition_get_index(arg);
1738 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001739 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001740
Kinson Chikf1a43512011-07-14 11:28:39 -07001741 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001742 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001743 return;
1744 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001745
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001746 lun = partition_get_lun(index);
1747 mmc_set_lun(lun);
1748
Oliver Wangcee448d2013-10-22 18:40:13 +08001749#if MMC_SDHCI_SUPPORT
1750 if (mmc_erase_card(ptn, size)) {
1751 fastboot_fail("failed to erase partition\n");
1752 return;
1753 }
1754#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001755 size = partition_get_size(index);
1756 if (size > DEFAULT_ERASE_SIZE)
1757 size = DEFAULT_ERASE_SIZE;
1758
neetidb4b24d62012-01-20 12:13:09 -08001759 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001760 0 in first several blocks */
1761 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001762 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001763 return;
1764 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001765#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001766 fastboot_okay("");
1767}
1768
1769
Ajay Dudani5c761132011-04-07 20:19:04 -07001770void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001771{
1772 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001773 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001774 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001775 char *token = NULL;
1776 char *pname = NULL;
1777 uint8_t lun = 0;
1778 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001779
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001780 token = strtok(arg, ":");
1781 pname = token;
1782 token = strtok(NULL, ":");
1783 if(token)
1784 {
1785 lun = atoi(token);
1786 mmc_set_lun(lun);
1787 lun_set = true;
1788 }
1789
1790 if (!strcmp(pname, "partition"))
Greg Grisco6e754772011-06-23 12:19:39 -07001791 {
1792 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001793 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001794 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001795 return;
1796 }
1797 }
Greg Grisco6e754772011-06-23 12:19:39 -07001798 else
1799 {
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001800 index = partition_get_index(pname);
Kinson Chikf1a43512011-07-14 11:28:39 -07001801 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001802 if(ptn == 0) {
1803 fastboot_fail("partition table doesn't exist");
1804 return;
1805 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001806
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001807 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
Greg Grisco6e754772011-06-23 12:19:39 -07001808 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1809 fastboot_fail("image is not a boot image");
1810 return;
1811 }
1812 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001813
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001814 if(!lun_set)
1815 {
1816 lun = partition_get_lun(index);
1817 mmc_set_lun(lun);
1818 }
1819
Kinson Chikf1a43512011-07-14 11:28:39 -07001820 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001821 if (ROUND_TO_PAGE(sz,511) > size) {
1822 fastboot_fail("size too large");
1823 return;
1824 }
1825 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1826 fastboot_fail("flash write failure");
1827 return;
1828 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001829 }
1830 fastboot_okay("");
1831 return;
1832}
1833
Ajay Dudani5c761132011-04-07 20:19:04 -07001834void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1835{
1836 unsigned int chunk;
1837 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001838 uint32_t *fill_buf = NULL;
1839 uint32_t fill_val;
1840 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001841 sparse_header_t *sparse_header;
1842 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001843 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001844 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301845 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001846 int index = INVALID_PTN;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001847 int i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001848 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001849
Kinson Chikf1a43512011-07-14 11:28:39 -07001850 index = partition_get_index(arg);
1851 ptn = partition_get_offset(index);
1852 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001853 fastboot_fail("partition table doesn't exist");
1854 return;
1855 }
1856
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301857 size = partition_get_size(index);
1858 if (ROUND_TO_PAGE(sz,511) > size) {
1859 fastboot_fail("size too large");
1860 return;
1861 }
1862
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001863 lun = partition_get_lun(index);
1864 mmc_set_lun(lun);
1865
Ajay Dudani5c761132011-04-07 20:19:04 -07001866 /* Read and skip over sparse image header */
1867 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001868 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1869 fastboot_fail("size too large");
1870 return;
1871 }
1872
Ajay Dudani5c761132011-04-07 20:19:04 -07001873 data += sparse_header->file_hdr_sz;
1874 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1875 {
1876 /* Skip the remaining bytes in a header that is longer than
1877 * we expected.
1878 */
1879 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1880 }
1881
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001882 dprintf (SPEW, "=== Sparse Image Header ===\n");
1883 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1884 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1885 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1886 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1887 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1888 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1889 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1890 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001891
1892 /* Start processing chunks */
1893 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1894 {
1895 /* Read and skip over chunk header */
1896 chunk_header = (chunk_header_t *) data;
1897 data += sizeof(chunk_header_t);
1898
1899 dprintf (SPEW, "=== Chunk Header ===\n");
1900 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1901 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1902 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1903
1904 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1905 {
1906 /* Skip the remaining bytes in a header that is longer than
1907 * we expected.
1908 */
1909 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1910 }
1911
1912 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1913 switch (chunk_header->chunk_type)
1914 {
1915 case CHUNK_TYPE_RAW:
1916 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1917 chunk_data_sz))
1918 {
1919 fastboot_fail("Bogus chunk size for chunk type Raw");
1920 return;
1921 }
1922
Ajay Dudaniab18f022011-05-12 14:39:22 -07001923 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1924 chunk_data_sz,
1925 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001926 {
1927 fastboot_fail("flash write failure");
1928 return;
1929 }
1930 total_blocks += chunk_header->chunk_sz;
1931 data += chunk_data_sz;
1932 break;
1933
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001934 case CHUNK_TYPE_FILL:
1935 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1936 sizeof(uint32_t)))
1937 {
1938 fastboot_fail("Bogus chunk size for chunk type FILL");
1939 return;
1940 }
1941
1942 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
1943 if (!fill_buf)
1944 {
1945 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
1946 return;
1947 }
1948
1949 fill_val = *(uint32_t *)data;
1950 data = (char *) data + sizeof(uint32_t);
1951 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
1952
1953 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
1954 {
1955 fill_buf[i] = fill_val;
1956 }
1957
1958 for (i = 0; i < chunk_blk_cnt; i++)
1959 {
1960 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1961 sparse_header->blk_sz,
1962 fill_buf))
1963 {
1964 fastboot_fail("flash write failure");
1965 free(fill_buf);
1966 return;
1967 }
1968
1969 total_blocks++;
1970 }
1971
1972 free(fill_buf);
1973 break;
1974
Ajay Dudani5c761132011-04-07 20:19:04 -07001975 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001976 total_blocks += chunk_header->chunk_sz;
1977 break;
1978
Ajay Dudani5c761132011-04-07 20:19:04 -07001979 case CHUNK_TYPE_CRC:
1980 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1981 {
1982 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1983 return;
1984 }
1985 total_blocks += chunk_header->chunk_sz;
1986 data += chunk_data_sz;
1987 break;
1988
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001989 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001990 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07001991 fastboot_fail("Unknown chunk type");
1992 return;
1993 }
1994 }
1995
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001996 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1997 total_blocks, sparse_header->total_blks);
1998
1999 if(total_blocks != sparse_header->total_blks)
2000 {
2001 fastboot_fail("sparse image write failure");
2002 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002003
2004 fastboot_okay("");
2005 return;
2006}
2007
2008void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2009{
2010 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002011 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2012 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002013
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002014#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002015 int ret=0;
2016 uint32 major_version=0;
2017 uint32 minor_version=0;
2018
2019 ret = scm_svc_version(&major_version,&minor_version);
2020 if(!ret)
2021 {
2022 if(major_version >= 2)
2023 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002024 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002025 {
2026 ret = encrypt_scm((uint32 **) &data, &sz);
2027 if (ret != 0) {
2028 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2029 return;
2030 }
2031
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002032 /* Protect only for SSD */
2033 if (!strcmp(arg, "ssd")) {
2034 ret = scm_protect_keystore((uint32 *) data, sz);
2035 if (ret != 0) {
2036 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2037 return;
2038 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002039 }
2040 }
2041 else
2042 {
2043 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2044 if(ret != 0)
2045 {
2046 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2047 return;
2048 }
2049 }
2050 }
2051 else
2052 {
2053 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2054 magic_number[1] == DECRYPT_MAGIC_1)
2055 {
2056 ret = decrypt_scm((uint32 **) &data, &sz);
2057 if (ret != 0) {
2058 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2059 return;
2060 }
2061 }
2062 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2063 magic_number[1] == ENCRYPT_MAGIC_1)
2064 {
2065 ret = encrypt_scm((uint32 **) &data, &sz);
2066 if (ret != 0) {
2067 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2068 return;
2069 }
2070 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002071 }
2072 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002073 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002074 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002075 dprintf(CRITICAL,"INVALID SVC Version\n");
2076 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002077 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002078#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002079
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002080 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002081 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
2082 cmd_flash_mmc_img(arg, data, sz);
2083 else
2084 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002085 return;
2086}
2087
Dima Zavin214cc642009-01-26 11:16:21 -08002088void cmd_flash(const char *arg, void *data, unsigned sz)
2089{
2090 struct ptentry *ptn;
2091 struct ptable *ptable;
2092 unsigned extra = 0;
2093
2094 ptable = flash_get_ptable();
2095 if (ptable == NULL) {
2096 fastboot_fail("partition table doesn't exist");
2097 return;
2098 }
2099
2100 ptn = ptable_find(ptable, arg);
2101 if (ptn == NULL) {
2102 fastboot_fail("unknown partition name");
2103 return;
2104 }
2105
2106 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2107 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2108 fastboot_fail("image is not a boot image");
2109 return;
2110 }
2111 }
2112
Amol Jadi5c61a952012-05-04 17:05:35 -07002113 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002114 || !strcmp(ptn->name, "userdata")
2115 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002116 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002117 || !strcmp(ptn->name, "modem"))
2118 {
2119 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07002120 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002121 else
2122 extra = 0;
2123 }
2124 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002125 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002126
2127 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2128 if (flash_write(ptn, extra, data, sz)) {
2129 fastboot_fail("flash write failure");
2130 return;
2131 }
2132 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2133 fastboot_okay("");
2134}
2135
2136void cmd_continue(const char *arg, void *data, unsigned sz)
2137{
2138 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002139 fastboot_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002140 if (target_is_emmc_boot())
2141 {
2142 boot_linux_from_mmc();
2143 }
2144 else
2145 {
2146 boot_linux_from_flash();
2147 }
Dima Zavin214cc642009-01-26 11:16:21 -08002148}
2149
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002150void cmd_reboot(const char *arg, void *data, unsigned sz)
2151{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002152 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002153 fastboot_okay("");
2154 reboot_device(0);
2155}
2156
2157void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2158{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002159 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002160 fastboot_okay("");
2161 reboot_device(FASTBOOT_MODE);
2162}
2163
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002164void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2165{
2166 dprintf(INFO, "Enabling charger screen check\n");
2167 device.charger_screen_enabled = 1;
2168 write_device_info(&device);
2169 fastboot_okay("");
2170}
2171
2172void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2173{
2174 dprintf(INFO, "Disabling charger screen check\n");
2175 device.charger_screen_enabled = 0;
2176 write_device_info(&device);
2177 fastboot_okay("");
2178}
2179
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302180void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2181{
2182 dprintf(INFO, "Selecting display panel %s\n", arg);
2183 if (arg)
2184 strlcpy(device.display_panel, arg,
2185 sizeof(device.display_panel));
2186 write_device_info(&device);
2187 fastboot_okay("");
2188}
2189
Shashank Mittal162244e2011-08-08 19:01:25 -07002190void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2191{
2192 if(!device.is_unlocked)
2193 {
2194 device.is_unlocked = 1;
2195 write_device_info(&device);
2196 }
2197 fastboot_okay("");
2198}
2199
Shashank Mittala0032282011-08-26 14:50:11 -07002200void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2201{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302202 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002203 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002204 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002205 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2206 fastboot_info(response);
2207 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002208 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302209 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2210 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002211 fastboot_okay("");
2212}
2213
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002214void cmd_preflash(const char *arg, void *data, unsigned sz)
2215{
2216 fastboot_okay("");
2217}
2218
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302219static struct fbimage logo_header = {0};
2220struct fbimage* splash_screen_flash();
2221
2222int splash_screen_check_header(struct fbimage *logo)
2223{
2224 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2225 return -1;
2226 if (logo->header.width == 0 || logo->header.height == 0)
2227 return -1;
2228 return 0;
2229}
2230
2231struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002232{
2233 struct ptentry *ptn;
2234 struct ptable *ptable;
2235 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302236 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002237
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302238
2239 ptable = flash_get_ptable();
2240 if (ptable == NULL) {
2241 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2242 return NULL;
2243 }
2244 ptn = ptable_find(ptable, "splash");
2245 if (ptn == NULL) {
2246 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2247 return NULL;
2248 }
2249
2250 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2251 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2252 return NULL;
2253 }
2254
2255 if (splash_screen_check_header(logo)) {
2256 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2257 return NULL;
2258 }
2259
2260 fb_display = fbcon_display();
2261 if (fb_display) {
2262 uint8_t *base = (uint8_t *) fb_display->base;
2263 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
2264 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002265 }
2266
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302267 if (flash_read(ptn + sizeof(logo->header), 0,
2268 base,
2269 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2270 fbcon_clear();
2271 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2272 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002273 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302274 logo->image = base;
2275 }
2276
2277 return logo;
2278}
2279
2280struct fbimage* splash_screen_mmc()
2281{
2282 int index = INVALID_PTN;
2283 unsigned long long ptn = 0;
2284 struct fbcon_config *fb_display = NULL;
2285 struct fbimage *logo = &logo_header;
2286
2287 index = partition_get_index("splash");
2288 if (index == 0) {
2289 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2290 return NULL;
2291 }
2292
2293 ptn = partition_get_offset(index);
2294 if (ptn == 0) {
2295 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2296 return NULL;
2297 }
2298
2299 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2300 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2301 return NULL;
2302 }
2303
2304 if (splash_screen_check_header(logo)) {
2305 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2306 return NULL;
2307 }
2308
2309 fb_display = fbcon_display();
2310 if (fb_display) {
2311 uint8_t *base = (uint8_t *) fb_display->base;
2312 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2313 base += LOGO_IMG_OFFSET;
2314
2315 if (mmc_read(ptn + sizeof(logo->header),
2316 base,
2317 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2318 fbcon_clear();
2319 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2320 return NULL;
2321 }
2322
2323 logo->image = base;
2324 }
2325
2326 return logo;
2327}
2328
2329
2330struct fbimage* fetch_image_from_partition()
2331{
2332 if (target_is_emmc_boot()) {
2333 return splash_screen_mmc();
2334 } else {
2335 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002336 }
2337}
2338
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002339/* Get the size from partiton name */
2340static void get_partition_size(const char *arg, char *response)
2341{
2342 uint64_t ptn = 0;
2343 uint64_t size;
2344 int index = INVALID_PTN;
2345
2346 index = partition_get_index(arg);
2347
2348 if (index == INVALID_PTN)
2349 {
2350 dprintf(CRITICAL, "Invalid partition index\n");
2351 return;
2352 }
2353
2354 ptn = partition_get_offset(index);
2355
2356 if(!ptn)
2357 {
2358 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2359 return;
2360 }
2361
2362 size = partition_get_size(index);
2363
2364 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2365 return;
2366}
2367
2368/*
2369 * Publish the partition type & size info
2370 * fastboot getvar will publish the required information.
2371 * fastboot getvar partition_size:<partition_name>: partition size in hex
2372 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2373 */
2374static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2375{
2376 uint8_t i;
2377
2378 for (i = 0; i < num_parts; i++) {
2379 get_partition_size(info[i].part_name, info[i].size_response);
2380
2381 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2382 {
2383 dprintf(CRITICAL, "partition size name truncated\n");
2384 return;
2385 }
2386 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2387 {
2388 dprintf(CRITICAL, "partition type name truncated\n");
2389 return;
2390 }
2391
2392 /* publish partition size & type info */
2393 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2394 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2395 }
2396}
2397
Amol Jadi5edf3552013-07-23 14:15:34 -07002398/* register commands and variables for fastboot */
2399void aboot_fastboot_register_commands(void)
2400{
2401 if (target_is_emmc_boot())
2402 {
2403 fastboot_register("flash:", cmd_flash_mmc);
2404 fastboot_register("erase:", cmd_erase_mmc);
2405 }
2406 else
2407 {
2408 fastboot_register("flash:", cmd_flash);
2409 fastboot_register("erase:", cmd_erase);
2410 }
2411
2412 fastboot_register("boot", cmd_boot);
2413 fastboot_register("continue", cmd_continue);
2414 fastboot_register("reboot", cmd_reboot);
2415 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2416 fastboot_register("oem unlock", cmd_oem_unlock);
2417 fastboot_register("oem device-info", cmd_oem_devinfo);
2418 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002419 fastboot_register("oem enable-charger-screen",
2420 cmd_oem_enable_charger_screen);
2421 fastboot_register("oem disable-charger-screen",
2422 cmd_oem_disable_charger_screen);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302423 fastboot_register("oem select-display-panel",
2424 cmd_oem_select_display_panel);
Amol Jadi5edf3552013-07-23 14:15:34 -07002425 /* publish variables and their values */
2426 fastboot_publish("product", TARGET(BOARD));
2427 fastboot_publish("kernel", "lk");
2428 fastboot_publish("serialno", sn_buf);
2429
2430 /*
2431 * partition info is supported only for emmc partitions
2432 * Calling this for NAND prints some error messages which
2433 * is harmless but misleading. Avoid calling this for NAND
2434 * devices.
2435 */
2436 if (target_is_emmc_boot())
2437 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2438
2439 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002440 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2441 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002442 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002443 /* Is the charger screen check enabled */
2444 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2445 device.charger_screen_enabled);
2446 fastboot_publish("charger-screen-enabled",
2447 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302448 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2449 device.display_panel);
2450 fastboot_publish("display-panel",
2451 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002452}
2453
Brian Swetland9c4c0752009-01-25 16:23:50 -08002454void aboot_init(const struct app_descriptor *app)
2455{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002456 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002457 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002458
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002459 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002460 if (target_is_emmc_boot())
2461 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002462 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002463 page_mask = page_size - 1;
2464 }
2465 else
2466 {
2467 page_size = flash_page_size();
2468 page_mask = page_size - 1;
2469 }
2470
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002471 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2472
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002473 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002474
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002475 /* Display splash screen if enabled */
2476#if DISPLAY_SPLASH_SCREEN
2477 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302478 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002479 dprintf(SPEW, "Display Init: Done\n");
2480#endif
2481
2482
Greg Griscod6250552011-06-29 14:40:23 -07002483 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002484 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002485
Dhaval Patel223ec952013-07-18 14:49:44 -07002486 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2487
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002488 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002489 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002490 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002491 dprintf(ALWAYS,"dload mode key sequence detected\n");
2492 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002493 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002494 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002495 }
2496 else
2497 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302498 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002499 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002500 }
2501 boot_into_fastboot = true;
2502 }
2503 if (!boot_into_fastboot)
2504 {
2505 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2506 boot_into_recovery = 1;
2507 if (!boot_into_recovery &&
2508 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002509 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002510 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002511 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002512 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002513 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002514 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002515
Ajay Dudani77421292010-10-27 19:34:06 -07002516 reboot_mode = check_reboot_mode();
2517 if (reboot_mode == RECOVERY_MODE) {
2518 boot_into_recovery = 1;
2519 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002520 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002521 }
2522
Pavel Nedev5d91d412013-04-29 11:34:24 +03002523 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002524 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002525 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002526 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002527 if(emmc_recovery_init())
2528 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2529 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002530 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002531 if((device.is_unlocked) || (device.is_tampered))
2532 {
2533 #ifdef TZ_TAMPER_FUSE
2534 set_tamper_fuse_cmd();
2535 #endif
2536 #if USE_PCOM_SECBOOT
2537 set_tamper_flag(device.is_tampered);
2538 #endif
2539 }
Shashank Mittala0032282011-08-26 14:50:11 -07002540 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002541 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002542 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002543 else
2544 {
2545 recovery_init();
2546 #if USE_PCOM_SECBOOT
2547 if((device.is_unlocked) || (device.is_tampered))
2548 set_tamper_flag(device.is_tampered);
2549 #endif
2550 boot_linux_from_flash();
2551 }
2552 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2553 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002554 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002555
Amol Jadi5edf3552013-07-23 14:15:34 -07002556 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002557
Amol Jadi5edf3552013-07-23 14:15:34 -07002558 /* register aboot specific fastboot commands */
2559 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002560
Amol Jadi5edf3552013-07-23 14:15:34 -07002561 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002562 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002563
2564 /* initialize and start fastboot */
2565 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002566}
2567
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002568uint32_t get_page_size()
2569{
2570 return page_size;
2571}
2572
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002573/*
2574 * Calculated and save hash (SHA256) for non-signed boot image.
2575 *
2576 * Hash the same data that is checked on the signed boot image.
2577 * Kernel and Ramdisk are already read to memory buffers.
2578 * Need to read the entire device-tree from mmc
2579 * since non-signed image only read the DT tags of the relevant platform.
2580 *
2581 * @param kernel_addr - kernel bufer
2582 * @param kernel_actual - kernel size in bytes
2583 * @param ramdisk_addr - ramdisk buffer
2584 * @param ramdisk_actual - ramdisk size
2585 * @param ptn - partition
2586 * @param dt_offset - device tree offset on mmc partition
2587 * @param dt_size
2588 *
2589 * @return int - 0 on success, negative value on failure.
2590 */
2591int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2592 void *ramdisk_addr, unsigned ramdisk_actual,
2593 unsigned long long ptn,
2594 unsigned dt_offset, unsigned dt_size)
2595{
2596 SHA256_CTX sha256_ctx;
2597 char digest[32]={0};
2598 char *buf = (char *)target_get_scratch_address();
2599 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2600 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2601
2602 SHA256_Init(&sha256_ctx);
2603
2604 /* Read Boot Header */
2605 if (mmc_read(ptn, buf, page_size))
2606 {
2607 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2608 return -1;
2609 }
2610 /* Read entire Device Tree */
2611 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2612 {
2613 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2614 return -1;
2615 }
2616 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2617 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2618 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2619 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2620
2621 SHA256_Final(digest, &sha256_ctx);
2622
2623 save_kernel_hash_cmd(digest);
2624 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2625
2626 return 0;
2627}
2628
Brian Swetland9c4c0752009-01-25 16:23:50 -08002629APP_START(aboot)
2630 .init = aboot_init,
2631APP_END