blob: 7e177dbe6b00988c3fe1067ede5ff0abea7a502f [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Amol Jadi2a15a272013-01-22 12:03:36 -08005 * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080038#include <kernel/thread.h>
39#include <arch/ops.h>
40
Dima Zavin214cc642009-01-26 11:16:21 -080041#include <dev/flash.h>
42#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080043#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080044#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080045#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070046#include <target.h>
47#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070048#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070049#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070050#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070051#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070052#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030053#include <sha.h>
Dima Zavin214cc642009-01-26 11:16:21 -080054
Neeti Desai17379b82012-06-04 18:42:53 -070055#if DEVICE_TREE
56#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070057#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070058#endif
59
Shashank Mittalcd98d472011-08-02 14:29:24 -070060#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080061#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080062#include "bootimg.h"
63#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070064#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070065#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070066#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070067#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080068
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);
72extern void dsb();
73extern void isb();
74extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070075extern void target_uninit(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070076
77void write_device_info_mmc(device_info *dev);
78void write_device_info_flash(device_info *dev);
79
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070080#define EXPAND(NAME) #NAME
81#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070082
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080083#ifdef MEMBASE
84#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
85#else
David Ng183a7422009-12-07 14:55:21 -080086#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080087#endif
88
Deepa Dinamani0e163a42013-05-24 17:08:15 -070089#ifndef MEMSIZE
90#define MEMSIZE 1024*1024
91#endif
92
93#define MAX_TAGS_SIZE 1024
94
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080095#define RECOVERY_MODE 0x77665502
96#define FASTBOOT_MODE 0x77665500
97
David Ng183a7422009-12-07 14:55:21 -080098static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080099static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300100static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800101static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800102static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700103static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300104static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800105
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800106static const char *baseband_apq = " androidboot.baseband=apq";
107static const char *baseband_msm = " androidboot.baseband=msm";
108static const char *baseband_csfb = " androidboot.baseband=csfb";
109static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700110static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700111static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800112static const char *baseband_dsda = " androidboot.baseband=dsda";
113static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800114static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800115
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700116static unsigned page_size = 0;
117static unsigned page_mask = 0;
118static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
119static bool boot_into_ffbm;
120
Shashank Mittalcd98d472011-08-02 14:29:24 -0700121/* Assuming unauthorized kernel image by default */
122static int auth_kernel_img = 0;
123
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700124static device_info device = {DEVICE_MAGIC, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700125
Dima Zavin42168f22009-01-30 11:52:22 -0800126struct atag_ptbl_entry
127{
128 char name[16];
129 unsigned offset;
130 unsigned size;
131 unsigned flags;
132};
133
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700134/*
135 * Partition info, required to be published
136 * for fastboot
137 */
138struct getvar_partition_info {
139 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
140 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
141 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
142 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
143 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
144};
145
146/*
147 * Right now, we are publishing the info for only
148 * three partitions
149 */
150struct getvar_partition_info part_info[] =
151{
152 { "system" , "partition-size:", "partition-type:", "", "ext4" },
153 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
154 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
155};
156
157char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700158char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800159char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700160
Greg Griscod2471ef2011-07-14 13:00:42 -0700161extern int emmc_recovery_init(void);
162
Kinson Chik0b1c8162011-08-31 16:31:57 -0700163#if NO_KEYPAD_DRIVER
164extern int fastboot_trigger(void);
165#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700166
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700167static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
168{
169 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700170#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
171 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
172 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
173 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700174#endif
175}
176
Dima Zavin42168f22009-01-30 11:52:22 -0800177static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
178{
179 struct atag_ptbl_entry atag_ptn;
180
181 memcpy(atag_ptn.name, ptn->name, 16);
182 atag_ptn.name[15] = '\0';
183 atag_ptn.offset = ptn->start;
184 atag_ptn.size = ptn->length;
185 atag_ptn.flags = ptn->flags;
186 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
187 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
188}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800189
Neeti Desaie245d492012-06-01 12:52:13 -0700190unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800191{
David Ng183a7422009-12-07 14:55:21 -0800192 int cmdline_len = 0;
193 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800194 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700195 int pause_at_bootup = 0;
Pavel Nedev5614d222013-06-17 18:01:02 +0300196 bool gpt_exists = partition_gpt_exists();
Dima Zavin42168f22009-01-30 11:52:22 -0800197
Brian Swetland9c4c0752009-01-25 16:23:50 -0800198 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800199 cmdline_len = strlen(cmdline);
200 have_cmdline = 1;
201 }
202 if (target_is_emmc_boot()) {
203 cmdline_len += strlen(emmc_cmdline);
204 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800205
206 cmdline_len += strlen(usb_sn_cmdline);
207 cmdline_len += strlen(sn_buf);
208
Pavel Nedev5614d222013-06-17 18:01:02 +0300209 if (boot_into_recovery && gpt_exists)
210 cmdline_len += strlen(secondary_gpt_enable);
211
Pavel Nedev328ac822013-04-05 15:25:11 +0300212 if (boot_into_ffbm) {
213 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700214 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800215 /* reduce kernel console messages to speed-up boot */
216 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700217 } else if (device.charger_screen_enabled &&
218 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700219 pause_at_bootup = 1;
220 cmdline_len += strlen(battchg_pause);
221 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800222
Shashank Mittalcd98d472011-08-02 14:29:24 -0700223 if(target_use_signed_kernel() && auth_kernel_img) {
224 cmdline_len += strlen(auth_kernel);
225 }
226
Ajay Dudanid04110c2011-01-17 23:55:07 -0800227 /* Determine correct androidboot.baseband to use */
228 switch(target_baseband())
229 {
230 case BASEBAND_APQ:
231 cmdline_len += strlen(baseband_apq);
232 break;
233
234 case BASEBAND_MSM:
235 cmdline_len += strlen(baseband_msm);
236 break;
237
238 case BASEBAND_CSFB:
239 cmdline_len += strlen(baseband_csfb);
240 break;
241
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800242 case BASEBAND_SVLTE2A:
243 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800244 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700245
246 case BASEBAND_MDM:
247 cmdline_len += strlen(baseband_mdm);
248 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700249
250 case BASEBAND_SGLTE:
251 cmdline_len += strlen(baseband_sglte);
252 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530253
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800254 case BASEBAND_SGLTE2:
255 cmdline_len += strlen(baseband_sglte2);
256 break;
257
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530258 case BASEBAND_DSDA:
259 cmdline_len += strlen(baseband_dsda);
260 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800261
262 case BASEBAND_DSDA2:
263 cmdline_len += strlen(baseband_dsda2);
264 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800265 }
266
David Ng183a7422009-12-07 14:55:21 -0800267 if (cmdline_len > 0) {
268 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700269 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
270 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700271
Amol Jadi168b7712012-03-06 16:15:00 -0800272 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700273 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800274 if (have_cmdline) {
275 src = cmdline;
276 while ((*dst++ = *src++));
277 }
278 if (target_is_emmc_boot()) {
279 src = emmc_cmdline;
280 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700281 have_cmdline = 1;
282 while ((*dst++ = *src++));
283 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800284
285 src = usb_sn_cmdline;
286 if (have_cmdline) --dst;
287 have_cmdline = 1;
288 while ((*dst++ = *src++));
289 src = sn_buf;
290 if (have_cmdline) --dst;
291 have_cmdline = 1;
292 while ((*dst++ = *src++));
293
Pavel Nedev5614d222013-06-17 18:01:02 +0300294 if (boot_into_recovery && gpt_exists) {
295 src = secondary_gpt_enable;
296 if (have_cmdline) --dst;
297 while ((*dst++ = *src++));
298 }
299
Pavel Nedev328ac822013-04-05 15:25:11 +0300300 if (boot_into_ffbm) {
301 src = androidboot_mode;
302 if (have_cmdline) --dst;
303 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700304 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300305 if (have_cmdline) --dst;
306 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800307 src = loglevel;
308 if (have_cmdline) --dst;
309 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300310 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700311 src = battchg_pause;
312 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800313 while ((*dst++ = *src++));
314 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800315
Shashank Mittalcd98d472011-08-02 14:29:24 -0700316 if(target_use_signed_kernel() && auth_kernel_img) {
317 src = auth_kernel;
318 if (have_cmdline) --dst;
319 while ((*dst++ = *src++));
320 }
321
Ajay Dudanid04110c2011-01-17 23:55:07 -0800322 switch(target_baseband())
323 {
324 case BASEBAND_APQ:
325 src = baseband_apq;
326 if (have_cmdline) --dst;
327 while ((*dst++ = *src++));
328 break;
329
330 case BASEBAND_MSM:
331 src = baseband_msm;
332 if (have_cmdline) --dst;
333 while ((*dst++ = *src++));
334 break;
335
336 case BASEBAND_CSFB:
337 src = baseband_csfb;
338 if (have_cmdline) --dst;
339 while ((*dst++ = *src++));
340 break;
341
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800342 case BASEBAND_SVLTE2A:
343 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800344 if (have_cmdline) --dst;
345 while ((*dst++ = *src++));
346 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700347
348 case BASEBAND_MDM:
349 src = baseband_mdm;
350 if (have_cmdline) --dst;
351 while ((*dst++ = *src++));
352 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700353
354 case BASEBAND_SGLTE:
355 src = baseband_sglte;
356 if (have_cmdline) --dst;
357 while ((*dst++ = *src++));
358 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530359
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800360 case BASEBAND_SGLTE2:
361 src = baseband_sglte2;
362 if (have_cmdline) --dst;
363 while ((*dst++ = *src++));
364 break;
365
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530366 case BASEBAND_DSDA:
367 src = baseband_dsda;
368 if (have_cmdline) --dst;
369 while ((*dst++ = *src++));
370 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800371
372 case BASEBAND_DSDA2:
373 src = baseband_dsda2;
374 if (have_cmdline) --dst;
375 while ((*dst++ = *src++));
376 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800377 }
Neeti Desaie245d492012-06-01 12:52:13 -0700378 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800379 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700380 return cmdline_final;
381}
382
383unsigned *atag_core(unsigned *ptr)
384{
385 /* CORE */
386 *ptr++ = 2;
387 *ptr++ = 0x54410001;
388
389 return ptr;
390
391}
392
393unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
394 unsigned ramdisk_size)
395{
396 if (ramdisk_size) {
397 *ptr++ = 4;
398 *ptr++ = 0x54420005;
399 *ptr++ = (unsigned)ramdisk;
400 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800401 }
402
Neeti Desaie245d492012-06-01 12:52:13 -0700403 return ptr;
404}
405
406unsigned *atag_ptable(unsigned **ptr_addr)
407{
408 int i;
409 struct ptable *ptable;
410
411 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700412 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
413 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700414 *(*ptr_addr)++ = 0x4d534d70;
415 for (i = 0; i < ptable->count; ++i)
416 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
417 }
418
419 return (*ptr_addr);
420}
421
422unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
423{
424 int cmdline_length = 0;
425 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700426 char *dest;
427
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800428 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700429 n = (cmdline_length + 4) & (~3);
430
431 *ptr++ = (n / 4) + 2;
432 *ptr++ = 0x54410009;
433 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800434 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700435 ptr += (n / 4);
436
437 return ptr;
438}
439
440unsigned *atag_end(unsigned *ptr)
441{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800442 /* END */
443 *ptr++ = 0;
444 *ptr++ = 0;
445
Neeti Desaie245d492012-06-01 12:52:13 -0700446 return ptr;
447}
448
449void generate_atags(unsigned *ptr, const char *cmdline,
450 void *ramdisk, unsigned ramdisk_size)
451{
452
453 ptr = atag_core(ptr);
454 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
455 ptr = target_atag_mem(ptr);
456
457 /* Skip NAND partition ATAGS for eMMC boot */
458 if (!target_is_emmc_boot()){
459 ptr = atag_ptable(&ptr);
460 }
461
462 ptr = atag_cmdline(ptr, cmdline);
463 ptr = atag_end(ptr);
464}
465
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700466typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700467void boot_linux(void *kernel, unsigned *tags,
468 const char *cmdline, unsigned machtype,
469 void *ramdisk, unsigned ramdisk_size)
470{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800471 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800472#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700473 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800474#endif
475
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700476 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800477 uint32_t tags_phys = PA((addr_t)tags);
478
479 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700480
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800481 final_cmdline = update_cmdline((const char*)cmdline);
482
Neeti Desai17379b82012-06-04 18:42:53 -0700483#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800484 dprintf(INFO, "Updating device tree: start\n");
485
Neeti Desai17379b82012-06-04 18:42:53 -0700486 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800487 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700488 if(ret)
489 {
490 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
491 ASSERT(0);
492 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800493 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700494#else
Neeti Desaie245d492012-06-01 12:52:13 -0700495 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800496 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700497#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700498
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700499 /* Perform target specific cleanup */
500 target_uninit();
501
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800502 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
503 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800504
505 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700506
Amol Jadi4421e652011-06-16 15:00:48 -0700507 /* do any platform specific cleanup before kernel entry */
508 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700509
Brian Swetland9c4c0752009-01-25 16:23:50 -0800510 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700511
Amol Jadi504f9fe2012-08-16 13:56:48 -0700512#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800513 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700514#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700515 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700516 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800517}
518
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700519/* Function to check if the memory address range falls within the aboot
520 * boundaries.
521 * start: Start of the memory region
522 * size: Size of the memory region
523 */
524int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
525{
526 /* Check for boundary conditions. */
527 if ((start + size) < start)
528 return -1;
529
530 /* Check for memory overlap. */
531 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
532 return 0;
533 else if (start > (MEMBASE + MEMSIZE))
534 return 0;
535 else
536 return -1;
537}
538
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800539#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800540
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700541BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800542#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700543BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800544#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800545
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700546static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
547{
548 int ret;
549
550 /* Assume device is rooted at this time. */
551 device.is_tampered = 1;
552
553 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
554
555 ret = image_verify((unsigned char *)bootimg_addr,
556 (unsigned char *)(bootimg_addr + bootimg_size),
557 bootimg_size,
558 CRYPTO_AUTH_ALG_SHA256);
559
560 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
561
562 if (ret)
563 {
564 /* Authorized kernel */
565 device.is_tampered = 0;
566 }
567
568#if USE_PCOM_SECBOOT
569 set_tamper_flag(device.is_tampered);
570#endif
571
572 if(device.is_tampered)
573 {
574 write_device_info_mmc(&device);
575 #ifdef TZ_TAMPER_FUSE
576 set_tamper_fuse_cmd();
577 #endif
578 #ifdef ASSERT_ON_TAMPER
579 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
580 ASSERT(0);
581 #endif
582 }
583}
584
Shashank Mittal23b8f422010-04-16 19:27:21 -0700585int boot_linux_from_mmc(void)
586{
587 struct boot_img_hdr *hdr = (void*) buf;
588 struct boot_img_hdr *uhdr;
589 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700590 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700591 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700592 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700593
Shashank Mittalcd98d472011-08-02 14:29:24 -0700594 unsigned char *image_addr = 0;
595 unsigned kernel_actual;
596 unsigned ramdisk_actual;
597 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700598 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700599
600#if DEVICE_TREE
601 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700602 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700603 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800604 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700605#endif
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700606 if (!boot_into_recovery) {
607 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
608 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
609 if (rcode <= 0) {
610 boot_into_ffbm = false;
611 if (rcode < 0)
612 dprintf(CRITICAL,"failed to get ffbm cookie");
613 } else
614 boot_into_ffbm = true;
615 } else
616 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700617 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
618 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
619 dprintf(INFO, "Unified boot method!\n");
620 hdr = uhdr;
621 goto unified_boot;
622 }
Greg Griscod6250552011-06-29 14:40:23 -0700623 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700624 index = partition_get_index("boot");
625 ptn = partition_get_offset(index);
626 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700627 dprintf(CRITICAL, "ERROR: No boot partition found\n");
628 return -1;
629 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700630 }
631 else {
632 index = partition_get_index("recovery");
633 ptn = partition_get_offset(index);
634 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700635 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
636 return -1;
637 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700638 }
639
Greg Griscod6250552011-06-29 14:40:23 -0700640 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700641 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
642 return -1;
643 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700644
645 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700646 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700647 return -1;
648 }
649
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700650 if (hdr->page_size && (hdr->page_size != page_size)) {
651 page_size = hdr->page_size;
652 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700653 }
654
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700655 /*
656 * Update the kernel/ramdisk/tags address if the boot image header
657 * has default values, these default values come from mkbootimg when
658 * the boot image is flashed using fastboot flash:raw
659 */
660 update_ker_tags_rdisk_addr(hdr);
661
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700662 /* Get virtual addresses since the hdr saves physical addresses. */
663 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
664 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
665 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700666
667 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
668 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
669
670 /* Check if the addresses in the header are valid. */
671 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
672 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
673 {
674 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
675 return -1;
676 }
677
678#ifndef DEVICE_TREE
679 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
680 {
681 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
682 return -1;
683 }
684#endif
685
Shashank Mittalcd98d472011-08-02 14:29:24 -0700686 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300687 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
688 (int) target_use_signed_kernel(),
689 device.is_unlocked,
690 device.is_tampered);
691
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700692 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800693 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700694 offset = 0;
695
Shashank Mittalcd98d472011-08-02 14:29:24 -0700696 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700697
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800698#if DEVICE_TREE
699 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
700 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700701
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700702 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700703 {
704 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
705 return -1;
706 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800707#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530708 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700709
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700710#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700711
Amol Jadib6be5c12012-11-14 13:39:51 -0800712 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700713 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800714
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700715 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
716 {
717 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
718 return -1;
719 }
720
Shashank Mittalcd98d472011-08-02 14:29:24 -0700721 /* Read image without signature */
722 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
723 {
724 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
725 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800726 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700727
Amol Jadib6be5c12012-11-14 13:39:51 -0800728 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700729 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800730
Shashank Mittalcd98d472011-08-02 14:29:24 -0700731 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700732
733 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
734 {
735 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
736 return -1;
737 }
738
Shashank Mittalcd98d472011-08-02 14:29:24 -0700739 /* Read signature */
740 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
741 {
742 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700743 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700744 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800745
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700746 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700747
Neeti Desai465491e2012-07-31 12:53:35 -0700748 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700749 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
750 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700751
Neeti Desai465491e2012-07-31 12:53:35 -0700752 #if DEVICE_TREE
753 if(hdr->dt_size) {
754 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700755 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700756
757 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
758
Joel Kingaa335dc2013-06-03 16:11:08 -0700759 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700760 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
761 return -1;
762 }
Neeti Desai465491e2012-07-31 12:53:35 -0700763 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700764 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700765 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
766 return -1;
767 }
768
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700769 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700770 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700771 {
772 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
773 return -1;
774 }
775
Joel Kingaa335dc2013-06-03 16:11:08 -0700776 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800777 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800778 /*
779 * If appended dev tree is found, update the atags with
780 * memory address to the DTB appended location on RAM.
781 * Else update with the atags address in the kernel header
782 */
783 void *dtb;
784 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700785 hdr->kernel_size,
786 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800787 if (!dtb) {
788 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
789 return -1;
790 }
Neeti Desai465491e2012-07-31 12:53:35 -0700791 }
792 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700793 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700794 else
795 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800796 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700797
Amol Jadib6be5c12012-11-14 13:39:51 -0800798 dprintf(INFO, "Loading boot image (%d): start\n",
799 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700800 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800801
802 offset = page_size;
803
804 /* Load kernel */
805 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700806 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
807 return -1;
808 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800809 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700810
Amol Jadib6be5c12012-11-14 13:39:51 -0800811 /* Load ramdisk */
812 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700813 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800814 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700815 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
816 return -1;
817 }
818 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800819 offset += ramdisk_actual;
820
821 dprintf(INFO, "Loading boot image (%d): done\n",
822 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700823 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700824
825 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800826 offset += second_actual;
827 /* Second image loading not implemented. */
828 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700829 }
830
831 #if DEVICE_TREE
832 if(hdr->dt_size != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700833 /* Read the device tree table into buffer */
834 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
835 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
836 return -1;
837 }
838 table = (struct dt_table*) dt_buf;
839
Joel Kingaa335dc2013-06-03 16:11:08 -0700840 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700841 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
842 return -1;
843 }
844
Joel Kingaa335dc2013-06-03 16:11:08 -0700845 /* Find index of device tree within device tree table */
846 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700847 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
848 return -1;
849 }
850
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700851 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700852 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700853 {
854 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
855 return -1;
856 }
857
Joel Kingaa335dc2013-06-03 16:11:08 -0700858 if(mmc_read(ptn + offset + dt_entry.offset,
859 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700860 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
861 return -1;
862 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300863 #ifdef TZ_SAVE_KERNEL_HASH
864 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
865 hdr->ramdisk_addr, ramdisk_actual,
866 ptn, offset, hdr->dt_size);
867 #endif /* TZ_SAVE_KERNEL_HASH */
868
Channagoud Kadabi35095622013-03-01 13:53:05 -0800869 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800870 /*
871 * If appended dev tree is found, update the atags with
872 * memory address to the DTB appended location on RAM.
873 * Else update with the atags address in the kernel header
874 */
875 void *dtb;
876 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700877 kernel_actual,
878 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800879 if (!dtb) {
880 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
881 return -1;
882 }
Neeti Desai465491e2012-07-31 12:53:35 -0700883 }
884 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700885 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700886
Stanimir Varbanov69ec5462013-07-18 18:17:42 +0300887 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
888 target_load_ssd_keystore();
889
Shashank Mittal23b8f422010-04-16 19:27:21 -0700890unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700891
Dima Zavin77e41f32013-03-06 16:10:43 -0800892 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700893 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700894 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
895
896 return 0;
897}
898
Dima Zavin214cc642009-01-26 11:16:21 -0800899int boot_linux_from_flash(void)
900{
901 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800902 struct ptentry *ptn;
903 struct ptable *ptable;
904 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800905
Shashank Mittalcd98d472011-08-02 14:29:24 -0700906 unsigned char *image_addr = 0;
907 unsigned kernel_actual;
908 unsigned ramdisk_actual;
909 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800910 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700911
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700912#if DEVICE_TREE
913 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700914 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800915 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700916#endif
917
David Ng183a7422009-12-07 14:55:21 -0800918 if (target_is_emmc_boot()) {
919 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
920 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
921 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
922 return -1;
923 }
924 goto continue_boot;
925 }
926
Dima Zavin214cc642009-01-26 11:16:21 -0800927 ptable = flash_get_ptable();
928 if (ptable == NULL) {
929 dprintf(CRITICAL, "ERROR: Partition table not found\n");
930 return -1;
931 }
932
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800933 if(!boot_into_recovery)
934 {
935 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700936
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800937 if (ptn == NULL) {
938 dprintf(CRITICAL, "ERROR: No boot partition found\n");
939 return -1;
940 }
941 }
942 else
943 {
944 ptn = ptable_find(ptable, "recovery");
945 if (ptn == NULL) {
946 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
947 return -1;
948 }
Dima Zavin214cc642009-01-26 11:16:21 -0800949 }
950
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800951 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800952 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
953 return -1;
954 }
Dima Zavin214cc642009-01-26 11:16:21 -0800955
956 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700957 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800958 return -1;
959 }
960
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800961 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700962 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 -0800963 return -1;
964 }
965
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700966 /*
967 * Update the kernel/ramdisk/tags address if the boot image header
968 * has default values, these default values come from mkbootimg when
969 * the boot image is flashed using fastboot flash:raw
970 */
971 update_ker_tags_rdisk_addr(hdr);
972
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700973 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700974 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
975 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
976 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
977
978 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
979 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
980
981 /* Check if the addresses in the header are valid. */
982 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
983 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
984 {
985 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
986 return -1;
987 }
988
989#ifndef DEVICE_TREE
990 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
991 {
992 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
993 return -1;
994 }
995#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700996
Shashank Mittalcd98d472011-08-02 14:29:24 -0700997 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700998 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700999 {
1000 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001001 offset = 0;
1002
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001003#if DEVICE_TREE
1004 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1005 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001006
1007 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1008 {
1009 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1010 return -1;
1011 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001012#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001013 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001014#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001015
Amol Jadib6be5c12012-11-14 13:39:51 -08001016 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001017 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001018
Shashank Mittalcd98d472011-08-02 14:29:24 -07001019 /* Read image without signature */
1020 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1021 {
1022 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1023 return -1;
1024 }
Dima Zavin214cc642009-01-26 11:16:21 -08001025
Amol Jadib6be5c12012-11-14 13:39:51 -08001026 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001027 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001028
Shashank Mittalcd98d472011-08-02 14:29:24 -07001029 offset = imagesize_actual;
1030 /* Read signature */
1031 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1032 {
1033 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001034 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001035 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001036
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001037 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001038
1039 /* Move kernel and ramdisk to correct address */
1040 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1041 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001042#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001043 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001044 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001045 {
1046 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1047 return -1;
1048 }
1049
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001050 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1051#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001052
1053 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001054 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001055 {
1056 write_device_info_flash(&device);
1057 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301058#if USE_PCOM_SECBOOT
1059 set_tamper_flag(device.is_tampered);
1060#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001061 }
1062 else
1063 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001064 offset = page_size;
1065
Amol Jadib6be5c12012-11-14 13:39:51 -08001066 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1067 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1068 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1069
1070 dprintf(INFO, "Loading boot image (%d): start\n",
1071 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001072 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001073
1074 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001075 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1076 return -1;
1077 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001078 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001079
Amol Jadib6be5c12012-11-14 13:39:51 -08001080 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001081 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1082 return -1;
1083 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001084 offset += ramdisk_actual;
1085
1086 dprintf(INFO, "Loading boot image (%d): done\n",
1087 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001088 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001089
1090 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001091 offset += second_actual;
1092 /* Second image loading not implemented. */
1093 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001094 }
1095
1096#if DEVICE_TREE
1097 if(hdr->dt_size != 0) {
1098
1099 /* Read the device tree table into buffer */
1100 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1101 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1102 return -1;
1103 }
1104
1105 table = (struct dt_table*) dt_buf;
1106
Joel Kingaa335dc2013-06-03 16:11:08 -07001107 if (dev_tree_validate(table, hdr->page_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001108 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1109 return -1;
1110 }
1111
Joel Kingaa335dc2013-06-03 16:11:08 -07001112 /* Find index of device tree within device tree table */
1113 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001114 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1115 return -1;
1116 }
1117
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001118 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001119 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001120 {
1121 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1122 return -1;
1123 }
1124
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001125 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001126 if(flash_read(ptn, offset + dt_entry.offset,
1127 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001128 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1129 return -1;
1130 }
1131 }
1132#endif
1133
Shashank Mittalcd98d472011-08-02 14:29:24 -07001134 }
David Ng183a7422009-12-07 14:55:21 -08001135continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001136
Dima Zavin214cc642009-01-26 11:16:21 -08001137 /* TODO: create/pass atags to kernel */
1138
Ajay Dudanie28a6072011-07-01 13:59:46 -07001139 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001140 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001141 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1142
1143 return 0;
1144}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001145
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001146BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001147void write_device_info_mmc(device_info *dev)
1148{
1149 struct device_info *info = (void*) info_buf;
1150 unsigned long long ptn = 0;
1151 unsigned long long size;
1152 int index = INVALID_PTN;
1153
1154 index = partition_get_index("aboot");
1155 ptn = partition_get_offset(index);
1156 if(ptn == 0)
1157 {
1158 return;
1159 }
1160
1161 size = partition_get_size(index);
1162
1163 memcpy(info, dev, sizeof(device_info));
1164
1165 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1166 {
1167 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1168 return;
1169 }
1170}
1171
1172void read_device_info_mmc(device_info *dev)
1173{
1174 struct device_info *info = (void*) info_buf;
1175 unsigned long long ptn = 0;
1176 unsigned long long size;
1177 int index = INVALID_PTN;
1178
1179 index = partition_get_index("aboot");
1180 ptn = partition_get_offset(index);
1181 if(ptn == 0)
1182 {
1183 return;
1184 }
1185
1186 size = partition_get_size(index);
1187
1188 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1189 {
1190 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1191 return;
1192 }
1193
1194 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1195 {
1196 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1197 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001198 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001199 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001200
1201 write_device_info_mmc(info);
1202 }
1203 memcpy(dev, info, sizeof(device_info));
1204}
1205
1206void write_device_info_flash(device_info *dev)
1207{
1208 struct device_info *info = (void *) info_buf;
1209 struct ptentry *ptn;
1210 struct ptable *ptable;
1211
1212 ptable = flash_get_ptable();
1213 if (ptable == NULL)
1214 {
1215 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1216 return;
1217 }
1218
1219 ptn = ptable_find(ptable, "devinfo");
1220 if (ptn == NULL)
1221 {
1222 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1223 return;
1224 }
1225
1226 memcpy(info, dev, sizeof(device_info));
1227
1228 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1229 {
1230 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1231 return;
1232 }
1233}
1234
1235void read_device_info_flash(device_info *dev)
1236{
1237 struct device_info *info = (void*) info_buf;
1238 struct ptentry *ptn;
1239 struct ptable *ptable;
1240
1241 ptable = flash_get_ptable();
1242 if (ptable == NULL)
1243 {
1244 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1245 return;
1246 }
1247
1248 ptn = ptable_find(ptable, "devinfo");
1249 if (ptn == NULL)
1250 {
1251 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1252 return;
1253 }
1254
1255 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1256 {
1257 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1258 return;
1259 }
1260
1261 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1262 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001263 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1264 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001265 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001266 write_device_info_flash(info);
1267 }
1268 memcpy(dev, info, sizeof(device_info));
1269}
1270
1271void write_device_info(device_info *dev)
1272{
1273 if(target_is_emmc_boot())
1274 {
1275 write_device_info_mmc(dev);
1276 }
1277 else
1278 {
1279 write_device_info_flash(dev);
1280 }
1281}
1282
1283void read_device_info(device_info *dev)
1284{
1285 if(target_is_emmc_boot())
1286 {
1287 read_device_info_mmc(dev);
1288 }
1289 else
1290 {
1291 read_device_info_flash(dev);
1292 }
1293}
1294
1295void reset_device_info()
1296{
1297 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001298 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001299 write_device_info(&device);
1300}
1301
1302void set_device_root()
1303{
1304 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001305 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001306 write_device_info(&device);
1307}
1308
Amol Jadicb524072012-08-09 16:40:18 -07001309#if DEVICE_TREE
1310int copy_dtb(uint8_t *boot_image_start)
1311{
1312 uint32 dt_image_offset = 0;
1313 uint32_t n;
1314 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001315 struct dt_entry dt_entry;
Amol Jadicb524072012-08-09 16:40:18 -07001316
1317 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1318
Amol Jadicb524072012-08-09 16:40:18 -07001319 if(hdr->dt_size != 0) {
1320
1321 /* add kernel offset */
1322 dt_image_offset += page_size;
1323 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1324 dt_image_offset += n;
1325
1326 /* add ramdisk offset */
1327 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1328 dt_image_offset += n;
1329
1330 /* add second offset */
1331 if(hdr->second_size != 0) {
1332 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1333 dt_image_offset += n;
1334 }
1335
1336 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001337 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001338
Joel Kingaa335dc2013-06-03 16:11:08 -07001339 if (dev_tree_validate(table, hdr->page_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001340 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1341 return -1;
1342 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001343 /* Find index of device tree within device tree table */
1344 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001345 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1346 return -1;
1347 }
1348
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001349 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001350 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001351 {
1352 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1353 return -1;
1354 }
1355
Amol Jadicb524072012-08-09 16:40:18 -07001356 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001357 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001358 boot_image_start + dt_image_offset + dt_entry.offset,
1359 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001360 } else
1361 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001362
1363 /* Everything looks fine. Return success. */
1364 return 0;
1365}
1366#endif
1367
Brian Swetland9c4c0752009-01-25 16:23:50 -08001368void cmd_boot(const char *arg, void *data, unsigned sz)
1369{
1370 unsigned kernel_actual;
1371 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001372 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001373 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001374 int ret = 0;
1375 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001376
1377 if (sz < sizeof(hdr)) {
1378 fastboot_fail("invalid bootimage header");
1379 return;
1380 }
1381
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001382 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001383
1384 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001385 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001386
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001387 if(target_is_emmc_boot() && hdr->page_size) {
1388 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001389 page_mask = page_size - 1;
1390 }
1391
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001392 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1393 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1394
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001395 /*
1396 * Update the kernel/ramdisk/tags address if the boot image header
1397 * has default values, these default values come from mkbootimg when
1398 * the boot image is flashed using fastboot flash:raw
1399 */
1400 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001401
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001402 /* Get virtual addresses since the hdr saves physical addresses. */
1403 hdr->kernel_addr = VA(hdr->kernel_addr);
1404 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1405 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001406
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001407 /* Check if the addresses in the header are valid. */
1408 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1409 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1410 {
1411 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1412 return -1;
1413 }
1414
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001415 /* sz should have atleast raw boot image */
1416 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001417 fastboot_fail("incomplete bootimage");
1418 return;
1419 }
1420
Amol Jadicb524072012-08-09 16:40:18 -07001421#if DEVICE_TREE
1422 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001423 ret = copy_dtb(data);
1424
1425 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001426#else
1427 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1428 {
1429 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1430 return -1;
1431 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001432#endif
1433
1434 /* Load ramdisk & kernel */
1435 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1436 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1437
1438#if DEVICE_TREE
1439 /*
1440 * If dtb is not found look for appended DTB in the kernel.
1441 * If appended dev tree is found, update the atags with
1442 * memory address to the DTB appended location on RAM.
1443 * Else update with the atags address in the kernel header
1444 */
1445 if (!dtb_copied) {
1446 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001447 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1448 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001449 if (!dtb) {
1450 fastboot_fail("dtb not found");
1451 return;
1452 }
Amol Jadicb524072012-08-09 16:40:18 -07001453 }
1454#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001455
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001456#ifndef DEVICE_TREE
1457 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1458 {
1459 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1460 return -1;
1461 }
1462#endif
1463
Brian Swetland9c4c0752009-01-25 16:23:50 -08001464 fastboot_okay("");
1465 udc_stop();
1466
Dima Zavin77e41f32013-03-06 16:10:43 -08001467 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001468 (const char*) hdr->cmdline, board_machtype(),
1469 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001470}
1471
Dima Zavin214cc642009-01-26 11:16:21 -08001472void cmd_erase(const char *arg, void *data, unsigned sz)
1473{
1474 struct ptentry *ptn;
1475 struct ptable *ptable;
1476
1477 ptable = flash_get_ptable();
1478 if (ptable == NULL) {
1479 fastboot_fail("partition table doesn't exist");
1480 return;
1481 }
1482
1483 ptn = ptable_find(ptable, arg);
1484 if (ptn == NULL) {
1485 fastboot_fail("unknown partition name");
1486 return;
1487 }
1488
1489 if (flash_erase(ptn)) {
1490 fastboot_fail("failed to erase partition");
1491 return;
1492 }
1493 fastboot_okay("");
1494}
1495
Bikas Gurungd48bd242010-09-04 19:54:32 -07001496
1497void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1498{
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001499 BUF_DMA_ALIGN(out, 512);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001500 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001501 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001502
Kinson Chikf1a43512011-07-14 11:28:39 -07001503 index = partition_get_index(arg);
1504 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001505
Kinson Chikf1a43512011-07-14 11:28:39 -07001506 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001507 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001508 return;
1509 }
neetidb4b24d62012-01-20 12:13:09 -08001510 /* Simple inefficient version of erase. Just writing
1511 0 in first block */
1512 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1513 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001514 return;
1515 }
1516 fastboot_okay("");
1517}
1518
1519
Ajay Dudani5c761132011-04-07 20:19:04 -07001520void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001521{
1522 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001523 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001524 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001525
Greg Grisco6e754772011-06-23 12:19:39 -07001526 if (!strcmp(arg, "partition"))
1527 {
1528 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001529 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001530 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001531 return;
1532 }
1533 }
Greg Grisco6e754772011-06-23 12:19:39 -07001534 else
1535 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001536 index = partition_get_index(arg);
1537 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001538 if(ptn == 0) {
1539 fastboot_fail("partition table doesn't exist");
1540 return;
1541 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001542
Greg Grisco6e754772011-06-23 12:19:39 -07001543 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1544 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1545 fastboot_fail("image is not a boot image");
1546 return;
1547 }
1548 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001549
Kinson Chikf1a43512011-07-14 11:28:39 -07001550 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001551 if (ROUND_TO_PAGE(sz,511) > size) {
1552 fastboot_fail("size too large");
1553 return;
1554 }
1555 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1556 fastboot_fail("flash write failure");
1557 return;
1558 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001559 }
1560 fastboot_okay("");
1561 return;
1562}
1563
Ajay Dudani5c761132011-04-07 20:19:04 -07001564void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1565{
1566 unsigned int chunk;
1567 unsigned int chunk_data_sz;
1568 sparse_header_t *sparse_header;
1569 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001570 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001571 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301572 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001573 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001574
Kinson Chikf1a43512011-07-14 11:28:39 -07001575 index = partition_get_index(arg);
1576 ptn = partition_get_offset(index);
1577 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001578 fastboot_fail("partition table doesn't exist");
1579 return;
1580 }
1581
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301582 size = partition_get_size(index);
1583 if (ROUND_TO_PAGE(sz,511) > size) {
1584 fastboot_fail("size too large");
1585 return;
1586 }
1587
Ajay Dudani5c761132011-04-07 20:19:04 -07001588 /* Read and skip over sparse image header */
1589 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001590 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1591 fastboot_fail("size too large");
1592 return;
1593 }
1594
Ajay Dudani5c761132011-04-07 20:19:04 -07001595 data += sparse_header->file_hdr_sz;
1596 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1597 {
1598 /* Skip the remaining bytes in a header that is longer than
1599 * we expected.
1600 */
1601 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1602 }
1603
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001604 dprintf (SPEW, "=== Sparse Image Header ===\n");
1605 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1606 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1607 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1608 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1609 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1610 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1611 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1612 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001613
1614 /* Start processing chunks */
1615 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1616 {
1617 /* Read and skip over chunk header */
1618 chunk_header = (chunk_header_t *) data;
1619 data += sizeof(chunk_header_t);
1620
1621 dprintf (SPEW, "=== Chunk Header ===\n");
1622 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1623 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1624 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1625
1626 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1627 {
1628 /* Skip the remaining bytes in a header that is longer than
1629 * we expected.
1630 */
1631 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1632 }
1633
1634 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1635 switch (chunk_header->chunk_type)
1636 {
1637 case CHUNK_TYPE_RAW:
1638 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1639 chunk_data_sz))
1640 {
1641 fastboot_fail("Bogus chunk size for chunk type Raw");
1642 return;
1643 }
1644
Ajay Dudaniab18f022011-05-12 14:39:22 -07001645 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1646 chunk_data_sz,
1647 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001648 {
1649 fastboot_fail("flash write failure");
1650 return;
1651 }
1652 total_blocks += chunk_header->chunk_sz;
1653 data += chunk_data_sz;
1654 break;
1655
1656 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001657 total_blocks += chunk_header->chunk_sz;
1658 break;
1659
Ajay Dudani5c761132011-04-07 20:19:04 -07001660 case CHUNK_TYPE_CRC:
1661 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1662 {
1663 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1664 return;
1665 }
1666 total_blocks += chunk_header->chunk_sz;
1667 data += chunk_data_sz;
1668 break;
1669
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001670 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001671 fastboot_fail("Unknown chunk type");
1672 return;
1673 }
1674 }
1675
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001676 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1677 total_blocks, sparse_header->total_blks);
1678
1679 if(total_blocks != sparse_header->total_blks)
1680 {
1681 fastboot_fail("sparse image write failure");
1682 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001683
1684 fastboot_okay("");
1685 return;
1686}
1687
1688void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1689{
1690 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001691 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1692 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001693
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001694#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001695 int ret=0;
1696 uint32 major_version=0;
1697 uint32 minor_version=0;
1698
1699 ret = scm_svc_version(&major_version,&minor_version);
1700 if(!ret)
1701 {
1702 if(major_version >= 2)
1703 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001704 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001705 {
1706 ret = encrypt_scm((uint32 **) &data, &sz);
1707 if (ret != 0) {
1708 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1709 return;
1710 }
1711
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001712 /* Protect only for SSD */
1713 if (!strcmp(arg, "ssd")) {
1714 ret = scm_protect_keystore((uint32 *) data, sz);
1715 if (ret != 0) {
1716 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1717 return;
1718 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001719 }
1720 }
1721 else
1722 {
1723 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1724 if(ret != 0)
1725 {
1726 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1727 return;
1728 }
1729 }
1730 }
1731 else
1732 {
1733 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1734 magic_number[1] == DECRYPT_MAGIC_1)
1735 {
1736 ret = decrypt_scm((uint32 **) &data, &sz);
1737 if (ret != 0) {
1738 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1739 return;
1740 }
1741 }
1742 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1743 magic_number[1] == ENCRYPT_MAGIC_1)
1744 {
1745 ret = encrypt_scm((uint32 **) &data, &sz);
1746 if (ret != 0) {
1747 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1748 return;
1749 }
1750 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001751 }
1752 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001753 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001754 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001755 dprintf(CRITICAL,"INVALID SVC Version\n");
1756 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001757 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001758#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001759
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001760 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001761 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1762 cmd_flash_mmc_img(arg, data, sz);
1763 else
1764 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001765 return;
1766}
1767
Dima Zavin214cc642009-01-26 11:16:21 -08001768void cmd_flash(const char *arg, void *data, unsigned sz)
1769{
1770 struct ptentry *ptn;
1771 struct ptable *ptable;
1772 unsigned extra = 0;
1773
1774 ptable = flash_get_ptable();
1775 if (ptable == NULL) {
1776 fastboot_fail("partition table doesn't exist");
1777 return;
1778 }
1779
1780 ptn = ptable_find(ptable, arg);
1781 if (ptn == NULL) {
1782 fastboot_fail("unknown partition name");
1783 return;
1784 }
1785
1786 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1787 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1788 fastboot_fail("image is not a boot image");
1789 return;
1790 }
1791 }
1792
Amol Jadi5c61a952012-05-04 17:05:35 -07001793 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001794 || !strcmp(ptn->name, "userdata")
1795 || !strcmp(ptn->name, "persist")
1796 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301797 if (flash_ecc_bch_enabled())
1798 /* Spare data bytes for 8 bit ECC increased by 4 */
1799 extra = ((page_size >> 9) * 20);
1800 else
1801 extra = ((page_size >> 9) * 16);
1802 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001803 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001804
1805 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1806 if (flash_write(ptn, extra, data, sz)) {
1807 fastboot_fail("flash write failure");
1808 return;
1809 }
1810 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1811 fastboot_okay("");
1812}
1813
1814void cmd_continue(const char *arg, void *data, unsigned sz)
1815{
1816 fastboot_okay("");
1817 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001818 if (target_is_emmc_boot())
1819 {
1820 boot_linux_from_mmc();
1821 }
1822 else
1823 {
1824 boot_linux_from_flash();
1825 }
Dima Zavin214cc642009-01-26 11:16:21 -08001826}
1827
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001828void cmd_reboot(const char *arg, void *data, unsigned sz)
1829{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001830 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001831 fastboot_okay("");
1832 reboot_device(0);
1833}
1834
1835void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1836{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001837 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001838 fastboot_okay("");
1839 reboot_device(FASTBOOT_MODE);
1840}
1841
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001842void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
1843{
1844 dprintf(INFO, "Enabling charger screen check\n");
1845 device.charger_screen_enabled = 1;
1846 write_device_info(&device);
1847 fastboot_okay("");
1848}
1849
1850void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
1851{
1852 dprintf(INFO, "Disabling charger screen check\n");
1853 device.charger_screen_enabled = 0;
1854 write_device_info(&device);
1855 fastboot_okay("");
1856}
1857
Shashank Mittal162244e2011-08-08 19:01:25 -07001858void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1859{
1860 if(!device.is_unlocked)
1861 {
1862 device.is_unlocked = 1;
1863 write_device_info(&device);
1864 }
1865 fastboot_okay("");
1866}
1867
Shashank Mittala0032282011-08-26 14:50:11 -07001868void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1869{
1870 char response[64];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001871 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07001872 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001873 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1874 fastboot_info(response);
1875 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07001876 fastboot_info(response);
1877 fastboot_okay("");
1878}
1879
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001880void cmd_preflash(const char *arg, void *data, unsigned sz)
1881{
1882 fastboot_okay("");
1883}
1884
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001885void splash_screen ()
1886{
1887 struct ptentry *ptn;
1888 struct ptable *ptable;
1889 struct fbcon_config *fb_display = NULL;
1890
1891 if (!target_is_emmc_boot())
1892 {
1893 ptable = flash_get_ptable();
1894 if (ptable == NULL) {
1895 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001896 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001897 }
1898
1899 ptn = ptable_find(ptable, "splash");
1900 if (ptn == NULL) {
1901 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1902 } else {
1903 fb_display = fbcon_display();
1904 if (fb_display) {
1905 if (flash_read(ptn, 0, fb_display->base,
1906 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1907 fbcon_clear();
1908 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1909 }
1910 }
1911 }
1912 }
1913}
1914
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001915/* Get the size from partiton name */
1916static void get_partition_size(const char *arg, char *response)
1917{
1918 uint64_t ptn = 0;
1919 uint64_t size;
1920 int index = INVALID_PTN;
1921
1922 index = partition_get_index(arg);
1923
1924 if (index == INVALID_PTN)
1925 {
1926 dprintf(CRITICAL, "Invalid partition index\n");
1927 return;
1928 }
1929
1930 ptn = partition_get_offset(index);
1931
1932 if(!ptn)
1933 {
1934 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1935 return;
1936 }
1937
1938 size = partition_get_size(index);
1939
1940 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1941 return;
1942}
1943
1944/*
1945 * Publish the partition type & size info
1946 * fastboot getvar will publish the required information.
1947 * fastboot getvar partition_size:<partition_name>: partition size in hex
1948 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1949 */
1950static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1951{
1952 uint8_t i;
1953
1954 for (i = 0; i < num_parts; i++) {
1955 get_partition_size(info[i].part_name, info[i].size_response);
1956
1957 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1958 {
1959 dprintf(CRITICAL, "partition size name truncated\n");
1960 return;
1961 }
1962 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1963 {
1964 dprintf(CRITICAL, "partition type name truncated\n");
1965 return;
1966 }
1967
1968 /* publish partition size & type info */
1969 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1970 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1971 }
1972}
1973
Amol Jadi5edf3552013-07-23 14:15:34 -07001974/* register commands and variables for fastboot */
1975void aboot_fastboot_register_commands(void)
1976{
1977 if (target_is_emmc_boot())
1978 {
1979 fastboot_register("flash:", cmd_flash_mmc);
1980 fastboot_register("erase:", cmd_erase_mmc);
1981 }
1982 else
1983 {
1984 fastboot_register("flash:", cmd_flash);
1985 fastboot_register("erase:", cmd_erase);
1986 }
1987
1988 fastboot_register("boot", cmd_boot);
1989 fastboot_register("continue", cmd_continue);
1990 fastboot_register("reboot", cmd_reboot);
1991 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
1992 fastboot_register("oem unlock", cmd_oem_unlock);
1993 fastboot_register("oem device-info", cmd_oem_devinfo);
1994 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001995 fastboot_register("oem enable-charger-screen",
1996 cmd_oem_enable_charger_screen);
1997 fastboot_register("oem disable-charger-screen",
1998 cmd_oem_disable_charger_screen);
Amol Jadi5edf3552013-07-23 14:15:34 -07001999 /* publish variables and their values */
2000 fastboot_publish("product", TARGET(BOARD));
2001 fastboot_publish("kernel", "lk");
2002 fastboot_publish("serialno", sn_buf);
2003
2004 /*
2005 * partition info is supported only for emmc partitions
2006 * Calling this for NAND prints some error messages which
2007 * is harmless but misleading. Avoid calling this for NAND
2008 * devices.
2009 */
2010 if (target_is_emmc_boot())
2011 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2012
2013 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002014 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2015 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002016 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002017 /* Is the charger screen check enabled */
2018 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2019 device.charger_screen_enabled);
2020 fastboot_publish("charger-screen-enabled",
2021 (const char *) charger_screen_enabled);
Amol Jadi5edf3552013-07-23 14:15:34 -07002022}
2023
Brian Swetland9c4c0752009-01-25 16:23:50 -08002024void aboot_init(const struct app_descriptor *app)
2025{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002026 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002027 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002028
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002029 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002030 if (target_is_emmc_boot())
2031 {
2032 page_size = 2048;
2033 page_mask = page_size - 1;
2034 }
2035 else
2036 {
2037 page_size = flash_page_size();
2038 page_mask = page_size - 1;
2039 }
2040
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002041 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2042
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002043 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002044
Greg Griscod6250552011-06-29 14:40:23 -07002045 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002046 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002047
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002048 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002049 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002050 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002051 dprintf(ALWAYS,"dload mode key sequence detected\n");
2052 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002053 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002054 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002055 }
2056 else
2057 {
2058 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002059 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002060 }
2061 boot_into_fastboot = true;
2062 }
2063 if (!boot_into_fastboot)
2064 {
2065 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2066 boot_into_recovery = 1;
2067 if (!boot_into_recovery &&
2068 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002069 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002070 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002071 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002072 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002073 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002074 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002075
Ajay Dudani77421292010-10-27 19:34:06 -07002076 reboot_mode = check_reboot_mode();
2077 if (reboot_mode == RECOVERY_MODE) {
2078 boot_into_recovery = 1;
2079 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002080 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002081 }
2082
Pavel Nedev5d91d412013-04-29 11:34:24 +03002083 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002084 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002085 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002086 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002087 if(emmc_recovery_init())
2088 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2089 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002090 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002091 if((device.is_unlocked) || (device.is_tampered))
2092 {
2093 #ifdef TZ_TAMPER_FUSE
2094 set_tamper_fuse_cmd();
2095 #endif
2096 #if USE_PCOM_SECBOOT
2097 set_tamper_flag(device.is_tampered);
2098 #endif
2099 }
Shashank Mittala0032282011-08-26 14:50:11 -07002100 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002101 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002102 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002103 else
2104 {
2105 recovery_init();
2106 #if USE_PCOM_SECBOOT
2107 if((device.is_unlocked) || (device.is_tampered))
2108 set_tamper_flag(device.is_tampered);
2109 #endif
2110 boot_linux_from_flash();
2111 }
2112 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2113 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002114 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002115
Amol Jadi5edf3552013-07-23 14:15:34 -07002116 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002117
Amol Jadi5edf3552013-07-23 14:15:34 -07002118 /* register aboot specific fastboot commands */
2119 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002120
Amol Jadi5edf3552013-07-23 14:15:34 -07002121 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002122 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002123
2124 /* initialize and start fastboot */
2125 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002126}
2127
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002128uint32_t get_page_size()
2129{
2130 return page_size;
2131}
2132
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002133/*
2134 * Calculated and save hash (SHA256) for non-signed boot image.
2135 *
2136 * Hash the same data that is checked on the signed boot image.
2137 * Kernel and Ramdisk are already read to memory buffers.
2138 * Need to read the entire device-tree from mmc
2139 * since non-signed image only read the DT tags of the relevant platform.
2140 *
2141 * @param kernel_addr - kernel bufer
2142 * @param kernel_actual - kernel size in bytes
2143 * @param ramdisk_addr - ramdisk buffer
2144 * @param ramdisk_actual - ramdisk size
2145 * @param ptn - partition
2146 * @param dt_offset - device tree offset on mmc partition
2147 * @param dt_size
2148 *
2149 * @return int - 0 on success, negative value on failure.
2150 */
2151int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2152 void *ramdisk_addr, unsigned ramdisk_actual,
2153 unsigned long long ptn,
2154 unsigned dt_offset, unsigned dt_size)
2155{
2156 SHA256_CTX sha256_ctx;
2157 char digest[32]={0};
2158 char *buf = (char *)target_get_scratch_address();
2159 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2160 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2161
2162 SHA256_Init(&sha256_ctx);
2163
2164 /* Read Boot Header */
2165 if (mmc_read(ptn, buf, page_size))
2166 {
2167 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2168 return -1;
2169 }
2170 /* Read entire Device Tree */
2171 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2172 {
2173 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2174 return -1;
2175 }
2176 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2177 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2178 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2179 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2180
2181 SHA256_Final(digest, &sha256_ctx);
2182
2183 save_kernel_hash_cmd(digest);
2184 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2185
2186 return 0;
2187}
2188
Brian Swetland9c4c0752009-01-25 16:23:50 -08002189APP_START(aboot)
2190 .init = aboot_init,
2191APP_END