blob: 1e36ab2be377da1412bcd6552f4475f37d0e846e [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"
Brian Swetland9c4c0752009-01-25 16:23:50 -080069
Shashank Mittal162244e2011-08-08 19:01:25 -070070#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070071
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070072extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070073extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070074extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070075extern int get_target_boot_params(const char *cmdline, const char *part,
76 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070077
78void write_device_info_mmc(device_info *dev);
79void write_device_info_flash(device_info *dev);
80
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070081#define EXPAND(NAME) #NAME
82#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070083
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080084#ifdef MEMBASE
85#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
86#else
David Ng183a7422009-12-07 14:55:21 -080087#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080088#endif
89
Deepa Dinamani0e163a42013-05-24 17:08:15 -070090#ifndef MEMSIZE
91#define MEMSIZE 1024*1024
92#endif
93
94#define MAX_TAGS_SIZE 1024
95
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080096#define RECOVERY_MODE 0x77665502
97#define FASTBOOT_MODE 0x77665500
98
Kun Liang2f1601a2013-08-12 16:29:54 +080099/* make 4096 as default size to ensure EFS,EXT4's erasing */
100#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800101#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800102
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800103#define UBI_MAGIC "UBI#"
104#define UBI_MAGIC_SIZE 0x04
105
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700106#if UFS_SUPPORT
107static const char *emmc_cmdline = " androidboot.bootdevice=msm_sdcc.1";
108static const char *ufs_cmdline = " androidboot.bootdevice=msm_ufs.1";
109#else
David Ng183a7422009-12-07 14:55:21 -0800110static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700111#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800112static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300113static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800114static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800115static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700116static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300117static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800118
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800119static const char *baseband_apq = " androidboot.baseband=apq";
120static const char *baseband_msm = " androidboot.baseband=msm";
121static const char *baseband_csfb = " androidboot.baseband=csfb";
122static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700123static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800124static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700125static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800126static const char *baseband_dsda = " androidboot.baseband=dsda";
127static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800128static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800129static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800130
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700131static unsigned page_size = 0;
132static unsigned page_mask = 0;
133static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
134static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700135static char target_boot_params[64];
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700136
Shashank Mittalcd98d472011-08-02 14:29:24 -0700137/* Assuming unauthorized kernel image by default */
138static int auth_kernel_img = 0;
139
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700140static device_info device = {DEVICE_MAGIC, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700141
Dima Zavin42168f22009-01-30 11:52:22 -0800142struct atag_ptbl_entry
143{
144 char name[16];
145 unsigned offset;
146 unsigned size;
147 unsigned flags;
148};
149
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700150/*
151 * Partition info, required to be published
152 * for fastboot
153 */
154struct getvar_partition_info {
155 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
156 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
157 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
158 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
159 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
160};
161
162/*
163 * Right now, we are publishing the info for only
164 * three partitions
165 */
166struct getvar_partition_info part_info[] =
167{
168 { "system" , "partition-size:", "partition-type:", "", "ext4" },
169 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
170 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
171};
172
173char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700174char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800175char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700176char display_panel_buf[MAX_PANEL_BUF_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700177
Greg Griscod2471ef2011-07-14 13:00:42 -0700178extern int emmc_recovery_init(void);
179
Kinson Chik0b1c8162011-08-31 16:31:57 -0700180#if NO_KEYPAD_DRIVER
181extern int fastboot_trigger(void);
182#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700183
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700184static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
185{
186 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700187#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
188 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
189 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
190 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700191#endif
192}
193
Dima Zavin42168f22009-01-30 11:52:22 -0800194static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
195{
196 struct atag_ptbl_entry atag_ptn;
197
198 memcpy(atag_ptn.name, ptn->name, 16);
199 atag_ptn.name[15] = '\0';
200 atag_ptn.offset = ptn->start;
201 atag_ptn.size = ptn->length;
202 atag_ptn.flags = ptn->flags;
203 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
204 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
205}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800206
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700207#if UFS_SUPPORT
208char* get_boot_dev_cmdline()
209{
210 const char *boot_device;
211 uint32_t val = 0;
212
213 val = target_get_boot_device();
214 switch(val)
215 {
216 case BOOT_DEFAULT:
217 case BOOT_EMMC:
218 boot_device = emmc_cmdline;
219 break;
220 case BOOT_UFS:
221 boot_device = ufs_cmdline;
222 break;
223 default:
224 dprintf(CRITICAL,"ERROR: Unexpected boot_device val=%x",val);
225 ASSERT(0);
226 };
227
228 return boot_device;
229}
230#endif
231
Neeti Desaie245d492012-06-01 12:52:13 -0700232unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800233{
David Ng183a7422009-12-07 14:55:21 -0800234 int cmdline_len = 0;
235 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800236 unsigned char *cmdline_final = NULL;
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700237#if UFS_SUPPORT
238 const char *boot_dev_cmdline = NULL;
239#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700240 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800241 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300242 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700243 int have_target_boot_params = 0;
Dima Zavin42168f22009-01-30 11:52:22 -0800244
Brian Swetland9c4c0752009-01-25 16:23:50 -0800245 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800246 cmdline_len = strlen(cmdline);
247 have_cmdline = 1;
248 }
249 if (target_is_emmc_boot()) {
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700250#if UFS_SUPPORT
251 boot_dev_cmdline = get_boot_dev_cmdline();
252 cmdline_len += strlen(boot_dev_cmdline);
253#else
David Ng183a7422009-12-07 14:55:21 -0800254 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700255#endif
David Ng183a7422009-12-07 14:55:21 -0800256 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800257
258 cmdline_len += strlen(usb_sn_cmdline);
259 cmdline_len += strlen(sn_buf);
260
Pavel Nedev5614d222013-06-17 18:01:02 +0300261 if (boot_into_recovery && gpt_exists)
262 cmdline_len += strlen(secondary_gpt_enable);
263
Pavel Nedev328ac822013-04-05 15:25:11 +0300264 if (boot_into_ffbm) {
265 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700266 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800267 /* reduce kernel console messages to speed-up boot */
268 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700269 } else if (device.charger_screen_enabled &&
270 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700271 pause_at_bootup = 1;
272 cmdline_len += strlen(battchg_pause);
273 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800274
Shashank Mittalcd98d472011-08-02 14:29:24 -0700275 if(target_use_signed_kernel() && auth_kernel_img) {
276 cmdline_len += strlen(auth_kernel);
277 }
278
Joonwoo Park61112782013-10-02 19:50:39 -0700279 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
280 "system",
281 target_boot_params,
282 sizeof(target_boot_params)) == 0) {
283 have_target_boot_params = 1;
284 cmdline_len += strlen(target_boot_params);
285 }
286
Ajay Dudanid04110c2011-01-17 23:55:07 -0800287 /* Determine correct androidboot.baseband to use */
288 switch(target_baseband())
289 {
290 case BASEBAND_APQ:
291 cmdline_len += strlen(baseband_apq);
292 break;
293
294 case BASEBAND_MSM:
295 cmdline_len += strlen(baseband_msm);
296 break;
297
298 case BASEBAND_CSFB:
299 cmdline_len += strlen(baseband_csfb);
300 break;
301
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800302 case BASEBAND_SVLTE2A:
303 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800304 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700305
306 case BASEBAND_MDM:
307 cmdline_len += strlen(baseband_mdm);
308 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700309
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800310 case BASEBAND_MDM2:
311 cmdline_len += strlen(baseband_mdm2);
312 break;
313
Amol Jadi5c61a952012-05-04 17:05:35 -0700314 case BASEBAND_SGLTE:
315 cmdline_len += strlen(baseband_sglte);
316 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530317
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800318 case BASEBAND_SGLTE2:
319 cmdline_len += strlen(baseband_sglte2);
320 break;
321
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530322 case BASEBAND_DSDA:
323 cmdline_len += strlen(baseband_dsda);
324 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800325
326 case BASEBAND_DSDA2:
327 cmdline_len += strlen(baseband_dsda2);
328 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800329 }
330
Dhaval Patel223ec952013-07-18 14:49:44 -0700331 if (target_display_panel_node(display_panel_buf, MAX_PANEL_BUF_SIZE) &&
332 strlen(display_panel_buf))
333 {
Dhaval Patel223ec952013-07-18 14:49:44 -0700334 cmdline_len += strlen(display_panel_buf);
335 }
336
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800337 if (target_warm_boot()) {
338 warm_boot = true;
339 cmdline_len += strlen(warmboot_cmdline);
340 }
341
David Ng183a7422009-12-07 14:55:21 -0800342 if (cmdline_len > 0) {
343 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700344 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
345 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700346
Amol Jadi168b7712012-03-06 16:15:00 -0800347 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700348 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800349 if (have_cmdline) {
350 src = cmdline;
351 while ((*dst++ = *src++));
352 }
353 if (target_is_emmc_boot()) {
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700354#if UFS_SUPPORT
355 src = boot_dev_cmdline;
356#else
David Ng183a7422009-12-07 14:55:21 -0800357 src = emmc_cmdline;
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700358#endif
David Ng183a7422009-12-07 14:55:21 -0800359 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700360 have_cmdline = 1;
361 while ((*dst++ = *src++));
362 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800363
364 src = usb_sn_cmdline;
365 if (have_cmdline) --dst;
366 have_cmdline = 1;
367 while ((*dst++ = *src++));
368 src = sn_buf;
369 if (have_cmdline) --dst;
370 have_cmdline = 1;
371 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800372 if (warm_boot) {
373 if (have_cmdline) --dst;
374 src = warmboot_cmdline;
375 while ((*dst++ = *src++));
376 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800377
Pavel Nedev5614d222013-06-17 18:01:02 +0300378 if (boot_into_recovery && gpt_exists) {
379 src = secondary_gpt_enable;
380 if (have_cmdline) --dst;
381 while ((*dst++ = *src++));
382 }
383
Pavel Nedev328ac822013-04-05 15:25:11 +0300384 if (boot_into_ffbm) {
385 src = androidboot_mode;
386 if (have_cmdline) --dst;
387 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700388 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300389 if (have_cmdline) --dst;
390 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800391 src = loglevel;
392 if (have_cmdline) --dst;
393 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300394 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700395 src = battchg_pause;
396 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800397 while ((*dst++ = *src++));
398 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800399
Shashank Mittalcd98d472011-08-02 14:29:24 -0700400 if(target_use_signed_kernel() && auth_kernel_img) {
401 src = auth_kernel;
402 if (have_cmdline) --dst;
403 while ((*dst++ = *src++));
404 }
405
Ajay Dudanid04110c2011-01-17 23:55:07 -0800406 switch(target_baseband())
407 {
408 case BASEBAND_APQ:
409 src = baseband_apq;
410 if (have_cmdline) --dst;
411 while ((*dst++ = *src++));
412 break;
413
414 case BASEBAND_MSM:
415 src = baseband_msm;
416 if (have_cmdline) --dst;
417 while ((*dst++ = *src++));
418 break;
419
420 case BASEBAND_CSFB:
421 src = baseband_csfb;
422 if (have_cmdline) --dst;
423 while ((*dst++ = *src++));
424 break;
425
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800426 case BASEBAND_SVLTE2A:
427 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800428 if (have_cmdline) --dst;
429 while ((*dst++ = *src++));
430 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700431
432 case BASEBAND_MDM:
433 src = baseband_mdm;
434 if (have_cmdline) --dst;
435 while ((*dst++ = *src++));
436 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700437
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800438 case BASEBAND_MDM2:
439 src = baseband_mdm2;
440 if (have_cmdline) --dst;
441 while ((*dst++ = *src++));
442 break;
443
Amol Jadi5c61a952012-05-04 17:05:35 -0700444 case BASEBAND_SGLTE:
445 src = baseband_sglte;
446 if (have_cmdline) --dst;
447 while ((*dst++ = *src++));
448 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530449
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800450 case BASEBAND_SGLTE2:
451 src = baseband_sglte2;
452 if (have_cmdline) --dst;
453 while ((*dst++ = *src++));
454 break;
455
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530456 case BASEBAND_DSDA:
457 src = baseband_dsda;
458 if (have_cmdline) --dst;
459 while ((*dst++ = *src++));
460 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800461
462 case BASEBAND_DSDA2:
463 src = baseband_dsda2;
464 if (have_cmdline) --dst;
465 while ((*dst++ = *src++));
466 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800467 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700468
469 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700470 src = display_panel_buf;
471 if (have_cmdline) --dst;
472 while ((*dst++ = *src++));
473 }
Joonwoo Park61112782013-10-02 19:50:39 -0700474
475 if (have_target_boot_params) {
476 if (have_cmdline) --dst;
477 src = target_boot_params;
478 while ((*dst++ = *src++));
479 }
Neeti Desaie245d492012-06-01 12:52:13 -0700480 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700481
482
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800483 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700484 return cmdline_final;
485}
486
487unsigned *atag_core(unsigned *ptr)
488{
489 /* CORE */
490 *ptr++ = 2;
491 *ptr++ = 0x54410001;
492
493 return ptr;
494
495}
496
497unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
498 unsigned ramdisk_size)
499{
500 if (ramdisk_size) {
501 *ptr++ = 4;
502 *ptr++ = 0x54420005;
503 *ptr++ = (unsigned)ramdisk;
504 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800505 }
506
Neeti Desaie245d492012-06-01 12:52:13 -0700507 return ptr;
508}
509
510unsigned *atag_ptable(unsigned **ptr_addr)
511{
512 int i;
513 struct ptable *ptable;
514
515 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700516 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
517 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700518 *(*ptr_addr)++ = 0x4d534d70;
519 for (i = 0; i < ptable->count; ++i)
520 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
521 }
522
523 return (*ptr_addr);
524}
525
526unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
527{
528 int cmdline_length = 0;
529 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700530 char *dest;
531
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800532 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700533 n = (cmdline_length + 4) & (~3);
534
535 *ptr++ = (n / 4) + 2;
536 *ptr++ = 0x54410009;
537 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800538 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700539 ptr += (n / 4);
540
541 return ptr;
542}
543
544unsigned *atag_end(unsigned *ptr)
545{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800546 /* END */
547 *ptr++ = 0;
548 *ptr++ = 0;
549
Neeti Desaie245d492012-06-01 12:52:13 -0700550 return ptr;
551}
552
553void generate_atags(unsigned *ptr, const char *cmdline,
554 void *ramdisk, unsigned ramdisk_size)
555{
556
557 ptr = atag_core(ptr);
558 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
559 ptr = target_atag_mem(ptr);
560
561 /* Skip NAND partition ATAGS for eMMC boot */
562 if (!target_is_emmc_boot()){
563 ptr = atag_ptable(&ptr);
564 }
565
566 ptr = atag_cmdline(ptr, cmdline);
567 ptr = atag_end(ptr);
568}
569
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700570typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700571void boot_linux(void *kernel, unsigned *tags,
572 const char *cmdline, unsigned machtype,
573 void *ramdisk, unsigned ramdisk_size)
574{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800575 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800576#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700577 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800578#endif
579
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700580 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800581 uint32_t tags_phys = PA((addr_t)tags);
582
583 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700584
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800585 final_cmdline = update_cmdline((const char*)cmdline);
586
Neeti Desai17379b82012-06-04 18:42:53 -0700587#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800588 dprintf(INFO, "Updating device tree: start\n");
589
Neeti Desai17379b82012-06-04 18:42:53 -0700590 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800591 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700592 if(ret)
593 {
594 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
595 ASSERT(0);
596 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800597 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700598#else
Neeti Desaie245d492012-06-01 12:52:13 -0700599 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800600 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700601#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700602
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700603 /* Perform target specific cleanup */
604 target_uninit();
605
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800606 /* Turn off splash screen if enabled */
607#if DISPLAY_SPLASH_SCREEN
608 target_display_shutdown();
609#endif
610
611
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800612 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
613 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800614
615 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700616
Amol Jadi4421e652011-06-16 15:00:48 -0700617 /* do any platform specific cleanup before kernel entry */
618 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700619
Brian Swetland9c4c0752009-01-25 16:23:50 -0800620 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700621
Amol Jadi504f9fe2012-08-16 13:56:48 -0700622#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800623 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700624#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700625 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700626 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800627}
628
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700629/* Function to check if the memory address range falls within the aboot
630 * boundaries.
631 * start: Start of the memory region
632 * size: Size of the memory region
633 */
634int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
635{
636 /* Check for boundary conditions. */
637 if ((start + size) < start)
638 return -1;
639
640 /* Check for memory overlap. */
641 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
642 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700643 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700644 return 0;
645 else
646 return -1;
647}
648
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800649#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800650
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700651BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800652#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700653BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800654#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800655
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700656static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
657{
658 int ret;
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800659#if IMAGE_VERIF_ALGO_SHA1
660 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
661#else
662 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
663#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700664
665 /* Assume device is rooted at this time. */
666 device.is_tampered = 1;
667
668 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
669
670 ret = image_verify((unsigned char *)bootimg_addr,
671 (unsigned char *)(bootimg_addr + bootimg_size),
672 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800673 auth_algo);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700674
675 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
676
677 if (ret)
678 {
679 /* Authorized kernel */
680 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700681 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700682 }
683
684#if USE_PCOM_SECBOOT
685 set_tamper_flag(device.is_tampered);
686#endif
687
688 if(device.is_tampered)
689 {
690 write_device_info_mmc(&device);
691 #ifdef TZ_TAMPER_FUSE
692 set_tamper_fuse_cmd();
693 #endif
694 #ifdef ASSERT_ON_TAMPER
695 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
696 ASSERT(0);
697 #endif
698 }
699}
700
Shashank Mittal23b8f422010-04-16 19:27:21 -0700701int boot_linux_from_mmc(void)
702{
703 struct boot_img_hdr *hdr = (void*) buf;
704 struct boot_img_hdr *uhdr;
705 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700706 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700707 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700708 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700709
Shashank Mittalcd98d472011-08-02 14:29:24 -0700710 unsigned char *image_addr = 0;
711 unsigned kernel_actual;
712 unsigned ramdisk_actual;
713 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700714 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700715
716#if DEVICE_TREE
717 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700718 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700719 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800720 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700721 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700722#endif
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700723 if (!boot_into_recovery) {
724 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
725 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
726 if (rcode <= 0) {
727 boot_into_ffbm = false;
728 if (rcode < 0)
729 dprintf(CRITICAL,"failed to get ffbm cookie");
730 } else
731 boot_into_ffbm = true;
732 } else
733 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700734 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
735 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
736 dprintf(INFO, "Unified boot method!\n");
737 hdr = uhdr;
738 goto unified_boot;
739 }
Greg Griscod6250552011-06-29 14:40:23 -0700740 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700741 index = partition_get_index("boot");
742 ptn = partition_get_offset(index);
743 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700744 dprintf(CRITICAL, "ERROR: No boot partition found\n");
745 return -1;
746 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700747 }
748 else {
749 index = partition_get_index("recovery");
750 ptn = partition_get_offset(index);
751 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700752 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
753 return -1;
754 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700755 }
756
Greg Griscod6250552011-06-29 14:40:23 -0700757 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700758 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
759 return -1;
760 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700761
762 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700763 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700764 return -1;
765 }
766
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700767 if (hdr->page_size && (hdr->page_size != page_size)) {
768 page_size = hdr->page_size;
769 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700770 }
771
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700772 /*
773 * Update the kernel/ramdisk/tags address if the boot image header
774 * has default values, these default values come from mkbootimg when
775 * the boot image is flashed using fastboot flash:raw
776 */
777 update_ker_tags_rdisk_addr(hdr);
778
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700779 /* Get virtual addresses since the hdr saves physical addresses. */
780 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
781 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
782 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700783
784 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
785 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
786
787 /* Check if the addresses in the header are valid. */
788 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
789 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
790 {
791 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
792 return -1;
793 }
794
795#ifndef DEVICE_TREE
796 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
797 {
798 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
799 return -1;
800 }
801#endif
802
Shashank Mittalcd98d472011-08-02 14:29:24 -0700803 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300804 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
805 (int) target_use_signed_kernel(),
806 device.is_unlocked,
807 device.is_tampered);
808
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700809 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800810 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700811 offset = 0;
812
Shashank Mittalcd98d472011-08-02 14:29:24 -0700813 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700814
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800815#if DEVICE_TREE
816 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
817 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700818
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700819 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700820 {
821 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
822 return -1;
823 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800824#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530825 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700826
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700827#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700828
Amol Jadib6be5c12012-11-14 13:39:51 -0800829 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700830 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800831
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700832 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
833 {
834 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
835 return -1;
836 }
837
Shashank Mittalcd98d472011-08-02 14:29:24 -0700838 /* Read image without signature */
839 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
840 {
841 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
842 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800843 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700844
Amol Jadib6be5c12012-11-14 13:39:51 -0800845 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700846 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800847
Shashank Mittalcd98d472011-08-02 14:29:24 -0700848 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700849
850 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
851 {
852 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
853 return -1;
854 }
855
Shashank Mittalcd98d472011-08-02 14:29:24 -0700856 /* Read signature */
857 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
858 {
859 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700860 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700861 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800862
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700863 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700864
Neeti Desai465491e2012-07-31 12:53:35 -0700865 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700866 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
867 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700868
Neeti Desai465491e2012-07-31 12:53:35 -0700869 #if DEVICE_TREE
870 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700871 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700872 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700873
Deepa Dinamani19648b42013-09-05 17:05:55 -0700874 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700875 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
876 return -1;
877 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700878
Neeti Desai465491e2012-07-31 12:53:35 -0700879 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700880 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700881 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
882 return -1;
883 }
884
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700885 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700886 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700887 {
888 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
889 return -1;
890 }
891
Joel Kingaa335dc2013-06-03 16:11:08 -0700892 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800893 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800894 /*
895 * If appended dev tree is found, update the atags with
896 * memory address to the DTB appended location on RAM.
897 * Else update with the atags address in the kernel header
898 */
899 void *dtb;
900 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700901 hdr->kernel_size,
902 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800903 if (!dtb) {
904 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
905 return -1;
906 }
Neeti Desai465491e2012-07-31 12:53:35 -0700907 }
908 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700909 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700910 else
911 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800912 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700913
Amol Jadib6be5c12012-11-14 13:39:51 -0800914 dprintf(INFO, "Loading boot image (%d): start\n",
915 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700916 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800917
918 offset = page_size;
919
920 /* Load kernel */
921 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700922 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
923 return -1;
924 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800925 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700926
Amol Jadib6be5c12012-11-14 13:39:51 -0800927 /* Load ramdisk */
928 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700929 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800930 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700931 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
932 return -1;
933 }
934 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800935 offset += ramdisk_actual;
936
937 dprintf(INFO, "Loading boot image (%d): done\n",
938 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700939 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700940
941 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800942 offset += second_actual;
943 /* Second image loading not implemented. */
944 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700945 }
946
947 #if DEVICE_TREE
948 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -0700949 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -0700950 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
951 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
952 return -1;
953 }
954 table = (struct dt_table*) dt_buf;
955
Deepa Dinamani19648b42013-09-05 17:05:55 -0700956 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700957 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
958 return -1;
959 }
960
Deepa Dinamani19648b42013-09-05 17:05:55 -0700961 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
962 if (!table)
963 return -1;
964
965 /* Read the entire device tree table into buffer */
966 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
967 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
968 return -1;
969 }
970
Joel Kingaa335dc2013-06-03 16:11:08 -0700971 /* Find index of device tree within device tree table */
972 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700973 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
974 return -1;
975 }
976
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700977 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700978 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700979 {
980 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
981 return -1;
982 }
983
Joel Kingaa335dc2013-06-03 16:11:08 -0700984 if(mmc_read(ptn + offset + dt_entry.offset,
985 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700986 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
987 return -1;
988 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300989 #ifdef TZ_SAVE_KERNEL_HASH
990 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
991 hdr->ramdisk_addr, ramdisk_actual,
992 ptn, offset, hdr->dt_size);
993 #endif /* TZ_SAVE_KERNEL_HASH */
994
Channagoud Kadabi35095622013-03-01 13:53:05 -0800995 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800996 /*
997 * If appended dev tree is found, update the atags with
998 * memory address to the DTB appended location on RAM.
999 * Else update with the atags address in the kernel header
1000 */
1001 void *dtb;
1002 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001003 kernel_actual,
1004 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001005 if (!dtb) {
1006 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1007 return -1;
1008 }
Neeti Desai465491e2012-07-31 12:53:35 -07001009 }
1010 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001011 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001012
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001013 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1014 target_load_ssd_keystore();
1015
Shashank Mittal23b8f422010-04-16 19:27:21 -07001016unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001017
Dima Zavin77e41f32013-03-06 16:10:43 -08001018 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001019 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001020 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1021
1022 return 0;
1023}
1024
Dima Zavin214cc642009-01-26 11:16:21 -08001025int boot_linux_from_flash(void)
1026{
1027 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001028 struct ptentry *ptn;
1029 struct ptable *ptable;
1030 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001031
Shashank Mittalcd98d472011-08-02 14:29:24 -07001032 unsigned char *image_addr = 0;
1033 unsigned kernel_actual;
1034 unsigned ramdisk_actual;
1035 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001036 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001037
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001038#if DEVICE_TREE
1039 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001040 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001041 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001042 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001043#endif
1044
David Ng183a7422009-12-07 14:55:21 -08001045 if (target_is_emmc_boot()) {
1046 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1047 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1048 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1049 return -1;
1050 }
1051 goto continue_boot;
1052 }
1053
Dima Zavin214cc642009-01-26 11:16:21 -08001054 ptable = flash_get_ptable();
1055 if (ptable == NULL) {
1056 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1057 return -1;
1058 }
1059
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001060 if(!boot_into_recovery)
1061 {
1062 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001063
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001064 if (ptn == NULL) {
1065 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1066 return -1;
1067 }
1068 }
1069 else
1070 {
1071 ptn = ptable_find(ptable, "recovery");
1072 if (ptn == NULL) {
1073 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1074 return -1;
1075 }
Dima Zavin214cc642009-01-26 11:16:21 -08001076 }
1077
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001078 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001079 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1080 return -1;
1081 }
Dima Zavin214cc642009-01-26 11:16:21 -08001082
1083 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001084 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001085 return -1;
1086 }
1087
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001088 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001089 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 -08001090 return -1;
1091 }
1092
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001093 /*
1094 * Update the kernel/ramdisk/tags address if the boot image header
1095 * has default values, these default values come from mkbootimg when
1096 * the boot image is flashed using fastboot flash:raw
1097 */
1098 update_ker_tags_rdisk_addr(hdr);
1099
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001100 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001101 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1102 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1103 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1104
1105 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1106 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1107
1108 /* Check if the addresses in the header are valid. */
1109 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1110 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1111 {
1112 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1113 return -1;
1114 }
1115
1116#ifndef DEVICE_TREE
1117 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1118 {
1119 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1120 return -1;
1121 }
1122#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001123
Shashank Mittalcd98d472011-08-02 14:29:24 -07001124 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001125 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001126 {
1127 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001128 offset = 0;
1129
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001130#if DEVICE_TREE
1131 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1132 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001133
1134 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1135 {
1136 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1137 return -1;
1138 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001139#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001140 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001141#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001142
Amol Jadib6be5c12012-11-14 13:39:51 -08001143 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001144 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001145
Shashank Mittalcd98d472011-08-02 14:29:24 -07001146 /* Read image without signature */
1147 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1148 {
1149 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1150 return -1;
1151 }
Dima Zavin214cc642009-01-26 11:16:21 -08001152
Amol Jadib6be5c12012-11-14 13:39:51 -08001153 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001154 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001155
Shashank Mittalcd98d472011-08-02 14:29:24 -07001156 offset = imagesize_actual;
1157 /* Read signature */
1158 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1159 {
1160 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001161 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001162 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001163
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001164 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001165
1166 /* Move kernel and ramdisk to correct address */
1167 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1168 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001169#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001170 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001171 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001172 {
1173 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1174 return -1;
1175 }
1176
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001177 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1178#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001179
1180 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001181 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001182 {
1183 write_device_info_flash(&device);
1184 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301185#if USE_PCOM_SECBOOT
1186 set_tamper_flag(device.is_tampered);
1187#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001188 }
1189 else
1190 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001191 offset = page_size;
1192
Amol Jadib6be5c12012-11-14 13:39:51 -08001193 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1194 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1195 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1196
1197 dprintf(INFO, "Loading boot image (%d): start\n",
1198 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001199 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001200
1201 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001202 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1203 return -1;
1204 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001205 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001206
Amol Jadib6be5c12012-11-14 13:39:51 -08001207 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001208 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1209 return -1;
1210 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001211 offset += ramdisk_actual;
1212
1213 dprintf(INFO, "Loading boot image (%d): done\n",
1214 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001215 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001216
1217 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001218 offset += second_actual;
1219 /* Second image loading not implemented. */
1220 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001221 }
1222
1223#if DEVICE_TREE
1224 if(hdr->dt_size != 0) {
1225
1226 /* Read the device tree table into buffer */
1227 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1228 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1229 return -1;
1230 }
1231
1232 table = (struct dt_table*) dt_buf;
1233
Deepa Dinamani19648b42013-09-05 17:05:55 -07001234 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001235 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1236 return -1;
1237 }
1238
Deepa Dinamani19648b42013-09-05 17:05:55 -07001239 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1240 if (!table)
1241 return -1;
1242
1243 /* Read the entire device tree table into buffer */
1244 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1245 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1246 return -1;
1247 }
1248
1249
Joel Kingaa335dc2013-06-03 16:11:08 -07001250 /* Find index of device tree within device tree table */
1251 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001252 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1253 return -1;
1254 }
1255
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001256 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001257 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001258 {
1259 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1260 return -1;
1261 }
1262
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001263 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001264 if(flash_read(ptn, offset + dt_entry.offset,
1265 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001266 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1267 return -1;
1268 }
1269 }
1270#endif
1271
Shashank Mittalcd98d472011-08-02 14:29:24 -07001272 }
David Ng183a7422009-12-07 14:55:21 -08001273continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001274
Dima Zavin214cc642009-01-26 11:16:21 -08001275 /* TODO: create/pass atags to kernel */
1276
Ajay Dudanie28a6072011-07-01 13:59:46 -07001277 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001278 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001279 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1280
1281 return 0;
1282}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001283
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001284BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001285void write_device_info_mmc(device_info *dev)
1286{
1287 struct device_info *info = (void*) info_buf;
1288 unsigned long long ptn = 0;
1289 unsigned long long size;
1290 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001291 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001292 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001293
1294 index = partition_get_index("aboot");
1295 ptn = partition_get_offset(index);
1296 if(ptn == 0)
1297 {
1298 return;
1299 }
1300
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001301 lun = partition_get_lun(index);
1302 mmc_set_lun(lun);
1303
Shashank Mittal162244e2011-08-08 19:01:25 -07001304 size = partition_get_size(index);
1305
1306 memcpy(info, dev, sizeof(device_info));
1307
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001308 blocksize = mmc_get_device_blocksize();
1309
1310 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Shashank Mittal162244e2011-08-08 19:01:25 -07001311 {
1312 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1313 return;
1314 }
1315}
1316
1317void read_device_info_mmc(device_info *dev)
1318{
1319 struct device_info *info = (void*) info_buf;
1320 unsigned long long ptn = 0;
1321 unsigned long long size;
1322 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001323 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001324
1325 index = partition_get_index("aboot");
1326 ptn = partition_get_offset(index);
1327 if(ptn == 0)
1328 {
1329 return;
1330 }
1331
1332 size = partition_get_size(index);
1333
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001334 blocksize = mmc_get_device_blocksize();
1335
1336 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Shashank Mittal162244e2011-08-08 19:01:25 -07001337 {
1338 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1339 return;
1340 }
1341
1342 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1343 {
1344 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1345 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001346 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001347 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001348
1349 write_device_info_mmc(info);
1350 }
1351 memcpy(dev, info, sizeof(device_info));
1352}
1353
1354void write_device_info_flash(device_info *dev)
1355{
1356 struct device_info *info = (void *) info_buf;
1357 struct ptentry *ptn;
1358 struct ptable *ptable;
1359
1360 ptable = flash_get_ptable();
1361 if (ptable == NULL)
1362 {
1363 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1364 return;
1365 }
1366
1367 ptn = ptable_find(ptable, "devinfo");
1368 if (ptn == NULL)
1369 {
1370 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1371 return;
1372 }
1373
1374 memcpy(info, dev, sizeof(device_info));
1375
1376 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1377 {
1378 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1379 return;
1380 }
1381}
1382
1383void read_device_info_flash(device_info *dev)
1384{
1385 struct device_info *info = (void*) info_buf;
1386 struct ptentry *ptn;
1387 struct ptable *ptable;
1388
1389 ptable = flash_get_ptable();
1390 if (ptable == NULL)
1391 {
1392 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1393 return;
1394 }
1395
1396 ptn = ptable_find(ptable, "devinfo");
1397 if (ptn == NULL)
1398 {
1399 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1400 return;
1401 }
1402
1403 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1404 {
1405 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1406 return;
1407 }
1408
1409 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1410 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001411 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1412 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001413 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001414 write_device_info_flash(info);
1415 }
1416 memcpy(dev, info, sizeof(device_info));
1417}
1418
1419void write_device_info(device_info *dev)
1420{
1421 if(target_is_emmc_boot())
1422 {
1423 write_device_info_mmc(dev);
1424 }
1425 else
1426 {
1427 write_device_info_flash(dev);
1428 }
1429}
1430
1431void read_device_info(device_info *dev)
1432{
1433 if(target_is_emmc_boot())
1434 {
1435 read_device_info_mmc(dev);
1436 }
1437 else
1438 {
1439 read_device_info_flash(dev);
1440 }
1441}
1442
1443void reset_device_info()
1444{
1445 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001446 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001447 write_device_info(&device);
1448}
1449
1450void set_device_root()
1451{
1452 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001453 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001454 write_device_info(&device);
1455}
1456
Amol Jadicb524072012-08-09 16:40:18 -07001457#if DEVICE_TREE
1458int copy_dtb(uint8_t *boot_image_start)
1459{
1460 uint32 dt_image_offset = 0;
1461 uint32_t n;
1462 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001463 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001464 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001465
1466 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1467
Amol Jadicb524072012-08-09 16:40:18 -07001468 if(hdr->dt_size != 0) {
1469
1470 /* add kernel offset */
1471 dt_image_offset += page_size;
1472 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1473 dt_image_offset += n;
1474
1475 /* add ramdisk offset */
1476 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1477 dt_image_offset += n;
1478
1479 /* add second offset */
1480 if(hdr->second_size != 0) {
1481 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1482 dt_image_offset += n;
1483 }
1484
1485 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001486 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001487
Deepa Dinamani19648b42013-09-05 17:05:55 -07001488 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001489 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1490 return -1;
1491 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001492 /* Find index of device tree within device tree table */
1493 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001494 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1495 return -1;
1496 }
1497
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001498 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001499 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001500 {
1501 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1502 return -1;
1503 }
1504
Amol Jadicb524072012-08-09 16:40:18 -07001505 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001506 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001507 boot_image_start + dt_image_offset + dt_entry.offset,
1508 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001509 } else
1510 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001511
1512 /* Everything looks fine. Return success. */
1513 return 0;
1514}
1515#endif
1516
Brian Swetland9c4c0752009-01-25 16:23:50 -08001517void cmd_boot(const char *arg, void *data, unsigned sz)
1518{
1519 unsigned kernel_actual;
1520 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001521 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001522 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001523 int ret = 0;
1524 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001525
1526 if (sz < sizeof(hdr)) {
1527 fastboot_fail("invalid bootimage header");
1528 return;
1529 }
1530
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001531 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001532
1533 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001534 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001535
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001536 if(target_is_emmc_boot() && hdr->page_size) {
1537 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001538 page_mask = page_size - 1;
1539 }
1540
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001541 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1542 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1543
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001544 /*
1545 * Update the kernel/ramdisk/tags address if the boot image header
1546 * has default values, these default values come from mkbootimg when
1547 * the boot image is flashed using fastboot flash:raw
1548 */
1549 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001550
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001551 /* Get virtual addresses since the hdr saves physical addresses. */
1552 hdr->kernel_addr = VA(hdr->kernel_addr);
1553 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1554 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001555
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001556 /* Check if the addresses in the header are valid. */
1557 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1558 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1559 {
1560 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001561 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001562 }
1563
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001564 /* sz should have atleast raw boot image */
1565 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001566 fastboot_fail("incomplete bootimage");
1567 return;
1568 }
1569
Amol Jadicb524072012-08-09 16:40:18 -07001570#if DEVICE_TREE
1571 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001572 ret = copy_dtb(data);
1573
1574 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001575#else
1576 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1577 {
1578 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001579 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001580 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001581#endif
1582
1583 /* Load ramdisk & kernel */
1584 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1585 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1586
1587#if DEVICE_TREE
1588 /*
1589 * If dtb is not found look for appended DTB in the kernel.
1590 * If appended dev tree is found, update the atags with
1591 * memory address to the DTB appended location on RAM.
1592 * Else update with the atags address in the kernel header
1593 */
1594 if (!dtb_copied) {
1595 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001596 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1597 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001598 if (!dtb) {
1599 fastboot_fail("dtb not found");
1600 return;
1601 }
Amol Jadicb524072012-08-09 16:40:18 -07001602 }
1603#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001604
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001605#ifndef DEVICE_TREE
1606 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1607 {
1608 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001609 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001610 }
1611#endif
1612
Brian Swetland9c4c0752009-01-25 16:23:50 -08001613 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001614 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001615
Dima Zavin77e41f32013-03-06 16:10:43 -08001616 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001617 (const char*) hdr->cmdline, board_machtype(),
1618 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001619}
1620
Dima Zavin214cc642009-01-26 11:16:21 -08001621void cmd_erase(const char *arg, void *data, unsigned sz)
1622{
1623 struct ptentry *ptn;
1624 struct ptable *ptable;
1625
1626 ptable = flash_get_ptable();
1627 if (ptable == NULL) {
1628 fastboot_fail("partition table doesn't exist");
1629 return;
1630 }
1631
1632 ptn = ptable_find(ptable, arg);
1633 if (ptn == NULL) {
1634 fastboot_fail("unknown partition name");
1635 return;
1636 }
1637
1638 if (flash_erase(ptn)) {
1639 fastboot_fail("failed to erase partition");
1640 return;
1641 }
1642 fastboot_okay("");
1643}
1644
Bikas Gurungd48bd242010-09-04 19:54:32 -07001645
1646void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1647{
Kun Liang2f1601a2013-08-12 16:29:54 +08001648 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001649 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001650 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001651 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001652 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001653
Kinson Chikf1a43512011-07-14 11:28:39 -07001654 index = partition_get_index(arg);
1655 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001656 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001657
Kinson Chikf1a43512011-07-14 11:28:39 -07001658 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001659 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001660 return;
1661 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001662
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001663 lun = partition_get_lun(index);
1664 mmc_set_lun(lun);
1665
Oliver Wangcee448d2013-10-22 18:40:13 +08001666#if MMC_SDHCI_SUPPORT
1667 if (mmc_erase_card(ptn, size)) {
1668 fastboot_fail("failed to erase partition\n");
1669 return;
1670 }
1671#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001672 size = partition_get_size(index);
1673 if (size > DEFAULT_ERASE_SIZE)
1674 size = DEFAULT_ERASE_SIZE;
1675
neetidb4b24d62012-01-20 12:13:09 -08001676 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001677 0 in first several blocks */
1678 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001679 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001680 return;
1681 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001682#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001683 fastboot_okay("");
1684}
1685
1686
Ajay Dudani5c761132011-04-07 20:19:04 -07001687void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001688{
1689 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001690 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001691 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001692 char *token = NULL;
1693 char *pname = NULL;
1694 uint8_t lun = 0;
1695 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001696
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001697 token = strtok(arg, ":");
1698 pname = token;
1699 token = strtok(NULL, ":");
1700 if(token)
1701 {
1702 lun = atoi(token);
1703 mmc_set_lun(lun);
1704 lun_set = true;
1705 }
1706
1707 if (!strcmp(pname, "partition"))
Greg Grisco6e754772011-06-23 12:19:39 -07001708 {
1709 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001710 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001711 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001712 return;
1713 }
1714 }
Greg Grisco6e754772011-06-23 12:19:39 -07001715 else
1716 {
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001717 index = partition_get_index(pname);
Kinson Chikf1a43512011-07-14 11:28:39 -07001718 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001719 if(ptn == 0) {
1720 fastboot_fail("partition table doesn't exist");
1721 return;
1722 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001723
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001724 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
Greg Grisco6e754772011-06-23 12:19:39 -07001725 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1726 fastboot_fail("image is not a boot image");
1727 return;
1728 }
1729 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001730
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001731 if(!lun_set)
1732 {
1733 lun = partition_get_lun(index);
1734 mmc_set_lun(lun);
1735 }
1736
Kinson Chikf1a43512011-07-14 11:28:39 -07001737 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001738 if (ROUND_TO_PAGE(sz,511) > size) {
1739 fastboot_fail("size too large");
1740 return;
1741 }
1742 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1743 fastboot_fail("flash write failure");
1744 return;
1745 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001746 }
1747 fastboot_okay("");
1748 return;
1749}
1750
Ajay Dudani5c761132011-04-07 20:19:04 -07001751void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1752{
1753 unsigned int chunk;
1754 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001755 uint32_t *fill_buf = NULL;
1756 uint32_t fill_val;
1757 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001758 sparse_header_t *sparse_header;
1759 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001760 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001761 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301762 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001763 int index = INVALID_PTN;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001764 int i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001765 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001766
Kinson Chikf1a43512011-07-14 11:28:39 -07001767 index = partition_get_index(arg);
1768 ptn = partition_get_offset(index);
1769 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001770 fastboot_fail("partition table doesn't exist");
1771 return;
1772 }
1773
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301774 size = partition_get_size(index);
1775 if (ROUND_TO_PAGE(sz,511) > size) {
1776 fastboot_fail("size too large");
1777 return;
1778 }
1779
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001780 lun = partition_get_lun(index);
1781 mmc_set_lun(lun);
1782
Ajay Dudani5c761132011-04-07 20:19:04 -07001783 /* Read and skip over sparse image header */
1784 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001785 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1786 fastboot_fail("size too large");
1787 return;
1788 }
1789
Ajay Dudani5c761132011-04-07 20:19:04 -07001790 data += sparse_header->file_hdr_sz;
1791 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1792 {
1793 /* Skip the remaining bytes in a header that is longer than
1794 * we expected.
1795 */
1796 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1797 }
1798
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001799 dprintf (SPEW, "=== Sparse Image Header ===\n");
1800 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1801 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1802 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1803 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1804 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1805 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1806 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1807 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001808
1809 /* Start processing chunks */
1810 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1811 {
1812 /* Read and skip over chunk header */
1813 chunk_header = (chunk_header_t *) data;
1814 data += sizeof(chunk_header_t);
1815
1816 dprintf (SPEW, "=== Chunk Header ===\n");
1817 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1818 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1819 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1820
1821 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1822 {
1823 /* Skip the remaining bytes in a header that is longer than
1824 * we expected.
1825 */
1826 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1827 }
1828
1829 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1830 switch (chunk_header->chunk_type)
1831 {
1832 case CHUNK_TYPE_RAW:
1833 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1834 chunk_data_sz))
1835 {
1836 fastboot_fail("Bogus chunk size for chunk type Raw");
1837 return;
1838 }
1839
Ajay Dudaniab18f022011-05-12 14:39:22 -07001840 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1841 chunk_data_sz,
1842 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001843 {
1844 fastboot_fail("flash write failure");
1845 return;
1846 }
1847 total_blocks += chunk_header->chunk_sz;
1848 data += chunk_data_sz;
1849 break;
1850
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001851 case CHUNK_TYPE_FILL:
1852 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1853 sizeof(uint32_t)))
1854 {
1855 fastboot_fail("Bogus chunk size for chunk type FILL");
1856 return;
1857 }
1858
1859 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
1860 if (!fill_buf)
1861 {
1862 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
1863 return;
1864 }
1865
1866 fill_val = *(uint32_t *)data;
1867 data = (char *) data + sizeof(uint32_t);
1868 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
1869
1870 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
1871 {
1872 fill_buf[i] = fill_val;
1873 }
1874
1875 for (i = 0; i < chunk_blk_cnt; i++)
1876 {
1877 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1878 sparse_header->blk_sz,
1879 fill_buf))
1880 {
1881 fastboot_fail("flash write failure");
1882 free(fill_buf);
1883 return;
1884 }
1885
1886 total_blocks++;
1887 }
1888
1889 free(fill_buf);
1890 break;
1891
Ajay Dudani5c761132011-04-07 20:19:04 -07001892 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001893 total_blocks += chunk_header->chunk_sz;
1894 break;
1895
Ajay Dudani5c761132011-04-07 20:19:04 -07001896 case CHUNK_TYPE_CRC:
1897 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1898 {
1899 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1900 return;
1901 }
1902 total_blocks += chunk_header->chunk_sz;
1903 data += chunk_data_sz;
1904 break;
1905
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001906 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001907 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07001908 fastboot_fail("Unknown chunk type");
1909 return;
1910 }
1911 }
1912
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001913 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1914 total_blocks, sparse_header->total_blks);
1915
1916 if(total_blocks != sparse_header->total_blks)
1917 {
1918 fastboot_fail("sparse image write failure");
1919 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001920
1921 fastboot_okay("");
1922 return;
1923}
1924
1925void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1926{
1927 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001928 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1929 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001930
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001931#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001932 int ret=0;
1933 uint32 major_version=0;
1934 uint32 minor_version=0;
1935
1936 ret = scm_svc_version(&major_version,&minor_version);
1937 if(!ret)
1938 {
1939 if(major_version >= 2)
1940 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001941 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001942 {
1943 ret = encrypt_scm((uint32 **) &data, &sz);
1944 if (ret != 0) {
1945 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1946 return;
1947 }
1948
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001949 /* Protect only for SSD */
1950 if (!strcmp(arg, "ssd")) {
1951 ret = scm_protect_keystore((uint32 *) data, sz);
1952 if (ret != 0) {
1953 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1954 return;
1955 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001956 }
1957 }
1958 else
1959 {
1960 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1961 if(ret != 0)
1962 {
1963 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1964 return;
1965 }
1966 }
1967 }
1968 else
1969 {
1970 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1971 magic_number[1] == DECRYPT_MAGIC_1)
1972 {
1973 ret = decrypt_scm((uint32 **) &data, &sz);
1974 if (ret != 0) {
1975 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1976 return;
1977 }
1978 }
1979 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1980 magic_number[1] == ENCRYPT_MAGIC_1)
1981 {
1982 ret = encrypt_scm((uint32 **) &data, &sz);
1983 if (ret != 0) {
1984 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1985 return;
1986 }
1987 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001988 }
1989 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001990 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001991 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001992 dprintf(CRITICAL,"INVALID SVC Version\n");
1993 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001994 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001995#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001996
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001997 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001998 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1999 cmd_flash_mmc_img(arg, data, sz);
2000 else
2001 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002002 return;
2003}
2004
Dima Zavin214cc642009-01-26 11:16:21 -08002005void cmd_flash(const char *arg, void *data, unsigned sz)
2006{
2007 struct ptentry *ptn;
2008 struct ptable *ptable;
2009 unsigned extra = 0;
2010
2011 ptable = flash_get_ptable();
2012 if (ptable == NULL) {
2013 fastboot_fail("partition table doesn't exist");
2014 return;
2015 }
2016
2017 ptn = ptable_find(ptable, arg);
2018 if (ptn == NULL) {
2019 fastboot_fail("unknown partition name");
2020 return;
2021 }
2022
2023 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2024 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2025 fastboot_fail("image is not a boot image");
2026 return;
2027 }
2028 }
2029
Amol Jadi5c61a952012-05-04 17:05:35 -07002030 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002031 || !strcmp(ptn->name, "userdata")
2032 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002033 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002034 || !strcmp(ptn->name, "modem"))
2035 {
2036 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07002037 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002038 else
2039 extra = 0;
2040 }
2041 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002042 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002043
2044 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2045 if (flash_write(ptn, extra, data, sz)) {
2046 fastboot_fail("flash write failure");
2047 return;
2048 }
2049 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2050 fastboot_okay("");
2051}
2052
2053void cmd_continue(const char *arg, void *data, unsigned sz)
2054{
2055 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002056 fastboot_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002057 if (target_is_emmc_boot())
2058 {
2059 boot_linux_from_mmc();
2060 }
2061 else
2062 {
2063 boot_linux_from_flash();
2064 }
Dima Zavin214cc642009-01-26 11:16:21 -08002065}
2066
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002067void cmd_reboot(const char *arg, void *data, unsigned sz)
2068{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002069 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002070 fastboot_okay("");
2071 reboot_device(0);
2072}
2073
2074void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2075{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002076 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002077 fastboot_okay("");
2078 reboot_device(FASTBOOT_MODE);
2079}
2080
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002081void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2082{
2083 dprintf(INFO, "Enabling charger screen check\n");
2084 device.charger_screen_enabled = 1;
2085 write_device_info(&device);
2086 fastboot_okay("");
2087}
2088
2089void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2090{
2091 dprintf(INFO, "Disabling charger screen check\n");
2092 device.charger_screen_enabled = 0;
2093 write_device_info(&device);
2094 fastboot_okay("");
2095}
2096
Shashank Mittal162244e2011-08-08 19:01:25 -07002097void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2098{
2099 if(!device.is_unlocked)
2100 {
2101 device.is_unlocked = 1;
2102 write_device_info(&device);
2103 }
2104 fastboot_okay("");
2105}
2106
Shashank Mittala0032282011-08-26 14:50:11 -07002107void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2108{
2109 char response[64];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002110 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002111 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002112 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2113 fastboot_info(response);
2114 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002115 fastboot_info(response);
2116 fastboot_okay("");
2117}
2118
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002119void cmd_preflash(const char *arg, void *data, unsigned sz)
2120{
2121 fastboot_okay("");
2122}
2123
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302124static struct fbimage logo_header = {0};
2125struct fbimage* splash_screen_flash();
2126
2127int splash_screen_check_header(struct fbimage *logo)
2128{
2129 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2130 return -1;
2131 if (logo->header.width == 0 || logo->header.height == 0)
2132 return -1;
2133 return 0;
2134}
2135
2136struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002137{
2138 struct ptentry *ptn;
2139 struct ptable *ptable;
2140 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302141 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002142
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302143
2144 ptable = flash_get_ptable();
2145 if (ptable == NULL) {
2146 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2147 return NULL;
2148 }
2149 ptn = ptable_find(ptable, "splash");
2150 if (ptn == NULL) {
2151 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2152 return NULL;
2153 }
2154
2155 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2156 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2157 return NULL;
2158 }
2159
2160 if (splash_screen_check_header(logo)) {
2161 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2162 return NULL;
2163 }
2164
2165 fb_display = fbcon_display();
2166 if (fb_display) {
2167 uint8_t *base = (uint8_t *) fb_display->base;
2168 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
2169 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002170 }
2171
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302172 if (flash_read(ptn + sizeof(logo->header), 0,
2173 base,
2174 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2175 fbcon_clear();
2176 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2177 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002178 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302179 logo->image = base;
2180 }
2181
2182 return logo;
2183}
2184
2185struct fbimage* splash_screen_mmc()
2186{
2187 int index = INVALID_PTN;
2188 unsigned long long ptn = 0;
2189 struct fbcon_config *fb_display = NULL;
2190 struct fbimage *logo = &logo_header;
2191
2192 index = partition_get_index("splash");
2193 if (index == 0) {
2194 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2195 return NULL;
2196 }
2197
2198 ptn = partition_get_offset(index);
2199 if (ptn == 0) {
2200 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2201 return NULL;
2202 }
2203
2204 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2205 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2206 return NULL;
2207 }
2208
2209 if (splash_screen_check_header(logo)) {
2210 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2211 return NULL;
2212 }
2213
2214 fb_display = fbcon_display();
2215 if (fb_display) {
2216 uint8_t *base = (uint8_t *) fb_display->base;
2217 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2218 base += LOGO_IMG_OFFSET;
2219
2220 if (mmc_read(ptn + sizeof(logo->header),
2221 base,
2222 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2223 fbcon_clear();
2224 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2225 return NULL;
2226 }
2227
2228 logo->image = base;
2229 }
2230
2231 return logo;
2232}
2233
2234
2235struct fbimage* fetch_image_from_partition()
2236{
2237 if (target_is_emmc_boot()) {
2238 return splash_screen_mmc();
2239 } else {
2240 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002241 }
2242}
2243
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002244/* Get the size from partiton name */
2245static void get_partition_size(const char *arg, char *response)
2246{
2247 uint64_t ptn = 0;
2248 uint64_t size;
2249 int index = INVALID_PTN;
2250
2251 index = partition_get_index(arg);
2252
2253 if (index == INVALID_PTN)
2254 {
2255 dprintf(CRITICAL, "Invalid partition index\n");
2256 return;
2257 }
2258
2259 ptn = partition_get_offset(index);
2260
2261 if(!ptn)
2262 {
2263 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2264 return;
2265 }
2266
2267 size = partition_get_size(index);
2268
2269 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2270 return;
2271}
2272
2273/*
2274 * Publish the partition type & size info
2275 * fastboot getvar will publish the required information.
2276 * fastboot getvar partition_size:<partition_name>: partition size in hex
2277 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2278 */
2279static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2280{
2281 uint8_t i;
2282
2283 for (i = 0; i < num_parts; i++) {
2284 get_partition_size(info[i].part_name, info[i].size_response);
2285
2286 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2287 {
2288 dprintf(CRITICAL, "partition size name truncated\n");
2289 return;
2290 }
2291 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2292 {
2293 dprintf(CRITICAL, "partition type name truncated\n");
2294 return;
2295 }
2296
2297 /* publish partition size & type info */
2298 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2299 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2300 }
2301}
2302
Amol Jadi5edf3552013-07-23 14:15:34 -07002303/* register commands and variables for fastboot */
2304void aboot_fastboot_register_commands(void)
2305{
2306 if (target_is_emmc_boot())
2307 {
2308 fastboot_register("flash:", cmd_flash_mmc);
2309 fastboot_register("erase:", cmd_erase_mmc);
2310 }
2311 else
2312 {
2313 fastboot_register("flash:", cmd_flash);
2314 fastboot_register("erase:", cmd_erase);
2315 }
2316
2317 fastboot_register("boot", cmd_boot);
2318 fastboot_register("continue", cmd_continue);
2319 fastboot_register("reboot", cmd_reboot);
2320 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2321 fastboot_register("oem unlock", cmd_oem_unlock);
2322 fastboot_register("oem device-info", cmd_oem_devinfo);
2323 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002324 fastboot_register("oem enable-charger-screen",
2325 cmd_oem_enable_charger_screen);
2326 fastboot_register("oem disable-charger-screen",
2327 cmd_oem_disable_charger_screen);
Amol Jadi5edf3552013-07-23 14:15:34 -07002328 /* publish variables and their values */
2329 fastboot_publish("product", TARGET(BOARD));
2330 fastboot_publish("kernel", "lk");
2331 fastboot_publish("serialno", sn_buf);
2332
2333 /*
2334 * partition info is supported only for emmc partitions
2335 * Calling this for NAND prints some error messages which
2336 * is harmless but misleading. Avoid calling this for NAND
2337 * devices.
2338 */
2339 if (target_is_emmc_boot())
2340 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2341
2342 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002343 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2344 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002345 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002346 /* Is the charger screen check enabled */
2347 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2348 device.charger_screen_enabled);
2349 fastboot_publish("charger-screen-enabled",
2350 (const char *) charger_screen_enabled);
Amol Jadi5edf3552013-07-23 14:15:34 -07002351}
2352
Brian Swetland9c4c0752009-01-25 16:23:50 -08002353void aboot_init(const struct app_descriptor *app)
2354{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002355 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002356 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002357
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002358 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002359 if (target_is_emmc_boot())
2360 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002361 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002362 page_mask = page_size - 1;
2363 }
2364 else
2365 {
2366 page_size = flash_page_size();
2367 page_mask = page_size - 1;
2368 }
2369
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002370 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2371
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002372 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002373
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002374 /* Display splash screen if enabled */
2375#if DISPLAY_SPLASH_SCREEN
2376 dprintf(SPEW, "Display Init: Start\n");
2377 target_display_init();
2378 dprintf(SPEW, "Display Init: Done\n");
2379#endif
2380
2381
Greg Griscod6250552011-06-29 14:40:23 -07002382 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002383 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002384
Dhaval Patel223ec952013-07-18 14:49:44 -07002385 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2386
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002387 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002388 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002389 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002390 dprintf(ALWAYS,"dload mode key sequence detected\n");
2391 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002392 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002393 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002394 }
2395 else
2396 {
2397 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002398 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002399 }
2400 boot_into_fastboot = true;
2401 }
2402 if (!boot_into_fastboot)
2403 {
2404 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2405 boot_into_recovery = 1;
2406 if (!boot_into_recovery &&
2407 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002408 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002409 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002410 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002411 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002412 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002413 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002414
Ajay Dudani77421292010-10-27 19:34:06 -07002415 reboot_mode = check_reboot_mode();
2416 if (reboot_mode == RECOVERY_MODE) {
2417 boot_into_recovery = 1;
2418 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002419 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002420 }
2421
Pavel Nedev5d91d412013-04-29 11:34:24 +03002422 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002423 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002424 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002425 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002426 if(emmc_recovery_init())
2427 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2428 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002429 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002430 if((device.is_unlocked) || (device.is_tampered))
2431 {
2432 #ifdef TZ_TAMPER_FUSE
2433 set_tamper_fuse_cmd();
2434 #endif
2435 #if USE_PCOM_SECBOOT
2436 set_tamper_flag(device.is_tampered);
2437 #endif
2438 }
Shashank Mittala0032282011-08-26 14:50:11 -07002439 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002440 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002441 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002442 else
2443 {
2444 recovery_init();
2445 #if USE_PCOM_SECBOOT
2446 if((device.is_unlocked) || (device.is_tampered))
2447 set_tamper_flag(device.is_tampered);
2448 #endif
2449 boot_linux_from_flash();
2450 }
2451 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2452 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002453 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002454
Amol Jadi5edf3552013-07-23 14:15:34 -07002455 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002456
Amol Jadi5edf3552013-07-23 14:15:34 -07002457 /* register aboot specific fastboot commands */
2458 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002459
Amol Jadi5edf3552013-07-23 14:15:34 -07002460 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002461 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002462
2463 /* initialize and start fastboot */
2464 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002465}
2466
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002467uint32_t get_page_size()
2468{
2469 return page_size;
2470}
2471
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002472/*
2473 * Calculated and save hash (SHA256) for non-signed boot image.
2474 *
2475 * Hash the same data that is checked on the signed boot image.
2476 * Kernel and Ramdisk are already read to memory buffers.
2477 * Need to read the entire device-tree from mmc
2478 * since non-signed image only read the DT tags of the relevant platform.
2479 *
2480 * @param kernel_addr - kernel bufer
2481 * @param kernel_actual - kernel size in bytes
2482 * @param ramdisk_addr - ramdisk buffer
2483 * @param ramdisk_actual - ramdisk size
2484 * @param ptn - partition
2485 * @param dt_offset - device tree offset on mmc partition
2486 * @param dt_size
2487 *
2488 * @return int - 0 on success, negative value on failure.
2489 */
2490int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2491 void *ramdisk_addr, unsigned ramdisk_actual,
2492 unsigned long long ptn,
2493 unsigned dt_offset, unsigned dt_size)
2494{
2495 SHA256_CTX sha256_ctx;
2496 char digest[32]={0};
2497 char *buf = (char *)target_get_scratch_address();
2498 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2499 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2500
2501 SHA256_Init(&sha256_ctx);
2502
2503 /* Read Boot Header */
2504 if (mmc_read(ptn, buf, page_size))
2505 {
2506 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2507 return -1;
2508 }
2509 /* Read entire Device Tree */
2510 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2511 {
2512 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2513 return -1;
2514 }
2515 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2516 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2517 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2518 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2519
2520 SHA256_Final(digest, &sha256_ctx);
2521
2522 save_kernel_hash_cmd(digest);
2523 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2524
2525 return 0;
2526}
2527
Brian Swetland9c4c0752009-01-25 16:23:50 -08002528APP_START(aboot)
2529 .init = aboot_init,
2530APP_END