blob: 589ffd2412336709eda6123b71861cc67dea3693 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Kuogee Hsieh55e92c52013-12-20 08:34:10 -08005 * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080038#include <kernel/thread.h>
39#include <arch/ops.h>
40
Dima Zavin214cc642009-01-26 11:16:21 -080041#include <dev/flash.h>
42#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080043#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080044#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080045#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070046#include <target.h>
47#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070048#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070049#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070050#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070051#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070052#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030053#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070054#include <platform/iomap.h>
Dima Zavin214cc642009-01-26 11:16:21 -080055
Neeti Desai17379b82012-06-04 18:42:53 -070056#if DEVICE_TREE
57#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070058#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070059#endif
60
Shashank Mittalcd98d472011-08-02 14:29:24 -070061#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080062#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080063#include "bootimg.h"
64#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070065#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070066#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070067#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070068#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070069#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070070
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070071extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070072extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070073extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070074extern int get_target_boot_params(const char *cmdline, const char *part,
75 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070076
77void write_device_info_mmc(device_info *dev);
78void write_device_info_flash(device_info *dev);
79
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070080#define EXPAND(NAME) #NAME
81#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070082
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080083#ifdef MEMBASE
84#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
85#else
David Ng183a7422009-12-07 14:55:21 -080086#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080087#endif
88
Deepa Dinamani0e163a42013-05-24 17:08:15 -070089#ifndef MEMSIZE
90#define MEMSIZE 1024*1024
91#endif
92
93#define MAX_TAGS_SIZE 1024
94
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080095#define RECOVERY_MODE 0x77665502
96#define FASTBOOT_MODE 0x77665500
97
Kun Liang2f1601a2013-08-12 16:29:54 +080098/* make 4096 as default size to ensure EFS,EXT4's erasing */
99#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800100#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800101
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800102#define UBI_MAGIC "UBI#"
103#define UBI_MAGIC_SIZE 0x04
104
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800105#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
106
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700107#if UFS_SUPPORT
108static const char *emmc_cmdline = " androidboot.bootdevice=msm_sdcc.1";
109static const char *ufs_cmdline = " androidboot.bootdevice=msm_ufs.1";
110#else
David Ng183a7422009-12-07 14:55:21 -0800111static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700112#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800113static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300114static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800115static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800116static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700117static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300118static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800119
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800120static const char *baseband_apq = " androidboot.baseband=apq";
121static const char *baseband_msm = " androidboot.baseband=msm";
122static const char *baseband_csfb = " androidboot.baseband=csfb";
123static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700124static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800125static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700126static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800127static const char *baseband_dsda = " androidboot.baseband=dsda";
128static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800129static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800130static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800131
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700132static unsigned page_size = 0;
133static unsigned page_mask = 0;
134static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
135static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700136static char target_boot_params[64];
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700137
Shashank Mittalcd98d472011-08-02 14:29:24 -0700138/* Assuming unauthorized kernel image by default */
139static int auth_kernel_img = 0;
140
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530141static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700142
Dima Zavin42168f22009-01-30 11:52:22 -0800143struct atag_ptbl_entry
144{
145 char name[16];
146 unsigned offset;
147 unsigned size;
148 unsigned flags;
149};
150
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700151/*
152 * Partition info, required to be published
153 * for fastboot
154 */
155struct getvar_partition_info {
156 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
157 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
158 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
159 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
160 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
161};
162
163/*
164 * Right now, we are publishing the info for only
165 * three partitions
166 */
167struct getvar_partition_info part_info[] =
168{
169 { "system" , "partition-size:", "partition-type:", "", "ext4" },
170 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
171 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
172};
173
174char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700175char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800176char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700177char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530178char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700179
Greg Griscod2471ef2011-07-14 13:00:42 -0700180extern int emmc_recovery_init(void);
181
Kinson Chik0b1c8162011-08-31 16:31:57 -0700182#if NO_KEYPAD_DRIVER
183extern int fastboot_trigger(void);
184#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700185
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800186static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700187{
188 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700189#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800190 if (is_arm64)
191 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
192 else
193 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700194 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
195 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700196#endif
197}
198
Dima Zavin42168f22009-01-30 11:52:22 -0800199static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
200{
201 struct atag_ptbl_entry atag_ptn;
202
203 memcpy(atag_ptn.name, ptn->name, 16);
204 atag_ptn.name[15] = '\0';
205 atag_ptn.offset = ptn->start;
206 atag_ptn.size = ptn->length;
207 atag_ptn.flags = ptn->flags;
208 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
209 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
210}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800211
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700212#if UFS_SUPPORT
213char* get_boot_dev_cmdline()
214{
215 const char *boot_device;
216 uint32_t val = 0;
217
218 val = target_get_boot_device();
219 switch(val)
220 {
221 case BOOT_DEFAULT:
222 case BOOT_EMMC:
223 boot_device = emmc_cmdline;
224 break;
225 case BOOT_UFS:
226 boot_device = ufs_cmdline;
227 break;
228 default:
229 dprintf(CRITICAL,"ERROR: Unexpected boot_device val=%x",val);
230 ASSERT(0);
231 };
232
233 return boot_device;
234}
235#endif
236
Neeti Desaie245d492012-06-01 12:52:13 -0700237unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800238{
David Ng183a7422009-12-07 14:55:21 -0800239 int cmdline_len = 0;
240 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800241 unsigned char *cmdline_final = NULL;
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700242#if UFS_SUPPORT
243 const char *boot_dev_cmdline = NULL;
244#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700245 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800246 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300247 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700248 int have_target_boot_params = 0;
Dima Zavin42168f22009-01-30 11:52:22 -0800249
Brian Swetland9c4c0752009-01-25 16:23:50 -0800250 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800251 cmdline_len = strlen(cmdline);
252 have_cmdline = 1;
253 }
254 if (target_is_emmc_boot()) {
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700255#if UFS_SUPPORT
256 boot_dev_cmdline = get_boot_dev_cmdline();
257 cmdline_len += strlen(boot_dev_cmdline);
258#else
David Ng183a7422009-12-07 14:55:21 -0800259 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700260#endif
David Ng183a7422009-12-07 14:55:21 -0800261 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800262
263 cmdline_len += strlen(usb_sn_cmdline);
264 cmdline_len += strlen(sn_buf);
265
Pavel Nedev5614d222013-06-17 18:01:02 +0300266 if (boot_into_recovery && gpt_exists)
267 cmdline_len += strlen(secondary_gpt_enable);
268
Pavel Nedev328ac822013-04-05 15:25:11 +0300269 if (boot_into_ffbm) {
270 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700271 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800272 /* reduce kernel console messages to speed-up boot */
273 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700274 } else if (device.charger_screen_enabled &&
275 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700276 pause_at_bootup = 1;
277 cmdline_len += strlen(battchg_pause);
278 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800279
Shashank Mittalcd98d472011-08-02 14:29:24 -0700280 if(target_use_signed_kernel() && auth_kernel_img) {
281 cmdline_len += strlen(auth_kernel);
282 }
283
Joonwoo Park61112782013-10-02 19:50:39 -0700284 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
285 "system",
286 target_boot_params,
287 sizeof(target_boot_params)) == 0) {
288 have_target_boot_params = 1;
289 cmdline_len += strlen(target_boot_params);
290 }
291
Ajay Dudanid04110c2011-01-17 23:55:07 -0800292 /* Determine correct androidboot.baseband to use */
293 switch(target_baseband())
294 {
295 case BASEBAND_APQ:
296 cmdline_len += strlen(baseband_apq);
297 break;
298
299 case BASEBAND_MSM:
300 cmdline_len += strlen(baseband_msm);
301 break;
302
303 case BASEBAND_CSFB:
304 cmdline_len += strlen(baseband_csfb);
305 break;
306
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800307 case BASEBAND_SVLTE2A:
308 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800309 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700310
311 case BASEBAND_MDM:
312 cmdline_len += strlen(baseband_mdm);
313 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700314
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800315 case BASEBAND_MDM2:
316 cmdline_len += strlen(baseband_mdm2);
317 break;
318
Amol Jadi5c61a952012-05-04 17:05:35 -0700319 case BASEBAND_SGLTE:
320 cmdline_len += strlen(baseband_sglte);
321 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530322
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800323 case BASEBAND_SGLTE2:
324 cmdline_len += strlen(baseband_sglte2);
325 break;
326
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530327 case BASEBAND_DSDA:
328 cmdline_len += strlen(baseband_dsda);
329 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800330
331 case BASEBAND_DSDA2:
332 cmdline_len += strlen(baseband_dsda2);
333 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800334 }
335
Dhaval Patel223ec952013-07-18 14:49:44 -0700336 if (target_display_panel_node(display_panel_buf, MAX_PANEL_BUF_SIZE) &&
337 strlen(display_panel_buf))
338 {
Dhaval Patel223ec952013-07-18 14:49:44 -0700339 cmdline_len += strlen(display_panel_buf);
340 }
341
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800342 if (target_warm_boot()) {
343 warm_boot = true;
344 cmdline_len += strlen(warmboot_cmdline);
345 }
346
David Ng183a7422009-12-07 14:55:21 -0800347 if (cmdline_len > 0) {
348 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700349 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
350 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700351
Amol Jadi168b7712012-03-06 16:15:00 -0800352 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700353 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800354 if (have_cmdline) {
355 src = cmdline;
356 while ((*dst++ = *src++));
357 }
358 if (target_is_emmc_boot()) {
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700359#if UFS_SUPPORT
360 src = boot_dev_cmdline;
361#else
David Ng183a7422009-12-07 14:55:21 -0800362 src = emmc_cmdline;
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700363#endif
David Ng183a7422009-12-07 14:55:21 -0800364 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700365 have_cmdline = 1;
366 while ((*dst++ = *src++));
367 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800368
369 src = usb_sn_cmdline;
370 if (have_cmdline) --dst;
371 have_cmdline = 1;
372 while ((*dst++ = *src++));
373 src = sn_buf;
374 if (have_cmdline) --dst;
375 have_cmdline = 1;
376 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800377 if (warm_boot) {
378 if (have_cmdline) --dst;
379 src = warmboot_cmdline;
380 while ((*dst++ = *src++));
381 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800382
Pavel Nedev5614d222013-06-17 18:01:02 +0300383 if (boot_into_recovery && gpt_exists) {
384 src = secondary_gpt_enable;
385 if (have_cmdline) --dst;
386 while ((*dst++ = *src++));
387 }
388
Pavel Nedev328ac822013-04-05 15:25:11 +0300389 if (boot_into_ffbm) {
390 src = androidboot_mode;
391 if (have_cmdline) --dst;
392 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700393 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300394 if (have_cmdline) --dst;
395 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800396 src = loglevel;
397 if (have_cmdline) --dst;
398 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300399 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700400 src = battchg_pause;
401 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800402 while ((*dst++ = *src++));
403 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800404
Shashank Mittalcd98d472011-08-02 14:29:24 -0700405 if(target_use_signed_kernel() && auth_kernel_img) {
406 src = auth_kernel;
407 if (have_cmdline) --dst;
408 while ((*dst++ = *src++));
409 }
410
Ajay Dudanid04110c2011-01-17 23:55:07 -0800411 switch(target_baseband())
412 {
413 case BASEBAND_APQ:
414 src = baseband_apq;
415 if (have_cmdline) --dst;
416 while ((*dst++ = *src++));
417 break;
418
419 case BASEBAND_MSM:
420 src = baseband_msm;
421 if (have_cmdline) --dst;
422 while ((*dst++ = *src++));
423 break;
424
425 case BASEBAND_CSFB:
426 src = baseband_csfb;
427 if (have_cmdline) --dst;
428 while ((*dst++ = *src++));
429 break;
430
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800431 case BASEBAND_SVLTE2A:
432 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800433 if (have_cmdline) --dst;
434 while ((*dst++ = *src++));
435 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700436
437 case BASEBAND_MDM:
438 src = baseband_mdm;
439 if (have_cmdline) --dst;
440 while ((*dst++ = *src++));
441 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700442
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800443 case BASEBAND_MDM2:
444 src = baseband_mdm2;
445 if (have_cmdline) --dst;
446 while ((*dst++ = *src++));
447 break;
448
Amol Jadi5c61a952012-05-04 17:05:35 -0700449 case BASEBAND_SGLTE:
450 src = baseband_sglte;
451 if (have_cmdline) --dst;
452 while ((*dst++ = *src++));
453 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530454
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800455 case BASEBAND_SGLTE2:
456 src = baseband_sglte2;
457 if (have_cmdline) --dst;
458 while ((*dst++ = *src++));
459 break;
460
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530461 case BASEBAND_DSDA:
462 src = baseband_dsda;
463 if (have_cmdline) --dst;
464 while ((*dst++ = *src++));
465 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800466
467 case BASEBAND_DSDA2:
468 src = baseband_dsda2;
469 if (have_cmdline) --dst;
470 while ((*dst++ = *src++));
471 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800472 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700473
474 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700475 src = display_panel_buf;
476 if (have_cmdline) --dst;
477 while ((*dst++ = *src++));
478 }
Joonwoo Park61112782013-10-02 19:50:39 -0700479
480 if (have_target_boot_params) {
481 if (have_cmdline) --dst;
482 src = target_boot_params;
483 while ((*dst++ = *src++));
484 }
Neeti Desaie245d492012-06-01 12:52:13 -0700485 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700486
487
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800488 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700489 return cmdline_final;
490}
491
492unsigned *atag_core(unsigned *ptr)
493{
494 /* CORE */
495 *ptr++ = 2;
496 *ptr++ = 0x54410001;
497
498 return ptr;
499
500}
501
502unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
503 unsigned ramdisk_size)
504{
505 if (ramdisk_size) {
506 *ptr++ = 4;
507 *ptr++ = 0x54420005;
508 *ptr++ = (unsigned)ramdisk;
509 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800510 }
511
Neeti Desaie245d492012-06-01 12:52:13 -0700512 return ptr;
513}
514
515unsigned *atag_ptable(unsigned **ptr_addr)
516{
517 int i;
518 struct ptable *ptable;
519
520 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700521 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
522 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700523 *(*ptr_addr)++ = 0x4d534d70;
524 for (i = 0; i < ptable->count; ++i)
525 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
526 }
527
528 return (*ptr_addr);
529}
530
531unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
532{
533 int cmdline_length = 0;
534 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700535 char *dest;
536
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800537 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700538 n = (cmdline_length + 4) & (~3);
539
540 *ptr++ = (n / 4) + 2;
541 *ptr++ = 0x54410009;
542 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800543 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700544 ptr += (n / 4);
545
546 return ptr;
547}
548
549unsigned *atag_end(unsigned *ptr)
550{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800551 /* END */
552 *ptr++ = 0;
553 *ptr++ = 0;
554
Neeti Desaie245d492012-06-01 12:52:13 -0700555 return ptr;
556}
557
558void generate_atags(unsigned *ptr, const char *cmdline,
559 void *ramdisk, unsigned ramdisk_size)
560{
561
562 ptr = atag_core(ptr);
563 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
564 ptr = target_atag_mem(ptr);
565
566 /* Skip NAND partition ATAGS for eMMC boot */
567 if (!target_is_emmc_boot()){
568 ptr = atag_ptable(&ptr);
569 }
570
571 ptr = atag_cmdline(ptr, cmdline);
572 ptr = atag_end(ptr);
573}
574
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700575typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700576void boot_linux(void *kernel, unsigned *tags,
577 const char *cmdline, unsigned machtype,
578 void *ramdisk, unsigned ramdisk_size)
579{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800580 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800581#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700582 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800583#endif
584
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700585 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800586 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800587 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800588
589 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700590
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800591 final_cmdline = update_cmdline((const char*)cmdline);
592
Neeti Desai17379b82012-06-04 18:42:53 -0700593#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800594 dprintf(INFO, "Updating device tree: start\n");
595
Neeti Desai17379b82012-06-04 18:42:53 -0700596 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800597 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700598 if(ret)
599 {
600 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
601 ASSERT(0);
602 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800603 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700604#else
Neeti Desaie245d492012-06-01 12:52:13 -0700605 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800606 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700607#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700608
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700609 /* Perform target specific cleanup */
610 target_uninit();
611
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800612 /* Turn off splash screen if enabled */
613#if DISPLAY_SPLASH_SCREEN
614 target_display_shutdown();
615#endif
616
617
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800618 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
619 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800620
621 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700622
Amol Jadi4421e652011-06-16 15:00:48 -0700623 /* do any platform specific cleanup before kernel entry */
624 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700625
Brian Swetland9c4c0752009-01-25 16:23:50 -0800626 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700627
Amol Jadi504f9fe2012-08-16 13:56:48 -0700628#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800629 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700630#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700631 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800632
633 if (IS_ARM64(kptr))
634 /* Jump to a 64bit kernel */
635 scm_elexec_call((paddr_t)kernel, tags_phys);
636 else
637 /* Jump to a 32bit kernel */
638 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800639}
640
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700641/* Function to check if the memory address range falls within the aboot
642 * boundaries.
643 * start: Start of the memory region
644 * size: Size of the memory region
645 */
646int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
647{
648 /* Check for boundary conditions. */
649 if ((start + size) < start)
650 return -1;
651
652 /* Check for memory overlap. */
653 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
654 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700655 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700656 return 0;
657 else
658 return -1;
659}
660
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800661#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800662
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800663BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800664#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800665BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800666#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800667
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700668static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
669{
670 int ret;
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800671#if IMAGE_VERIF_ALGO_SHA1
672 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
673#else
674 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
675#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700676
677 /* Assume device is rooted at this time. */
678 device.is_tampered = 1;
679
680 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
681
682 ret = image_verify((unsigned char *)bootimg_addr,
683 (unsigned char *)(bootimg_addr + bootimg_size),
684 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800685 auth_algo);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700686
687 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
688
689 if (ret)
690 {
691 /* Authorized kernel */
692 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700693 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700694 }
695
696#if USE_PCOM_SECBOOT
697 set_tamper_flag(device.is_tampered);
698#endif
699
700 if(device.is_tampered)
701 {
702 write_device_info_mmc(&device);
703 #ifdef TZ_TAMPER_FUSE
704 set_tamper_fuse_cmd();
705 #endif
706 #ifdef ASSERT_ON_TAMPER
707 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
708 ASSERT(0);
709 #endif
710 }
711}
712
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530713static bool check_format_bit()
714{
715 bool ret = false;
716 int index;
717 uint64_t offset;
718 struct boot_selection_info *in = NULL;
719 char *buf = NULL;
720
721 index = partition_get_index("bootselect");
722 if (index == INVALID_PTN)
723 {
724 dprintf(INFO, "Unable to locate /bootselect partition\n");
725 return ret;
726 }
727 offset = partition_get_offset(index);
728 if(!offset)
729 {
730 dprintf(INFO, "partition /bootselect doesn't exist\n");
731 return ret;
732 }
733 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
734 ASSERT(buf);
735 if (mmc_read(offset, (unsigned int *)buf, page_size))
736 {
737 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
738 free(buf);
739 return ret;
740 }
741 in = (struct boot_selection_info *) buf;
742 if ((in->signature == BOOTSELECT_SIGNATURE) &&
743 (in->version == BOOTSELECT_VERSION)) {
744 if ((in->state_info & BOOTSELECT_FORMAT) &&
745 !(in->state_info & BOOTSELECT_FACTORY))
746 ret = true;
747 } else {
748 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
749 in->signature, in->version);
750 ASSERT(0);
751 }
752 free(buf);
753 return ret;
754}
755
Shashank Mittal23b8f422010-04-16 19:27:21 -0700756int boot_linux_from_mmc(void)
757{
758 struct boot_img_hdr *hdr = (void*) buf;
759 struct boot_img_hdr *uhdr;
760 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700761 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700762 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700763 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700764
Shashank Mittalcd98d472011-08-02 14:29:24 -0700765 unsigned char *image_addr = 0;
766 unsigned kernel_actual;
767 unsigned ramdisk_actual;
768 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700769 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700770
771#if DEVICE_TREE
772 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700773 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700774 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800775 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700776 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700777#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800778 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
779 struct kernel64_hdr *kptr = (void*) kbuf;
780
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530781 if (check_format_bit())
782 boot_into_recovery = 1;
783
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700784 if (!boot_into_recovery) {
785 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
786 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
787 if (rcode <= 0) {
788 boot_into_ffbm = false;
789 if (rcode < 0)
790 dprintf(CRITICAL,"failed to get ffbm cookie");
791 } else
792 boot_into_ffbm = true;
793 } else
794 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700795 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
796 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
797 dprintf(INFO, "Unified boot method!\n");
798 hdr = uhdr;
799 goto unified_boot;
800 }
Greg Griscod6250552011-06-29 14:40:23 -0700801 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700802 index = partition_get_index("boot");
803 ptn = partition_get_offset(index);
804 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700805 dprintf(CRITICAL, "ERROR: No boot partition found\n");
806 return -1;
807 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700808 }
809 else {
810 index = partition_get_index("recovery");
811 ptn = partition_get_offset(index);
812 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700813 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
814 return -1;
815 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700816 }
817
Greg Griscod6250552011-06-29 14:40:23 -0700818 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700819 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
820 return -1;
821 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700822
823 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700824 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700825 return -1;
826 }
827
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700828 if (hdr->page_size && (hdr->page_size != page_size)) {
829 page_size = hdr->page_size;
830 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700831 }
832
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800833 /* Read the next page to get kernel Image header
834 * which lives in the second page for arm64 targets.
835 */
836
837 if (mmc_read(ptn + page_size, (unsigned int *) kbuf, page_size)) {
838 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
839 return -1;
840 }
841
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700842 /*
843 * Update the kernel/ramdisk/tags address if the boot image header
844 * has default values, these default values come from mkbootimg when
845 * the boot image is flashed using fastboot flash:raw
846 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800847 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700848
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700849 /* Get virtual addresses since the hdr saves physical addresses. */
850 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
851 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
852 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700853
854 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
855 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
856
857 /* Check if the addresses in the header are valid. */
858 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
859 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
860 {
861 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
862 return -1;
863 }
864
865#ifndef DEVICE_TREE
866 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
867 {
868 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
869 return -1;
870 }
871#endif
872
Shashank Mittalcd98d472011-08-02 14:29:24 -0700873 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300874 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
875 (int) target_use_signed_kernel(),
876 device.is_unlocked,
877 device.is_tampered);
878
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700879 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800880 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700881 offset = 0;
882
Shashank Mittalcd98d472011-08-02 14:29:24 -0700883 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700884
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800885#if DEVICE_TREE
886 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
887 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700888
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700889 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700890 {
891 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
892 return -1;
893 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800894#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530895 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700896
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700897#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700898
Amol Jadib6be5c12012-11-14 13:39:51 -0800899 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700900 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800901
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700902 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
903 {
904 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
905 return -1;
906 }
907
Shashank Mittalcd98d472011-08-02 14:29:24 -0700908 /* Read image without signature */
909 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
910 {
911 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
912 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800913 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700914
Amol Jadib6be5c12012-11-14 13:39:51 -0800915 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700916 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800917
Shashank Mittalcd98d472011-08-02 14:29:24 -0700918 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700919
920 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
921 {
922 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
923 return -1;
924 }
925
Shashank Mittalcd98d472011-08-02 14:29:24 -0700926 /* Read signature */
927 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
928 {
929 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700930 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700931 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800932
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700933 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700934
Neeti Desai465491e2012-07-31 12:53:35 -0700935 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700936 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
937 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700938
Neeti Desai465491e2012-07-31 12:53:35 -0700939 #if DEVICE_TREE
940 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700941 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700942 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700943
Deepa Dinamani19648b42013-09-05 17:05:55 -0700944 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700945 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
946 return -1;
947 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700948
Neeti Desai465491e2012-07-31 12:53:35 -0700949 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700950 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700951 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
952 return -1;
953 }
954
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700955 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700956 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700957 {
958 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
959 return -1;
960 }
961
Joel Kingaa335dc2013-06-03 16:11:08 -0700962 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800963 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800964 /*
965 * If appended dev tree is found, update the atags with
966 * memory address to the DTB appended location on RAM.
967 * Else update with the atags address in the kernel header
968 */
969 void *dtb;
970 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700971 hdr->kernel_size,
972 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800973 if (!dtb) {
974 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
975 return -1;
976 }
Neeti Desai465491e2012-07-31 12:53:35 -0700977 }
978 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700979 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700980 else
981 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800982 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700983
Amol Jadib6be5c12012-11-14 13:39:51 -0800984 dprintf(INFO, "Loading boot image (%d): start\n",
985 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700986 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800987
988 offset = page_size;
989
990 /* Load kernel */
991 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700992 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
993 return -1;
994 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800995 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700996
Amol Jadib6be5c12012-11-14 13:39:51 -0800997 /* Load ramdisk */
998 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700999 {
Amol Jadib6be5c12012-11-14 13:39:51 -08001000 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001001 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1002 return -1;
1003 }
1004 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001005 offset += ramdisk_actual;
1006
1007 dprintf(INFO, "Loading boot image (%d): done\n",
1008 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001009 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -07001010
1011 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001012 offset += second_actual;
1013 /* Second image loading not implemented. */
1014 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -07001015 }
1016
1017 #if DEVICE_TREE
1018 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -07001019 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -07001020 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
1021 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1022 return -1;
1023 }
1024 table = (struct dt_table*) dt_buf;
1025
Deepa Dinamani19648b42013-09-05 17:05:55 -07001026 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001027 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1028 return -1;
1029 }
1030
Deepa Dinamani19648b42013-09-05 17:05:55 -07001031 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1032 if (!table)
1033 return -1;
1034
1035 /* Read the entire device tree table into buffer */
1036 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
1037 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1038 return -1;
1039 }
1040
Joel Kingaa335dc2013-06-03 16:11:08 -07001041 /* Find index of device tree within device tree table */
1042 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001043 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1044 return -1;
1045 }
1046
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001047 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001048 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001049 {
1050 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1051 return -1;
1052 }
1053
Joel Kingaa335dc2013-06-03 16:11:08 -07001054 if(mmc_read(ptn + offset + dt_entry.offset,
1055 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -07001056 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1057 return -1;
1058 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +03001059 #ifdef TZ_SAVE_KERNEL_HASH
1060 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
1061 hdr->ramdisk_addr, ramdisk_actual,
1062 ptn, offset, hdr->dt_size);
1063 #endif /* TZ_SAVE_KERNEL_HASH */
1064
Channagoud Kadabi35095622013-03-01 13:53:05 -08001065 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -08001066 /*
1067 * If appended dev tree is found, update the atags with
1068 * memory address to the DTB appended location on RAM.
1069 * Else update with the atags address in the kernel header
1070 */
1071 void *dtb;
1072 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001073 kernel_actual,
1074 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001075 if (!dtb) {
1076 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1077 return -1;
1078 }
Neeti Desai465491e2012-07-31 12:53:35 -07001079 }
1080 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001081 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001082
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001083 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1084 target_load_ssd_keystore();
1085
Shashank Mittal23b8f422010-04-16 19:27:21 -07001086unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001087
Dima Zavin77e41f32013-03-06 16:10:43 -08001088 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001089 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001090 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1091
1092 return 0;
1093}
1094
Dima Zavin214cc642009-01-26 11:16:21 -08001095int boot_linux_from_flash(void)
1096{
1097 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001098 struct ptentry *ptn;
1099 struct ptable *ptable;
1100 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001101
Shashank Mittalcd98d472011-08-02 14:29:24 -07001102 unsigned char *image_addr = 0;
1103 unsigned kernel_actual;
1104 unsigned ramdisk_actual;
1105 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001106 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001107
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001108#if DEVICE_TREE
1109 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001110 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001111 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001112 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001113#endif
1114
David Ng183a7422009-12-07 14:55:21 -08001115 if (target_is_emmc_boot()) {
1116 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1117 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1118 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1119 return -1;
1120 }
1121 goto continue_boot;
1122 }
1123
Dima Zavin214cc642009-01-26 11:16:21 -08001124 ptable = flash_get_ptable();
1125 if (ptable == NULL) {
1126 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1127 return -1;
1128 }
1129
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001130 if(!boot_into_recovery)
1131 {
1132 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001133
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001134 if (ptn == NULL) {
1135 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1136 return -1;
1137 }
1138 }
1139 else
1140 {
1141 ptn = ptable_find(ptable, "recovery");
1142 if (ptn == NULL) {
1143 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1144 return -1;
1145 }
Dima Zavin214cc642009-01-26 11:16:21 -08001146 }
1147
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001148 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001149 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1150 return -1;
1151 }
Dima Zavin214cc642009-01-26 11:16:21 -08001152
1153 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001154 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001155 return -1;
1156 }
1157
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001158 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001159 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 -08001160 return -1;
1161 }
1162
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001163 /*
1164 * Update the kernel/ramdisk/tags address if the boot image header
1165 * has default values, these default values come from mkbootimg when
1166 * the boot image is flashed using fastboot flash:raw
1167 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001168 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001169
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001170 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001171 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1172 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1173 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1174
1175 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1176 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1177
1178 /* Check if the addresses in the header are valid. */
1179 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1180 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1181 {
1182 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1183 return -1;
1184 }
1185
1186#ifndef DEVICE_TREE
1187 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1188 {
1189 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1190 return -1;
1191 }
1192#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001193
Shashank Mittalcd98d472011-08-02 14:29:24 -07001194 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001195 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001196 {
1197 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001198 offset = 0;
1199
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001200#if DEVICE_TREE
1201 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1202 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001203
1204 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1205 {
1206 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1207 return -1;
1208 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001209#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001210 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001211#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001212
Amol Jadib6be5c12012-11-14 13:39:51 -08001213 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001214 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001215
Shashank Mittalcd98d472011-08-02 14:29:24 -07001216 /* Read image without signature */
1217 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1218 {
1219 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1220 return -1;
1221 }
Dima Zavin214cc642009-01-26 11:16:21 -08001222
Amol Jadib6be5c12012-11-14 13:39:51 -08001223 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001224 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001225
Shashank Mittalcd98d472011-08-02 14:29:24 -07001226 offset = imagesize_actual;
1227 /* Read signature */
1228 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1229 {
1230 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001231 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001232 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001233
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001234 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001235
1236 /* Move kernel and ramdisk to correct address */
1237 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1238 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001239#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001240 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001241 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001242 {
1243 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1244 return -1;
1245 }
1246
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001247 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1248#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001249
1250 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001251 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001252 {
1253 write_device_info_flash(&device);
1254 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301255#if USE_PCOM_SECBOOT
1256 set_tamper_flag(device.is_tampered);
1257#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001258 }
1259 else
1260 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001261 offset = page_size;
1262
Amol Jadib6be5c12012-11-14 13:39:51 -08001263 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1264 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1265 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1266
1267 dprintf(INFO, "Loading boot image (%d): start\n",
1268 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001269 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001270
1271 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001272 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1273 return -1;
1274 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001275 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001276
Amol Jadib6be5c12012-11-14 13:39:51 -08001277 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001278 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1279 return -1;
1280 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001281 offset += ramdisk_actual;
1282
1283 dprintf(INFO, "Loading boot image (%d): done\n",
1284 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001285 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001286
1287 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001288 offset += second_actual;
1289 /* Second image loading not implemented. */
1290 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001291 }
1292
1293#if DEVICE_TREE
1294 if(hdr->dt_size != 0) {
1295
1296 /* Read the device tree table into buffer */
1297 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1298 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1299 return -1;
1300 }
1301
1302 table = (struct dt_table*) dt_buf;
1303
Deepa Dinamani19648b42013-09-05 17:05:55 -07001304 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001305 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1306 return -1;
1307 }
1308
Deepa Dinamani19648b42013-09-05 17:05:55 -07001309 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1310 if (!table)
1311 return -1;
1312
1313 /* Read the entire device tree table into buffer */
1314 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1315 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1316 return -1;
1317 }
1318
1319
Joel Kingaa335dc2013-06-03 16:11:08 -07001320 /* Find index of device tree within device tree table */
1321 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001322 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1323 return -1;
1324 }
1325
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001326 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001327 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001328 {
1329 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1330 return -1;
1331 }
1332
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001333 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001334 if(flash_read(ptn, offset + dt_entry.offset,
1335 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001336 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1337 return -1;
1338 }
1339 }
1340#endif
1341
Shashank Mittalcd98d472011-08-02 14:29:24 -07001342 }
David Ng183a7422009-12-07 14:55:21 -08001343continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001344
Dima Zavin214cc642009-01-26 11:16:21 -08001345 /* TODO: create/pass atags to kernel */
1346
Ajay Dudanie28a6072011-07-01 13:59:46 -07001347 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001348 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001349 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1350
1351 return 0;
1352}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001353
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001354BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001355void write_device_info_mmc(device_info *dev)
1356{
1357 struct device_info *info = (void*) info_buf;
1358 unsigned long long ptn = 0;
1359 unsigned long long size;
1360 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001361 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001362 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001363
1364 index = partition_get_index("aboot");
1365 ptn = partition_get_offset(index);
1366 if(ptn == 0)
1367 {
1368 return;
1369 }
1370
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001371 lun = partition_get_lun(index);
1372 mmc_set_lun(lun);
1373
Shashank Mittal162244e2011-08-08 19:01:25 -07001374 size = partition_get_size(index);
1375
1376 memcpy(info, dev, sizeof(device_info));
1377
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001378 blocksize = mmc_get_device_blocksize();
1379
1380 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Shashank Mittal162244e2011-08-08 19:01:25 -07001381 {
1382 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1383 return;
1384 }
1385}
1386
1387void read_device_info_mmc(device_info *dev)
1388{
1389 struct device_info *info = (void*) info_buf;
1390 unsigned long long ptn = 0;
1391 unsigned long long size;
1392 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001393 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001394
1395 index = partition_get_index("aboot");
1396 ptn = partition_get_offset(index);
1397 if(ptn == 0)
1398 {
1399 return;
1400 }
1401
1402 size = partition_get_size(index);
1403
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001404 blocksize = mmc_get_device_blocksize();
1405
1406 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Shashank Mittal162244e2011-08-08 19:01:25 -07001407 {
1408 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1409 return;
1410 }
1411
1412 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1413 {
1414 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1415 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001416 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001417 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001418
1419 write_device_info_mmc(info);
1420 }
1421 memcpy(dev, info, sizeof(device_info));
1422}
1423
1424void write_device_info_flash(device_info *dev)
1425{
1426 struct device_info *info = (void *) info_buf;
1427 struct ptentry *ptn;
1428 struct ptable *ptable;
1429
1430 ptable = flash_get_ptable();
1431 if (ptable == NULL)
1432 {
1433 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1434 return;
1435 }
1436
1437 ptn = ptable_find(ptable, "devinfo");
1438 if (ptn == NULL)
1439 {
1440 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1441 return;
1442 }
1443
1444 memcpy(info, dev, sizeof(device_info));
1445
1446 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1447 {
1448 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1449 return;
1450 }
1451}
1452
1453void read_device_info_flash(device_info *dev)
1454{
1455 struct device_info *info = (void*) info_buf;
1456 struct ptentry *ptn;
1457 struct ptable *ptable;
1458
1459 ptable = flash_get_ptable();
1460 if (ptable == NULL)
1461 {
1462 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1463 return;
1464 }
1465
1466 ptn = ptable_find(ptable, "devinfo");
1467 if (ptn == NULL)
1468 {
1469 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1470 return;
1471 }
1472
1473 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1474 {
1475 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1476 return;
1477 }
1478
1479 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1480 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001481 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1482 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001483 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001484 write_device_info_flash(info);
1485 }
1486 memcpy(dev, info, sizeof(device_info));
1487}
1488
1489void write_device_info(device_info *dev)
1490{
1491 if(target_is_emmc_boot())
1492 {
1493 write_device_info_mmc(dev);
1494 }
1495 else
1496 {
1497 write_device_info_flash(dev);
1498 }
1499}
1500
1501void read_device_info(device_info *dev)
1502{
1503 if(target_is_emmc_boot())
1504 {
1505 read_device_info_mmc(dev);
1506 }
1507 else
1508 {
1509 read_device_info_flash(dev);
1510 }
1511}
1512
1513void reset_device_info()
1514{
1515 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001516 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001517 write_device_info(&device);
1518}
1519
1520void set_device_root()
1521{
1522 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001523 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001524 write_device_info(&device);
1525}
1526
Amol Jadicb524072012-08-09 16:40:18 -07001527#if DEVICE_TREE
1528int copy_dtb(uint8_t *boot_image_start)
1529{
1530 uint32 dt_image_offset = 0;
1531 uint32_t n;
1532 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001533 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001534 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001535
1536 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1537
Amol Jadicb524072012-08-09 16:40:18 -07001538 if(hdr->dt_size != 0) {
1539
1540 /* add kernel offset */
1541 dt_image_offset += page_size;
1542 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1543 dt_image_offset += n;
1544
1545 /* add ramdisk offset */
1546 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1547 dt_image_offset += n;
1548
1549 /* add second offset */
1550 if(hdr->second_size != 0) {
1551 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1552 dt_image_offset += n;
1553 }
1554
1555 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001556 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001557
Deepa Dinamani19648b42013-09-05 17:05:55 -07001558 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001559 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1560 return -1;
1561 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001562 /* Find index of device tree within device tree table */
1563 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001564 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1565 return -1;
1566 }
1567
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001568 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001569 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001570 {
1571 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1572 return -1;
1573 }
1574
Amol Jadicb524072012-08-09 16:40:18 -07001575 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001576 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001577 boot_image_start + dt_image_offset + dt_entry.offset,
1578 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001579 } else
1580 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001581
1582 /* Everything looks fine. Return success. */
1583 return 0;
1584}
1585#endif
1586
Brian Swetland9c4c0752009-01-25 16:23:50 -08001587void cmd_boot(const char *arg, void *data, unsigned sz)
1588{
1589 unsigned kernel_actual;
1590 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001591 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001592 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001593 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001594 int ret = 0;
1595 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001596
1597 if (sz < sizeof(hdr)) {
1598 fastboot_fail("invalid bootimage header");
1599 return;
1600 }
1601
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001602 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001603
1604 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001605 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001606
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001607 if(target_is_emmc_boot() && hdr->page_size) {
1608 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001609 page_mask = page_size - 1;
1610 }
1611
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001612 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1613 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1614
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001615 /*
1616 * Update the kernel/ramdisk/tags address if the boot image header
1617 * has default values, these default values come from mkbootimg when
1618 * the boot image is flashed using fastboot flash:raw
1619 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001620 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1621 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001622
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001623 /* Get virtual addresses since the hdr saves physical addresses. */
1624 hdr->kernel_addr = VA(hdr->kernel_addr);
1625 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1626 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001627
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001628 /* Check if the addresses in the header are valid. */
1629 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1630 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1631 {
1632 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001633 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001634 }
1635
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001636 /* sz should have atleast raw boot image */
1637 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001638 fastboot_fail("incomplete bootimage");
1639 return;
1640 }
1641
Amol Jadicb524072012-08-09 16:40:18 -07001642#if DEVICE_TREE
1643 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001644 ret = copy_dtb(data);
1645
1646 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001647#else
1648 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1649 {
1650 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001651 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001652 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001653#endif
1654
1655 /* Load ramdisk & kernel */
1656 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1657 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1658
1659#if DEVICE_TREE
1660 /*
1661 * If dtb is not found look for appended DTB in the kernel.
1662 * If appended dev tree is found, update the atags with
1663 * memory address to the DTB appended location on RAM.
1664 * Else update with the atags address in the kernel header
1665 */
1666 if (!dtb_copied) {
1667 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001668 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1669 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001670 if (!dtb) {
1671 fastboot_fail("dtb not found");
1672 return;
1673 }
Amol Jadicb524072012-08-09 16:40:18 -07001674 }
1675#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001676
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001677#ifndef DEVICE_TREE
1678 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1679 {
1680 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001681 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001682 }
1683#endif
1684
Brian Swetland9c4c0752009-01-25 16:23:50 -08001685 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001686 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001687
Dima Zavin77e41f32013-03-06 16:10:43 -08001688 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001689 (const char*) hdr->cmdline, board_machtype(),
1690 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001691}
1692
Dima Zavin214cc642009-01-26 11:16:21 -08001693void cmd_erase(const char *arg, void *data, unsigned sz)
1694{
1695 struct ptentry *ptn;
1696 struct ptable *ptable;
1697
1698 ptable = flash_get_ptable();
1699 if (ptable == NULL) {
1700 fastboot_fail("partition table doesn't exist");
1701 return;
1702 }
1703
1704 ptn = ptable_find(ptable, arg);
1705 if (ptn == NULL) {
1706 fastboot_fail("unknown partition name");
1707 return;
1708 }
1709
1710 if (flash_erase(ptn)) {
1711 fastboot_fail("failed to erase partition");
1712 return;
1713 }
1714 fastboot_okay("");
1715}
1716
Bikas Gurungd48bd242010-09-04 19:54:32 -07001717
1718void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1719{
Kun Liang2f1601a2013-08-12 16:29:54 +08001720 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001721 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001722 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001723 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001724 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001725
Kinson Chikf1a43512011-07-14 11:28:39 -07001726 index = partition_get_index(arg);
1727 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001728 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001729
Kinson Chikf1a43512011-07-14 11:28:39 -07001730 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001731 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001732 return;
1733 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001734
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001735 lun = partition_get_lun(index);
1736 mmc_set_lun(lun);
1737
Oliver Wangcee448d2013-10-22 18:40:13 +08001738#if MMC_SDHCI_SUPPORT
1739 if (mmc_erase_card(ptn, size)) {
1740 fastboot_fail("failed to erase partition\n");
1741 return;
1742 }
1743#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001744 size = partition_get_size(index);
1745 if (size > DEFAULT_ERASE_SIZE)
1746 size = DEFAULT_ERASE_SIZE;
1747
neetidb4b24d62012-01-20 12:13:09 -08001748 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001749 0 in first several blocks */
1750 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001751 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001752 return;
1753 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001754#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001755 fastboot_okay("");
1756}
1757
1758
Ajay Dudani5c761132011-04-07 20:19:04 -07001759void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001760{
1761 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001762 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001763 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001764 char *token = NULL;
1765 char *pname = NULL;
1766 uint8_t lun = 0;
1767 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001768
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001769 token = strtok(arg, ":");
1770 pname = token;
1771 token = strtok(NULL, ":");
1772 if(token)
1773 {
1774 lun = atoi(token);
1775 mmc_set_lun(lun);
1776 lun_set = true;
1777 }
1778
1779 if (!strcmp(pname, "partition"))
Greg Grisco6e754772011-06-23 12:19:39 -07001780 {
1781 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001782 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001783 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001784 return;
1785 }
1786 }
Greg Grisco6e754772011-06-23 12:19:39 -07001787 else
1788 {
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001789 index = partition_get_index(pname);
Kinson Chikf1a43512011-07-14 11:28:39 -07001790 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001791 if(ptn == 0) {
1792 fastboot_fail("partition table doesn't exist");
1793 return;
1794 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001795
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001796 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
Greg Grisco6e754772011-06-23 12:19:39 -07001797 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1798 fastboot_fail("image is not a boot image");
1799 return;
1800 }
1801 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001802
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001803 if(!lun_set)
1804 {
1805 lun = partition_get_lun(index);
1806 mmc_set_lun(lun);
1807 }
1808
Kinson Chikf1a43512011-07-14 11:28:39 -07001809 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001810 if (ROUND_TO_PAGE(sz,511) > size) {
1811 fastboot_fail("size too large");
1812 return;
1813 }
1814 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1815 fastboot_fail("flash write failure");
1816 return;
1817 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001818 }
1819 fastboot_okay("");
1820 return;
1821}
1822
Ajay Dudani5c761132011-04-07 20:19:04 -07001823void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1824{
1825 unsigned int chunk;
1826 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001827 uint32_t *fill_buf = NULL;
1828 uint32_t fill_val;
1829 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001830 sparse_header_t *sparse_header;
1831 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001832 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001833 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301834 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001835 int index = INVALID_PTN;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001836 int i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001837 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001838
Kinson Chikf1a43512011-07-14 11:28:39 -07001839 index = partition_get_index(arg);
1840 ptn = partition_get_offset(index);
1841 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001842 fastboot_fail("partition table doesn't exist");
1843 return;
1844 }
1845
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301846 size = partition_get_size(index);
1847 if (ROUND_TO_PAGE(sz,511) > size) {
1848 fastboot_fail("size too large");
1849 return;
1850 }
1851
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001852 lun = partition_get_lun(index);
1853 mmc_set_lun(lun);
1854
Ajay Dudani5c761132011-04-07 20:19:04 -07001855 /* Read and skip over sparse image header */
1856 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001857 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1858 fastboot_fail("size too large");
1859 return;
1860 }
1861
Ajay Dudani5c761132011-04-07 20:19:04 -07001862 data += sparse_header->file_hdr_sz;
1863 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1864 {
1865 /* Skip the remaining bytes in a header that is longer than
1866 * we expected.
1867 */
1868 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1869 }
1870
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001871 dprintf (SPEW, "=== Sparse Image Header ===\n");
1872 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1873 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1874 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1875 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1876 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1877 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1878 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1879 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001880
1881 /* Start processing chunks */
1882 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1883 {
1884 /* Read and skip over chunk header */
1885 chunk_header = (chunk_header_t *) data;
1886 data += sizeof(chunk_header_t);
1887
1888 dprintf (SPEW, "=== Chunk Header ===\n");
1889 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1890 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1891 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1892
1893 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1894 {
1895 /* Skip the remaining bytes in a header that is longer than
1896 * we expected.
1897 */
1898 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1899 }
1900
1901 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1902 switch (chunk_header->chunk_type)
1903 {
1904 case CHUNK_TYPE_RAW:
1905 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1906 chunk_data_sz))
1907 {
1908 fastboot_fail("Bogus chunk size for chunk type Raw");
1909 return;
1910 }
1911
Ajay Dudaniab18f022011-05-12 14:39:22 -07001912 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1913 chunk_data_sz,
1914 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001915 {
1916 fastboot_fail("flash write failure");
1917 return;
1918 }
1919 total_blocks += chunk_header->chunk_sz;
1920 data += chunk_data_sz;
1921 break;
1922
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001923 case CHUNK_TYPE_FILL:
1924 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1925 sizeof(uint32_t)))
1926 {
1927 fastboot_fail("Bogus chunk size for chunk type FILL");
1928 return;
1929 }
1930
1931 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
1932 if (!fill_buf)
1933 {
1934 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
1935 return;
1936 }
1937
1938 fill_val = *(uint32_t *)data;
1939 data = (char *) data + sizeof(uint32_t);
1940 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
1941
1942 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
1943 {
1944 fill_buf[i] = fill_val;
1945 }
1946
1947 for (i = 0; i < chunk_blk_cnt; i++)
1948 {
1949 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1950 sparse_header->blk_sz,
1951 fill_buf))
1952 {
1953 fastboot_fail("flash write failure");
1954 free(fill_buf);
1955 return;
1956 }
1957
1958 total_blocks++;
1959 }
1960
1961 free(fill_buf);
1962 break;
1963
Ajay Dudani5c761132011-04-07 20:19:04 -07001964 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001965 total_blocks += chunk_header->chunk_sz;
1966 break;
1967
Ajay Dudani5c761132011-04-07 20:19:04 -07001968 case CHUNK_TYPE_CRC:
1969 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1970 {
1971 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1972 return;
1973 }
1974 total_blocks += chunk_header->chunk_sz;
1975 data += chunk_data_sz;
1976 break;
1977
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001978 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001979 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07001980 fastboot_fail("Unknown chunk type");
1981 return;
1982 }
1983 }
1984
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001985 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1986 total_blocks, sparse_header->total_blks);
1987
1988 if(total_blocks != sparse_header->total_blks)
1989 {
1990 fastboot_fail("sparse image write failure");
1991 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001992
1993 fastboot_okay("");
1994 return;
1995}
1996
1997void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1998{
1999 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002000 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2001 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002002
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002003#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002004 int ret=0;
2005 uint32 major_version=0;
2006 uint32 minor_version=0;
2007
2008 ret = scm_svc_version(&major_version,&minor_version);
2009 if(!ret)
2010 {
2011 if(major_version >= 2)
2012 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002013 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002014 {
2015 ret = encrypt_scm((uint32 **) &data, &sz);
2016 if (ret != 0) {
2017 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2018 return;
2019 }
2020
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002021 /* Protect only for SSD */
2022 if (!strcmp(arg, "ssd")) {
2023 ret = scm_protect_keystore((uint32 *) data, sz);
2024 if (ret != 0) {
2025 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2026 return;
2027 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002028 }
2029 }
2030 else
2031 {
2032 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2033 if(ret != 0)
2034 {
2035 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2036 return;
2037 }
2038 }
2039 }
2040 else
2041 {
2042 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2043 magic_number[1] == DECRYPT_MAGIC_1)
2044 {
2045 ret = decrypt_scm((uint32 **) &data, &sz);
2046 if (ret != 0) {
2047 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2048 return;
2049 }
2050 }
2051 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2052 magic_number[1] == ENCRYPT_MAGIC_1)
2053 {
2054 ret = encrypt_scm((uint32 **) &data, &sz);
2055 if (ret != 0) {
2056 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2057 return;
2058 }
2059 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002060 }
2061 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002062 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002063 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002064 dprintf(CRITICAL,"INVALID SVC Version\n");
2065 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002066 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002067#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002068
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002069 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002070 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
2071 cmd_flash_mmc_img(arg, data, sz);
2072 else
2073 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002074 return;
2075}
2076
Dima Zavin214cc642009-01-26 11:16:21 -08002077void cmd_flash(const char *arg, void *data, unsigned sz)
2078{
2079 struct ptentry *ptn;
2080 struct ptable *ptable;
2081 unsigned extra = 0;
2082
2083 ptable = flash_get_ptable();
2084 if (ptable == NULL) {
2085 fastboot_fail("partition table doesn't exist");
2086 return;
2087 }
2088
2089 ptn = ptable_find(ptable, arg);
2090 if (ptn == NULL) {
2091 fastboot_fail("unknown partition name");
2092 return;
2093 }
2094
2095 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2096 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2097 fastboot_fail("image is not a boot image");
2098 return;
2099 }
2100 }
2101
Amol Jadi5c61a952012-05-04 17:05:35 -07002102 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002103 || !strcmp(ptn->name, "userdata")
2104 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002105 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002106 || !strcmp(ptn->name, "modem"))
2107 {
2108 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07002109 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002110 else
2111 extra = 0;
2112 }
2113 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002114 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002115
2116 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2117 if (flash_write(ptn, extra, data, sz)) {
2118 fastboot_fail("flash write failure");
2119 return;
2120 }
2121 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2122 fastboot_okay("");
2123}
2124
2125void cmd_continue(const char *arg, void *data, unsigned sz)
2126{
2127 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002128 fastboot_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002129 if (target_is_emmc_boot())
2130 {
2131 boot_linux_from_mmc();
2132 }
2133 else
2134 {
2135 boot_linux_from_flash();
2136 }
Dima Zavin214cc642009-01-26 11:16:21 -08002137}
2138
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002139void cmd_reboot(const char *arg, void *data, unsigned sz)
2140{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002141 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002142 fastboot_okay("");
2143 reboot_device(0);
2144}
2145
2146void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2147{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002148 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002149 fastboot_okay("");
2150 reboot_device(FASTBOOT_MODE);
2151}
2152
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002153void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2154{
2155 dprintf(INFO, "Enabling charger screen check\n");
2156 device.charger_screen_enabled = 1;
2157 write_device_info(&device);
2158 fastboot_okay("");
2159}
2160
2161void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2162{
2163 dprintf(INFO, "Disabling charger screen check\n");
2164 device.charger_screen_enabled = 0;
2165 write_device_info(&device);
2166 fastboot_okay("");
2167}
2168
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302169void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2170{
2171 dprintf(INFO, "Selecting display panel %s\n", arg);
2172 if (arg)
2173 strlcpy(device.display_panel, arg,
2174 sizeof(device.display_panel));
2175 write_device_info(&device);
2176 fastboot_okay("");
2177}
2178
Shashank Mittal162244e2011-08-08 19:01:25 -07002179void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2180{
2181 if(!device.is_unlocked)
2182 {
2183 device.is_unlocked = 1;
2184 write_device_info(&device);
2185 }
2186 fastboot_okay("");
2187}
2188
Shashank Mittala0032282011-08-26 14:50:11 -07002189void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2190{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302191 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002192 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002193 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002194 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2195 fastboot_info(response);
2196 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002197 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302198 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2199 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002200 fastboot_okay("");
2201}
2202
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002203void cmd_preflash(const char *arg, void *data, unsigned sz)
2204{
2205 fastboot_okay("");
2206}
2207
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302208static struct fbimage logo_header = {0};
2209struct fbimage* splash_screen_flash();
2210
2211int splash_screen_check_header(struct fbimage *logo)
2212{
2213 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2214 return -1;
2215 if (logo->header.width == 0 || logo->header.height == 0)
2216 return -1;
2217 return 0;
2218}
2219
2220struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002221{
2222 struct ptentry *ptn;
2223 struct ptable *ptable;
2224 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302225 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002226
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302227
2228 ptable = flash_get_ptable();
2229 if (ptable == NULL) {
2230 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2231 return NULL;
2232 }
2233 ptn = ptable_find(ptable, "splash");
2234 if (ptn == NULL) {
2235 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2236 return NULL;
2237 }
2238
2239 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2240 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2241 return NULL;
2242 }
2243
2244 if (splash_screen_check_header(logo)) {
2245 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2246 return NULL;
2247 }
2248
2249 fb_display = fbcon_display();
2250 if (fb_display) {
2251 uint8_t *base = (uint8_t *) fb_display->base;
2252 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
2253 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002254 }
2255
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302256 if (flash_read(ptn + sizeof(logo->header), 0,
2257 base,
2258 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2259 fbcon_clear();
2260 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2261 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002262 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302263 logo->image = base;
2264 }
2265
2266 return logo;
2267}
2268
2269struct fbimage* splash_screen_mmc()
2270{
2271 int index = INVALID_PTN;
2272 unsigned long long ptn = 0;
2273 struct fbcon_config *fb_display = NULL;
2274 struct fbimage *logo = &logo_header;
2275
2276 index = partition_get_index("splash");
2277 if (index == 0) {
2278 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2279 return NULL;
2280 }
2281
2282 ptn = partition_get_offset(index);
2283 if (ptn == 0) {
2284 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2285 return NULL;
2286 }
2287
2288 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2289 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2290 return NULL;
2291 }
2292
2293 if (splash_screen_check_header(logo)) {
2294 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2295 return NULL;
2296 }
2297
2298 fb_display = fbcon_display();
2299 if (fb_display) {
2300 uint8_t *base = (uint8_t *) fb_display->base;
2301 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2302 base += LOGO_IMG_OFFSET;
2303
2304 if (mmc_read(ptn + sizeof(logo->header),
2305 base,
2306 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2307 fbcon_clear();
2308 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2309 return NULL;
2310 }
2311
2312 logo->image = base;
2313 }
2314
2315 return logo;
2316}
2317
2318
2319struct fbimage* fetch_image_from_partition()
2320{
2321 if (target_is_emmc_boot()) {
2322 return splash_screen_mmc();
2323 } else {
2324 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002325 }
2326}
2327
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002328/* Get the size from partiton name */
2329static void get_partition_size(const char *arg, char *response)
2330{
2331 uint64_t ptn = 0;
2332 uint64_t size;
2333 int index = INVALID_PTN;
2334
2335 index = partition_get_index(arg);
2336
2337 if (index == INVALID_PTN)
2338 {
2339 dprintf(CRITICAL, "Invalid partition index\n");
2340 return;
2341 }
2342
2343 ptn = partition_get_offset(index);
2344
2345 if(!ptn)
2346 {
2347 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2348 return;
2349 }
2350
2351 size = partition_get_size(index);
2352
2353 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2354 return;
2355}
2356
2357/*
2358 * Publish the partition type & size info
2359 * fastboot getvar will publish the required information.
2360 * fastboot getvar partition_size:<partition_name>: partition size in hex
2361 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2362 */
2363static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2364{
2365 uint8_t i;
2366
2367 for (i = 0; i < num_parts; i++) {
2368 get_partition_size(info[i].part_name, info[i].size_response);
2369
2370 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2371 {
2372 dprintf(CRITICAL, "partition size name truncated\n");
2373 return;
2374 }
2375 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2376 {
2377 dprintf(CRITICAL, "partition type name truncated\n");
2378 return;
2379 }
2380
2381 /* publish partition size & type info */
2382 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2383 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2384 }
2385}
2386
Amol Jadi5edf3552013-07-23 14:15:34 -07002387/* register commands and variables for fastboot */
2388void aboot_fastboot_register_commands(void)
2389{
2390 if (target_is_emmc_boot())
2391 {
2392 fastboot_register("flash:", cmd_flash_mmc);
2393 fastboot_register("erase:", cmd_erase_mmc);
2394 }
2395 else
2396 {
2397 fastboot_register("flash:", cmd_flash);
2398 fastboot_register("erase:", cmd_erase);
2399 }
2400
2401 fastboot_register("boot", cmd_boot);
2402 fastboot_register("continue", cmd_continue);
2403 fastboot_register("reboot", cmd_reboot);
2404 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2405 fastboot_register("oem unlock", cmd_oem_unlock);
2406 fastboot_register("oem device-info", cmd_oem_devinfo);
2407 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002408 fastboot_register("oem enable-charger-screen",
2409 cmd_oem_enable_charger_screen);
2410 fastboot_register("oem disable-charger-screen",
2411 cmd_oem_disable_charger_screen);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302412 fastboot_register("oem select-display-panel",
2413 cmd_oem_select_display_panel);
Amol Jadi5edf3552013-07-23 14:15:34 -07002414 /* publish variables and their values */
2415 fastboot_publish("product", TARGET(BOARD));
2416 fastboot_publish("kernel", "lk");
2417 fastboot_publish("serialno", sn_buf);
2418
2419 /*
2420 * partition info is supported only for emmc partitions
2421 * Calling this for NAND prints some error messages which
2422 * is harmless but misleading. Avoid calling this for NAND
2423 * devices.
2424 */
2425 if (target_is_emmc_boot())
2426 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2427
2428 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002429 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2430 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002431 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002432 /* Is the charger screen check enabled */
2433 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2434 device.charger_screen_enabled);
2435 fastboot_publish("charger-screen-enabled",
2436 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302437 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2438 device.display_panel);
2439 fastboot_publish("display-panel",
2440 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002441}
2442
Brian Swetland9c4c0752009-01-25 16:23:50 -08002443void aboot_init(const struct app_descriptor *app)
2444{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002445 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002446 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002447
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002448 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002449 if (target_is_emmc_boot())
2450 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002451 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002452 page_mask = page_size - 1;
2453 }
2454 else
2455 {
2456 page_size = flash_page_size();
2457 page_mask = page_size - 1;
2458 }
2459
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002460 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2461
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002462 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002463
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002464 /* Display splash screen if enabled */
2465#if DISPLAY_SPLASH_SCREEN
2466 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302467 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002468 dprintf(SPEW, "Display Init: Done\n");
2469#endif
2470
2471
Greg Griscod6250552011-06-29 14:40:23 -07002472 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002473 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002474
Dhaval Patel223ec952013-07-18 14:49:44 -07002475 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2476
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002477 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002478 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002479 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002480 dprintf(ALWAYS,"dload mode key sequence detected\n");
2481 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002482 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002483 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002484 }
2485 else
2486 {
2487 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002488 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002489 }
2490 boot_into_fastboot = true;
2491 }
2492 if (!boot_into_fastboot)
2493 {
2494 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2495 boot_into_recovery = 1;
2496 if (!boot_into_recovery &&
2497 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002498 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002499 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002500 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002501 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002502 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002503 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002504
Ajay Dudani77421292010-10-27 19:34:06 -07002505 reboot_mode = check_reboot_mode();
2506 if (reboot_mode == RECOVERY_MODE) {
2507 boot_into_recovery = 1;
2508 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002509 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002510 }
2511
Pavel Nedev5d91d412013-04-29 11:34:24 +03002512 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002513 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002514 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002515 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002516 if(emmc_recovery_init())
2517 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2518 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002519 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002520 if((device.is_unlocked) || (device.is_tampered))
2521 {
2522 #ifdef TZ_TAMPER_FUSE
2523 set_tamper_fuse_cmd();
2524 #endif
2525 #if USE_PCOM_SECBOOT
2526 set_tamper_flag(device.is_tampered);
2527 #endif
2528 }
Shashank Mittala0032282011-08-26 14:50:11 -07002529 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002530 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002531 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002532 else
2533 {
2534 recovery_init();
2535 #if USE_PCOM_SECBOOT
2536 if((device.is_unlocked) || (device.is_tampered))
2537 set_tamper_flag(device.is_tampered);
2538 #endif
2539 boot_linux_from_flash();
2540 }
2541 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2542 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002543 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002544
Amol Jadi5edf3552013-07-23 14:15:34 -07002545 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002546
Amol Jadi5edf3552013-07-23 14:15:34 -07002547 /* register aboot specific fastboot commands */
2548 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002549
Amol Jadi5edf3552013-07-23 14:15:34 -07002550 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002551 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002552
2553 /* initialize and start fastboot */
2554 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002555}
2556
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002557uint32_t get_page_size()
2558{
2559 return page_size;
2560}
2561
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002562/*
2563 * Calculated and save hash (SHA256) for non-signed boot image.
2564 *
2565 * Hash the same data that is checked on the signed boot image.
2566 * Kernel and Ramdisk are already read to memory buffers.
2567 * Need to read the entire device-tree from mmc
2568 * since non-signed image only read the DT tags of the relevant platform.
2569 *
2570 * @param kernel_addr - kernel bufer
2571 * @param kernel_actual - kernel size in bytes
2572 * @param ramdisk_addr - ramdisk buffer
2573 * @param ramdisk_actual - ramdisk size
2574 * @param ptn - partition
2575 * @param dt_offset - device tree offset on mmc partition
2576 * @param dt_size
2577 *
2578 * @return int - 0 on success, negative value on failure.
2579 */
2580int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2581 void *ramdisk_addr, unsigned ramdisk_actual,
2582 unsigned long long ptn,
2583 unsigned dt_offset, unsigned dt_size)
2584{
2585 SHA256_CTX sha256_ctx;
2586 char digest[32]={0};
2587 char *buf = (char *)target_get_scratch_address();
2588 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2589 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2590
2591 SHA256_Init(&sha256_ctx);
2592
2593 /* Read Boot Header */
2594 if (mmc_read(ptn, buf, page_size))
2595 {
2596 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2597 return -1;
2598 }
2599 /* Read entire Device Tree */
2600 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2601 {
2602 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2603 return -1;
2604 }
2605 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2606 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2607 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2608 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2609
2610 SHA256_Final(digest, &sha256_ctx);
2611
2612 save_kernel_hash_cmd(digest);
2613 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2614
2615 return 0;
2616}
2617
Brian Swetland9c4c0752009-01-25 16:23:50 -08002618APP_START(aboot)
2619 .init = aboot_init,
2620APP_END