blob: af03c6c2cc517aff67923d99b84936e24a0ad168 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Amol Jadi2a15a272013-01-22 12:03:36 -08005 * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080038#include <kernel/thread.h>
39#include <arch/ops.h>
40
Dima Zavin214cc642009-01-26 11:16:21 -080041#include <dev/flash.h>
42#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080043#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080044#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080045#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070046#include <target.h>
47#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070048#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070049#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070050#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070051#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070052#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030053#include <sha.h>
Dima Zavin214cc642009-01-26 11:16:21 -080054
Neeti Desai17379b82012-06-04 18:42:53 -070055#if DEVICE_TREE
56#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070057#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070058#endif
59
Shashank Mittalcd98d472011-08-02 14:29:24 -070060#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080061#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080062#include "bootimg.h"
63#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070064#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070065#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070066#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070067#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080068
Shashank Mittal162244e2011-08-08 19:01:25 -070069#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070070
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070071extern bool target_use_signed_kernel(void);
72extern void dsb();
73extern void isb();
74extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070075extern void target_uninit(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070076
77void write_device_info_mmc(device_info *dev);
78void write_device_info_flash(device_info *dev);
79
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070080#define EXPAND(NAME) #NAME
81#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070082
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080083#ifdef MEMBASE
84#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
85#else
David Ng183a7422009-12-07 14:55:21 -080086#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080087#endif
88
Deepa Dinamani0e163a42013-05-24 17:08:15 -070089#ifndef MEMSIZE
90#define MEMSIZE 1024*1024
91#endif
92
93#define MAX_TAGS_SIZE 1024
94
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080095#define RECOVERY_MODE 0x77665502
96#define FASTBOOT_MODE 0x77665500
97
David Ng183a7422009-12-07 14:55:21 -080098static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080099static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300100static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800101static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800102static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700103static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300104static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800105
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800106static const char *baseband_apq = " androidboot.baseband=apq";
107static const char *baseband_msm = " androidboot.baseband=msm";
108static const char *baseband_csfb = " androidboot.baseband=csfb";
109static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700110static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700111static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800112static const char *baseband_dsda = " androidboot.baseband=dsda";
113static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800114static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800115
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700116static unsigned page_size = 0;
117static unsigned page_mask = 0;
118static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
119static bool boot_into_ffbm;
120
Shashank Mittalcd98d472011-08-02 14:29:24 -0700121/* Assuming unauthorized kernel image by default */
122static int auth_kernel_img = 0;
123
Shashank Mittal162244e2011-08-08 19:01:25 -0700124static device_info device = {DEVICE_MAGIC, 0, 0};
125
Dima Zavin42168f22009-01-30 11:52:22 -0800126struct atag_ptbl_entry
127{
128 char name[16];
129 unsigned offset;
130 unsigned size;
131 unsigned flags;
132};
133
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700134/*
135 * Partition info, required to be published
136 * for fastboot
137 */
138struct getvar_partition_info {
139 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
140 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
141 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
142 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
143 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
144};
145
146/*
147 * Right now, we are publishing the info for only
148 * three partitions
149 */
150struct getvar_partition_info part_info[] =
151{
152 { "system" , "partition-size:", "partition-type:", "", "ext4" },
153 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
154 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
155};
156
157char max_download_size[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800158char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700159
Greg Griscod2471ef2011-07-14 13:00:42 -0700160extern int emmc_recovery_init(void);
161
Kinson Chik0b1c8162011-08-31 16:31:57 -0700162#if NO_KEYPAD_DRIVER
163extern int fastboot_trigger(void);
164#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700165
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700166static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
167{
168 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700169#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
170 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
171 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
172 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700173#endif
174}
175
Dima Zavin42168f22009-01-30 11:52:22 -0800176static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
177{
178 struct atag_ptbl_entry atag_ptn;
179
180 memcpy(atag_ptn.name, ptn->name, 16);
181 atag_ptn.name[15] = '\0';
182 atag_ptn.offset = ptn->start;
183 atag_ptn.size = ptn->length;
184 atag_ptn.flags = ptn->flags;
185 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
186 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
187}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800188
Neeti Desaie245d492012-06-01 12:52:13 -0700189unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800190{
David Ng183a7422009-12-07 14:55:21 -0800191 int cmdline_len = 0;
192 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800193 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700194 int pause_at_bootup = 0;
Pavel Nedev5614d222013-06-17 18:01:02 +0300195 bool gpt_exists = partition_gpt_exists();
Dima Zavin42168f22009-01-30 11:52:22 -0800196
Brian Swetland9c4c0752009-01-25 16:23:50 -0800197 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800198 cmdline_len = strlen(cmdline);
199 have_cmdline = 1;
200 }
201 if (target_is_emmc_boot()) {
202 cmdline_len += strlen(emmc_cmdline);
203 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800204
205 cmdline_len += strlen(usb_sn_cmdline);
206 cmdline_len += strlen(sn_buf);
207
Pavel Nedev5614d222013-06-17 18:01:02 +0300208 if (boot_into_recovery && gpt_exists)
209 cmdline_len += strlen(secondary_gpt_enable);
210
Pavel Nedev328ac822013-04-05 15:25:11 +0300211 if (boot_into_ffbm) {
212 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700213 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800214 /* reduce kernel console messages to speed-up boot */
215 cmdline_len += strlen(loglevel);
Pavel Nedev328ac822013-04-05 15:25:11 +0300216 } else if (target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700217 pause_at_bootup = 1;
218 cmdline_len += strlen(battchg_pause);
219 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800220
Shashank Mittalcd98d472011-08-02 14:29:24 -0700221 if(target_use_signed_kernel() && auth_kernel_img) {
222 cmdline_len += strlen(auth_kernel);
223 }
224
Ajay Dudanid04110c2011-01-17 23:55:07 -0800225 /* Determine correct androidboot.baseband to use */
226 switch(target_baseband())
227 {
228 case BASEBAND_APQ:
229 cmdline_len += strlen(baseband_apq);
230 break;
231
232 case BASEBAND_MSM:
233 cmdline_len += strlen(baseband_msm);
234 break;
235
236 case BASEBAND_CSFB:
237 cmdline_len += strlen(baseband_csfb);
238 break;
239
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800240 case BASEBAND_SVLTE2A:
241 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800242 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700243
244 case BASEBAND_MDM:
245 cmdline_len += strlen(baseband_mdm);
246 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700247
248 case BASEBAND_SGLTE:
249 cmdline_len += strlen(baseband_sglte);
250 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530251
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800252 case BASEBAND_SGLTE2:
253 cmdline_len += strlen(baseband_sglte2);
254 break;
255
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530256 case BASEBAND_DSDA:
257 cmdline_len += strlen(baseband_dsda);
258 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800259
260 case BASEBAND_DSDA2:
261 cmdline_len += strlen(baseband_dsda2);
262 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800263 }
264
David Ng183a7422009-12-07 14:55:21 -0800265 if (cmdline_len > 0) {
266 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700267 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
268 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700269
Amol Jadi168b7712012-03-06 16:15:00 -0800270 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700271 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800272 if (have_cmdline) {
273 src = cmdline;
274 while ((*dst++ = *src++));
275 }
276 if (target_is_emmc_boot()) {
277 src = emmc_cmdline;
278 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700279 have_cmdline = 1;
280 while ((*dst++ = *src++));
281 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800282
283 src = usb_sn_cmdline;
284 if (have_cmdline) --dst;
285 have_cmdline = 1;
286 while ((*dst++ = *src++));
287 src = sn_buf;
288 if (have_cmdline) --dst;
289 have_cmdline = 1;
290 while ((*dst++ = *src++));
291
Pavel Nedev5614d222013-06-17 18:01:02 +0300292 if (boot_into_recovery && gpt_exists) {
293 src = secondary_gpt_enable;
294 if (have_cmdline) --dst;
295 while ((*dst++ = *src++));
296 }
297
Pavel Nedev328ac822013-04-05 15:25:11 +0300298 if (boot_into_ffbm) {
299 src = androidboot_mode;
300 if (have_cmdline) --dst;
301 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700302 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300303 if (have_cmdline) --dst;
304 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800305 src = loglevel;
306 if (have_cmdline) --dst;
307 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300308 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700309 src = battchg_pause;
310 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800311 while ((*dst++ = *src++));
312 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800313
Shashank Mittalcd98d472011-08-02 14:29:24 -0700314 if(target_use_signed_kernel() && auth_kernel_img) {
315 src = auth_kernel;
316 if (have_cmdline) --dst;
317 while ((*dst++ = *src++));
318 }
319
Ajay Dudanid04110c2011-01-17 23:55:07 -0800320 switch(target_baseband())
321 {
322 case BASEBAND_APQ:
323 src = baseband_apq;
324 if (have_cmdline) --dst;
325 while ((*dst++ = *src++));
326 break;
327
328 case BASEBAND_MSM:
329 src = baseband_msm;
330 if (have_cmdline) --dst;
331 while ((*dst++ = *src++));
332 break;
333
334 case BASEBAND_CSFB:
335 src = baseband_csfb;
336 if (have_cmdline) --dst;
337 while ((*dst++ = *src++));
338 break;
339
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800340 case BASEBAND_SVLTE2A:
341 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800342 if (have_cmdline) --dst;
343 while ((*dst++ = *src++));
344 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700345
346 case BASEBAND_MDM:
347 src = baseband_mdm;
348 if (have_cmdline) --dst;
349 while ((*dst++ = *src++));
350 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700351
352 case BASEBAND_SGLTE:
353 src = baseband_sglte;
354 if (have_cmdline) --dst;
355 while ((*dst++ = *src++));
356 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530357
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800358 case BASEBAND_SGLTE2:
359 src = baseband_sglte2;
360 if (have_cmdline) --dst;
361 while ((*dst++ = *src++));
362 break;
363
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530364 case BASEBAND_DSDA:
365 src = baseband_dsda;
366 if (have_cmdline) --dst;
367 while ((*dst++ = *src++));
368 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800369
370 case BASEBAND_DSDA2:
371 src = baseband_dsda2;
372 if (have_cmdline) --dst;
373 while ((*dst++ = *src++));
374 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800375 }
Neeti Desaie245d492012-06-01 12:52:13 -0700376 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800377 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700378 return cmdline_final;
379}
380
381unsigned *atag_core(unsigned *ptr)
382{
383 /* CORE */
384 *ptr++ = 2;
385 *ptr++ = 0x54410001;
386
387 return ptr;
388
389}
390
391unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
392 unsigned ramdisk_size)
393{
394 if (ramdisk_size) {
395 *ptr++ = 4;
396 *ptr++ = 0x54420005;
397 *ptr++ = (unsigned)ramdisk;
398 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800399 }
400
Neeti Desaie245d492012-06-01 12:52:13 -0700401 return ptr;
402}
403
404unsigned *atag_ptable(unsigned **ptr_addr)
405{
406 int i;
407 struct ptable *ptable;
408
409 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700410 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
411 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700412 *(*ptr_addr)++ = 0x4d534d70;
413 for (i = 0; i < ptable->count; ++i)
414 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
415 }
416
417 return (*ptr_addr);
418}
419
420unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
421{
422 int cmdline_length = 0;
423 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700424 char *dest;
425
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800426 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700427 n = (cmdline_length + 4) & (~3);
428
429 *ptr++ = (n / 4) + 2;
430 *ptr++ = 0x54410009;
431 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800432 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700433 ptr += (n / 4);
434
435 return ptr;
436}
437
438unsigned *atag_end(unsigned *ptr)
439{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800440 /* END */
441 *ptr++ = 0;
442 *ptr++ = 0;
443
Neeti Desaie245d492012-06-01 12:52:13 -0700444 return ptr;
445}
446
447void generate_atags(unsigned *ptr, const char *cmdline,
448 void *ramdisk, unsigned ramdisk_size)
449{
450
451 ptr = atag_core(ptr);
452 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
453 ptr = target_atag_mem(ptr);
454
455 /* Skip NAND partition ATAGS for eMMC boot */
456 if (!target_is_emmc_boot()){
457 ptr = atag_ptable(&ptr);
458 }
459
460 ptr = atag_cmdline(ptr, cmdline);
461 ptr = atag_end(ptr);
462}
463
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700464typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700465void boot_linux(void *kernel, unsigned *tags,
466 const char *cmdline, unsigned machtype,
467 void *ramdisk, unsigned ramdisk_size)
468{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800469 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800470#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700471 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800472#endif
473
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700474 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800475 uint32_t tags_phys = PA((addr_t)tags);
476
477 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700478
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800479 final_cmdline = update_cmdline((const char*)cmdline);
480
Neeti Desai17379b82012-06-04 18:42:53 -0700481#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800482 dprintf(INFO, "Updating device tree: start\n");
483
Neeti Desai17379b82012-06-04 18:42:53 -0700484 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800485 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700486 if(ret)
487 {
488 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
489 ASSERT(0);
490 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800491 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700492#else
Neeti Desaie245d492012-06-01 12:52:13 -0700493 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800494 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700495#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700496
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700497 /* Perform target specific cleanup */
498 target_uninit();
499
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800500 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
501 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800502
503 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700504
Amol Jadi4421e652011-06-16 15:00:48 -0700505 /* do any platform specific cleanup before kernel entry */
506 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700507
Brian Swetland9c4c0752009-01-25 16:23:50 -0800508 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700509
Amol Jadi504f9fe2012-08-16 13:56:48 -0700510#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800511 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700512#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700513 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700514 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800515}
516
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700517/* Function to check if the memory address range falls within the aboot
518 * boundaries.
519 * start: Start of the memory region
520 * size: Size of the memory region
521 */
522int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
523{
524 /* Check for boundary conditions. */
525 if ((start + size) < start)
526 return -1;
527
528 /* Check for memory overlap. */
529 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
530 return 0;
531 else if (start > (MEMBASE + MEMSIZE))
532 return 0;
533 else
534 return -1;
535}
536
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800537#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800538
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700539BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800540#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700541BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800542#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800543
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700544static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
545{
546 int ret;
547
548 /* Assume device is rooted at this time. */
549 device.is_tampered = 1;
550
551 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
552
553 ret = image_verify((unsigned char *)bootimg_addr,
554 (unsigned char *)(bootimg_addr + bootimg_size),
555 bootimg_size,
556 CRYPTO_AUTH_ALG_SHA256);
557
558 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
559
560 if (ret)
561 {
562 /* Authorized kernel */
563 device.is_tampered = 0;
564 }
565
566#if USE_PCOM_SECBOOT
567 set_tamper_flag(device.is_tampered);
568#endif
569
570 if(device.is_tampered)
571 {
572 write_device_info_mmc(&device);
573 #ifdef TZ_TAMPER_FUSE
574 set_tamper_fuse_cmd();
575 #endif
576 #ifdef ASSERT_ON_TAMPER
577 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
578 ASSERT(0);
579 #endif
580 }
581}
582
Shashank Mittal23b8f422010-04-16 19:27:21 -0700583int boot_linux_from_mmc(void)
584{
585 struct boot_img_hdr *hdr = (void*) buf;
586 struct boot_img_hdr *uhdr;
587 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700588 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700589 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700590 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700591
Shashank Mittalcd98d472011-08-02 14:29:24 -0700592 unsigned char *image_addr = 0;
593 unsigned kernel_actual;
594 unsigned ramdisk_actual;
595 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700596 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700597
598#if DEVICE_TREE
599 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700600 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700601 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800602 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700603#endif
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700604 if (!boot_into_recovery) {
605 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
606 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
607 if (rcode <= 0) {
608 boot_into_ffbm = false;
609 if (rcode < 0)
610 dprintf(CRITICAL,"failed to get ffbm cookie");
611 } else
612 boot_into_ffbm = true;
613 } else
614 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700615 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
616 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
617 dprintf(INFO, "Unified boot method!\n");
618 hdr = uhdr;
619 goto unified_boot;
620 }
Greg Griscod6250552011-06-29 14:40:23 -0700621 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700622 index = partition_get_index("boot");
623 ptn = partition_get_offset(index);
624 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700625 dprintf(CRITICAL, "ERROR: No boot partition found\n");
626 return -1;
627 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700628 }
629 else {
630 index = partition_get_index("recovery");
631 ptn = partition_get_offset(index);
632 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700633 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
634 return -1;
635 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700636 }
637
Greg Griscod6250552011-06-29 14:40:23 -0700638 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700639 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
640 return -1;
641 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700642
643 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700644 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700645 return -1;
646 }
647
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700648 if (hdr->page_size && (hdr->page_size != page_size)) {
649 page_size = hdr->page_size;
650 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700651 }
652
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700653 /*
654 * Update the kernel/ramdisk/tags address if the boot image header
655 * has default values, these default values come from mkbootimg when
656 * the boot image is flashed using fastboot flash:raw
657 */
658 update_ker_tags_rdisk_addr(hdr);
659
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700660 /* Get virtual addresses since the hdr saves physical addresses. */
661 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
662 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
663 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700664
665 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
666 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
667
668 /* Check if the addresses in the header are valid. */
669 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
670 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
671 {
672 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
673 return -1;
674 }
675
676#ifndef DEVICE_TREE
677 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
678 {
679 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
680 return -1;
681 }
682#endif
683
Shashank Mittalcd98d472011-08-02 14:29:24 -0700684 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300685 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
686 (int) target_use_signed_kernel(),
687 device.is_unlocked,
688 device.is_tampered);
689
Shashank Mittala0032282011-08-26 14:50:11 -0700690 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800691 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700692 offset = 0;
693
Shashank Mittalcd98d472011-08-02 14:29:24 -0700694 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700695
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800696#if DEVICE_TREE
697 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
698 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700699
700 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
701 {
702 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
703 return -1;
704 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800705#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530706 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700707
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700708#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700709
Amol Jadib6be5c12012-11-14 13:39:51 -0800710 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700711 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800712
Shashank Mittalcd98d472011-08-02 14:29:24 -0700713 /* Read image without signature */
714 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
715 {
716 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
717 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800718 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700719
Amol Jadib6be5c12012-11-14 13:39:51 -0800720 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700721 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800722
Shashank Mittalcd98d472011-08-02 14:29:24 -0700723 offset = imagesize_actual;
724 /* Read signature */
725 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
726 {
727 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700728 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700729 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800730
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700731 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700732
Neeti Desai465491e2012-07-31 12:53:35 -0700733 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700734 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
735 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700736
Neeti Desai465491e2012-07-31 12:53:35 -0700737 #if DEVICE_TREE
738 if(hdr->dt_size) {
739 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700740 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700741
742 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
743
Joel Kingaa335dc2013-06-03 16:11:08 -0700744 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700745 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
746 return -1;
747 }
Neeti Desai465491e2012-07-31 12:53:35 -0700748 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700749 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700750 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
751 return -1;
752 }
753
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700754 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700755 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700756 {
757 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
758 return -1;
759 }
760
Joel Kingaa335dc2013-06-03 16:11:08 -0700761 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800762 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800763 /*
764 * If appended dev tree is found, update the atags with
765 * memory address to the DTB appended location on RAM.
766 * Else update with the atags address in the kernel header
767 */
768 void *dtb;
769 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700770 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800771 if (!dtb) {
772 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
773 return -1;
774 }
Neeti Desai465491e2012-07-31 12:53:35 -0700775 }
776 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700777 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700778 else
779 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800780 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700781
Amol Jadib6be5c12012-11-14 13:39:51 -0800782 dprintf(INFO, "Loading boot image (%d): start\n",
783 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700784 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800785
786 offset = page_size;
787
788 /* Load kernel */
789 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700790 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
791 return -1;
792 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800793 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700794
Amol Jadib6be5c12012-11-14 13:39:51 -0800795 /* Load ramdisk */
796 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700797 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800798 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700799 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
800 return -1;
801 }
802 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800803 offset += ramdisk_actual;
804
805 dprintf(INFO, "Loading boot image (%d): done\n",
806 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700807 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700808
809 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800810 offset += second_actual;
811 /* Second image loading not implemented. */
812 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700813 }
814
815 #if DEVICE_TREE
816 if(hdr->dt_size != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700817 /* Read the device tree table into buffer */
818 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
819 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
820 return -1;
821 }
822 table = (struct dt_table*) dt_buf;
823
Joel Kingaa335dc2013-06-03 16:11:08 -0700824 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700825 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
826 return -1;
827 }
828
Joel Kingaa335dc2013-06-03 16:11:08 -0700829 /* Find index of device tree within device tree table */
830 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700831 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
832 return -1;
833 }
834
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700835 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700836 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700837 {
838 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
839 return -1;
840 }
841
Joel Kingaa335dc2013-06-03 16:11:08 -0700842 if(mmc_read(ptn + offset + dt_entry.offset,
843 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700844 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
845 return -1;
846 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300847 #ifdef TZ_SAVE_KERNEL_HASH
848 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
849 hdr->ramdisk_addr, ramdisk_actual,
850 ptn, offset, hdr->dt_size);
851 #endif /* TZ_SAVE_KERNEL_HASH */
852
Channagoud Kadabi35095622013-03-01 13:53:05 -0800853 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800854 /*
855 * If appended dev tree is found, update the atags with
856 * memory address to the DTB appended location on RAM.
857 * Else update with the atags address in the kernel header
858 */
859 void *dtb;
860 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700861 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800862 if (!dtb) {
863 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
864 return -1;
865 }
Neeti Desai465491e2012-07-31 12:53:35 -0700866 }
867 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700868 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700869
870unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700871
Dima Zavin77e41f32013-03-06 16:10:43 -0800872 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700873 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700874 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
875
876 return 0;
877}
878
Dima Zavin214cc642009-01-26 11:16:21 -0800879int boot_linux_from_flash(void)
880{
881 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800882 struct ptentry *ptn;
883 struct ptable *ptable;
884 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800885
Shashank Mittalcd98d472011-08-02 14:29:24 -0700886 unsigned char *image_addr = 0;
887 unsigned kernel_actual;
888 unsigned ramdisk_actual;
889 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800890 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700891
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700892#if DEVICE_TREE
893 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700894 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800895 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700896#endif
897
David Ng183a7422009-12-07 14:55:21 -0800898 if (target_is_emmc_boot()) {
899 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
900 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
901 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
902 return -1;
903 }
904 goto continue_boot;
905 }
906
Dima Zavin214cc642009-01-26 11:16:21 -0800907 ptable = flash_get_ptable();
908 if (ptable == NULL) {
909 dprintf(CRITICAL, "ERROR: Partition table not found\n");
910 return -1;
911 }
912
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800913 if(!boot_into_recovery)
914 {
915 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700916
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800917 if (ptn == NULL) {
918 dprintf(CRITICAL, "ERROR: No boot partition found\n");
919 return -1;
920 }
921 }
922 else
923 {
924 ptn = ptable_find(ptable, "recovery");
925 if (ptn == NULL) {
926 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
927 return -1;
928 }
Dima Zavin214cc642009-01-26 11:16:21 -0800929 }
930
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800931 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800932 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
933 return -1;
934 }
Dima Zavin214cc642009-01-26 11:16:21 -0800935
936 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700937 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800938 return -1;
939 }
940
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800941 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700942 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 -0800943 return -1;
944 }
945
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700946 /*
947 * Update the kernel/ramdisk/tags address if the boot image header
948 * has default values, these default values come from mkbootimg when
949 * the boot image is flashed using fastboot flash:raw
950 */
951 update_ker_tags_rdisk_addr(hdr);
952
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700953 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700954 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
955 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
956 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
957
958 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
959 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
960
961 /* Check if the addresses in the header are valid. */
962 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
963 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
964 {
965 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
966 return -1;
967 }
968
969#ifndef DEVICE_TREE
970 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
971 {
972 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
973 return -1;
974 }
975#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700976
Shashank Mittalcd98d472011-08-02 14:29:24 -0700977 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700978 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700979 {
980 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700981 offset = 0;
982
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800983#if DEVICE_TREE
984 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
985 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700986
987 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
988 {
989 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
990 return -1;
991 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800992#else
Shashank Mittalcd98d472011-08-02 14:29:24 -0700993 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800994#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700995
Amol Jadib6be5c12012-11-14 13:39:51 -0800996 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700997 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800998
Shashank Mittalcd98d472011-08-02 14:29:24 -0700999 /* Read image without signature */
1000 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1001 {
1002 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1003 return -1;
1004 }
Dima Zavin214cc642009-01-26 11:16:21 -08001005
Amol Jadib6be5c12012-11-14 13:39:51 -08001006 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001007 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001008
Shashank Mittalcd98d472011-08-02 14:29:24 -07001009 offset = imagesize_actual;
1010 /* Read signature */
1011 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1012 {
1013 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001014 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001015 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001016
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001017 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001018
1019 /* Move kernel and ramdisk to correct address */
1020 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1021 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001022#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001023 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001024 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001025 {
1026 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1027 return -1;
1028 }
1029
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001030 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1031#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001032
1033 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001034 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001035 {
1036 write_device_info_flash(&device);
1037 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301038#if USE_PCOM_SECBOOT
1039 set_tamper_flag(device.is_tampered);
1040#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001041 }
1042 else
1043 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001044 offset = page_size;
1045
Amol Jadib6be5c12012-11-14 13:39:51 -08001046 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1047 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1048 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1049
1050 dprintf(INFO, "Loading boot image (%d): start\n",
1051 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001052 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001053
1054 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001055 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1056 return -1;
1057 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001058 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001059
Amol Jadib6be5c12012-11-14 13:39:51 -08001060 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001061 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1062 return -1;
1063 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001064 offset += ramdisk_actual;
1065
1066 dprintf(INFO, "Loading boot image (%d): done\n",
1067 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001068 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001069
1070 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001071 offset += second_actual;
1072 /* Second image loading not implemented. */
1073 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001074 }
1075
1076#if DEVICE_TREE
1077 if(hdr->dt_size != 0) {
1078
1079 /* Read the device tree table into buffer */
1080 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1081 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1082 return -1;
1083 }
1084
1085 table = (struct dt_table*) dt_buf;
1086
Joel Kingaa335dc2013-06-03 16:11:08 -07001087 if (dev_tree_validate(table, hdr->page_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001088 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1089 return -1;
1090 }
1091
Joel Kingaa335dc2013-06-03 16:11:08 -07001092 /* Find index of device tree within device tree table */
1093 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001094 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1095 return -1;
1096 }
1097
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001098 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001099 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001100 {
1101 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1102 return -1;
1103 }
1104
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001105 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001106 if(flash_read(ptn, offset + dt_entry.offset,
1107 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001108 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1109 return -1;
1110 }
1111 }
1112#endif
1113
Shashank Mittalcd98d472011-08-02 14:29:24 -07001114 }
David Ng183a7422009-12-07 14:55:21 -08001115continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001116
Dima Zavin214cc642009-01-26 11:16:21 -08001117 /* TODO: create/pass atags to kernel */
1118
Ajay Dudanie28a6072011-07-01 13:59:46 -07001119 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001120 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001121 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1122
1123 return 0;
1124}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001125
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001126BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001127void write_device_info_mmc(device_info *dev)
1128{
1129 struct device_info *info = (void*) info_buf;
1130 unsigned long long ptn = 0;
1131 unsigned long long size;
1132 int index = INVALID_PTN;
1133
1134 index = partition_get_index("aboot");
1135 ptn = partition_get_offset(index);
1136 if(ptn == 0)
1137 {
1138 return;
1139 }
1140
1141 size = partition_get_size(index);
1142
1143 memcpy(info, dev, sizeof(device_info));
1144
1145 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1146 {
1147 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1148 return;
1149 }
1150}
1151
1152void read_device_info_mmc(device_info *dev)
1153{
1154 struct device_info *info = (void*) info_buf;
1155 unsigned long long ptn = 0;
1156 unsigned long long size;
1157 int index = INVALID_PTN;
1158
1159 index = partition_get_index("aboot");
1160 ptn = partition_get_offset(index);
1161 if(ptn == 0)
1162 {
1163 return;
1164 }
1165
1166 size = partition_get_size(index);
1167
1168 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1169 {
1170 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1171 return;
1172 }
1173
1174 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1175 {
1176 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1177 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001178 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001179
1180 write_device_info_mmc(info);
1181 }
1182 memcpy(dev, info, sizeof(device_info));
1183}
1184
1185void write_device_info_flash(device_info *dev)
1186{
1187 struct device_info *info = (void *) info_buf;
1188 struct ptentry *ptn;
1189 struct ptable *ptable;
1190
1191 ptable = flash_get_ptable();
1192 if (ptable == NULL)
1193 {
1194 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1195 return;
1196 }
1197
1198 ptn = ptable_find(ptable, "devinfo");
1199 if (ptn == NULL)
1200 {
1201 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1202 return;
1203 }
1204
1205 memcpy(info, dev, sizeof(device_info));
1206
1207 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1208 {
1209 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1210 return;
1211 }
1212}
1213
1214void read_device_info_flash(device_info *dev)
1215{
1216 struct device_info *info = (void*) info_buf;
1217 struct ptentry *ptn;
1218 struct ptable *ptable;
1219
1220 ptable = flash_get_ptable();
1221 if (ptable == NULL)
1222 {
1223 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1224 return;
1225 }
1226
1227 ptn = ptable_find(ptable, "devinfo");
1228 if (ptn == NULL)
1229 {
1230 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1231 return;
1232 }
1233
1234 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1235 {
1236 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1237 return;
1238 }
1239
1240 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1241 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001242 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1243 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001244 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001245 write_device_info_flash(info);
1246 }
1247 memcpy(dev, info, sizeof(device_info));
1248}
1249
1250void write_device_info(device_info *dev)
1251{
1252 if(target_is_emmc_boot())
1253 {
1254 write_device_info_mmc(dev);
1255 }
1256 else
1257 {
1258 write_device_info_flash(dev);
1259 }
1260}
1261
1262void read_device_info(device_info *dev)
1263{
1264 if(target_is_emmc_boot())
1265 {
1266 read_device_info_mmc(dev);
1267 }
1268 else
1269 {
1270 read_device_info_flash(dev);
1271 }
1272}
1273
1274void reset_device_info()
1275{
1276 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001277 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001278 write_device_info(&device);
1279}
1280
1281void set_device_root()
1282{
1283 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001284 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001285 write_device_info(&device);
1286}
1287
Amol Jadicb524072012-08-09 16:40:18 -07001288#if DEVICE_TREE
1289int copy_dtb(uint8_t *boot_image_start)
1290{
1291 uint32 dt_image_offset = 0;
1292 uint32_t n;
1293 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001294 struct dt_entry dt_entry;
Amol Jadicb524072012-08-09 16:40:18 -07001295
1296 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1297
Amol Jadicb524072012-08-09 16:40:18 -07001298 if(hdr->dt_size != 0) {
1299
1300 /* add kernel offset */
1301 dt_image_offset += page_size;
1302 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1303 dt_image_offset += n;
1304
1305 /* add ramdisk offset */
1306 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1307 dt_image_offset += n;
1308
1309 /* add second offset */
1310 if(hdr->second_size != 0) {
1311 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1312 dt_image_offset += n;
1313 }
1314
1315 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001316 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001317
Joel Kingaa335dc2013-06-03 16:11:08 -07001318 if (dev_tree_validate(table, hdr->page_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001319 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1320 return -1;
1321 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001322 /* Find index of device tree within device tree table */
1323 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001324 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1325 return -1;
1326 }
1327
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001328 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001329 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001330 {
1331 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1332 return -1;
1333 }
1334
Amol Jadicb524072012-08-09 16:40:18 -07001335 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001336 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001337 boot_image_start + dt_image_offset + dt_entry.offset,
1338 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001339 } else
1340 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001341
1342 /* Everything looks fine. Return success. */
1343 return 0;
1344}
1345#endif
1346
Brian Swetland9c4c0752009-01-25 16:23:50 -08001347void cmd_boot(const char *arg, void *data, unsigned sz)
1348{
1349 unsigned kernel_actual;
1350 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001351 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001352 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001353 int ret = 0;
1354 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001355
1356 if (sz < sizeof(hdr)) {
1357 fastboot_fail("invalid bootimage header");
1358 return;
1359 }
1360
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001361 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001362
1363 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001364 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001365
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001366 if(target_is_emmc_boot() && hdr->page_size) {
1367 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001368 page_mask = page_size - 1;
1369 }
1370
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001371 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1372 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1373
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001374 /*
1375 * Update the kernel/ramdisk/tags address if the boot image header
1376 * has default values, these default values come from mkbootimg when
1377 * the boot image is flashed using fastboot flash:raw
1378 */
1379 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001380
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001381 /* Get virtual addresses since the hdr saves physical addresses. */
1382 hdr->kernel_addr = VA(hdr->kernel_addr);
1383 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1384 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001385
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001386 /* Check if the addresses in the header are valid. */
1387 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1388 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1389 {
1390 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1391 return -1;
1392 }
1393
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001394 /* sz should have atleast raw boot image */
1395 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001396 fastboot_fail("incomplete bootimage");
1397 return;
1398 }
1399
Amol Jadicb524072012-08-09 16:40:18 -07001400#if DEVICE_TREE
1401 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001402 ret = copy_dtb(data);
1403
1404 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001405#else
1406 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1407 {
1408 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1409 return -1;
1410 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001411#endif
1412
1413 /* Load ramdisk & kernel */
1414 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1415 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1416
1417#if DEVICE_TREE
1418 /*
1419 * If dtb is not found look for appended DTB in the kernel.
1420 * If appended dev tree is found, update the atags with
1421 * memory address to the DTB appended location on RAM.
1422 * Else update with the atags address in the kernel header
1423 */
1424 if (!dtb_copied) {
1425 void *dtb;
Channagoud Kadabia83a6972013-04-25 15:26:04 -07001426 dtb = dev_tree_appended((void *)hdr->kernel_addr, (void *)hdr->tags_addr, hdr->kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001427 if (!dtb) {
1428 fastboot_fail("dtb not found");
1429 return;
1430 }
Amol Jadicb524072012-08-09 16:40:18 -07001431 }
1432#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001433
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001434#ifndef DEVICE_TREE
1435 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1436 {
1437 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1438 return -1;
1439 }
1440#endif
1441
Brian Swetland9c4c0752009-01-25 16:23:50 -08001442 fastboot_okay("");
1443 udc_stop();
1444
Dima Zavin77e41f32013-03-06 16:10:43 -08001445 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001446 (const char*) hdr->cmdline, board_machtype(),
1447 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001448}
1449
Dima Zavin214cc642009-01-26 11:16:21 -08001450void cmd_erase(const char *arg, void *data, unsigned sz)
1451{
1452 struct ptentry *ptn;
1453 struct ptable *ptable;
1454
1455 ptable = flash_get_ptable();
1456 if (ptable == NULL) {
1457 fastboot_fail("partition table doesn't exist");
1458 return;
1459 }
1460
1461 ptn = ptable_find(ptable, arg);
1462 if (ptn == NULL) {
1463 fastboot_fail("unknown partition name");
1464 return;
1465 }
1466
1467 if (flash_erase(ptn)) {
1468 fastboot_fail("failed to erase partition");
1469 return;
1470 }
1471 fastboot_okay("");
1472}
1473
Bikas Gurungd48bd242010-09-04 19:54:32 -07001474
1475void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1476{
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001477 BUF_DMA_ALIGN(out, 512);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001478 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001479 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001480
Kinson Chikf1a43512011-07-14 11:28:39 -07001481 index = partition_get_index(arg);
1482 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001483
Kinson Chikf1a43512011-07-14 11:28:39 -07001484 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001485 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001486 return;
1487 }
neetidb4b24d62012-01-20 12:13:09 -08001488 /* Simple inefficient version of erase. Just writing
1489 0 in first block */
1490 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1491 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001492 return;
1493 }
1494 fastboot_okay("");
1495}
1496
1497
Ajay Dudani5c761132011-04-07 20:19:04 -07001498void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001499{
1500 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001501 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001502 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001503
Greg Grisco6e754772011-06-23 12:19:39 -07001504 if (!strcmp(arg, "partition"))
1505 {
1506 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001507 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001508 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001509 return;
1510 }
1511 }
Greg Grisco6e754772011-06-23 12:19:39 -07001512 else
1513 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001514 index = partition_get_index(arg);
1515 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001516 if(ptn == 0) {
1517 fastboot_fail("partition table doesn't exist");
1518 return;
1519 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001520
Greg Grisco6e754772011-06-23 12:19:39 -07001521 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1522 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1523 fastboot_fail("image is not a boot image");
1524 return;
1525 }
1526 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001527
Kinson Chikf1a43512011-07-14 11:28:39 -07001528 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001529 if (ROUND_TO_PAGE(sz,511) > size) {
1530 fastboot_fail("size too large");
1531 return;
1532 }
1533 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1534 fastboot_fail("flash write failure");
1535 return;
1536 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001537 }
1538 fastboot_okay("");
1539 return;
1540}
1541
Ajay Dudani5c761132011-04-07 20:19:04 -07001542void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1543{
1544 unsigned int chunk;
1545 unsigned int chunk_data_sz;
1546 sparse_header_t *sparse_header;
1547 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001548 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001549 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301550 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001551 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001552
Kinson Chikf1a43512011-07-14 11:28:39 -07001553 index = partition_get_index(arg);
1554 ptn = partition_get_offset(index);
1555 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001556 fastboot_fail("partition table doesn't exist");
1557 return;
1558 }
1559
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301560 size = partition_get_size(index);
1561 if (ROUND_TO_PAGE(sz,511) > size) {
1562 fastboot_fail("size too large");
1563 return;
1564 }
1565
Ajay Dudani5c761132011-04-07 20:19:04 -07001566 /* Read and skip over sparse image header */
1567 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001568 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1569 fastboot_fail("size too large");
1570 return;
1571 }
1572
Ajay Dudani5c761132011-04-07 20:19:04 -07001573 data += sparse_header->file_hdr_sz;
1574 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1575 {
1576 /* Skip the remaining bytes in a header that is longer than
1577 * we expected.
1578 */
1579 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1580 }
1581
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001582 dprintf (SPEW, "=== Sparse Image Header ===\n");
1583 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1584 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1585 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1586 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1587 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1588 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1589 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1590 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001591
1592 /* Start processing chunks */
1593 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1594 {
1595 /* Read and skip over chunk header */
1596 chunk_header = (chunk_header_t *) data;
1597 data += sizeof(chunk_header_t);
1598
1599 dprintf (SPEW, "=== Chunk Header ===\n");
1600 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1601 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1602 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1603
1604 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1605 {
1606 /* Skip the remaining bytes in a header that is longer than
1607 * we expected.
1608 */
1609 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1610 }
1611
1612 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1613 switch (chunk_header->chunk_type)
1614 {
1615 case CHUNK_TYPE_RAW:
1616 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1617 chunk_data_sz))
1618 {
1619 fastboot_fail("Bogus chunk size for chunk type Raw");
1620 return;
1621 }
1622
Ajay Dudaniab18f022011-05-12 14:39:22 -07001623 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1624 chunk_data_sz,
1625 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001626 {
1627 fastboot_fail("flash write failure");
1628 return;
1629 }
1630 total_blocks += chunk_header->chunk_sz;
1631 data += chunk_data_sz;
1632 break;
1633
1634 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001635 total_blocks += chunk_header->chunk_sz;
1636 break;
1637
Ajay Dudani5c761132011-04-07 20:19:04 -07001638 case CHUNK_TYPE_CRC:
1639 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1640 {
1641 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1642 return;
1643 }
1644 total_blocks += chunk_header->chunk_sz;
1645 data += chunk_data_sz;
1646 break;
1647
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001648 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001649 fastboot_fail("Unknown chunk type");
1650 return;
1651 }
1652 }
1653
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001654 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1655 total_blocks, sparse_header->total_blks);
1656
1657 if(total_blocks != sparse_header->total_blks)
1658 {
1659 fastboot_fail("sparse image write failure");
1660 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001661
1662 fastboot_okay("");
1663 return;
1664}
1665
1666void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1667{
1668 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001669 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1670 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001671
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001672#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001673 int ret=0;
1674 uint32 major_version=0;
1675 uint32 minor_version=0;
1676
1677 ret = scm_svc_version(&major_version,&minor_version);
1678 if(!ret)
1679 {
1680 if(major_version >= 2)
1681 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001682 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001683 {
1684 ret = encrypt_scm((uint32 **) &data, &sz);
1685 if (ret != 0) {
1686 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1687 return;
1688 }
1689
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001690 /* Protect only for SSD */
1691 if (!strcmp(arg, "ssd")) {
1692 ret = scm_protect_keystore((uint32 *) data, sz);
1693 if (ret != 0) {
1694 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1695 return;
1696 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001697 }
1698 }
1699 else
1700 {
1701 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1702 if(ret != 0)
1703 {
1704 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1705 return;
1706 }
1707 }
1708 }
1709 else
1710 {
1711 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1712 magic_number[1] == DECRYPT_MAGIC_1)
1713 {
1714 ret = decrypt_scm((uint32 **) &data, &sz);
1715 if (ret != 0) {
1716 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1717 return;
1718 }
1719 }
1720 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1721 magic_number[1] == ENCRYPT_MAGIC_1)
1722 {
1723 ret = encrypt_scm((uint32 **) &data, &sz);
1724 if (ret != 0) {
1725 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1726 return;
1727 }
1728 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001729 }
1730 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001731 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001732 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001733 dprintf(CRITICAL,"INVALID SVC Version\n");
1734 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001735 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001736#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001737
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001738 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001739 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1740 cmd_flash_mmc_img(arg, data, sz);
1741 else
1742 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001743 return;
1744}
1745
Dima Zavin214cc642009-01-26 11:16:21 -08001746void cmd_flash(const char *arg, void *data, unsigned sz)
1747{
1748 struct ptentry *ptn;
1749 struct ptable *ptable;
1750 unsigned extra = 0;
1751
1752 ptable = flash_get_ptable();
1753 if (ptable == NULL) {
1754 fastboot_fail("partition table doesn't exist");
1755 return;
1756 }
1757
1758 ptn = ptable_find(ptable, arg);
1759 if (ptn == NULL) {
1760 fastboot_fail("unknown partition name");
1761 return;
1762 }
1763
1764 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1765 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1766 fastboot_fail("image is not a boot image");
1767 return;
1768 }
1769 }
1770
Amol Jadi5c61a952012-05-04 17:05:35 -07001771 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001772 || !strcmp(ptn->name, "userdata")
1773 || !strcmp(ptn->name, "persist")
1774 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301775 if (flash_ecc_bch_enabled())
1776 /* Spare data bytes for 8 bit ECC increased by 4 */
1777 extra = ((page_size >> 9) * 20);
1778 else
1779 extra = ((page_size >> 9) * 16);
1780 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001781 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001782
1783 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1784 if (flash_write(ptn, extra, data, sz)) {
1785 fastboot_fail("flash write failure");
1786 return;
1787 }
1788 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1789 fastboot_okay("");
1790}
1791
1792void cmd_continue(const char *arg, void *data, unsigned sz)
1793{
1794 fastboot_okay("");
1795 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001796 if (target_is_emmc_boot())
1797 {
1798 boot_linux_from_mmc();
1799 }
1800 else
1801 {
1802 boot_linux_from_flash();
1803 }
Dima Zavin214cc642009-01-26 11:16:21 -08001804}
1805
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001806void cmd_reboot(const char *arg, void *data, unsigned sz)
1807{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001808 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001809 fastboot_okay("");
1810 reboot_device(0);
1811}
1812
1813void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1814{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001815 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001816 fastboot_okay("");
1817 reboot_device(FASTBOOT_MODE);
1818}
1819
Shashank Mittal162244e2011-08-08 19:01:25 -07001820void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1821{
1822 if(!device.is_unlocked)
1823 {
1824 device.is_unlocked = 1;
1825 write_device_info(&device);
1826 }
1827 fastboot_okay("");
1828}
1829
Shashank Mittala0032282011-08-26 14:50:11 -07001830void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1831{
1832 char response[64];
1833 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1834 fastboot_info(response);
1835 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1836 fastboot_info(response);
1837 fastboot_okay("");
1838}
1839
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001840void cmd_preflash(const char *arg, void *data, unsigned sz)
1841{
1842 fastboot_okay("");
1843}
1844
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001845void splash_screen ()
1846{
1847 struct ptentry *ptn;
1848 struct ptable *ptable;
1849 struct fbcon_config *fb_display = NULL;
1850
1851 if (!target_is_emmc_boot())
1852 {
1853 ptable = flash_get_ptable();
1854 if (ptable == NULL) {
1855 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001856 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001857 }
1858
1859 ptn = ptable_find(ptable, "splash");
1860 if (ptn == NULL) {
1861 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1862 } else {
1863 fb_display = fbcon_display();
1864 if (fb_display) {
1865 if (flash_read(ptn, 0, fb_display->base,
1866 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1867 fbcon_clear();
1868 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1869 }
1870 }
1871 }
1872 }
1873}
1874
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001875/* Get the size from partiton name */
1876static void get_partition_size(const char *arg, char *response)
1877{
1878 uint64_t ptn = 0;
1879 uint64_t size;
1880 int index = INVALID_PTN;
1881
1882 index = partition_get_index(arg);
1883
1884 if (index == INVALID_PTN)
1885 {
1886 dprintf(CRITICAL, "Invalid partition index\n");
1887 return;
1888 }
1889
1890 ptn = partition_get_offset(index);
1891
1892 if(!ptn)
1893 {
1894 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1895 return;
1896 }
1897
1898 size = partition_get_size(index);
1899
1900 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1901 return;
1902}
1903
1904/*
1905 * Publish the partition type & size info
1906 * fastboot getvar will publish the required information.
1907 * fastboot getvar partition_size:<partition_name>: partition size in hex
1908 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1909 */
1910static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1911{
1912 uint8_t i;
1913
1914 for (i = 0; i < num_parts; i++) {
1915 get_partition_size(info[i].part_name, info[i].size_response);
1916
1917 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1918 {
1919 dprintf(CRITICAL, "partition size name truncated\n");
1920 return;
1921 }
1922 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1923 {
1924 dprintf(CRITICAL, "partition type name truncated\n");
1925 return;
1926 }
1927
1928 /* publish partition size & type info */
1929 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1930 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1931 }
1932}
1933
Amol Jadi5edf3552013-07-23 14:15:34 -07001934/* register commands and variables for fastboot */
1935void aboot_fastboot_register_commands(void)
1936{
1937 if (target_is_emmc_boot())
1938 {
1939 fastboot_register("flash:", cmd_flash_mmc);
1940 fastboot_register("erase:", cmd_erase_mmc);
1941 }
1942 else
1943 {
1944 fastboot_register("flash:", cmd_flash);
1945 fastboot_register("erase:", cmd_erase);
1946 }
1947
1948 fastboot_register("boot", cmd_boot);
1949 fastboot_register("continue", cmd_continue);
1950 fastboot_register("reboot", cmd_reboot);
1951 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
1952 fastboot_register("oem unlock", cmd_oem_unlock);
1953 fastboot_register("oem device-info", cmd_oem_devinfo);
1954 fastboot_register("preflash", cmd_preflash);
1955
1956 /* publish variables and their values */
1957 fastboot_publish("product", TARGET(BOARD));
1958 fastboot_publish("kernel", "lk");
1959 fastboot_publish("serialno", sn_buf);
1960
1961 /*
1962 * partition info is supported only for emmc partitions
1963 * Calling this for NAND prints some error messages which
1964 * is harmless but misleading. Avoid calling this for NAND
1965 * devices.
1966 */
1967 if (target_is_emmc_boot())
1968 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
1969
1970 /* Max download size supported */
1971 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", target_get_max_flash_size());
1972 fastboot_publish("max-download-size", (const char *) max_download_size);
1973}
1974
Brian Swetland9c4c0752009-01-25 16:23:50 -08001975void aboot_init(const struct app_descriptor *app)
1976{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001977 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03001978 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001979
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001980 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001981 if (target_is_emmc_boot())
1982 {
1983 page_size = 2048;
1984 page_mask = page_size - 1;
1985 }
1986 else
1987 {
1988 page_size = flash_page_size();
1989 page_mask = page_size - 1;
1990 }
1991
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001992 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
1993
Shashank Mittal162244e2011-08-08 19:01:25 -07001994 if(target_use_signed_kernel())
1995 {
1996 read_device_info(&device);
1997
Shashank Mittal162244e2011-08-08 19:01:25 -07001998 }
1999
Greg Griscod6250552011-06-29 14:40:23 -07002000 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002001 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002002
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002003 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002004 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002005 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002006 dprintf(ALWAYS,"dload mode key sequence detected\n");
2007 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002008 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002009 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002010 }
2011 else
2012 {
2013 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002014 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002015 }
2016 boot_into_fastboot = true;
2017 }
2018 if (!boot_into_fastboot)
2019 {
2020 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2021 boot_into_recovery = 1;
2022 if (!boot_into_recovery &&
2023 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002024 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002025 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002026 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002027 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002028 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002029 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002030
Ajay Dudani77421292010-10-27 19:34:06 -07002031 reboot_mode = check_reboot_mode();
2032 if (reboot_mode == RECOVERY_MODE) {
2033 boot_into_recovery = 1;
2034 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002035 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002036 }
2037
Pavel Nedev5d91d412013-04-29 11:34:24 +03002038 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002039 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002040 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002041 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002042 if(emmc_recovery_init())
2043 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2044 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002045 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002046 if((device.is_unlocked) || (device.is_tampered))
2047 {
2048 #ifdef TZ_TAMPER_FUSE
2049 set_tamper_fuse_cmd();
2050 #endif
2051 #if USE_PCOM_SECBOOT
2052 set_tamper_flag(device.is_tampered);
2053 #endif
2054 }
Shashank Mittala0032282011-08-26 14:50:11 -07002055 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002056 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002057 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002058 else
2059 {
2060 recovery_init();
2061 #if USE_PCOM_SECBOOT
2062 if((device.is_unlocked) || (device.is_tampered))
2063 set_tamper_flag(device.is_tampered);
2064 #endif
2065 boot_linux_from_flash();
2066 }
2067 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2068 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002069 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002070
Amol Jadi5edf3552013-07-23 14:15:34 -07002071 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002072
Amol Jadi5edf3552013-07-23 14:15:34 -07002073 /* register aboot specific fastboot commands */
2074 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002075
Amol Jadi5edf3552013-07-23 14:15:34 -07002076 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002077 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002078
2079 /* initialize and start fastboot */
2080 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002081}
2082
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002083uint32_t get_page_size()
2084{
2085 return page_size;
2086}
2087
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002088/*
2089 * Calculated and save hash (SHA256) for non-signed boot image.
2090 *
2091 * Hash the same data that is checked on the signed boot image.
2092 * Kernel and Ramdisk are already read to memory buffers.
2093 * Need to read the entire device-tree from mmc
2094 * since non-signed image only read the DT tags of the relevant platform.
2095 *
2096 * @param kernel_addr - kernel bufer
2097 * @param kernel_actual - kernel size in bytes
2098 * @param ramdisk_addr - ramdisk buffer
2099 * @param ramdisk_actual - ramdisk size
2100 * @param ptn - partition
2101 * @param dt_offset - device tree offset on mmc partition
2102 * @param dt_size
2103 *
2104 * @return int - 0 on success, negative value on failure.
2105 */
2106int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2107 void *ramdisk_addr, unsigned ramdisk_actual,
2108 unsigned long long ptn,
2109 unsigned dt_offset, unsigned dt_size)
2110{
2111 SHA256_CTX sha256_ctx;
2112 char digest[32]={0};
2113 char *buf = (char *)target_get_scratch_address();
2114 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2115 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2116
2117 SHA256_Init(&sha256_ctx);
2118
2119 /* Read Boot Header */
2120 if (mmc_read(ptn, buf, page_size))
2121 {
2122 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2123 return -1;
2124 }
2125 /* Read entire Device Tree */
2126 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2127 {
2128 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2129 return -1;
2130 }
2131 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2132 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2133 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2134 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2135
2136 SHA256_Final(digest, &sha256_ctx);
2137
2138 save_kernel_hash_cmd(digest);
2139 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2140
2141 return 0;
2142}
2143
Brian Swetland9c4c0752009-01-25 16:23:50 -08002144APP_START(aboot)
2145 .init = aboot_init,
2146APP_END