blob: 24025a3c3b27044e6dc4c7a9bf196e5126f029a4 [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
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700315 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
316 target_display_panel_node(device.display_panel,
Ajay Singh Parmar430bbb32014-03-18 15:26:30 -0700317 display_panel_buf, MAX_PANEL_BUF_SIZE) &&
318 strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700319 cmdline_len += strlen(display_panel_buf);
320 }
321
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800322 if (target_warm_boot()) {
323 warm_boot = true;
324 cmdline_len += strlen(warmboot_cmdline);
325 }
326
David Ng183a7422009-12-07 14:55:21 -0800327 if (cmdline_len > 0) {
328 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700329 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
330 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700331
Amol Jadi168b7712012-03-06 16:15:00 -0800332 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700333 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800334 if (have_cmdline) {
335 src = cmdline;
336 while ((*dst++ = *src++));
337 }
338 if (target_is_emmc_boot()) {
339 src = emmc_cmdline;
340 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700341 have_cmdline = 1;
342 while ((*dst++ = *src++));
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700343#if UFS_SUPPORT
344 src = boot_dev_buf;
345 if (have_cmdline) --dst;
346 while ((*dst++ = *src++));
347#endif
David Ngf773dde2010-07-26 19:55:08 -0700348 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800349
350 src = usb_sn_cmdline;
351 if (have_cmdline) --dst;
352 have_cmdline = 1;
353 while ((*dst++ = *src++));
354 src = sn_buf;
355 if (have_cmdline) --dst;
356 have_cmdline = 1;
357 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800358 if (warm_boot) {
359 if (have_cmdline) --dst;
360 src = warmboot_cmdline;
361 while ((*dst++ = *src++));
362 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800363
Pavel Nedev5614d222013-06-17 18:01:02 +0300364 if (boot_into_recovery && gpt_exists) {
365 src = secondary_gpt_enable;
366 if (have_cmdline) --dst;
367 while ((*dst++ = *src++));
368 }
369
Pavel Nedev328ac822013-04-05 15:25:11 +0300370 if (boot_into_ffbm) {
371 src = androidboot_mode;
372 if (have_cmdline) --dst;
373 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700374 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300375 if (have_cmdline) --dst;
376 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800377 src = loglevel;
378 if (have_cmdline) --dst;
379 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300380 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700381 src = battchg_pause;
382 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800383 while ((*dst++ = *src++));
384 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800385
Shashank Mittalcd98d472011-08-02 14:29:24 -0700386 if(target_use_signed_kernel() && auth_kernel_img) {
387 src = auth_kernel;
388 if (have_cmdline) --dst;
389 while ((*dst++ = *src++));
390 }
391
Ajay Dudanid04110c2011-01-17 23:55:07 -0800392 switch(target_baseband())
393 {
394 case BASEBAND_APQ:
395 src = baseband_apq;
396 if (have_cmdline) --dst;
397 while ((*dst++ = *src++));
398 break;
399
400 case BASEBAND_MSM:
401 src = baseband_msm;
402 if (have_cmdline) --dst;
403 while ((*dst++ = *src++));
404 break;
405
406 case BASEBAND_CSFB:
407 src = baseband_csfb;
408 if (have_cmdline) --dst;
409 while ((*dst++ = *src++));
410 break;
411
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800412 case BASEBAND_SVLTE2A:
413 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800414 if (have_cmdline) --dst;
415 while ((*dst++ = *src++));
416 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700417
418 case BASEBAND_MDM:
419 src = baseband_mdm;
420 if (have_cmdline) --dst;
421 while ((*dst++ = *src++));
422 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700423
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800424 case BASEBAND_MDM2:
425 src = baseband_mdm2;
426 if (have_cmdline) --dst;
427 while ((*dst++ = *src++));
428 break;
429
Amol Jadi5c61a952012-05-04 17:05:35 -0700430 case BASEBAND_SGLTE:
431 src = baseband_sglte;
432 if (have_cmdline) --dst;
433 while ((*dst++ = *src++));
434 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530435
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800436 case BASEBAND_SGLTE2:
437 src = baseband_sglte2;
438 if (have_cmdline) --dst;
439 while ((*dst++ = *src++));
440 break;
441
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530442 case BASEBAND_DSDA:
443 src = baseband_dsda;
444 if (have_cmdline) --dst;
445 while ((*dst++ = *src++));
446 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800447
448 case BASEBAND_DSDA2:
449 src = baseband_dsda2;
450 if (have_cmdline) --dst;
451 while ((*dst++ = *src++));
452 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800453 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700454
455 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700456 src = display_panel_buf;
457 if (have_cmdline) --dst;
458 while ((*dst++ = *src++));
459 }
Joonwoo Park61112782013-10-02 19:50:39 -0700460
461 if (have_target_boot_params) {
462 if (have_cmdline) --dst;
463 src = target_boot_params;
464 while ((*dst++ = *src++));
465 }
Neeti Desaie245d492012-06-01 12:52:13 -0700466 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700467
468
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700469 if (boot_dev_buf)
470 free(boot_dev_buf);
471
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800472 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700473 return cmdline_final;
474}
475
476unsigned *atag_core(unsigned *ptr)
477{
478 /* CORE */
479 *ptr++ = 2;
480 *ptr++ = 0x54410001;
481
482 return ptr;
483
484}
485
486unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
487 unsigned ramdisk_size)
488{
489 if (ramdisk_size) {
490 *ptr++ = 4;
491 *ptr++ = 0x54420005;
492 *ptr++ = (unsigned)ramdisk;
493 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800494 }
495
Neeti Desaie245d492012-06-01 12:52:13 -0700496 return ptr;
497}
498
499unsigned *atag_ptable(unsigned **ptr_addr)
500{
501 int i;
502 struct ptable *ptable;
503
504 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700505 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
506 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700507 *(*ptr_addr)++ = 0x4d534d70;
508 for (i = 0; i < ptable->count; ++i)
509 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
510 }
511
512 return (*ptr_addr);
513}
514
515unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
516{
517 int cmdline_length = 0;
518 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700519 char *dest;
520
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800521 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700522 n = (cmdline_length + 4) & (~3);
523
524 *ptr++ = (n / 4) + 2;
525 *ptr++ = 0x54410009;
526 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800527 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700528 ptr += (n / 4);
529
530 return ptr;
531}
532
533unsigned *atag_end(unsigned *ptr)
534{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800535 /* END */
536 *ptr++ = 0;
537 *ptr++ = 0;
538
Neeti Desaie245d492012-06-01 12:52:13 -0700539 return ptr;
540}
541
542void generate_atags(unsigned *ptr, const char *cmdline,
543 void *ramdisk, unsigned ramdisk_size)
544{
545
546 ptr = atag_core(ptr);
547 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
548 ptr = target_atag_mem(ptr);
549
550 /* Skip NAND partition ATAGS for eMMC boot */
551 if (!target_is_emmc_boot()){
552 ptr = atag_ptable(&ptr);
553 }
554
555 ptr = atag_cmdline(ptr, cmdline);
556 ptr = atag_end(ptr);
557}
558
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700559typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700560void boot_linux(void *kernel, unsigned *tags,
561 const char *cmdline, unsigned machtype,
562 void *ramdisk, unsigned ramdisk_size)
563{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800564 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800565#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700566 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800567#endif
568
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700569 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800570 uint32_t tags_phys = PA((addr_t)tags);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800571 struct kernel64_hdr *kptr = (struct kernel64_hdr*)kernel;
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800572
573 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700574
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800575 final_cmdline = update_cmdline((const char*)cmdline);
576
Neeti Desai17379b82012-06-04 18:42:53 -0700577#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800578 dprintf(INFO, "Updating device tree: start\n");
579
Neeti Desai17379b82012-06-04 18:42:53 -0700580 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800581 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700582 if(ret)
583 {
584 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
585 ASSERT(0);
586 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800587 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700588#else
Neeti Desaie245d492012-06-01 12:52:13 -0700589 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800590 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700591#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700592
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700593 /* Perform target specific cleanup */
594 target_uninit();
595
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800596 /* Turn off splash screen if enabled */
597#if DISPLAY_SPLASH_SCREEN
598 target_display_shutdown();
599#endif
600
601
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800602 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
603 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800604
605 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700606
Amol Jadi4421e652011-06-16 15:00:48 -0700607 /* do any platform specific cleanup before kernel entry */
608 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700609
Brian Swetland9c4c0752009-01-25 16:23:50 -0800610 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700611
Amol Jadi504f9fe2012-08-16 13:56:48 -0700612#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800613 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700614#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700615 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800616
617 if (IS_ARM64(kptr))
618 /* Jump to a 64bit kernel */
619 scm_elexec_call((paddr_t)kernel, tags_phys);
620 else
621 /* Jump to a 32bit kernel */
622 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800623}
624
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700625/* Function to check if the memory address range falls within the aboot
626 * boundaries.
627 * start: Start of the memory region
628 * size: Size of the memory region
629 */
630int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
631{
632 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800633 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700634 return -1;
635
636 /* Check for memory overlap. */
637 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
638 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700639 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700640 return 0;
641 else
642 return -1;
643}
644
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800645#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800646
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800647BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800648#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800649BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800650#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800651
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700652static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
653{
654 int ret;
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800655#if IMAGE_VERIF_ALGO_SHA1
656 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
657#else
658 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
659#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700660
661 /* Assume device is rooted at this time. */
662 device.is_tampered = 1;
663
664 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
665
666 ret = image_verify((unsigned char *)bootimg_addr,
667 (unsigned char *)(bootimg_addr + bootimg_size),
668 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800669 auth_algo);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700670
671 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
672
673 if (ret)
674 {
675 /* Authorized kernel */
676 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700677 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700678 }
679
680#if USE_PCOM_SECBOOT
681 set_tamper_flag(device.is_tampered);
682#endif
683
684 if(device.is_tampered)
685 {
686 write_device_info_mmc(&device);
687 #ifdef TZ_TAMPER_FUSE
688 set_tamper_fuse_cmd();
689 #endif
690 #ifdef ASSERT_ON_TAMPER
691 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
692 ASSERT(0);
693 #endif
694 }
695}
696
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530697static bool check_format_bit()
698{
699 bool ret = false;
700 int index;
701 uint64_t offset;
702 struct boot_selection_info *in = NULL;
703 char *buf = NULL;
704
705 index = partition_get_index("bootselect");
706 if (index == INVALID_PTN)
707 {
708 dprintf(INFO, "Unable to locate /bootselect partition\n");
709 return ret;
710 }
711 offset = partition_get_offset(index);
712 if(!offset)
713 {
714 dprintf(INFO, "partition /bootselect doesn't exist\n");
715 return ret;
716 }
717 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
718 ASSERT(buf);
719 if (mmc_read(offset, (unsigned int *)buf, page_size))
720 {
721 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
722 free(buf);
723 return ret;
724 }
725 in = (struct boot_selection_info *) buf;
726 if ((in->signature == BOOTSELECT_SIGNATURE) &&
727 (in->version == BOOTSELECT_VERSION)) {
728 if ((in->state_info & BOOTSELECT_FORMAT) &&
729 !(in->state_info & BOOTSELECT_FACTORY))
730 ret = true;
731 } else {
732 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
733 in->signature, in->version);
734 ASSERT(0);
735 }
736 free(buf);
737 return ret;
738}
739
Shashank Mittal23b8f422010-04-16 19:27:21 -0700740int boot_linux_from_mmc(void)
741{
742 struct boot_img_hdr *hdr = (void*) buf;
743 struct boot_img_hdr *uhdr;
744 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700745 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700746 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700747 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700748
Shashank Mittalcd98d472011-08-02 14:29:24 -0700749 unsigned char *image_addr = 0;
750 unsigned kernel_actual;
751 unsigned ramdisk_actual;
752 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700753 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700754
755#if DEVICE_TREE
756 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700757 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700758 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800759 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700760 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700761#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800762 BUF_DMA_ALIGN(kbuf, BOOT_IMG_MAX_PAGE_SIZE);
763 struct kernel64_hdr *kptr = (void*) kbuf;
764
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530765 if (check_format_bit())
766 boot_into_recovery = 1;
767
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700768 if (!boot_into_recovery) {
769 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
770 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
771 if (rcode <= 0) {
772 boot_into_ffbm = false;
773 if (rcode < 0)
774 dprintf(CRITICAL,"failed to get ffbm cookie");
775 } else
776 boot_into_ffbm = true;
777 } else
778 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700779 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
780 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
781 dprintf(INFO, "Unified boot method!\n");
782 hdr = uhdr;
783 goto unified_boot;
784 }
Greg Griscod6250552011-06-29 14:40:23 -0700785 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700786 index = partition_get_index("boot");
787 ptn = partition_get_offset(index);
788 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700789 dprintf(CRITICAL, "ERROR: No boot partition found\n");
790 return -1;
791 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700792 }
793 else {
794 index = partition_get_index("recovery");
795 ptn = partition_get_offset(index);
796 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700797 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
798 return -1;
799 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700800 }
801
Greg Griscod6250552011-06-29 14:40:23 -0700802 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700803 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
804 return -1;
805 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700806
807 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700808 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700809 return -1;
810 }
811
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700812 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +0530813
814 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
815 dprintf(CRITICAL, "ERROR: Invalid page size\n");
816 return -1;
817 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700818 page_size = hdr->page_size;
819 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700820 }
821
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800822 /* Read the next page to get kernel Image header
823 * which lives in the second page for arm64 targets.
824 */
825
826 if (mmc_read(ptn + page_size, (unsigned int *) kbuf, page_size)) {
827 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
828 return -1;
829 }
830
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700831 /*
832 * Update the kernel/ramdisk/tags address if the boot image header
833 * has default values, these default values come from mkbootimg when
834 * the boot image is flashed using fastboot flash:raw
835 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800836 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700837
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700838 /* Get virtual addresses since the hdr saves physical addresses. */
839 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
840 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
841 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700842
843 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
844 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
845
846 /* Check if the addresses in the header are valid. */
847 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
848 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
849 {
850 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
851 return -1;
852 }
853
854#ifndef DEVICE_TREE
855 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
856 {
857 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
858 return -1;
859 }
860#endif
861
Shashank Mittalcd98d472011-08-02 14:29:24 -0700862 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300863 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
864 (int) target_use_signed_kernel(),
865 device.is_unlocked,
866 device.is_tampered);
867
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700868 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800869 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700870 offset = 0;
871
Shashank Mittalcd98d472011-08-02 14:29:24 -0700872 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700873
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800874#if DEVICE_TREE
875 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
876 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700877
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700878 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700879 {
880 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
881 return -1;
882 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800883#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530884 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700885
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700886#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700887
Amol Jadib6be5c12012-11-14 13:39:51 -0800888 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700889 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800890
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700891 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
892 {
893 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
894 return -1;
895 }
896
Shashank Mittalcd98d472011-08-02 14:29:24 -0700897 /* Read image without signature */
898 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
899 {
900 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
901 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800902 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700903
Amol Jadib6be5c12012-11-14 13:39:51 -0800904 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700905 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800906
Shashank Mittalcd98d472011-08-02 14:29:24 -0700907 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700908
909 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
910 {
911 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
912 return -1;
913 }
914
Shashank Mittalcd98d472011-08-02 14:29:24 -0700915 /* Read signature */
916 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
917 {
918 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700919 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700920 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800921
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700922 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700923
Neeti Desai465491e2012-07-31 12:53:35 -0700924 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700925 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
926 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700927
Neeti Desai465491e2012-07-31 12:53:35 -0700928 #if DEVICE_TREE
929 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700930 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700931 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700932
Deepa Dinamani19648b42013-09-05 17:05:55 -0700933 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700934 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
935 return -1;
936 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700937
Neeti Desai465491e2012-07-31 12:53:35 -0700938 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700939 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700940 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
941 return -1;
942 }
943
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700944 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700945 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700946 {
947 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
948 return -1;
949 }
950
Joel Kingaa335dc2013-06-03 16:11:08 -0700951 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800952 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800953 /*
954 * If appended dev tree is found, update the atags with
955 * memory address to the DTB appended location on RAM.
956 * Else update with the atags address in the kernel header
957 */
958 void *dtb;
959 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700960 hdr->kernel_size,
961 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800962 if (!dtb) {
963 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
964 return -1;
965 }
Neeti Desai465491e2012-07-31 12:53:35 -0700966 }
967 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700968 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700969 else
970 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800971 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700972
Amol Jadib6be5c12012-11-14 13:39:51 -0800973 dprintf(INFO, "Loading boot image (%d): start\n",
974 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700975 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800976
977 offset = page_size;
978
979 /* Load kernel */
980 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700981 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
982 return -1;
983 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800984 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700985
Amol Jadib6be5c12012-11-14 13:39:51 -0800986 /* Load ramdisk */
987 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700988 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800989 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700990 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
991 return -1;
992 }
993 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800994 offset += ramdisk_actual;
995
996 dprintf(INFO, "Loading boot image (%d): done\n",
997 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700998 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700999
1000 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001001 offset += second_actual;
1002 /* Second image loading not implemented. */
1003 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -07001004 }
1005
1006 #if DEVICE_TREE
1007 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -07001008 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -07001009 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
1010 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1011 return -1;
1012 }
1013 table = (struct dt_table*) dt_buf;
1014
Deepa Dinamani19648b42013-09-05 17:05:55 -07001015 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -07001016 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1017 return -1;
1018 }
1019
Deepa Dinamani19648b42013-09-05 17:05:55 -07001020 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1021 if (!table)
1022 return -1;
1023
1024 /* Read the entire device tree table into buffer */
1025 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
1026 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1027 return -1;
1028 }
1029
Joel Kingaa335dc2013-06-03 16:11:08 -07001030 /* Find index of device tree within device tree table */
1031 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -07001032 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1033 return -1;
1034 }
1035
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001036 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001037 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001038 {
1039 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1040 return -1;
1041 }
1042
Joel Kingaa335dc2013-06-03 16:11:08 -07001043 if(mmc_read(ptn + offset + dt_entry.offset,
1044 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -07001045 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1046 return -1;
1047 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +03001048 #ifdef TZ_SAVE_KERNEL_HASH
1049 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
1050 hdr->ramdisk_addr, ramdisk_actual,
1051 ptn, offset, hdr->dt_size);
1052 #endif /* TZ_SAVE_KERNEL_HASH */
1053
Channagoud Kadabi35095622013-03-01 13:53:05 -08001054 } else {
Aparna Mallavarapu33127372014-06-03 22:15:54 +05301055
1056 /* Validate the tags_addr */
1057 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
1058 {
1059 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1060 return -1;
1061 }
Dima Zavin77e41f32013-03-06 16:10:43 -08001062 /*
1063 * If appended dev tree is found, update the atags with
1064 * memory address to the DTB appended location on RAM.
1065 * Else update with the atags address in the kernel header
1066 */
1067 void *dtb;
1068 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -07001069 kernel_actual,
1070 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -08001071 if (!dtb) {
1072 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1073 return -1;
1074 }
Neeti Desai465491e2012-07-31 12:53:35 -07001075 }
1076 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001077 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001078
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001079 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1080 target_load_ssd_keystore();
1081
Shashank Mittal23b8f422010-04-16 19:27:21 -07001082unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001083
Dima Zavin77e41f32013-03-06 16:10:43 -08001084 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001085 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001086 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1087
1088 return 0;
1089}
1090
Dima Zavin214cc642009-01-26 11:16:21 -08001091int boot_linux_from_flash(void)
1092{
1093 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001094 struct ptentry *ptn;
1095 struct ptable *ptable;
1096 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001097
Shashank Mittalcd98d472011-08-02 14:29:24 -07001098 unsigned char *image_addr = 0;
1099 unsigned kernel_actual;
1100 unsigned ramdisk_actual;
1101 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001102 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001103
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001104#if DEVICE_TREE
1105 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001106 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001107 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001108 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001109#endif
1110
David Ng183a7422009-12-07 14:55:21 -08001111 if (target_is_emmc_boot()) {
1112 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1113 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1114 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1115 return -1;
1116 }
1117 goto continue_boot;
1118 }
1119
Dima Zavin214cc642009-01-26 11:16:21 -08001120 ptable = flash_get_ptable();
1121 if (ptable == NULL) {
1122 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1123 return -1;
1124 }
1125
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001126 if(!boot_into_recovery)
1127 {
1128 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001129
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001130 if (ptn == NULL) {
1131 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1132 return -1;
1133 }
1134 }
1135 else
1136 {
1137 ptn = ptable_find(ptable, "recovery");
1138 if (ptn == NULL) {
1139 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1140 return -1;
1141 }
Dima Zavin214cc642009-01-26 11:16:21 -08001142 }
1143
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001144 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001145 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1146 return -1;
1147 }
Dima Zavin214cc642009-01-26 11:16:21 -08001148
1149 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001150 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001151 return -1;
1152 }
1153
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001154 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001155 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 -08001156 return -1;
1157 }
1158
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001159 /*
1160 * Update the kernel/ramdisk/tags address if the boot image header
1161 * has default values, these default values come from mkbootimg when
1162 * the boot image is flashed using fastboot flash:raw
1163 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001164 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001165
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001166 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001167 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1168 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1169 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1170
1171 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1172 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1173
1174 /* Check if the addresses in the header are valid. */
1175 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1176 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1177 {
1178 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1179 return -1;
1180 }
1181
1182#ifndef DEVICE_TREE
1183 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1184 {
1185 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1186 return -1;
1187 }
1188#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001189
Shashank Mittalcd98d472011-08-02 14:29:24 -07001190 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001191 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001192 {
1193 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001194 offset = 0;
1195
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001196#if DEVICE_TREE
1197 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1198 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001199
1200 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1201 {
1202 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1203 return -1;
1204 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001205#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001206 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001207#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001208
Amol Jadib6be5c12012-11-14 13:39:51 -08001209 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001210 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001211
Shashank Mittalcd98d472011-08-02 14:29:24 -07001212 /* Read image without signature */
1213 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1214 {
1215 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1216 return -1;
1217 }
Dima Zavin214cc642009-01-26 11:16:21 -08001218
Amol Jadib6be5c12012-11-14 13:39:51 -08001219 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001220 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001221
Shashank Mittalcd98d472011-08-02 14:29:24 -07001222 offset = imagesize_actual;
1223 /* Read signature */
1224 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1225 {
1226 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001227 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001228 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001229
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001230 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001231
1232 /* Move kernel and ramdisk to correct address */
1233 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1234 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001235#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001236 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001237 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001238 {
1239 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1240 return -1;
1241 }
1242
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001243 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1244#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001245
1246 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001247 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001248 {
1249 write_device_info_flash(&device);
1250 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301251#if USE_PCOM_SECBOOT
1252 set_tamper_flag(device.is_tampered);
1253#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001254 }
1255 else
1256 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001257 offset = page_size;
1258
Amol Jadib6be5c12012-11-14 13:39:51 -08001259 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1260 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1261 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1262
1263 dprintf(INFO, "Loading boot image (%d): start\n",
1264 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001265 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001266
1267 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001268 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1269 return -1;
1270 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001271 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001272
Amol Jadib6be5c12012-11-14 13:39:51 -08001273 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001274 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1275 return -1;
1276 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001277 offset += ramdisk_actual;
1278
1279 dprintf(INFO, "Loading boot image (%d): done\n",
1280 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001281 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001282
1283 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001284 offset += second_actual;
1285 /* Second image loading not implemented. */
1286 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001287 }
1288
1289#if DEVICE_TREE
1290 if(hdr->dt_size != 0) {
1291
1292 /* Read the device tree table into buffer */
1293 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1294 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1295 return -1;
1296 }
1297
1298 table = (struct dt_table*) dt_buf;
1299
Deepa Dinamani19648b42013-09-05 17:05:55 -07001300 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001301 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1302 return -1;
1303 }
1304
Deepa Dinamani19648b42013-09-05 17:05:55 -07001305 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1306 if (!table)
1307 return -1;
1308
1309 /* Read the entire device tree table into buffer */
1310 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1311 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1312 return -1;
1313 }
1314
1315
Joel Kingaa335dc2013-06-03 16:11:08 -07001316 /* Find index of device tree within device tree table */
1317 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001318 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1319 return -1;
1320 }
1321
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001322 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001323 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001324 {
1325 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1326 return -1;
1327 }
1328
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001329 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001330 if(flash_read(ptn, offset + dt_entry.offset,
1331 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001332 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1333 return -1;
1334 }
1335 }
1336#endif
1337
Shashank Mittalcd98d472011-08-02 14:29:24 -07001338 }
David Ng183a7422009-12-07 14:55:21 -08001339continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001340
Dima Zavin214cc642009-01-26 11:16:21 -08001341 /* TODO: create/pass atags to kernel */
1342
Ajay Dudanie28a6072011-07-01 13:59:46 -07001343 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001344 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001345 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1346
1347 return 0;
1348}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001349
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001350BUF_DMA_ALIGN(info_buf, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07001351void write_device_info_mmc(device_info *dev)
1352{
1353 struct device_info *info = (void*) info_buf;
1354 unsigned long long ptn = 0;
1355 unsigned long long size;
1356 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001357 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001358 uint8_t lun = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001359
1360 index = partition_get_index("aboot");
1361 ptn = partition_get_offset(index);
1362 if(ptn == 0)
1363 {
1364 return;
1365 }
1366
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001367 lun = partition_get_lun(index);
1368 mmc_set_lun(lun);
1369
Shashank Mittal162244e2011-08-08 19:01:25 -07001370 size = partition_get_size(index);
1371
1372 memcpy(info, dev, sizeof(device_info));
1373
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001374 blocksize = mmc_get_device_blocksize();
1375
1376 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Shashank Mittal162244e2011-08-08 19:01:25 -07001377 {
1378 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1379 return;
1380 }
1381}
1382
1383void read_device_info_mmc(device_info *dev)
1384{
1385 struct device_info *info = (void*) info_buf;
1386 unsigned long long ptn = 0;
1387 unsigned long long size;
1388 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001389 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001390
1391 index = partition_get_index("aboot");
1392 ptn = partition_get_offset(index);
1393 if(ptn == 0)
1394 {
1395 return;
1396 }
1397
1398 size = partition_get_size(index);
1399
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001400 blocksize = mmc_get_device_blocksize();
1401
1402 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Shashank Mittal162244e2011-08-08 19:01:25 -07001403 {
1404 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1405 return;
1406 }
1407
1408 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1409 {
1410 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1411 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001412 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001413 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001414
1415 write_device_info_mmc(info);
1416 }
1417 memcpy(dev, info, sizeof(device_info));
1418}
1419
1420void write_device_info_flash(device_info *dev)
1421{
1422 struct device_info *info = (void *) info_buf;
1423 struct ptentry *ptn;
1424 struct ptable *ptable;
1425
1426 ptable = flash_get_ptable();
1427 if (ptable == NULL)
1428 {
1429 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1430 return;
1431 }
1432
1433 ptn = ptable_find(ptable, "devinfo");
1434 if (ptn == NULL)
1435 {
1436 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1437 return;
1438 }
1439
1440 memcpy(info, dev, sizeof(device_info));
1441
1442 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1443 {
1444 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1445 return;
1446 }
1447}
1448
1449void read_device_info_flash(device_info *dev)
1450{
1451 struct device_info *info = (void*) info_buf;
1452 struct ptentry *ptn;
1453 struct ptable *ptable;
1454
1455 ptable = flash_get_ptable();
1456 if (ptable == NULL)
1457 {
1458 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1459 return;
1460 }
1461
1462 ptn = ptable_find(ptable, "devinfo");
1463 if (ptn == NULL)
1464 {
1465 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1466 return;
1467 }
1468
1469 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1470 {
1471 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1472 return;
1473 }
1474
1475 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1476 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001477 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1478 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001479 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001480 write_device_info_flash(info);
1481 }
1482 memcpy(dev, info, sizeof(device_info));
1483}
1484
1485void write_device_info(device_info *dev)
1486{
1487 if(target_is_emmc_boot())
1488 {
1489 write_device_info_mmc(dev);
1490 }
1491 else
1492 {
1493 write_device_info_flash(dev);
1494 }
1495}
1496
1497void read_device_info(device_info *dev)
1498{
1499 if(target_is_emmc_boot())
1500 {
1501 read_device_info_mmc(dev);
1502 }
1503 else
1504 {
1505 read_device_info_flash(dev);
1506 }
1507}
1508
1509void reset_device_info()
1510{
1511 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001512 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001513 write_device_info(&device);
1514}
1515
1516void set_device_root()
1517{
1518 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001519 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001520 write_device_info(&device);
1521}
1522
Amol Jadicb524072012-08-09 16:40:18 -07001523#if DEVICE_TREE
1524int copy_dtb(uint8_t *boot_image_start)
1525{
1526 uint32 dt_image_offset = 0;
1527 uint32_t n;
1528 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001529 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001530 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001531
1532 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1533
Amol Jadicb524072012-08-09 16:40:18 -07001534 if(hdr->dt_size != 0) {
1535
1536 /* add kernel offset */
1537 dt_image_offset += page_size;
1538 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1539 dt_image_offset += n;
1540
1541 /* add ramdisk offset */
1542 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1543 dt_image_offset += n;
1544
1545 /* add second offset */
1546 if(hdr->second_size != 0) {
1547 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1548 dt_image_offset += n;
1549 }
1550
1551 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001552 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001553
Deepa Dinamani19648b42013-09-05 17:05:55 -07001554 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001555 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1556 return -1;
1557 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001558 /* Find index of device tree within device tree table */
1559 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001560 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1561 return -1;
1562 }
1563
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001564 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001565 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001566 {
1567 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1568 return -1;
1569 }
1570
Amol Jadicb524072012-08-09 16:40:18 -07001571 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001572 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001573 boot_image_start + dt_image_offset + dt_entry.offset,
1574 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001575 } else
1576 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001577
1578 /* Everything looks fine. Return success. */
1579 return 0;
1580}
1581#endif
1582
Brian Swetland9c4c0752009-01-25 16:23:50 -08001583void cmd_boot(const char *arg, void *data, unsigned sz)
1584{
1585 unsigned kernel_actual;
1586 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001587 uint32_t image_actual;
1588 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001589 uint32_t sig_actual = SIGNATURE_SIZE;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001590 struct boot_img_hdr *hdr;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001591 struct kernel64_hdr *kptr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001592 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001593 int ret = 0;
1594 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001595
1596 if (sz < sizeof(hdr)) {
1597 fastboot_fail("invalid bootimage header");
1598 return;
1599 }
1600
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001601 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001602
1603 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001604 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001605
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001606 if(target_is_emmc_boot() && hdr->page_size) {
1607 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001608 page_mask = page_size - 1;
1609 }
1610
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001611 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1612 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001613#if DEVICE_TREE
1614 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1615#endif
1616
1617 image_actual = ADD_OF(page_size, kernel_actual);
1618 image_actual = ADD_OF(image_actual, ramdisk_actual);
1619 image_actual = ADD_OF(image_actual, dt_actual);
1620
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001621 if (target_use_signed_kernel() && (!device.is_unlocked))
1622 image_actual = ADD_OF(image_actual, sig_actual);
1623
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001624 /* sz should have atleast raw boot image */
1625 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001626 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07001627 return;
1628 }
1629
1630 /* Verify the boot image
1631 * device & page_size are initialized in aboot_init
1632 */
1633 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi3d839012014-06-26 14:26:39 -07001634 /* Pass size excluding signature size, otherwise we would try to
1635 * access signature beyond its length
1636 */
1637 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001638
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001639 /*
1640 * Update the kernel/ramdisk/tags address if the boot image header
1641 * has default values, these default values come from mkbootimg when
1642 * the boot image is flashed using fastboot flash:raw
1643 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001644 kptr = (struct kernel64_hdr*)((char*) data + page_size);
1645 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07001646
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001647 /* Get virtual addresses since the hdr saves physical addresses. */
1648 hdr->kernel_addr = VA(hdr->kernel_addr);
1649 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1650 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001651
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001652 /* Check if the addresses in the header are valid. */
1653 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1654 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1655 {
1656 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001657 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001658 }
1659
Amol Jadicb524072012-08-09 16:40:18 -07001660#if DEVICE_TREE
1661 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001662 ret = copy_dtb(data);
1663
1664 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001665#else
1666 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1667 {
1668 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001669 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001670 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001671#endif
1672
1673 /* Load ramdisk & kernel */
1674 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1675 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1676
1677#if DEVICE_TREE
1678 /*
1679 * If dtb is not found look for appended DTB in the kernel.
1680 * If appended dev tree is found, update the atags with
1681 * memory address to the DTB appended location on RAM.
1682 * Else update with the atags address in the kernel header
1683 */
1684 if (!dtb_copied) {
1685 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001686 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1687 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001688 if (!dtb) {
1689 fastboot_fail("dtb not found");
1690 return;
1691 }
Amol Jadicb524072012-08-09 16:40:18 -07001692 }
1693#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001694
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001695#ifndef DEVICE_TREE
1696 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1697 {
1698 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001699 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001700 }
1701#endif
1702
Brian Swetland9c4c0752009-01-25 16:23:50 -08001703 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001704 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001705
Dima Zavin77e41f32013-03-06 16:10:43 -08001706 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001707 (const char*) hdr->cmdline, board_machtype(),
1708 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001709}
1710
Dima Zavin214cc642009-01-26 11:16:21 -08001711void cmd_erase(const char *arg, void *data, unsigned sz)
1712{
1713 struct ptentry *ptn;
1714 struct ptable *ptable;
1715
1716 ptable = flash_get_ptable();
1717 if (ptable == NULL) {
1718 fastboot_fail("partition table doesn't exist");
1719 return;
1720 }
1721
1722 ptn = ptable_find(ptable, arg);
1723 if (ptn == NULL) {
1724 fastboot_fail("unknown partition name");
1725 return;
1726 }
1727
1728 if (flash_erase(ptn)) {
1729 fastboot_fail("failed to erase partition");
1730 return;
1731 }
1732 fastboot_okay("");
1733}
1734
Bikas Gurungd48bd242010-09-04 19:54:32 -07001735
1736void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1737{
Kun Liang2f1601a2013-08-12 16:29:54 +08001738 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001739 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001740 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001741 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001742 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001743
Kinson Chikf1a43512011-07-14 11:28:39 -07001744 index = partition_get_index(arg);
1745 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001746 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001747
Kinson Chikf1a43512011-07-14 11:28:39 -07001748 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001749 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001750 return;
1751 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001752
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001753 lun = partition_get_lun(index);
1754 mmc_set_lun(lun);
1755
Oliver Wangcee448d2013-10-22 18:40:13 +08001756#if MMC_SDHCI_SUPPORT
1757 if (mmc_erase_card(ptn, size)) {
1758 fastboot_fail("failed to erase partition\n");
1759 return;
1760 }
1761#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001762 size = partition_get_size(index);
1763 if (size > DEFAULT_ERASE_SIZE)
1764 size = DEFAULT_ERASE_SIZE;
1765
neetidb4b24d62012-01-20 12:13:09 -08001766 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001767 0 in first several blocks */
1768 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001769 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001770 return;
1771 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001772#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001773 fastboot_okay("");
1774}
1775
1776
Ajay Dudani5c761132011-04-07 20:19:04 -07001777void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001778{
1779 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001780 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001781 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001782 char *token = NULL;
1783 char *pname = NULL;
1784 uint8_t lun = 0;
1785 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001786
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001787 token = strtok(arg, ":");
1788 pname = token;
1789 token = strtok(NULL, ":");
1790 if(token)
1791 {
1792 lun = atoi(token);
1793 mmc_set_lun(lun);
1794 lun_set = true;
1795 }
1796
1797 if (!strcmp(pname, "partition"))
Greg Grisco6e754772011-06-23 12:19:39 -07001798 {
1799 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001800 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001801 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001802 return;
1803 }
1804 }
Greg Grisco6e754772011-06-23 12:19:39 -07001805 else
1806 {
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001807 index = partition_get_index(pname);
Kinson Chikf1a43512011-07-14 11:28:39 -07001808 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001809 if(ptn == 0) {
1810 fastboot_fail("partition table doesn't exist");
1811 return;
1812 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001813
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001814 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
Greg Grisco6e754772011-06-23 12:19:39 -07001815 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1816 fastboot_fail("image is not a boot image");
1817 return;
1818 }
1819 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001820
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001821 if(!lun_set)
1822 {
1823 lun = partition_get_lun(index);
1824 mmc_set_lun(lun);
1825 }
1826
Kinson Chikf1a43512011-07-14 11:28:39 -07001827 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001828 if (ROUND_TO_PAGE(sz,511) > size) {
1829 fastboot_fail("size too large");
1830 return;
1831 }
1832 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1833 fastboot_fail("flash write failure");
1834 return;
1835 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001836 }
1837 fastboot_okay("");
1838 return;
1839}
1840
Ajay Dudani5c761132011-04-07 20:19:04 -07001841void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1842{
1843 unsigned int chunk;
1844 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001845 uint32_t *fill_buf = NULL;
1846 uint32_t fill_val;
1847 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001848 sparse_header_t *sparse_header;
1849 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001850 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001851 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301852 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001853 int index = INVALID_PTN;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001854 int i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001855 uint8_t lun = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001856
Kinson Chikf1a43512011-07-14 11:28:39 -07001857 index = partition_get_index(arg);
1858 ptn = partition_get_offset(index);
1859 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001860 fastboot_fail("partition table doesn't exist");
1861 return;
1862 }
1863
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301864 size = partition_get_size(index);
1865 if (ROUND_TO_PAGE(sz,511) > size) {
1866 fastboot_fail("size too large");
1867 return;
1868 }
1869
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001870 lun = partition_get_lun(index);
1871 mmc_set_lun(lun);
1872
Ajay Dudani5c761132011-04-07 20:19:04 -07001873 /* Read and skip over sparse image header */
1874 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001875 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1876 fastboot_fail("size too large");
1877 return;
1878 }
1879
Ajay Dudani5c761132011-04-07 20:19:04 -07001880 data += sparse_header->file_hdr_sz;
1881 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1882 {
1883 /* Skip the remaining bytes in a header that is longer than
1884 * we expected.
1885 */
1886 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1887 }
1888
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001889 dprintf (SPEW, "=== Sparse Image Header ===\n");
1890 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1891 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1892 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1893 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1894 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1895 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1896 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1897 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001898
1899 /* Start processing chunks */
1900 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1901 {
1902 /* Read and skip over chunk header */
1903 chunk_header = (chunk_header_t *) data;
1904 data += sizeof(chunk_header_t);
1905
1906 dprintf (SPEW, "=== Chunk Header ===\n");
1907 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1908 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1909 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1910
1911 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1912 {
1913 /* Skip the remaining bytes in a header that is longer than
1914 * we expected.
1915 */
1916 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1917 }
1918
1919 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1920 switch (chunk_header->chunk_type)
1921 {
1922 case CHUNK_TYPE_RAW:
1923 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1924 chunk_data_sz))
1925 {
1926 fastboot_fail("Bogus chunk size for chunk type Raw");
1927 return;
1928 }
1929
Ajay Dudaniab18f022011-05-12 14:39:22 -07001930 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1931 chunk_data_sz,
1932 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001933 {
1934 fastboot_fail("flash write failure");
1935 return;
1936 }
1937 total_blocks += chunk_header->chunk_sz;
1938 data += chunk_data_sz;
1939 break;
1940
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001941 case CHUNK_TYPE_FILL:
1942 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1943 sizeof(uint32_t)))
1944 {
1945 fastboot_fail("Bogus chunk size for chunk type FILL");
1946 return;
1947 }
1948
1949 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
1950 if (!fill_buf)
1951 {
1952 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
1953 return;
1954 }
1955
1956 fill_val = *(uint32_t *)data;
1957 data = (char *) data + sizeof(uint32_t);
1958 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
1959
1960 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
1961 {
1962 fill_buf[i] = fill_val;
1963 }
1964
1965 for (i = 0; i < chunk_blk_cnt; i++)
1966 {
1967 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1968 sparse_header->blk_sz,
1969 fill_buf))
1970 {
1971 fastboot_fail("flash write failure");
1972 free(fill_buf);
1973 return;
1974 }
1975
1976 total_blocks++;
1977 }
1978
1979 free(fill_buf);
1980 break;
1981
Ajay Dudani5c761132011-04-07 20:19:04 -07001982 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001983 total_blocks += chunk_header->chunk_sz;
1984 break;
1985
Ajay Dudani5c761132011-04-07 20:19:04 -07001986 case CHUNK_TYPE_CRC:
1987 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1988 {
1989 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1990 return;
1991 }
1992 total_blocks += chunk_header->chunk_sz;
1993 data += chunk_data_sz;
1994 break;
1995
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001996 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001997 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07001998 fastboot_fail("Unknown chunk type");
1999 return;
2000 }
2001 }
2002
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002003 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2004 total_blocks, sparse_header->total_blks);
2005
2006 if(total_blocks != sparse_header->total_blks)
2007 {
2008 fastboot_fail("sparse image write failure");
2009 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002010
2011 fastboot_okay("");
2012 return;
2013}
2014
2015void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2016{
2017 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002018 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2019 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002020
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002021#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002022 int ret=0;
2023 uint32 major_version=0;
2024 uint32 minor_version=0;
2025
2026 ret = scm_svc_version(&major_version,&minor_version);
2027 if(!ret)
2028 {
2029 if(major_version >= 2)
2030 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002031 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002032 {
2033 ret = encrypt_scm((uint32 **) &data, &sz);
2034 if (ret != 0) {
2035 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2036 return;
2037 }
2038
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002039 /* Protect only for SSD */
2040 if (!strcmp(arg, "ssd")) {
2041 ret = scm_protect_keystore((uint32 *) data, sz);
2042 if (ret != 0) {
2043 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2044 return;
2045 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002046 }
2047 }
2048 else
2049 {
2050 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2051 if(ret != 0)
2052 {
2053 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2054 return;
2055 }
2056 }
2057 }
2058 else
2059 {
2060 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2061 magic_number[1] == DECRYPT_MAGIC_1)
2062 {
2063 ret = decrypt_scm((uint32 **) &data, &sz);
2064 if (ret != 0) {
2065 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2066 return;
2067 }
2068 }
2069 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2070 magic_number[1] == ENCRYPT_MAGIC_1)
2071 {
2072 ret = encrypt_scm((uint32 **) &data, &sz);
2073 if (ret != 0) {
2074 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2075 return;
2076 }
2077 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002078 }
2079 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002080 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002081 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002082 dprintf(CRITICAL,"INVALID SVC Version\n");
2083 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002084 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002085#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002086
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002087 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07002088 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
2089 cmd_flash_mmc_img(arg, data, sz);
2090 else
2091 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07002092 return;
2093}
2094
Dima Zavin214cc642009-01-26 11:16:21 -08002095void cmd_flash(const char *arg, void *data, unsigned sz)
2096{
2097 struct ptentry *ptn;
2098 struct ptable *ptable;
2099 unsigned extra = 0;
2100
2101 ptable = flash_get_ptable();
2102 if (ptable == NULL) {
2103 fastboot_fail("partition table doesn't exist");
2104 return;
2105 }
2106
2107 ptn = ptable_find(ptable, arg);
2108 if (ptn == NULL) {
2109 fastboot_fail("unknown partition name");
2110 return;
2111 }
2112
2113 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
2114 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2115 fastboot_fail("image is not a boot image");
2116 return;
2117 }
2118 }
2119
Amol Jadi5c61a952012-05-04 17:05:35 -07002120 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07002121 || !strcmp(ptn->name, "userdata")
2122 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08002123 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002124 || !strcmp(ptn->name, "modem"))
2125 {
2126 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07002127 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002128 else
2129 extra = 0;
2130 }
2131 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002132 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002133
2134 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2135 if (flash_write(ptn, extra, data, sz)) {
2136 fastboot_fail("flash write failure");
2137 return;
2138 }
2139 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2140 fastboot_okay("");
2141}
2142
2143void cmd_continue(const char *arg, void *data, unsigned sz)
2144{
2145 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002146 fastboot_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002147 if (target_is_emmc_boot())
2148 {
2149 boot_linux_from_mmc();
2150 }
2151 else
2152 {
2153 boot_linux_from_flash();
2154 }
Dima Zavin214cc642009-01-26 11:16:21 -08002155}
2156
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002157void cmd_reboot(const char *arg, void *data, unsigned sz)
2158{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002159 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002160 fastboot_okay("");
2161 reboot_device(0);
2162}
2163
2164void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2165{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002166 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002167 fastboot_okay("");
2168 reboot_device(FASTBOOT_MODE);
2169}
2170
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002171void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2172{
2173 dprintf(INFO, "Enabling charger screen check\n");
2174 device.charger_screen_enabled = 1;
2175 write_device_info(&device);
2176 fastboot_okay("");
2177}
2178
2179void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2180{
2181 dprintf(INFO, "Disabling charger screen check\n");
2182 device.charger_screen_enabled = 0;
2183 write_device_info(&device);
2184 fastboot_okay("");
2185}
2186
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302187void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2188{
2189 dprintf(INFO, "Selecting display panel %s\n", arg);
2190 if (arg)
2191 strlcpy(device.display_panel, arg,
2192 sizeof(device.display_panel));
2193 write_device_info(&device);
2194 fastboot_okay("");
2195}
2196
Shashank Mittal162244e2011-08-08 19:01:25 -07002197void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2198{
2199 if(!device.is_unlocked)
2200 {
2201 device.is_unlocked = 1;
2202 write_device_info(&device);
2203 }
2204 fastboot_okay("");
2205}
2206
Shashank Mittala0032282011-08-26 14:50:11 -07002207void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2208{
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302209 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002210 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002211 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002212 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2213 fastboot_info(response);
2214 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002215 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302216 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2217 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002218 fastboot_okay("");
2219}
2220
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002221void cmd_preflash(const char *arg, void *data, unsigned sz)
2222{
2223 fastboot_okay("");
2224}
2225
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302226static struct fbimage logo_header = {0};
2227struct fbimage* splash_screen_flash();
2228
2229int splash_screen_check_header(struct fbimage *logo)
2230{
2231 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2232 return -1;
2233 if (logo->header.width == 0 || logo->header.height == 0)
2234 return -1;
2235 return 0;
2236}
2237
2238struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002239{
2240 struct ptentry *ptn;
2241 struct ptable *ptable;
2242 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302243 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002244
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302245
2246 ptable = flash_get_ptable();
2247 if (ptable == NULL) {
2248 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2249 return NULL;
2250 }
2251 ptn = ptable_find(ptable, "splash");
2252 if (ptn == NULL) {
2253 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2254 return NULL;
2255 }
2256
2257 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2258 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2259 return NULL;
2260 }
2261
2262 if (splash_screen_check_header(logo)) {
2263 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2264 return NULL;
2265 }
2266
2267 fb_display = fbcon_display();
2268 if (fb_display) {
2269 uint8_t *base = (uint8_t *) fb_display->base;
2270 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
2271 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002272 }
2273
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302274 if (flash_read(ptn + sizeof(logo->header), 0,
2275 base,
2276 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2277 fbcon_clear();
2278 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2279 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002280 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302281 logo->image = base;
2282 }
2283
2284 return logo;
2285}
2286
2287struct fbimage* splash_screen_mmc()
2288{
2289 int index = INVALID_PTN;
2290 unsigned long long ptn = 0;
2291 struct fbcon_config *fb_display = NULL;
2292 struct fbimage *logo = &logo_header;
2293
2294 index = partition_get_index("splash");
2295 if (index == 0) {
2296 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2297 return NULL;
2298 }
2299
2300 ptn = partition_get_offset(index);
2301 if (ptn == 0) {
2302 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2303 return NULL;
2304 }
2305
2306 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2307 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2308 return NULL;
2309 }
2310
2311 if (splash_screen_check_header(logo)) {
2312 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2313 return NULL;
2314 }
2315
2316 fb_display = fbcon_display();
2317 if (fb_display) {
2318 uint8_t *base = (uint8_t *) fb_display->base;
2319 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2320 base += LOGO_IMG_OFFSET;
2321
2322 if (mmc_read(ptn + sizeof(logo->header),
2323 base,
2324 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2325 fbcon_clear();
2326 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2327 return NULL;
2328 }
2329
2330 logo->image = base;
2331 }
2332
2333 return logo;
2334}
2335
2336
2337struct fbimage* fetch_image_from_partition()
2338{
2339 if (target_is_emmc_boot()) {
2340 return splash_screen_mmc();
2341 } else {
2342 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002343 }
2344}
2345
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002346/* Get the size from partiton name */
2347static void get_partition_size(const char *arg, char *response)
2348{
2349 uint64_t ptn = 0;
2350 uint64_t size;
2351 int index = INVALID_PTN;
2352
2353 index = partition_get_index(arg);
2354
2355 if (index == INVALID_PTN)
2356 {
2357 dprintf(CRITICAL, "Invalid partition index\n");
2358 return;
2359 }
2360
2361 ptn = partition_get_offset(index);
2362
2363 if(!ptn)
2364 {
2365 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2366 return;
2367 }
2368
2369 size = partition_get_size(index);
2370
2371 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2372 return;
2373}
2374
2375/*
2376 * Publish the partition type & size info
2377 * fastboot getvar will publish the required information.
2378 * fastboot getvar partition_size:<partition_name>: partition size in hex
2379 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2380 */
2381static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2382{
2383 uint8_t i;
2384
2385 for (i = 0; i < num_parts; i++) {
2386 get_partition_size(info[i].part_name, info[i].size_response);
2387
2388 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2389 {
2390 dprintf(CRITICAL, "partition size name truncated\n");
2391 return;
2392 }
2393 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2394 {
2395 dprintf(CRITICAL, "partition type name truncated\n");
2396 return;
2397 }
2398
2399 /* publish partition size & type info */
2400 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2401 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2402 }
2403}
2404
Amol Jadi5edf3552013-07-23 14:15:34 -07002405/* register commands and variables for fastboot */
2406void aboot_fastboot_register_commands(void)
2407{
2408 if (target_is_emmc_boot())
2409 {
2410 fastboot_register("flash:", cmd_flash_mmc);
2411 fastboot_register("erase:", cmd_erase_mmc);
2412 }
2413 else
2414 {
2415 fastboot_register("flash:", cmd_flash);
2416 fastboot_register("erase:", cmd_erase);
2417 }
2418
2419 fastboot_register("boot", cmd_boot);
2420 fastboot_register("continue", cmd_continue);
2421 fastboot_register("reboot", cmd_reboot);
2422 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2423 fastboot_register("oem unlock", cmd_oem_unlock);
2424 fastboot_register("oem device-info", cmd_oem_devinfo);
2425 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002426 fastboot_register("oem enable-charger-screen",
2427 cmd_oem_enable_charger_screen);
2428 fastboot_register("oem disable-charger-screen",
2429 cmd_oem_disable_charger_screen);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302430 fastboot_register("oem select-display-panel",
2431 cmd_oem_select_display_panel);
Amol Jadi5edf3552013-07-23 14:15:34 -07002432 /* publish variables and their values */
2433 fastboot_publish("product", TARGET(BOARD));
2434 fastboot_publish("kernel", "lk");
2435 fastboot_publish("serialno", sn_buf);
2436
2437 /*
2438 * partition info is supported only for emmc partitions
2439 * Calling this for NAND prints some error messages which
2440 * is harmless but misleading. Avoid calling this for NAND
2441 * devices.
2442 */
2443 if (target_is_emmc_boot())
2444 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2445
2446 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002447 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2448 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002449 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002450 /* Is the charger screen check enabled */
2451 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2452 device.charger_screen_enabled);
2453 fastboot_publish("charger-screen-enabled",
2454 (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302455 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2456 device.display_panel);
2457 fastboot_publish("display-panel",
2458 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002459}
2460
Brian Swetland9c4c0752009-01-25 16:23:50 -08002461void aboot_init(const struct app_descriptor *app)
2462{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002463 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002464 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002465
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002466 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002467 if (target_is_emmc_boot())
2468 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002469 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002470 page_mask = page_size - 1;
2471 }
2472 else
2473 {
2474 page_size = flash_page_size();
2475 page_mask = page_size - 1;
2476 }
2477
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002478 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2479
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002480 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002481
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002482 /* Display splash screen if enabled */
2483#if DISPLAY_SPLASH_SCREEN
2484 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05302485 target_display_init(device.display_panel);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08002486 dprintf(SPEW, "Display Init: Done\n");
2487#endif
2488
2489
Greg Griscod6250552011-06-29 14:40:23 -07002490 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002491 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002492
Dhaval Patel223ec952013-07-18 14:49:44 -07002493 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2494
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002495 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002496 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002497 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002498 dprintf(ALWAYS,"dload mode key sequence detected\n");
2499 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002500 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002501 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002502 }
2503 else
2504 {
Aparna Mallavarapu0751f7f2014-04-01 22:40:39 +05302505 reboot_device(DLOAD);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002506 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002507 }
2508 boot_into_fastboot = true;
2509 }
2510 if (!boot_into_fastboot)
2511 {
2512 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2513 boot_into_recovery = 1;
2514 if (!boot_into_recovery &&
2515 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002516 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002517 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002518 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002519 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002520 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002521 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002522
Ajay Dudani77421292010-10-27 19:34:06 -07002523 reboot_mode = check_reboot_mode();
2524 if (reboot_mode == RECOVERY_MODE) {
2525 boot_into_recovery = 1;
2526 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002527 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002528 }
2529
Pavel Nedev5d91d412013-04-29 11:34:24 +03002530 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002531 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002532 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002533 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002534 if(emmc_recovery_init())
2535 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2536 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002537 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002538 if((device.is_unlocked) || (device.is_tampered))
2539 {
2540 #ifdef TZ_TAMPER_FUSE
2541 set_tamper_fuse_cmd();
2542 #endif
2543 #if USE_PCOM_SECBOOT
2544 set_tamper_flag(device.is_tampered);
2545 #endif
2546 }
Shashank Mittala0032282011-08-26 14:50:11 -07002547 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002548 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002549 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002550 else
2551 {
2552 recovery_init();
2553 #if USE_PCOM_SECBOOT
2554 if((device.is_unlocked) || (device.is_tampered))
2555 set_tamper_flag(device.is_tampered);
2556 #endif
2557 boot_linux_from_flash();
2558 }
2559 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2560 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002561 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002562
Amol Jadi5edf3552013-07-23 14:15:34 -07002563 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002564
Amol Jadi5edf3552013-07-23 14:15:34 -07002565 /* register aboot specific fastboot commands */
2566 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002567
Amol Jadi5edf3552013-07-23 14:15:34 -07002568 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002569 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002570
2571 /* initialize and start fastboot */
2572 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002573}
2574
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002575uint32_t get_page_size()
2576{
2577 return page_size;
2578}
2579
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002580/*
2581 * Calculated and save hash (SHA256) for non-signed boot image.
2582 *
2583 * Hash the same data that is checked on the signed boot image.
2584 * Kernel and Ramdisk are already read to memory buffers.
2585 * Need to read the entire device-tree from mmc
2586 * since non-signed image only read the DT tags of the relevant platform.
2587 *
2588 * @param kernel_addr - kernel bufer
2589 * @param kernel_actual - kernel size in bytes
2590 * @param ramdisk_addr - ramdisk buffer
2591 * @param ramdisk_actual - ramdisk size
2592 * @param ptn - partition
2593 * @param dt_offset - device tree offset on mmc partition
2594 * @param dt_size
2595 *
2596 * @return int - 0 on success, negative value on failure.
2597 */
2598int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2599 void *ramdisk_addr, unsigned ramdisk_actual,
2600 unsigned long long ptn,
2601 unsigned dt_offset, unsigned dt_size)
2602{
2603 SHA256_CTX sha256_ctx;
2604 char digest[32]={0};
2605 char *buf = (char *)target_get_scratch_address();
2606 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2607 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2608
2609 SHA256_Init(&sha256_ctx);
2610
2611 /* Read Boot Header */
2612 if (mmc_read(ptn, buf, page_size))
2613 {
2614 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2615 return -1;
2616 }
2617 /* Read entire Device Tree */
2618 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2619 {
2620 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2621 return -1;
2622 }
2623 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2624 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2625 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2626 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2627
2628 SHA256_Final(digest, &sha256_ctx);
2629
2630 save_kernel_hash_cmd(digest);
2631 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2632
2633 return 0;
2634}
2635
Brian Swetland9c4c0752009-01-25 16:23:50 -08002636APP_START(aboot)
2637 .init = aboot_init,
2638APP_END