blob: 24eec54142d925df9840cd9badc7a573d08cedbe [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>
36#include <dev/udc.h>
37#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070038#include <stdlib.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"
Brian Swetland9c4c0752009-01-25 16:23:50 -080069
Shashank Mittal162244e2011-08-08 19:01:25 -070070#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070071
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070072extern bool target_use_signed_kernel(void);
73extern void dsb();
74extern void isb();
75extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070076extern void target_uninit(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070077
78void write_device_info_mmc(device_info *dev);
79void write_device_info_flash(device_info *dev);
80
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070081#define EXPAND(NAME) #NAME
82#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070083
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080084#ifdef MEMBASE
85#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
86#else
David Ng183a7422009-12-07 14:55:21 -080087#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080088#endif
89
Deepa Dinamani0e163a42013-05-24 17:08:15 -070090#ifndef MEMSIZE
91#define MEMSIZE 1024*1024
92#endif
93
94#define MAX_TAGS_SIZE 1024
95
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080096#define RECOVERY_MODE 0x77665502
97#define FASTBOOT_MODE 0x77665500
98
David Ng183a7422009-12-07 14:55:21 -080099static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800100static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300101static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800102static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800103static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700104static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300105static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800106
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800107static const char *baseband_apq = " androidboot.baseband=apq";
108static const char *baseband_msm = " androidboot.baseband=msm";
109static const char *baseband_csfb = " androidboot.baseband=csfb";
110static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700111static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700112static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800113static const char *baseband_dsda = " androidboot.baseband=dsda";
114static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800115static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800116
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700117static unsigned page_size = 0;
118static unsigned page_mask = 0;
119static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
120static bool boot_into_ffbm;
121
Shashank Mittalcd98d472011-08-02 14:29:24 -0700122/* Assuming unauthorized kernel image by default */
123static int auth_kernel_img = 0;
124
Shashank Mittal162244e2011-08-08 19:01:25 -0700125static device_info device = {DEVICE_MAGIC, 0, 0};
126
Brian Swetland9c4c0752009-01-25 16:23:50 -0800127static struct udc_device surf_udc_device = {
128 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -0800129 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800130 .version_id = 0x0100,
131 .manufacturer = "Google",
132 .product = "Android",
133};
134
Dima Zavin42168f22009-01-30 11:52:22 -0800135struct atag_ptbl_entry
136{
137 char name[16];
138 unsigned offset;
139 unsigned size;
140 unsigned flags;
141};
142
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700143/*
144 * Partition info, required to be published
145 * for fastboot
146 */
147struct getvar_partition_info {
148 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
149 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
150 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
151 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
152 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
153};
154
155/*
156 * Right now, we are publishing the info for only
157 * three partitions
158 */
159struct getvar_partition_info part_info[] =
160{
161 { "system" , "partition-size:", "partition-type:", "", "ext4" },
162 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
163 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
164};
165
166char max_download_size[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800167char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700168
Greg Griscod2471ef2011-07-14 13:00:42 -0700169extern int emmc_recovery_init(void);
170
Kinson Chik0b1c8162011-08-31 16:31:57 -0700171#if NO_KEYPAD_DRIVER
172extern int fastboot_trigger(void);
173#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700174
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700175static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
176{
177 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700178#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
179 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
180 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
181 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700182#endif
183}
184
Dima Zavin42168f22009-01-30 11:52:22 -0800185static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
186{
187 struct atag_ptbl_entry atag_ptn;
188
189 memcpy(atag_ptn.name, ptn->name, 16);
190 atag_ptn.name[15] = '\0';
191 atag_ptn.offset = ptn->start;
192 atag_ptn.size = ptn->length;
193 atag_ptn.flags = ptn->flags;
194 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
195 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
196}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800197
Neeti Desaie245d492012-06-01 12:52:13 -0700198unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800199{
David Ng183a7422009-12-07 14:55:21 -0800200 int cmdline_len = 0;
201 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800202 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700203 int pause_at_bootup = 0;
Pavel Nedev5614d222013-06-17 18:01:02 +0300204 bool gpt_exists = partition_gpt_exists();
Dima Zavin42168f22009-01-30 11:52:22 -0800205
Brian Swetland9c4c0752009-01-25 16:23:50 -0800206 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800207 cmdline_len = strlen(cmdline);
208 have_cmdline = 1;
209 }
210 if (target_is_emmc_boot()) {
211 cmdline_len += strlen(emmc_cmdline);
212 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800213
214 cmdline_len += strlen(usb_sn_cmdline);
215 cmdline_len += strlen(sn_buf);
216
Pavel Nedev5614d222013-06-17 18:01:02 +0300217 if (boot_into_recovery && gpt_exists)
218 cmdline_len += strlen(secondary_gpt_enable);
219
Pavel Nedev328ac822013-04-05 15:25:11 +0300220 if (boot_into_ffbm) {
221 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700222 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800223 /* reduce kernel console messages to speed-up boot */
224 cmdline_len += strlen(loglevel);
Pavel Nedev328ac822013-04-05 15:25:11 +0300225 } else if (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
David Ng183a7422009-12-07 14:55:21 -0800274 if (cmdline_len > 0) {
275 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700276 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
277 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700278
Amol Jadi168b7712012-03-06 16:15:00 -0800279 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700280 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800281 if (have_cmdline) {
282 src = cmdline;
283 while ((*dst++ = *src++));
284 }
285 if (target_is_emmc_boot()) {
286 src = emmc_cmdline;
287 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700288 have_cmdline = 1;
289 while ((*dst++ = *src++));
290 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800291
292 src = usb_sn_cmdline;
293 if (have_cmdline) --dst;
294 have_cmdline = 1;
295 while ((*dst++ = *src++));
296 src = sn_buf;
297 if (have_cmdline) --dst;
298 have_cmdline = 1;
299 while ((*dst++ = *src++));
300
Pavel Nedev5614d222013-06-17 18:01:02 +0300301 if (boot_into_recovery && gpt_exists) {
302 src = secondary_gpt_enable;
303 if (have_cmdline) --dst;
304 while ((*dst++ = *src++));
305 }
306
Pavel Nedev328ac822013-04-05 15:25:11 +0300307 if (boot_into_ffbm) {
308 src = androidboot_mode;
309 if (have_cmdline) --dst;
310 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700311 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300312 if (have_cmdline) --dst;
313 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800314 src = loglevel;
315 if (have_cmdline) --dst;
316 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300317 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700318 src = battchg_pause;
319 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800320 while ((*dst++ = *src++));
321 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800322
Shashank Mittalcd98d472011-08-02 14:29:24 -0700323 if(target_use_signed_kernel() && auth_kernel_img) {
324 src = auth_kernel;
325 if (have_cmdline) --dst;
326 while ((*dst++ = *src++));
327 }
328
Ajay Dudanid04110c2011-01-17 23:55:07 -0800329 switch(target_baseband())
330 {
331 case BASEBAND_APQ:
332 src = baseband_apq;
333 if (have_cmdline) --dst;
334 while ((*dst++ = *src++));
335 break;
336
337 case BASEBAND_MSM:
338 src = baseband_msm;
339 if (have_cmdline) --dst;
340 while ((*dst++ = *src++));
341 break;
342
343 case BASEBAND_CSFB:
344 src = baseband_csfb;
345 if (have_cmdline) --dst;
346 while ((*dst++ = *src++));
347 break;
348
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800349 case BASEBAND_SVLTE2A:
350 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800351 if (have_cmdline) --dst;
352 while ((*dst++ = *src++));
353 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700354
355 case BASEBAND_MDM:
356 src = baseband_mdm;
357 if (have_cmdline) --dst;
358 while ((*dst++ = *src++));
359 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700360
361 case BASEBAND_SGLTE:
362 src = baseband_sglte;
363 if (have_cmdline) --dst;
364 while ((*dst++ = *src++));
365 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530366
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800367 case BASEBAND_SGLTE2:
368 src = baseband_sglte2;
369 if (have_cmdline) --dst;
370 while ((*dst++ = *src++));
371 break;
372
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530373 case BASEBAND_DSDA:
374 src = baseband_dsda;
375 if (have_cmdline) --dst;
376 while ((*dst++ = *src++));
377 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800378
379 case BASEBAND_DSDA2:
380 src = baseband_dsda2;
381 if (have_cmdline) --dst;
382 while ((*dst++ = *src++));
383 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800384 }
Neeti Desaie245d492012-06-01 12:52:13 -0700385 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800386 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700387 return cmdline_final;
388}
389
390unsigned *atag_core(unsigned *ptr)
391{
392 /* CORE */
393 *ptr++ = 2;
394 *ptr++ = 0x54410001;
395
396 return ptr;
397
398}
399
400unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
401 unsigned ramdisk_size)
402{
403 if (ramdisk_size) {
404 *ptr++ = 4;
405 *ptr++ = 0x54420005;
406 *ptr++ = (unsigned)ramdisk;
407 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800408 }
409
Neeti Desaie245d492012-06-01 12:52:13 -0700410 return ptr;
411}
412
413unsigned *atag_ptable(unsigned **ptr_addr)
414{
415 int i;
416 struct ptable *ptable;
417
418 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700419 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
420 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700421 *(*ptr_addr)++ = 0x4d534d70;
422 for (i = 0; i < ptable->count; ++i)
423 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
424 }
425
426 return (*ptr_addr);
427}
428
429unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
430{
431 int cmdline_length = 0;
432 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700433 char *dest;
434
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800435 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700436 n = (cmdline_length + 4) & (~3);
437
438 *ptr++ = (n / 4) + 2;
439 *ptr++ = 0x54410009;
440 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800441 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700442 ptr += (n / 4);
443
444 return ptr;
445}
446
447unsigned *atag_end(unsigned *ptr)
448{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800449 /* END */
450 *ptr++ = 0;
451 *ptr++ = 0;
452
Neeti Desaie245d492012-06-01 12:52:13 -0700453 return ptr;
454}
455
456void generate_atags(unsigned *ptr, const char *cmdline,
457 void *ramdisk, unsigned ramdisk_size)
458{
459
460 ptr = atag_core(ptr);
461 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
462 ptr = target_atag_mem(ptr);
463
464 /* Skip NAND partition ATAGS for eMMC boot */
465 if (!target_is_emmc_boot()){
466 ptr = atag_ptable(&ptr);
467 }
468
469 ptr = atag_cmdline(ptr, cmdline);
470 ptr = atag_end(ptr);
471}
472
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700473typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700474void boot_linux(void *kernel, unsigned *tags,
475 const char *cmdline, unsigned machtype,
476 void *ramdisk, unsigned ramdisk_size)
477{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800478 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800479#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700480 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800481#endif
482
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700483 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800484 uint32_t tags_phys = PA((addr_t)tags);
485
486 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700487
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800488 final_cmdline = update_cmdline((const char*)cmdline);
489
Neeti Desai17379b82012-06-04 18:42:53 -0700490#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800491 dprintf(INFO, "Updating device tree: start\n");
492
Neeti Desai17379b82012-06-04 18:42:53 -0700493 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800494 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700495 if(ret)
496 {
497 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
498 ASSERT(0);
499 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800500 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700501#else
Neeti Desaie245d492012-06-01 12:52:13 -0700502 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800503 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700504#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700505
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700506 /* Perform target specific cleanup */
507 target_uninit();
508
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800509 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
510 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800511
512 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700513
Amol Jadi4421e652011-06-16 15:00:48 -0700514 /* do any platform specific cleanup before kernel entry */
515 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700516
Brian Swetland9c4c0752009-01-25 16:23:50 -0800517 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700518
Amol Jadi504f9fe2012-08-16 13:56:48 -0700519#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800520 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700521#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700522 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700523 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800524}
525
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700526/* Function to check if the memory address range falls within the aboot
527 * boundaries.
528 * start: Start of the memory region
529 * size: Size of the memory region
530 */
531int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
532{
533 /* Check for boundary conditions. */
534 if ((start + size) < start)
535 return -1;
536
537 /* Check for memory overlap. */
538 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
539 return 0;
540 else if (start > (MEMBASE + MEMSIZE))
541 return 0;
542 else
543 return -1;
544}
545
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800546#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800547
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700548BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800549#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700550BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800551#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800552
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700553static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
554{
555 int ret;
556
557 /* Assume device is rooted at this time. */
558 device.is_tampered = 1;
559
560 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
561
562 ret = image_verify((unsigned char *)bootimg_addr,
563 (unsigned char *)(bootimg_addr + bootimg_size),
564 bootimg_size,
565 CRYPTO_AUTH_ALG_SHA256);
566
567 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
568
569 if (ret)
570 {
571 /* Authorized kernel */
572 device.is_tampered = 0;
573 }
574
575#if USE_PCOM_SECBOOT
576 set_tamper_flag(device.is_tampered);
577#endif
578
579 if(device.is_tampered)
580 {
581 write_device_info_mmc(&device);
582 #ifdef TZ_TAMPER_FUSE
583 set_tamper_fuse_cmd();
584 #endif
585 #ifdef ASSERT_ON_TAMPER
586 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
587 ASSERT(0);
588 #endif
589 }
590}
591
Shashank Mittal23b8f422010-04-16 19:27:21 -0700592int boot_linux_from_mmc(void)
593{
594 struct boot_img_hdr *hdr = (void*) buf;
595 struct boot_img_hdr *uhdr;
596 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700597 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700598 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700599 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700600
Shashank Mittalcd98d472011-08-02 14:29:24 -0700601 unsigned char *image_addr = 0;
602 unsigned kernel_actual;
603 unsigned ramdisk_actual;
604 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700605 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700606
607#if DEVICE_TREE
608 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700609 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700610 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800611 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700612#endif
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700613 if (!boot_into_recovery) {
614 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
615 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
616 if (rcode <= 0) {
617 boot_into_ffbm = false;
618 if (rcode < 0)
619 dprintf(CRITICAL,"failed to get ffbm cookie");
620 } else
621 boot_into_ffbm = true;
622 } else
623 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700624 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
625 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
626 dprintf(INFO, "Unified boot method!\n");
627 hdr = uhdr;
628 goto unified_boot;
629 }
Greg Griscod6250552011-06-29 14:40:23 -0700630 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700631 index = partition_get_index("boot");
632 ptn = partition_get_offset(index);
633 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700634 dprintf(CRITICAL, "ERROR: No boot partition found\n");
635 return -1;
636 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700637 }
638 else {
639 index = partition_get_index("recovery");
640 ptn = partition_get_offset(index);
641 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700642 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
643 return -1;
644 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700645 }
646
Greg Griscod6250552011-06-29 14:40:23 -0700647 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700648 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
649 return -1;
650 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700651
652 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700653 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700654 return -1;
655 }
656
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700657 if (hdr->page_size && (hdr->page_size != page_size)) {
658 page_size = hdr->page_size;
659 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700660 }
661
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700662 /*
663 * Update the kernel/ramdisk/tags address if the boot image header
664 * has default values, these default values come from mkbootimg when
665 * the boot image is flashed using fastboot flash:raw
666 */
667 update_ker_tags_rdisk_addr(hdr);
668
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700669 /* Get virtual addresses since the hdr saves physical addresses. */
670 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
671 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
672 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700673
674 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
675 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
676
677 /* Check if the addresses in the header are valid. */
678 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
679 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
680 {
681 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
682 return -1;
683 }
684
685#ifndef DEVICE_TREE
686 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
687 {
688 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
689 return -1;
690 }
691#endif
692
Shashank Mittalcd98d472011-08-02 14:29:24 -0700693 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300694 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
695 (int) target_use_signed_kernel(),
696 device.is_unlocked,
697 device.is_tampered);
698
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700699 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800700 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700701 offset = 0;
702
Shashank Mittalcd98d472011-08-02 14:29:24 -0700703 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700704
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800705#if DEVICE_TREE
706 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
707 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700708
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700709 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700710 {
711 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
712 return -1;
713 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800714#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530715 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700716
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700717#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700718
Amol Jadib6be5c12012-11-14 13:39:51 -0800719 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700720 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800721
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700722 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
723 {
724 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
725 return -1;
726 }
727
Shashank Mittalcd98d472011-08-02 14:29:24 -0700728 /* Read image without signature */
729 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
730 {
731 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
732 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800733 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700734
Amol Jadib6be5c12012-11-14 13:39:51 -0800735 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700736 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800737
Shashank Mittalcd98d472011-08-02 14:29:24 -0700738 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700739
740 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
741 {
742 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
743 return -1;
744 }
745
Shashank Mittalcd98d472011-08-02 14:29:24 -0700746 /* Read signature */
747 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
748 {
749 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700750 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700751 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800752
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700753 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700754
Neeti Desai465491e2012-07-31 12:53:35 -0700755 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700756 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
757 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700758
Neeti Desai465491e2012-07-31 12:53:35 -0700759 #if DEVICE_TREE
760 if(hdr->dt_size) {
761 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700762 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700763
764 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
765
Joel Kingaa335dc2013-06-03 16:11:08 -0700766 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700767 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
768 return -1;
769 }
Neeti Desai465491e2012-07-31 12:53:35 -0700770 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700771 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700772 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
773 return -1;
774 }
775
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700776 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700777 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700778 {
779 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
780 return -1;
781 }
782
Joel Kingaa335dc2013-06-03 16:11:08 -0700783 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800784 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800785 /*
786 * If appended dev tree is found, update the atags with
787 * memory address to the DTB appended location on RAM.
788 * Else update with the atags address in the kernel header
789 */
790 void *dtb;
791 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700792 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800793 if (!dtb) {
794 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
795 return -1;
796 }
Neeti Desai465491e2012-07-31 12:53:35 -0700797 }
798 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700799 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700800 else
801 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800802 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700803
Amol Jadib6be5c12012-11-14 13:39:51 -0800804 dprintf(INFO, "Loading boot image (%d): start\n",
805 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700806 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800807
808 offset = page_size;
809
810 /* Load kernel */
811 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700812 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
813 return -1;
814 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800815 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700816
Amol Jadib6be5c12012-11-14 13:39:51 -0800817 /* Load ramdisk */
818 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700819 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800820 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700821 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
822 return -1;
823 }
824 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800825 offset += ramdisk_actual;
826
827 dprintf(INFO, "Loading boot image (%d): done\n",
828 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700829 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700830
831 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800832 offset += second_actual;
833 /* Second image loading not implemented. */
834 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700835 }
836
837 #if DEVICE_TREE
838 if(hdr->dt_size != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700839 /* Read the device tree table into buffer */
840 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
841 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
842 return -1;
843 }
844 table = (struct dt_table*) dt_buf;
845
Joel Kingaa335dc2013-06-03 16:11:08 -0700846 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700847 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
848 return -1;
849 }
850
Joel Kingaa335dc2013-06-03 16:11:08 -0700851 /* Find index of device tree within device tree table */
852 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700853 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
854 return -1;
855 }
856
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700857 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700858 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700859 {
860 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
861 return -1;
862 }
863
Joel Kingaa335dc2013-06-03 16:11:08 -0700864 if(mmc_read(ptn + offset + dt_entry.offset,
865 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700866 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
867 return -1;
868 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300869 #ifdef TZ_SAVE_KERNEL_HASH
870 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
871 hdr->ramdisk_addr, ramdisk_actual,
872 ptn, offset, hdr->dt_size);
873 #endif /* TZ_SAVE_KERNEL_HASH */
874
Channagoud Kadabi35095622013-03-01 13:53:05 -0800875 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800876 /*
877 * If appended dev tree is found, update the atags with
878 * memory address to the DTB appended location on RAM.
879 * Else update with the atags address in the kernel header
880 */
881 void *dtb;
882 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700883 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800884 if (!dtb) {
885 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
886 return -1;
887 }
Neeti Desai465491e2012-07-31 12:53:35 -0700888 }
889 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700890 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700891
892unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700893
Dima Zavin77e41f32013-03-06 16:10:43 -0800894 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700895 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700896 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
897
898 return 0;
899}
900
Dima Zavin214cc642009-01-26 11:16:21 -0800901int boot_linux_from_flash(void)
902{
903 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800904 struct ptentry *ptn;
905 struct ptable *ptable;
906 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800907
Shashank Mittalcd98d472011-08-02 14:29:24 -0700908 unsigned char *image_addr = 0;
909 unsigned kernel_actual;
910 unsigned ramdisk_actual;
911 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800912 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700913
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700914#if DEVICE_TREE
915 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700916 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800917 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700918#endif
919
David Ng183a7422009-12-07 14:55:21 -0800920 if (target_is_emmc_boot()) {
921 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
922 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
923 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
924 return -1;
925 }
926 goto continue_boot;
927 }
928
Dima Zavin214cc642009-01-26 11:16:21 -0800929 ptable = flash_get_ptable();
930 if (ptable == NULL) {
931 dprintf(CRITICAL, "ERROR: Partition table not found\n");
932 return -1;
933 }
934
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800935 if(!boot_into_recovery)
936 {
937 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700938
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800939 if (ptn == NULL) {
940 dprintf(CRITICAL, "ERROR: No boot partition found\n");
941 return -1;
942 }
943 }
944 else
945 {
946 ptn = ptable_find(ptable, "recovery");
947 if (ptn == NULL) {
948 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
949 return -1;
950 }
Dima Zavin214cc642009-01-26 11:16:21 -0800951 }
952
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800953 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800954 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
955 return -1;
956 }
Dima Zavin214cc642009-01-26 11:16:21 -0800957
958 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700959 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800960 return -1;
961 }
962
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800963 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700964 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 -0800965 return -1;
966 }
967
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700968 /*
969 * Update the kernel/ramdisk/tags address if the boot image header
970 * has default values, these default values come from mkbootimg when
971 * the boot image is flashed using fastboot flash:raw
972 */
973 update_ker_tags_rdisk_addr(hdr);
974
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700975 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700976 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
977 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
978 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
979
980 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
981 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
982
983 /* Check if the addresses in the header are valid. */
984 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
985 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
986 {
987 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
988 return -1;
989 }
990
991#ifndef DEVICE_TREE
992 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
993 {
994 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
995 return -1;
996 }
997#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700998
Shashank Mittalcd98d472011-08-02 14:29:24 -0700999 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001000 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001001 {
1002 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001003 offset = 0;
1004
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001005#if DEVICE_TREE
1006 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1007 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001008
1009 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1010 {
1011 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1012 return -1;
1013 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001014#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001015 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001016#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001017
Amol Jadib6be5c12012-11-14 13:39:51 -08001018 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001019 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001020
Shashank Mittalcd98d472011-08-02 14:29:24 -07001021 /* Read image without signature */
1022 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1023 {
1024 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1025 return -1;
1026 }
Dima Zavin214cc642009-01-26 11:16:21 -08001027
Amol Jadib6be5c12012-11-14 13:39:51 -08001028 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001029 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001030
Shashank Mittalcd98d472011-08-02 14:29:24 -07001031 offset = imagesize_actual;
1032 /* Read signature */
1033 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1034 {
1035 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001036 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001037 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001038
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001039 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001040
1041 /* Move kernel and ramdisk to correct address */
1042 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1043 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001044#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001045 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001046 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001047 {
1048 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1049 return -1;
1050 }
1051
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001052 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1053#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001054
1055 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001056 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001057 {
1058 write_device_info_flash(&device);
1059 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301060#if USE_PCOM_SECBOOT
1061 set_tamper_flag(device.is_tampered);
1062#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001063 }
1064 else
1065 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001066 offset = page_size;
1067
Amol Jadib6be5c12012-11-14 13:39:51 -08001068 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1069 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1070 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1071
1072 dprintf(INFO, "Loading boot image (%d): start\n",
1073 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001074 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001075
1076 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001077 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1078 return -1;
1079 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001080 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001081
Amol Jadib6be5c12012-11-14 13:39:51 -08001082 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001083 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1084 return -1;
1085 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001086 offset += ramdisk_actual;
1087
1088 dprintf(INFO, "Loading boot image (%d): done\n",
1089 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001090 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001091
1092 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001093 offset += second_actual;
1094 /* Second image loading not implemented. */
1095 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001096 }
1097
1098#if DEVICE_TREE
1099 if(hdr->dt_size != 0) {
1100
1101 /* Read the device tree table into buffer */
1102 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1103 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1104 return -1;
1105 }
1106
1107 table = (struct dt_table*) dt_buf;
1108
Joel Kingaa335dc2013-06-03 16:11:08 -07001109 if (dev_tree_validate(table, hdr->page_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001110 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1111 return -1;
1112 }
1113
Joel Kingaa335dc2013-06-03 16:11:08 -07001114 /* Find index of device tree within device tree table */
1115 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001116 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1117 return -1;
1118 }
1119
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001120 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001121 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001122 {
1123 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1124 return -1;
1125 }
1126
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001127 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001128 if(flash_read(ptn, offset + dt_entry.offset,
1129 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001130 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1131 return -1;
1132 }
1133 }
1134#endif
1135
Shashank Mittalcd98d472011-08-02 14:29:24 -07001136 }
David Ng183a7422009-12-07 14:55:21 -08001137continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001138
Dima Zavin214cc642009-01-26 11:16:21 -08001139 /* TODO: create/pass atags to kernel */
1140
Ajay Dudanie28a6072011-07-01 13:59:46 -07001141 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001142 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001143 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1144
1145 return 0;
1146}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001147
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001148BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001149void write_device_info_mmc(device_info *dev)
1150{
1151 struct device_info *info = (void*) info_buf;
1152 unsigned long long ptn = 0;
1153 unsigned long long size;
1154 int index = INVALID_PTN;
1155
1156 index = partition_get_index("aboot");
1157 ptn = partition_get_offset(index);
1158 if(ptn == 0)
1159 {
1160 return;
1161 }
1162
1163 size = partition_get_size(index);
1164
1165 memcpy(info, dev, sizeof(device_info));
1166
1167 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1168 {
1169 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1170 return;
1171 }
1172}
1173
1174void read_device_info_mmc(device_info *dev)
1175{
1176 struct device_info *info = (void*) info_buf;
1177 unsigned long long ptn = 0;
1178 unsigned long long size;
1179 int index = INVALID_PTN;
1180
1181 index = partition_get_index("aboot");
1182 ptn = partition_get_offset(index);
1183 if(ptn == 0)
1184 {
1185 return;
1186 }
1187
1188 size = partition_get_size(index);
1189
1190 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1191 {
1192 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1193 return;
1194 }
1195
1196 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1197 {
1198 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1199 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001200 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001201
1202 write_device_info_mmc(info);
1203 }
1204 memcpy(dev, info, sizeof(device_info));
1205}
1206
1207void write_device_info_flash(device_info *dev)
1208{
1209 struct device_info *info = (void *) info_buf;
1210 struct ptentry *ptn;
1211 struct ptable *ptable;
1212
1213 ptable = flash_get_ptable();
1214 if (ptable == NULL)
1215 {
1216 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1217 return;
1218 }
1219
1220 ptn = ptable_find(ptable, "devinfo");
1221 if (ptn == NULL)
1222 {
1223 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1224 return;
1225 }
1226
1227 memcpy(info, dev, sizeof(device_info));
1228
1229 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1230 {
1231 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1232 return;
1233 }
1234}
1235
1236void read_device_info_flash(device_info *dev)
1237{
1238 struct device_info *info = (void*) info_buf;
1239 struct ptentry *ptn;
1240 struct ptable *ptable;
1241
1242 ptable = flash_get_ptable();
1243 if (ptable == NULL)
1244 {
1245 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1246 return;
1247 }
1248
1249 ptn = ptable_find(ptable, "devinfo");
1250 if (ptn == NULL)
1251 {
1252 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1253 return;
1254 }
1255
1256 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1257 {
1258 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1259 return;
1260 }
1261
1262 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1263 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001264 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1265 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001266 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001267 write_device_info_flash(info);
1268 }
1269 memcpy(dev, info, sizeof(device_info));
1270}
1271
1272void write_device_info(device_info *dev)
1273{
1274 if(target_is_emmc_boot())
1275 {
1276 write_device_info_mmc(dev);
1277 }
1278 else
1279 {
1280 write_device_info_flash(dev);
1281 }
1282}
1283
1284void read_device_info(device_info *dev)
1285{
1286 if(target_is_emmc_boot())
1287 {
1288 read_device_info_mmc(dev);
1289 }
1290 else
1291 {
1292 read_device_info_flash(dev);
1293 }
1294}
1295
1296void reset_device_info()
1297{
1298 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001299 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001300 write_device_info(&device);
1301}
1302
1303void set_device_root()
1304{
1305 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001306 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001307 write_device_info(&device);
1308}
1309
Amol Jadicb524072012-08-09 16:40:18 -07001310#if DEVICE_TREE
1311int copy_dtb(uint8_t *boot_image_start)
1312{
1313 uint32 dt_image_offset = 0;
1314 uint32_t n;
1315 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001316 struct dt_entry dt_entry;
Amol Jadicb524072012-08-09 16:40:18 -07001317
1318 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1319
Amol Jadicb524072012-08-09 16:40:18 -07001320 if(hdr->dt_size != 0) {
1321
1322 /* add kernel offset */
1323 dt_image_offset += page_size;
1324 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1325 dt_image_offset += n;
1326
1327 /* add ramdisk offset */
1328 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1329 dt_image_offset += n;
1330
1331 /* add second offset */
1332 if(hdr->second_size != 0) {
1333 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1334 dt_image_offset += n;
1335 }
1336
1337 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001338 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001339
Joel Kingaa335dc2013-06-03 16:11:08 -07001340 if (dev_tree_validate(table, hdr->page_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001341 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1342 return -1;
1343 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001344 /* Find index of device tree within device tree table */
1345 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001346 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1347 return -1;
1348 }
1349
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001350 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001351 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001352 {
1353 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1354 return -1;
1355 }
1356
Amol Jadicb524072012-08-09 16:40:18 -07001357 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001358 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001359 boot_image_start + dt_image_offset + dt_entry.offset,
1360 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001361 } else
1362 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001363
1364 /* Everything looks fine. Return success. */
1365 return 0;
1366}
1367#endif
1368
Brian Swetland9c4c0752009-01-25 16:23:50 -08001369void cmd_boot(const char *arg, void *data, unsigned sz)
1370{
1371 unsigned kernel_actual;
1372 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001373 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001374 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001375 int ret = 0;
1376 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001377
1378 if (sz < sizeof(hdr)) {
1379 fastboot_fail("invalid bootimage header");
1380 return;
1381 }
1382
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001383 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001384
1385 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001386 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001387
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001388 if(target_is_emmc_boot() && hdr->page_size) {
1389 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001390 page_mask = page_size - 1;
1391 }
1392
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001393 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1394 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1395
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001396 /*
1397 * Update the kernel/ramdisk/tags address if the boot image header
1398 * has default values, these default values come from mkbootimg when
1399 * the boot image is flashed using fastboot flash:raw
1400 */
1401 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001402
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001403 /* Get virtual addresses since the hdr saves physical addresses. */
1404 hdr->kernel_addr = VA(hdr->kernel_addr);
1405 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1406 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001407
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001408 /* Check if the addresses in the header are valid. */
1409 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1410 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1411 {
1412 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1413 return -1;
1414 }
1415
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001416 /* sz should have atleast raw boot image */
1417 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001418 fastboot_fail("incomplete bootimage");
1419 return;
1420 }
1421
Amol Jadicb524072012-08-09 16:40:18 -07001422#if DEVICE_TREE
1423 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001424 ret = copy_dtb(data);
1425
1426 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001427#else
1428 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1429 {
1430 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1431 return -1;
1432 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001433#endif
1434
1435 /* Load ramdisk & kernel */
1436 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1437 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1438
1439#if DEVICE_TREE
1440 /*
1441 * If dtb is not found look for appended DTB in the kernel.
1442 * If appended dev tree is found, update the atags with
1443 * memory address to the DTB appended location on RAM.
1444 * Else update with the atags address in the kernel header
1445 */
1446 if (!dtb_copied) {
1447 void *dtb;
Channagoud Kadabia83a6972013-04-25 15:26:04 -07001448 dtb = dev_tree_appended((void *)hdr->kernel_addr, (void *)hdr->tags_addr, hdr->kernel_size);
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
Shashank Mittal162244e2011-08-08 19:01:25 -07001842void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1843{
1844 if(!device.is_unlocked)
1845 {
1846 device.is_unlocked = 1;
1847 write_device_info(&device);
1848 }
1849 fastboot_okay("");
1850}
1851
Shashank Mittala0032282011-08-26 14:50:11 -07001852void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1853{
1854 char response[64];
1855 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1856 fastboot_info(response);
1857 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1858 fastboot_info(response);
1859 fastboot_okay("");
1860}
1861
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001862void cmd_preflash(const char *arg, void *data, unsigned sz)
1863{
1864 fastboot_okay("");
1865}
1866
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001867void splash_screen ()
1868{
1869 struct ptentry *ptn;
1870 struct ptable *ptable;
1871 struct fbcon_config *fb_display = NULL;
1872
1873 if (!target_is_emmc_boot())
1874 {
1875 ptable = flash_get_ptable();
1876 if (ptable == NULL) {
1877 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001878 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001879 }
1880
1881 ptn = ptable_find(ptable, "splash");
1882 if (ptn == NULL) {
1883 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1884 } else {
1885 fb_display = fbcon_display();
1886 if (fb_display) {
1887 if (flash_read(ptn, 0, fb_display->base,
1888 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1889 fbcon_clear();
1890 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1891 }
1892 }
1893 }
1894 }
1895}
1896
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001897/* Get the size from partiton name */
1898static void get_partition_size(const char *arg, char *response)
1899{
1900 uint64_t ptn = 0;
1901 uint64_t size;
1902 int index = INVALID_PTN;
1903
1904 index = partition_get_index(arg);
1905
1906 if (index == INVALID_PTN)
1907 {
1908 dprintf(CRITICAL, "Invalid partition index\n");
1909 return;
1910 }
1911
1912 ptn = partition_get_offset(index);
1913
1914 if(!ptn)
1915 {
1916 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1917 return;
1918 }
1919
1920 size = partition_get_size(index);
1921
1922 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1923 return;
1924}
1925
1926/*
1927 * Publish the partition type & size info
1928 * fastboot getvar will publish the required information.
1929 * fastboot getvar partition_size:<partition_name>: partition size in hex
1930 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1931 */
1932static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1933{
1934 uint8_t i;
1935
1936 for (i = 0; i < num_parts; i++) {
1937 get_partition_size(info[i].part_name, info[i].size_response);
1938
1939 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1940 {
1941 dprintf(CRITICAL, "partition size name truncated\n");
1942 return;
1943 }
1944 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1945 {
1946 dprintf(CRITICAL, "partition type name truncated\n");
1947 return;
1948 }
1949
1950 /* publish partition size & type info */
1951 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1952 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1953 }
1954}
1955
Brian Swetland9c4c0752009-01-25 16:23:50 -08001956void aboot_init(const struct app_descriptor *app)
1957{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001958 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001959 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001960 unsigned sz = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03001961 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001962
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001963 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001964 if (target_is_emmc_boot())
1965 {
1966 page_size = 2048;
1967 page_mask = page_size - 1;
1968 }
1969 else
1970 {
1971 page_size = flash_page_size();
1972 page_mask = page_size - 1;
1973 }
1974
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001975 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
1976
Shashank Mittal162244e2011-08-08 19:01:25 -07001977 if(target_use_signed_kernel())
1978 {
1979 read_device_info(&device);
1980
Shashank Mittal162244e2011-08-08 19:01:25 -07001981 }
1982
Greg Griscod6250552011-06-29 14:40:23 -07001983 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001984 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001985 surf_udc_device.serialno = sn_buf;
1986
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001987 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001988 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001989 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001990 dprintf(ALWAYS,"dload mode key sequence detected\n");
1991 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001992 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001993 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001994 }
1995 else
1996 {
1997 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001998 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001999 }
2000 boot_into_fastboot = true;
2001 }
2002 if (!boot_into_fastboot)
2003 {
2004 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2005 boot_into_recovery = 1;
2006 if (!boot_into_recovery &&
2007 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002008 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002009 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002010 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002011 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002012 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002013 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002014
Ajay Dudani77421292010-10-27 19:34:06 -07002015 reboot_mode = check_reboot_mode();
2016 if (reboot_mode == RECOVERY_MODE) {
2017 boot_into_recovery = 1;
2018 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002019 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002020 }
2021
Pavel Nedev5d91d412013-04-29 11:34:24 +03002022 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002023 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002024 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002025 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002026 if(emmc_recovery_init())
2027 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2028 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002029 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002030 if((device.is_unlocked) || (device.is_tampered))
2031 {
2032 #ifdef TZ_TAMPER_FUSE
2033 set_tamper_fuse_cmd();
2034 #endif
2035 #if USE_PCOM_SECBOOT
2036 set_tamper_flag(device.is_tampered);
2037 #endif
2038 }
Shashank Mittala0032282011-08-26 14:50:11 -07002039 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002040 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002041 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002042 else
2043 {
2044 recovery_init();
2045 #if USE_PCOM_SECBOOT
2046 if((device.is_unlocked) || (device.is_tampered))
2047 set_tamper_flag(device.is_tampered);
2048 #endif
2049 boot_linux_from_flash();
2050 }
2051 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2052 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002053 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002054
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002055 sz = target_get_max_flash_size();
2056
Shashank Mittal162244e2011-08-08 19:01:25 -07002057 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002058
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002059 if(!usb_init)
2060 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002061
2062 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002063
Shashank Mittal23b8f422010-04-16 19:27:21 -07002064 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002065 {
2066 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002067 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002068 }
2069 else
2070 {
2071 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002072 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002073 }
2074
2075 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002076 fastboot_register("reboot", cmd_reboot);
2077 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07002078 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07002079 fastboot_register("oem device-info", cmd_oem_devinfo);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002080 fastboot_register("preflash", cmd_preflash);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07002081 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08002082 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08002083 fastboot_publish("serialno", sn_buf);
Channagoud Kadabi873d8ff2013-07-01 17:31:40 -07002084 /*
2085 * fastboot publish is supported only for emmc partitions
2086 * Calling this for NAND prints some error messages which
2087 * is harmless but misleading. Avoid calling this for NAND
2088 * devices.
2089 */
2090 if (target_is_emmc_boot())
2091 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002092 /* Max download size supported */
2093 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", sz);
2094 fastboot_publish("max-download-size", (const char *) max_download_size);
Kinson Chikf1a43512011-07-14 11:28:39 -07002095 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07002096 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002097 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002098}
2099
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002100uint32_t get_page_size()
2101{
2102 return page_size;
2103}
2104
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002105/*
2106 * Calculated and save hash (SHA256) for non-signed boot image.
2107 *
2108 * Hash the same data that is checked on the signed boot image.
2109 * Kernel and Ramdisk are already read to memory buffers.
2110 * Need to read the entire device-tree from mmc
2111 * since non-signed image only read the DT tags of the relevant platform.
2112 *
2113 * @param kernel_addr - kernel bufer
2114 * @param kernel_actual - kernel size in bytes
2115 * @param ramdisk_addr - ramdisk buffer
2116 * @param ramdisk_actual - ramdisk size
2117 * @param ptn - partition
2118 * @param dt_offset - device tree offset on mmc partition
2119 * @param dt_size
2120 *
2121 * @return int - 0 on success, negative value on failure.
2122 */
2123int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2124 void *ramdisk_addr, unsigned ramdisk_actual,
2125 unsigned long long ptn,
2126 unsigned dt_offset, unsigned dt_size)
2127{
2128 SHA256_CTX sha256_ctx;
2129 char digest[32]={0};
2130 char *buf = (char *)target_get_scratch_address();
2131 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2132 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2133
2134 SHA256_Init(&sha256_ctx);
2135
2136 /* Read Boot Header */
2137 if (mmc_read(ptn, buf, page_size))
2138 {
2139 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2140 return -1;
2141 }
2142 /* Read entire Device Tree */
2143 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2144 {
2145 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2146 return -1;
2147 }
2148 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2149 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2150 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2151 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2152
2153 SHA256_Final(digest, &sha256_ctx);
2154
2155 save_kernel_hash_cmd(digest);
2156 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2157
2158 return 0;
2159}
2160
Brian Swetland9c4c0752009-01-25 16:23:50 -08002161APP_START(aboot)
2162 .init = aboot_init,
2163APP_END