blob: 4a3011edd943ab4a1a84f1fa0c7236c7b85d06a3 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Kuogee Hsiehfb4b5c32013-12-20 08:34:10 -08005 * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabice737c82014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
43#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080044#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080045#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080046#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070047#include <target.h>
48#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070049#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070050#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070051#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070052#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070053#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030054#include <sha.h>
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"
Shashank Mittal162244e2011-08-08 19:01:25 -070069#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070070
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070071extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070072extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070073extern void target_uninit(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070074
75void write_device_info_mmc(device_info *dev);
76void write_device_info_flash(device_info *dev);
77
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070078#define EXPAND(NAME) #NAME
79#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070080
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080081#ifdef MEMBASE
82#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
83#else
David Ng183a7422009-12-07 14:55:21 -080084#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080085#endif
86
Deepa Dinamani0e163a42013-05-24 17:08:15 -070087#ifndef MEMSIZE
88#define MEMSIZE 1024*1024
89#endif
90
91#define MAX_TAGS_SIZE 1024
92
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080093#define RECOVERY_MODE 0x77665502
94#define FASTBOOT_MODE 0x77665500
95
Kun Liang2f1601a2013-08-12 16:29:54 +080096/* make 4096 as default size to ensure EFS,EXT4's erasing */
97#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsiehfb4b5c32013-12-20 08:34:10 -080098#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +080099
Channagoud Kadabice737c82014-06-18 17:41:01 -0700100#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
101
David Ng183a7422009-12-07 14:55:21 -0800102static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800103static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300104static const char *androidboot_mode = " androidboot.mode=";
Dhaval Patel223ec952013-07-18 14:49:44 -0700105static const char *display_cmdline = " mdss_mdp.panel=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800106static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800107static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700108static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300109static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800110
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800111static const char *baseband_apq = " androidboot.baseband=apq";
112static const char *baseband_msm = " androidboot.baseband=msm";
113static const char *baseband_csfb = " androidboot.baseband=csfb";
114static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700115static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700116static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800117static const char *baseband_dsda = " androidboot.baseband=dsda";
118static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800119static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800120
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700121static unsigned page_size = 0;
122static unsigned page_mask = 0;
123static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
124static bool boot_into_ffbm;
125
Shashank Mittalcd98d472011-08-02 14:29:24 -0700126/* Assuming unauthorized kernel image by default */
127static int auth_kernel_img = 0;
128
Unnati Gandhi15d08f92014-01-24 11:01:01 +0530129static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700130
Dima Zavin42168f22009-01-30 11:52:22 -0800131struct atag_ptbl_entry
132{
133 char name[16];
134 unsigned offset;
135 unsigned size;
136 unsigned flags;
137};
138
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700139/*
140 * Partition info, required to be published
141 * for fastboot
142 */
143struct getvar_partition_info {
144 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
145 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
146 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
147 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
148 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
149};
150
151/*
152 * Right now, we are publishing the info for only
153 * three partitions
154 */
155struct getvar_partition_info part_info[] =
156{
157 { "system" , "partition-size:", "partition-type:", "", "ext4" },
158 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
159 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
160};
161
162char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700163char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800164char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700165char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi15d08f92014-01-24 11:01:01 +0530166char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700167
Greg Griscod2471ef2011-07-14 13:00:42 -0700168extern int emmc_recovery_init(void);
169
Kinson Chik0b1c8162011-08-31 16:31:57 -0700170#if NO_KEYPAD_DRIVER
171extern int fastboot_trigger(void);
172#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700173
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700174static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
175{
176 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700177#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
178 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
179 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
180 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700181#endif
182}
183
Dima Zavin42168f22009-01-30 11:52:22 -0800184static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
185{
186 struct atag_ptbl_entry atag_ptn;
187
188 memcpy(atag_ptn.name, ptn->name, 16);
189 atag_ptn.name[15] = '\0';
190 atag_ptn.offset = ptn->start;
191 atag_ptn.size = ptn->length;
192 atag_ptn.flags = ptn->flags;
193 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
194 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
195}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800196
Neeti Desaie245d492012-06-01 12:52:13 -0700197unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800198{
David Ng183a7422009-12-07 14:55:21 -0800199 int cmdline_len = 0;
200 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800201 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700202 int pause_at_bootup = 0;
Pavel Nedev5614d222013-06-17 18:01:02 +0300203 bool gpt_exists = partition_gpt_exists();
Dima Zavin42168f22009-01-30 11:52:22 -0800204
Brian Swetland9c4c0752009-01-25 16:23:50 -0800205 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800206 cmdline_len = strlen(cmdline);
207 have_cmdline = 1;
208 }
209 if (target_is_emmc_boot()) {
210 cmdline_len += strlen(emmc_cmdline);
211 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800212
213 cmdline_len += strlen(usb_sn_cmdline);
214 cmdline_len += strlen(sn_buf);
215
Pavel Nedev5614d222013-06-17 18:01:02 +0300216 if (boot_into_recovery && gpt_exists)
217 cmdline_len += strlen(secondary_gpt_enable);
218
Pavel Nedev328ac822013-04-05 15:25:11 +0300219 if (boot_into_ffbm) {
220 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700221 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800222 /* reduce kernel console messages to speed-up boot */
223 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700224 } else if (device.charger_screen_enabled &&
225 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700226 pause_at_bootup = 1;
227 cmdline_len += strlen(battchg_pause);
228 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800229
Shashank Mittalcd98d472011-08-02 14:29:24 -0700230 if(target_use_signed_kernel() && auth_kernel_img) {
231 cmdline_len += strlen(auth_kernel);
232 }
233
Ajay Dudanid04110c2011-01-17 23:55:07 -0800234 /* Determine correct androidboot.baseband to use */
235 switch(target_baseband())
236 {
237 case BASEBAND_APQ:
238 cmdline_len += strlen(baseband_apq);
239 break;
240
241 case BASEBAND_MSM:
242 cmdline_len += strlen(baseband_msm);
243 break;
244
245 case BASEBAND_CSFB:
246 cmdline_len += strlen(baseband_csfb);
247 break;
248
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800249 case BASEBAND_SVLTE2A:
250 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800251 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700252
253 case BASEBAND_MDM:
254 cmdline_len += strlen(baseband_mdm);
255 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700256
257 case BASEBAND_SGLTE:
258 cmdline_len += strlen(baseband_sglte);
259 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530260
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800261 case BASEBAND_SGLTE2:
262 cmdline_len += strlen(baseband_sglte2);
263 break;
264
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530265 case BASEBAND_DSDA:
266 cmdline_len += strlen(baseband_dsda);
267 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800268
269 case BASEBAND_DSDA2:
270 cmdline_len += strlen(baseband_dsda2);
271 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800272 }
273
Dhaval Patel223ec952013-07-18 14:49:44 -0700274 if (target_display_panel_node(display_panel_buf, MAX_PANEL_BUF_SIZE) &&
275 strlen(display_panel_buf))
276 {
277 cmdline_len += strlen(display_cmdline);
278 cmdline_len += strlen(display_panel_buf);
279 }
280
David Ng183a7422009-12-07 14:55:21 -0800281 if (cmdline_len > 0) {
282 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700283 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
284 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700285
Amol Jadi168b7712012-03-06 16:15:00 -0800286 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700287 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800288 if (have_cmdline) {
289 src = cmdline;
290 while ((*dst++ = *src++));
291 }
292 if (target_is_emmc_boot()) {
293 src = emmc_cmdline;
294 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700295 have_cmdline = 1;
296 while ((*dst++ = *src++));
297 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800298
299 src = usb_sn_cmdline;
300 if (have_cmdline) --dst;
301 have_cmdline = 1;
302 while ((*dst++ = *src++));
303 src = sn_buf;
304 if (have_cmdline) --dst;
305 have_cmdline = 1;
306 while ((*dst++ = *src++));
307
Pavel Nedev5614d222013-06-17 18:01:02 +0300308 if (boot_into_recovery && gpt_exists) {
309 src = secondary_gpt_enable;
310 if (have_cmdline) --dst;
311 while ((*dst++ = *src++));
312 }
313
Pavel Nedev328ac822013-04-05 15:25:11 +0300314 if (boot_into_ffbm) {
315 src = androidboot_mode;
316 if (have_cmdline) --dst;
317 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700318 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300319 if (have_cmdline) --dst;
320 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800321 src = loglevel;
322 if (have_cmdline) --dst;
323 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300324 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700325 src = battchg_pause;
326 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800327 while ((*dst++ = *src++));
328 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800329
Shashank Mittalcd98d472011-08-02 14:29:24 -0700330 if(target_use_signed_kernel() && auth_kernel_img) {
331 src = auth_kernel;
332 if (have_cmdline) --dst;
333 while ((*dst++ = *src++));
334 }
335
Ajay Dudanid04110c2011-01-17 23:55:07 -0800336 switch(target_baseband())
337 {
338 case BASEBAND_APQ:
339 src = baseband_apq;
340 if (have_cmdline) --dst;
341 while ((*dst++ = *src++));
342 break;
343
344 case BASEBAND_MSM:
345 src = baseband_msm;
346 if (have_cmdline) --dst;
347 while ((*dst++ = *src++));
348 break;
349
350 case BASEBAND_CSFB:
351 src = baseband_csfb;
352 if (have_cmdline) --dst;
353 while ((*dst++ = *src++));
354 break;
355
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800356 case BASEBAND_SVLTE2A:
357 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800358 if (have_cmdline) --dst;
359 while ((*dst++ = *src++));
360 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700361
362 case BASEBAND_MDM:
363 src = baseband_mdm;
364 if (have_cmdline) --dst;
365 while ((*dst++ = *src++));
366 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700367
368 case BASEBAND_SGLTE:
369 src = baseband_sglte;
370 if (have_cmdline) --dst;
371 while ((*dst++ = *src++));
372 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530373
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800374 case BASEBAND_SGLTE2:
375 src = baseband_sglte2;
376 if (have_cmdline) --dst;
377 while ((*dst++ = *src++));
378 break;
379
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530380 case BASEBAND_DSDA:
381 src = baseband_dsda;
382 if (have_cmdline) --dst;
383 while ((*dst++ = *src++));
384 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800385
386 case BASEBAND_DSDA2:
387 src = baseband_dsda2;
388 if (have_cmdline) --dst;
389 while ((*dst++ = *src++));
390 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800391 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700392
393 if (strlen(display_panel_buf)) {
394 src = display_cmdline;
395 if (have_cmdline) --dst;
396 while ((*dst++ = *src++));
397 src = display_panel_buf;
398 if (have_cmdline) --dst;
399 while ((*dst++ = *src++));
400 }
Neeti Desaie245d492012-06-01 12:52:13 -0700401 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700402
403
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800404 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700405 return cmdline_final;
406}
407
408unsigned *atag_core(unsigned *ptr)
409{
410 /* CORE */
411 *ptr++ = 2;
412 *ptr++ = 0x54410001;
413
414 return ptr;
415
416}
417
418unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
419 unsigned ramdisk_size)
420{
421 if (ramdisk_size) {
422 *ptr++ = 4;
423 *ptr++ = 0x54420005;
424 *ptr++ = (unsigned)ramdisk;
425 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800426 }
427
Neeti Desaie245d492012-06-01 12:52:13 -0700428 return ptr;
429}
430
431unsigned *atag_ptable(unsigned **ptr_addr)
432{
433 int i;
434 struct ptable *ptable;
435
436 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700437 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
438 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700439 *(*ptr_addr)++ = 0x4d534d70;
440 for (i = 0; i < ptable->count; ++i)
441 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
442 }
443
444 return (*ptr_addr);
445}
446
447unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
448{
449 int cmdline_length = 0;
450 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700451 char *dest;
452
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800453 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700454 n = (cmdline_length + 4) & (~3);
455
456 *ptr++ = (n / 4) + 2;
457 *ptr++ = 0x54410009;
458 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800459 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700460 ptr += (n / 4);
461
462 return ptr;
463}
464
465unsigned *atag_end(unsigned *ptr)
466{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800467 /* END */
468 *ptr++ = 0;
469 *ptr++ = 0;
470
Neeti Desaie245d492012-06-01 12:52:13 -0700471 return ptr;
472}
473
474void generate_atags(unsigned *ptr, const char *cmdline,
475 void *ramdisk, unsigned ramdisk_size)
476{
477
478 ptr = atag_core(ptr);
479 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
480 ptr = target_atag_mem(ptr);
481
482 /* Skip NAND partition ATAGS for eMMC boot */
483 if (!target_is_emmc_boot()){
484 ptr = atag_ptable(&ptr);
485 }
486
487 ptr = atag_cmdline(ptr, cmdline);
488 ptr = atag_end(ptr);
489}
490
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700491typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700492void boot_linux(void *kernel, unsigned *tags,
493 const char *cmdline, unsigned machtype,
494 void *ramdisk, unsigned ramdisk_size)
495{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800496 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800497#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700498 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800499#endif
500
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700501 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800502 uint32_t tags_phys = PA((addr_t)tags);
503
504 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700505
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800506 final_cmdline = update_cmdline((const char*)cmdline);
507
Neeti Desai17379b82012-06-04 18:42:53 -0700508#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800509 dprintf(INFO, "Updating device tree: start\n");
510
Neeti Desai17379b82012-06-04 18:42:53 -0700511 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800512 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700513 if(ret)
514 {
515 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
516 ASSERT(0);
517 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800518 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700519#else
Neeti Desaie245d492012-06-01 12:52:13 -0700520 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800521 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700522#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700523
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700524 /* Perform target specific cleanup */
525 target_uninit();
526
Aravind Venkateswarandb0d5262014-02-25 16:25:30 -0800527 /* Turn off splash screen if enabled */
528#if DISPLAY_SPLASH_SCREEN
529 target_display_shutdown();
530#endif
531
532
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800533 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
534 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800535
536 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700537
Amol Jadi4421e652011-06-16 15:00:48 -0700538 /* do any platform specific cleanup before kernel entry */
539 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700540
Brian Swetland9c4c0752009-01-25 16:23:50 -0800541 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700542
Amol Jadi504f9fe2012-08-16 13:56:48 -0700543#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800544 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700545#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700546 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700547 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800548}
549
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700550/* Function to check if the memory address range falls within the aboot
551 * boundaries.
552 * start: Start of the memory region
553 * size: Size of the memory region
554 */
555int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
556{
557 /* Check for boundary conditions. */
558 if ((start + size) < start)
559 return -1;
560
561 /* Check for memory overlap. */
562 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
563 return 0;
Channagoud Kadabib4413fd2013-10-15 12:53:52 -0700564 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700565 return 0;
566 else
567 return -1;
568}
569
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800570#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800571
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700572BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800573#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700574BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800575#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800576
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700577static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
578{
579 int ret;
580
581 /* Assume device is rooted at this time. */
582 device.is_tampered = 1;
583
584 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
585
586 ret = image_verify((unsigned char *)bootimg_addr,
587 (unsigned char *)(bootimg_addr + bootimg_size),
588 bootimg_size,
589 CRYPTO_AUTH_ALG_SHA256);
590
591 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
592
593 if (ret)
594 {
595 /* Authorized kernel */
596 device.is_tampered = 0;
597 }
598
599#if USE_PCOM_SECBOOT
600 set_tamper_flag(device.is_tampered);
601#endif
602
603 if(device.is_tampered)
604 {
605 write_device_info_mmc(&device);
606 #ifdef TZ_TAMPER_FUSE
607 set_tamper_fuse_cmd();
608 #endif
609 #ifdef ASSERT_ON_TAMPER
610 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
611 ASSERT(0);
612 #endif
613 }
614}
615
Maunik Shah78a85e52014-03-02 17:47:58 +0530616static bool check_format_bit()
617{
618 bool ret = false;
619 int index;
620 uint64_t offset;
621 struct boot_selection_info *in = NULL;
622 char *buf = NULL;
623
624 index = partition_get_index("bootselect");
625 if (index == INVALID_PTN)
626 {
627 dprintf(INFO, "Unable to locate /bootselect partition\n");
628 return ret;
629 }
630 offset = partition_get_offset(index);
631 if(!offset)
632 {
633 dprintf(INFO, "partition /bootselect doesn't exist\n");
634 return ret;
635 }
636 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
637 ASSERT(buf);
638 if (mmc_read(offset, (unsigned int *)buf, page_size))
639 {
640 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
641 free(buf);
642 return ret;
643 }
644 in = (struct boot_selection_info *) buf;
645 if ((in->signature == BOOTSELECT_SIGNATURE) &&
646 (in->version == BOOTSELECT_VERSION)) {
647 if ((in->state_info & BOOTSELECT_FORMAT) &&
648 !(in->state_info & BOOTSELECT_FACTORY))
649 ret = true;
650 } else {
651 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
652 in->signature, in->version);
653 ASSERT(0);
654 }
655 free(buf);
656 return ret;
657}
658
Shashank Mittal23b8f422010-04-16 19:27:21 -0700659int boot_linux_from_mmc(void)
660{
661 struct boot_img_hdr *hdr = (void*) buf;
662 struct boot_img_hdr *uhdr;
663 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700664 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700665 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700666 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700667
Shashank Mittalcd98d472011-08-02 14:29:24 -0700668 unsigned char *image_addr = 0;
669 unsigned kernel_actual;
670 unsigned ramdisk_actual;
671 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700672 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700673
674#if DEVICE_TREE
675 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700676 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700677 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800678 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700679 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700680#endif
Maunik Shah78a85e52014-03-02 17:47:58 +0530681 if (check_format_bit())
682 boot_into_recovery = 1;
683
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700684 if (!boot_into_recovery) {
685 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
686 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
687 if (rcode <= 0) {
688 boot_into_ffbm = false;
689 if (rcode < 0)
690 dprintf(CRITICAL,"failed to get ffbm cookie");
691 } else
692 boot_into_ffbm = true;
693 } else
694 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700695 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
696 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
697 dprintf(INFO, "Unified boot method!\n");
698 hdr = uhdr;
699 goto unified_boot;
700 }
Greg Griscod6250552011-06-29 14:40:23 -0700701 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700702 index = partition_get_index("boot");
703 ptn = partition_get_offset(index);
704 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700705 dprintf(CRITICAL, "ERROR: No boot partition found\n");
706 return -1;
707 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700708 }
709 else {
710 index = partition_get_index("recovery");
711 ptn = partition_get_offset(index);
712 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700713 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
714 return -1;
715 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700716 }
717
Greg Griscod6250552011-06-29 14:40:23 -0700718 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700719 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
720 return -1;
721 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700722
723 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700724 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700725 return -1;
726 }
727
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700728 if (hdr->page_size && (hdr->page_size != page_size)) {
729 page_size = hdr->page_size;
730 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700731 }
732
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700733 /*
734 * Update the kernel/ramdisk/tags address if the boot image header
735 * has default values, these default values come from mkbootimg when
736 * the boot image is flashed using fastboot flash:raw
737 */
738 update_ker_tags_rdisk_addr(hdr);
739
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700740 /* Get virtual addresses since the hdr saves physical addresses. */
741 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
742 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
743 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700744
745 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
746 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
747
748 /* Check if the addresses in the header are valid. */
749 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
750 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
751 {
752 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
753 return -1;
754 }
755
756#ifndef DEVICE_TREE
757 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
758 {
759 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
760 return -1;
761 }
762#endif
763
Shashank Mittalcd98d472011-08-02 14:29:24 -0700764 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300765 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
766 (int) target_use_signed_kernel(),
767 device.is_unlocked,
768 device.is_tampered);
769
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700770 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800771 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700772 offset = 0;
773
Shashank Mittalcd98d472011-08-02 14:29:24 -0700774 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700775
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800776#if DEVICE_TREE
777 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
778 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700779
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700780 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700781 {
782 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
783 return -1;
784 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800785#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530786 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700787
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700788#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700789
Amol Jadib6be5c12012-11-14 13:39:51 -0800790 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700791 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800792
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700793 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
794 {
795 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
796 return -1;
797 }
798
Shashank Mittalcd98d472011-08-02 14:29:24 -0700799 /* Read image without signature */
800 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
801 {
802 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
803 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800804 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700805
Amol Jadib6be5c12012-11-14 13:39:51 -0800806 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700807 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800808
Shashank Mittalcd98d472011-08-02 14:29:24 -0700809 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700810
811 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
812 {
813 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
814 return -1;
815 }
816
Shashank Mittalcd98d472011-08-02 14:29:24 -0700817 /* Read signature */
818 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
819 {
820 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700821 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700822 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800823
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700824 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700825
Neeti Desai465491e2012-07-31 12:53:35 -0700826 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700827 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
828 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700829
Neeti Desai465491e2012-07-31 12:53:35 -0700830 #if DEVICE_TREE
831 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700832 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700833 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700834
Deepa Dinamani19648b42013-09-05 17:05:55 -0700835 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700836 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
837 return -1;
838 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700839
Neeti Desai465491e2012-07-31 12:53:35 -0700840 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700841 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700842 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
843 return -1;
844 }
845
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700846 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700847 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700848 {
849 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
850 return -1;
851 }
852
Joel Kingaa335dc2013-06-03 16:11:08 -0700853 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800854 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800855 /*
856 * If appended dev tree is found, update the atags with
857 * memory address to the DTB appended location on RAM.
858 * Else update with the atags address in the kernel header
859 */
860 void *dtb;
861 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700862 hdr->kernel_size,
863 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800864 if (!dtb) {
865 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
866 return -1;
867 }
Neeti Desai465491e2012-07-31 12:53:35 -0700868 }
869 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700870 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700871 else
872 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800873 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700874
Amol Jadib6be5c12012-11-14 13:39:51 -0800875 dprintf(INFO, "Loading boot image (%d): start\n",
876 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700877 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800878
879 offset = page_size;
880
881 /* Load kernel */
882 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700883 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
884 return -1;
885 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800886 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700887
Amol Jadib6be5c12012-11-14 13:39:51 -0800888 /* Load ramdisk */
889 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700890 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800891 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700892 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
893 return -1;
894 }
895 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800896 offset += ramdisk_actual;
897
898 dprintf(INFO, "Loading boot image (%d): done\n",
899 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700900 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700901
902 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800903 offset += second_actual;
904 /* Second image loading not implemented. */
905 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700906 }
907
908 #if DEVICE_TREE
909 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -0700910 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -0700911 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
912 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
913 return -1;
914 }
915 table = (struct dt_table*) dt_buf;
916
Deepa Dinamani19648b42013-09-05 17:05:55 -0700917 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700918 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
919 return -1;
920 }
921
Deepa Dinamani19648b42013-09-05 17:05:55 -0700922 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
923 if (!table)
924 return -1;
925
926 /* Read the entire device tree table into buffer */
927 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
928 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
929 return -1;
930 }
931
Joel Kingaa335dc2013-06-03 16:11:08 -0700932 /* Find index of device tree within device tree table */
933 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700934 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
935 return -1;
936 }
937
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700938 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700939 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700940 {
941 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
942 return -1;
943 }
944
Joel Kingaa335dc2013-06-03 16:11:08 -0700945 if(mmc_read(ptn + offset + dt_entry.offset,
946 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700947 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
948 return -1;
949 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300950 #ifdef TZ_SAVE_KERNEL_HASH
951 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
952 hdr->ramdisk_addr, ramdisk_actual,
953 ptn, offset, hdr->dt_size);
954 #endif /* TZ_SAVE_KERNEL_HASH */
955
Channagoud Kadabi35095622013-03-01 13:53:05 -0800956 } else {
Aparna Mallavarapuf5432e42014-06-03 22:15:54 +0530957
958 /* Validate the tags_addr */
959 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
960 {
961 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
962 return -1;
963 }
Dima Zavin77e41f32013-03-06 16:10:43 -0800964 /*
965 * If appended dev tree is found, update the atags with
966 * memory address to the DTB appended location on RAM.
967 * Else update with the atags address in the kernel header
968 */
969 void *dtb;
970 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700971 kernel_actual,
972 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800973 if (!dtb) {
974 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
975 return -1;
976 }
Neeti Desai465491e2012-07-31 12:53:35 -0700977 }
978 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700979 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700980
Stanimir Varbanov69ec5462013-07-18 18:17:42 +0300981 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
982 target_load_ssd_keystore();
983
Shashank Mittal23b8f422010-04-16 19:27:21 -0700984unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700985
Dima Zavin77e41f32013-03-06 16:10:43 -0800986 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700987 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700988 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
989
990 return 0;
991}
992
Dima Zavin214cc642009-01-26 11:16:21 -0800993int boot_linux_from_flash(void)
994{
995 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800996 struct ptentry *ptn;
997 struct ptable *ptable;
998 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800999
Shashank Mittalcd98d472011-08-02 14:29:24 -07001000 unsigned char *image_addr = 0;
1001 unsigned kernel_actual;
1002 unsigned ramdisk_actual;
1003 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001004 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001005
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001006#if DEVICE_TREE
1007 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001008 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001009 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001010 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001011#endif
1012
David Ng183a7422009-12-07 14:55:21 -08001013 if (target_is_emmc_boot()) {
1014 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1015 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1016 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1017 return -1;
1018 }
1019 goto continue_boot;
1020 }
1021
Dima Zavin214cc642009-01-26 11:16:21 -08001022 ptable = flash_get_ptable();
1023 if (ptable == NULL) {
1024 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1025 return -1;
1026 }
1027
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001028 if(!boot_into_recovery)
1029 {
1030 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001031
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001032 if (ptn == NULL) {
1033 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1034 return -1;
1035 }
1036 }
1037 else
1038 {
1039 ptn = ptable_find(ptable, "recovery");
1040 if (ptn == NULL) {
1041 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1042 return -1;
1043 }
Dima Zavin214cc642009-01-26 11:16:21 -08001044 }
1045
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001046 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001047 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1048 return -1;
1049 }
Dima Zavin214cc642009-01-26 11:16:21 -08001050
1051 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001052 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001053 return -1;
1054 }
1055
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001056 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001057 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 -08001058 return -1;
1059 }
1060
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001061 /*
1062 * Update the kernel/ramdisk/tags address if the boot image header
1063 * has default values, these default values come from mkbootimg when
1064 * the boot image is flashed using fastboot flash:raw
1065 */
1066 update_ker_tags_rdisk_addr(hdr);
1067
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001068 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001069 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1070 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1071 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1072
1073 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1074 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1075
1076 /* Check if the addresses in the header are valid. */
1077 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1078 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1079 {
1080 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1081 return -1;
1082 }
1083
1084#ifndef DEVICE_TREE
1085 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1086 {
1087 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1088 return -1;
1089 }
1090#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001091
Shashank Mittalcd98d472011-08-02 14:29:24 -07001092 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001093 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001094 {
1095 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001096 offset = 0;
1097
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001098#if DEVICE_TREE
1099 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1100 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001101
1102 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1103 {
1104 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1105 return -1;
1106 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001107#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001108 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001109#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001110
Amol Jadib6be5c12012-11-14 13:39:51 -08001111 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001112 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001113
Shashank Mittalcd98d472011-08-02 14:29:24 -07001114 /* Read image without signature */
1115 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1116 {
1117 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1118 return -1;
1119 }
Dima Zavin214cc642009-01-26 11:16:21 -08001120
Amol Jadib6be5c12012-11-14 13:39:51 -08001121 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001122 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001123
Shashank Mittalcd98d472011-08-02 14:29:24 -07001124 offset = imagesize_actual;
1125 /* Read signature */
1126 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1127 {
1128 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001129 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001130 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001131
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001132 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001133
1134 /* Move kernel and ramdisk to correct address */
1135 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1136 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001137#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001138 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001139 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001140 {
1141 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1142 return -1;
1143 }
1144
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001145 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1146#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001147
1148 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001149 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001150 {
1151 write_device_info_flash(&device);
1152 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301153#if USE_PCOM_SECBOOT
1154 set_tamper_flag(device.is_tampered);
1155#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001156 }
1157 else
1158 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001159 offset = page_size;
1160
Amol Jadib6be5c12012-11-14 13:39:51 -08001161 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1162 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1163 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1164
1165 dprintf(INFO, "Loading boot image (%d): start\n",
1166 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001167 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001168
1169 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001170 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1171 return -1;
1172 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001173 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001174
Amol Jadib6be5c12012-11-14 13:39:51 -08001175 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001176 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1177 return -1;
1178 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001179 offset += ramdisk_actual;
1180
1181 dprintf(INFO, "Loading boot image (%d): done\n",
1182 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001183 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001184
1185 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001186 offset += second_actual;
1187 /* Second image loading not implemented. */
1188 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001189 }
1190
1191#if DEVICE_TREE
1192 if(hdr->dt_size != 0) {
1193
1194 /* Read the device tree table into buffer */
1195 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1196 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1197 return -1;
1198 }
1199
1200 table = (struct dt_table*) dt_buf;
1201
Deepa Dinamani19648b42013-09-05 17:05:55 -07001202 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001203 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1204 return -1;
1205 }
1206
Deepa Dinamani19648b42013-09-05 17:05:55 -07001207 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1208 if (!table)
1209 return -1;
1210
1211 /* Read the entire device tree table into buffer */
1212 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1213 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1214 return -1;
1215 }
1216
1217
Joel Kingaa335dc2013-06-03 16:11:08 -07001218 /* Find index of device tree within device tree table */
1219 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001220 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1221 return -1;
1222 }
1223
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001224 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001225 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001226 {
1227 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1228 return -1;
1229 }
1230
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001231 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001232 if(flash_read(ptn, offset + dt_entry.offset,
1233 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001234 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1235 return -1;
1236 }
1237 }
1238#endif
1239
Shashank Mittalcd98d472011-08-02 14:29:24 -07001240 }
David Ng183a7422009-12-07 14:55:21 -08001241continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001242
Dima Zavin214cc642009-01-26 11:16:21 -08001243 /* TODO: create/pass atags to kernel */
1244
Ajay Dudanie28a6072011-07-01 13:59:46 -07001245 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001246 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001247 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1248
1249 return 0;
1250}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001251
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001252BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001253void write_device_info_mmc(device_info *dev)
1254{
1255 struct device_info *info = (void*) info_buf;
1256 unsigned long long ptn = 0;
1257 unsigned long long size;
1258 int index = INVALID_PTN;
1259
1260 index = partition_get_index("aboot");
1261 ptn = partition_get_offset(index);
1262 if(ptn == 0)
1263 {
1264 return;
1265 }
1266
1267 size = partition_get_size(index);
1268
1269 memcpy(info, dev, sizeof(device_info));
1270
1271 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1272 {
1273 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1274 return;
1275 }
1276}
1277
1278void read_device_info_mmc(device_info *dev)
1279{
1280 struct device_info *info = (void*) info_buf;
1281 unsigned long long ptn = 0;
1282 unsigned long long size;
1283 int index = INVALID_PTN;
1284
1285 index = partition_get_index("aboot");
1286 ptn = partition_get_offset(index);
1287 if(ptn == 0)
1288 {
1289 return;
1290 }
1291
1292 size = partition_get_size(index);
1293
1294 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1295 {
1296 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1297 return;
1298 }
1299
1300 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1301 {
1302 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1303 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001304 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001305 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001306
1307 write_device_info_mmc(info);
1308 }
1309 memcpy(dev, info, sizeof(device_info));
1310}
1311
1312void write_device_info_flash(device_info *dev)
1313{
1314 struct device_info *info = (void *) info_buf;
1315 struct ptentry *ptn;
1316 struct ptable *ptable;
1317
1318 ptable = flash_get_ptable();
1319 if (ptable == NULL)
1320 {
1321 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1322 return;
1323 }
1324
1325 ptn = ptable_find(ptable, "devinfo");
1326 if (ptn == NULL)
1327 {
1328 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1329 return;
1330 }
1331
1332 memcpy(info, dev, sizeof(device_info));
1333
1334 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1335 {
1336 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1337 return;
1338 }
1339}
1340
1341void read_device_info_flash(device_info *dev)
1342{
1343 struct device_info *info = (void*) info_buf;
1344 struct ptentry *ptn;
1345 struct ptable *ptable;
1346
1347 ptable = flash_get_ptable();
1348 if (ptable == NULL)
1349 {
1350 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1351 return;
1352 }
1353
1354 ptn = ptable_find(ptable, "devinfo");
1355 if (ptn == NULL)
1356 {
1357 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1358 return;
1359 }
1360
1361 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1362 {
1363 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1364 return;
1365 }
1366
1367 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1368 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001369 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1370 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001371 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001372 write_device_info_flash(info);
1373 }
1374 memcpy(dev, info, sizeof(device_info));
1375}
1376
1377void write_device_info(device_info *dev)
1378{
1379 if(target_is_emmc_boot())
1380 {
1381 write_device_info_mmc(dev);
1382 }
1383 else
1384 {
1385 write_device_info_flash(dev);
1386 }
1387}
1388
1389void read_device_info(device_info *dev)
1390{
1391 if(target_is_emmc_boot())
1392 {
1393 read_device_info_mmc(dev);
1394 }
1395 else
1396 {
1397 read_device_info_flash(dev);
1398 }
1399}
1400
1401void reset_device_info()
1402{
1403 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001404 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001405 write_device_info(&device);
1406}
1407
1408void set_device_root()
1409{
1410 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001411 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001412 write_device_info(&device);
1413}
1414
Amol Jadicb524072012-08-09 16:40:18 -07001415#if DEVICE_TREE
1416int copy_dtb(uint8_t *boot_image_start)
1417{
1418 uint32 dt_image_offset = 0;
1419 uint32_t n;
1420 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001421 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001422 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001423
1424 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1425
Amol Jadicb524072012-08-09 16:40:18 -07001426 if(hdr->dt_size != 0) {
1427
1428 /* add kernel offset */
1429 dt_image_offset += page_size;
1430 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1431 dt_image_offset += n;
1432
1433 /* add ramdisk offset */
1434 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1435 dt_image_offset += n;
1436
1437 /* add second offset */
1438 if(hdr->second_size != 0) {
1439 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1440 dt_image_offset += n;
1441 }
1442
1443 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001444 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001445
Deepa Dinamani19648b42013-09-05 17:05:55 -07001446 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001447 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1448 return -1;
1449 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001450 /* Find index of device tree within device tree table */
1451 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001452 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1453 return -1;
1454 }
1455
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001456 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001457 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001458 {
1459 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1460 return -1;
1461 }
1462
Amol Jadicb524072012-08-09 16:40:18 -07001463 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001464 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001465 boot_image_start + dt_image_offset + dt_entry.offset,
1466 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001467 } else
1468 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001469
1470 /* Everything looks fine. Return success. */
1471 return 0;
1472}
1473#endif
1474
Brian Swetland9c4c0752009-01-25 16:23:50 -08001475void cmd_boot(const char *arg, void *data, unsigned sz)
1476{
1477 unsigned kernel_actual;
1478 unsigned ramdisk_actual;
Channagoud Kadabice737c82014-06-18 17:41:01 -07001479 uint32_t image_actual;
1480 uint32_t dt_actual = 0;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001481 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001482 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001483 int ret = 0;
1484 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001485
1486 if (sz < sizeof(hdr)) {
1487 fastboot_fail("invalid bootimage header");
1488 return;
1489 }
1490
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001491 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001492
1493 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001494 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001495
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001496 if(target_is_emmc_boot() && hdr->page_size) {
1497 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001498 page_mask = page_size - 1;
1499 }
1500
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001501 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1502 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabice737c82014-06-18 17:41:01 -07001503#if DEVICE_TREE
1504 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1505#endif
1506
1507 image_actual = ADD_OF(page_size, kernel_actual);
1508 image_actual = ADD_OF(image_actual, ramdisk_actual);
1509 image_actual = ADD_OF(image_actual, dt_actual);
1510
1511 /* sz should have atleast raw boot image */
1512 if (image_actual > sz) {
1513 fastboot_fail("incomplete bootimage");
1514 return;
1515 }
1516
1517 /* Verify the boot image
1518 * device & page_size are initialized in aboot_init
1519 */
1520 if (target_use_signed_kernel() && (!device.is_unlocked))
1521 verify_signed_bootimg((uint32_t)data, image_actual);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001522
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001523 /*
1524 * Update the kernel/ramdisk/tags address if the boot image header
1525 * has default values, these default values come from mkbootimg when
1526 * the boot image is flashed using fastboot flash:raw
1527 */
1528 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001529
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001530 /* Get virtual addresses since the hdr saves physical addresses. */
1531 hdr->kernel_addr = VA(hdr->kernel_addr);
1532 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1533 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001534
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001535 /* Check if the addresses in the header are valid. */
1536 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1537 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1538 {
1539 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabia54102a2013-12-03 11:19:20 -08001540 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001541 }
1542
Amol Jadicb524072012-08-09 16:40:18 -07001543#if DEVICE_TREE
1544 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001545 ret = copy_dtb(data);
1546
1547 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001548#else
1549 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1550 {
1551 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabia54102a2013-12-03 11:19:20 -08001552 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001553 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001554#endif
1555
1556 /* Load ramdisk & kernel */
1557 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1558 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1559
1560#if DEVICE_TREE
1561 /*
1562 * If dtb is not found look for appended DTB in the kernel.
1563 * If appended dev tree is found, update the atags with
1564 * memory address to the DTB appended location on RAM.
1565 * Else update with the atags address in the kernel header
1566 */
1567 if (!dtb_copied) {
1568 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001569 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1570 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001571 if (!dtb) {
1572 fastboot_fail("dtb not found");
1573 return;
1574 }
Amol Jadicb524072012-08-09 16:40:18 -07001575 }
1576#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001577
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001578#ifndef DEVICE_TREE
1579 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1580 {
1581 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabia54102a2013-12-03 11:19:20 -08001582 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001583 }
1584#endif
1585
Brian Swetland9c4c0752009-01-25 16:23:50 -08001586 fastboot_okay("");
1587 udc_stop();
1588
Dima Zavin77e41f32013-03-06 16:10:43 -08001589 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001590 (const char*) hdr->cmdline, board_machtype(),
1591 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001592}
1593
Dima Zavin214cc642009-01-26 11:16:21 -08001594void cmd_erase(const char *arg, void *data, unsigned sz)
1595{
1596 struct ptentry *ptn;
1597 struct ptable *ptable;
1598
1599 ptable = flash_get_ptable();
1600 if (ptable == NULL) {
1601 fastboot_fail("partition table doesn't exist");
1602 return;
1603 }
1604
1605 ptn = ptable_find(ptable, arg);
1606 if (ptn == NULL) {
1607 fastboot_fail("unknown partition name");
1608 return;
1609 }
1610
1611 if (flash_erase(ptn)) {
1612 fastboot_fail("failed to erase partition");
1613 return;
1614 }
1615 fastboot_okay("");
1616}
1617
Bikas Gurungd48bd242010-09-04 19:54:32 -07001618
1619void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1620{
Kun Liang2f1601a2013-08-12 16:29:54 +08001621 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001622 unsigned long long ptn = 0;
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001623 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001624 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001625
Kinson Chikf1a43512011-07-14 11:28:39 -07001626 index = partition_get_index(arg);
1627 ptn = partition_get_offset(index);
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001628 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001629
Kinson Chikf1a43512011-07-14 11:28:39 -07001630 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001631 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001632 return;
1633 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001634
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001635#if MMC_SDHCI_SUPPORT
1636 if (mmc_erase_card(ptn, size)) {
1637 fastboot_fail("failed to erase partition\n");
1638 return;
1639 }
1640#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001641 size = partition_get_size(index);
1642 if (size > DEFAULT_ERASE_SIZE)
1643 size = DEFAULT_ERASE_SIZE;
1644
neetidb4b24d62012-01-20 12:13:09 -08001645 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001646 0 in first several blocks */
1647 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001648 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001649 return;
1650 }
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001651#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001652 fastboot_okay("");
1653}
1654
1655
Ajay Dudani5c761132011-04-07 20:19:04 -07001656void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001657{
1658 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001659 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001660 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001661
Greg Grisco6e754772011-06-23 12:19:39 -07001662 if (!strcmp(arg, "partition"))
1663 {
1664 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001665 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001666 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001667 return;
1668 }
1669 }
Greg Grisco6e754772011-06-23 12:19:39 -07001670 else
1671 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001672 index = partition_get_index(arg);
1673 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001674 if(ptn == 0) {
1675 fastboot_fail("partition table doesn't exist");
1676 return;
1677 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001678
Greg Grisco6e754772011-06-23 12:19:39 -07001679 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1680 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1681 fastboot_fail("image is not a boot image");
1682 return;
1683 }
1684 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001685
Kinson Chikf1a43512011-07-14 11:28:39 -07001686 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001687 if (ROUND_TO_PAGE(sz,511) > size) {
1688 fastboot_fail("size too large");
1689 return;
1690 }
1691 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1692 fastboot_fail("flash write failure");
1693 return;
1694 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001695 }
1696 fastboot_okay("");
1697 return;
1698}
1699
Ajay Dudani5c761132011-04-07 20:19:04 -07001700void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1701{
1702 unsigned int chunk;
1703 unsigned int chunk_data_sz;
1704 sparse_header_t *sparse_header;
1705 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001706 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001707 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301708 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001709 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001710
Kinson Chikf1a43512011-07-14 11:28:39 -07001711 index = partition_get_index(arg);
1712 ptn = partition_get_offset(index);
1713 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001714 fastboot_fail("partition table doesn't exist");
1715 return;
1716 }
1717
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301718 size = partition_get_size(index);
1719 if (ROUND_TO_PAGE(sz,511) > size) {
1720 fastboot_fail("size too large");
1721 return;
1722 }
1723
Ajay Dudani5c761132011-04-07 20:19:04 -07001724 /* Read and skip over sparse image header */
1725 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001726 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1727 fastboot_fail("size too large");
1728 return;
1729 }
1730
Ajay Dudani5c761132011-04-07 20:19:04 -07001731 data += sparse_header->file_hdr_sz;
1732 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1733 {
1734 /* Skip the remaining bytes in a header that is longer than
1735 * we expected.
1736 */
1737 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1738 }
1739
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001740 dprintf (SPEW, "=== Sparse Image Header ===\n");
1741 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1742 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1743 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1744 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1745 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1746 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1747 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1748 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001749
1750 /* Start processing chunks */
1751 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1752 {
1753 /* Read and skip over chunk header */
1754 chunk_header = (chunk_header_t *) data;
1755 data += sizeof(chunk_header_t);
1756
1757 dprintf (SPEW, "=== Chunk Header ===\n");
1758 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1759 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1760 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1761
1762 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1763 {
1764 /* Skip the remaining bytes in a header that is longer than
1765 * we expected.
1766 */
1767 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1768 }
1769
1770 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1771 switch (chunk_header->chunk_type)
1772 {
1773 case CHUNK_TYPE_RAW:
1774 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1775 chunk_data_sz))
1776 {
1777 fastboot_fail("Bogus chunk size for chunk type Raw");
1778 return;
1779 }
1780
Ajay Dudaniab18f022011-05-12 14:39:22 -07001781 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1782 chunk_data_sz,
1783 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001784 {
1785 fastboot_fail("flash write failure");
1786 return;
1787 }
1788 total_blocks += chunk_header->chunk_sz;
1789 data += chunk_data_sz;
1790 break;
1791
1792 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001793 total_blocks += chunk_header->chunk_sz;
1794 break;
1795
Ajay Dudani5c761132011-04-07 20:19:04 -07001796 case CHUNK_TYPE_CRC:
1797 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1798 {
1799 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1800 return;
1801 }
1802 total_blocks += chunk_header->chunk_sz;
1803 data += chunk_data_sz;
1804 break;
1805
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001806 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001807 fastboot_fail("Unknown chunk type");
1808 return;
1809 }
1810 }
1811
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001812 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1813 total_blocks, sparse_header->total_blks);
1814
1815 if(total_blocks != sparse_header->total_blks)
1816 {
1817 fastboot_fail("sparse image write failure");
1818 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001819
1820 fastboot_okay("");
1821 return;
1822}
1823
1824void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1825{
1826 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001827 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1828 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001829
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001830#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001831 int ret=0;
1832 uint32 major_version=0;
1833 uint32 minor_version=0;
1834
1835 ret = scm_svc_version(&major_version,&minor_version);
1836 if(!ret)
1837 {
1838 if(major_version >= 2)
1839 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001840 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001841 {
1842 ret = encrypt_scm((uint32 **) &data, &sz);
1843 if (ret != 0) {
1844 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1845 return;
1846 }
1847
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001848 /* Protect only for SSD */
1849 if (!strcmp(arg, "ssd")) {
1850 ret = scm_protect_keystore((uint32 *) data, sz);
1851 if (ret != 0) {
1852 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1853 return;
1854 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001855 }
1856 }
1857 else
1858 {
1859 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1860 if(ret != 0)
1861 {
1862 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1863 return;
1864 }
1865 }
1866 }
1867 else
1868 {
1869 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1870 magic_number[1] == DECRYPT_MAGIC_1)
1871 {
1872 ret = decrypt_scm((uint32 **) &data, &sz);
1873 if (ret != 0) {
1874 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1875 return;
1876 }
1877 }
1878 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1879 magic_number[1] == ENCRYPT_MAGIC_1)
1880 {
1881 ret = encrypt_scm((uint32 **) &data, &sz);
1882 if (ret != 0) {
1883 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1884 return;
1885 }
1886 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001887 }
1888 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001889 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001890 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001891 dprintf(CRITICAL,"INVALID SVC Version\n");
1892 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001893 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001894#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001895
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001896 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001897 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1898 cmd_flash_mmc_img(arg, data, sz);
1899 else
1900 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001901 return;
1902}
1903
Dima Zavin214cc642009-01-26 11:16:21 -08001904void cmd_flash(const char *arg, void *data, unsigned sz)
1905{
1906 struct ptentry *ptn;
1907 struct ptable *ptable;
1908 unsigned extra = 0;
1909
1910 ptable = flash_get_ptable();
1911 if (ptable == NULL) {
1912 fastboot_fail("partition table doesn't exist");
1913 return;
1914 }
1915
1916 ptn = ptable_find(ptable, arg);
1917 if (ptn == NULL) {
1918 fastboot_fail("unknown partition name");
1919 return;
1920 }
1921
1922 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1923 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1924 fastboot_fail("image is not a boot image");
1925 return;
1926 }
1927 }
1928
Amol Jadi5c61a952012-05-04 17:05:35 -07001929 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001930 || !strcmp(ptn->name, "userdata")
1931 || !strcmp(ptn->name, "persist")
1932 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301933 if (flash_ecc_bch_enabled())
1934 /* Spare data bytes for 8 bit ECC increased by 4 */
1935 extra = ((page_size >> 9) * 20);
1936 else
1937 extra = ((page_size >> 9) * 16);
1938 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001939 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001940
1941 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1942 if (flash_write(ptn, extra, data, sz)) {
1943 fastboot_fail("flash write failure");
1944 return;
1945 }
1946 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1947 fastboot_okay("");
1948}
1949
1950void cmd_continue(const char *arg, void *data, unsigned sz)
1951{
1952 fastboot_okay("");
1953 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001954 if (target_is_emmc_boot())
1955 {
1956 boot_linux_from_mmc();
1957 }
1958 else
1959 {
1960 boot_linux_from_flash();
1961 }
Dima Zavin214cc642009-01-26 11:16:21 -08001962}
1963
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001964void cmd_reboot(const char *arg, void *data, unsigned sz)
1965{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001966 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001967 fastboot_okay("");
1968 reboot_device(0);
1969}
1970
1971void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1972{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001973 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001974 fastboot_okay("");
1975 reboot_device(FASTBOOT_MODE);
1976}
1977
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001978void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
1979{
1980 dprintf(INFO, "Enabling charger screen check\n");
1981 device.charger_screen_enabled = 1;
1982 write_device_info(&device);
1983 fastboot_okay("");
1984}
1985
1986void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
1987{
1988 dprintf(INFO, "Disabling charger screen check\n");
1989 device.charger_screen_enabled = 0;
1990 write_device_info(&device);
1991 fastboot_okay("");
1992}
1993
Unnati Gandhi15d08f92014-01-24 11:01:01 +05301994void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
1995{
1996 dprintf(INFO, "Selecting display panel %s\n", arg);
1997 if (arg)
1998 strlcpy(device.display_panel, arg,
1999 sizeof(device.display_panel));
2000 write_device_info(&device);
2001 fastboot_okay("");
2002}
2003
Shashank Mittal162244e2011-08-08 19:01:25 -07002004void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2005{
2006 if(!device.is_unlocked)
2007 {
2008 device.is_unlocked = 1;
2009 write_device_info(&device);
2010 }
2011 fastboot_okay("");
2012}
2013
Shashank Mittala0032282011-08-26 14:50:11 -07002014void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2015{
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302016 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002017 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002018 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002019 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2020 fastboot_info(response);
2021 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002022 fastboot_info(response);
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302023 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2024 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002025 fastboot_okay("");
2026}
2027
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002028void cmd_preflash(const char *arg, void *data, unsigned sz)
2029{
2030 fastboot_okay("");
2031}
2032
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302033static struct fbimage logo_header = {0};
2034struct fbimage* splash_screen_flash();
2035
2036int splash_screen_check_header(struct fbimage *logo)
2037{
2038 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2039 return -1;
2040 if (logo->header.width == 0 || logo->header.height == 0)
2041 return -1;
2042 return 0;
2043}
2044
2045struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002046{
2047 struct ptentry *ptn;
2048 struct ptable *ptable;
2049 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302050 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002051
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302052
2053 ptable = flash_get_ptable();
2054 if (ptable == NULL) {
2055 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2056 return NULL;
2057 }
2058 ptn = ptable_find(ptable, "splash");
2059 if (ptn == NULL) {
2060 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2061 return NULL;
2062 }
2063
2064 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2065 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2066 return NULL;
2067 }
2068
2069 if (splash_screen_check_header(logo)) {
2070 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2071 return NULL;
2072 }
2073
2074 fb_display = fbcon_display();
2075 if (fb_display) {
2076 uint8_t *base = (uint8_t *) fb_display->base;
2077 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
2078 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002079 }
2080
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302081 if (flash_read(ptn + sizeof(logo->header), 0,
2082 base,
2083 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2084 fbcon_clear();
2085 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2086 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002087 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302088 logo->image = base;
2089 }
2090
2091 return logo;
2092}
2093
2094struct fbimage* splash_screen_mmc()
2095{
2096 int index = INVALID_PTN;
2097 unsigned long long ptn = 0;
2098 struct fbcon_config *fb_display = NULL;
2099 struct fbimage *logo = &logo_header;
2100
2101 index = partition_get_index("splash");
2102 if (index == 0) {
2103 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2104 return NULL;
2105 }
2106
2107 ptn = partition_get_offset(index);
2108 if (ptn == 0) {
2109 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2110 return NULL;
2111 }
2112
2113 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2114 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2115 return NULL;
2116 }
2117
2118 if (splash_screen_check_header(logo)) {
2119 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2120 return NULL;
2121 }
2122
2123 fb_display = fbcon_display();
2124 if (fb_display) {
2125 uint8_t *base = (uint8_t *) fb_display->base;
2126 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2127 base += LOGO_IMG_OFFSET;
2128
2129 if (mmc_read(ptn + sizeof(logo->header),
2130 base,
2131 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2132 fbcon_clear();
2133 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2134 return NULL;
2135 }
2136
2137 logo->image = base;
2138 }
2139
2140 return logo;
2141}
2142
2143
2144struct fbimage* fetch_image_from_partition()
2145{
2146 if (target_is_emmc_boot()) {
2147 return splash_screen_mmc();
2148 } else {
2149 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002150 }
2151}
2152
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002153/* Get the size from partiton name */
2154static void get_partition_size(const char *arg, char *response)
2155{
2156 uint64_t ptn = 0;
2157 uint64_t size;
2158 int index = INVALID_PTN;
2159
2160 index = partition_get_index(arg);
2161
2162 if (index == INVALID_PTN)
2163 {
2164 dprintf(CRITICAL, "Invalid partition index\n");
2165 return;
2166 }
2167
2168 ptn = partition_get_offset(index);
2169
2170 if(!ptn)
2171 {
2172 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2173 return;
2174 }
2175
2176 size = partition_get_size(index);
2177
2178 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2179 return;
2180}
2181
2182/*
2183 * Publish the partition type & size info
2184 * fastboot getvar will publish the required information.
2185 * fastboot getvar partition_size:<partition_name>: partition size in hex
2186 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2187 */
2188static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2189{
2190 uint8_t i;
2191
2192 for (i = 0; i < num_parts; i++) {
2193 get_partition_size(info[i].part_name, info[i].size_response);
2194
2195 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2196 {
2197 dprintf(CRITICAL, "partition size name truncated\n");
2198 return;
2199 }
2200 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2201 {
2202 dprintf(CRITICAL, "partition type name truncated\n");
2203 return;
2204 }
2205
2206 /* publish partition size & type info */
2207 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2208 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2209 }
2210}
2211
Amol Jadi5edf3552013-07-23 14:15:34 -07002212/* register commands and variables for fastboot */
2213void aboot_fastboot_register_commands(void)
2214{
2215 if (target_is_emmc_boot())
2216 {
2217 fastboot_register("flash:", cmd_flash_mmc);
2218 fastboot_register("erase:", cmd_erase_mmc);
2219 }
2220 else
2221 {
2222 fastboot_register("flash:", cmd_flash);
2223 fastboot_register("erase:", cmd_erase);
2224 }
2225
2226 fastboot_register("boot", cmd_boot);
2227 fastboot_register("continue", cmd_continue);
2228 fastboot_register("reboot", cmd_reboot);
2229 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2230 fastboot_register("oem unlock", cmd_oem_unlock);
2231 fastboot_register("oem device-info", cmd_oem_devinfo);
2232 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002233 fastboot_register("oem enable-charger-screen",
2234 cmd_oem_enable_charger_screen);
2235 fastboot_register("oem disable-charger-screen",
2236 cmd_oem_disable_charger_screen);
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302237 fastboot_register("oem select-display-panel",
2238 cmd_oem_select_display_panel);
Amol Jadi5edf3552013-07-23 14:15:34 -07002239 /* publish variables and their values */
2240 fastboot_publish("product", TARGET(BOARD));
2241 fastboot_publish("kernel", "lk");
2242 fastboot_publish("serialno", sn_buf);
2243
2244 /*
2245 * partition info is supported only for emmc partitions
2246 * Calling this for NAND prints some error messages which
2247 * is harmless but misleading. Avoid calling this for NAND
2248 * devices.
2249 */
2250 if (target_is_emmc_boot())
2251 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2252
2253 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002254 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2255 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002256 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002257 /* Is the charger screen check enabled */
2258 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2259 device.charger_screen_enabled);
2260 fastboot_publish("charger-screen-enabled",
2261 (const char *) charger_screen_enabled);
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302262 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2263 device.display_panel);
2264 fastboot_publish("display-panel",
2265 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002266}
2267
Brian Swetland9c4c0752009-01-25 16:23:50 -08002268void aboot_init(const struct app_descriptor *app)
2269{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002270 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002271 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002272
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002273 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002274 if (target_is_emmc_boot())
2275 {
2276 page_size = 2048;
2277 page_mask = page_size - 1;
2278 }
2279 else
2280 {
2281 page_size = flash_page_size();
2282 page_mask = page_size - 1;
2283 }
2284
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002285 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2286
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002287 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002288
Aravind Venkateswarandb0d5262014-02-25 16:25:30 -08002289 /* Display splash screen if enabled */
2290#if DISPLAY_SPLASH_SCREEN
2291 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302292 target_display_init(device.display_panel);
Aravind Venkateswarandb0d5262014-02-25 16:25:30 -08002293 dprintf(SPEW, "Display Init: Done\n");
2294#endif
2295
2296
Greg Griscod6250552011-06-29 14:40:23 -07002297 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002298 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002299
Dhaval Patel223ec952013-07-18 14:49:44 -07002300 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2301
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002302 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002303 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002304 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002305 dprintf(ALWAYS,"dload mode key sequence detected\n");
2306 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002307 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002308 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002309 }
2310 else
2311 {
2312 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002313 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002314 }
2315 boot_into_fastboot = true;
2316 }
2317 if (!boot_into_fastboot)
2318 {
2319 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2320 boot_into_recovery = 1;
2321 if (!boot_into_recovery &&
2322 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002323 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002324 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002325 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002326 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002327 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002328 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002329
Ajay Dudani77421292010-10-27 19:34:06 -07002330 reboot_mode = check_reboot_mode();
2331 if (reboot_mode == RECOVERY_MODE) {
2332 boot_into_recovery = 1;
2333 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002334 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002335 }
2336
Pavel Nedev5d91d412013-04-29 11:34:24 +03002337 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002338 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002339 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002340 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002341 if(emmc_recovery_init())
2342 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2343 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002344 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002345 if((device.is_unlocked) || (device.is_tampered))
2346 {
2347 #ifdef TZ_TAMPER_FUSE
2348 set_tamper_fuse_cmd();
2349 #endif
2350 #if USE_PCOM_SECBOOT
2351 set_tamper_flag(device.is_tampered);
2352 #endif
2353 }
Shashank Mittala0032282011-08-26 14:50:11 -07002354 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002355 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002356 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002357 else
2358 {
2359 recovery_init();
2360 #if USE_PCOM_SECBOOT
2361 if((device.is_unlocked) || (device.is_tampered))
2362 set_tamper_flag(device.is_tampered);
2363 #endif
2364 boot_linux_from_flash();
2365 }
2366 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2367 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002368 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002369
Amol Jadi5edf3552013-07-23 14:15:34 -07002370 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002371
Amol Jadi5edf3552013-07-23 14:15:34 -07002372 /* register aboot specific fastboot commands */
2373 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002374
Amol Jadi5edf3552013-07-23 14:15:34 -07002375 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002376 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002377
2378 /* initialize and start fastboot */
2379 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002380}
2381
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002382uint32_t get_page_size()
2383{
2384 return page_size;
2385}
2386
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002387/*
2388 * Calculated and save hash (SHA256) for non-signed boot image.
2389 *
2390 * Hash the same data that is checked on the signed boot image.
2391 * Kernel and Ramdisk are already read to memory buffers.
2392 * Need to read the entire device-tree from mmc
2393 * since non-signed image only read the DT tags of the relevant platform.
2394 *
2395 * @param kernel_addr - kernel bufer
2396 * @param kernel_actual - kernel size in bytes
2397 * @param ramdisk_addr - ramdisk buffer
2398 * @param ramdisk_actual - ramdisk size
2399 * @param ptn - partition
2400 * @param dt_offset - device tree offset on mmc partition
2401 * @param dt_size
2402 *
2403 * @return int - 0 on success, negative value on failure.
2404 */
2405int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2406 void *ramdisk_addr, unsigned ramdisk_actual,
2407 unsigned long long ptn,
2408 unsigned dt_offset, unsigned dt_size)
2409{
2410 SHA256_CTX sha256_ctx;
2411 char digest[32]={0};
2412 char *buf = (char *)target_get_scratch_address();
2413 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2414 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2415
2416 SHA256_Init(&sha256_ctx);
2417
2418 /* Read Boot Header */
2419 if (mmc_read(ptn, buf, page_size))
2420 {
2421 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2422 return -1;
2423 }
2424 /* Read entire Device Tree */
2425 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2426 {
2427 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2428 return -1;
2429 }
2430 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2431 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2432 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2433 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2434
2435 SHA256_Final(digest, &sha256_ctx);
2436
2437 save_kernel_hash_cmd(digest);
2438 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2439
2440 return 0;
2441}
2442
Brian Swetland9c4c0752009-01-25 16:23:50 -08002443APP_START(aboot)
2444 .init = aboot_init,
2445APP_END