blob: f788ba0504d87b57ac68d0af19eaab6e334b8b3b [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Kuogee Hsieh55e92c52013-12-20 08:34:10 -08005 * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080038#include <kernel/thread.h>
39#include <arch/ops.h>
40
Dima Zavin214cc642009-01-26 11:16:21 -080041#include <dev/flash.h>
42#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080043#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080044#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080045#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070046#include <target.h>
47#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070048#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070049#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070050#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070051#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070052#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030053#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070054#include <platform/iomap.h>
Dima Zavin214cc642009-01-26 11:16:21 -080055
Neeti Desai17379b82012-06-04 18:42:53 -070056#if DEVICE_TREE
57#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070058#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070059#endif
60
Shashank Mittalcd98d472011-08-02 14:29:24 -070061#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080062#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080063#include "bootimg.h"
64#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070065#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070066#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070067#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070068#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080069
Shashank Mittal162244e2011-08-08 19:01:25 -070070#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070071
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070072extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070073extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070074extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -070075extern int get_target_boot_params(const char *cmdline, const char *part,
76 char *buf, int buflen);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070077
78void write_device_info_mmc(device_info *dev);
79void write_device_info_flash(device_info *dev);
80
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070081#define EXPAND(NAME) #NAME
82#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070083
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080084#ifdef MEMBASE
85#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
86#else
David Ng183a7422009-12-07 14:55:21 -080087#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080088#endif
89
Deepa Dinamani0e163a42013-05-24 17:08:15 -070090#ifndef MEMSIZE
91#define MEMSIZE 1024*1024
92#endif
93
94#define MAX_TAGS_SIZE 1024
95
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080096#define RECOVERY_MODE 0x77665502
97#define FASTBOOT_MODE 0x77665500
98
Kun Liang2f1601a2013-08-12 16:29:54 +080099/* make 4096 as default size to ensure EFS,EXT4's erasing */
100#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsieh55e92c52013-12-20 08:34:10 -0800101#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800102
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800103#define UBI_MAGIC "UBI#"
104#define UBI_MAGIC_SIZE 0x04
105
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700106#if UFS_SUPPORT
107static const char *emmc_cmdline = " androidboot.bootdevice=msm_sdcc.1";
108static const char *ufs_cmdline = " androidboot.bootdevice=msm_ufs.1";
109#else
David Ng183a7422009-12-07 14:55:21 -0800110static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700111#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800112static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300113static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800114static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800115static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700116static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300117static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800118
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800119static const char *baseband_apq = " androidboot.baseband=apq";
120static const char *baseband_msm = " androidboot.baseband=msm";
121static const char *baseband_csfb = " androidboot.baseband=csfb";
122static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700123static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800124static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700125static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800126static const char *baseband_dsda = " androidboot.baseband=dsda";
127static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800128static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800129static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800130
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700131static unsigned page_size = 0;
132static unsigned page_mask = 0;
133static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
134static bool boot_into_ffbm;
Joonwoo Park61112782013-10-02 19:50:39 -0700135static char target_boot_params[64];
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700136
Shashank Mittalcd98d472011-08-02 14:29:24 -0700137/* Assuming unauthorized kernel image by default */
138static int auth_kernel_img = 0;
139
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700140static device_info device = {DEVICE_MAGIC, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700141
Dima Zavin42168f22009-01-30 11:52:22 -0800142struct atag_ptbl_entry
143{
144 char name[16];
145 unsigned offset;
146 unsigned size;
147 unsigned flags;
148};
149
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700150/*
151 * Partition info, required to be published
152 * for fastboot
153 */
154struct getvar_partition_info {
155 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
156 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
157 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
158 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
159 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
160};
161
162/*
163 * Right now, we are publishing the info for only
164 * three partitions
165 */
166struct getvar_partition_info part_info[] =
167{
168 { "system" , "partition-size:", "partition-type:", "", "ext4" },
169 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
170 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
171};
172
173char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700174char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800175char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700176char display_panel_buf[MAX_PANEL_BUF_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700177
Greg Griscod2471ef2011-07-14 13:00:42 -0700178extern int emmc_recovery_init(void);
179
Kinson Chik0b1c8162011-08-31 16:31:57 -0700180#if NO_KEYPAD_DRIVER
181extern int fastboot_trigger(void);
182#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700183
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700184static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
185{
186 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700187#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
188 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
189 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
190 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700191#endif
192}
193
Dima Zavin42168f22009-01-30 11:52:22 -0800194static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
195{
196 struct atag_ptbl_entry atag_ptn;
197
198 memcpy(atag_ptn.name, ptn->name, 16);
199 atag_ptn.name[15] = '\0';
200 atag_ptn.offset = ptn->start;
201 atag_ptn.size = ptn->length;
202 atag_ptn.flags = ptn->flags;
203 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
204 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
205}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800206
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700207#if UFS_SUPPORT
208char* get_boot_dev_cmdline()
209{
210 const char *boot_device;
211 uint32_t val = 0;
212
213 val = target_get_boot_device();
214 switch(val)
215 {
216 case BOOT_DEFAULT:
217 case BOOT_EMMC:
218 boot_device = emmc_cmdline;
219 break;
220 case BOOT_UFS:
221 boot_device = ufs_cmdline;
222 break;
223 default:
224 dprintf(CRITICAL,"ERROR: Unexpected boot_device val=%x",val);
225 ASSERT(0);
226 };
227
228 return boot_device;
229}
230#endif
231
Neeti Desaie245d492012-06-01 12:52:13 -0700232unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800233{
David Ng183a7422009-12-07 14:55:21 -0800234 int cmdline_len = 0;
235 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800236 unsigned char *cmdline_final = NULL;
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700237#if UFS_SUPPORT
238 const char *boot_dev_cmdline = NULL;
239#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700240 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800241 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300242 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700243 int have_target_boot_params = 0;
Dima Zavin42168f22009-01-30 11:52:22 -0800244
Brian Swetland9c4c0752009-01-25 16:23:50 -0800245 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800246 cmdline_len = strlen(cmdline);
247 have_cmdline = 1;
248 }
249 if (target_is_emmc_boot()) {
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700250#if UFS_SUPPORT
251 boot_dev_cmdline = get_boot_dev_cmdline();
252 cmdline_len += strlen(boot_dev_cmdline);
253#else
David Ng183a7422009-12-07 14:55:21 -0800254 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700255#endif
David Ng183a7422009-12-07 14:55:21 -0800256 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800257
258 cmdline_len += strlen(usb_sn_cmdline);
259 cmdline_len += strlen(sn_buf);
260
Pavel Nedev5614d222013-06-17 18:01:02 +0300261 if (boot_into_recovery && gpt_exists)
262 cmdline_len += strlen(secondary_gpt_enable);
263
Pavel Nedev328ac822013-04-05 15:25:11 +0300264 if (boot_into_ffbm) {
265 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700266 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800267 /* reduce kernel console messages to speed-up boot */
268 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700269 } else if (device.charger_screen_enabled &&
270 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700271 pause_at_bootup = 1;
272 cmdline_len += strlen(battchg_pause);
273 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800274
Shashank Mittalcd98d472011-08-02 14:29:24 -0700275 if(target_use_signed_kernel() && auth_kernel_img) {
276 cmdline_len += strlen(auth_kernel);
277 }
278
Joonwoo Park61112782013-10-02 19:50:39 -0700279 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
280 "system",
281 target_boot_params,
282 sizeof(target_boot_params)) == 0) {
283 have_target_boot_params = 1;
284 cmdline_len += strlen(target_boot_params);
285 }
286
Ajay Dudanid04110c2011-01-17 23:55:07 -0800287 /* Determine correct androidboot.baseband to use */
288 switch(target_baseband())
289 {
290 case BASEBAND_APQ:
291 cmdline_len += strlen(baseband_apq);
292 break;
293
294 case BASEBAND_MSM:
295 cmdline_len += strlen(baseband_msm);
296 break;
297
298 case BASEBAND_CSFB:
299 cmdline_len += strlen(baseband_csfb);
300 break;
301
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800302 case BASEBAND_SVLTE2A:
303 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800304 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700305
306 case BASEBAND_MDM:
307 cmdline_len += strlen(baseband_mdm);
308 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700309
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800310 case BASEBAND_MDM2:
311 cmdline_len += strlen(baseband_mdm2);
312 break;
313
Amol Jadi5c61a952012-05-04 17:05:35 -0700314 case BASEBAND_SGLTE:
315 cmdline_len += strlen(baseband_sglte);
316 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530317
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800318 case BASEBAND_SGLTE2:
319 cmdline_len += strlen(baseband_sglte2);
320 break;
321
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530322 case BASEBAND_DSDA:
323 cmdline_len += strlen(baseband_dsda);
324 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800325
326 case BASEBAND_DSDA2:
327 cmdline_len += strlen(baseband_dsda2);
328 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800329 }
330
Dhaval Patel223ec952013-07-18 14:49:44 -0700331 if (target_display_panel_node(display_panel_buf, MAX_PANEL_BUF_SIZE) &&
332 strlen(display_panel_buf))
333 {
Dhaval Patel223ec952013-07-18 14:49:44 -0700334 cmdline_len += strlen(display_panel_buf);
335 }
336
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800337 if (target_warm_boot()) {
338 warm_boot = true;
339 cmdline_len += strlen(warmboot_cmdline);
340 }
341
David Ng183a7422009-12-07 14:55:21 -0800342 if (cmdline_len > 0) {
343 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700344 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
345 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700346
Amol Jadi168b7712012-03-06 16:15:00 -0800347 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700348 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800349 if (have_cmdline) {
350 src = cmdline;
351 while ((*dst++ = *src++));
352 }
353 if (target_is_emmc_boot()) {
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700354#if UFS_SUPPORT
355 src = boot_dev_cmdline;
356#else
David Ng183a7422009-12-07 14:55:21 -0800357 src = emmc_cmdline;
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700358#endif
David Ng183a7422009-12-07 14:55:21 -0800359 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700360 have_cmdline = 1;
361 while ((*dst++ = *src++));
362 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800363
364 src = usb_sn_cmdline;
365 if (have_cmdline) --dst;
366 have_cmdline = 1;
367 while ((*dst++ = *src++));
368 src = sn_buf;
369 if (have_cmdline) --dst;
370 have_cmdline = 1;
371 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800372 if (warm_boot) {
373 if (have_cmdline) --dst;
374 src = warmboot_cmdline;
375 while ((*dst++ = *src++));
376 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800377
Pavel Nedev5614d222013-06-17 18:01:02 +0300378 if (boot_into_recovery && gpt_exists) {
379 src = secondary_gpt_enable;
380 if (have_cmdline) --dst;
381 while ((*dst++ = *src++));
382 }
383
Pavel Nedev328ac822013-04-05 15:25:11 +0300384 if (boot_into_ffbm) {
385 src = androidboot_mode;
386 if (have_cmdline) --dst;
387 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700388 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300389 if (have_cmdline) --dst;
390 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800391 src = loglevel;
392 if (have_cmdline) --dst;
393 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300394 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700395 src = battchg_pause;
396 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800397 while ((*dst++ = *src++));
398 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800399
Shashank Mittalcd98d472011-08-02 14:29:24 -0700400 if(target_use_signed_kernel() && auth_kernel_img) {
401 src = auth_kernel;
402 if (have_cmdline) --dst;
403 while ((*dst++ = *src++));
404 }
405
Ajay Dudanid04110c2011-01-17 23:55:07 -0800406 switch(target_baseband())
407 {
408 case BASEBAND_APQ:
409 src = baseband_apq;
410 if (have_cmdline) --dst;
411 while ((*dst++ = *src++));
412 break;
413
414 case BASEBAND_MSM:
415 src = baseband_msm;
416 if (have_cmdline) --dst;
417 while ((*dst++ = *src++));
418 break;
419
420 case BASEBAND_CSFB:
421 src = baseband_csfb;
422 if (have_cmdline) --dst;
423 while ((*dst++ = *src++));
424 break;
425
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800426 case BASEBAND_SVLTE2A:
427 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800428 if (have_cmdline) --dst;
429 while ((*dst++ = *src++));
430 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700431
432 case BASEBAND_MDM:
433 src = baseband_mdm;
434 if (have_cmdline) --dst;
435 while ((*dst++ = *src++));
436 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700437
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800438 case BASEBAND_MDM2:
439 src = baseband_mdm2;
440 if (have_cmdline) --dst;
441 while ((*dst++ = *src++));
442 break;
443
Amol Jadi5c61a952012-05-04 17:05:35 -0700444 case BASEBAND_SGLTE:
445 src = baseband_sglte;
446 if (have_cmdline) --dst;
447 while ((*dst++ = *src++));
448 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530449
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800450 case BASEBAND_SGLTE2:
451 src = baseband_sglte2;
452 if (have_cmdline) --dst;
453 while ((*dst++ = *src++));
454 break;
455
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530456 case BASEBAND_DSDA:
457 src = baseband_dsda;
458 if (have_cmdline) --dst;
459 while ((*dst++ = *src++));
460 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800461
462 case BASEBAND_DSDA2:
463 src = baseband_dsda2;
464 if (have_cmdline) --dst;
465 while ((*dst++ = *src++));
466 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800467 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700468
469 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700470 src = display_panel_buf;
471 if (have_cmdline) --dst;
472 while ((*dst++ = *src++));
473 }
Joonwoo Park61112782013-10-02 19:50:39 -0700474
475 if (have_target_boot_params) {
476 if (have_cmdline) --dst;
477 src = target_boot_params;
478 while ((*dst++ = *src++));
479 }
Neeti Desaie245d492012-06-01 12:52:13 -0700480 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700481
482
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800483 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700484 return cmdline_final;
485}
486
487unsigned *atag_core(unsigned *ptr)
488{
489 /* CORE */
490 *ptr++ = 2;
491 *ptr++ = 0x54410001;
492
493 return ptr;
494
495}
496
497unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
498 unsigned ramdisk_size)
499{
500 if (ramdisk_size) {
501 *ptr++ = 4;
502 *ptr++ = 0x54420005;
503 *ptr++ = (unsigned)ramdisk;
504 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800505 }
506
Neeti Desaie245d492012-06-01 12:52:13 -0700507 return ptr;
508}
509
510unsigned *atag_ptable(unsigned **ptr_addr)
511{
512 int i;
513 struct ptable *ptable;
514
515 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700516 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
517 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700518 *(*ptr_addr)++ = 0x4d534d70;
519 for (i = 0; i < ptable->count; ++i)
520 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
521 }
522
523 return (*ptr_addr);
524}
525
526unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
527{
528 int cmdline_length = 0;
529 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700530 char *dest;
531
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800532 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700533 n = (cmdline_length + 4) & (~3);
534
535 *ptr++ = (n / 4) + 2;
536 *ptr++ = 0x54410009;
537 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800538 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700539 ptr += (n / 4);
540
541 return ptr;
542}
543
544unsigned *atag_end(unsigned *ptr)
545{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800546 /* END */
547 *ptr++ = 0;
548 *ptr++ = 0;
549
Neeti Desaie245d492012-06-01 12:52:13 -0700550 return ptr;
551}
552
553void generate_atags(unsigned *ptr, const char *cmdline,
554 void *ramdisk, unsigned ramdisk_size)
555{
556
557 ptr = atag_core(ptr);
558 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
559 ptr = target_atag_mem(ptr);
560
561 /* Skip NAND partition ATAGS for eMMC boot */
562 if (!target_is_emmc_boot()){
563 ptr = atag_ptable(&ptr);
564 }
565
566 ptr = atag_cmdline(ptr, cmdline);
567 ptr = atag_end(ptr);
568}
569
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700570typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700571void boot_linux(void *kernel, unsigned *tags,
572 const char *cmdline, unsigned machtype,
573 void *ramdisk, unsigned ramdisk_size)
574{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800575 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800576#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700577 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800578#endif
579
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700580 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800581 uint32_t tags_phys = PA((addr_t)tags);
582
583 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700584
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800585 final_cmdline = update_cmdline((const char*)cmdline);
586
Neeti Desai17379b82012-06-04 18:42:53 -0700587#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800588 dprintf(INFO, "Updating device tree: start\n");
589
Neeti Desai17379b82012-06-04 18:42:53 -0700590 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800591 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700592 if(ret)
593 {
594 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
595 ASSERT(0);
596 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800597 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700598#else
Neeti Desaie245d492012-06-01 12:52:13 -0700599 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800600 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700601#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700602
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700603 /* Perform target specific cleanup */
604 target_uninit();
605
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800606 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
607 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800608
609 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700610
Amol Jadi4421e652011-06-16 15:00:48 -0700611 /* do any platform specific cleanup before kernel entry */
612 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700613
Brian Swetland9c4c0752009-01-25 16:23:50 -0800614 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700615
Amol Jadi504f9fe2012-08-16 13:56:48 -0700616#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800617 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700618#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700619 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700620 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800621}
622
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700623/* Function to check if the memory address range falls within the aboot
624 * boundaries.
625 * start: Start of the memory region
626 * size: Size of the memory region
627 */
628int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
629{
630 /* Check for boundary conditions. */
631 if ((start + size) < start)
632 return -1;
633
634 /* Check for memory overlap. */
635 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
636 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700637 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700638 return 0;
639 else
640 return -1;
641}
642
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800643#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800644
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700645BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800646#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700647BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800648#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800649
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700650static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
651{
652 int ret;
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800653#if IMAGE_VERIF_ALGO_SHA1
654 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
655#else
656 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
657#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700658
659 /* Assume device is rooted at this time. */
660 device.is_tampered = 1;
661
662 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
663
664 ret = image_verify((unsigned char *)bootimg_addr,
665 (unsigned char *)(bootimg_addr + bootimg_size),
666 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800667 auth_algo);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700668
669 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
670
671 if (ret)
672 {
673 /* Authorized kernel */
674 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700675 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700676 }
677
678#if USE_PCOM_SECBOOT
679 set_tamper_flag(device.is_tampered);
680#endif
681
682 if(device.is_tampered)
683 {
684 write_device_info_mmc(&device);
685 #ifdef TZ_TAMPER_FUSE
686 set_tamper_fuse_cmd();
687 #endif
688 #ifdef ASSERT_ON_TAMPER
689 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
690 ASSERT(0);
691 #endif
692 }
693}
694
Shashank Mittal23b8f422010-04-16 19:27:21 -0700695int boot_linux_from_mmc(void)
696{
697 struct boot_img_hdr *hdr = (void*) buf;
698 struct boot_img_hdr *uhdr;
699 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700700 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700701 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700702 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700703
Shashank Mittalcd98d472011-08-02 14:29:24 -0700704 unsigned char *image_addr = 0;
705 unsigned kernel_actual;
706 unsigned ramdisk_actual;
707 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700708 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700709
710#if DEVICE_TREE
711 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700712 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700713 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800714 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700715 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700716#endif
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700717 if (!boot_into_recovery) {
718 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
719 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
720 if (rcode <= 0) {
721 boot_into_ffbm = false;
722 if (rcode < 0)
723 dprintf(CRITICAL,"failed to get ffbm cookie");
724 } else
725 boot_into_ffbm = true;
726 } else
727 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700728 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
729 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
730 dprintf(INFO, "Unified boot method!\n");
731 hdr = uhdr;
732 goto unified_boot;
733 }
Greg Griscod6250552011-06-29 14:40:23 -0700734 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700735 index = partition_get_index("boot");
736 ptn = partition_get_offset(index);
737 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700738 dprintf(CRITICAL, "ERROR: No boot partition found\n");
739 return -1;
740 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700741 }
742 else {
743 index = partition_get_index("recovery");
744 ptn = partition_get_offset(index);
745 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700746 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
747 return -1;
748 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700749 }
750
Greg Griscod6250552011-06-29 14:40:23 -0700751 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700752 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
753 return -1;
754 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700755
756 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700757 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700758 return -1;
759 }
760
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700761 if (hdr->page_size && (hdr->page_size != page_size)) {
762 page_size = hdr->page_size;
763 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700764 }
765
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700766 /*
767 * Update the kernel/ramdisk/tags address if the boot image header
768 * has default values, these default values come from mkbootimg when
769 * the boot image is flashed using fastboot flash:raw
770 */
771 update_ker_tags_rdisk_addr(hdr);
772
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700773 /* Get virtual addresses since the hdr saves physical addresses. */
774 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
775 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
776 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700777
778 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
779 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
780
781 /* Check if the addresses in the header are valid. */
782 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
783 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
784 {
785 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
786 return -1;
787 }
788
789#ifndef DEVICE_TREE
790 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
791 {
792 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
793 return -1;
794 }
795#endif
796
Shashank Mittalcd98d472011-08-02 14:29:24 -0700797 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300798 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
799 (int) target_use_signed_kernel(),
800 device.is_unlocked,
801 device.is_tampered);
802
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700803 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800804 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700805 offset = 0;
806
Shashank Mittalcd98d472011-08-02 14:29:24 -0700807 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700808
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800809#if DEVICE_TREE
810 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
811 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700812
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700813 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700814 {
815 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
816 return -1;
817 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800818#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530819 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700820
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700821#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700822
Amol Jadib6be5c12012-11-14 13:39:51 -0800823 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700824 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800825
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700826 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
827 {
828 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
829 return -1;
830 }
831
Shashank Mittalcd98d472011-08-02 14:29:24 -0700832 /* Read image without signature */
833 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
834 {
835 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
836 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800837 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700838
Amol Jadib6be5c12012-11-14 13:39:51 -0800839 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700840 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800841
Shashank Mittalcd98d472011-08-02 14:29:24 -0700842 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700843
844 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
845 {
846 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
847 return -1;
848 }
849
Shashank Mittalcd98d472011-08-02 14:29:24 -0700850 /* Read signature */
851 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
852 {
853 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700854 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700855 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800856
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700857 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700858
Neeti Desai465491e2012-07-31 12:53:35 -0700859 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700860 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
861 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700862
Neeti Desai465491e2012-07-31 12:53:35 -0700863 #if DEVICE_TREE
864 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700865 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700866 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700867
Deepa Dinamani19648b42013-09-05 17:05:55 -0700868 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700869 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
870 return -1;
871 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700872
Neeti Desai465491e2012-07-31 12:53:35 -0700873 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700874 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700875 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
876 return -1;
877 }
878
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700879 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700880 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700881 {
882 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
883 return -1;
884 }
885
Joel Kingaa335dc2013-06-03 16:11:08 -0700886 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800887 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800888 /*
889 * If appended dev tree is found, update the atags with
890 * memory address to the DTB appended location on RAM.
891 * Else update with the atags address in the kernel header
892 */
893 void *dtb;
894 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700895 hdr->kernel_size,
896 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800897 if (!dtb) {
898 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
899 return -1;
900 }
Neeti Desai465491e2012-07-31 12:53:35 -0700901 }
902 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700903 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700904 else
905 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800906 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700907
Amol Jadib6be5c12012-11-14 13:39:51 -0800908 dprintf(INFO, "Loading boot image (%d): start\n",
909 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700910 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800911
912 offset = page_size;
913
914 /* Load kernel */
915 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700916 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
917 return -1;
918 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800919 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700920
Amol Jadib6be5c12012-11-14 13:39:51 -0800921 /* Load ramdisk */
922 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700923 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800924 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700925 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
926 return -1;
927 }
928 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800929 offset += ramdisk_actual;
930
931 dprintf(INFO, "Loading boot image (%d): done\n",
932 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700933 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700934
935 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800936 offset += second_actual;
937 /* Second image loading not implemented. */
938 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700939 }
940
941 #if DEVICE_TREE
942 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -0700943 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -0700944 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
945 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
946 return -1;
947 }
948 table = (struct dt_table*) dt_buf;
949
Deepa Dinamani19648b42013-09-05 17:05:55 -0700950 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700951 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
952 return -1;
953 }
954
Deepa Dinamani19648b42013-09-05 17:05:55 -0700955 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
956 if (!table)
957 return -1;
958
959 /* Read the entire device tree table into buffer */
960 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
961 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
962 return -1;
963 }
964
Joel Kingaa335dc2013-06-03 16:11:08 -0700965 /* Find index of device tree within device tree table */
966 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700967 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
968 return -1;
969 }
970
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700971 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700972 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700973 {
974 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
975 return -1;
976 }
977
Joel Kingaa335dc2013-06-03 16:11:08 -0700978 if(mmc_read(ptn + offset + dt_entry.offset,
979 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700980 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
981 return -1;
982 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300983 #ifdef TZ_SAVE_KERNEL_HASH
984 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
985 hdr->ramdisk_addr, ramdisk_actual,
986 ptn, offset, hdr->dt_size);
987 #endif /* TZ_SAVE_KERNEL_HASH */
988
Channagoud Kadabi35095622013-03-01 13:53:05 -0800989 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800990 /*
991 * If appended dev tree is found, update the atags with
992 * memory address to the DTB appended location on RAM.
993 * Else update with the atags address in the kernel header
994 */
995 void *dtb;
996 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700997 kernel_actual,
998 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800999 if (!dtb) {
1000 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1001 return -1;
1002 }
Neeti Desai465491e2012-07-31 12:53:35 -07001003 }
1004 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001005 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001006
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001007 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1008 target_load_ssd_keystore();
1009
Shashank Mittal23b8f422010-04-16 19:27:21 -07001010unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001011
Dima Zavin77e41f32013-03-06 16:10:43 -08001012 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001013 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001014 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1015
1016 return 0;
1017}
1018
Dima Zavin214cc642009-01-26 11:16:21 -08001019int boot_linux_from_flash(void)
1020{
1021 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001022 struct ptentry *ptn;
1023 struct ptable *ptable;
1024 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001025
Shashank Mittalcd98d472011-08-02 14:29:24 -07001026 unsigned char *image_addr = 0;
1027 unsigned kernel_actual;
1028 unsigned ramdisk_actual;
1029 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001030 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001031
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001032#if DEVICE_TREE
1033 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001034 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001035 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001036 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001037#endif
1038
David Ng183a7422009-12-07 14:55:21 -08001039 if (target_is_emmc_boot()) {
1040 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1041 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1042 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1043 return -1;
1044 }
1045 goto continue_boot;
1046 }
1047
Dima Zavin214cc642009-01-26 11:16:21 -08001048 ptable = flash_get_ptable();
1049 if (ptable == NULL) {
1050 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1051 return -1;
1052 }
1053
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001054 if(!boot_into_recovery)
1055 {
1056 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001057
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001058 if (ptn == NULL) {
1059 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1060 return -1;
1061 }
1062 }
1063 else
1064 {
1065 ptn = ptable_find(ptable, "recovery");
1066 if (ptn == NULL) {
1067 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1068 return -1;
1069 }
Dima Zavin214cc642009-01-26 11:16:21 -08001070 }
1071
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001072 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001073 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1074 return -1;
1075 }
Dima Zavin214cc642009-01-26 11:16:21 -08001076
1077 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001078 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001079 return -1;
1080 }
1081
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001082 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001083 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 -08001084 return -1;
1085 }
1086
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001087 /*
1088 * Update the kernel/ramdisk/tags address if the boot image header
1089 * has default values, these default values come from mkbootimg when
1090 * the boot image is flashed using fastboot flash:raw
1091 */
1092 update_ker_tags_rdisk_addr(hdr);
1093
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001094 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001095 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1096 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1097 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1098
1099 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1100 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1101
1102 /* Check if the addresses in the header are valid. */
1103 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1104 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1105 {
1106 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1107 return -1;
1108 }
1109
1110#ifndef DEVICE_TREE
1111 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1112 {
1113 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1114 return -1;
1115 }
1116#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001117
Shashank Mittalcd98d472011-08-02 14:29:24 -07001118 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001119 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001120 {
1121 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001122 offset = 0;
1123
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001124#if DEVICE_TREE
1125 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1126 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001127
1128 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1129 {
1130 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1131 return -1;
1132 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001133#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001134 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001135#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001136
Amol Jadib6be5c12012-11-14 13:39:51 -08001137 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001138 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001139
Shashank Mittalcd98d472011-08-02 14:29:24 -07001140 /* Read image without signature */
1141 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1142 {
1143 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1144 return -1;
1145 }
Dima Zavin214cc642009-01-26 11:16:21 -08001146
Amol Jadib6be5c12012-11-14 13:39:51 -08001147 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001148 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001149
Shashank Mittalcd98d472011-08-02 14:29:24 -07001150 offset = imagesize_actual;
1151 /* Read signature */
1152 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1153 {
1154 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001155 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001156 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001157
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001158 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001159
1160 /* Move kernel and ramdisk to correct address */
1161 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1162 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001163#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001164 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001165 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001166 {
1167 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1168 return -1;
1169 }
1170
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001171 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1172#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001173
1174 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001175 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001176 {
1177 write_device_info_flash(&device);
1178 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301179#if USE_PCOM_SECBOOT
1180 set_tamper_flag(device.is_tampered);
1181#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001182 }
1183 else
1184 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001185 offset = page_size;
1186
Amol Jadib6be5c12012-11-14 13:39:51 -08001187 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1188 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1189 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1190
1191 dprintf(INFO, "Loading boot image (%d): start\n",
1192 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001193 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001194
1195 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001196 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1197 return -1;
1198 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001199 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001200
Amol Jadib6be5c12012-11-14 13:39:51 -08001201 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001202 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1203 return -1;
1204 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001205 offset += ramdisk_actual;
1206
1207 dprintf(INFO, "Loading boot image (%d): done\n",
1208 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001209 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001210
1211 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001212 offset += second_actual;
1213 /* Second image loading not implemented. */
1214 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001215 }
1216
1217#if DEVICE_TREE
1218 if(hdr->dt_size != 0) {
1219
1220 /* Read the device tree table into buffer */
1221 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1222 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1223 return -1;
1224 }
1225
1226 table = (struct dt_table*) dt_buf;
1227
Deepa Dinamani19648b42013-09-05 17:05:55 -07001228 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001229 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1230 return -1;
1231 }
1232
Deepa Dinamani19648b42013-09-05 17:05:55 -07001233 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1234 if (!table)
1235 return -1;
1236
1237 /* Read the entire device tree table into buffer */
1238 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1239 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1240 return -1;
1241 }
1242
1243
Joel Kingaa335dc2013-06-03 16:11:08 -07001244 /* Find index of device tree within device tree table */
1245 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001246 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1247 return -1;
1248 }
1249
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001250 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001251 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001252 {
1253 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1254 return -1;
1255 }
1256
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001257 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001258 if(flash_read(ptn, offset + dt_entry.offset,
1259 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001260 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1261 return -1;
1262 }
1263 }
1264#endif
1265
Shashank Mittalcd98d472011-08-02 14:29:24 -07001266 }
David Ng183a7422009-12-07 14:55:21 -08001267continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001268
Dima Zavin214cc642009-01-26 11:16:21 -08001269 /* TODO: create/pass atags to kernel */
1270
Ajay Dudanie28a6072011-07-01 13:59:46 -07001271 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001272 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001273 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1274
1275 return 0;
1276}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001277
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001278BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001279void write_device_info_mmc(device_info *dev)
1280{
1281 struct device_info *info = (void*) info_buf;
1282 unsigned long long ptn = 0;
1283 unsigned long long size;
1284 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001285 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001286
1287 index = partition_get_index("aboot");
1288 ptn = partition_get_offset(index);
1289 if(ptn == 0)
1290 {
1291 return;
1292 }
1293
1294 size = partition_get_size(index);
1295
1296 memcpy(info, dev, sizeof(device_info));
1297
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001298 blocksize = mmc_get_device_blocksize();
1299
1300 if(mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf))
Shashank Mittal162244e2011-08-08 19:01:25 -07001301 {
1302 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1303 return;
1304 }
1305}
1306
1307void read_device_info_mmc(device_info *dev)
1308{
1309 struct device_info *info = (void*) info_buf;
1310 unsigned long long ptn = 0;
1311 unsigned long long size;
1312 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001313 uint32_t blocksize;
Shashank Mittal162244e2011-08-08 19:01:25 -07001314
1315 index = partition_get_index("aboot");
1316 ptn = partition_get_offset(index);
1317 if(ptn == 0)
1318 {
1319 return;
1320 }
1321
1322 size = partition_get_size(index);
1323
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001324 blocksize = mmc_get_device_blocksize();
1325
1326 if(mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize))
Shashank Mittal162244e2011-08-08 19:01:25 -07001327 {
1328 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1329 return;
1330 }
1331
1332 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1333 {
1334 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1335 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001336 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001337 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001338
1339 write_device_info_mmc(info);
1340 }
1341 memcpy(dev, info, sizeof(device_info));
1342}
1343
1344void write_device_info_flash(device_info *dev)
1345{
1346 struct device_info *info = (void *) info_buf;
1347 struct ptentry *ptn;
1348 struct ptable *ptable;
1349
1350 ptable = flash_get_ptable();
1351 if (ptable == NULL)
1352 {
1353 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1354 return;
1355 }
1356
1357 ptn = ptable_find(ptable, "devinfo");
1358 if (ptn == NULL)
1359 {
1360 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1361 return;
1362 }
1363
1364 memcpy(info, dev, sizeof(device_info));
1365
1366 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1367 {
1368 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1369 return;
1370 }
1371}
1372
1373void read_device_info_flash(device_info *dev)
1374{
1375 struct device_info *info = (void*) info_buf;
1376 struct ptentry *ptn;
1377 struct ptable *ptable;
1378
1379 ptable = flash_get_ptable();
1380 if (ptable == NULL)
1381 {
1382 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1383 return;
1384 }
1385
1386 ptn = ptable_find(ptable, "devinfo");
1387 if (ptn == NULL)
1388 {
1389 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1390 return;
1391 }
1392
1393 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1394 {
1395 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1396 return;
1397 }
1398
1399 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1400 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001401 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1402 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001403 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001404 write_device_info_flash(info);
1405 }
1406 memcpy(dev, info, sizeof(device_info));
1407}
1408
1409void write_device_info(device_info *dev)
1410{
1411 if(target_is_emmc_boot())
1412 {
1413 write_device_info_mmc(dev);
1414 }
1415 else
1416 {
1417 write_device_info_flash(dev);
1418 }
1419}
1420
1421void read_device_info(device_info *dev)
1422{
1423 if(target_is_emmc_boot())
1424 {
1425 read_device_info_mmc(dev);
1426 }
1427 else
1428 {
1429 read_device_info_flash(dev);
1430 }
1431}
1432
1433void reset_device_info()
1434{
1435 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001436 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001437 write_device_info(&device);
1438}
1439
1440void set_device_root()
1441{
1442 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001443 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001444 write_device_info(&device);
1445}
1446
Amol Jadicb524072012-08-09 16:40:18 -07001447#if DEVICE_TREE
1448int copy_dtb(uint8_t *boot_image_start)
1449{
1450 uint32 dt_image_offset = 0;
1451 uint32_t n;
1452 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001453 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001454 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001455
1456 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1457
Amol Jadicb524072012-08-09 16:40:18 -07001458 if(hdr->dt_size != 0) {
1459
1460 /* add kernel offset */
1461 dt_image_offset += page_size;
1462 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1463 dt_image_offset += n;
1464
1465 /* add ramdisk offset */
1466 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1467 dt_image_offset += n;
1468
1469 /* add second offset */
1470 if(hdr->second_size != 0) {
1471 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1472 dt_image_offset += n;
1473 }
1474
1475 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001476 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001477
Deepa Dinamani19648b42013-09-05 17:05:55 -07001478 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001479 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1480 return -1;
1481 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001482 /* Find index of device tree within device tree table */
1483 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001484 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1485 return -1;
1486 }
1487
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001488 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001489 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001490 {
1491 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1492 return -1;
1493 }
1494
Amol Jadicb524072012-08-09 16:40:18 -07001495 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001496 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001497 boot_image_start + dt_image_offset + dt_entry.offset,
1498 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001499 } else
1500 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001501
1502 /* Everything looks fine. Return success. */
1503 return 0;
1504}
1505#endif
1506
Brian Swetland9c4c0752009-01-25 16:23:50 -08001507void cmd_boot(const char *arg, void *data, unsigned sz)
1508{
1509 unsigned kernel_actual;
1510 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001511 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001512 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001513 int ret = 0;
1514 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001515
1516 if (sz < sizeof(hdr)) {
1517 fastboot_fail("invalid bootimage header");
1518 return;
1519 }
1520
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001521 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001522
1523 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001524 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001525
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001526 if(target_is_emmc_boot() && hdr->page_size) {
1527 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001528 page_mask = page_size - 1;
1529 }
1530
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001531 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1532 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1533
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001534 /*
1535 * Update the kernel/ramdisk/tags address if the boot image header
1536 * has default values, these default values come from mkbootimg when
1537 * the boot image is flashed using fastboot flash:raw
1538 */
1539 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001540
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001541 /* Get virtual addresses since the hdr saves physical addresses. */
1542 hdr->kernel_addr = VA(hdr->kernel_addr);
1543 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1544 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001545
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001546 /* Check if the addresses in the header are valid. */
1547 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1548 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1549 {
1550 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001551 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001552 }
1553
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001554 /* sz should have atleast raw boot image */
1555 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001556 fastboot_fail("incomplete bootimage");
1557 return;
1558 }
1559
Amol Jadicb524072012-08-09 16:40:18 -07001560#if DEVICE_TREE
1561 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001562 ret = copy_dtb(data);
1563
1564 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001565#else
1566 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1567 {
1568 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001569 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001570 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001571#endif
1572
1573 /* Load ramdisk & kernel */
1574 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1575 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1576
1577#if DEVICE_TREE
1578 /*
1579 * If dtb is not found look for appended DTB in the kernel.
1580 * If appended dev tree is found, update the atags with
1581 * memory address to the DTB appended location on RAM.
1582 * Else update with the atags address in the kernel header
1583 */
1584 if (!dtb_copied) {
1585 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001586 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1587 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001588 if (!dtb) {
1589 fastboot_fail("dtb not found");
1590 return;
1591 }
Amol Jadicb524072012-08-09 16:40:18 -07001592 }
1593#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001594
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001595#ifndef DEVICE_TREE
1596 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1597 {
1598 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08001599 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001600 }
1601#endif
1602
Brian Swetland9c4c0752009-01-25 16:23:50 -08001603 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07001604 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001605
Dima Zavin77e41f32013-03-06 16:10:43 -08001606 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001607 (const char*) hdr->cmdline, board_machtype(),
1608 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001609}
1610
Dima Zavin214cc642009-01-26 11:16:21 -08001611void cmd_erase(const char *arg, void *data, unsigned sz)
1612{
1613 struct ptentry *ptn;
1614 struct ptable *ptable;
1615
1616 ptable = flash_get_ptable();
1617 if (ptable == NULL) {
1618 fastboot_fail("partition table doesn't exist");
1619 return;
1620 }
1621
1622 ptn = ptable_find(ptable, arg);
1623 if (ptn == NULL) {
1624 fastboot_fail("unknown partition name");
1625 return;
1626 }
1627
1628 if (flash_erase(ptn)) {
1629 fastboot_fail("failed to erase partition");
1630 return;
1631 }
1632 fastboot_okay("");
1633}
1634
Bikas Gurungd48bd242010-09-04 19:54:32 -07001635
1636void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1637{
Kun Liang2f1601a2013-08-12 16:29:54 +08001638 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001639 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08001640 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001641 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001642
Kinson Chikf1a43512011-07-14 11:28:39 -07001643 index = partition_get_index(arg);
1644 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08001645 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001646
Kinson Chikf1a43512011-07-14 11:28:39 -07001647 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001648 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001649 return;
1650 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001651
Oliver Wangcee448d2013-10-22 18:40:13 +08001652#if MMC_SDHCI_SUPPORT
1653 if (mmc_erase_card(ptn, size)) {
1654 fastboot_fail("failed to erase partition\n");
1655 return;
1656 }
1657#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001658 size = partition_get_size(index);
1659 if (size > DEFAULT_ERASE_SIZE)
1660 size = DEFAULT_ERASE_SIZE;
1661
neetidb4b24d62012-01-20 12:13:09 -08001662 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001663 0 in first several blocks */
1664 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001665 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001666 return;
1667 }
Oliver Wangcee448d2013-10-22 18:40:13 +08001668#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001669 fastboot_okay("");
1670}
1671
1672
Ajay Dudani5c761132011-04-07 20:19:04 -07001673void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001674{
1675 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001676 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001677 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001678
Greg Grisco6e754772011-06-23 12:19:39 -07001679 if (!strcmp(arg, "partition"))
1680 {
1681 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001682 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001683 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001684 return;
1685 }
1686 }
Greg Grisco6e754772011-06-23 12:19:39 -07001687 else
1688 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001689 index = partition_get_index(arg);
1690 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001691 if(ptn == 0) {
1692 fastboot_fail("partition table doesn't exist");
1693 return;
1694 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001695
Greg Grisco6e754772011-06-23 12:19:39 -07001696 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1697 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1698 fastboot_fail("image is not a boot image");
1699 return;
1700 }
1701 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001702
Kinson Chikf1a43512011-07-14 11:28:39 -07001703 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001704 if (ROUND_TO_PAGE(sz,511) > size) {
1705 fastboot_fail("size too large");
1706 return;
1707 }
1708 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1709 fastboot_fail("flash write failure");
1710 return;
1711 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001712 }
1713 fastboot_okay("");
1714 return;
1715}
1716
Ajay Dudani5c761132011-04-07 20:19:04 -07001717void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1718{
1719 unsigned int chunk;
1720 unsigned int chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001721 uint32_t *fill_buf = NULL;
1722 uint32_t fill_val;
1723 uint32_t chunk_blk_cnt = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001724 sparse_header_t *sparse_header;
1725 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001726 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001727 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301728 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001729 int index = INVALID_PTN;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001730 int i;
Ajay Dudani5c761132011-04-07 20:19:04 -07001731
Kinson Chikf1a43512011-07-14 11:28:39 -07001732 index = partition_get_index(arg);
1733 ptn = partition_get_offset(index);
1734 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001735 fastboot_fail("partition table doesn't exist");
1736 return;
1737 }
1738
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301739 size = partition_get_size(index);
1740 if (ROUND_TO_PAGE(sz,511) > size) {
1741 fastboot_fail("size too large");
1742 return;
1743 }
1744
Ajay Dudani5c761132011-04-07 20:19:04 -07001745 /* Read and skip over sparse image header */
1746 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001747 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1748 fastboot_fail("size too large");
1749 return;
1750 }
1751
Ajay Dudani5c761132011-04-07 20:19:04 -07001752 data += sparse_header->file_hdr_sz;
1753 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1754 {
1755 /* Skip the remaining bytes in a header that is longer than
1756 * we expected.
1757 */
1758 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1759 }
1760
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001761 dprintf (SPEW, "=== Sparse Image Header ===\n");
1762 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1763 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1764 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1765 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1766 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1767 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1768 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1769 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001770
1771 /* Start processing chunks */
1772 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1773 {
1774 /* Read and skip over chunk header */
1775 chunk_header = (chunk_header_t *) data;
1776 data += sizeof(chunk_header_t);
1777
1778 dprintf (SPEW, "=== Chunk Header ===\n");
1779 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1780 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1781 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1782
1783 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1784 {
1785 /* Skip the remaining bytes in a header that is longer than
1786 * we expected.
1787 */
1788 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1789 }
1790
1791 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1792 switch (chunk_header->chunk_type)
1793 {
1794 case CHUNK_TYPE_RAW:
1795 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1796 chunk_data_sz))
1797 {
1798 fastboot_fail("Bogus chunk size for chunk type Raw");
1799 return;
1800 }
1801
Ajay Dudaniab18f022011-05-12 14:39:22 -07001802 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1803 chunk_data_sz,
1804 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001805 {
1806 fastboot_fail("flash write failure");
1807 return;
1808 }
1809 total_blocks += chunk_header->chunk_sz;
1810 data += chunk_data_sz;
1811 break;
1812
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001813 case CHUNK_TYPE_FILL:
1814 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1815 sizeof(uint32_t)))
1816 {
1817 fastboot_fail("Bogus chunk size for chunk type FILL");
1818 return;
1819 }
1820
1821 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
1822 if (!fill_buf)
1823 {
1824 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
1825 return;
1826 }
1827
1828 fill_val = *(uint32_t *)data;
1829 data = (char *) data + sizeof(uint32_t);
1830 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
1831
1832 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
1833 {
1834 fill_buf[i] = fill_val;
1835 }
1836
1837 for (i = 0; i < chunk_blk_cnt; i++)
1838 {
1839 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1840 sparse_header->blk_sz,
1841 fill_buf))
1842 {
1843 fastboot_fail("flash write failure");
1844 free(fill_buf);
1845 return;
1846 }
1847
1848 total_blocks++;
1849 }
1850
1851 free(fill_buf);
1852 break;
1853
Ajay Dudani5c761132011-04-07 20:19:04 -07001854 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001855 total_blocks += chunk_header->chunk_sz;
1856 break;
1857
Ajay Dudani5c761132011-04-07 20:19:04 -07001858 case CHUNK_TYPE_CRC:
1859 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1860 {
1861 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1862 return;
1863 }
1864 total_blocks += chunk_header->chunk_sz;
1865 data += chunk_data_sz;
1866 break;
1867
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001868 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07001869 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07001870 fastboot_fail("Unknown chunk type");
1871 return;
1872 }
1873 }
1874
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001875 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1876 total_blocks, sparse_header->total_blks);
1877
1878 if(total_blocks != sparse_header->total_blks)
1879 {
1880 fastboot_fail("sparse image write failure");
1881 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001882
1883 fastboot_okay("");
1884 return;
1885}
1886
1887void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1888{
1889 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001890 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1891 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001892
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001893#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001894 int ret=0;
1895 uint32 major_version=0;
1896 uint32 minor_version=0;
1897
1898 ret = scm_svc_version(&major_version,&minor_version);
1899 if(!ret)
1900 {
1901 if(major_version >= 2)
1902 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001903 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001904 {
1905 ret = encrypt_scm((uint32 **) &data, &sz);
1906 if (ret != 0) {
1907 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1908 return;
1909 }
1910
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001911 /* Protect only for SSD */
1912 if (!strcmp(arg, "ssd")) {
1913 ret = scm_protect_keystore((uint32 *) data, sz);
1914 if (ret != 0) {
1915 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1916 return;
1917 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001918 }
1919 }
1920 else
1921 {
1922 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1923 if(ret != 0)
1924 {
1925 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1926 return;
1927 }
1928 }
1929 }
1930 else
1931 {
1932 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1933 magic_number[1] == DECRYPT_MAGIC_1)
1934 {
1935 ret = decrypt_scm((uint32 **) &data, &sz);
1936 if (ret != 0) {
1937 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1938 return;
1939 }
1940 }
1941 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1942 magic_number[1] == ENCRYPT_MAGIC_1)
1943 {
1944 ret = encrypt_scm((uint32 **) &data, &sz);
1945 if (ret != 0) {
1946 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1947 return;
1948 }
1949 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001950 }
1951 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001952 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001953 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001954 dprintf(CRITICAL,"INVALID SVC Version\n");
1955 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001956 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001957#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001958
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001959 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001960 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1961 cmd_flash_mmc_img(arg, data, sz);
1962 else
1963 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001964 return;
1965}
1966
Dima Zavin214cc642009-01-26 11:16:21 -08001967void cmd_flash(const char *arg, void *data, unsigned sz)
1968{
1969 struct ptentry *ptn;
1970 struct ptable *ptable;
1971 unsigned extra = 0;
1972
1973 ptable = flash_get_ptable();
1974 if (ptable == NULL) {
1975 fastboot_fail("partition table doesn't exist");
1976 return;
1977 }
1978
1979 ptn = ptable_find(ptable, arg);
1980 if (ptn == NULL) {
1981 fastboot_fail("unknown partition name");
1982 return;
1983 }
1984
1985 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1986 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1987 fastboot_fail("image is not a boot image");
1988 return;
1989 }
1990 }
1991
Amol Jadi5c61a952012-05-04 17:05:35 -07001992 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001993 || !strcmp(ptn->name, "userdata")
1994 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08001995 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08001996 || !strcmp(ptn->name, "modem"))
1997 {
1998 if (memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))
Deepa Dinamaniea177912013-04-30 15:51:10 -07001999 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08002000 else
2001 extra = 0;
2002 }
2003 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002004 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08002005
2006 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
2007 if (flash_write(ptn, extra, data, sz)) {
2008 fastboot_fail("flash write failure");
2009 return;
2010 }
2011 dprintf(INFO, "partition '%s' updated\n", ptn->name);
2012 fastboot_okay("");
2013}
2014
2015void cmd_continue(const char *arg, void *data, unsigned sz)
2016{
2017 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002018 fastboot_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002019 if (target_is_emmc_boot())
2020 {
2021 boot_linux_from_mmc();
2022 }
2023 else
2024 {
2025 boot_linux_from_flash();
2026 }
Dima Zavin214cc642009-01-26 11:16:21 -08002027}
2028
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002029void cmd_reboot(const char *arg, void *data, unsigned sz)
2030{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002031 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002032 fastboot_okay("");
2033 reboot_device(0);
2034}
2035
2036void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2037{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002038 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002039 fastboot_okay("");
2040 reboot_device(FASTBOOT_MODE);
2041}
2042
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002043void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2044{
2045 dprintf(INFO, "Enabling charger screen check\n");
2046 device.charger_screen_enabled = 1;
2047 write_device_info(&device);
2048 fastboot_okay("");
2049}
2050
2051void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2052{
2053 dprintf(INFO, "Disabling charger screen check\n");
2054 device.charger_screen_enabled = 0;
2055 write_device_info(&device);
2056 fastboot_okay("");
2057}
2058
Shashank Mittal162244e2011-08-08 19:01:25 -07002059void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2060{
2061 if(!device.is_unlocked)
2062 {
2063 device.is_unlocked = 1;
2064 write_device_info(&device);
2065 }
2066 fastboot_okay("");
2067}
2068
Shashank Mittala0032282011-08-26 14:50:11 -07002069void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2070{
2071 char response[64];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002072 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002073 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002074 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2075 fastboot_info(response);
2076 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002077 fastboot_info(response);
2078 fastboot_okay("");
2079}
2080
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002081void cmd_preflash(const char *arg, void *data, unsigned sz)
2082{
2083 fastboot_okay("");
2084}
2085
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302086static struct fbimage logo_header = {0};
2087struct fbimage* splash_screen_flash();
2088
2089int splash_screen_check_header(struct fbimage *logo)
2090{
2091 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2092 return -1;
2093 if (logo->header.width == 0 || logo->header.height == 0)
2094 return -1;
2095 return 0;
2096}
2097
2098struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002099{
2100 struct ptentry *ptn;
2101 struct ptable *ptable;
2102 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302103 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002104
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302105
2106 ptable = flash_get_ptable();
2107 if (ptable == NULL) {
2108 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2109 return NULL;
2110 }
2111 ptn = ptable_find(ptable, "splash");
2112 if (ptn == NULL) {
2113 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2114 return NULL;
2115 }
2116
2117 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2118 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2119 return NULL;
2120 }
2121
2122 if (splash_screen_check_header(logo)) {
2123 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2124 return NULL;
2125 }
2126
2127 fb_display = fbcon_display();
2128 if (fb_display) {
2129 uint8_t *base = (uint8_t *) fb_display->base;
2130 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
2131 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002132 }
2133
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302134 if (flash_read(ptn + sizeof(logo->header), 0,
2135 base,
2136 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2137 fbcon_clear();
2138 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2139 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002140 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302141 logo->image = base;
2142 }
2143
2144 return logo;
2145}
2146
2147struct fbimage* splash_screen_mmc()
2148{
2149 int index = INVALID_PTN;
2150 unsigned long long ptn = 0;
2151 struct fbcon_config *fb_display = NULL;
2152 struct fbimage *logo = &logo_header;
2153
2154 index = partition_get_index("splash");
2155 if (index == 0) {
2156 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2157 return NULL;
2158 }
2159
2160 ptn = partition_get_offset(index);
2161 if (ptn == 0) {
2162 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2163 return NULL;
2164 }
2165
2166 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2167 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2168 return NULL;
2169 }
2170
2171 if (splash_screen_check_header(logo)) {
2172 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2173 return NULL;
2174 }
2175
2176 fb_display = fbcon_display();
2177 if (fb_display) {
2178 uint8_t *base = (uint8_t *) fb_display->base;
2179 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2180 base += LOGO_IMG_OFFSET;
2181
2182 if (mmc_read(ptn + sizeof(logo->header),
2183 base,
2184 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2185 fbcon_clear();
2186 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2187 return NULL;
2188 }
2189
2190 logo->image = base;
2191 }
2192
2193 return logo;
2194}
2195
2196
2197struct fbimage* fetch_image_from_partition()
2198{
2199 if (target_is_emmc_boot()) {
2200 return splash_screen_mmc();
2201 } else {
2202 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002203 }
2204}
2205
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002206/* Get the size from partiton name */
2207static void get_partition_size(const char *arg, char *response)
2208{
2209 uint64_t ptn = 0;
2210 uint64_t size;
2211 int index = INVALID_PTN;
2212
2213 index = partition_get_index(arg);
2214
2215 if (index == INVALID_PTN)
2216 {
2217 dprintf(CRITICAL, "Invalid partition index\n");
2218 return;
2219 }
2220
2221 ptn = partition_get_offset(index);
2222
2223 if(!ptn)
2224 {
2225 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2226 return;
2227 }
2228
2229 size = partition_get_size(index);
2230
2231 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2232 return;
2233}
2234
2235/*
2236 * Publish the partition type & size info
2237 * fastboot getvar will publish the required information.
2238 * fastboot getvar partition_size:<partition_name>: partition size in hex
2239 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2240 */
2241static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2242{
2243 uint8_t i;
2244
2245 for (i = 0; i < num_parts; i++) {
2246 get_partition_size(info[i].part_name, info[i].size_response);
2247
2248 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2249 {
2250 dprintf(CRITICAL, "partition size name truncated\n");
2251 return;
2252 }
2253 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2254 {
2255 dprintf(CRITICAL, "partition type name truncated\n");
2256 return;
2257 }
2258
2259 /* publish partition size & type info */
2260 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2261 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2262 }
2263}
2264
Amol Jadi5edf3552013-07-23 14:15:34 -07002265/* register commands and variables for fastboot */
2266void aboot_fastboot_register_commands(void)
2267{
2268 if (target_is_emmc_boot())
2269 {
2270 fastboot_register("flash:", cmd_flash_mmc);
2271 fastboot_register("erase:", cmd_erase_mmc);
2272 }
2273 else
2274 {
2275 fastboot_register("flash:", cmd_flash);
2276 fastboot_register("erase:", cmd_erase);
2277 }
2278
2279 fastboot_register("boot", cmd_boot);
2280 fastboot_register("continue", cmd_continue);
2281 fastboot_register("reboot", cmd_reboot);
2282 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2283 fastboot_register("oem unlock", cmd_oem_unlock);
2284 fastboot_register("oem device-info", cmd_oem_devinfo);
2285 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002286 fastboot_register("oem enable-charger-screen",
2287 cmd_oem_enable_charger_screen);
2288 fastboot_register("oem disable-charger-screen",
2289 cmd_oem_disable_charger_screen);
Amol Jadi5edf3552013-07-23 14:15:34 -07002290 /* publish variables and their values */
2291 fastboot_publish("product", TARGET(BOARD));
2292 fastboot_publish("kernel", "lk");
2293 fastboot_publish("serialno", sn_buf);
2294
2295 /*
2296 * partition info is supported only for emmc partitions
2297 * Calling this for NAND prints some error messages which
2298 * is harmless but misleading. Avoid calling this for NAND
2299 * devices.
2300 */
2301 if (target_is_emmc_boot())
2302 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2303
2304 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002305 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2306 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002307 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002308 /* Is the charger screen check enabled */
2309 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2310 device.charger_screen_enabled);
2311 fastboot_publish("charger-screen-enabled",
2312 (const char *) charger_screen_enabled);
Amol Jadi5edf3552013-07-23 14:15:34 -07002313}
2314
Brian Swetland9c4c0752009-01-25 16:23:50 -08002315void aboot_init(const struct app_descriptor *app)
2316{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002317 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002318 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002319
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002320 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002321 if (target_is_emmc_boot())
2322 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002323 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002324 page_mask = page_size - 1;
2325 }
2326 else
2327 {
2328 page_size = flash_page_size();
2329 page_mask = page_size - 1;
2330 }
2331
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002332 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2333
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002334 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002335
Greg Griscod6250552011-06-29 14:40:23 -07002336 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002337 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002338
Dhaval Patel223ec952013-07-18 14:49:44 -07002339 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2340
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002341 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002342 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002343 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002344 dprintf(ALWAYS,"dload mode key sequence detected\n");
2345 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002346 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002347 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002348 }
2349 else
2350 {
2351 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002352 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002353 }
2354 boot_into_fastboot = true;
2355 }
2356 if (!boot_into_fastboot)
2357 {
2358 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2359 boot_into_recovery = 1;
2360 if (!boot_into_recovery &&
2361 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002362 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002363 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002364 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002365 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002366 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002367 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002368
Ajay Dudani77421292010-10-27 19:34:06 -07002369 reboot_mode = check_reboot_mode();
2370 if (reboot_mode == RECOVERY_MODE) {
2371 boot_into_recovery = 1;
2372 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002373 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002374 }
2375
Pavel Nedev5d91d412013-04-29 11:34:24 +03002376 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002377 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002378 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002379 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002380 if(emmc_recovery_init())
2381 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2382 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002383 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002384 if((device.is_unlocked) || (device.is_tampered))
2385 {
2386 #ifdef TZ_TAMPER_FUSE
2387 set_tamper_fuse_cmd();
2388 #endif
2389 #if USE_PCOM_SECBOOT
2390 set_tamper_flag(device.is_tampered);
2391 #endif
2392 }
Shashank Mittala0032282011-08-26 14:50:11 -07002393 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002394 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002395 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002396 else
2397 {
2398 recovery_init();
2399 #if USE_PCOM_SECBOOT
2400 if((device.is_unlocked) || (device.is_tampered))
2401 set_tamper_flag(device.is_tampered);
2402 #endif
2403 boot_linux_from_flash();
2404 }
2405 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2406 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002407 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002408
Amol Jadi5edf3552013-07-23 14:15:34 -07002409 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002410
Amol Jadi5edf3552013-07-23 14:15:34 -07002411 /* register aboot specific fastboot commands */
2412 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002413
Amol Jadi5edf3552013-07-23 14:15:34 -07002414 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002415 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002416
2417 /* initialize and start fastboot */
2418 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002419}
2420
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002421uint32_t get_page_size()
2422{
2423 return page_size;
2424}
2425
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002426/*
2427 * Calculated and save hash (SHA256) for non-signed boot image.
2428 *
2429 * Hash the same data that is checked on the signed boot image.
2430 * Kernel and Ramdisk are already read to memory buffers.
2431 * Need to read the entire device-tree from mmc
2432 * since non-signed image only read the DT tags of the relevant platform.
2433 *
2434 * @param kernel_addr - kernel bufer
2435 * @param kernel_actual - kernel size in bytes
2436 * @param ramdisk_addr - ramdisk buffer
2437 * @param ramdisk_actual - ramdisk size
2438 * @param ptn - partition
2439 * @param dt_offset - device tree offset on mmc partition
2440 * @param dt_size
2441 *
2442 * @return int - 0 on success, negative value on failure.
2443 */
2444int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2445 void *ramdisk_addr, unsigned ramdisk_actual,
2446 unsigned long long ptn,
2447 unsigned dt_offset, unsigned dt_size)
2448{
2449 SHA256_CTX sha256_ctx;
2450 char digest[32]={0};
2451 char *buf = (char *)target_get_scratch_address();
2452 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2453 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2454
2455 SHA256_Init(&sha256_ctx);
2456
2457 /* Read Boot Header */
2458 if (mmc_read(ptn, buf, page_size))
2459 {
2460 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2461 return -1;
2462 }
2463 /* Read entire Device Tree */
2464 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2465 {
2466 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2467 return -1;
2468 }
2469 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2470 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2471 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2472 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2473
2474 SHA256_Final(digest, &sha256_ctx);
2475
2476 save_kernel_hash_cmd(digest);
2477 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2478
2479 return 0;
2480}
2481
Brian Swetland9c4c0752009-01-25 16:23:50 -08002482APP_START(aboot)
2483 .init = aboot_init,
2484APP_END