blob: 710ffd4d749bea79cae021ddac145b57579cd06a [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Kuogee Hsieh55e92c52013-12-20 08:34:10 -08005 * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
43#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080044#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080045#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080046#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070047#include <target.h>
48#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070049#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070050#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070051#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070052#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070053#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030054#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070055#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070056#include <boot_device.h>
Dima Zavin214cc642009-01-26 11:16:21 -080057
Neeti Desai17379b82012-06-04 18:42:53 -070058#if DEVICE_TREE
59#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070060#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070061#endif
62
Shashank Mittalcd98d472011-08-02 14:29:24 -070063#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080064#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080065#include "bootimg.h"
66#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070067#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070068#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070069#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070070#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070071#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070072
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070073extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070074extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070075extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070076extern int get_target_boot_params(const char *cmdline, const char *part,
77 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070078
79void write_device_info_mmc(device_info *dev);
80void write_device_info_flash(device_info *dev);
81
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070082#define EXPAND(NAME) #NAME
83#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070084
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080085#ifdef MEMBASE
86#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
87#else
David Ng183a7422009-12-07 14:55:21 -080088#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080089#endif
90
Deepa Dinamani0e163a42013-05-24 17:08:15 -070091#ifndef MEMSIZE
92#define MEMSIZE 1024*1024
93#endif
94
95#define MAX_TAGS_SIZE 1024
96
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080097#define RECOVERY_MODE 0x77665502
98#define FASTBOOT_MODE 0x77665500
99
Kun Liang2f1601a2013-08-12 16:29:54 +0800100/* make 4096 as default size to ensure EFS,EXT4's erasing */
101#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800102#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800103
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800104#define UBI_MAGIC "UBI#"
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700105#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800106#define UBI_MAGIC_SIZE 0x04
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700107#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800108
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800109#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
110
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700111#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
112
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700113#if UFS_SUPPORT
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700114static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700115#else
David Ng183a7422009-12-07 14:55:21 -0800116static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700117#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800118static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300119static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800120static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800121static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700122static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300123static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800124
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800125static const char *baseband_apq = " androidboot.baseband=apq";
126static const char *baseband_msm = " androidboot.baseband=msm";
127static const char *baseband_csfb = " androidboot.baseband=csfb";
128static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700129static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800130static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700131static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800132static const char *baseband_dsda = " androidboot.baseband=dsda";
133static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800134static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800135static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800136
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700137static unsigned page_size = 0;
138static unsigned page_mask = 0;
139static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
140static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700141static char target_boot_params[64];
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700142
Shashank Mittalcd98d472011-08-02 14:29:24 -0700143/* Assuming unauthorized kernel image by default */
144static int auth_kernel_img = 0;
145
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530146static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700147
Dima Zavin42168f22009-01-30 11:52:22 -0800148struct atag_ptbl_entry
149{
150 char name[16];
151 unsigned offset;
152 unsigned size;
153 unsigned flags;
154};
155
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700156/*
157 * Partition info, required to be published
158 * for fastboot
159 */
160struct getvar_partition_info {
161 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
162 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
163 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
164 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
165 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
166};
167
168/*
169 * Right now, we are publishing the info for only
170 * three partitions
171 */
172struct getvar_partition_info part_info[] =
173{
174 { "system" , "partition-size:", "partition-type:", "", "ext4" },
175 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
176 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
177};
178
179char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700180char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800181char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700182char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530183char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700184
Greg Griscod2471ef2011-07-14 13:00:42 -0700185extern int emmc_recovery_init(void);
186
Kinson Chik0b1c8162011-08-31 16:31:57 -0700187#if NO_KEYPAD_DRIVER
188extern int fastboot_trigger(void);
189#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700190
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800191static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700192{
193 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700194#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800195 if (is_arm64)
196 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
197 else
198 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700199 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
200 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700201#endif
202}
203
Dima Zavin42168f22009-01-30 11:52:22 -0800204static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
205{
206 struct atag_ptbl_entry atag_ptn;
207
208 memcpy(atag_ptn.name, ptn->name, 16);
209 atag_ptn.name[15] = '\0';
210 atag_ptn.offset = ptn->start;
211 atag_ptn.size = ptn->length;
212 atag_ptn.flags = ptn->flags;
213 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
214 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
215}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800216
Neeti Desaie245d492012-06-01 12:52:13 -0700217unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800218{
David Ng183a7422009-12-07 14:55:21 -0800219 int cmdline_len = 0;
220 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800221 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700222 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800223 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300224 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700225 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700226 char *boot_dev_buf = NULL;
Dima Zavin42168f22009-01-30 11:52:22 -0800227
Brian Swetland9c4c0752009-01-25 16:23:50 -0800228 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800229 cmdline_len = strlen(cmdline);
230 have_cmdline = 1;
231 }
232 if (target_is_emmc_boot()) {
233 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700234#if UFS_SUPPORT
235 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
236 ASSERT(boot_dev_buf);
237 platform_boot_dev_cmdline(boot_dev_buf);
238 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700239#endif
David Ng183a7422009-12-07 14:55:21 -0800240 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800241
242 cmdline_len += strlen(usb_sn_cmdline);
243 cmdline_len += strlen(sn_buf);
244
Pavel Nedev5614d222013-06-17 18:01:02 +0300245 if (boot_into_recovery && gpt_exists)
246 cmdline_len += strlen(secondary_gpt_enable);
247
Pavel Nedev328ac822013-04-05 15:25:11 +0300248 if (boot_into_ffbm) {
249 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700250 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800251 /* reduce kernel console messages to speed-up boot */
252 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700253 } else if (device.charger_screen_enabled &&
254 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700255 pause_at_bootup = 1;
256 cmdline_len += strlen(battchg_pause);
257 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800258
Shashank Mittalcd98d472011-08-02 14:29:24 -0700259 if(target_use_signed_kernel() && auth_kernel_img) {
260 cmdline_len += strlen(auth_kernel);
261 }
262
Joonwoo Park61112782013-10-02 19:50:39 -0700263 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
264 "system",
265 target_boot_params,
266 sizeof(target_boot_params)) == 0) {
267 have_target_boot_params = 1;
268 cmdline_len += strlen(target_boot_params);
269 }
270
Ajay Dudanid04110c2011-01-17 23:55:07 -0800271 /* Determine correct androidboot.baseband to use */
272 switch(target_baseband())
273 {
274 case BASEBAND_APQ:
275 cmdline_len += strlen(baseband_apq);
276 break;
277
278 case BASEBAND_MSM:
279 cmdline_len += strlen(baseband_msm);
280 break;
281
282 case BASEBAND_CSFB:
283 cmdline_len += strlen(baseband_csfb);
284 break;
285
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800286 case BASEBAND_SVLTE2A:
287 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800288 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700289
290 case BASEBAND_MDM:
291 cmdline_len += strlen(baseband_mdm);
292 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700293
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800294 case BASEBAND_MDM2:
295 cmdline_len += strlen(baseband_mdm2);
296 break;
297
Amol Jadi5c61a952012-05-04 17:05:35 -0700298 case BASEBAND_SGLTE:
299 cmdline_len += strlen(baseband_sglte);
300 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530301
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800302 case BASEBAND_SGLTE2:
303 cmdline_len += strlen(baseband_sglte2);
304 break;
305
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530306 case BASEBAND_DSDA:
307 cmdline_len += strlen(baseband_dsda);
308 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800309
310 case BASEBAND_DSDA2:
311 cmdline_len += strlen(baseband_dsda2);
312 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800313 }
314
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800315 if (cmdline) {
316 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
317 target_display_panel_node(device.display_panel,
318 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
319 strlen(display_panel_buf)) {
320 cmdline_len += strlen(display_panel_buf);
321 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700322 }
323
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800324 if (target_warm_boot()) {
325 warm_boot = true;
326 cmdline_len += strlen(warmboot_cmdline);
327 }
328
David Ng183a7422009-12-07 14:55:21 -0800329 if (cmdline_len > 0) {
330 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700331 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
332 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700333
Amol Jadi168b7712012-03-06 16:15:00 -0800334 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700335 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800336 if (have_cmdline) {
337 src = cmdline;
338 while ((*dst++ = *src++));
339 }
340 if (target_is_emmc_boot()) {
341 src = emmc_cmdline;
342 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700343 have_cmdline = 1;
344 while ((*dst++ = *src++));
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700345#if UFS_SUPPORT
346 src = boot_dev_buf;
347 if (have_cmdline) --dst;
348 while ((*dst++ = *src++));
349#endif
David Ngf773dde2010-07-26 19:55:08 -0700350 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800351
352 src = usb_sn_cmdline;
353 if (have_cmdline) --dst;
354 have_cmdline = 1;
355 while ((*dst++ = *src++));
356 src = sn_buf;
357 if (have_cmdline) --dst;
358 have_cmdline = 1;
359 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800360 if (warm_boot) {
361 if (have_cmdline) --dst;
362 src = warmboot_cmdline;
363 while ((*dst++ = *src++));
364 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800365
Pavel Nedev5614d222013-06-17 18:01:02 +0300366 if (boot_into_recovery && gpt_exists) {
367 src = secondary_gpt_enable;
368 if (have_cmdline) --dst;
369 while ((*dst++ = *src++));
370 }
371
Pavel Nedev328ac822013-04-05 15:25:11 +0300372 if (boot_into_ffbm) {
373 src = androidboot_mode;
374 if (have_cmdline) --dst;
375 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700376 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300377 if (have_cmdline) --dst;
378 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800379 src = loglevel;
380 if (have_cmdline) --dst;
381 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300382 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700383 src = battchg_pause;
384 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800385 while ((*dst++ = *src++));
386 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800387
Shashank Mittalcd98d472011-08-02 14:29:24 -0700388 if(target_use_signed_kernel() && auth_kernel_img) {
389 src = auth_kernel;
390 if (have_cmdline) --dst;
391 while ((*dst++ = *src++));
392 }
393
Ajay Dudanid04110c2011-01-17 23:55:07 -0800394 switch(target_baseband())
395 {
396 case BASEBAND_APQ:
397 src = baseband_apq;
398 if (have_cmdline) --dst;
399 while ((*dst++ = *src++));
400 break;
401
402 case BASEBAND_MSM:
403 src = baseband_msm;
404 if (have_cmdline) --dst;
405 while ((*dst++ = *src++));
406 break;
407
408 case BASEBAND_CSFB:
409 src = baseband_csfb;
410 if (have_cmdline) --dst;
411 while ((*dst++ = *src++));
412 break;
413
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800414 case BASEBAND_SVLTE2A:
415 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800416 if (have_cmdline) --dst;
417 while ((*dst++ = *src++));
418 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700419
420 case BASEBAND_MDM:
421 src = baseband_mdm;
422 if (have_cmdline) --dst;
423 while ((*dst++ = *src++));
424 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700425
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800426 case BASEBAND_MDM2:
427 src = baseband_mdm2;
428 if (have_cmdline) --dst;
429 while ((*dst++ = *src++));
430 break;
431
Amol Jadi5c61a952012-05-04 17:05:35 -0700432 case BASEBAND_SGLTE:
433 src = baseband_sglte;
434 if (have_cmdline) --dst;
435 while ((*dst++ = *src++));
436 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530437
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800438 case BASEBAND_SGLTE2:
439 src = baseband_sglte2;
440 if (have_cmdline) --dst;
441 while ((*dst++ = *src++));
442 break;
443
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530444 case BASEBAND_DSDA:
445 src = baseband_dsda;
446 if (have_cmdline) --dst;
447 while ((*dst++ = *src++));
448 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800449
450 case BASEBAND_DSDA2:
451 src = baseband_dsda2;
452 if (have_cmdline) --dst;
453 while ((*dst++ = *src++));
454 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800455 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700456
457 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700458 src = display_panel_buf;
459 if (have_cmdline) --dst;
460 while ((*dst++ = *src++));
461 }
Joonwoo Park61112782013-10-02 19:50:39 -0700462
463 if (have_target_boot_params) {
464 if (have_cmdline) --dst;
465 src = target_boot_params;
466 while ((*dst++ = *src++));
467 }
Neeti Desaie245d492012-06-01 12:52:13 -0700468 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700469
470
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700471 if (boot_dev_buf)
472 free(boot_dev_buf);
473
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800474 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700475 return cmdline_final;
476}
477
478unsigned *atag_core(unsigned *ptr)
479{
480 /* CORE */
481 *ptr++ = 2;
482 *ptr++ = 0x54410001;
483
484 return ptr;
485
486}
487
488unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
489 unsigned ramdisk_size)
490{
491 if (ramdisk_size) {
492 *ptr++ = 4;
493 *ptr++ = 0x54420005;
494 *ptr++ = (unsigned)ramdisk;
495 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800496 }
497
Neeti Desaie245d492012-06-01 12:52:13 -0700498 return ptr;
499}
500
501unsigned *atag_ptable(unsigned **ptr_addr)
502{
503 int i;
504 struct ptable *ptable;
505
506 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700507 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
508 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700509 *(*ptr_addr)++ = 0x4d534d70;
510 for (i = 0; i < ptable->count; ++i)
511 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
512 }
513
514 return (*ptr_addr);
515}
516
517unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
518{
519 int cmdline_length = 0;
520 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700521 char *dest;
522
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800523 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700524 n = (cmdline_length + 4) & (~3);
525
526 *ptr++ = (n / 4) + 2;
527 *ptr++ = 0x54410009;
528 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800529 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700530 ptr += (n / 4);
531
532 return ptr;
533}
534
535unsigned *atag_end(unsigned *ptr)
536{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800537 /* END */
538 *ptr++ = 0;
539 *ptr++ = 0;
540
Neeti Desaie245d492012-06-01 12:52:13 -0700541 return ptr;
542}
543
544void generate_atags(unsigned *ptr, const char *cmdline,
545 void *ramdisk, unsigned ramdisk_size)
546{
547
548 ptr = atag_core(ptr);
549 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
550 ptr = target_atag_mem(ptr);
551
552 /* Skip NAND partition ATAGS for eMMC boot */
553 if (!target_is_emmc_boot()){
554 ptr = atag_ptable(&ptr);
555 }
556
557 ptr = atag_cmdline(ptr, cmdline);
558 ptr = atag_end(ptr);
559}
560
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700561typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700562void boot_linux(void *kernel, unsigned *tags,
563 const char *cmdline, unsigned machtype,
564 void *ramdisk, unsigned ramdisk_size)
565{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800566 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800567#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700568 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800569#endif
570
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700571 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800572 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800573 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800574
575 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700576
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800577 final_cmdline = update_cmdline((const char*)cmdline);
578
Neeti Desai17379b82012-06-04 18:42:53 -0700579#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800580 dprintf(INFO, "Updating device tree: start\n");
581
Neeti Desai17379b82012-06-04 18:42:53 -0700582 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800583 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700584 if(ret)
585 {
586 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
587 ASSERT(0);
588 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800589 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700590#else
Neeti Desaie245d492012-06-01 12:52:13 -0700591 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800592 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700593#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700594
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700595 /* Perform target specific cleanup */
596 target_uninit();
597
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800598 /* Turn off splash screen if enabled */
599#if DISPLAY_SPLASH_SCREEN
600 target_display_shutdown();
601#endif
602
603
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800604 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
605 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800606
607 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700608
Amol Jadi4421e652011-06-16 15:00:48 -0700609 /* do any platform specific cleanup before kernel entry */
610 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700611
Brian Swetland9c4c0752009-01-25 16:23:50 -0800612 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700613
Amol Jadi504f9fe2012-08-16 13:56:48 -0700614#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800615 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700616#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700617 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800618
619 if (IS_ARM64(kptr))
620 /* Jump to a 64bit kernel */
621 scm_elexec_call((paddr_t)kernel, tags_phys);
622 else
623 /* Jump to a 32bit kernel */
624 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800625}
626
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700627/* Function to check if the memory address range falls within the aboot
628 * boundaries.
629 * start: Start of the memory region
630 * size: Size of the memory region
631 */
632int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
633{
634 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800635 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700636 return -1;
637
638 /* Check for memory overlap. */
639 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
640 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700641 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700642 return 0;
643 else
644 return -1;
645}
646
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800647#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800648
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800649BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800650#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800651BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800652#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800653
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700654static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
655{
656 int ret;
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800657#if IMAGE_VERIF_ALGO_SHA1
658 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
659#else
660 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
661#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700662
663 /* Assume device is rooted at this time. */
664 device.is_tampered = 1;
665
666 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
667
668 ret = image_verify((unsigned char *)bootimg_addr,
669 (unsigned char *)(bootimg_addr + bootimg_size),
670 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800671 auth_algo);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700672
673 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
674
675 if (ret)
676 {
677 /* Authorized kernel */
678 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700679 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700680 }
681
682#if USE_PCOM_SECBOOT
683 set_tamper_flag(device.is_tampered);
684#endif
685
686 if(device.is_tampered)
687 {
688 write_device_info_mmc(&device);
689 #ifdef TZ_TAMPER_FUSE
690 set_tamper_fuse_cmd();
691 #endif
692 #ifdef ASSERT_ON_TAMPER
693 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
694 ASSERT(0);
695 #endif
696 }
697}
698
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530699static bool check_format_bit()
700{
701 bool ret = false;
702 int index;
703 uint64_t offset;
704 struct boot_selection_info *in = NULL;
705 char *buf = NULL;
706
707 index = partition_get_index("bootselect");
708 if (index == INVALID_PTN)
709 {
710 dprintf(INFO, "Unable to locate /bootselect partition\n");
711 return ret;
712 }
713 offset = partition_get_offset(index);
714 if(!offset)
715 {
716 dprintf(INFO, "partition /bootselect doesn't exist\n");
717 return ret;
718 }
719 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
720 ASSERT(buf);
721 if (mmc_read(offset, (unsigned int *)buf, page_size))
722 {
723 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
724 free(buf);
725 return ret;
726 }
727 in = (struct boot_selection_info *) buf;
728 if ((in->signature == BOOTSELECT_SIGNATURE) &&
729 (in->version == BOOTSELECT_VERSION)) {
730 if ((in->state_info & BOOTSELECT_FORMAT) &&
731 !(in->state_info & BOOTSELECT_FACTORY))
732 ret = true;
733 } else {
734 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
735 in->signature, in->version);
736 ASSERT(0);
737 }
738 free(buf);
739 return ret;
740}
741
Shashank Mittal23b8f422010-04-16 19:27:21 -0700742int boot_linux_from_mmc(void)
743{
744 struct boot_img_hdr *hdr = (void*) buf;
745 struct boot_img_hdr *uhdr;
746 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700747 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700748 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700749 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700750
Shashank Mittalcd98d472011-08-02 14:29:24 -0700751 unsigned char *image_addr = 0;
752 unsigned kernel_actual;
753 unsigned ramdisk_actual;
754 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700755 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700756
757#if DEVICE_TREE
758 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700759 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700760 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800761 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700762 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700763#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800764 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
765 struct kernel64_hdr *kptr = (void*) kbuf;
766
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530767 if (check_format_bit())
768 boot_into_recovery = 1;
769
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700770 if (!boot_into_recovery) {
771 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
772 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
773 if (rcode <= 0) {
774 boot_into_ffbm = false;
775 if (rcode < 0)
776 dprintf(CRITICAL,"failed to get ffbm cookie");
777 } else
778 boot_into_ffbm = true;
779 } else
780 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700781 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
782 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
783 dprintf(INFO, "Unified boot method!\n");
784 hdr = uhdr;
785 goto unified_boot;
786 }
Greg Griscod6250552011-06-29 14:40:23 -0700787 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700788 index = partition_get_index("boot");
789 ptn = partition_get_offset(index);
790 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700791 dprintf(CRITICAL, "ERROR: No boot partition found\n");
792 return -1;
793 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700794 }
795 else {
796 index = partition_get_index("recovery");
797 ptn = partition_get_offset(index);
798 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700799 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
800 return -1;
801 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700802 }
803
Greg Griscod6250552011-06-29 14:40:23 -0700804 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700805 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
806 return -1;
807 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700808
809 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700810 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700811 return -1;
812 }
813
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700814 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530815
816 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
817 dprintf(CRITICAL, "ERROR: Invalid page size\n");
818 return -1;
819 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700820 page_size = hdr->page_size;
821 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700822 }
823
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800824 /* Read the next page to get kernel Image header
825 * which lives in the second page for arm64 targets.
826 */
827
828 if (mmc_read(ptn + page_size, (unsigned int *) kbuf, page_size)) {
829 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
830 return -1;
831 }
832
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700833 /*
834 * Update the kernel/ramdisk/tags address if the boot image header
835 * has default values, these default values come from mkbootimg when
836 * the boot image is flashed using fastboot flash:raw
837 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800838 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700839
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700840 /* Get virtual addresses since the hdr saves physical addresses. */
841 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
842 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
843 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700844
845 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
846 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
847
848 /* Check if the addresses in the header are valid. */
849 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
850 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
851 {
852 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
853 return -1;
854 }
855
856#ifndef DEVICE_TREE
857 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
858 {
859 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
860 return -1;
861 }
862#endif
863
Shashank Mittalcd98d472011-08-02 14:29:24 -0700864 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300865 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
866 (int) target_use_signed_kernel(),
867 device.is_unlocked,
868 device.is_tampered);
869
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700870 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800871 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700872 offset = 0;
873
Shashank Mittalcd98d472011-08-02 14:29:24 -0700874 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700875
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800876#if DEVICE_TREE
877 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
878 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700879
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700880 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700881 {
882 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
883 return -1;
884 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800885#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530886 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700887
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700888#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700889
Amol Jadib6be5c12012-11-14 13:39:51 -0800890 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700891 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800892
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700893 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
894 {
895 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
896 return -1;
897 }
898
Shashank Mittalcd98d472011-08-02 14:29:24 -0700899 /* Read image without signature */
900 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
901 {
902 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
903 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800904 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700905
Amol Jadib6be5c12012-11-14 13:39:51 -0800906 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700907 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800908
Shashank Mittalcd98d472011-08-02 14:29:24 -0700909 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700910
911 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
912 {
913 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
914 return -1;
915 }
916
Shashank Mittalcd98d472011-08-02 14:29:24 -0700917 /* Read signature */
918 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
919 {
920 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700921 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700922 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800923
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700924 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700925
Neeti Desai465491e2012-07-31 12:53:35 -0700926 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700927 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
928 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700929
Neeti Desai465491e2012-07-31 12:53:35 -0700930 #if DEVICE_TREE
931 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700932 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700933 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700934
Deepa Dinamani19648b42013-09-05 17:05:55 -0700935 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700936 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
937 return -1;
938 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700939
Neeti Desai465491e2012-07-31 12:53:35 -0700940 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700941 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700942 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
943 return -1;
944 }
945
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700946 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700947 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700948 {
949 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
950 return -1;
951 }
952
Joel Kingaa335dc2013-06-03 16:11:08 -0700953 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800954 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800955 /*
956 * If appended dev tree is found, update the atags with
957 * memory address to the DTB appended location on RAM.
958 * Else update with the atags address in the kernel header
959 */
960 void *dtb;
961 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700962 hdr->kernel_size,
963 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800964 if (!dtb) {
965 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
966 return -1;
967 }
Neeti Desai465491e2012-07-31 12:53:35 -0700968 }
969 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700970 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700971 else
972 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800973 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700974
Amol Jadib6be5c12012-11-14 13:39:51 -0800975 dprintf(INFO, "Loading boot image (%d): start\n",
976 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700977 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800978
979 offset = page_size;
980
981 /* Load kernel */
982 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700983 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
984 return -1;
985 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800986 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700987
Amol Jadib6be5c12012-11-14 13:39:51 -0800988 /* Load ramdisk */
989 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700990 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800991 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700992 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
993 return -1;
994 }
995 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800996 offset += ramdisk_actual;
997
998 dprintf(INFO, "Loading boot image (%d): done\n",
999 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001000 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -07001001
1002 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001003 offset += second_actual;
1004 /* Second image loading not implemented. */
1005 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -07001006 }
1007
1008 #if DEVICE_TREE
1009 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -07001010 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -07001011 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
1012 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1013 return -1;
1014 }
1015 table = (struct dt_table*) dt_buf;
1016
Deepa Dinamani19648b42013-09-05 17:05:55 -07001017 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001018 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1019 return -1;
1020 }
1021
Deepa Dinamani19648b42013-09-05 17:05:55 -07001022 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1023 if (!table)
1024 return -1;
1025
1026 /* Read the entire device tree table into buffer */
1027 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
1028 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1029 return -1;
1030 }
1031
Joel Kingaa335dc2013-06-03 16:11:08 -07001032 /* Find index of device tree within device tree table */
1033 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001034 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1035 return -1;
1036 }
1037
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001038 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001039 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001040 {
1041 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1042 return -1;
1043 }
1044
Joel Kingaa335dc2013-06-03 16:11:08 -07001045 if(mmc_read(ptn + offset + dt_entry.offset,
1046 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -07001047 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1048 return -1;
1049 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +03001050 #ifdef TZ_SAVE_KERNEL_HASH
1051 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
1052 hdr->ramdisk_addr, ramdisk_actual,
1053 ptn, offset, hdr->dt_size);
1054 #endif /* TZ_SAVE_KERNEL_HASH */
1055
Channagoud Kadabi35095622013-03-01 13:53:05 -08001056 } else {
Aparna Mallavarapu33127372014-06-03 22:15:54 +05301057
1058 /* Validate the tags_addr */
1059 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1060 {
1061 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1062 return -1;
1063 }
Dima Zavin77e41f32013-03-06 16:10:43 -08001064 /*
1065 * If appended dev tree is found, update the atags with
1066 * memory address to the DTB appended location on RAM.
1067 * Else update with the atags address in the kernel header
1068 */
1069 void *dtb;
1070 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001071 kernel_actual,
1072 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001073 if (!dtb) {
1074 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1075 return -1;
1076 }
Neeti Desai465491e2012-07-31 12:53:35 -07001077 }
1078 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001079 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001080
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001081 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1082 target_load_ssd_keystore();
1083
Shashank Mittal23b8f422010-04-16 19:27:21 -07001084unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001085
Dima Zavin77e41f32013-03-06 16:10:43 -08001086 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001087 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001088 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1089
1090 return 0;
1091}
1092
Dima Zavin214cc642009-01-26 11:16:21 -08001093int boot_linux_from_flash(void)
1094{
1095 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001096 struct ptentry *ptn;
1097 struct ptable *ptable;
1098 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001099
Shashank Mittalcd98d472011-08-02 14:29:24 -07001100 unsigned char *image_addr = 0;
1101 unsigned kernel_actual;
1102 unsigned ramdisk_actual;
1103 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001104 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001105
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001106#if DEVICE_TREE
1107 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001108 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001109 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001110 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001111#endif
1112
David Ng183a7422009-12-07 14:55:21 -08001113 if (target_is_emmc_boot()) {
1114 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1115 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1116 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1117 return -1;
1118 }
1119 goto continue_boot;
1120 }
1121
Dima Zavin214cc642009-01-26 11:16:21 -08001122 ptable = flash_get_ptable();
1123 if (ptable == NULL) {
1124 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1125 return -1;
1126 }
1127
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001128 if(!boot_into_recovery)
1129 {
1130 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001131
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001132 if (ptn == NULL) {
1133 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1134 return -1;
1135 }
1136 }
1137 else
1138 {
1139 ptn = ptable_find(ptable, "recovery");
1140 if (ptn == NULL) {
1141 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1142 return -1;
1143 }
Dima Zavin214cc642009-01-26 11:16:21 -08001144 }
1145
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001146 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001147 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1148 return -1;
1149 }
Dima Zavin214cc642009-01-26 11:16:21 -08001150
1151 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001152 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001153 return -1;
1154 }
1155
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001156 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001157 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 -08001158 return -1;
1159 }
1160
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001161 /*
1162 * Update the kernel/ramdisk/tags address if the boot image header
1163 * has default values, these default values come from mkbootimg when
1164 * the boot image is flashed using fastboot flash:raw
1165 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001166 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001167
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001168 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001169 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1170 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1171 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1172
1173 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1174 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1175
1176 /* Check if the addresses in the header are valid. */
1177 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1178 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1179 {
1180 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1181 return -1;
1182 }
1183
1184#ifndef DEVICE_TREE
1185 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1186 {
1187 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1188 return -1;
1189 }
1190#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001191
Shashank Mittalcd98d472011-08-02 14:29:24 -07001192 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001193 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001194 {
1195 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001196 offset = 0;
1197
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001198#if DEVICE_TREE
1199 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1200 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001201
1202 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1203 {
1204 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1205 return -1;
1206 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001207#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001208 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001209#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001210
Amol Jadib6be5c12012-11-14 13:39:51 -08001211 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001212 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001213
Shashank Mittalcd98d472011-08-02 14:29:24 -07001214 /* Read image without signature */
1215 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1216 {
1217 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1218 return -1;
1219 }
Dima Zavin214cc642009-01-26 11:16:21 -08001220
Amol Jadib6be5c12012-11-14 13:39:51 -08001221 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001222 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001223
Shashank Mittalcd98d472011-08-02 14:29:24 -07001224 offset = imagesize_actual;
1225 /* Read signature */
1226 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1227 {
1228 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001229 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001230 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001231
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001232 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001233
1234 /* Move kernel and ramdisk to correct address */
1235 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1236 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001237#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001238 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001239 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001240 {
1241 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1242 return -1;
1243 }
1244
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001245 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1246#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001247
1248 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001249 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001250 {
1251 write_device_info_flash(&device);
1252 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301253#if USE_PCOM_SECBOOT
1254 set_tamper_flag(device.is_tampered);
1255#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001256 }
1257 else
1258 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001259 offset = page_size;
1260
Amol Jadib6be5c12012-11-14 13:39:51 -08001261 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1262 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1263 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1264
1265 dprintf(INFO, "Loading boot image (%d): start\n",
1266 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001267 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001268
1269 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001270 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1271 return -1;
1272 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001273 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001274
Amol Jadib6be5c12012-11-14 13:39:51 -08001275 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001276 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1277 return -1;
1278 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001279 offset += ramdisk_actual;
1280
1281 dprintf(INFO, "Loading boot image (%d): done\n",
1282 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001283 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001284
1285 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001286 offset += second_actual;
1287 /* Second image loading not implemented. */
1288 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001289 }
1290
1291#if DEVICE_TREE
1292 if(hdr->dt_size != 0) {
1293
1294 /* Read the device tree table into buffer */
1295 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1296 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1297 return -1;
1298 }
1299
1300 table = (struct dt_table*) dt_buf;
1301
Deepa Dinamani19648b42013-09-05 17:05:55 -07001302 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001303 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1304 return -1;
1305 }
1306
Deepa Dinamani19648b42013-09-05 17:05:55 -07001307 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1308 if (!table)
1309 return -1;
1310
1311 /* Read the entire device tree table into buffer */
1312 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1313 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1314 return -1;
1315 }
1316
1317
Joel Kingaa335dc2013-06-03 16:11:08 -07001318 /* Find index of device tree within device tree table */
1319 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001320 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1321 return -1;
1322 }
1323
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001324 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001325 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001326 {
1327 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1328 return -1;
1329 }
1330
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001331 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001332 if(flash_read(ptn, offset + dt_entry.offset,
1333 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001334 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1335 return -1;
1336 }
1337 }
1338#endif
1339
Shashank Mittalcd98d472011-08-02 14:29:24 -07001340 }
David Ng183a7422009-12-07 14:55:21 -08001341continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001342
Dima Zavin214cc642009-01-26 11:16:21 -08001343 /* TODO: create/pass atags to kernel */
1344
Ajay Dudanie28a6072011-07-01 13:59:46 -07001345 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001346 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001347 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1348
1349 return 0;
1350}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001351
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001352BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001353void write_device_info_mmc(device_info *dev)
1354{
1355 struct device_info *info = (void*) info_buf;
1356 unsigned long long ptn = 0;
1357 unsigned long long size;
1358 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001359 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001360 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001361
1362 index = partition_get_index("aboot");
1363 ptn = partition_get_offset(index);
1364 if(ptn == 0)
1365 {
1366 return;
1367 }
1368
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001369 lun = partition_get_lun(index);
1370 mmc_set_lun(lun);
1371
Shashank Mittal162244e2011-08-08 19:01:25 -07001372 size = partition_get_size(index);
1373
1374 memcpy(info, dev, sizeof(device_info));
1375
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001376 blocksize = mmc_get_device_blocksize();
1377
1378 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Shashank Mittal162244e2011-08-08 19:01:25 -07001379 {
1380 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1381 return;
1382 }
1383}
1384
1385void read_device_info_mmc(device_info *dev)
1386{
1387 struct device_info *info = (void*) info_buf;
1388 unsigned long long ptn = 0;
1389 unsigned long long size;
1390 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001391 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001392
1393 index = partition_get_index("aboot");
1394 ptn = partition_get_offset(index);
1395 if(ptn == 0)
1396 {
1397 return;
1398 }
1399
1400 size = partition_get_size(index);
1401
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001402 blocksize = mmc_get_device_blocksize();
1403
1404 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Shashank Mittal162244e2011-08-08 19:01:25 -07001405 {
1406 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1407 return;
1408 }
1409
1410 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1411 {
1412 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1413 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001414 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001415 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001416
1417 write_device_info_mmc(info);
1418 }
1419 memcpy(dev, info, sizeof(device_info));
1420}
1421
1422void write_device_info_flash(device_info *dev)
1423{
1424 struct device_info *info = (void *) info_buf;
1425 struct ptentry *ptn;
1426 struct ptable *ptable;
1427
1428 ptable = flash_get_ptable();
1429 if (ptable == NULL)
1430 {
1431 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1432 return;
1433 }
1434
1435 ptn = ptable_find(ptable, "devinfo");
1436 if (ptn == NULL)
1437 {
1438 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1439 return;
1440 }
1441
1442 memcpy(info, dev, sizeof(device_info));
1443
1444 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1445 {
1446 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1447 return;
1448 }
1449}
1450
1451void read_device_info_flash(device_info *dev)
1452{
1453 struct device_info *info = (void*) info_buf;
1454 struct ptentry *ptn;
1455 struct ptable *ptable;
1456
1457 ptable = flash_get_ptable();
1458 if (ptable == NULL)
1459 {
1460 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1461 return;
1462 }
1463
1464 ptn = ptable_find(ptable, "devinfo");
1465 if (ptn == NULL)
1466 {
1467 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1468 return;
1469 }
1470
1471 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1472 {
1473 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1474 return;
1475 }
1476
1477 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1478 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001479 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1480 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001481 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001482 write_device_info_flash(info);
1483 }
1484 memcpy(dev, info, sizeof(device_info));
1485}
1486
1487void write_device_info(device_info *dev)
1488{
1489 if(target_is_emmc_boot())
1490 {
1491 write_device_info_mmc(dev);
1492 }
1493 else
1494 {
1495 write_device_info_flash(dev);
1496 }
1497}
1498
1499void read_device_info(device_info *dev)
1500{
1501 if(target_is_emmc_boot())
1502 {
1503 read_device_info_mmc(dev);
1504 }
1505 else
1506 {
1507 read_device_info_flash(dev);
1508 }
1509}
1510
1511void reset_device_info()
1512{
1513 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001514 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001515 write_device_info(&device);
1516}
1517
1518void set_device_root()
1519{
1520 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001521 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001522 write_device_info(&device);
1523}
1524
Amol Jadicb524072012-08-09 16:40:18 -07001525#if DEVICE_TREE
1526int copy_dtb(uint8_t *boot_image_start)
1527{
1528 uint32 dt_image_offset = 0;
1529 uint32_t n;
1530 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001531 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001532 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001533
1534 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1535
Amol Jadicb524072012-08-09 16:40:18 -07001536 if(hdr->dt_size != 0) {
1537
1538 /* add kernel offset */
1539 dt_image_offset += page_size;
1540 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1541 dt_image_offset += n;
1542
1543 /* add ramdisk offset */
1544 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1545 dt_image_offset += n;
1546
1547 /* add second offset */
1548 if(hdr->second_size != 0) {
1549 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1550 dt_image_offset += n;
1551 }
1552
1553 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001554 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001555
Deepa Dinamani19648b42013-09-05 17:05:55 -07001556 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001557 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1558 return -1;
1559 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001560 /* Find index of device tree within device tree table */
1561 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001562 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1563 return -1;
1564 }
1565
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001566 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001567 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001568 {
1569 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1570 return -1;
1571 }
1572
Amol Jadicb524072012-08-09 16:40:18 -07001573 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001574 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001575 boot_image_start + dt_image_offset + dt_entry.offset,
1576 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001577 } else
1578 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001579
1580 /* Everything looks fine. Return success. */
1581 return 0;
1582}
1583#endif
1584
Brian Swetland9c4c0752009-01-25 16:23:50 -08001585void cmd_boot(const char *arg, void *data, unsigned sz)
1586{
1587 unsigned kernel_actual;
1588 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001589 uint32_t image_actual;
1590 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001591 uint32_t sig_actual = SIGNATURE_SIZE;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001592 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001593 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001594 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001595 int ret = 0;
1596 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001597
1598 if (sz < sizeof(hdr)) {
1599 fastboot_fail("invalid bootimage header");
1600 return;
1601 }
1602
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001603 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001604
1605 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001606 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001607
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001608 if(target_is_emmc_boot() && hdr->page_size) {
1609 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001610 page_mask = page_size - 1;
1611 }
1612
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001613 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1614 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001615#if DEVICE_TREE
1616 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1617#endif
1618
1619 image_actual = ADD_OF(page_size, kernel_actual);
1620 image_actual = ADD_OF(image_actual, ramdisk_actual);
1621 image_actual = ADD_OF(image_actual, dt_actual);
1622
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001623 if (target_use_signed_kernel() && (!device.is_unlocked))
1624 image_actual = ADD_OF(image_actual, sig_actual);
1625
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001626 /* sz should have atleast raw boot image */
1627 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001628 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001629 return;
1630 }
1631
1632 /* Verify the boot image
1633 * device & page_size are initialized in aboot_init
1634 */
1635 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001636 /* Pass size excluding signature size, otherwise we would try to
1637 * access signature beyond its length
1638 */
1639 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001640
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001641 /*
1642 * Update the kernel/ramdisk/tags address if the boot image header
1643 * has default values, these default values come from mkbootimg when
1644 * the boot image is flashed using fastboot flash:raw
1645 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001646 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1647 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001648
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001649 /* Get virtual addresses since the hdr saves physical addresses. */
1650 hdr->kernel_addr = VA(hdr->kernel_addr);
1651 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1652 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001653
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001654 /* Check if the addresses in the header are valid. */
1655 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1656 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1657 {
1658 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001659 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001660 }
1661
Amol Jadicb524072012-08-09 16:40:18 -07001662#if DEVICE_TREE
1663 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001664 ret = copy_dtb(data);
1665
1666 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001667#else
1668 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1669 {
1670 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001671 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001672 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001673#endif
1674
1675 /* Load ramdisk & kernel */
1676 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1677 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1678
1679#if DEVICE_TREE
1680 /*
1681 * If dtb is not found look for appended DTB in the kernel.
1682 * If appended dev tree is found, update the atags with
1683 * memory address to the DTB appended location on RAM.
1684 * Else update with the atags address in the kernel header
1685 */
1686 if (!dtb_copied) {
1687 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001688 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1689 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001690 if (!dtb) {
1691 fastboot_fail("dtb not found");
1692 return;
1693 }
Amol Jadicb524072012-08-09 16:40:18 -07001694 }
1695#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001696
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001697#ifndef DEVICE_TREE
1698 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1699 {
1700 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001701 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001702 }
1703#endif
1704
Brian Swetland9c4c0752009-01-25 16:23:50 -08001705 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001706 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001707
Dima Zavin77e41f32013-03-06 16:10:43 -08001708 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001709 (const char*) hdr->cmdline, board_machtype(),
1710 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001711}
1712
Dima Zavin214cc642009-01-26 11:16:21 -08001713void cmd_erase(const char *arg, void *data, unsigned sz)
1714{
1715 struct ptentry *ptn;
1716 struct ptable *ptable;
1717
1718 ptable = flash_get_ptable();
1719 if (ptable == NULL) {
1720 fastboot_fail("partition table doesn't exist");
1721 return;
1722 }
1723
1724 ptn = ptable_find(ptable, arg);
1725 if (ptn == NULL) {
1726 fastboot_fail("unknown partition name");
1727 return;
1728 }
1729
1730 if (flash_erase(ptn)) {
1731 fastboot_fail("failed to erase partition");
1732 return;
1733 }
1734 fastboot_okay("");
1735}
1736
Bikas Gurungd48bd242010-09-04 19:54:32 -07001737
1738void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1739{
Kun Liang2f1601a2013-08-12 16:29:54 +08001740 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001741 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001742 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001743 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001744 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001745
Kinson Chikf1a43512011-07-14 11:28:39 -07001746 index = partition_get_index(arg);
1747 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001748 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001749
Kinson Chikf1a43512011-07-14 11:28:39 -07001750 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001751 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001752 return;
1753 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001754
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001755 lun = partition_get_lun(index);
1756 mmc_set_lun(lun);
1757
Oliver Wangcee448d2013-10-22 18:40:13 +08001758#if MMC_SDHCI_SUPPORT
1759 if (mmc_erase_card(ptn, size)) {
1760 fastboot_fail("failed to erase partition\n");
1761 return;
1762 }
1763#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001764 size = partition_get_size(index);
1765 if (size > DEFAULT_ERASE_SIZE)
1766 size = DEFAULT_ERASE_SIZE;
1767
neetidb4b24d62012-01-20 12:13:09 -08001768 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001769 0 in first several blocks */
1770 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001771 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001772 return;
1773 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001774#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001775 fastboot_okay("");
1776}
1777
1778
Ajay Dudani5c761132011-04-07 20:19:04 -07001779void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001780{
1781 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001782 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001783 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001784 char *token = NULL;
1785 char *pname = NULL;
1786 uint8_t lun = 0;
1787 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001788
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001789 token = strtok(arg, ":");
1790 pname = token;
1791 token = strtok(NULL, ":");
1792 if(token)
1793 {
1794 lun = atoi(token);
1795 mmc_set_lun(lun);
1796 lun_set = true;
1797 }
1798
1799 if (!strcmp(pname, "partition"))
Greg Grisco6e754772011-06-23 12:19:39 -07001800 {
1801 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001802 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001803 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001804 return;
1805 }
1806 }
Greg Grisco6e754772011-06-23 12:19:39 -07001807 else
1808 {
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001809 index = partition_get_index(pname);
Kinson Chikf1a43512011-07-14 11:28:39 -07001810 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001811 if(ptn == 0) {
1812 fastboot_fail("partition table doesn't exist");
1813 return;
1814 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001815
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001816 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
Greg Grisco6e754772011-06-23 12:19:39 -07001817 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1818 fastboot_fail("image is not a boot image");
1819 return;
1820 }
1821 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001822
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001823 if(!lun_set)
1824 {
1825 lun = partition_get_lun(index);
1826 mmc_set_lun(lun);
1827 }
1828
Kinson Chikf1a43512011-07-14 11:28:39 -07001829 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001830 if (ROUND_TO_PAGE(sz,511) > size) {
1831 fastboot_fail("size too large");
1832 return;
1833 }
1834 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1835 fastboot_fail("flash write failure");
1836 return;
1837 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001838 }
1839 fastboot_okay("");
1840 return;
1841}
1842
Ajay Dudani5c761132011-04-07 20:19:04 -07001843void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1844{
1845 unsigned int chunk;
1846 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001847 uint32_t *fill_buf = NULL;
1848 uint32_t fill_val;
1849 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001850 sparse_header_t *sparse_header;
1851 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001852 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001853 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301854 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001855 int index = INVALID_PTN;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001856 int i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001857 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001858
Kinson Chikf1a43512011-07-14 11:28:39 -07001859 index = partition_get_index(arg);
1860 ptn = partition_get_offset(index);
1861 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001862 fastboot_fail("partition table doesn't exist");
1863 return;
1864 }
1865
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301866 size = partition_get_size(index);
1867 if (ROUND_TO_PAGE(sz,511) > size) {
1868 fastboot_fail("size too large");
1869 return;
1870 }
1871
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001872 lun = partition_get_lun(index);
1873 mmc_set_lun(lun);
1874
Ajay Dudani5c761132011-04-07 20:19:04 -07001875 /* Read and skip over sparse image header */
1876 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001877 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1878 fastboot_fail("size too large");
1879 return;
1880 }
1881
Ajay Dudani5c761132011-04-07 20:19:04 -07001882 data += sparse_header->file_hdr_sz;
1883 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1884 {
1885 /* Skip the remaining bytes in a header that is longer than
1886 * we expected.
1887 */
1888 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1889 }
1890
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001891 dprintf (SPEW, "=== Sparse Image Header ===\n");
1892 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1893 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1894 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1895 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1896 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1897 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1898 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1899 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001900
1901 /* Start processing chunks */
1902 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1903 {
1904 /* Read and skip over chunk header */
1905 chunk_header = (chunk_header_t *) data;
1906 data += sizeof(chunk_header_t);
1907
1908 dprintf (SPEW, "=== Chunk Header ===\n");
1909 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1910 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1911 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1912
1913 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1914 {
1915 /* Skip the remaining bytes in a header that is longer than
1916 * we expected.
1917 */
1918 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1919 }
1920
1921 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1922 switch (chunk_header->chunk_type)
1923 {
1924 case CHUNK_TYPE_RAW:
1925 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1926 chunk_data_sz))
1927 {
1928 fastboot_fail("Bogus chunk size for chunk type Raw");
1929 return;
1930 }
1931
Ajay Dudaniab18f022011-05-12 14:39:22 -07001932 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1933 chunk_data_sz,
1934 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001935 {
1936 fastboot_fail("flash write failure");
1937 return;
1938 }
1939 total_blocks += chunk_header->chunk_sz;
1940 data += chunk_data_sz;
1941 break;
1942
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001943 case CHUNK_TYPE_FILL:
1944 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1945 sizeof(uint32_t)))
1946 {
1947 fastboot_fail("Bogus chunk size for chunk type FILL");
1948 return;
1949 }
1950
1951 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
1952 if (!fill_buf)
1953 {
1954 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
1955 return;
1956 }
1957
1958 fill_val = *(uint32_t *)data;
1959 data = (char *) data + sizeof(uint32_t);
1960 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
1961
1962 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
1963 {
1964 fill_buf[i] = fill_val;
1965 }
1966
1967 for (i = 0; i < chunk_blk_cnt; i++)
1968 {
1969 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1970 sparse_header->blk_sz,
1971 fill_buf))
1972 {
1973 fastboot_fail("flash write failure");
1974 free(fill_buf);
1975 return;
1976 }
1977
1978 total_blocks++;
1979 }
1980
1981 free(fill_buf);
1982 break;
1983
Ajay Dudani5c761132011-04-07 20:19:04 -07001984 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001985 total_blocks += chunk_header->chunk_sz;
1986 break;
1987
Ajay Dudani5c761132011-04-07 20:19:04 -07001988 case CHUNK_TYPE_CRC:
1989 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1990 {
1991 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1992 return;
1993 }
1994 total_blocks += chunk_header->chunk_sz;
1995 data += chunk_data_sz;
1996 break;
1997
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001998 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001999 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002000 fastboot_fail("Unknown chunk type");
2001 return;
2002 }
2003 }
2004
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002005 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2006 total_blocks, sparse_header->total_blks);
2007
2008 if(total_blocks != sparse_header->total_blks)
2009 {
2010 fastboot_fail("sparse image write failure");
2011 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002012
2013 fastboot_okay("");
2014 return;
2015}
2016
2017void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2018{
2019 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002020 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2021 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002022
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002023#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002024 int ret=0;
2025 uint32 major_version=0;
2026 uint32 minor_version=0;
2027
2028 ret = scm_svc_version(&major_version,&minor_version);
2029 if(!ret)
2030 {
2031 if(major_version >= 2)
2032 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002033 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002034 {
2035 ret = encrypt_scm((uint32 **) &data, &sz);
2036 if (ret != 0) {
2037 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2038 return;
2039 }
2040
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002041 /* Protect only for SSD */
2042 if (!strcmp(arg, "ssd")) {
2043 ret = scm_protect_keystore((uint32 *) data, sz);
2044 if (ret != 0) {
2045 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2046 return;
2047 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002048 }
2049 }
2050 else
2051 {
2052 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2053 if(ret != 0)
2054 {
2055 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2056 return;
2057 }
2058 }
2059 }
2060 else
2061 {
2062 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2063 magic_number[1] == DECRYPT_MAGIC_1)
2064 {
2065 ret = decrypt_scm((uint32 **) &data, &sz);
2066 if (ret != 0) {
2067 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2068 return;
2069 }
2070 }
2071 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2072 magic_number[1] == ENCRYPT_MAGIC_1)
2073 {
2074 ret = encrypt_scm((uint32 **) &data, &sz);
2075 if (ret != 0) {
2076 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2077 return;
2078 }
2079 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002080 }
2081 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002082 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002083 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002084 dprintf(CRITICAL,"INVALID SVC Version\n");
2085 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002086 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002087#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002088
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002089 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002090 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
2091 cmd_flash_mmc_img(arg, data, sz);
2092 else
2093 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002094 return;
2095}
2096
Dima Zavin214cc642009-01-26 11:16:21 -08002097void cmd_flash(const char *arg, void *data, unsigned sz)
2098{
2099 struct ptentry *ptn;
2100 struct ptable *ptable;
2101 unsigned extra = 0;
2102
2103 ptable = flash_get_ptable();
2104 if (ptable == NULL) {
2105 fastboot_fail("partition table doesn't exist");
2106 return;
2107 }
2108
2109 ptn = ptable_find(ptable, arg);
2110 if (ptn == NULL) {
2111 fastboot_fail("unknown partition name");
2112 return;
2113 }
2114
2115 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2116 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2117 fastboot_fail("image is not a boot image");
2118 return;
2119 }
2120 }
2121
Amol Jadi5c61a952012-05-04 17:05:35 -07002122 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002123 || !strcmp(ptn->name, "userdata")
2124 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002125 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002126 || !strcmp(ptn->name, "modem"))
2127 {
2128 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07002129 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002130 else
2131 extra = 0;
2132 }
2133 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002134 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002135
2136 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2137 if (flash_write(ptn, extra, data, sz)) {
2138 fastboot_fail("flash write failure");
2139 return;
2140 }
2141 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2142 fastboot_okay("");
2143}
2144
2145void cmd_continue(const char *arg, void *data, unsigned sz)
2146{
2147 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002148 fastboot_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002149 if (target_is_emmc_boot())
2150 {
2151 boot_linux_from_mmc();
2152 }
2153 else
2154 {
2155 boot_linux_from_flash();
2156 }
Dima Zavin214cc642009-01-26 11:16:21 -08002157}
2158
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002159void cmd_reboot(const char *arg, void *data, unsigned sz)
2160{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002161 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002162 fastboot_okay("");
2163 reboot_device(0);
2164}
2165
2166void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2167{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002168 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002169 fastboot_okay("");
2170 reboot_device(FASTBOOT_MODE);
2171}
2172
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002173void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2174{
2175 dprintf(INFO, "Enabling charger screen check\n");
2176 device.charger_screen_enabled = 1;
2177 write_device_info(&device);
2178 fastboot_okay("");
2179}
2180
2181void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2182{
2183 dprintf(INFO, "Disabling charger screen check\n");
2184 device.charger_screen_enabled = 0;
2185 write_device_info(&device);
2186 fastboot_okay("");
2187}
2188
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302189void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2190{
2191 dprintf(INFO, "Selecting display panel %s\n", arg);
2192 if (arg)
2193 strlcpy(device.display_panel, arg,
2194 sizeof(device.display_panel));
2195 write_device_info(&device);
2196 fastboot_okay("");
2197}
2198
Shashank Mittal162244e2011-08-08 19:01:25 -07002199void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2200{
2201 if(!device.is_unlocked)
2202 {
2203 device.is_unlocked = 1;
2204 write_device_info(&device);
2205 }
2206 fastboot_okay("");
2207}
2208
Shashank Mittala0032282011-08-26 14:50:11 -07002209void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2210{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302211 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002212 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002213 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002214 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2215 fastboot_info(response);
2216 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002217 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302218 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2219 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002220 fastboot_okay("");
2221}
2222
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002223void cmd_preflash(const char *arg, void *data, unsigned sz)
2224{
2225 fastboot_okay("");
2226}
2227
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302228static struct fbimage logo_header = {0};
2229struct fbimage* splash_screen_flash();
2230
2231int splash_screen_check_header(struct fbimage *logo)
2232{
2233 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2234 return -1;
2235 if (logo->header.width == 0 || logo->header.height == 0)
2236 return -1;
2237 return 0;
2238}
2239
2240struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002241{
2242 struct ptentry *ptn;
2243 struct ptable *ptable;
2244 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302245 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002246
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302247
2248 ptable = flash_get_ptable();
2249 if (ptable == NULL) {
2250 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2251 return NULL;
2252 }
2253 ptn = ptable_find(ptable, "splash");
2254 if (ptn == NULL) {
2255 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2256 return NULL;
2257 }
2258
2259 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2260 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2261 return NULL;
2262 }
2263
2264 if (splash_screen_check_header(logo)) {
2265 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2266 return NULL;
2267 }
2268
2269 fb_display = fbcon_display();
2270 if (fb_display) {
2271 uint8_t *base = (uint8_t *) fb_display->base;
2272 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
2273 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002274 }
2275
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302276 if (flash_read(ptn + sizeof(logo->header), 0,
2277 base,
2278 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2279 fbcon_clear();
2280 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2281 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002282 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302283 logo->image = base;
2284 }
2285
2286 return logo;
2287}
2288
2289struct fbimage* splash_screen_mmc()
2290{
2291 int index = INVALID_PTN;
2292 unsigned long long ptn = 0;
2293 struct fbcon_config *fb_display = NULL;
2294 struct fbimage *logo = &logo_header;
2295
2296 index = partition_get_index("splash");
2297 if (index == 0) {
2298 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2299 return NULL;
2300 }
2301
2302 ptn = partition_get_offset(index);
2303 if (ptn == 0) {
2304 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2305 return NULL;
2306 }
2307
2308 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2309 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2310 return NULL;
2311 }
2312
2313 if (splash_screen_check_header(logo)) {
2314 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2315 return NULL;
2316 }
2317
2318 fb_display = fbcon_display();
2319 if (fb_display) {
2320 uint8_t *base = (uint8_t *) fb_display->base;
2321 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2322 base += LOGO_IMG_OFFSET;
2323
2324 if (mmc_read(ptn + sizeof(logo->header),
2325 base,
2326 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2327 fbcon_clear();
2328 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2329 return NULL;
2330 }
2331
2332 logo->image = base;
2333 }
2334
2335 return logo;
2336}
2337
2338
2339struct fbimage* fetch_image_from_partition()
2340{
2341 if (target_is_emmc_boot()) {
2342 return splash_screen_mmc();
2343 } else {
2344 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002345 }
2346}
2347
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002348/* Get the size from partiton name */
2349static void get_partition_size(const char *arg, char *response)
2350{
2351 uint64_t ptn = 0;
2352 uint64_t size;
2353 int index = INVALID_PTN;
2354
2355 index = partition_get_index(arg);
2356
2357 if (index == INVALID_PTN)
2358 {
2359 dprintf(CRITICAL, "Invalid partition index\n");
2360 return;
2361 }
2362
2363 ptn = partition_get_offset(index);
2364
2365 if(!ptn)
2366 {
2367 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2368 return;
2369 }
2370
2371 size = partition_get_size(index);
2372
2373 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2374 return;
2375}
2376
2377/*
2378 * Publish the partition type & size info
2379 * fastboot getvar will publish the required information.
2380 * fastboot getvar partition_size:<partition_name>: partition size in hex
2381 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2382 */
2383static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2384{
2385 uint8_t i;
2386
2387 for (i = 0; i < num_parts; i++) {
2388 get_partition_size(info[i].part_name, info[i].size_response);
2389
2390 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2391 {
2392 dprintf(CRITICAL, "partition size name truncated\n");
2393 return;
2394 }
2395 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2396 {
2397 dprintf(CRITICAL, "partition type name truncated\n");
2398 return;
2399 }
2400
2401 /* publish partition size & type info */
2402 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2403 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2404 }
2405}
2406
Amol Jadi5edf3552013-07-23 14:15:34 -07002407/* register commands and variables for fastboot */
2408void aboot_fastboot_register_commands(void)
2409{
2410 if (target_is_emmc_boot())
2411 {
2412 fastboot_register("flash:", cmd_flash_mmc);
2413 fastboot_register("erase:", cmd_erase_mmc);
2414 }
2415 else
2416 {
2417 fastboot_register("flash:", cmd_flash);
2418 fastboot_register("erase:", cmd_erase);
2419 }
2420
2421 fastboot_register("boot", cmd_boot);
2422 fastboot_register("continue", cmd_continue);
2423 fastboot_register("reboot", cmd_reboot);
2424 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2425 fastboot_register("oem unlock", cmd_oem_unlock);
2426 fastboot_register("oem device-info", cmd_oem_devinfo);
2427 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002428 fastboot_register("oem enable-charger-screen",
2429 cmd_oem_enable_charger_screen);
2430 fastboot_register("oem disable-charger-screen",
2431 cmd_oem_disable_charger_screen);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302432 fastboot_register("oem select-display-panel",
2433 cmd_oem_select_display_panel);
Amol Jadi5edf3552013-07-23 14:15:34 -07002434 /* publish variables and their values */
2435 fastboot_publish("product", TARGET(BOARD));
2436 fastboot_publish("kernel", "lk");
2437 fastboot_publish("serialno", sn_buf);
2438
2439 /*
2440 * partition info is supported only for emmc partitions
2441 * Calling this for NAND prints some error messages which
2442 * is harmless but misleading. Avoid calling this for NAND
2443 * devices.
2444 */
2445 if (target_is_emmc_boot())
2446 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2447
2448 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002449 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2450 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002451 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002452 /* Is the charger screen check enabled */
2453 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2454 device.charger_screen_enabled);
2455 fastboot_publish("charger-screen-enabled",
2456 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302457 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2458 device.display_panel);
2459 fastboot_publish("display-panel",
2460 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002461}
2462
Brian Swetland9c4c0752009-01-25 16:23:50 -08002463void aboot_init(const struct app_descriptor *app)
2464{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002465 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002466 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002467
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002468 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002469 if (target_is_emmc_boot())
2470 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002471 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002472 page_mask = page_size - 1;
2473 }
2474 else
2475 {
2476 page_size = flash_page_size();
2477 page_mask = page_size - 1;
2478 }
2479
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002480 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2481
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002482 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002483
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002484 /* Display splash screen if enabled */
2485#if DISPLAY_SPLASH_SCREEN
2486 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302487 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002488 dprintf(SPEW, "Display Init: Done\n");
2489#endif
2490
2491
Greg Griscod6250552011-06-29 14:40:23 -07002492 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002493 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002494
Dhaval Patel223ec952013-07-18 14:49:44 -07002495 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2496
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002497 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002498 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002499 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002500 dprintf(ALWAYS,"dload mode key sequence detected\n");
2501 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002502 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002503 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002504 }
2505 else
2506 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302507 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002508 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002509 }
2510 boot_into_fastboot = true;
2511 }
2512 if (!boot_into_fastboot)
2513 {
2514 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2515 boot_into_recovery = 1;
2516 if (!boot_into_recovery &&
2517 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002518 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002519 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002520 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002521 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002522 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002523 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002524
Ajay Dudani77421292010-10-27 19:34:06 -07002525 reboot_mode = check_reboot_mode();
2526 if (reboot_mode == RECOVERY_MODE) {
2527 boot_into_recovery = 1;
2528 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002529 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002530 }
2531
Pavel Nedev5d91d412013-04-29 11:34:24 +03002532 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002533 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002534 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002535 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002536 if(emmc_recovery_init())
2537 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2538 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002539 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002540 if((device.is_unlocked) || (device.is_tampered))
2541 {
2542 #ifdef TZ_TAMPER_FUSE
2543 set_tamper_fuse_cmd();
2544 #endif
2545 #if USE_PCOM_SECBOOT
2546 set_tamper_flag(device.is_tampered);
2547 #endif
2548 }
Shashank Mittala0032282011-08-26 14:50:11 -07002549 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002550 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002551 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002552 else
2553 {
2554 recovery_init();
2555 #if USE_PCOM_SECBOOT
2556 if((device.is_unlocked) || (device.is_tampered))
2557 set_tamper_flag(device.is_tampered);
2558 #endif
2559 boot_linux_from_flash();
2560 }
2561 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2562 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002563 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002564
Amol Jadi5edf3552013-07-23 14:15:34 -07002565 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002566
Amol Jadi5edf3552013-07-23 14:15:34 -07002567 /* register aboot specific fastboot commands */
2568 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002569
Amol Jadi5edf3552013-07-23 14:15:34 -07002570 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002571 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002572
2573 /* initialize and start fastboot */
2574 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002575}
2576
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002577uint32_t get_page_size()
2578{
2579 return page_size;
2580}
2581
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002582/*
2583 * Calculated and save hash (SHA256) for non-signed boot image.
2584 *
2585 * Hash the same data that is checked on the signed boot image.
2586 * Kernel and Ramdisk are already read to memory buffers.
2587 * Need to read the entire device-tree from mmc
2588 * since non-signed image only read the DT tags of the relevant platform.
2589 *
2590 * @param kernel_addr - kernel bufer
2591 * @param kernel_actual - kernel size in bytes
2592 * @param ramdisk_addr - ramdisk buffer
2593 * @param ramdisk_actual - ramdisk size
2594 * @param ptn - partition
2595 * @param dt_offset - device tree offset on mmc partition
2596 * @param dt_size
2597 *
2598 * @return int - 0 on success, negative value on failure.
2599 */
2600int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2601 void *ramdisk_addr, unsigned ramdisk_actual,
2602 unsigned long long ptn,
2603 unsigned dt_offset, unsigned dt_size)
2604{
2605 SHA256_CTX sha256_ctx;
2606 char digest[32]={0};
2607 char *buf = (char *)target_get_scratch_address();
2608 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2609 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2610
2611 SHA256_Init(&sha256_ctx);
2612
2613 /* Read Boot Header */
2614 if (mmc_read(ptn, buf, page_size))
2615 {
2616 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2617 return -1;
2618 }
2619 /* Read entire Device Tree */
2620 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2621 {
2622 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2623 return -1;
2624 }
2625 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2626 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2627 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2628 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2629
2630 SHA256_Final(digest, &sha256_ctx);
2631
2632 save_kernel_hash_cmd(digest);
2633 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2634
2635 return 0;
2636}
2637
Brian Swetland9c4c0752009-01-25 16:23:50 -08002638APP_START(aboot)
2639 .init = aboot_init,
2640APP_END