blob: 71d6abe43b622a01b56403896efb7f71e15abc19 [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
709 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
710 {
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
Shashank Mittalcd98d472011-08-02 14:29:24 -0700722 /* Read image without signature */
723 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
724 {
725 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
726 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800727 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700728
Amol Jadib6be5c12012-11-14 13:39:51 -0800729 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700730 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800731
Shashank Mittalcd98d472011-08-02 14:29:24 -0700732 offset = imagesize_actual;
733 /* Read signature */
734 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
735 {
736 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700737 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700738 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800739
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700740 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700741
Neeti Desai465491e2012-07-31 12:53:35 -0700742 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700743 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
744 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700745
Neeti Desai465491e2012-07-31 12:53:35 -0700746 #if DEVICE_TREE
747 if(hdr->dt_size) {
748 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700749 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700750
751 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
752
Joel Kingaa335dc2013-06-03 16:11:08 -0700753 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700754 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
755 return -1;
756 }
Neeti Desai465491e2012-07-31 12:53:35 -0700757 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700758 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700759 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
760 return -1;
761 }
762
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700763 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700764 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700765 {
766 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
767 return -1;
768 }
769
Joel Kingaa335dc2013-06-03 16:11:08 -0700770 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800771 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800772 /*
773 * If appended dev tree is found, update the atags with
774 * memory address to the DTB appended location on RAM.
775 * Else update with the atags address in the kernel header
776 */
777 void *dtb;
778 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700779 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800780 if (!dtb) {
781 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
782 return -1;
783 }
Neeti Desai465491e2012-07-31 12:53:35 -0700784 }
785 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700786 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700787 else
788 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800789 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700790
Amol Jadib6be5c12012-11-14 13:39:51 -0800791 dprintf(INFO, "Loading boot image (%d): start\n",
792 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700793 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800794
795 offset = page_size;
796
797 /* Load kernel */
798 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700799 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
800 return -1;
801 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800802 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700803
Amol Jadib6be5c12012-11-14 13:39:51 -0800804 /* Load ramdisk */
805 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700806 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800807 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700808 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
809 return -1;
810 }
811 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800812 offset += ramdisk_actual;
813
814 dprintf(INFO, "Loading boot image (%d): done\n",
815 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700816 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700817
818 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800819 offset += second_actual;
820 /* Second image loading not implemented. */
821 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700822 }
823
824 #if DEVICE_TREE
825 if(hdr->dt_size != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700826 /* Read the device tree table into buffer */
827 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
828 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
829 return -1;
830 }
831 table = (struct dt_table*) dt_buf;
832
Joel Kingaa335dc2013-06-03 16:11:08 -0700833 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700834 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
835 return -1;
836 }
837
Joel Kingaa335dc2013-06-03 16:11:08 -0700838 /* Find index of device tree within device tree table */
839 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700840 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
841 return -1;
842 }
843
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700844 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700845 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700846 {
847 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
848 return -1;
849 }
850
Joel Kingaa335dc2013-06-03 16:11:08 -0700851 if(mmc_read(ptn + offset + dt_entry.offset,
852 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700853 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
854 return -1;
855 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300856 #ifdef TZ_SAVE_KERNEL_HASH
857 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
858 hdr->ramdisk_addr, ramdisk_actual,
859 ptn, offset, hdr->dt_size);
860 #endif /* TZ_SAVE_KERNEL_HASH */
861
Channagoud Kadabi35095622013-03-01 13:53:05 -0800862 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800863 /*
864 * If appended dev tree is found, update the atags with
865 * memory address to the DTB appended location on RAM.
866 * Else update with the atags address in the kernel header
867 */
868 void *dtb;
869 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700870 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800871 if (!dtb) {
872 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
873 return -1;
874 }
Neeti Desai465491e2012-07-31 12:53:35 -0700875 }
876 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700877 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700878
879unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700880
Dima Zavin77e41f32013-03-06 16:10:43 -0800881 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700882 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700883 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
884
885 return 0;
886}
887
Dima Zavin214cc642009-01-26 11:16:21 -0800888int boot_linux_from_flash(void)
889{
890 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800891 struct ptentry *ptn;
892 struct ptable *ptable;
893 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800894
Shashank Mittalcd98d472011-08-02 14:29:24 -0700895 unsigned char *image_addr = 0;
896 unsigned kernel_actual;
897 unsigned ramdisk_actual;
898 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800899 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700900
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700901#if DEVICE_TREE
902 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700903 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800904 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700905#endif
906
David Ng183a7422009-12-07 14:55:21 -0800907 if (target_is_emmc_boot()) {
908 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
909 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
910 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
911 return -1;
912 }
913 goto continue_boot;
914 }
915
Dima Zavin214cc642009-01-26 11:16:21 -0800916 ptable = flash_get_ptable();
917 if (ptable == NULL) {
918 dprintf(CRITICAL, "ERROR: Partition table not found\n");
919 return -1;
920 }
921
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800922 if(!boot_into_recovery)
923 {
924 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700925
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800926 if (ptn == NULL) {
927 dprintf(CRITICAL, "ERROR: No boot partition found\n");
928 return -1;
929 }
930 }
931 else
932 {
933 ptn = ptable_find(ptable, "recovery");
934 if (ptn == NULL) {
935 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
936 return -1;
937 }
Dima Zavin214cc642009-01-26 11:16:21 -0800938 }
939
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800940 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800941 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
942 return -1;
943 }
Dima Zavin214cc642009-01-26 11:16:21 -0800944
945 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700946 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800947 return -1;
948 }
949
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800950 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700951 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 -0800952 return -1;
953 }
954
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700955 /*
956 * Update the kernel/ramdisk/tags address if the boot image header
957 * has default values, these default values come from mkbootimg when
958 * the boot image is flashed using fastboot flash:raw
959 */
960 update_ker_tags_rdisk_addr(hdr);
961
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700962 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700963 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
964 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
965 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
966
967 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
968 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
969
970 /* Check if the addresses in the header are valid. */
971 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
972 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
973 {
974 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
975 return -1;
976 }
977
978#ifndef DEVICE_TREE
979 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
980 {
981 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
982 return -1;
983 }
984#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700985
Shashank Mittalcd98d472011-08-02 14:29:24 -0700986 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700987 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700988 {
989 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700990 offset = 0;
991
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800992#if DEVICE_TREE
993 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
994 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700995
996 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
997 {
998 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
999 return -1;
1000 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001001#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001002 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001003#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001004
Amol Jadib6be5c12012-11-14 13:39:51 -08001005 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001006 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001007
Shashank Mittalcd98d472011-08-02 14:29:24 -07001008 /* Read image without signature */
1009 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1010 {
1011 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1012 return -1;
1013 }
Dima Zavin214cc642009-01-26 11:16:21 -08001014
Amol Jadib6be5c12012-11-14 13:39:51 -08001015 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001016 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001017
Shashank Mittalcd98d472011-08-02 14:29:24 -07001018 offset = imagesize_actual;
1019 /* Read signature */
1020 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1021 {
1022 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001023 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001024 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001025
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001026 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001027
1028 /* Move kernel and ramdisk to correct address */
1029 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1030 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001031#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001032 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001033 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001034 {
1035 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1036 return -1;
1037 }
1038
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001039 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1040#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001041
1042 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001043 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001044 {
1045 write_device_info_flash(&device);
1046 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301047#if USE_PCOM_SECBOOT
1048 set_tamper_flag(device.is_tampered);
1049#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001050 }
1051 else
1052 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001053 offset = page_size;
1054
Amol Jadib6be5c12012-11-14 13:39:51 -08001055 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1056 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1057 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1058
1059 dprintf(INFO, "Loading boot image (%d): start\n",
1060 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001061 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001062
1063 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001064 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1065 return -1;
1066 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001067 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001068
Amol Jadib6be5c12012-11-14 13:39:51 -08001069 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001070 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1071 return -1;
1072 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001073 offset += ramdisk_actual;
1074
1075 dprintf(INFO, "Loading boot image (%d): done\n",
1076 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001077 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001078
1079 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001080 offset += second_actual;
1081 /* Second image loading not implemented. */
1082 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001083 }
1084
1085#if DEVICE_TREE
1086 if(hdr->dt_size != 0) {
1087
1088 /* Read the device tree table into buffer */
1089 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1090 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1091 return -1;
1092 }
1093
1094 table = (struct dt_table*) dt_buf;
1095
Joel Kingaa335dc2013-06-03 16:11:08 -07001096 if (dev_tree_validate(table, hdr->page_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001097 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1098 return -1;
1099 }
1100
Joel Kingaa335dc2013-06-03 16:11:08 -07001101 /* Find index of device tree within device tree table */
1102 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001103 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1104 return -1;
1105 }
1106
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001107 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001108 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001109 {
1110 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1111 return -1;
1112 }
1113
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001114 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001115 if(flash_read(ptn, offset + dt_entry.offset,
1116 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001117 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1118 return -1;
1119 }
1120 }
1121#endif
1122
Shashank Mittalcd98d472011-08-02 14:29:24 -07001123 }
David Ng183a7422009-12-07 14:55:21 -08001124continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001125
Dima Zavin214cc642009-01-26 11:16:21 -08001126 /* TODO: create/pass atags to kernel */
1127
Ajay Dudanie28a6072011-07-01 13:59:46 -07001128 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001129 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001130 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1131
1132 return 0;
1133}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001134
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001135BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001136void write_device_info_mmc(device_info *dev)
1137{
1138 struct device_info *info = (void*) info_buf;
1139 unsigned long long ptn = 0;
1140 unsigned long long size;
1141 int index = INVALID_PTN;
1142
1143 index = partition_get_index("aboot");
1144 ptn = partition_get_offset(index);
1145 if(ptn == 0)
1146 {
1147 return;
1148 }
1149
1150 size = partition_get_size(index);
1151
1152 memcpy(info, dev, sizeof(device_info));
1153
1154 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1155 {
1156 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1157 return;
1158 }
1159}
1160
1161void read_device_info_mmc(device_info *dev)
1162{
1163 struct device_info *info = (void*) info_buf;
1164 unsigned long long ptn = 0;
1165 unsigned long long size;
1166 int index = INVALID_PTN;
1167
1168 index = partition_get_index("aboot");
1169 ptn = partition_get_offset(index);
1170 if(ptn == 0)
1171 {
1172 return;
1173 }
1174
1175 size = partition_get_size(index);
1176
1177 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1178 {
1179 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1180 return;
1181 }
1182
1183 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1184 {
1185 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1186 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001187 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001188
1189 write_device_info_mmc(info);
1190 }
1191 memcpy(dev, info, sizeof(device_info));
1192}
1193
1194void write_device_info_flash(device_info *dev)
1195{
1196 struct device_info *info = (void *) info_buf;
1197 struct ptentry *ptn;
1198 struct ptable *ptable;
1199
1200 ptable = flash_get_ptable();
1201 if (ptable == NULL)
1202 {
1203 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1204 return;
1205 }
1206
1207 ptn = ptable_find(ptable, "devinfo");
1208 if (ptn == NULL)
1209 {
1210 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1211 return;
1212 }
1213
1214 memcpy(info, dev, sizeof(device_info));
1215
1216 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1217 {
1218 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1219 return;
1220 }
1221}
1222
1223void read_device_info_flash(device_info *dev)
1224{
1225 struct device_info *info = (void*) info_buf;
1226 struct ptentry *ptn;
1227 struct ptable *ptable;
1228
1229 ptable = flash_get_ptable();
1230 if (ptable == NULL)
1231 {
1232 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1233 return;
1234 }
1235
1236 ptn = ptable_find(ptable, "devinfo");
1237 if (ptn == NULL)
1238 {
1239 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1240 return;
1241 }
1242
1243 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1244 {
1245 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1246 return;
1247 }
1248
1249 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1250 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001251 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1252 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001253 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001254 write_device_info_flash(info);
1255 }
1256 memcpy(dev, info, sizeof(device_info));
1257}
1258
1259void write_device_info(device_info *dev)
1260{
1261 if(target_is_emmc_boot())
1262 {
1263 write_device_info_mmc(dev);
1264 }
1265 else
1266 {
1267 write_device_info_flash(dev);
1268 }
1269}
1270
1271void read_device_info(device_info *dev)
1272{
1273 if(target_is_emmc_boot())
1274 {
1275 read_device_info_mmc(dev);
1276 }
1277 else
1278 {
1279 read_device_info_flash(dev);
1280 }
1281}
1282
1283void reset_device_info()
1284{
1285 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001286 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001287 write_device_info(&device);
1288}
1289
1290void set_device_root()
1291{
1292 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001293 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001294 write_device_info(&device);
1295}
1296
Amol Jadicb524072012-08-09 16:40:18 -07001297#if DEVICE_TREE
1298int copy_dtb(uint8_t *boot_image_start)
1299{
1300 uint32 dt_image_offset = 0;
1301 uint32_t n;
1302 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001303 struct dt_entry dt_entry;
Amol Jadicb524072012-08-09 16:40:18 -07001304
1305 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1306
Amol Jadicb524072012-08-09 16:40:18 -07001307 if(hdr->dt_size != 0) {
1308
1309 /* add kernel offset */
1310 dt_image_offset += page_size;
1311 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1312 dt_image_offset += n;
1313
1314 /* add ramdisk offset */
1315 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1316 dt_image_offset += n;
1317
1318 /* add second offset */
1319 if(hdr->second_size != 0) {
1320 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1321 dt_image_offset += n;
1322 }
1323
1324 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001325 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001326
Joel Kingaa335dc2013-06-03 16:11:08 -07001327 if (dev_tree_validate(table, hdr->page_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001328 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1329 return -1;
1330 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001331 /* Find index of device tree within device tree table */
1332 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001333 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1334 return -1;
1335 }
1336
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001337 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001338 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001339 {
1340 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1341 return -1;
1342 }
1343
Amol Jadicb524072012-08-09 16:40:18 -07001344 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001345 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001346 boot_image_start + dt_image_offset + dt_entry.offset,
1347 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001348 } else
1349 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001350
1351 /* Everything looks fine. Return success. */
1352 return 0;
1353}
1354#endif
1355
Brian Swetland9c4c0752009-01-25 16:23:50 -08001356void cmd_boot(const char *arg, void *data, unsigned sz)
1357{
1358 unsigned kernel_actual;
1359 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001360 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001361 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001362 int ret = 0;
1363 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001364
1365 if (sz < sizeof(hdr)) {
1366 fastboot_fail("invalid bootimage header");
1367 return;
1368 }
1369
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001370 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001371
1372 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001373 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001374
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001375 if(target_is_emmc_boot() && hdr->page_size) {
1376 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001377 page_mask = page_size - 1;
1378 }
1379
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001380 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1381 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1382
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001383 /*
1384 * Update the kernel/ramdisk/tags address if the boot image header
1385 * has default values, these default values come from mkbootimg when
1386 * the boot image is flashed using fastboot flash:raw
1387 */
1388 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001389
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001390 /* Get virtual addresses since the hdr saves physical addresses. */
1391 hdr->kernel_addr = VA(hdr->kernel_addr);
1392 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1393 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001394
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001395 /* Check if the addresses in the header are valid. */
1396 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1397 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1398 {
1399 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1400 return -1;
1401 }
1402
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001403 /* sz should have atleast raw boot image */
1404 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001405 fastboot_fail("incomplete bootimage");
1406 return;
1407 }
1408
Amol Jadicb524072012-08-09 16:40:18 -07001409#if DEVICE_TREE
1410 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001411 ret = copy_dtb(data);
1412
1413 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001414#else
1415 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1416 {
1417 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1418 return -1;
1419 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001420#endif
1421
1422 /* Load ramdisk & kernel */
1423 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1424 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1425
1426#if DEVICE_TREE
1427 /*
1428 * If dtb is not found look for appended DTB in the kernel.
1429 * If appended dev tree is found, update the atags with
1430 * memory address to the DTB appended location on RAM.
1431 * Else update with the atags address in the kernel header
1432 */
1433 if (!dtb_copied) {
1434 void *dtb;
Channagoud Kadabia83a6972013-04-25 15:26:04 -07001435 dtb = dev_tree_appended((void *)hdr->kernel_addr, (void *)hdr->tags_addr, hdr->kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001436 if (!dtb) {
1437 fastboot_fail("dtb not found");
1438 return;
1439 }
Amol Jadicb524072012-08-09 16:40:18 -07001440 }
1441#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001442
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001443#ifndef DEVICE_TREE
1444 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1445 {
1446 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1447 return -1;
1448 }
1449#endif
1450
Brian Swetland9c4c0752009-01-25 16:23:50 -08001451 fastboot_okay("");
1452 udc_stop();
1453
Dima Zavin77e41f32013-03-06 16:10:43 -08001454 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001455 (const char*) hdr->cmdline, board_machtype(),
1456 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001457}
1458
Dima Zavin214cc642009-01-26 11:16:21 -08001459void cmd_erase(const char *arg, void *data, unsigned sz)
1460{
1461 struct ptentry *ptn;
1462 struct ptable *ptable;
1463
1464 ptable = flash_get_ptable();
1465 if (ptable == NULL) {
1466 fastboot_fail("partition table doesn't exist");
1467 return;
1468 }
1469
1470 ptn = ptable_find(ptable, arg);
1471 if (ptn == NULL) {
1472 fastboot_fail("unknown partition name");
1473 return;
1474 }
1475
1476 if (flash_erase(ptn)) {
1477 fastboot_fail("failed to erase partition");
1478 return;
1479 }
1480 fastboot_okay("");
1481}
1482
Bikas Gurungd48bd242010-09-04 19:54:32 -07001483
1484void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1485{
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001486 BUF_DMA_ALIGN(out, 512);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001487 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001488 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001489
Kinson Chikf1a43512011-07-14 11:28:39 -07001490 index = partition_get_index(arg);
1491 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001492
Kinson Chikf1a43512011-07-14 11:28:39 -07001493 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001494 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001495 return;
1496 }
neetidb4b24d62012-01-20 12:13:09 -08001497 /* Simple inefficient version of erase. Just writing
1498 0 in first block */
1499 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1500 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001501 return;
1502 }
1503 fastboot_okay("");
1504}
1505
1506
Ajay Dudani5c761132011-04-07 20:19:04 -07001507void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001508{
1509 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001510 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001511 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001512
Greg Grisco6e754772011-06-23 12:19:39 -07001513 if (!strcmp(arg, "partition"))
1514 {
1515 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001516 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001517 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001518 return;
1519 }
1520 }
Greg Grisco6e754772011-06-23 12:19:39 -07001521 else
1522 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001523 index = partition_get_index(arg);
1524 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001525 if(ptn == 0) {
1526 fastboot_fail("partition table doesn't exist");
1527 return;
1528 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001529
Greg Grisco6e754772011-06-23 12:19:39 -07001530 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1531 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1532 fastboot_fail("image is not a boot image");
1533 return;
1534 }
1535 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001536
Kinson Chikf1a43512011-07-14 11:28:39 -07001537 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001538 if (ROUND_TO_PAGE(sz,511) > size) {
1539 fastboot_fail("size too large");
1540 return;
1541 }
1542 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1543 fastboot_fail("flash write failure");
1544 return;
1545 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001546 }
1547 fastboot_okay("");
1548 return;
1549}
1550
Ajay Dudani5c761132011-04-07 20:19:04 -07001551void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1552{
1553 unsigned int chunk;
1554 unsigned int chunk_data_sz;
1555 sparse_header_t *sparse_header;
1556 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001557 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001558 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301559 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001560 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001561
Kinson Chikf1a43512011-07-14 11:28:39 -07001562 index = partition_get_index(arg);
1563 ptn = partition_get_offset(index);
1564 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001565 fastboot_fail("partition table doesn't exist");
1566 return;
1567 }
1568
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301569 size = partition_get_size(index);
1570 if (ROUND_TO_PAGE(sz,511) > size) {
1571 fastboot_fail("size too large");
1572 return;
1573 }
1574
Ajay Dudani5c761132011-04-07 20:19:04 -07001575 /* Read and skip over sparse image header */
1576 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001577 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1578 fastboot_fail("size too large");
1579 return;
1580 }
1581
Ajay Dudani5c761132011-04-07 20:19:04 -07001582 data += sparse_header->file_hdr_sz;
1583 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1584 {
1585 /* Skip the remaining bytes in a header that is longer than
1586 * we expected.
1587 */
1588 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1589 }
1590
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001591 dprintf (SPEW, "=== Sparse Image Header ===\n");
1592 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1593 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1594 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1595 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1596 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1597 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1598 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1599 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001600
1601 /* Start processing chunks */
1602 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1603 {
1604 /* Read and skip over chunk header */
1605 chunk_header = (chunk_header_t *) data;
1606 data += sizeof(chunk_header_t);
1607
1608 dprintf (SPEW, "=== Chunk Header ===\n");
1609 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1610 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1611 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1612
1613 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1614 {
1615 /* Skip the remaining bytes in a header that is longer than
1616 * we expected.
1617 */
1618 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1619 }
1620
1621 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1622 switch (chunk_header->chunk_type)
1623 {
1624 case CHUNK_TYPE_RAW:
1625 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1626 chunk_data_sz))
1627 {
1628 fastboot_fail("Bogus chunk size for chunk type Raw");
1629 return;
1630 }
1631
Ajay Dudaniab18f022011-05-12 14:39:22 -07001632 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1633 chunk_data_sz,
1634 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001635 {
1636 fastboot_fail("flash write failure");
1637 return;
1638 }
1639 total_blocks += chunk_header->chunk_sz;
1640 data += chunk_data_sz;
1641 break;
1642
1643 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001644 total_blocks += chunk_header->chunk_sz;
1645 break;
1646
Ajay Dudani5c761132011-04-07 20:19:04 -07001647 case CHUNK_TYPE_CRC:
1648 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1649 {
1650 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1651 return;
1652 }
1653 total_blocks += chunk_header->chunk_sz;
1654 data += chunk_data_sz;
1655 break;
1656
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001657 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001658 fastboot_fail("Unknown chunk type");
1659 return;
1660 }
1661 }
1662
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001663 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1664 total_blocks, sparse_header->total_blks);
1665
1666 if(total_blocks != sparse_header->total_blks)
1667 {
1668 fastboot_fail("sparse image write failure");
1669 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001670
1671 fastboot_okay("");
1672 return;
1673}
1674
1675void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1676{
1677 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001678 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1679 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001680
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001681#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001682 int ret=0;
1683 uint32 major_version=0;
1684 uint32 minor_version=0;
1685
1686 ret = scm_svc_version(&major_version,&minor_version);
1687 if(!ret)
1688 {
1689 if(major_version >= 2)
1690 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001691 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001692 {
1693 ret = encrypt_scm((uint32 **) &data, &sz);
1694 if (ret != 0) {
1695 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1696 return;
1697 }
1698
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001699 /* Protect only for SSD */
1700 if (!strcmp(arg, "ssd")) {
1701 ret = scm_protect_keystore((uint32 *) data, sz);
1702 if (ret != 0) {
1703 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1704 return;
1705 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001706 }
1707 }
1708 else
1709 {
1710 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1711 if(ret != 0)
1712 {
1713 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1714 return;
1715 }
1716 }
1717 }
1718 else
1719 {
1720 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1721 magic_number[1] == DECRYPT_MAGIC_1)
1722 {
1723 ret = decrypt_scm((uint32 **) &data, &sz);
1724 if (ret != 0) {
1725 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1726 return;
1727 }
1728 }
1729 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1730 magic_number[1] == ENCRYPT_MAGIC_1)
1731 {
1732 ret = encrypt_scm((uint32 **) &data, &sz);
1733 if (ret != 0) {
1734 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1735 return;
1736 }
1737 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001738 }
1739 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001740 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001741 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001742 dprintf(CRITICAL,"INVALID SVC Version\n");
1743 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001744 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001745#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001746
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001747 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001748 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1749 cmd_flash_mmc_img(arg, data, sz);
1750 else
1751 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001752 return;
1753}
1754
Dima Zavin214cc642009-01-26 11:16:21 -08001755void cmd_flash(const char *arg, void *data, unsigned sz)
1756{
1757 struct ptentry *ptn;
1758 struct ptable *ptable;
1759 unsigned extra = 0;
1760
1761 ptable = flash_get_ptable();
1762 if (ptable == NULL) {
1763 fastboot_fail("partition table doesn't exist");
1764 return;
1765 }
1766
1767 ptn = ptable_find(ptable, arg);
1768 if (ptn == NULL) {
1769 fastboot_fail("unknown partition name");
1770 return;
1771 }
1772
1773 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1774 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1775 fastboot_fail("image is not a boot image");
1776 return;
1777 }
1778 }
1779
Amol Jadi5c61a952012-05-04 17:05:35 -07001780 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001781 || !strcmp(ptn->name, "userdata")
1782 || !strcmp(ptn->name, "persist")
1783 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301784 if (flash_ecc_bch_enabled())
1785 /* Spare data bytes for 8 bit ECC increased by 4 */
1786 extra = ((page_size >> 9) * 20);
1787 else
1788 extra = ((page_size >> 9) * 16);
1789 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001790 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001791
1792 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1793 if (flash_write(ptn, extra, data, sz)) {
1794 fastboot_fail("flash write failure");
1795 return;
1796 }
1797 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1798 fastboot_okay("");
1799}
1800
1801void cmd_continue(const char *arg, void *data, unsigned sz)
1802{
1803 fastboot_okay("");
1804 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001805 if (target_is_emmc_boot())
1806 {
1807 boot_linux_from_mmc();
1808 }
1809 else
1810 {
1811 boot_linux_from_flash();
1812 }
Dima Zavin214cc642009-01-26 11:16:21 -08001813}
1814
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001815void cmd_reboot(const char *arg, void *data, unsigned sz)
1816{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001817 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001818 fastboot_okay("");
1819 reboot_device(0);
1820}
1821
1822void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1823{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001824 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001825 fastboot_okay("");
1826 reboot_device(FASTBOOT_MODE);
1827}
1828
Shashank Mittal162244e2011-08-08 19:01:25 -07001829void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1830{
1831 if(!device.is_unlocked)
1832 {
1833 device.is_unlocked = 1;
1834 write_device_info(&device);
1835 }
1836 fastboot_okay("");
1837}
1838
Shashank Mittala0032282011-08-26 14:50:11 -07001839void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1840{
1841 char response[64];
1842 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1843 fastboot_info(response);
1844 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1845 fastboot_info(response);
1846 fastboot_okay("");
1847}
1848
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001849void cmd_preflash(const char *arg, void *data, unsigned sz)
1850{
1851 fastboot_okay("");
1852}
1853
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001854void splash_screen ()
1855{
1856 struct ptentry *ptn;
1857 struct ptable *ptable;
1858 struct fbcon_config *fb_display = NULL;
1859
1860 if (!target_is_emmc_boot())
1861 {
1862 ptable = flash_get_ptable();
1863 if (ptable == NULL) {
1864 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001865 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001866 }
1867
1868 ptn = ptable_find(ptable, "splash");
1869 if (ptn == NULL) {
1870 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1871 } else {
1872 fb_display = fbcon_display();
1873 if (fb_display) {
1874 if (flash_read(ptn, 0, fb_display->base,
1875 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1876 fbcon_clear();
1877 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1878 }
1879 }
1880 }
1881 }
1882}
1883
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001884/* Get the size from partiton name */
1885static void get_partition_size(const char *arg, char *response)
1886{
1887 uint64_t ptn = 0;
1888 uint64_t size;
1889 int index = INVALID_PTN;
1890
1891 index = partition_get_index(arg);
1892
1893 if (index == INVALID_PTN)
1894 {
1895 dprintf(CRITICAL, "Invalid partition index\n");
1896 return;
1897 }
1898
1899 ptn = partition_get_offset(index);
1900
1901 if(!ptn)
1902 {
1903 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1904 return;
1905 }
1906
1907 size = partition_get_size(index);
1908
1909 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1910 return;
1911}
1912
1913/*
1914 * Publish the partition type & size info
1915 * fastboot getvar will publish the required information.
1916 * fastboot getvar partition_size:<partition_name>: partition size in hex
1917 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1918 */
1919static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1920{
1921 uint8_t i;
1922
1923 for (i = 0; i < num_parts; i++) {
1924 get_partition_size(info[i].part_name, info[i].size_response);
1925
1926 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1927 {
1928 dprintf(CRITICAL, "partition size name truncated\n");
1929 return;
1930 }
1931 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1932 {
1933 dprintf(CRITICAL, "partition type name truncated\n");
1934 return;
1935 }
1936
1937 /* publish partition size & type info */
1938 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1939 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1940 }
1941}
1942
Brian Swetland9c4c0752009-01-25 16:23:50 -08001943void aboot_init(const struct app_descriptor *app)
1944{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001945 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001946 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001947 unsigned sz = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03001948 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001949
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001950 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001951 if (target_is_emmc_boot())
1952 {
1953 page_size = 2048;
1954 page_mask = page_size - 1;
1955 }
1956 else
1957 {
1958 page_size = flash_page_size();
1959 page_mask = page_size - 1;
1960 }
1961
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001962 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
1963
Shashank Mittal162244e2011-08-08 19:01:25 -07001964 if(target_use_signed_kernel())
1965 {
1966 read_device_info(&device);
1967
Shashank Mittal162244e2011-08-08 19:01:25 -07001968 }
1969
Greg Griscod6250552011-06-29 14:40:23 -07001970 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001971 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001972 surf_udc_device.serialno = sn_buf;
1973
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001974 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001975 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001976 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001977 dprintf(ALWAYS,"dload mode key sequence detected\n");
1978 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001979 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001980 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001981 }
1982 else
1983 {
1984 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001985 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001986 }
1987 boot_into_fastboot = true;
1988 }
1989 if (!boot_into_fastboot)
1990 {
1991 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
1992 boot_into_recovery = 1;
1993 if (!boot_into_recovery &&
1994 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03001995 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001996 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001997 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001998 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03001999 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002000 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002001
Ajay Dudani77421292010-10-27 19:34:06 -07002002 reboot_mode = check_reboot_mode();
2003 if (reboot_mode == RECOVERY_MODE) {
2004 boot_into_recovery = 1;
2005 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002006 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002007 }
2008
Pavel Nedev5d91d412013-04-29 11:34:24 +03002009 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002010 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002011 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002012 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002013 if(emmc_recovery_init())
2014 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2015 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002016 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002017 if((device.is_unlocked) || (device.is_tampered))
2018 {
2019 #ifdef TZ_TAMPER_FUSE
2020 set_tamper_fuse_cmd();
2021 #endif
2022 #if USE_PCOM_SECBOOT
2023 set_tamper_flag(device.is_tampered);
2024 #endif
2025 }
Shashank Mittala0032282011-08-26 14:50:11 -07002026 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002027 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002028 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002029 else
2030 {
2031 recovery_init();
2032 #if USE_PCOM_SECBOOT
2033 if((device.is_unlocked) || (device.is_tampered))
2034 set_tamper_flag(device.is_tampered);
2035 #endif
2036 boot_linux_from_flash();
2037 }
2038 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2039 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002040 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002041
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002042 sz = target_get_max_flash_size();
2043
Shashank Mittal162244e2011-08-08 19:01:25 -07002044 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002045
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002046 if(!usb_init)
2047 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002048
2049 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002050
Shashank Mittal23b8f422010-04-16 19:27:21 -07002051 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002052 {
2053 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002054 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002055 }
2056 else
2057 {
2058 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002059 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002060 }
2061
2062 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002063 fastboot_register("reboot", cmd_reboot);
2064 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07002065 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07002066 fastboot_register("oem device-info", cmd_oem_devinfo);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002067 fastboot_register("preflash", cmd_preflash);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07002068 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08002069 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08002070 fastboot_publish("serialno", sn_buf);
Channagoud Kadabi873d8ff2013-07-01 17:31:40 -07002071 /*
2072 * fastboot publish is supported only for emmc partitions
2073 * Calling this for NAND prints some error messages which
2074 * is harmless but misleading. Avoid calling this for NAND
2075 * devices.
2076 */
2077 if (target_is_emmc_boot())
2078 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002079 /* Max download size supported */
2080 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", sz);
2081 fastboot_publish("max-download-size", (const char *) max_download_size);
Kinson Chikf1a43512011-07-14 11:28:39 -07002082 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07002083 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002084 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002085}
2086
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002087uint32_t get_page_size()
2088{
2089 return page_size;
2090}
2091
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002092/*
2093 * Calculated and save hash (SHA256) for non-signed boot image.
2094 *
2095 * Hash the same data that is checked on the signed boot image.
2096 * Kernel and Ramdisk are already read to memory buffers.
2097 * Need to read the entire device-tree from mmc
2098 * since non-signed image only read the DT tags of the relevant platform.
2099 *
2100 * @param kernel_addr - kernel bufer
2101 * @param kernel_actual - kernel size in bytes
2102 * @param ramdisk_addr - ramdisk buffer
2103 * @param ramdisk_actual - ramdisk size
2104 * @param ptn - partition
2105 * @param dt_offset - device tree offset on mmc partition
2106 * @param dt_size
2107 *
2108 * @return int - 0 on success, negative value on failure.
2109 */
2110int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2111 void *ramdisk_addr, unsigned ramdisk_actual,
2112 unsigned long long ptn,
2113 unsigned dt_offset, unsigned dt_size)
2114{
2115 SHA256_CTX sha256_ctx;
2116 char digest[32]={0};
2117 char *buf = (char *)target_get_scratch_address();
2118 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2119 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2120
2121 SHA256_Init(&sha256_ctx);
2122
2123 /* Read Boot Header */
2124 if (mmc_read(ptn, buf, page_size))
2125 {
2126 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2127 return -1;
2128 }
2129 /* Read entire Device Tree */
2130 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2131 {
2132 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2133 return -1;
2134 }
2135 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2136 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2137 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2138 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2139
2140 SHA256_Final(digest, &sha256_ctx);
2141
2142 save_kernel_hash_cmd(digest);
2143 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2144
2145 return 0;
2146}
2147
Brian Swetland9c4c0752009-01-25 16:23:50 -08002148APP_START(aboot)
2149 .init = aboot_init,
2150APP_END