blob: 0b6baf764a70be7ac7b0e314f820c934abce9168 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Kuogee Hsiehfb4b5c32013-12-20 08:34:10 -08005 * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080038#include <kernel/thread.h>
39#include <arch/ops.h>
40
Dima Zavin214cc642009-01-26 11:16:21 -080041#include <dev/flash.h>
42#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080043#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080044#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080045#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070046#include <target.h>
47#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070048#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070049#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070050#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070051#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070052#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030053#include <sha.h>
Dima Zavin214cc642009-01-26 11:16:21 -080054
Neeti Desai17379b82012-06-04 18:42:53 -070055#if DEVICE_TREE
56#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070057#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070058#endif
59
Shashank Mittalcd98d472011-08-02 14:29:24 -070060#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080061#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080062#include "bootimg.h"
63#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070064#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070065#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070066#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070067#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070068#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070069
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070070extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070071extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070072extern void target_uninit(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070073
74void write_device_info_mmc(device_info *dev);
75void write_device_info_flash(device_info *dev);
76
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070077#define EXPAND(NAME) #NAME
78#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070079
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080080#ifdef MEMBASE
81#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
82#else
David Ng183a7422009-12-07 14:55:21 -080083#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080084#endif
85
Deepa Dinamani0e163a42013-05-24 17:08:15 -070086#ifndef MEMSIZE
87#define MEMSIZE 1024*1024
88#endif
89
90#define MAX_TAGS_SIZE 1024
91
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080092#define RECOVERY_MODE 0x77665502
93#define FASTBOOT_MODE 0x77665500
94
Kun Liang2f1601a2013-08-12 16:29:54 +080095/* make 4096 as default size to ensure EFS,EXT4's erasing */
96#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsiehfb4b5c32013-12-20 08:34:10 -080097#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +080098
David Ng183a7422009-12-07 14:55:21 -080099static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800100static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300101static const char *androidboot_mode = " androidboot.mode=";
Dhaval Patel223ec952013-07-18 14:49:44 -0700102static const char *display_cmdline = " mdss_mdp.panel=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800103static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800104static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700105static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300106static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800107
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800108static const char *baseband_apq = " androidboot.baseband=apq";
109static const char *baseband_msm = " androidboot.baseband=msm";
110static const char *baseband_csfb = " androidboot.baseband=csfb";
111static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700112static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700113static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800114static const char *baseband_dsda = " androidboot.baseband=dsda";
115static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800116static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800117
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700118static unsigned page_size = 0;
119static unsigned page_mask = 0;
120static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
121static bool boot_into_ffbm;
122
Shashank Mittalcd98d472011-08-02 14:29:24 -0700123/* Assuming unauthorized kernel image by default */
124static int auth_kernel_img = 0;
125
Unnati Gandhi15d08f92014-01-24 11:01:01 +0530126static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700127
Dima Zavin42168f22009-01-30 11:52:22 -0800128struct atag_ptbl_entry
129{
130 char name[16];
131 unsigned offset;
132 unsigned size;
133 unsigned flags;
134};
135
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700136/*
137 * Partition info, required to be published
138 * for fastboot
139 */
140struct getvar_partition_info {
141 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
142 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
143 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
144 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
145 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
146};
147
148/*
149 * Right now, we are publishing the info for only
150 * three partitions
151 */
152struct getvar_partition_info part_info[] =
153{
154 { "system" , "partition-size:", "partition-type:", "", "ext4" },
155 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
156 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
157};
158
159char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700160char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800161char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700162char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi15d08f92014-01-24 11:01:01 +0530163char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700164
Greg Griscod2471ef2011-07-14 13:00:42 -0700165extern int emmc_recovery_init(void);
166
Kinson Chik0b1c8162011-08-31 16:31:57 -0700167#if NO_KEYPAD_DRIVER
168extern int fastboot_trigger(void);
169#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700170
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700171static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
172{
173 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700174#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
175 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
176 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
177 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700178#endif
179}
180
Dima Zavin42168f22009-01-30 11:52:22 -0800181static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
182{
183 struct atag_ptbl_entry atag_ptn;
184
185 memcpy(atag_ptn.name, ptn->name, 16);
186 atag_ptn.name[15] = '\0';
187 atag_ptn.offset = ptn->start;
188 atag_ptn.size = ptn->length;
189 atag_ptn.flags = ptn->flags;
190 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
191 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
192}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800193
Neeti Desaie245d492012-06-01 12:52:13 -0700194unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800195{
David Ng183a7422009-12-07 14:55:21 -0800196 int cmdline_len = 0;
197 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800198 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700199 int pause_at_bootup = 0;
Pavel Nedev5614d222013-06-17 18:01:02 +0300200 bool gpt_exists = partition_gpt_exists();
Dima Zavin42168f22009-01-30 11:52:22 -0800201
Brian Swetland9c4c0752009-01-25 16:23:50 -0800202 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800203 cmdline_len = strlen(cmdline);
204 have_cmdline = 1;
205 }
206 if (target_is_emmc_boot()) {
207 cmdline_len += strlen(emmc_cmdline);
208 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800209
210 cmdline_len += strlen(usb_sn_cmdline);
211 cmdline_len += strlen(sn_buf);
212
Pavel Nedev5614d222013-06-17 18:01:02 +0300213 if (boot_into_recovery && gpt_exists)
214 cmdline_len += strlen(secondary_gpt_enable);
215
Pavel Nedev328ac822013-04-05 15:25:11 +0300216 if (boot_into_ffbm) {
217 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700218 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800219 /* reduce kernel console messages to speed-up boot */
220 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700221 } else if (device.charger_screen_enabled &&
222 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700223 pause_at_bootup = 1;
224 cmdline_len += strlen(battchg_pause);
225 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800226
Shashank Mittalcd98d472011-08-02 14:29:24 -0700227 if(target_use_signed_kernel() && auth_kernel_img) {
228 cmdline_len += strlen(auth_kernel);
229 }
230
Ajay Dudanid04110c2011-01-17 23:55:07 -0800231 /* Determine correct androidboot.baseband to use */
232 switch(target_baseband())
233 {
234 case BASEBAND_APQ:
235 cmdline_len += strlen(baseband_apq);
236 break;
237
238 case BASEBAND_MSM:
239 cmdline_len += strlen(baseband_msm);
240 break;
241
242 case BASEBAND_CSFB:
243 cmdline_len += strlen(baseband_csfb);
244 break;
245
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800246 case BASEBAND_SVLTE2A:
247 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800248 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700249
250 case BASEBAND_MDM:
251 cmdline_len += strlen(baseband_mdm);
252 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700253
254 case BASEBAND_SGLTE:
255 cmdline_len += strlen(baseband_sglte);
256 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530257
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800258 case BASEBAND_SGLTE2:
259 cmdline_len += strlen(baseband_sglte2);
260 break;
261
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530262 case BASEBAND_DSDA:
263 cmdline_len += strlen(baseband_dsda);
264 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800265
266 case BASEBAND_DSDA2:
267 cmdline_len += strlen(baseband_dsda2);
268 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800269 }
270
Dhaval Patel223ec952013-07-18 14:49:44 -0700271 if (target_display_panel_node(display_panel_buf, MAX_PANEL_BUF_SIZE) &&
272 strlen(display_panel_buf))
273 {
274 cmdline_len += strlen(display_cmdline);
275 cmdline_len += strlen(display_panel_buf);
276 }
277
David Ng183a7422009-12-07 14:55:21 -0800278 if (cmdline_len > 0) {
279 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700280 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
281 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700282
Amol Jadi168b7712012-03-06 16:15:00 -0800283 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700284 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800285 if (have_cmdline) {
286 src = cmdline;
287 while ((*dst++ = *src++));
288 }
289 if (target_is_emmc_boot()) {
290 src = emmc_cmdline;
291 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700292 have_cmdline = 1;
293 while ((*dst++ = *src++));
294 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800295
296 src = usb_sn_cmdline;
297 if (have_cmdline) --dst;
298 have_cmdline = 1;
299 while ((*dst++ = *src++));
300 src = sn_buf;
301 if (have_cmdline) --dst;
302 have_cmdline = 1;
303 while ((*dst++ = *src++));
304
Pavel Nedev5614d222013-06-17 18:01:02 +0300305 if (boot_into_recovery && gpt_exists) {
306 src = secondary_gpt_enable;
307 if (have_cmdline) --dst;
308 while ((*dst++ = *src++));
309 }
310
Pavel Nedev328ac822013-04-05 15:25:11 +0300311 if (boot_into_ffbm) {
312 src = androidboot_mode;
313 if (have_cmdline) --dst;
314 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700315 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300316 if (have_cmdline) --dst;
317 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800318 src = loglevel;
319 if (have_cmdline) --dst;
320 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300321 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700322 src = battchg_pause;
323 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800324 while ((*dst++ = *src++));
325 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800326
Shashank Mittalcd98d472011-08-02 14:29:24 -0700327 if(target_use_signed_kernel() && auth_kernel_img) {
328 src = auth_kernel;
329 if (have_cmdline) --dst;
330 while ((*dst++ = *src++));
331 }
332
Ajay Dudanid04110c2011-01-17 23:55:07 -0800333 switch(target_baseband())
334 {
335 case BASEBAND_APQ:
336 src = baseband_apq;
337 if (have_cmdline) --dst;
338 while ((*dst++ = *src++));
339 break;
340
341 case BASEBAND_MSM:
342 src = baseband_msm;
343 if (have_cmdline) --dst;
344 while ((*dst++ = *src++));
345 break;
346
347 case BASEBAND_CSFB:
348 src = baseband_csfb;
349 if (have_cmdline) --dst;
350 while ((*dst++ = *src++));
351 break;
352
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800353 case BASEBAND_SVLTE2A:
354 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800355 if (have_cmdline) --dst;
356 while ((*dst++ = *src++));
357 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700358
359 case BASEBAND_MDM:
360 src = baseband_mdm;
361 if (have_cmdline) --dst;
362 while ((*dst++ = *src++));
363 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700364
365 case BASEBAND_SGLTE:
366 src = baseband_sglte;
367 if (have_cmdline) --dst;
368 while ((*dst++ = *src++));
369 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530370
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800371 case BASEBAND_SGLTE2:
372 src = baseband_sglte2;
373 if (have_cmdline) --dst;
374 while ((*dst++ = *src++));
375 break;
376
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530377 case BASEBAND_DSDA:
378 src = baseband_dsda;
379 if (have_cmdline) --dst;
380 while ((*dst++ = *src++));
381 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800382
383 case BASEBAND_DSDA2:
384 src = baseband_dsda2;
385 if (have_cmdline) --dst;
386 while ((*dst++ = *src++));
387 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800388 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700389
390 if (strlen(display_panel_buf)) {
391 src = display_cmdline;
392 if (have_cmdline) --dst;
393 while ((*dst++ = *src++));
394 src = display_panel_buf;
395 if (have_cmdline) --dst;
396 while ((*dst++ = *src++));
397 }
Neeti Desaie245d492012-06-01 12:52:13 -0700398 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700399
400
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800401 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700402 return cmdline_final;
403}
404
405unsigned *atag_core(unsigned *ptr)
406{
407 /* CORE */
408 *ptr++ = 2;
409 *ptr++ = 0x54410001;
410
411 return ptr;
412
413}
414
415unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
416 unsigned ramdisk_size)
417{
418 if (ramdisk_size) {
419 *ptr++ = 4;
420 *ptr++ = 0x54420005;
421 *ptr++ = (unsigned)ramdisk;
422 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800423 }
424
Neeti Desaie245d492012-06-01 12:52:13 -0700425 return ptr;
426}
427
428unsigned *atag_ptable(unsigned **ptr_addr)
429{
430 int i;
431 struct ptable *ptable;
432
433 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700434 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
435 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700436 *(*ptr_addr)++ = 0x4d534d70;
437 for (i = 0; i < ptable->count; ++i)
438 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
439 }
440
441 return (*ptr_addr);
442}
443
444unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
445{
446 int cmdline_length = 0;
447 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700448 char *dest;
449
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800450 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700451 n = (cmdline_length + 4) & (~3);
452
453 *ptr++ = (n / 4) + 2;
454 *ptr++ = 0x54410009;
455 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800456 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700457 ptr += (n / 4);
458
459 return ptr;
460}
461
462unsigned *atag_end(unsigned *ptr)
463{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800464 /* END */
465 *ptr++ = 0;
466 *ptr++ = 0;
467
Neeti Desaie245d492012-06-01 12:52:13 -0700468 return ptr;
469}
470
471void generate_atags(unsigned *ptr, const char *cmdline,
472 void *ramdisk, unsigned ramdisk_size)
473{
474
475 ptr = atag_core(ptr);
476 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
477 ptr = target_atag_mem(ptr);
478
479 /* Skip NAND partition ATAGS for eMMC boot */
480 if (!target_is_emmc_boot()){
481 ptr = atag_ptable(&ptr);
482 }
483
484 ptr = atag_cmdline(ptr, cmdline);
485 ptr = atag_end(ptr);
486}
487
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700488typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700489void boot_linux(void *kernel, unsigned *tags,
490 const char *cmdline, unsigned machtype,
491 void *ramdisk, unsigned ramdisk_size)
492{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800493 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800494#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700495 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800496#endif
497
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700498 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800499 uint32_t tags_phys = PA((addr_t)tags);
500
501 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700502
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800503 final_cmdline = update_cmdline((const char*)cmdline);
504
Neeti Desai17379b82012-06-04 18:42:53 -0700505#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800506 dprintf(INFO, "Updating device tree: start\n");
507
Neeti Desai17379b82012-06-04 18:42:53 -0700508 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800509 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700510 if(ret)
511 {
512 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
513 ASSERT(0);
514 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800515 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700516#else
Neeti Desaie245d492012-06-01 12:52:13 -0700517 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800518 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700519#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700520
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700521 /* Perform target specific cleanup */
522 target_uninit();
523
Aravind Venkateswarandb0d5262014-02-25 16:25:30 -0800524 /* Turn off splash screen if enabled */
525#if DISPLAY_SPLASH_SCREEN
526 target_display_shutdown();
527#endif
528
529
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800530 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
531 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800532
533 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700534
Amol Jadi4421e652011-06-16 15:00:48 -0700535 /* do any platform specific cleanup before kernel entry */
536 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700537
Brian Swetland9c4c0752009-01-25 16:23:50 -0800538 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700539
Amol Jadi504f9fe2012-08-16 13:56:48 -0700540#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800541 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700542#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700543 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700544 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800545}
546
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700547/* Function to check if the memory address range falls within the aboot
548 * boundaries.
549 * start: Start of the memory region
550 * size: Size of the memory region
551 */
552int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
553{
554 /* Check for boundary conditions. */
555 if ((start + size) < start)
556 return -1;
557
558 /* Check for memory overlap. */
559 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
560 return 0;
Channagoud Kadabib4413fd2013-10-15 12:53:52 -0700561 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700562 return 0;
563 else
564 return -1;
565}
566
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800567#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800568
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700569BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800570#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700571BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800572#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800573
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700574static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
575{
576 int ret;
577
578 /* Assume device is rooted at this time. */
579 device.is_tampered = 1;
580
581 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
582
583 ret = image_verify((unsigned char *)bootimg_addr,
584 (unsigned char *)(bootimg_addr + bootimg_size),
585 bootimg_size,
586 CRYPTO_AUTH_ALG_SHA256);
587
588 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
589
590 if (ret)
591 {
592 /* Authorized kernel */
593 device.is_tampered = 0;
594 }
595
596#if USE_PCOM_SECBOOT
597 set_tamper_flag(device.is_tampered);
598#endif
599
600 if(device.is_tampered)
601 {
602 write_device_info_mmc(&device);
603 #ifdef TZ_TAMPER_FUSE
604 set_tamper_fuse_cmd();
605 #endif
606 #ifdef ASSERT_ON_TAMPER
607 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
608 ASSERT(0);
609 #endif
610 }
611}
612
Maunik Shah78a85e52014-03-02 17:47:58 +0530613static bool check_format_bit()
614{
615 bool ret = false;
616 int index;
617 uint64_t offset;
618 struct boot_selection_info *in = NULL;
619 char *buf = NULL;
620
621 index = partition_get_index("bootselect");
622 if (index == INVALID_PTN)
623 {
624 dprintf(INFO, "Unable to locate /bootselect partition\n");
625 return ret;
626 }
627 offset = partition_get_offset(index);
628 if(!offset)
629 {
630 dprintf(INFO, "partition /bootselect doesn't exist\n");
631 return ret;
632 }
633 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
634 ASSERT(buf);
635 if (mmc_read(offset, (unsigned int *)buf, page_size))
636 {
637 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
638 free(buf);
639 return ret;
640 }
641 in = (struct boot_selection_info *) buf;
642 if ((in->signature == BOOTSELECT_SIGNATURE) &&
643 (in->version == BOOTSELECT_VERSION)) {
644 if ((in->state_info & BOOTSELECT_FORMAT) &&
645 !(in->state_info & BOOTSELECT_FACTORY))
646 ret = true;
647 } else {
648 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
649 in->signature, in->version);
650 ASSERT(0);
651 }
652 free(buf);
653 return ret;
654}
655
Shashank Mittal23b8f422010-04-16 19:27:21 -0700656int boot_linux_from_mmc(void)
657{
658 struct boot_img_hdr *hdr = (void*) buf;
659 struct boot_img_hdr *uhdr;
660 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700661 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700662 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700663 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700664
Shashank Mittalcd98d472011-08-02 14:29:24 -0700665 unsigned char *image_addr = 0;
666 unsigned kernel_actual;
667 unsigned ramdisk_actual;
668 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700669 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700670
671#if DEVICE_TREE
672 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700673 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700674 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800675 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700676 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700677#endif
Maunik Shah78a85e52014-03-02 17:47:58 +0530678 if (check_format_bit())
679 boot_into_recovery = 1;
680
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700681 if (!boot_into_recovery) {
682 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
683 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
684 if (rcode <= 0) {
685 boot_into_ffbm = false;
686 if (rcode < 0)
687 dprintf(CRITICAL,"failed to get ffbm cookie");
688 } else
689 boot_into_ffbm = true;
690 } else
691 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700692 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
693 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
694 dprintf(INFO, "Unified boot method!\n");
695 hdr = uhdr;
696 goto unified_boot;
697 }
Greg Griscod6250552011-06-29 14:40:23 -0700698 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700699 index = partition_get_index("boot");
700 ptn = partition_get_offset(index);
701 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700702 dprintf(CRITICAL, "ERROR: No boot partition found\n");
703 return -1;
704 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700705 }
706 else {
707 index = partition_get_index("recovery");
708 ptn = partition_get_offset(index);
709 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700710 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
711 return -1;
712 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700713 }
714
Greg Griscod6250552011-06-29 14:40:23 -0700715 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700716 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
717 return -1;
718 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700719
720 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700721 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700722 return -1;
723 }
724
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700725 if (hdr->page_size && (hdr->page_size != page_size)) {
726 page_size = hdr->page_size;
727 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700728 }
729
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700730 /*
731 * Update the kernel/ramdisk/tags address if the boot image header
732 * has default values, these default values come from mkbootimg when
733 * the boot image is flashed using fastboot flash:raw
734 */
735 update_ker_tags_rdisk_addr(hdr);
736
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700737 /* Get virtual addresses since the hdr saves physical addresses. */
738 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
739 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
740 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700741
742 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
743 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
744
745 /* Check if the addresses in the header are valid. */
746 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
747 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
748 {
749 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
750 return -1;
751 }
752
753#ifndef DEVICE_TREE
754 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
755 {
756 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
757 return -1;
758 }
759#endif
760
Shashank Mittalcd98d472011-08-02 14:29:24 -0700761 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300762 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
763 (int) target_use_signed_kernel(),
764 device.is_unlocked,
765 device.is_tampered);
766
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700767 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800768 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700769 offset = 0;
770
Shashank Mittalcd98d472011-08-02 14:29:24 -0700771 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700772
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800773#if DEVICE_TREE
774 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
775 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700776
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700777 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700778 {
779 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
780 return -1;
781 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800782#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530783 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700784
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700785#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700786
Amol Jadib6be5c12012-11-14 13:39:51 -0800787 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700788 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800789
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700790 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
791 {
792 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
793 return -1;
794 }
795
Shashank Mittalcd98d472011-08-02 14:29:24 -0700796 /* Read image without signature */
797 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
798 {
799 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
800 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800801 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700802
Amol Jadib6be5c12012-11-14 13:39:51 -0800803 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700804 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800805
Shashank Mittalcd98d472011-08-02 14:29:24 -0700806 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700807
808 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
809 {
810 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
811 return -1;
812 }
813
Shashank Mittalcd98d472011-08-02 14:29:24 -0700814 /* Read signature */
815 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
816 {
817 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700818 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700819 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800820
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700821 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700822
Neeti Desai465491e2012-07-31 12:53:35 -0700823 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700824 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
825 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700826
Neeti Desai465491e2012-07-31 12:53:35 -0700827 #if DEVICE_TREE
828 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700829 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700830 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700831
Deepa Dinamani19648b42013-09-05 17:05:55 -0700832 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700833 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
834 return -1;
835 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700836
Neeti Desai465491e2012-07-31 12:53:35 -0700837 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700838 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700839 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
840 return -1;
841 }
842
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700843 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700844 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700845 {
846 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
847 return -1;
848 }
849
Joel Kingaa335dc2013-06-03 16:11:08 -0700850 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800851 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800852 /*
853 * If appended dev tree is found, update the atags with
854 * memory address to the DTB appended location on RAM.
855 * Else update with the atags address in the kernel header
856 */
857 void *dtb;
858 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700859 hdr->kernel_size,
860 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800861 if (!dtb) {
862 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
863 return -1;
864 }
Neeti Desai465491e2012-07-31 12:53:35 -0700865 }
866 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700867 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700868 else
869 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800870 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700871
Amol Jadib6be5c12012-11-14 13:39:51 -0800872 dprintf(INFO, "Loading boot image (%d): start\n",
873 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700874 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800875
876 offset = page_size;
877
878 /* Load kernel */
879 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700880 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
881 return -1;
882 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800883 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700884
Amol Jadib6be5c12012-11-14 13:39:51 -0800885 /* Load ramdisk */
886 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700887 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800888 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700889 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
890 return -1;
891 }
892 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800893 offset += ramdisk_actual;
894
895 dprintf(INFO, "Loading boot image (%d): done\n",
896 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700897 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700898
899 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800900 offset += second_actual;
901 /* Second image loading not implemented. */
902 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700903 }
904
905 #if DEVICE_TREE
906 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -0700907 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -0700908 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
909 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
910 return -1;
911 }
912 table = (struct dt_table*) dt_buf;
913
Deepa Dinamani19648b42013-09-05 17:05:55 -0700914 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700915 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
916 return -1;
917 }
918
Deepa Dinamani19648b42013-09-05 17:05:55 -0700919 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
920 if (!table)
921 return -1;
922
923 /* Read the entire device tree table into buffer */
924 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
925 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
926 return -1;
927 }
928
Joel Kingaa335dc2013-06-03 16:11:08 -0700929 /* Find index of device tree within device tree table */
930 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700931 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
932 return -1;
933 }
934
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700935 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700936 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700937 {
938 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
939 return -1;
940 }
941
Joel Kingaa335dc2013-06-03 16:11:08 -0700942 if(mmc_read(ptn + offset + dt_entry.offset,
943 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700944 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
945 return -1;
946 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300947 #ifdef TZ_SAVE_KERNEL_HASH
948 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
949 hdr->ramdisk_addr, ramdisk_actual,
950 ptn, offset, hdr->dt_size);
951 #endif /* TZ_SAVE_KERNEL_HASH */
952
Channagoud Kadabi35095622013-03-01 13:53:05 -0800953 } else {
Aparna Mallavarapuf5432e42014-06-03 22:15:54 +0530954
955 /* Validate the tags_addr */
956 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
957 {
958 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
959 return -1;
960 }
Dima Zavin77e41f32013-03-06 16:10:43 -0800961 /*
962 * If appended dev tree is found, update the atags with
963 * memory address to the DTB appended location on RAM.
964 * Else update with the atags address in the kernel header
965 */
966 void *dtb;
967 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700968 kernel_actual,
969 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800970 if (!dtb) {
971 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
972 return -1;
973 }
Neeti Desai465491e2012-07-31 12:53:35 -0700974 }
975 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700976 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700977
Stanimir Varbanov69ec5462013-07-18 18:17:42 +0300978 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
979 target_load_ssd_keystore();
980
Shashank Mittal23b8f422010-04-16 19:27:21 -0700981unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700982
Dima Zavin77e41f32013-03-06 16:10:43 -0800983 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700984 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700985 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
986
987 return 0;
988}
989
Dima Zavin214cc642009-01-26 11:16:21 -0800990int boot_linux_from_flash(void)
991{
992 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800993 struct ptentry *ptn;
994 struct ptable *ptable;
995 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800996
Shashank Mittalcd98d472011-08-02 14:29:24 -0700997 unsigned char *image_addr = 0;
998 unsigned kernel_actual;
999 unsigned ramdisk_actual;
1000 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001001 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001002
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001003#if DEVICE_TREE
1004 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001005 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001006 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001007 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001008#endif
1009
David Ng183a7422009-12-07 14:55:21 -08001010 if (target_is_emmc_boot()) {
1011 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1012 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1013 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1014 return -1;
1015 }
1016 goto continue_boot;
1017 }
1018
Dima Zavin214cc642009-01-26 11:16:21 -08001019 ptable = flash_get_ptable();
1020 if (ptable == NULL) {
1021 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1022 return -1;
1023 }
1024
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001025 if(!boot_into_recovery)
1026 {
1027 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001028
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001029 if (ptn == NULL) {
1030 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1031 return -1;
1032 }
1033 }
1034 else
1035 {
1036 ptn = ptable_find(ptable, "recovery");
1037 if (ptn == NULL) {
1038 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1039 return -1;
1040 }
Dima Zavin214cc642009-01-26 11:16:21 -08001041 }
1042
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001043 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001044 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1045 return -1;
1046 }
Dima Zavin214cc642009-01-26 11:16:21 -08001047
1048 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001049 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001050 return -1;
1051 }
1052
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001053 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001054 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 -08001055 return -1;
1056 }
1057
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001058 /*
1059 * Update the kernel/ramdisk/tags address if the boot image header
1060 * has default values, these default values come from mkbootimg when
1061 * the boot image is flashed using fastboot flash:raw
1062 */
1063 update_ker_tags_rdisk_addr(hdr);
1064
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001065 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001066 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1067 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1068 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1069
1070 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1071 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1072
1073 /* Check if the addresses in the header are valid. */
1074 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1075 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1076 {
1077 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1078 return -1;
1079 }
1080
1081#ifndef DEVICE_TREE
1082 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1083 {
1084 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1085 return -1;
1086 }
1087#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001088
Shashank Mittalcd98d472011-08-02 14:29:24 -07001089 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001090 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001091 {
1092 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001093 offset = 0;
1094
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001095#if DEVICE_TREE
1096 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1097 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001098
1099 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1100 {
1101 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1102 return -1;
1103 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001104#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001105 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001106#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001107
Amol Jadib6be5c12012-11-14 13:39:51 -08001108 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001109 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001110
Shashank Mittalcd98d472011-08-02 14:29:24 -07001111 /* Read image without signature */
1112 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1113 {
1114 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1115 return -1;
1116 }
Dima Zavin214cc642009-01-26 11:16:21 -08001117
Amol Jadib6be5c12012-11-14 13:39:51 -08001118 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001119 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001120
Shashank Mittalcd98d472011-08-02 14:29:24 -07001121 offset = imagesize_actual;
1122 /* Read signature */
1123 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1124 {
1125 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001126 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001127 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001128
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001129 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001130
1131 /* Move kernel and ramdisk to correct address */
1132 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1133 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001134#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001135 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001136 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001137 {
1138 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1139 return -1;
1140 }
1141
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001142 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1143#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001144
1145 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001146 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001147 {
1148 write_device_info_flash(&device);
1149 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301150#if USE_PCOM_SECBOOT
1151 set_tamper_flag(device.is_tampered);
1152#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001153 }
1154 else
1155 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001156 offset = page_size;
1157
Amol Jadib6be5c12012-11-14 13:39:51 -08001158 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1159 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1160 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1161
1162 dprintf(INFO, "Loading boot image (%d): start\n",
1163 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001164 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001165
1166 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001167 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1168 return -1;
1169 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001170 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001171
Amol Jadib6be5c12012-11-14 13:39:51 -08001172 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001173 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1174 return -1;
1175 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001176 offset += ramdisk_actual;
1177
1178 dprintf(INFO, "Loading boot image (%d): done\n",
1179 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001180 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001181
1182 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001183 offset += second_actual;
1184 /* Second image loading not implemented. */
1185 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001186 }
1187
1188#if DEVICE_TREE
1189 if(hdr->dt_size != 0) {
1190
1191 /* Read the device tree table into buffer */
1192 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1193 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1194 return -1;
1195 }
1196
1197 table = (struct dt_table*) dt_buf;
1198
Deepa Dinamani19648b42013-09-05 17:05:55 -07001199 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001200 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1201 return -1;
1202 }
1203
Deepa Dinamani19648b42013-09-05 17:05:55 -07001204 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1205 if (!table)
1206 return -1;
1207
1208 /* Read the entire device tree table into buffer */
1209 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1210 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1211 return -1;
1212 }
1213
1214
Joel Kingaa335dc2013-06-03 16:11:08 -07001215 /* Find index of device tree within device tree table */
1216 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001217 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1218 return -1;
1219 }
1220
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001221 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001222 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001223 {
1224 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1225 return -1;
1226 }
1227
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001228 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001229 if(flash_read(ptn, offset + dt_entry.offset,
1230 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001231 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1232 return -1;
1233 }
1234 }
1235#endif
1236
Shashank Mittalcd98d472011-08-02 14:29:24 -07001237 }
David Ng183a7422009-12-07 14:55:21 -08001238continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001239
Dima Zavin214cc642009-01-26 11:16:21 -08001240 /* TODO: create/pass atags to kernel */
1241
Ajay Dudanie28a6072011-07-01 13:59:46 -07001242 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001243 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001244 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1245
1246 return 0;
1247}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001248
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001249BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001250void write_device_info_mmc(device_info *dev)
1251{
1252 struct device_info *info = (void*) info_buf;
1253 unsigned long long ptn = 0;
1254 unsigned long long size;
1255 int index = INVALID_PTN;
1256
1257 index = partition_get_index("aboot");
1258 ptn = partition_get_offset(index);
1259 if(ptn == 0)
1260 {
1261 return;
1262 }
1263
1264 size = partition_get_size(index);
1265
1266 memcpy(info, dev, sizeof(device_info));
1267
1268 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1269 {
1270 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1271 return;
1272 }
1273}
1274
1275void read_device_info_mmc(device_info *dev)
1276{
1277 struct device_info *info = (void*) info_buf;
1278 unsigned long long ptn = 0;
1279 unsigned long long size;
1280 int index = INVALID_PTN;
1281
1282 index = partition_get_index("aboot");
1283 ptn = partition_get_offset(index);
1284 if(ptn == 0)
1285 {
1286 return;
1287 }
1288
1289 size = partition_get_size(index);
1290
1291 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1292 {
1293 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1294 return;
1295 }
1296
1297 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1298 {
1299 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1300 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001301 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001302 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001303
1304 write_device_info_mmc(info);
1305 }
1306 memcpy(dev, info, sizeof(device_info));
1307}
1308
1309void write_device_info_flash(device_info *dev)
1310{
1311 struct device_info *info = (void *) info_buf;
1312 struct ptentry *ptn;
1313 struct ptable *ptable;
1314
1315 ptable = flash_get_ptable();
1316 if (ptable == NULL)
1317 {
1318 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1319 return;
1320 }
1321
1322 ptn = ptable_find(ptable, "devinfo");
1323 if (ptn == NULL)
1324 {
1325 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1326 return;
1327 }
1328
1329 memcpy(info, dev, sizeof(device_info));
1330
1331 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1332 {
1333 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1334 return;
1335 }
1336}
1337
1338void read_device_info_flash(device_info *dev)
1339{
1340 struct device_info *info = (void*) info_buf;
1341 struct ptentry *ptn;
1342 struct ptable *ptable;
1343
1344 ptable = flash_get_ptable();
1345 if (ptable == NULL)
1346 {
1347 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1348 return;
1349 }
1350
1351 ptn = ptable_find(ptable, "devinfo");
1352 if (ptn == NULL)
1353 {
1354 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1355 return;
1356 }
1357
1358 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1359 {
1360 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1361 return;
1362 }
1363
1364 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1365 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001366 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1367 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001368 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001369 write_device_info_flash(info);
1370 }
1371 memcpy(dev, info, sizeof(device_info));
1372}
1373
1374void write_device_info(device_info *dev)
1375{
1376 if(target_is_emmc_boot())
1377 {
1378 write_device_info_mmc(dev);
1379 }
1380 else
1381 {
1382 write_device_info_flash(dev);
1383 }
1384}
1385
1386void read_device_info(device_info *dev)
1387{
1388 if(target_is_emmc_boot())
1389 {
1390 read_device_info_mmc(dev);
1391 }
1392 else
1393 {
1394 read_device_info_flash(dev);
1395 }
1396}
1397
1398void reset_device_info()
1399{
1400 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001401 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001402 write_device_info(&device);
1403}
1404
1405void set_device_root()
1406{
1407 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001408 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001409 write_device_info(&device);
1410}
1411
Amol Jadicb524072012-08-09 16:40:18 -07001412#if DEVICE_TREE
1413int copy_dtb(uint8_t *boot_image_start)
1414{
1415 uint32 dt_image_offset = 0;
1416 uint32_t n;
1417 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001418 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001419 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001420
1421 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1422
Amol Jadicb524072012-08-09 16:40:18 -07001423 if(hdr->dt_size != 0) {
1424
1425 /* add kernel offset */
1426 dt_image_offset += page_size;
1427 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1428 dt_image_offset += n;
1429
1430 /* add ramdisk offset */
1431 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1432 dt_image_offset += n;
1433
1434 /* add second offset */
1435 if(hdr->second_size != 0) {
1436 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1437 dt_image_offset += n;
1438 }
1439
1440 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001441 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001442
Deepa Dinamani19648b42013-09-05 17:05:55 -07001443 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001444 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1445 return -1;
1446 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001447 /* Find index of device tree within device tree table */
1448 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001449 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1450 return -1;
1451 }
1452
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001453 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001454 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001455 {
1456 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1457 return -1;
1458 }
1459
Amol Jadicb524072012-08-09 16:40:18 -07001460 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001461 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001462 boot_image_start + dt_image_offset + dt_entry.offset,
1463 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001464 } else
1465 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001466
1467 /* Everything looks fine. Return success. */
1468 return 0;
1469}
1470#endif
1471
Brian Swetland9c4c0752009-01-25 16:23:50 -08001472void cmd_boot(const char *arg, void *data, unsigned sz)
1473{
1474 unsigned kernel_actual;
1475 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001476 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001477 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001478 int ret = 0;
1479 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001480
1481 if (sz < sizeof(hdr)) {
1482 fastboot_fail("invalid bootimage header");
1483 return;
1484 }
1485
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001486 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001487
1488 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001489 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001490
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001491 if(target_is_emmc_boot() && hdr->page_size) {
1492 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001493 page_mask = page_size - 1;
1494 }
1495
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001496 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1497 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1498
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001499 /*
1500 * Update the kernel/ramdisk/tags address if the boot image header
1501 * has default values, these default values come from mkbootimg when
1502 * the boot image is flashed using fastboot flash:raw
1503 */
1504 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001505
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001506 /* Get virtual addresses since the hdr saves physical addresses. */
1507 hdr->kernel_addr = VA(hdr->kernel_addr);
1508 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1509 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001510
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001511 /* Check if the addresses in the header are valid. */
1512 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1513 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1514 {
1515 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabia54102a2013-12-03 11:19:20 -08001516 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001517 }
1518
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001519 /* sz should have atleast raw boot image */
1520 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001521 fastboot_fail("incomplete bootimage");
1522 return;
1523 }
1524
Amol Jadicb524072012-08-09 16:40:18 -07001525#if DEVICE_TREE
1526 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001527 ret = copy_dtb(data);
1528
1529 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001530#else
1531 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1532 {
1533 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabia54102a2013-12-03 11:19:20 -08001534 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001535 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001536#endif
1537
1538 /* Load ramdisk & kernel */
1539 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1540 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1541
1542#if DEVICE_TREE
1543 /*
1544 * If dtb is not found look for appended DTB in the kernel.
1545 * If appended dev tree is found, update the atags with
1546 * memory address to the DTB appended location on RAM.
1547 * Else update with the atags address in the kernel header
1548 */
1549 if (!dtb_copied) {
1550 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001551 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1552 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001553 if (!dtb) {
1554 fastboot_fail("dtb not found");
1555 return;
1556 }
Amol Jadicb524072012-08-09 16:40:18 -07001557 }
1558#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001559
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001560#ifndef DEVICE_TREE
1561 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1562 {
1563 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabia54102a2013-12-03 11:19:20 -08001564 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001565 }
1566#endif
1567
Brian Swetland9c4c0752009-01-25 16:23:50 -08001568 fastboot_okay("");
1569 udc_stop();
1570
Dima Zavin77e41f32013-03-06 16:10:43 -08001571 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001572 (const char*) hdr->cmdline, board_machtype(),
1573 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001574}
1575
Dima Zavin214cc642009-01-26 11:16:21 -08001576void cmd_erase(const char *arg, void *data, unsigned sz)
1577{
1578 struct ptentry *ptn;
1579 struct ptable *ptable;
1580
1581 ptable = flash_get_ptable();
1582 if (ptable == NULL) {
1583 fastboot_fail("partition table doesn't exist");
1584 return;
1585 }
1586
1587 ptn = ptable_find(ptable, arg);
1588 if (ptn == NULL) {
1589 fastboot_fail("unknown partition name");
1590 return;
1591 }
1592
1593 if (flash_erase(ptn)) {
1594 fastboot_fail("failed to erase partition");
1595 return;
1596 }
1597 fastboot_okay("");
1598}
1599
Bikas Gurungd48bd242010-09-04 19:54:32 -07001600
1601void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1602{
Kun Liang2f1601a2013-08-12 16:29:54 +08001603 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001604 unsigned long long ptn = 0;
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001605 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001606 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001607
Kinson Chikf1a43512011-07-14 11:28:39 -07001608 index = partition_get_index(arg);
1609 ptn = partition_get_offset(index);
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001610 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001611
Kinson Chikf1a43512011-07-14 11:28:39 -07001612 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001613 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001614 return;
1615 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001616
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001617#if MMC_SDHCI_SUPPORT
1618 if (mmc_erase_card(ptn, size)) {
1619 fastboot_fail("failed to erase partition\n");
1620 return;
1621 }
1622#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001623 size = partition_get_size(index);
1624 if (size > DEFAULT_ERASE_SIZE)
1625 size = DEFAULT_ERASE_SIZE;
1626
neetidb4b24d62012-01-20 12:13:09 -08001627 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001628 0 in first several blocks */
1629 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001630 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001631 return;
1632 }
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001633#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001634 fastboot_okay("");
1635}
1636
1637
Ajay Dudani5c761132011-04-07 20:19:04 -07001638void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001639{
1640 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001641 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001642 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001643
Greg Grisco6e754772011-06-23 12:19:39 -07001644 if (!strcmp(arg, "partition"))
1645 {
1646 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001647 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001648 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001649 return;
1650 }
1651 }
Greg Grisco6e754772011-06-23 12:19:39 -07001652 else
1653 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001654 index = partition_get_index(arg);
1655 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001656 if(ptn == 0) {
1657 fastboot_fail("partition table doesn't exist");
1658 return;
1659 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001660
Greg Grisco6e754772011-06-23 12:19:39 -07001661 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1662 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1663 fastboot_fail("image is not a boot image");
1664 return;
1665 }
1666 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001667
Kinson Chikf1a43512011-07-14 11:28:39 -07001668 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001669 if (ROUND_TO_PAGE(sz,511) > size) {
1670 fastboot_fail("size too large");
1671 return;
1672 }
1673 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1674 fastboot_fail("flash write failure");
1675 return;
1676 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001677 }
1678 fastboot_okay("");
1679 return;
1680}
1681
Ajay Dudani5c761132011-04-07 20:19:04 -07001682void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1683{
1684 unsigned int chunk;
1685 unsigned int chunk_data_sz;
1686 sparse_header_t *sparse_header;
1687 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001688 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001689 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301690 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001691 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001692
Kinson Chikf1a43512011-07-14 11:28:39 -07001693 index = partition_get_index(arg);
1694 ptn = partition_get_offset(index);
1695 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001696 fastboot_fail("partition table doesn't exist");
1697 return;
1698 }
1699
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301700 size = partition_get_size(index);
1701 if (ROUND_TO_PAGE(sz,511) > size) {
1702 fastboot_fail("size too large");
1703 return;
1704 }
1705
Ajay Dudani5c761132011-04-07 20:19:04 -07001706 /* Read and skip over sparse image header */
1707 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001708 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1709 fastboot_fail("size too large");
1710 return;
1711 }
1712
Ajay Dudani5c761132011-04-07 20:19:04 -07001713 data += sparse_header->file_hdr_sz;
1714 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1715 {
1716 /* Skip the remaining bytes in a header that is longer than
1717 * we expected.
1718 */
1719 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1720 }
1721
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001722 dprintf (SPEW, "=== Sparse Image Header ===\n");
1723 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1724 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1725 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1726 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1727 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1728 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1729 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1730 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001731
1732 /* Start processing chunks */
1733 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1734 {
1735 /* Read and skip over chunk header */
1736 chunk_header = (chunk_header_t *) data;
1737 data += sizeof(chunk_header_t);
1738
1739 dprintf (SPEW, "=== Chunk Header ===\n");
1740 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1741 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1742 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1743
1744 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1745 {
1746 /* Skip the remaining bytes in a header that is longer than
1747 * we expected.
1748 */
1749 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1750 }
1751
1752 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1753 switch (chunk_header->chunk_type)
1754 {
1755 case CHUNK_TYPE_RAW:
1756 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1757 chunk_data_sz))
1758 {
1759 fastboot_fail("Bogus chunk size for chunk type Raw");
1760 return;
1761 }
1762
Ajay Dudaniab18f022011-05-12 14:39:22 -07001763 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1764 chunk_data_sz,
1765 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001766 {
1767 fastboot_fail("flash write failure");
1768 return;
1769 }
1770 total_blocks += chunk_header->chunk_sz;
1771 data += chunk_data_sz;
1772 break;
1773
1774 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001775 total_blocks += chunk_header->chunk_sz;
1776 break;
1777
Ajay Dudani5c761132011-04-07 20:19:04 -07001778 case CHUNK_TYPE_CRC:
1779 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1780 {
1781 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1782 return;
1783 }
1784 total_blocks += chunk_header->chunk_sz;
1785 data += chunk_data_sz;
1786 break;
1787
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001788 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001789 fastboot_fail("Unknown chunk type");
1790 return;
1791 }
1792 }
1793
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001794 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1795 total_blocks, sparse_header->total_blks);
1796
1797 if(total_blocks != sparse_header->total_blks)
1798 {
1799 fastboot_fail("sparse image write failure");
1800 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001801
1802 fastboot_okay("");
1803 return;
1804}
1805
1806void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1807{
1808 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001809 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1810 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001811
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001812#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001813 int ret=0;
1814 uint32 major_version=0;
1815 uint32 minor_version=0;
1816
1817 ret = scm_svc_version(&major_version,&minor_version);
1818 if(!ret)
1819 {
1820 if(major_version >= 2)
1821 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001822 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001823 {
1824 ret = encrypt_scm((uint32 **) &data, &sz);
1825 if (ret != 0) {
1826 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1827 return;
1828 }
1829
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001830 /* Protect only for SSD */
1831 if (!strcmp(arg, "ssd")) {
1832 ret = scm_protect_keystore((uint32 *) data, sz);
1833 if (ret != 0) {
1834 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1835 return;
1836 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001837 }
1838 }
1839 else
1840 {
1841 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1842 if(ret != 0)
1843 {
1844 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1845 return;
1846 }
1847 }
1848 }
1849 else
1850 {
1851 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1852 magic_number[1] == DECRYPT_MAGIC_1)
1853 {
1854 ret = decrypt_scm((uint32 **) &data, &sz);
1855 if (ret != 0) {
1856 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1857 return;
1858 }
1859 }
1860 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1861 magic_number[1] == ENCRYPT_MAGIC_1)
1862 {
1863 ret = encrypt_scm((uint32 **) &data, &sz);
1864 if (ret != 0) {
1865 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1866 return;
1867 }
1868 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001869 }
1870 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001871 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001872 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001873 dprintf(CRITICAL,"INVALID SVC Version\n");
1874 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001875 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001876#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001877
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001878 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001879 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1880 cmd_flash_mmc_img(arg, data, sz);
1881 else
1882 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001883 return;
1884}
1885
Dima Zavin214cc642009-01-26 11:16:21 -08001886void cmd_flash(const char *arg, void *data, unsigned sz)
1887{
1888 struct ptentry *ptn;
1889 struct ptable *ptable;
1890 unsigned extra = 0;
1891
1892 ptable = flash_get_ptable();
1893 if (ptable == NULL) {
1894 fastboot_fail("partition table doesn't exist");
1895 return;
1896 }
1897
1898 ptn = ptable_find(ptable, arg);
1899 if (ptn == NULL) {
1900 fastboot_fail("unknown partition name");
1901 return;
1902 }
1903
1904 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1905 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1906 fastboot_fail("image is not a boot image");
1907 return;
1908 }
1909 }
1910
Amol Jadi5c61a952012-05-04 17:05:35 -07001911 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001912 || !strcmp(ptn->name, "userdata")
1913 || !strcmp(ptn->name, "persist")
1914 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301915 if (flash_ecc_bch_enabled())
1916 /* Spare data bytes for 8 bit ECC increased by 4 */
1917 extra = ((page_size >> 9) * 20);
1918 else
1919 extra = ((page_size >> 9) * 16);
1920 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001921 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001922
1923 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1924 if (flash_write(ptn, extra, data, sz)) {
1925 fastboot_fail("flash write failure");
1926 return;
1927 }
1928 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1929 fastboot_okay("");
1930}
1931
1932void cmd_continue(const char *arg, void *data, unsigned sz)
1933{
1934 fastboot_okay("");
1935 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001936 if (target_is_emmc_boot())
1937 {
1938 boot_linux_from_mmc();
1939 }
1940 else
1941 {
1942 boot_linux_from_flash();
1943 }
Dima Zavin214cc642009-01-26 11:16:21 -08001944}
1945
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001946void cmd_reboot(const char *arg, void *data, unsigned sz)
1947{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001948 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001949 fastboot_okay("");
1950 reboot_device(0);
1951}
1952
1953void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1954{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001955 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001956 fastboot_okay("");
1957 reboot_device(FASTBOOT_MODE);
1958}
1959
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001960void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
1961{
1962 dprintf(INFO, "Enabling charger screen check\n");
1963 device.charger_screen_enabled = 1;
1964 write_device_info(&device);
1965 fastboot_okay("");
1966}
1967
1968void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
1969{
1970 dprintf(INFO, "Disabling charger screen check\n");
1971 device.charger_screen_enabled = 0;
1972 write_device_info(&device);
1973 fastboot_okay("");
1974}
1975
Unnati Gandhi15d08f92014-01-24 11:01:01 +05301976void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
1977{
1978 dprintf(INFO, "Selecting display panel %s\n", arg);
1979 if (arg)
1980 strlcpy(device.display_panel, arg,
1981 sizeof(device.display_panel));
1982 write_device_info(&device);
1983 fastboot_okay("");
1984}
1985
Shashank Mittal162244e2011-08-08 19:01:25 -07001986void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1987{
1988 if(!device.is_unlocked)
1989 {
1990 device.is_unlocked = 1;
1991 write_device_info(&device);
1992 }
1993 fastboot_okay("");
1994}
1995
Shashank Mittala0032282011-08-26 14:50:11 -07001996void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1997{
Unnati Gandhi15d08f92014-01-24 11:01:01 +05301998 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001999 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002000 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002001 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2002 fastboot_info(response);
2003 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002004 fastboot_info(response);
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302005 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2006 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002007 fastboot_okay("");
2008}
2009
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002010void cmd_preflash(const char *arg, void *data, unsigned sz)
2011{
2012 fastboot_okay("");
2013}
2014
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302015static struct fbimage logo_header = {0};
2016struct fbimage* splash_screen_flash();
2017
2018int splash_screen_check_header(struct fbimage *logo)
2019{
2020 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2021 return -1;
2022 if (logo->header.width == 0 || logo->header.height == 0)
2023 return -1;
2024 return 0;
2025}
2026
2027struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002028{
2029 struct ptentry *ptn;
2030 struct ptable *ptable;
2031 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302032 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002033
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302034
2035 ptable = flash_get_ptable();
2036 if (ptable == NULL) {
2037 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2038 return NULL;
2039 }
2040 ptn = ptable_find(ptable, "splash");
2041 if (ptn == NULL) {
2042 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2043 return NULL;
2044 }
2045
2046 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2047 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2048 return NULL;
2049 }
2050
2051 if (splash_screen_check_header(logo)) {
2052 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2053 return NULL;
2054 }
2055
2056 fb_display = fbcon_display();
2057 if (fb_display) {
2058 uint8_t *base = (uint8_t *) fb_display->base;
2059 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
2060 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002061 }
2062
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302063 if (flash_read(ptn + sizeof(logo->header), 0,
2064 base,
2065 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2066 fbcon_clear();
2067 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2068 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002069 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302070 logo->image = base;
2071 }
2072
2073 return logo;
2074}
2075
2076struct fbimage* splash_screen_mmc()
2077{
2078 int index = INVALID_PTN;
2079 unsigned long long ptn = 0;
2080 struct fbcon_config *fb_display = NULL;
2081 struct fbimage *logo = &logo_header;
2082
2083 index = partition_get_index("splash");
2084 if (index == 0) {
2085 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2086 return NULL;
2087 }
2088
2089 ptn = partition_get_offset(index);
2090 if (ptn == 0) {
2091 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2092 return NULL;
2093 }
2094
2095 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2096 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2097 return NULL;
2098 }
2099
2100 if (splash_screen_check_header(logo)) {
2101 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2102 return NULL;
2103 }
2104
2105 fb_display = fbcon_display();
2106 if (fb_display) {
2107 uint8_t *base = (uint8_t *) fb_display->base;
2108 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2109 base += LOGO_IMG_OFFSET;
2110
2111 if (mmc_read(ptn + sizeof(logo->header),
2112 base,
2113 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2114 fbcon_clear();
2115 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2116 return NULL;
2117 }
2118
2119 logo->image = base;
2120 }
2121
2122 return logo;
2123}
2124
2125
2126struct fbimage* fetch_image_from_partition()
2127{
2128 if (target_is_emmc_boot()) {
2129 return splash_screen_mmc();
2130 } else {
2131 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002132 }
2133}
2134
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002135/* Get the size from partiton name */
2136static void get_partition_size(const char *arg, char *response)
2137{
2138 uint64_t ptn = 0;
2139 uint64_t size;
2140 int index = INVALID_PTN;
2141
2142 index = partition_get_index(arg);
2143
2144 if (index == INVALID_PTN)
2145 {
2146 dprintf(CRITICAL, "Invalid partition index\n");
2147 return;
2148 }
2149
2150 ptn = partition_get_offset(index);
2151
2152 if(!ptn)
2153 {
2154 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2155 return;
2156 }
2157
2158 size = partition_get_size(index);
2159
2160 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2161 return;
2162}
2163
2164/*
2165 * Publish the partition type & size info
2166 * fastboot getvar will publish the required information.
2167 * fastboot getvar partition_size:<partition_name>: partition size in hex
2168 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2169 */
2170static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2171{
2172 uint8_t i;
2173
2174 for (i = 0; i < num_parts; i++) {
2175 get_partition_size(info[i].part_name, info[i].size_response);
2176
2177 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2178 {
2179 dprintf(CRITICAL, "partition size name truncated\n");
2180 return;
2181 }
2182 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2183 {
2184 dprintf(CRITICAL, "partition type name truncated\n");
2185 return;
2186 }
2187
2188 /* publish partition size & type info */
2189 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2190 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2191 }
2192}
2193
Amol Jadi5edf3552013-07-23 14:15:34 -07002194/* register commands and variables for fastboot */
2195void aboot_fastboot_register_commands(void)
2196{
2197 if (target_is_emmc_boot())
2198 {
2199 fastboot_register("flash:", cmd_flash_mmc);
2200 fastboot_register("erase:", cmd_erase_mmc);
2201 }
2202 else
2203 {
2204 fastboot_register("flash:", cmd_flash);
2205 fastboot_register("erase:", cmd_erase);
2206 }
2207
2208 fastboot_register("boot", cmd_boot);
2209 fastboot_register("continue", cmd_continue);
2210 fastboot_register("reboot", cmd_reboot);
2211 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2212 fastboot_register("oem unlock", cmd_oem_unlock);
2213 fastboot_register("oem device-info", cmd_oem_devinfo);
2214 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002215 fastboot_register("oem enable-charger-screen",
2216 cmd_oem_enable_charger_screen);
2217 fastboot_register("oem disable-charger-screen",
2218 cmd_oem_disable_charger_screen);
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302219 fastboot_register("oem select-display-panel",
2220 cmd_oem_select_display_panel);
Amol Jadi5edf3552013-07-23 14:15:34 -07002221 /* publish variables and their values */
2222 fastboot_publish("product", TARGET(BOARD));
2223 fastboot_publish("kernel", "lk");
2224 fastboot_publish("serialno", sn_buf);
2225
2226 /*
2227 * partition info is supported only for emmc partitions
2228 * Calling this for NAND prints some error messages which
2229 * is harmless but misleading. Avoid calling this for NAND
2230 * devices.
2231 */
2232 if (target_is_emmc_boot())
2233 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2234
2235 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002236 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2237 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002238 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002239 /* Is the charger screen check enabled */
2240 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2241 device.charger_screen_enabled);
2242 fastboot_publish("charger-screen-enabled",
2243 (const char *) charger_screen_enabled);
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302244 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2245 device.display_panel);
2246 fastboot_publish("display-panel",
2247 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002248}
2249
Brian Swetland9c4c0752009-01-25 16:23:50 -08002250void aboot_init(const struct app_descriptor *app)
2251{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002252 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002253 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002254
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002255 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002256 if (target_is_emmc_boot())
2257 {
2258 page_size = 2048;
2259 page_mask = page_size - 1;
2260 }
2261 else
2262 {
2263 page_size = flash_page_size();
2264 page_mask = page_size - 1;
2265 }
2266
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002267 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2268
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002269 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002270
Aravind Venkateswarandb0d5262014-02-25 16:25:30 -08002271 /* Display splash screen if enabled */
2272#if DISPLAY_SPLASH_SCREEN
2273 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302274 target_display_init(device.display_panel);
Aravind Venkateswarandb0d5262014-02-25 16:25:30 -08002275 dprintf(SPEW, "Display Init: Done\n");
2276#endif
2277
2278
Greg Griscod6250552011-06-29 14:40:23 -07002279 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002280 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002281
Dhaval Patel223ec952013-07-18 14:49:44 -07002282 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2283
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002284 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002285 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002286 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002287 dprintf(ALWAYS,"dload mode key sequence detected\n");
2288 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002289 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002290 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002291 }
2292 else
2293 {
2294 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002295 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002296 }
2297 boot_into_fastboot = true;
2298 }
2299 if (!boot_into_fastboot)
2300 {
2301 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2302 boot_into_recovery = 1;
2303 if (!boot_into_recovery &&
2304 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002305 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002306 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002307 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002308 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002309 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002310 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002311
Ajay Dudani77421292010-10-27 19:34:06 -07002312 reboot_mode = check_reboot_mode();
2313 if (reboot_mode == RECOVERY_MODE) {
2314 boot_into_recovery = 1;
2315 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002316 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002317 }
2318
Pavel Nedev5d91d412013-04-29 11:34:24 +03002319 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002320 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002321 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002322 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002323 if(emmc_recovery_init())
2324 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2325 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002326 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002327 if((device.is_unlocked) || (device.is_tampered))
2328 {
2329 #ifdef TZ_TAMPER_FUSE
2330 set_tamper_fuse_cmd();
2331 #endif
2332 #if USE_PCOM_SECBOOT
2333 set_tamper_flag(device.is_tampered);
2334 #endif
2335 }
Shashank Mittala0032282011-08-26 14:50:11 -07002336 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002337 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002338 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002339 else
2340 {
2341 recovery_init();
2342 #if USE_PCOM_SECBOOT
2343 if((device.is_unlocked) || (device.is_tampered))
2344 set_tamper_flag(device.is_tampered);
2345 #endif
2346 boot_linux_from_flash();
2347 }
2348 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2349 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002350 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002351
Amol Jadi5edf3552013-07-23 14:15:34 -07002352 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002353
Amol Jadi5edf3552013-07-23 14:15:34 -07002354 /* register aboot specific fastboot commands */
2355 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002356
Amol Jadi5edf3552013-07-23 14:15:34 -07002357 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002358 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002359
2360 /* initialize and start fastboot */
2361 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002362}
2363
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002364uint32_t get_page_size()
2365{
2366 return page_size;
2367}
2368
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002369/*
2370 * Calculated and save hash (SHA256) for non-signed boot image.
2371 *
2372 * Hash the same data that is checked on the signed boot image.
2373 * Kernel and Ramdisk are already read to memory buffers.
2374 * Need to read the entire device-tree from mmc
2375 * since non-signed image only read the DT tags of the relevant platform.
2376 *
2377 * @param kernel_addr - kernel bufer
2378 * @param kernel_actual - kernel size in bytes
2379 * @param ramdisk_addr - ramdisk buffer
2380 * @param ramdisk_actual - ramdisk size
2381 * @param ptn - partition
2382 * @param dt_offset - device tree offset on mmc partition
2383 * @param dt_size
2384 *
2385 * @return int - 0 on success, negative value on failure.
2386 */
2387int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2388 void *ramdisk_addr, unsigned ramdisk_actual,
2389 unsigned long long ptn,
2390 unsigned dt_offset, unsigned dt_size)
2391{
2392 SHA256_CTX sha256_ctx;
2393 char digest[32]={0};
2394 char *buf = (char *)target_get_scratch_address();
2395 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2396 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2397
2398 SHA256_Init(&sha256_ctx);
2399
2400 /* Read Boot Header */
2401 if (mmc_read(ptn, buf, page_size))
2402 {
2403 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2404 return -1;
2405 }
2406 /* Read entire Device Tree */
2407 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2408 {
2409 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2410 return -1;
2411 }
2412 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2413 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2414 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2415 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2416
2417 SHA256_Final(digest, &sha256_ctx);
2418
2419 save_kernel_hash_cmd(digest);
2420 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2421
2422 return 0;
2423}
2424
Brian Swetland9c4c0752009-01-25 16:23:50 -08002425APP_START(aboot)
2426 .init = aboot_init,
2427APP_END