blob: be8af025b30b57025ceb961ff9f50bca97bf8562 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Amol Jadi2a15a272013-01-22 12:03:36 -08005 * Copyright (c) 2009-2013, 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);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070075
76void write_device_info_mmc(device_info *dev);
77void write_device_info_flash(device_info *dev);
78
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070079#define EXPAND(NAME) #NAME
80#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070081
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080082#ifdef MEMBASE
83#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
84#else
David Ng183a7422009-12-07 14:55:21 -080085#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080086#endif
87
Deepa Dinamani0e163a42013-05-24 17:08:15 -070088#ifndef MEMSIZE
89#define MEMSIZE 1024*1024
90#endif
91
92#define MAX_TAGS_SIZE 1024
93
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080094#define RECOVERY_MODE 0x77665502
95#define FASTBOOT_MODE 0x77665500
96
Kun Liang2f1601a2013-08-12 16:29:54 +080097/* make 4096 as default size to ensure EFS,EXT4's erasing */
98#define DEFAULT_ERASE_SIZE 4096
Dhaval Patel223ec952013-07-18 14:49:44 -070099#define MAX_PANEL_BUF_SIZE 64
Kun Liang2f1601a2013-08-12 16:29:54 +0800100
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700101#if UFS_SUPPORT
102static const char *emmc_cmdline = " androidboot.bootdevice=msm_sdcc.1";
103static const char *ufs_cmdline = " androidboot.bootdevice=msm_ufs.1";
104#else
David Ng183a7422009-12-07 14:55:21 -0800105static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700106#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800107static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300108static const char *androidboot_mode = " androidboot.mode=";
Dhaval Patel223ec952013-07-18 14:49:44 -0700109static const char *display_cmdline = " mdss_mdp.panel=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800110static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800111static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700112static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300113static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800114
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800115static const char *baseband_apq = " androidboot.baseband=apq";
116static const char *baseband_msm = " androidboot.baseband=msm";
117static const char *baseband_csfb = " androidboot.baseband=csfb";
118static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700119static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700120static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800121static const char *baseband_dsda = " androidboot.baseband=dsda";
122static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800123static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800124
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700125static unsigned page_size = 0;
126static unsigned page_mask = 0;
127static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
128static bool boot_into_ffbm;
129
Shashank Mittalcd98d472011-08-02 14:29:24 -0700130/* Assuming unauthorized kernel image by default */
131static int auth_kernel_img = 0;
132
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700133static device_info device = {DEVICE_MAGIC, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700134
Dima Zavin42168f22009-01-30 11:52:22 -0800135struct atag_ptbl_entry
136{
137 char name[16];
138 unsigned offset;
139 unsigned size;
140 unsigned flags;
141};
142
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700143/*
144 * Partition info, required to be published
145 * for fastboot
146 */
147struct getvar_partition_info {
148 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
149 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
150 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
151 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
152 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
153};
154
155/*
156 * Right now, we are publishing the info for only
157 * three partitions
158 */
159struct getvar_partition_info part_info[] =
160{
161 { "system" , "partition-size:", "partition-type:", "", "ext4" },
162 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
163 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
164};
165
166char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700167char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800168char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700169char display_panel_buf[MAX_PANEL_BUF_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700170
Greg Griscod2471ef2011-07-14 13:00:42 -0700171extern int emmc_recovery_init(void);
172
Kinson Chik0b1c8162011-08-31 16:31:57 -0700173#if NO_KEYPAD_DRIVER
174extern int fastboot_trigger(void);
175#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700176
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700177static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
178{
179 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700180#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
181 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
182 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
183 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700184#endif
185}
186
Dima Zavin42168f22009-01-30 11:52:22 -0800187static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
188{
189 struct atag_ptbl_entry atag_ptn;
190
191 memcpy(atag_ptn.name, ptn->name, 16);
192 atag_ptn.name[15] = '\0';
193 atag_ptn.offset = ptn->start;
194 atag_ptn.size = ptn->length;
195 atag_ptn.flags = ptn->flags;
196 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
197 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
198}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800199
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700200#if UFS_SUPPORT
201char* get_boot_dev_cmdline()
202{
203 const char *boot_device;
204 uint32_t val = 0;
205
206 val = target_get_boot_device();
207 switch(val)
208 {
209 case BOOT_DEFAULT:
210 case BOOT_EMMC:
211 boot_device = emmc_cmdline;
212 break;
213 case BOOT_UFS:
214 boot_device = ufs_cmdline;
215 break;
216 default:
217 dprintf(CRITICAL,"ERROR: Unexpected boot_device val=%x",val);
218 ASSERT(0);
219 };
220
221 return boot_device;
222}
223#endif
224
Neeti Desaie245d492012-06-01 12:52:13 -0700225unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800226{
David Ng183a7422009-12-07 14:55:21 -0800227 int cmdline_len = 0;
228 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800229 unsigned char *cmdline_final = NULL;
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700230#if UFS_SUPPORT
231 const char *boot_dev_cmdline = NULL;
232#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700233 int pause_at_bootup = 0;
Pavel Nedev5614d222013-06-17 18:01:02 +0300234 bool gpt_exists = partition_gpt_exists();
Dima Zavin42168f22009-01-30 11:52:22 -0800235
Brian Swetland9c4c0752009-01-25 16:23:50 -0800236 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800237 cmdline_len = strlen(cmdline);
238 have_cmdline = 1;
239 }
240 if (target_is_emmc_boot()) {
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700241#if UFS_SUPPORT
242 boot_dev_cmdline = get_boot_dev_cmdline();
243 cmdline_len += strlen(boot_dev_cmdline);
244#else
David Ng183a7422009-12-07 14:55:21 -0800245 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700246#endif
David Ng183a7422009-12-07 14:55:21 -0800247 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800248
249 cmdline_len += strlen(usb_sn_cmdline);
250 cmdline_len += strlen(sn_buf);
251
Pavel Nedev5614d222013-06-17 18:01:02 +0300252 if (boot_into_recovery && gpt_exists)
253 cmdline_len += strlen(secondary_gpt_enable);
254
Pavel Nedev328ac822013-04-05 15:25:11 +0300255 if (boot_into_ffbm) {
256 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700257 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800258 /* reduce kernel console messages to speed-up boot */
259 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700260 } else if (device.charger_screen_enabled &&
261 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700262 pause_at_bootup = 1;
263 cmdline_len += strlen(battchg_pause);
264 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800265
Shashank Mittalcd98d472011-08-02 14:29:24 -0700266 if(target_use_signed_kernel() && auth_kernel_img) {
267 cmdline_len += strlen(auth_kernel);
268 }
269
Ajay Dudanid04110c2011-01-17 23:55:07 -0800270 /* Determine correct androidboot.baseband to use */
271 switch(target_baseband())
272 {
273 case BASEBAND_APQ:
274 cmdline_len += strlen(baseband_apq);
275 break;
276
277 case BASEBAND_MSM:
278 cmdline_len += strlen(baseband_msm);
279 break;
280
281 case BASEBAND_CSFB:
282 cmdline_len += strlen(baseband_csfb);
283 break;
284
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800285 case BASEBAND_SVLTE2A:
286 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800287 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700288
289 case BASEBAND_MDM:
290 cmdline_len += strlen(baseband_mdm);
291 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700292
293 case BASEBAND_SGLTE:
294 cmdline_len += strlen(baseband_sglte);
295 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530296
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800297 case BASEBAND_SGLTE2:
298 cmdline_len += strlen(baseband_sglte2);
299 break;
300
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530301 case BASEBAND_DSDA:
302 cmdline_len += strlen(baseband_dsda);
303 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800304
305 case BASEBAND_DSDA2:
306 cmdline_len += strlen(baseband_dsda2);
307 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800308 }
309
Dhaval Patel223ec952013-07-18 14:49:44 -0700310 if (target_display_panel_node(display_panel_buf, MAX_PANEL_BUF_SIZE) &&
311 strlen(display_panel_buf))
312 {
313 cmdline_len += strlen(display_cmdline);
314 cmdline_len += strlen(display_panel_buf);
315 }
316
David Ng183a7422009-12-07 14:55:21 -0800317 if (cmdline_len > 0) {
318 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700319 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
320 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700321
Amol Jadi168b7712012-03-06 16:15:00 -0800322 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700323 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800324 if (have_cmdline) {
325 src = cmdline;
326 while ((*dst++ = *src++));
327 }
328 if (target_is_emmc_boot()) {
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700329#if UFS_SUPPORT
330 src = boot_dev_cmdline;
331#else
David Ng183a7422009-12-07 14:55:21 -0800332 src = emmc_cmdline;
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700333#endif
David Ng183a7422009-12-07 14:55:21 -0800334 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700335 have_cmdline = 1;
336 while ((*dst++ = *src++));
337 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800338
339 src = usb_sn_cmdline;
340 if (have_cmdline) --dst;
341 have_cmdline = 1;
342 while ((*dst++ = *src++));
343 src = sn_buf;
344 if (have_cmdline) --dst;
345 have_cmdline = 1;
346 while ((*dst++ = *src++));
347
Pavel Nedev5614d222013-06-17 18:01:02 +0300348 if (boot_into_recovery && gpt_exists) {
349 src = secondary_gpt_enable;
350 if (have_cmdline) --dst;
351 while ((*dst++ = *src++));
352 }
353
Pavel Nedev328ac822013-04-05 15:25:11 +0300354 if (boot_into_ffbm) {
355 src = androidboot_mode;
356 if (have_cmdline) --dst;
357 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700358 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300359 if (have_cmdline) --dst;
360 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800361 src = loglevel;
362 if (have_cmdline) --dst;
363 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300364 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700365 src = battchg_pause;
366 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800367 while ((*dst++ = *src++));
368 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800369
Shashank Mittalcd98d472011-08-02 14:29:24 -0700370 if(target_use_signed_kernel() && auth_kernel_img) {
371 src = auth_kernel;
372 if (have_cmdline) --dst;
373 while ((*dst++ = *src++));
374 }
375
Ajay Dudanid04110c2011-01-17 23:55:07 -0800376 switch(target_baseband())
377 {
378 case BASEBAND_APQ:
379 src = baseband_apq;
380 if (have_cmdline) --dst;
381 while ((*dst++ = *src++));
382 break;
383
384 case BASEBAND_MSM:
385 src = baseband_msm;
386 if (have_cmdline) --dst;
387 while ((*dst++ = *src++));
388 break;
389
390 case BASEBAND_CSFB:
391 src = baseband_csfb;
392 if (have_cmdline) --dst;
393 while ((*dst++ = *src++));
394 break;
395
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800396 case BASEBAND_SVLTE2A:
397 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800398 if (have_cmdline) --dst;
399 while ((*dst++ = *src++));
400 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700401
402 case BASEBAND_MDM:
403 src = baseband_mdm;
404 if (have_cmdline) --dst;
405 while ((*dst++ = *src++));
406 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700407
408 case BASEBAND_SGLTE:
409 src = baseband_sglte;
410 if (have_cmdline) --dst;
411 while ((*dst++ = *src++));
412 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530413
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800414 case BASEBAND_SGLTE2:
415 src = baseband_sglte2;
416 if (have_cmdline) --dst;
417 while ((*dst++ = *src++));
418 break;
419
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530420 case BASEBAND_DSDA:
421 src = baseband_dsda;
422 if (have_cmdline) --dst;
423 while ((*dst++ = *src++));
424 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800425
426 case BASEBAND_DSDA2:
427 src = baseband_dsda2;
428 if (have_cmdline) --dst;
429 while ((*dst++ = *src++));
430 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800431 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700432
433 if (strlen(display_panel_buf)) {
434 src = display_cmdline;
435 if (have_cmdline) --dst;
436 while ((*dst++ = *src++));
437 src = display_panel_buf;
438 if (have_cmdline) --dst;
439 while ((*dst++ = *src++));
440 }
Neeti Desaie245d492012-06-01 12:52:13 -0700441 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700442
443
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800444 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700445 return cmdline_final;
446}
447
448unsigned *atag_core(unsigned *ptr)
449{
450 /* CORE */
451 *ptr++ = 2;
452 *ptr++ = 0x54410001;
453
454 return ptr;
455
456}
457
458unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
459 unsigned ramdisk_size)
460{
461 if (ramdisk_size) {
462 *ptr++ = 4;
463 *ptr++ = 0x54420005;
464 *ptr++ = (unsigned)ramdisk;
465 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800466 }
467
Neeti Desaie245d492012-06-01 12:52:13 -0700468 return ptr;
469}
470
471unsigned *atag_ptable(unsigned **ptr_addr)
472{
473 int i;
474 struct ptable *ptable;
475
476 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700477 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
478 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700479 *(*ptr_addr)++ = 0x4d534d70;
480 for (i = 0; i < ptable->count; ++i)
481 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
482 }
483
484 return (*ptr_addr);
485}
486
487unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
488{
489 int cmdline_length = 0;
490 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700491 char *dest;
492
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800493 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700494 n = (cmdline_length + 4) & (~3);
495
496 *ptr++ = (n / 4) + 2;
497 *ptr++ = 0x54410009;
498 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800499 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700500 ptr += (n / 4);
501
502 return ptr;
503}
504
505unsigned *atag_end(unsigned *ptr)
506{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800507 /* END */
508 *ptr++ = 0;
509 *ptr++ = 0;
510
Neeti Desaie245d492012-06-01 12:52:13 -0700511 return ptr;
512}
513
514void generate_atags(unsigned *ptr, const char *cmdline,
515 void *ramdisk, unsigned ramdisk_size)
516{
517
518 ptr = atag_core(ptr);
519 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
520 ptr = target_atag_mem(ptr);
521
522 /* Skip NAND partition ATAGS for eMMC boot */
523 if (!target_is_emmc_boot()){
524 ptr = atag_ptable(&ptr);
525 }
526
527 ptr = atag_cmdline(ptr, cmdline);
528 ptr = atag_end(ptr);
529}
530
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700531typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700532void boot_linux(void *kernel, unsigned *tags,
533 const char *cmdline, unsigned machtype,
534 void *ramdisk, unsigned ramdisk_size)
535{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800536 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800537#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700538 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800539#endif
540
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700541 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800542 uint32_t tags_phys = PA((addr_t)tags);
543
544 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700545
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800546 final_cmdline = update_cmdline((const char*)cmdline);
547
Neeti Desai17379b82012-06-04 18:42:53 -0700548#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800549 dprintf(INFO, "Updating device tree: start\n");
550
Neeti Desai17379b82012-06-04 18:42:53 -0700551 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800552 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700553 if(ret)
554 {
555 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
556 ASSERT(0);
557 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800558 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700559#else
Neeti Desaie245d492012-06-01 12:52:13 -0700560 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800561 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700562#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700563
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700564 /* Perform target specific cleanup */
565 target_uninit();
566
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800567 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
568 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800569
570 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700571
Amol Jadi4421e652011-06-16 15:00:48 -0700572 /* do any platform specific cleanup before kernel entry */
573 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700574
Brian Swetland9c4c0752009-01-25 16:23:50 -0800575 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700576
Amol Jadi504f9fe2012-08-16 13:56:48 -0700577#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800578 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700579#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700580 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700581 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800582}
583
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700584/* Function to check if the memory address range falls within the aboot
585 * boundaries.
586 * start: Start of the memory region
587 * size: Size of the memory region
588 */
589int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
590{
591 /* Check for boundary conditions. */
592 if ((start + size) < start)
593 return -1;
594
595 /* Check for memory overlap. */
596 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
597 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700598 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700599 return 0;
600 else
601 return -1;
602}
603
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800604#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800605
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700606BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800607#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700608BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800609#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800610
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700611static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
612{
613 int ret;
614
615 /* Assume device is rooted at this time. */
616 device.is_tampered = 1;
617
618 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
619
620 ret = image_verify((unsigned char *)bootimg_addr,
621 (unsigned char *)(bootimg_addr + bootimg_size),
622 bootimg_size,
623 CRYPTO_AUTH_ALG_SHA256);
624
625 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
626
627 if (ret)
628 {
629 /* Authorized kernel */
630 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700631 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700632 }
633
634#if USE_PCOM_SECBOOT
635 set_tamper_flag(device.is_tampered);
636#endif
637
638 if(device.is_tampered)
639 {
640 write_device_info_mmc(&device);
641 #ifdef TZ_TAMPER_FUSE
642 set_tamper_fuse_cmd();
643 #endif
644 #ifdef ASSERT_ON_TAMPER
645 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
646 ASSERT(0);
647 #endif
648 }
649}
650
Shashank Mittal23b8f422010-04-16 19:27:21 -0700651int boot_linux_from_mmc(void)
652{
653 struct boot_img_hdr *hdr = (void*) buf;
654 struct boot_img_hdr *uhdr;
655 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700656 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700657 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700658 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700659
Shashank Mittalcd98d472011-08-02 14:29:24 -0700660 unsigned char *image_addr = 0;
661 unsigned kernel_actual;
662 unsigned ramdisk_actual;
663 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700664 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700665
666#if DEVICE_TREE
667 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700668 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700669 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800670 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700671 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700672#endif
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700673 if (!boot_into_recovery) {
674 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
675 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
676 if (rcode <= 0) {
677 boot_into_ffbm = false;
678 if (rcode < 0)
679 dprintf(CRITICAL,"failed to get ffbm cookie");
680 } else
681 boot_into_ffbm = true;
682 } else
683 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700684 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
685 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
686 dprintf(INFO, "Unified boot method!\n");
687 hdr = uhdr;
688 goto unified_boot;
689 }
Greg Griscod6250552011-06-29 14:40:23 -0700690 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700691 index = partition_get_index("boot");
692 ptn = partition_get_offset(index);
693 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700694 dprintf(CRITICAL, "ERROR: No boot partition found\n");
695 return -1;
696 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700697 }
698 else {
699 index = partition_get_index("recovery");
700 ptn = partition_get_offset(index);
701 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700702 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
703 return -1;
704 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700705 }
706
Greg Griscod6250552011-06-29 14:40:23 -0700707 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700708 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
709 return -1;
710 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700711
712 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700713 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700714 return -1;
715 }
716
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700717 if (hdr->page_size && (hdr->page_size != page_size)) {
718 page_size = hdr->page_size;
719 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700720 }
721
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700722 /*
723 * Update the kernel/ramdisk/tags address if the boot image header
724 * has default values, these default values come from mkbootimg when
725 * the boot image is flashed using fastboot flash:raw
726 */
727 update_ker_tags_rdisk_addr(hdr);
728
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700729 /* Get virtual addresses since the hdr saves physical addresses. */
730 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
731 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
732 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700733
734 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
735 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
736
737 /* Check if the addresses in the header are valid. */
738 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
739 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
740 {
741 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
742 return -1;
743 }
744
745#ifndef DEVICE_TREE
746 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
747 {
748 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
749 return -1;
750 }
751#endif
752
Shashank Mittalcd98d472011-08-02 14:29:24 -0700753 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300754 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
755 (int) target_use_signed_kernel(),
756 device.is_unlocked,
757 device.is_tampered);
758
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700759 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800760 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700761 offset = 0;
762
Shashank Mittalcd98d472011-08-02 14:29:24 -0700763 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700764
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800765#if DEVICE_TREE
766 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
767 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700768
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700769 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700770 {
771 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
772 return -1;
773 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800774#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530775 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700776
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700777#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700778
Amol Jadib6be5c12012-11-14 13:39:51 -0800779 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700780 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800781
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700782 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
783 {
784 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
785 return -1;
786 }
787
Shashank Mittalcd98d472011-08-02 14:29:24 -0700788 /* Read image without signature */
789 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
790 {
791 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
792 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800793 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700794
Amol Jadib6be5c12012-11-14 13:39:51 -0800795 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700796 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800797
Shashank Mittalcd98d472011-08-02 14:29:24 -0700798 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700799
800 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
801 {
802 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
803 return -1;
804 }
805
Shashank Mittalcd98d472011-08-02 14:29:24 -0700806 /* Read signature */
807 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
808 {
809 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700810 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700811 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800812
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700813 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700814
Neeti Desai465491e2012-07-31 12:53:35 -0700815 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700816 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
817 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700818
Neeti Desai465491e2012-07-31 12:53:35 -0700819 #if DEVICE_TREE
820 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700821 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700822 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700823
Deepa Dinamani19648b42013-09-05 17:05:55 -0700824 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700825 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
826 return -1;
827 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700828
Neeti Desai465491e2012-07-31 12:53:35 -0700829 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700830 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700831 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
832 return -1;
833 }
834
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700835 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700836 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700837 {
838 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
839 return -1;
840 }
841
Joel Kingaa335dc2013-06-03 16:11:08 -0700842 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800843 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800844 /*
845 * If appended dev tree is found, update the atags with
846 * memory address to the DTB appended location on RAM.
847 * Else update with the atags address in the kernel header
848 */
849 void *dtb;
850 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700851 hdr->kernel_size,
852 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800853 if (!dtb) {
854 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
855 return -1;
856 }
Neeti Desai465491e2012-07-31 12:53:35 -0700857 }
858 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700859 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700860 else
861 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800862 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700863
Amol Jadib6be5c12012-11-14 13:39:51 -0800864 dprintf(INFO, "Loading boot image (%d): start\n",
865 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700866 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800867
868 offset = page_size;
869
870 /* Load kernel */
871 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700872 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
873 return -1;
874 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800875 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700876
Amol Jadib6be5c12012-11-14 13:39:51 -0800877 /* Load ramdisk */
878 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700879 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800880 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700881 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
882 return -1;
883 }
884 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800885 offset += ramdisk_actual;
886
887 dprintf(INFO, "Loading boot image (%d): done\n",
888 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700889 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700890
891 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800892 offset += second_actual;
893 /* Second image loading not implemented. */
894 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700895 }
896
897 #if DEVICE_TREE
898 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -0700899 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -0700900 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
901 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
902 return -1;
903 }
904 table = (struct dt_table*) dt_buf;
905
Deepa Dinamani19648b42013-09-05 17:05:55 -0700906 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700907 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
908 return -1;
909 }
910
Deepa Dinamani19648b42013-09-05 17:05:55 -0700911 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
912 if (!table)
913 return -1;
914
915 /* Read the entire device tree table into buffer */
916 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
917 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
918 return -1;
919 }
920
Joel Kingaa335dc2013-06-03 16:11:08 -0700921 /* Find index of device tree within device tree table */
922 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700923 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
924 return -1;
925 }
926
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700927 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700928 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700929 {
930 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
931 return -1;
932 }
933
Joel Kingaa335dc2013-06-03 16:11:08 -0700934 if(mmc_read(ptn + offset + dt_entry.offset,
935 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700936 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
937 return -1;
938 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300939 #ifdef TZ_SAVE_KERNEL_HASH
940 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
941 hdr->ramdisk_addr, ramdisk_actual,
942 ptn, offset, hdr->dt_size);
943 #endif /* TZ_SAVE_KERNEL_HASH */
944
Channagoud Kadabi35095622013-03-01 13:53:05 -0800945 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800946 /*
947 * If appended dev tree is found, update the atags with
948 * memory address to the DTB appended location on RAM.
949 * Else update with the atags address in the kernel header
950 */
951 void *dtb;
952 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700953 kernel_actual,
954 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800955 if (!dtb) {
956 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
957 return -1;
958 }
Neeti Desai465491e2012-07-31 12:53:35 -0700959 }
960 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700961 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700962
Stanimir Varbanov69ec5462013-07-18 18:17:42 +0300963 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
964 target_load_ssd_keystore();
965
Shashank Mittal23b8f422010-04-16 19:27:21 -0700966unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700967
Dima Zavin77e41f32013-03-06 16:10:43 -0800968 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700969 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700970 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
971
972 return 0;
973}
974
Dima Zavin214cc642009-01-26 11:16:21 -0800975int boot_linux_from_flash(void)
976{
977 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800978 struct ptentry *ptn;
979 struct ptable *ptable;
980 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800981
Shashank Mittalcd98d472011-08-02 14:29:24 -0700982 unsigned char *image_addr = 0;
983 unsigned kernel_actual;
984 unsigned ramdisk_actual;
985 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800986 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700987
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700988#if DEVICE_TREE
989 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700990 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800991 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700992 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700993#endif
994
David Ng183a7422009-12-07 14:55:21 -0800995 if (target_is_emmc_boot()) {
996 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
997 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
998 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
999 return -1;
1000 }
1001 goto continue_boot;
1002 }
1003
Dima Zavin214cc642009-01-26 11:16:21 -08001004 ptable = flash_get_ptable();
1005 if (ptable == NULL) {
1006 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1007 return -1;
1008 }
1009
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001010 if(!boot_into_recovery)
1011 {
1012 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001013
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001014 if (ptn == NULL) {
1015 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1016 return -1;
1017 }
1018 }
1019 else
1020 {
1021 ptn = ptable_find(ptable, "recovery");
1022 if (ptn == NULL) {
1023 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1024 return -1;
1025 }
Dima Zavin214cc642009-01-26 11:16:21 -08001026 }
1027
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001028 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001029 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1030 return -1;
1031 }
Dima Zavin214cc642009-01-26 11:16:21 -08001032
1033 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001034 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001035 return -1;
1036 }
1037
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001038 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001039 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 -08001040 return -1;
1041 }
1042
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001043 /*
1044 * Update the kernel/ramdisk/tags address if the boot image header
1045 * has default values, these default values come from mkbootimg when
1046 * the boot image is flashed using fastboot flash:raw
1047 */
1048 update_ker_tags_rdisk_addr(hdr);
1049
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001050 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001051 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1052 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1053 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1054
1055 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1056 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1057
1058 /* Check if the addresses in the header are valid. */
1059 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1060 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1061 {
1062 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1063 return -1;
1064 }
1065
1066#ifndef DEVICE_TREE
1067 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1068 {
1069 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1070 return -1;
1071 }
1072#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001073
Shashank Mittalcd98d472011-08-02 14:29:24 -07001074 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001075 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001076 {
1077 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001078 offset = 0;
1079
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001080#if DEVICE_TREE
1081 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1082 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001083
1084 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1085 {
1086 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1087 return -1;
1088 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001089#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001090 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001091#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001092
Amol Jadib6be5c12012-11-14 13:39:51 -08001093 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001094 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001095
Shashank Mittalcd98d472011-08-02 14:29:24 -07001096 /* Read image without signature */
1097 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1098 {
1099 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1100 return -1;
1101 }
Dima Zavin214cc642009-01-26 11:16:21 -08001102
Amol Jadib6be5c12012-11-14 13:39:51 -08001103 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001104 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001105
Shashank Mittalcd98d472011-08-02 14:29:24 -07001106 offset = imagesize_actual;
1107 /* Read signature */
1108 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1109 {
1110 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001111 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001112 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001113
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001114 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001115
1116 /* Move kernel and ramdisk to correct address */
1117 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1118 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001119#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001120 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001121 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001122 {
1123 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1124 return -1;
1125 }
1126
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001127 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1128#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001129
1130 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001131 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001132 {
1133 write_device_info_flash(&device);
1134 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301135#if USE_PCOM_SECBOOT
1136 set_tamper_flag(device.is_tampered);
1137#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001138 }
1139 else
1140 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001141 offset = page_size;
1142
Amol Jadib6be5c12012-11-14 13:39:51 -08001143 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1144 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1145 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1146
1147 dprintf(INFO, "Loading boot image (%d): start\n",
1148 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001149 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001150
1151 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001152 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1153 return -1;
1154 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001155 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001156
Amol Jadib6be5c12012-11-14 13:39:51 -08001157 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001158 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1159 return -1;
1160 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001161 offset += ramdisk_actual;
1162
1163 dprintf(INFO, "Loading boot image (%d): done\n",
1164 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001165 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001166
1167 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001168 offset += second_actual;
1169 /* Second image loading not implemented. */
1170 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001171 }
1172
1173#if DEVICE_TREE
1174 if(hdr->dt_size != 0) {
1175
1176 /* Read the device tree table into buffer */
1177 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1178 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1179 return -1;
1180 }
1181
1182 table = (struct dt_table*) dt_buf;
1183
Deepa Dinamani19648b42013-09-05 17:05:55 -07001184 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001185 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1186 return -1;
1187 }
1188
Deepa Dinamani19648b42013-09-05 17:05:55 -07001189 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1190 if (!table)
1191 return -1;
1192
1193 /* Read the entire device tree table into buffer */
1194 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1195 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1196 return -1;
1197 }
1198
1199
Joel Kingaa335dc2013-06-03 16:11:08 -07001200 /* Find index of device tree within device tree table */
1201 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001202 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1203 return -1;
1204 }
1205
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001206 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001207 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001208 {
1209 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1210 return -1;
1211 }
1212
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001213 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001214 if(flash_read(ptn, offset + dt_entry.offset,
1215 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001216 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1217 return -1;
1218 }
1219 }
1220#endif
1221
Shashank Mittalcd98d472011-08-02 14:29:24 -07001222 }
David Ng183a7422009-12-07 14:55:21 -08001223continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001224
Dima Zavin214cc642009-01-26 11:16:21 -08001225 /* TODO: create/pass atags to kernel */
1226
Ajay Dudanie28a6072011-07-01 13:59:46 -07001227 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001228 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001229 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1230
1231 return 0;
1232}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001233
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001234BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001235void write_device_info_mmc(device_info *dev)
1236{
1237 struct device_info *info = (void*) info_buf;
1238 unsigned long long ptn = 0;
1239 unsigned long long size;
1240 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001241 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001242
1243 index = partition_get_index("aboot");
1244 ptn = partition_get_offset(index);
1245 if(ptn == 0)
1246 {
1247 return;
1248 }
1249
1250 size = partition_get_size(index);
1251
1252 memcpy(info, dev, sizeof(device_info));
1253
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001254 blocksize = mmc_get_device_blocksize();
1255
1256 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Shashank Mittal162244e2011-08-08 19:01:25 -07001257 {
1258 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1259 return;
1260 }
1261}
1262
1263void read_device_info_mmc(device_info *dev)
1264{
1265 struct device_info *info = (void*) info_buf;
1266 unsigned long long ptn = 0;
1267 unsigned long long size;
1268 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001269 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001270
1271 index = partition_get_index("aboot");
1272 ptn = partition_get_offset(index);
1273 if(ptn == 0)
1274 {
1275 return;
1276 }
1277
1278 size = partition_get_size(index);
1279
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001280 blocksize = mmc_get_device_blocksize();
1281
1282 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Shashank Mittal162244e2011-08-08 19:01:25 -07001283 {
1284 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1285 return;
1286 }
1287
1288 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1289 {
1290 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1291 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001292 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001293 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001294
1295 write_device_info_mmc(info);
1296 }
1297 memcpy(dev, info, sizeof(device_info));
1298}
1299
1300void write_device_info_flash(device_info *dev)
1301{
1302 struct device_info *info = (void *) info_buf;
1303 struct ptentry *ptn;
1304 struct ptable *ptable;
1305
1306 ptable = flash_get_ptable();
1307 if (ptable == NULL)
1308 {
1309 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1310 return;
1311 }
1312
1313 ptn = ptable_find(ptable, "devinfo");
1314 if (ptn == NULL)
1315 {
1316 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1317 return;
1318 }
1319
1320 memcpy(info, dev, sizeof(device_info));
1321
1322 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1323 {
1324 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1325 return;
1326 }
1327}
1328
1329void read_device_info_flash(device_info *dev)
1330{
1331 struct device_info *info = (void*) info_buf;
1332 struct ptentry *ptn;
1333 struct ptable *ptable;
1334
1335 ptable = flash_get_ptable();
1336 if (ptable == NULL)
1337 {
1338 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1339 return;
1340 }
1341
1342 ptn = ptable_find(ptable, "devinfo");
1343 if (ptn == NULL)
1344 {
1345 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1346 return;
1347 }
1348
1349 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1350 {
1351 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1352 return;
1353 }
1354
1355 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1356 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001357 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1358 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001359 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001360 write_device_info_flash(info);
1361 }
1362 memcpy(dev, info, sizeof(device_info));
1363}
1364
1365void write_device_info(device_info *dev)
1366{
1367 if(target_is_emmc_boot())
1368 {
1369 write_device_info_mmc(dev);
1370 }
1371 else
1372 {
1373 write_device_info_flash(dev);
1374 }
1375}
1376
1377void read_device_info(device_info *dev)
1378{
1379 if(target_is_emmc_boot())
1380 {
1381 read_device_info_mmc(dev);
1382 }
1383 else
1384 {
1385 read_device_info_flash(dev);
1386 }
1387}
1388
1389void reset_device_info()
1390{
1391 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001392 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001393 write_device_info(&device);
1394}
1395
1396void set_device_root()
1397{
1398 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001399 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001400 write_device_info(&device);
1401}
1402
Amol Jadicb524072012-08-09 16:40:18 -07001403#if DEVICE_TREE
1404int copy_dtb(uint8_t *boot_image_start)
1405{
1406 uint32 dt_image_offset = 0;
1407 uint32_t n;
1408 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001409 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001410 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001411
1412 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1413
Amol Jadicb524072012-08-09 16:40:18 -07001414 if(hdr->dt_size != 0) {
1415
1416 /* add kernel offset */
1417 dt_image_offset += page_size;
1418 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1419 dt_image_offset += n;
1420
1421 /* add ramdisk offset */
1422 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1423 dt_image_offset += n;
1424
1425 /* add second offset */
1426 if(hdr->second_size != 0) {
1427 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1428 dt_image_offset += n;
1429 }
1430
1431 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001432 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001433
Deepa Dinamani19648b42013-09-05 17:05:55 -07001434 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001435 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1436 return -1;
1437 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001438 /* Find index of device tree within device tree table */
1439 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001440 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1441 return -1;
1442 }
1443
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001444 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001445 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001446 {
1447 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1448 return -1;
1449 }
1450
Amol Jadicb524072012-08-09 16:40:18 -07001451 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001452 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001453 boot_image_start + dt_image_offset + dt_entry.offset,
1454 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001455 } else
1456 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001457
1458 /* Everything looks fine. Return success. */
1459 return 0;
1460}
1461#endif
1462
Brian Swetland9c4c0752009-01-25 16:23:50 -08001463void cmd_boot(const char *arg, void *data, unsigned sz)
1464{
1465 unsigned kernel_actual;
1466 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001467 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001468 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001469 int ret = 0;
1470 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001471
1472 if (sz < sizeof(hdr)) {
1473 fastboot_fail("invalid bootimage header");
1474 return;
1475 }
1476
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001477 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001478
1479 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001480 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001481
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001482 if(target_is_emmc_boot() && hdr->page_size) {
1483 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001484 page_mask = page_size - 1;
1485 }
1486
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001487 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1488 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1489
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001490 /*
1491 * Update the kernel/ramdisk/tags address if the boot image header
1492 * has default values, these default values come from mkbootimg when
1493 * the boot image is flashed using fastboot flash:raw
1494 */
1495 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001496
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001497 /* Get virtual addresses since the hdr saves physical addresses. */
1498 hdr->kernel_addr = VA(hdr->kernel_addr);
1499 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1500 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001501
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001502 /* Check if the addresses in the header are valid. */
1503 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1504 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1505 {
1506 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1507 return -1;
1508 }
1509
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001510 /* sz should have atleast raw boot image */
1511 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001512 fastboot_fail("incomplete bootimage");
1513 return;
1514 }
1515
Amol Jadicb524072012-08-09 16:40:18 -07001516#if DEVICE_TREE
1517 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001518 ret = copy_dtb(data);
1519
1520 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001521#else
1522 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1523 {
1524 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1525 return -1;
1526 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001527#endif
1528
1529 /* Load ramdisk & kernel */
1530 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1531 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1532
1533#if DEVICE_TREE
1534 /*
1535 * If dtb is not found look for appended DTB in the kernel.
1536 * If appended dev tree is found, update the atags with
1537 * memory address to the DTB appended location on RAM.
1538 * Else update with the atags address in the kernel header
1539 */
1540 if (!dtb_copied) {
1541 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001542 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1543 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001544 if (!dtb) {
1545 fastboot_fail("dtb not found");
1546 return;
1547 }
Amol Jadicb524072012-08-09 16:40:18 -07001548 }
1549#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001550
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001551#ifndef DEVICE_TREE
1552 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1553 {
1554 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1555 return -1;
1556 }
1557#endif
1558
Brian Swetland9c4c0752009-01-25 16:23:50 -08001559 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001560 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001561
Dima Zavin77e41f32013-03-06 16:10:43 -08001562 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001563 (const char*) hdr->cmdline, board_machtype(),
1564 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001565}
1566
Dima Zavin214cc642009-01-26 11:16:21 -08001567void cmd_erase(const char *arg, void *data, unsigned sz)
1568{
1569 struct ptentry *ptn;
1570 struct ptable *ptable;
1571
1572 ptable = flash_get_ptable();
1573 if (ptable == NULL) {
1574 fastboot_fail("partition table doesn't exist");
1575 return;
1576 }
1577
1578 ptn = ptable_find(ptable, arg);
1579 if (ptn == NULL) {
1580 fastboot_fail("unknown partition name");
1581 return;
1582 }
1583
1584 if (flash_erase(ptn)) {
1585 fastboot_fail("failed to erase partition");
1586 return;
1587 }
1588 fastboot_okay("");
1589}
1590
Bikas Gurungd48bd242010-09-04 19:54:32 -07001591
1592void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1593{
Kun Liang2f1601a2013-08-12 16:29:54 +08001594 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001595 unsigned long long ptn = 0;
Kun Liang2f1601a2013-08-12 16:29:54 +08001596 unsigned long long size;
Kinson Chikf1a43512011-07-14 11:28:39 -07001597 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001598
Kinson Chikf1a43512011-07-14 11:28:39 -07001599 index = partition_get_index(arg);
1600 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001601
Kinson Chikf1a43512011-07-14 11:28:39 -07001602 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001603 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001604 return;
1605 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001606
1607 size = partition_get_size(index);
1608 if (size > DEFAULT_ERASE_SIZE)
1609 size = DEFAULT_ERASE_SIZE;
1610
neetidb4b24d62012-01-20 12:13:09 -08001611 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001612 0 in first several blocks */
1613 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001614 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001615 return;
1616 }
1617 fastboot_okay("");
1618}
1619
1620
Ajay Dudani5c761132011-04-07 20:19:04 -07001621void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001622{
1623 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001624 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001625 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001626
Greg Grisco6e754772011-06-23 12:19:39 -07001627 if (!strcmp(arg, "partition"))
1628 {
1629 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001630 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001631 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001632 return;
1633 }
1634 }
Greg Grisco6e754772011-06-23 12:19:39 -07001635 else
1636 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001637 index = partition_get_index(arg);
1638 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001639 if(ptn == 0) {
1640 fastboot_fail("partition table doesn't exist");
1641 return;
1642 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001643
Greg Grisco6e754772011-06-23 12:19:39 -07001644 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1645 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1646 fastboot_fail("image is not a boot image");
1647 return;
1648 }
1649 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001650
Kinson Chikf1a43512011-07-14 11:28:39 -07001651 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001652 if (ROUND_TO_PAGE(sz,511) > size) {
1653 fastboot_fail("size too large");
1654 return;
1655 }
1656 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1657 fastboot_fail("flash write failure");
1658 return;
1659 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001660 }
1661 fastboot_okay("");
1662 return;
1663}
1664
Ajay Dudani5c761132011-04-07 20:19:04 -07001665void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1666{
1667 unsigned int chunk;
1668 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001669 uint32_t *fill_buf = NULL;
1670 uint32_t fill_val;
1671 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001672 sparse_header_t *sparse_header;
1673 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001674 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001675 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301676 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001677 int index = INVALID_PTN;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001678 int i;
Ajay Dudani5c761132011-04-07 20:19:04 -07001679
Kinson Chikf1a43512011-07-14 11:28:39 -07001680 index = partition_get_index(arg);
1681 ptn = partition_get_offset(index);
1682 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001683 fastboot_fail("partition table doesn't exist");
1684 return;
1685 }
1686
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301687 size = partition_get_size(index);
1688 if (ROUND_TO_PAGE(sz,511) > size) {
1689 fastboot_fail("size too large");
1690 return;
1691 }
1692
Ajay Dudani5c761132011-04-07 20:19:04 -07001693 /* Read and skip over sparse image header */
1694 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001695 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1696 fastboot_fail("size too large");
1697 return;
1698 }
1699
Ajay Dudani5c761132011-04-07 20:19:04 -07001700 data += sparse_header->file_hdr_sz;
1701 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1702 {
1703 /* Skip the remaining bytes in a header that is longer than
1704 * we expected.
1705 */
1706 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1707 }
1708
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001709 dprintf (SPEW, "=== Sparse Image Header ===\n");
1710 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1711 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1712 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1713 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1714 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1715 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1716 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1717 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001718
1719 /* Start processing chunks */
1720 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1721 {
1722 /* Read and skip over chunk header */
1723 chunk_header = (chunk_header_t *) data;
1724 data += sizeof(chunk_header_t);
1725
1726 dprintf (SPEW, "=== Chunk Header ===\n");
1727 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1728 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1729 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1730
1731 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1732 {
1733 /* Skip the remaining bytes in a header that is longer than
1734 * we expected.
1735 */
1736 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1737 }
1738
1739 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1740 switch (chunk_header->chunk_type)
1741 {
1742 case CHUNK_TYPE_RAW:
1743 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1744 chunk_data_sz))
1745 {
1746 fastboot_fail("Bogus chunk size for chunk type Raw");
1747 return;
1748 }
1749
Ajay Dudaniab18f022011-05-12 14:39:22 -07001750 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1751 chunk_data_sz,
1752 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001753 {
1754 fastboot_fail("flash write failure");
1755 return;
1756 }
1757 total_blocks += chunk_header->chunk_sz;
1758 data += chunk_data_sz;
1759 break;
1760
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001761 case CHUNK_TYPE_FILL:
1762 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1763 sizeof(uint32_t)))
1764 {
1765 fastboot_fail("Bogus chunk size for chunk type FILL");
1766 return;
1767 }
1768
1769 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
1770 if (!fill_buf)
1771 {
1772 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
1773 return;
1774 }
1775
1776 fill_val = *(uint32_t *)data;
1777 data = (char *) data + sizeof(uint32_t);
1778 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
1779
1780 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
1781 {
1782 fill_buf[i] = fill_val;
1783 }
1784
1785 for (i = 0; i < chunk_blk_cnt; i++)
1786 {
1787 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1788 sparse_header->blk_sz,
1789 fill_buf))
1790 {
1791 fastboot_fail("flash write failure");
1792 free(fill_buf);
1793 return;
1794 }
1795
1796 total_blocks++;
1797 }
1798
1799 free(fill_buf);
1800 break;
1801
Ajay Dudani5c761132011-04-07 20:19:04 -07001802 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001803 total_blocks += chunk_header->chunk_sz;
1804 break;
1805
Ajay Dudani5c761132011-04-07 20:19:04 -07001806 case CHUNK_TYPE_CRC:
1807 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1808 {
1809 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1810 return;
1811 }
1812 total_blocks += chunk_header->chunk_sz;
1813 data += chunk_data_sz;
1814 break;
1815
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001816 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001817 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07001818 fastboot_fail("Unknown chunk type");
1819 return;
1820 }
1821 }
1822
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001823 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1824 total_blocks, sparse_header->total_blks);
1825
1826 if(total_blocks != sparse_header->total_blks)
1827 {
1828 fastboot_fail("sparse image write failure");
1829 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001830
1831 fastboot_okay("");
1832 return;
1833}
1834
1835void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1836{
1837 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001838 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1839 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001840
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001841#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001842 int ret=0;
1843 uint32 major_version=0;
1844 uint32 minor_version=0;
1845
1846 ret = scm_svc_version(&major_version,&minor_version);
1847 if(!ret)
1848 {
1849 if(major_version >= 2)
1850 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001851 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001852 {
1853 ret = encrypt_scm((uint32 **) &data, &sz);
1854 if (ret != 0) {
1855 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1856 return;
1857 }
1858
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001859 /* Protect only for SSD */
1860 if (!strcmp(arg, "ssd")) {
1861 ret = scm_protect_keystore((uint32 *) data, sz);
1862 if (ret != 0) {
1863 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1864 return;
1865 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001866 }
1867 }
1868 else
1869 {
1870 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1871 if(ret != 0)
1872 {
1873 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1874 return;
1875 }
1876 }
1877 }
1878 else
1879 {
1880 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1881 magic_number[1] == DECRYPT_MAGIC_1)
1882 {
1883 ret = decrypt_scm((uint32 **) &data, &sz);
1884 if (ret != 0) {
1885 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1886 return;
1887 }
1888 }
1889 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1890 magic_number[1] == ENCRYPT_MAGIC_1)
1891 {
1892 ret = encrypt_scm((uint32 **) &data, &sz);
1893 if (ret != 0) {
1894 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1895 return;
1896 }
1897 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001898 }
1899 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001900 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001901 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001902 dprintf(CRITICAL,"INVALID SVC Version\n");
1903 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001904 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001905#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001906
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001907 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001908 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1909 cmd_flash_mmc_img(arg, data, sz);
1910 else
1911 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001912 return;
1913}
1914
Dima Zavin214cc642009-01-26 11:16:21 -08001915void cmd_flash(const char *arg, void *data, unsigned sz)
1916{
1917 struct ptentry *ptn;
1918 struct ptable *ptable;
1919 unsigned extra = 0;
1920
1921 ptable = flash_get_ptable();
1922 if (ptable == NULL) {
1923 fastboot_fail("partition table doesn't exist");
1924 return;
1925 }
1926
1927 ptn = ptable_find(ptable, arg);
1928 if (ptn == NULL) {
1929 fastboot_fail("unknown partition name");
1930 return;
1931 }
1932
1933 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1934 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1935 fastboot_fail("image is not a boot image");
1936 return;
1937 }
1938 }
1939
Amol Jadi5c61a952012-05-04 17:05:35 -07001940 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001941 || !strcmp(ptn->name, "userdata")
1942 || !strcmp(ptn->name, "persist")
1943 || !strcmp(ptn->name, "recoveryfs")) {
Deepa Dinamaniea177912013-04-30 15:51:10 -07001944 extra = 1;
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301945 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001946 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001947
1948 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1949 if (flash_write(ptn, extra, data, sz)) {
1950 fastboot_fail("flash write failure");
1951 return;
1952 }
1953 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1954 fastboot_okay("");
1955}
1956
1957void cmd_continue(const char *arg, void *data, unsigned sz)
1958{
1959 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001960 fastboot_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001961 if (target_is_emmc_boot())
1962 {
1963 boot_linux_from_mmc();
1964 }
1965 else
1966 {
1967 boot_linux_from_flash();
1968 }
Dima Zavin214cc642009-01-26 11:16:21 -08001969}
1970
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001971void cmd_reboot(const char *arg, void *data, unsigned sz)
1972{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001973 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001974 fastboot_okay("");
1975 reboot_device(0);
1976}
1977
1978void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1979{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001980 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001981 fastboot_okay("");
1982 reboot_device(FASTBOOT_MODE);
1983}
1984
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001985void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
1986{
1987 dprintf(INFO, "Enabling charger screen check\n");
1988 device.charger_screen_enabled = 1;
1989 write_device_info(&device);
1990 fastboot_okay("");
1991}
1992
1993void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
1994{
1995 dprintf(INFO, "Disabling charger screen check\n");
1996 device.charger_screen_enabled = 0;
1997 write_device_info(&device);
1998 fastboot_okay("");
1999}
2000
Shashank Mittal162244e2011-08-08 19:01:25 -07002001void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2002{
2003 if(!device.is_unlocked)
2004 {
2005 device.is_unlocked = 1;
2006 write_device_info(&device);
2007 }
2008 fastboot_okay("");
2009}
2010
Shashank Mittala0032282011-08-26 14:50:11 -07002011void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2012{
2013 char response[64];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002014 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002015 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002016 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2017 fastboot_info(response);
2018 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002019 fastboot_info(response);
2020 fastboot_okay("");
2021}
2022
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002023void cmd_preflash(const char *arg, void *data, unsigned sz)
2024{
2025 fastboot_okay("");
2026}
2027
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302028static struct fbimage logo_header = {0};
2029struct fbimage* splash_screen_flash();
2030
2031int splash_screen_check_header(struct fbimage *logo)
2032{
2033 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2034 return -1;
2035 if (logo->header.width == 0 || logo->header.height == 0)
2036 return -1;
2037 return 0;
2038}
2039
2040struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002041{
2042 struct ptentry *ptn;
2043 struct ptable *ptable;
2044 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302045 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002046
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302047
2048 ptable = flash_get_ptable();
2049 if (ptable == NULL) {
2050 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2051 return NULL;
2052 }
2053 ptn = ptable_find(ptable, "splash");
2054 if (ptn == NULL) {
2055 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2056 return NULL;
2057 }
2058
2059 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2060 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2061 return NULL;
2062 }
2063
2064 if (splash_screen_check_header(logo)) {
2065 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2066 return NULL;
2067 }
2068
2069 fb_display = fbcon_display();
2070 if (fb_display) {
2071 uint8_t *base = (uint8_t *) fb_display->base;
2072 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
2073 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002074 }
2075
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302076 if (flash_read(ptn + sizeof(logo->header), 0,
2077 base,
2078 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2079 fbcon_clear();
2080 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2081 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002082 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302083 logo->image = base;
2084 }
2085
2086 return logo;
2087}
2088
2089struct fbimage* splash_screen_mmc()
2090{
2091 int index = INVALID_PTN;
2092 unsigned long long ptn = 0;
2093 struct fbcon_config *fb_display = NULL;
2094 struct fbimage *logo = &logo_header;
2095
2096 index = partition_get_index("splash");
2097 if (index == 0) {
2098 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2099 return NULL;
2100 }
2101
2102 ptn = partition_get_offset(index);
2103 if (ptn == 0) {
2104 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2105 return NULL;
2106 }
2107
2108 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2109 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2110 return NULL;
2111 }
2112
2113 if (splash_screen_check_header(logo)) {
2114 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2115 return NULL;
2116 }
2117
2118 fb_display = fbcon_display();
2119 if (fb_display) {
2120 uint8_t *base = (uint8_t *) fb_display->base;
2121 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2122 base += LOGO_IMG_OFFSET;
2123
2124 if (mmc_read(ptn + sizeof(logo->header),
2125 base,
2126 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2127 fbcon_clear();
2128 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2129 return NULL;
2130 }
2131
2132 logo->image = base;
2133 }
2134
2135 return logo;
2136}
2137
2138
2139struct fbimage* fetch_image_from_partition()
2140{
2141 if (target_is_emmc_boot()) {
2142 return splash_screen_mmc();
2143 } else {
2144 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002145 }
2146}
2147
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002148/* Get the size from partiton name */
2149static void get_partition_size(const char *arg, char *response)
2150{
2151 uint64_t ptn = 0;
2152 uint64_t size;
2153 int index = INVALID_PTN;
2154
2155 index = partition_get_index(arg);
2156
2157 if (index == INVALID_PTN)
2158 {
2159 dprintf(CRITICAL, "Invalid partition index\n");
2160 return;
2161 }
2162
2163 ptn = partition_get_offset(index);
2164
2165 if(!ptn)
2166 {
2167 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2168 return;
2169 }
2170
2171 size = partition_get_size(index);
2172
2173 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2174 return;
2175}
2176
2177/*
2178 * Publish the partition type & size info
2179 * fastboot getvar will publish the required information.
2180 * fastboot getvar partition_size:<partition_name>: partition size in hex
2181 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2182 */
2183static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2184{
2185 uint8_t i;
2186
2187 for (i = 0; i < num_parts; i++) {
2188 get_partition_size(info[i].part_name, info[i].size_response);
2189
2190 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2191 {
2192 dprintf(CRITICAL, "partition size name truncated\n");
2193 return;
2194 }
2195 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2196 {
2197 dprintf(CRITICAL, "partition type name truncated\n");
2198 return;
2199 }
2200
2201 /* publish partition size & type info */
2202 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2203 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2204 }
2205}
2206
Amol Jadi5edf3552013-07-23 14:15:34 -07002207/* register commands and variables for fastboot */
2208void aboot_fastboot_register_commands(void)
2209{
2210 if (target_is_emmc_boot())
2211 {
2212 fastboot_register("flash:", cmd_flash_mmc);
2213 fastboot_register("erase:", cmd_erase_mmc);
2214 }
2215 else
2216 {
2217 fastboot_register("flash:", cmd_flash);
2218 fastboot_register("erase:", cmd_erase);
2219 }
2220
2221 fastboot_register("boot", cmd_boot);
2222 fastboot_register("continue", cmd_continue);
2223 fastboot_register("reboot", cmd_reboot);
2224 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2225 fastboot_register("oem unlock", cmd_oem_unlock);
2226 fastboot_register("oem device-info", cmd_oem_devinfo);
2227 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002228 fastboot_register("oem enable-charger-screen",
2229 cmd_oem_enable_charger_screen);
2230 fastboot_register("oem disable-charger-screen",
2231 cmd_oem_disable_charger_screen);
Amol Jadi5edf3552013-07-23 14:15:34 -07002232 /* publish variables and their values */
2233 fastboot_publish("product", TARGET(BOARD));
2234 fastboot_publish("kernel", "lk");
2235 fastboot_publish("serialno", sn_buf);
2236
2237 /*
2238 * partition info is supported only for emmc partitions
2239 * Calling this for NAND prints some error messages which
2240 * is harmless but misleading. Avoid calling this for NAND
2241 * devices.
2242 */
2243 if (target_is_emmc_boot())
2244 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2245
2246 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002247 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2248 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002249 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002250 /* Is the charger screen check enabled */
2251 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2252 device.charger_screen_enabled);
2253 fastboot_publish("charger-screen-enabled",
2254 (const char *) charger_screen_enabled);
Amol Jadi5edf3552013-07-23 14:15:34 -07002255}
2256
Brian Swetland9c4c0752009-01-25 16:23:50 -08002257void aboot_init(const struct app_descriptor *app)
2258{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002259 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002260 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002261
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002262 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002263 if (target_is_emmc_boot())
2264 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002265 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002266 page_mask = page_size - 1;
2267 }
2268 else
2269 {
2270 page_size = flash_page_size();
2271 page_mask = page_size - 1;
2272 }
2273
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002274 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2275
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002276 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002277
Greg Griscod6250552011-06-29 14:40:23 -07002278 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002279 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002280
Dhaval Patel223ec952013-07-18 14:49:44 -07002281 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2282
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002283 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002284 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002285 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002286 dprintf(ALWAYS,"dload mode key sequence detected\n");
2287 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002288 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002289 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002290 }
2291 else
2292 {
2293 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002294 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002295 }
2296 boot_into_fastboot = true;
2297 }
2298 if (!boot_into_fastboot)
2299 {
2300 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2301 boot_into_recovery = 1;
2302 if (!boot_into_recovery &&
2303 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002304 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002305 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002306 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002307 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002308 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002309 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002310
Ajay Dudani77421292010-10-27 19:34:06 -07002311 reboot_mode = check_reboot_mode();
2312 if (reboot_mode == RECOVERY_MODE) {
2313 boot_into_recovery = 1;
2314 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002315 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002316 }
2317
Pavel Nedev5d91d412013-04-29 11:34:24 +03002318 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002319 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002320 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002321 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002322 if(emmc_recovery_init())
2323 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2324 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002325 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002326 if((device.is_unlocked) || (device.is_tampered))
2327 {
2328 #ifdef TZ_TAMPER_FUSE
2329 set_tamper_fuse_cmd();
2330 #endif
2331 #if USE_PCOM_SECBOOT
2332 set_tamper_flag(device.is_tampered);
2333 #endif
2334 }
Shashank Mittala0032282011-08-26 14:50:11 -07002335 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002336 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002337 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002338 else
2339 {
2340 recovery_init();
2341 #if USE_PCOM_SECBOOT
2342 if((device.is_unlocked) || (device.is_tampered))
2343 set_tamper_flag(device.is_tampered);
2344 #endif
2345 boot_linux_from_flash();
2346 }
2347 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2348 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002349 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002350
Amol Jadi5edf3552013-07-23 14:15:34 -07002351 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002352
Amol Jadi5edf3552013-07-23 14:15:34 -07002353 /* register aboot specific fastboot commands */
2354 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002355
Amol Jadi5edf3552013-07-23 14:15:34 -07002356 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002357 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002358
2359 /* initialize and start fastboot */
2360 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002361}
2362
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002363uint32_t get_page_size()
2364{
2365 return page_size;
2366}
2367
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002368/*
2369 * Calculated and save hash (SHA256) for non-signed boot image.
2370 *
2371 * Hash the same data that is checked on the signed boot image.
2372 * Kernel and Ramdisk are already read to memory buffers.
2373 * Need to read the entire device-tree from mmc
2374 * since non-signed image only read the DT tags of the relevant platform.
2375 *
2376 * @param kernel_addr - kernel bufer
2377 * @param kernel_actual - kernel size in bytes
2378 * @param ramdisk_addr - ramdisk buffer
2379 * @param ramdisk_actual - ramdisk size
2380 * @param ptn - partition
2381 * @param dt_offset - device tree offset on mmc partition
2382 * @param dt_size
2383 *
2384 * @return int - 0 on success, negative value on failure.
2385 */
2386int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2387 void *ramdisk_addr, unsigned ramdisk_actual,
2388 unsigned long long ptn,
2389 unsigned dt_offset, unsigned dt_size)
2390{
2391 SHA256_CTX sha256_ctx;
2392 char digest[32]={0};
2393 char *buf = (char *)target_get_scratch_address();
2394 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2395 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2396
2397 SHA256_Init(&sha256_ctx);
2398
2399 /* Read Boot Header */
2400 if (mmc_read(ptn, buf, page_size))
2401 {
2402 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2403 return -1;
2404 }
2405 /* Read entire Device Tree */
2406 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2407 {
2408 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2409 return -1;
2410 }
2411 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2412 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2413 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2414 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2415
2416 SHA256_Final(digest, &sha256_ctx);
2417
2418 save_kernel_hash_cmd(digest);
2419 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2420
2421 return 0;
2422}
2423
Brian Swetland9c4c0752009-01-25 16:23:50 -08002424APP_START(aboot)
2425 .init = aboot_init,
2426APP_END