blob: 86cbadc957325d4db235fdf921281d381a1425fb [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);
76
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";
David Ng183a7422009-12-07 14:55:21 -0800104
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800105static const char *baseband_apq = " androidboot.baseband=apq";
106static const char *baseband_msm = " androidboot.baseband=msm";
107static const char *baseband_csfb = " androidboot.baseband=csfb";
108static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700109static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700110static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800111static const char *baseband_dsda = " androidboot.baseband=dsda";
112static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800113static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800114
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700115static unsigned page_size = 0;
116static unsigned page_mask = 0;
117static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
118static bool boot_into_ffbm;
119
Shashank Mittalcd98d472011-08-02 14:29:24 -0700120/* Assuming unauthorized kernel image by default */
121static int auth_kernel_img = 0;
122
Shashank Mittal162244e2011-08-08 19:01:25 -0700123static device_info device = {DEVICE_MAGIC, 0, 0};
124
Brian Swetland9c4c0752009-01-25 16:23:50 -0800125static struct udc_device surf_udc_device = {
126 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -0800127 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800128 .version_id = 0x0100,
129 .manufacturer = "Google",
130 .product = "Android",
131};
132
Dima Zavin42168f22009-01-30 11:52:22 -0800133struct atag_ptbl_entry
134{
135 char name[16];
136 unsigned offset;
137 unsigned size;
138 unsigned flags;
139};
140
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700141/*
142 * Partition info, required to be published
143 * for fastboot
144 */
145struct getvar_partition_info {
146 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
147 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
148 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
149 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
150 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
151};
152
153/*
154 * Right now, we are publishing the info for only
155 * three partitions
156 */
157struct getvar_partition_info part_info[] =
158{
159 { "system" , "partition-size:", "partition-type:", "", "ext4" },
160 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
161 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
162};
163
164char max_download_size[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800165char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700166
Greg Griscod2471ef2011-07-14 13:00:42 -0700167extern int emmc_recovery_init(void);
168
Kinson Chik0b1c8162011-08-31 16:31:57 -0700169#if NO_KEYPAD_DRIVER
170extern int fastboot_trigger(void);
171#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700172
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700173static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
174{
175 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700176#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
177 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
178 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
179 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700180#endif
181}
182
Dima Zavin42168f22009-01-30 11:52:22 -0800183static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
184{
185 struct atag_ptbl_entry atag_ptn;
186
187 memcpy(atag_ptn.name, ptn->name, 16);
188 atag_ptn.name[15] = '\0';
189 atag_ptn.offset = ptn->start;
190 atag_ptn.size = ptn->length;
191 atag_ptn.flags = ptn->flags;
192 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
193 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
194}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800195
Neeti Desaie245d492012-06-01 12:52:13 -0700196unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800197{
David Ng183a7422009-12-07 14:55:21 -0800198 int cmdline_len = 0;
199 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800200 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700201 int pause_at_bootup = 0;
Dima Zavin42168f22009-01-30 11:52:22 -0800202
Brian Swetland9c4c0752009-01-25 16:23:50 -0800203 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800204 cmdline_len = strlen(cmdline);
205 have_cmdline = 1;
206 }
207 if (target_is_emmc_boot()) {
208 cmdline_len += strlen(emmc_cmdline);
209 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800210
211 cmdline_len += strlen(usb_sn_cmdline);
212 cmdline_len += strlen(sn_buf);
213
Pavel Nedev328ac822013-04-05 15:25:11 +0300214 if (boot_into_ffbm) {
215 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700216 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800217 /* reduce kernel console messages to speed-up boot */
218 cmdline_len += strlen(loglevel);
Pavel Nedev328ac822013-04-05 15:25:11 +0300219 } else if (target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700220 pause_at_bootup = 1;
221 cmdline_len += strlen(battchg_pause);
222 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800223
Shashank Mittalcd98d472011-08-02 14:29:24 -0700224 if(target_use_signed_kernel() && auth_kernel_img) {
225 cmdline_len += strlen(auth_kernel);
226 }
227
Ajay Dudanid04110c2011-01-17 23:55:07 -0800228 /* Determine correct androidboot.baseband to use */
229 switch(target_baseband())
230 {
231 case BASEBAND_APQ:
232 cmdline_len += strlen(baseband_apq);
233 break;
234
235 case BASEBAND_MSM:
236 cmdline_len += strlen(baseband_msm);
237 break;
238
239 case BASEBAND_CSFB:
240 cmdline_len += strlen(baseband_csfb);
241 break;
242
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800243 case BASEBAND_SVLTE2A:
244 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800245 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700246
247 case BASEBAND_MDM:
248 cmdline_len += strlen(baseband_mdm);
249 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700250
251 case BASEBAND_SGLTE:
252 cmdline_len += strlen(baseband_sglte);
253 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530254
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800255 case BASEBAND_SGLTE2:
256 cmdline_len += strlen(baseband_sglte2);
257 break;
258
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530259 case BASEBAND_DSDA:
260 cmdline_len += strlen(baseband_dsda);
261 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800262
263 case BASEBAND_DSDA2:
264 cmdline_len += strlen(baseband_dsda2);
265 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800266 }
267
David Ng183a7422009-12-07 14:55:21 -0800268 if (cmdline_len > 0) {
269 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700270 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
271 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700272
Amol Jadi168b7712012-03-06 16:15:00 -0800273 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700274 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800275 if (have_cmdline) {
276 src = cmdline;
277 while ((*dst++ = *src++));
278 }
279 if (target_is_emmc_boot()) {
280 src = emmc_cmdline;
281 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700282 have_cmdline = 1;
283 while ((*dst++ = *src++));
284 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800285
286 src = usb_sn_cmdline;
287 if (have_cmdline) --dst;
288 have_cmdline = 1;
289 while ((*dst++ = *src++));
290 src = sn_buf;
291 if (have_cmdline) --dst;
292 have_cmdline = 1;
293 while ((*dst++ = *src++));
294
Pavel Nedev328ac822013-04-05 15:25:11 +0300295 if (boot_into_ffbm) {
296 src = androidboot_mode;
297 if (have_cmdline) --dst;
298 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700299 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300300 if (have_cmdline) --dst;
301 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800302 src = loglevel;
303 if (have_cmdline) --dst;
304 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300305 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700306 src = battchg_pause;
307 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800308 while ((*dst++ = *src++));
309 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800310
Shashank Mittalcd98d472011-08-02 14:29:24 -0700311 if(target_use_signed_kernel() && auth_kernel_img) {
312 src = auth_kernel;
313 if (have_cmdline) --dst;
314 while ((*dst++ = *src++));
315 }
316
Ajay Dudanid04110c2011-01-17 23:55:07 -0800317 switch(target_baseband())
318 {
319 case BASEBAND_APQ:
320 src = baseband_apq;
321 if (have_cmdline) --dst;
322 while ((*dst++ = *src++));
323 break;
324
325 case BASEBAND_MSM:
326 src = baseband_msm;
327 if (have_cmdline) --dst;
328 while ((*dst++ = *src++));
329 break;
330
331 case BASEBAND_CSFB:
332 src = baseband_csfb;
333 if (have_cmdline) --dst;
334 while ((*dst++ = *src++));
335 break;
336
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800337 case BASEBAND_SVLTE2A:
338 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800339 if (have_cmdline) --dst;
340 while ((*dst++ = *src++));
341 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700342
343 case BASEBAND_MDM:
344 src = baseband_mdm;
345 if (have_cmdline) --dst;
346 while ((*dst++ = *src++));
347 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700348
349 case BASEBAND_SGLTE:
350 src = baseband_sglte;
351 if (have_cmdline) --dst;
352 while ((*dst++ = *src++));
353 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530354
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800355 case BASEBAND_SGLTE2:
356 src = baseband_sglte2;
357 if (have_cmdline) --dst;
358 while ((*dst++ = *src++));
359 break;
360
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530361 case BASEBAND_DSDA:
362 src = baseband_dsda;
363 if (have_cmdline) --dst;
364 while ((*dst++ = *src++));
365 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800366
367 case BASEBAND_DSDA2:
368 src = baseband_dsda2;
369 if (have_cmdline) --dst;
370 while ((*dst++ = *src++));
371 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800372 }
Neeti Desaie245d492012-06-01 12:52:13 -0700373 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800374 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700375 return cmdline_final;
376}
377
378unsigned *atag_core(unsigned *ptr)
379{
380 /* CORE */
381 *ptr++ = 2;
382 *ptr++ = 0x54410001;
383
384 return ptr;
385
386}
387
388unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
389 unsigned ramdisk_size)
390{
391 if (ramdisk_size) {
392 *ptr++ = 4;
393 *ptr++ = 0x54420005;
394 *ptr++ = (unsigned)ramdisk;
395 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800396 }
397
Neeti Desaie245d492012-06-01 12:52:13 -0700398 return ptr;
399}
400
401unsigned *atag_ptable(unsigned **ptr_addr)
402{
403 int i;
404 struct ptable *ptable;
405
406 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700407 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
408 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700409 *(*ptr_addr)++ = 0x4d534d70;
410 for (i = 0; i < ptable->count; ++i)
411 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
412 }
413
414 return (*ptr_addr);
415}
416
417unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
418{
419 int cmdline_length = 0;
420 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700421 char *dest;
422
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800423 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700424 n = (cmdline_length + 4) & (~3);
425
426 *ptr++ = (n / 4) + 2;
427 *ptr++ = 0x54410009;
428 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800429 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700430 ptr += (n / 4);
431
432 return ptr;
433}
434
435unsigned *atag_end(unsigned *ptr)
436{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800437 /* END */
438 *ptr++ = 0;
439 *ptr++ = 0;
440
Neeti Desaie245d492012-06-01 12:52:13 -0700441 return ptr;
442}
443
444void generate_atags(unsigned *ptr, const char *cmdline,
445 void *ramdisk, unsigned ramdisk_size)
446{
447
448 ptr = atag_core(ptr);
449 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
450 ptr = target_atag_mem(ptr);
451
452 /* Skip NAND partition ATAGS for eMMC boot */
453 if (!target_is_emmc_boot()){
454 ptr = atag_ptable(&ptr);
455 }
456
457 ptr = atag_cmdline(ptr, cmdline);
458 ptr = atag_end(ptr);
459}
460
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700461typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700462void boot_linux(void *kernel, unsigned *tags,
463 const char *cmdline, unsigned machtype,
464 void *ramdisk, unsigned ramdisk_size)
465{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800466 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800467#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700468 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800469#endif
470
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700471 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800472 uint32_t tags_phys = PA((addr_t)tags);
473
474 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700475
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800476 final_cmdline = update_cmdline((const char*)cmdline);
477
Neeti Desai17379b82012-06-04 18:42:53 -0700478#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800479 dprintf(INFO, "Updating device tree: start\n");
480
Neeti Desai17379b82012-06-04 18:42:53 -0700481 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800482 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700483 if(ret)
484 {
485 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
486 ASSERT(0);
487 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800488 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700489#else
Neeti Desaie245d492012-06-01 12:52:13 -0700490 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800491 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700492#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700493
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800494 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
495 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800496
497 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700498
Amol Jadi4421e652011-06-16 15:00:48 -0700499 /* do any platform specific cleanup before kernel entry */
500 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700501
Brian Swetland9c4c0752009-01-25 16:23:50 -0800502 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700503
Amol Jadi504f9fe2012-08-16 13:56:48 -0700504#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800505 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700506#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700507 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700508 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800509}
510
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700511/* Function to check if the memory address range falls within the aboot
512 * boundaries.
513 * start: Start of the memory region
514 * size: Size of the memory region
515 */
516int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
517{
518 /* Check for boundary conditions. */
519 if ((start + size) < start)
520 return -1;
521
522 /* Check for memory overlap. */
523 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
524 return 0;
525 else if (start > (MEMBASE + MEMSIZE))
526 return 0;
527 else
528 return -1;
529}
530
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800531#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800532
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700533BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800534#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700535BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800536#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800537
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700538static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
539{
540 int ret;
541
542 /* Assume device is rooted at this time. */
543 device.is_tampered = 1;
544
545 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
546
547 ret = image_verify((unsigned char *)bootimg_addr,
548 (unsigned char *)(bootimg_addr + bootimg_size),
549 bootimg_size,
550 CRYPTO_AUTH_ALG_SHA256);
551
552 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
553
554 if (ret)
555 {
556 /* Authorized kernel */
557 device.is_tampered = 0;
558 }
559
560#if USE_PCOM_SECBOOT
561 set_tamper_flag(device.is_tampered);
562#endif
563
564 if(device.is_tampered)
565 {
566 write_device_info_mmc(&device);
567 #ifdef TZ_TAMPER_FUSE
568 set_tamper_fuse_cmd();
569 #endif
570 #ifdef ASSERT_ON_TAMPER
571 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
572 ASSERT(0);
573 #endif
574 }
575}
576
Shashank Mittal23b8f422010-04-16 19:27:21 -0700577int boot_linux_from_mmc(void)
578{
579 struct boot_img_hdr *hdr = (void*) buf;
580 struct boot_img_hdr *uhdr;
581 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700582 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700583 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700584 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700585
Shashank Mittalcd98d472011-08-02 14:29:24 -0700586 unsigned char *image_addr = 0;
587 unsigned kernel_actual;
588 unsigned ramdisk_actual;
589 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700590 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700591
592#if DEVICE_TREE
593 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700594 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700595 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800596 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700597#endif
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700598 if (!boot_into_recovery) {
599 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
600 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
601 if (rcode <= 0) {
602 boot_into_ffbm = false;
603 if (rcode < 0)
604 dprintf(CRITICAL,"failed to get ffbm cookie");
605 } else
606 boot_into_ffbm = true;
607 } else
608 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700609 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
610 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
611 dprintf(INFO, "Unified boot method!\n");
612 hdr = uhdr;
613 goto unified_boot;
614 }
Greg Griscod6250552011-06-29 14:40:23 -0700615 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700616 index = partition_get_index("boot");
617 ptn = partition_get_offset(index);
618 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700619 dprintf(CRITICAL, "ERROR: No boot partition found\n");
620 return -1;
621 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700622 }
623 else {
624 index = partition_get_index("recovery");
625 ptn = partition_get_offset(index);
626 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700627 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
628 return -1;
629 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700630 }
631
Greg Griscod6250552011-06-29 14:40:23 -0700632 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700633 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
634 return -1;
635 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700636
637 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700638 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700639 return -1;
640 }
641
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700642 if (hdr->page_size && (hdr->page_size != page_size)) {
643 page_size = hdr->page_size;
644 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700645 }
646
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700647 /*
648 * Update the kernel/ramdisk/tags address if the boot image header
649 * has default values, these default values come from mkbootimg when
650 * the boot image is flashed using fastboot flash:raw
651 */
652 update_ker_tags_rdisk_addr(hdr);
653
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700654 /* Get virtual addresses since the hdr saves physical addresses. */
655 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
656 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
657 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700658
659 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
660 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
661
662 /* Check if the addresses in the header are valid. */
663 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
664 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
665 {
666 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
667 return -1;
668 }
669
670#ifndef DEVICE_TREE
671 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
672 {
673 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
674 return -1;
675 }
676#endif
677
Shashank Mittalcd98d472011-08-02 14:29:24 -0700678 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300679 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
680 (int) target_use_signed_kernel(),
681 device.is_unlocked,
682 device.is_tampered);
683
Shashank Mittala0032282011-08-26 14:50:11 -0700684 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800685 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700686 offset = 0;
687
Shashank Mittalcd98d472011-08-02 14:29:24 -0700688 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700689
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800690#if DEVICE_TREE
691 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
692 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700693
694 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
695 {
696 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
697 return -1;
698 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800699#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530700 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700701
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700702#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700703
Amol Jadib6be5c12012-11-14 13:39:51 -0800704 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700705 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800706
Shashank Mittalcd98d472011-08-02 14:29:24 -0700707 /* Read image without signature */
708 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
709 {
710 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
711 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800712 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700713
Amol Jadib6be5c12012-11-14 13:39:51 -0800714 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700715 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800716
Shashank Mittalcd98d472011-08-02 14:29:24 -0700717 offset = imagesize_actual;
718 /* Read signature */
719 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
720 {
721 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700722 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700723 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800724
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700725 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700726
Neeti Desai465491e2012-07-31 12:53:35 -0700727 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700728 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
729 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700730
Neeti Desai465491e2012-07-31 12:53:35 -0700731 #if DEVICE_TREE
732 if(hdr->dt_size) {
733 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700734 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700735
736 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
737
Joel Kingaa335dc2013-06-03 16:11:08 -0700738 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700739 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
740 return -1;
741 }
Neeti Desai465491e2012-07-31 12:53:35 -0700742 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700743 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700744 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
745 return -1;
746 }
747
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700748 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700749 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700750 {
751 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
752 return -1;
753 }
754
Joel Kingaa335dc2013-06-03 16:11:08 -0700755 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800756 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800757 /*
758 * If appended dev tree is found, update the atags with
759 * memory address to the DTB appended location on RAM.
760 * Else update with the atags address in the kernel header
761 */
762 void *dtb;
763 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700764 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800765 if (!dtb) {
766 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
767 return -1;
768 }
Neeti Desai465491e2012-07-31 12:53:35 -0700769 }
770 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700771 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700772 else
773 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800774 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700775
Amol Jadib6be5c12012-11-14 13:39:51 -0800776 dprintf(INFO, "Loading boot image (%d): start\n",
777 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700778 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800779
780 offset = page_size;
781
782 /* Load kernel */
783 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700784 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
785 return -1;
786 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800787 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700788
Amol Jadib6be5c12012-11-14 13:39:51 -0800789 /* Load ramdisk */
790 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700791 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800792 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700793 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
794 return -1;
795 }
796 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800797 offset += ramdisk_actual;
798
799 dprintf(INFO, "Loading boot image (%d): done\n",
800 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700801 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700802
803 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800804 offset += second_actual;
805 /* Second image loading not implemented. */
806 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700807 }
808
809 #if DEVICE_TREE
810 if(hdr->dt_size != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700811 /* Read the device tree table into buffer */
812 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
813 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
814 return -1;
815 }
816 table = (struct dt_table*) dt_buf;
817
Joel Kingaa335dc2013-06-03 16:11:08 -0700818 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700819 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
820 return -1;
821 }
822
Joel Kingaa335dc2013-06-03 16:11:08 -0700823 /* Find index of device tree within device tree table */
824 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700825 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
826 return -1;
827 }
828
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700829 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700830 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700831 {
832 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
833 return -1;
834 }
835
Joel Kingaa335dc2013-06-03 16:11:08 -0700836 if(mmc_read(ptn + offset + dt_entry.offset,
837 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700838 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
839 return -1;
840 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300841 #ifdef TZ_SAVE_KERNEL_HASH
842 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
843 hdr->ramdisk_addr, ramdisk_actual,
844 ptn, offset, hdr->dt_size);
845 #endif /* TZ_SAVE_KERNEL_HASH */
846
Channagoud Kadabi35095622013-03-01 13:53:05 -0800847 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800848 /*
849 * If appended dev tree is found, update the atags with
850 * memory address to the DTB appended location on RAM.
851 * Else update with the atags address in the kernel header
852 */
853 void *dtb;
854 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700855 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800856 if (!dtb) {
857 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
858 return -1;
859 }
Neeti Desai465491e2012-07-31 12:53:35 -0700860 }
861 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700862 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700863
864unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700865
Dima Zavin77e41f32013-03-06 16:10:43 -0800866 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700867 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700868 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
869
870 return 0;
871}
872
Dima Zavin214cc642009-01-26 11:16:21 -0800873int boot_linux_from_flash(void)
874{
875 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800876 struct ptentry *ptn;
877 struct ptable *ptable;
878 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800879
Shashank Mittalcd98d472011-08-02 14:29:24 -0700880 unsigned char *image_addr = 0;
881 unsigned kernel_actual;
882 unsigned ramdisk_actual;
883 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800884 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700885
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700886#if DEVICE_TREE
887 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700888 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800889 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700890#endif
891
David Ng183a7422009-12-07 14:55:21 -0800892 if (target_is_emmc_boot()) {
893 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
894 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
895 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
896 return -1;
897 }
898 goto continue_boot;
899 }
900
Dima Zavin214cc642009-01-26 11:16:21 -0800901 ptable = flash_get_ptable();
902 if (ptable == NULL) {
903 dprintf(CRITICAL, "ERROR: Partition table not found\n");
904 return -1;
905 }
906
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800907 if(!boot_into_recovery)
908 {
909 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700910
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800911 if (ptn == NULL) {
912 dprintf(CRITICAL, "ERROR: No boot partition found\n");
913 return -1;
914 }
915 }
916 else
917 {
918 ptn = ptable_find(ptable, "recovery");
919 if (ptn == NULL) {
920 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
921 return -1;
922 }
Dima Zavin214cc642009-01-26 11:16:21 -0800923 }
924
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800925 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800926 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
927 return -1;
928 }
Dima Zavin214cc642009-01-26 11:16:21 -0800929
930 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700931 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800932 return -1;
933 }
934
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800935 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700936 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 -0800937 return -1;
938 }
939
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700940 /*
941 * Update the kernel/ramdisk/tags address if the boot image header
942 * has default values, these default values come from mkbootimg when
943 * the boot image is flashed using fastboot flash:raw
944 */
945 update_ker_tags_rdisk_addr(hdr);
946
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700947 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700948 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
949 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
950 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
951
952 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
953 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
954
955 /* Check if the addresses in the header are valid. */
956 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
957 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
958 {
959 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
960 return -1;
961 }
962
963#ifndef DEVICE_TREE
964 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
965 {
966 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
967 return -1;
968 }
969#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700970
Shashank Mittalcd98d472011-08-02 14:29:24 -0700971 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700972 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700973 {
974 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700975 offset = 0;
976
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800977#if DEVICE_TREE
978 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
979 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700980
981 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
982 {
983 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
984 return -1;
985 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800986#else
Shashank Mittalcd98d472011-08-02 14:29:24 -0700987 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800988#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700989
Amol Jadib6be5c12012-11-14 13:39:51 -0800990 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700991 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800992
Shashank Mittalcd98d472011-08-02 14:29:24 -0700993 /* Read image without signature */
994 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
995 {
996 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
997 return -1;
998 }
Dima Zavin214cc642009-01-26 11:16:21 -0800999
Amol Jadib6be5c12012-11-14 13:39:51 -08001000 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001001 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001002
Shashank Mittalcd98d472011-08-02 14:29:24 -07001003 offset = imagesize_actual;
1004 /* Read signature */
1005 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1006 {
1007 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001008 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001009 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001010
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001011 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001012
1013 /* Move kernel and ramdisk to correct address */
1014 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1015 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001016#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001017 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001018 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001019 {
1020 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1021 return -1;
1022 }
1023
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001024 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1025#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001026
1027 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001028 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001029 {
1030 write_device_info_flash(&device);
1031 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301032#if USE_PCOM_SECBOOT
1033 set_tamper_flag(device.is_tampered);
1034#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001035 }
1036 else
1037 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001038 offset = page_size;
1039
Amol Jadib6be5c12012-11-14 13:39:51 -08001040 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1041 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1042 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1043
1044 dprintf(INFO, "Loading boot image (%d): start\n",
1045 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001046 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001047
1048 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001049 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1050 return -1;
1051 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001052 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001053
Amol Jadib6be5c12012-11-14 13:39:51 -08001054 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001055 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1056 return -1;
1057 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001058 offset += ramdisk_actual;
1059
1060 dprintf(INFO, "Loading boot image (%d): done\n",
1061 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001062 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001063
1064 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001065 offset += second_actual;
1066 /* Second image loading not implemented. */
1067 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001068 }
1069
1070#if DEVICE_TREE
1071 if(hdr->dt_size != 0) {
1072
1073 /* Read the device tree table into buffer */
1074 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1075 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1076 return -1;
1077 }
1078
1079 table = (struct dt_table*) dt_buf;
1080
Joel Kingaa335dc2013-06-03 16:11:08 -07001081 if (dev_tree_validate(table, hdr->page_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001082 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1083 return -1;
1084 }
1085
Joel Kingaa335dc2013-06-03 16:11:08 -07001086 /* Find index of device tree within device tree table */
1087 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001088 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1089 return -1;
1090 }
1091
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001092 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001093 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001094 {
1095 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1096 return -1;
1097 }
1098
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001099 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001100 if(flash_read(ptn, offset + dt_entry.offset,
1101 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001102 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1103 return -1;
1104 }
1105 }
1106#endif
1107
Shashank Mittalcd98d472011-08-02 14:29:24 -07001108 }
David Ng183a7422009-12-07 14:55:21 -08001109continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001110
Dima Zavin214cc642009-01-26 11:16:21 -08001111 /* TODO: create/pass atags to kernel */
1112
Ajay Dudanie28a6072011-07-01 13:59:46 -07001113 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001114 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001115 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1116
1117 return 0;
1118}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001119
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001120BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001121void write_device_info_mmc(device_info *dev)
1122{
1123 struct device_info *info = (void*) info_buf;
1124 unsigned long long ptn = 0;
1125 unsigned long long size;
1126 int index = INVALID_PTN;
1127
1128 index = partition_get_index("aboot");
1129 ptn = partition_get_offset(index);
1130 if(ptn == 0)
1131 {
1132 return;
1133 }
1134
1135 size = partition_get_size(index);
1136
1137 memcpy(info, dev, sizeof(device_info));
1138
1139 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1140 {
1141 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1142 return;
1143 }
1144}
1145
1146void read_device_info_mmc(device_info *dev)
1147{
1148 struct device_info *info = (void*) info_buf;
1149 unsigned long long ptn = 0;
1150 unsigned long long size;
1151 int index = INVALID_PTN;
1152
1153 index = partition_get_index("aboot");
1154 ptn = partition_get_offset(index);
1155 if(ptn == 0)
1156 {
1157 return;
1158 }
1159
1160 size = partition_get_size(index);
1161
1162 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1163 {
1164 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1165 return;
1166 }
1167
1168 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1169 {
1170 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1171 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001172 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001173
1174 write_device_info_mmc(info);
1175 }
1176 memcpy(dev, info, sizeof(device_info));
1177}
1178
1179void write_device_info_flash(device_info *dev)
1180{
1181 struct device_info *info = (void *) info_buf;
1182 struct ptentry *ptn;
1183 struct ptable *ptable;
1184
1185 ptable = flash_get_ptable();
1186 if (ptable == NULL)
1187 {
1188 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1189 return;
1190 }
1191
1192 ptn = ptable_find(ptable, "devinfo");
1193 if (ptn == NULL)
1194 {
1195 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1196 return;
1197 }
1198
1199 memcpy(info, dev, sizeof(device_info));
1200
1201 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1202 {
1203 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1204 return;
1205 }
1206}
1207
1208void read_device_info_flash(device_info *dev)
1209{
1210 struct device_info *info = (void*) info_buf;
1211 struct ptentry *ptn;
1212 struct ptable *ptable;
1213
1214 ptable = flash_get_ptable();
1215 if (ptable == NULL)
1216 {
1217 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1218 return;
1219 }
1220
1221 ptn = ptable_find(ptable, "devinfo");
1222 if (ptn == NULL)
1223 {
1224 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1225 return;
1226 }
1227
1228 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1229 {
1230 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1231 return;
1232 }
1233
1234 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1235 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001236 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1237 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001238 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001239 write_device_info_flash(info);
1240 }
1241 memcpy(dev, info, sizeof(device_info));
1242}
1243
1244void write_device_info(device_info *dev)
1245{
1246 if(target_is_emmc_boot())
1247 {
1248 write_device_info_mmc(dev);
1249 }
1250 else
1251 {
1252 write_device_info_flash(dev);
1253 }
1254}
1255
1256void read_device_info(device_info *dev)
1257{
1258 if(target_is_emmc_boot())
1259 {
1260 read_device_info_mmc(dev);
1261 }
1262 else
1263 {
1264 read_device_info_flash(dev);
1265 }
1266}
1267
1268void reset_device_info()
1269{
1270 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001271 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001272 write_device_info(&device);
1273}
1274
1275void set_device_root()
1276{
1277 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001278 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001279 write_device_info(&device);
1280}
1281
Amol Jadicb524072012-08-09 16:40:18 -07001282#if DEVICE_TREE
1283int copy_dtb(uint8_t *boot_image_start)
1284{
1285 uint32 dt_image_offset = 0;
1286 uint32_t n;
1287 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001288 struct dt_entry dt_entry;
Amol Jadicb524072012-08-09 16:40:18 -07001289
1290 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1291
Amol Jadicb524072012-08-09 16:40:18 -07001292 if(hdr->dt_size != 0) {
1293
1294 /* add kernel offset */
1295 dt_image_offset += page_size;
1296 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1297 dt_image_offset += n;
1298
1299 /* add ramdisk offset */
1300 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1301 dt_image_offset += n;
1302
1303 /* add second offset */
1304 if(hdr->second_size != 0) {
1305 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1306 dt_image_offset += n;
1307 }
1308
1309 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001310 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001311
Joel Kingaa335dc2013-06-03 16:11:08 -07001312 if (dev_tree_validate(table, hdr->page_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001313 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1314 return -1;
1315 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001316 /* Find index of device tree within device tree table */
1317 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001318 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1319 return -1;
1320 }
1321
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001322 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001323 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001324 {
1325 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1326 return -1;
1327 }
1328
Amol Jadicb524072012-08-09 16:40:18 -07001329 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001330 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001331 boot_image_start + dt_image_offset + dt_entry.offset,
1332 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001333 } else
1334 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001335
1336 /* Everything looks fine. Return success. */
1337 return 0;
1338}
1339#endif
1340
Brian Swetland9c4c0752009-01-25 16:23:50 -08001341void cmd_boot(const char *arg, void *data, unsigned sz)
1342{
1343 unsigned kernel_actual;
1344 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001345 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001346 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001347 int ret = 0;
1348 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001349
1350 if (sz < sizeof(hdr)) {
1351 fastboot_fail("invalid bootimage header");
1352 return;
1353 }
1354
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001355 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001356
1357 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001358 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001359
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001360 if(target_is_emmc_boot() && hdr->page_size) {
1361 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001362 page_mask = page_size - 1;
1363 }
1364
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001365 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1366 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1367
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001368 /*
1369 * Update the kernel/ramdisk/tags address if the boot image header
1370 * has default values, these default values come from mkbootimg when
1371 * the boot image is flashed using fastboot flash:raw
1372 */
1373 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001374
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001375 /* Get virtual addresses since the hdr saves physical addresses. */
1376 hdr->kernel_addr = VA(hdr->kernel_addr);
1377 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1378 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001379
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001380 /* Check if the addresses in the header are valid. */
1381 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1382 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1383 {
1384 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1385 return -1;
1386 }
1387
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001388 /* sz should have atleast raw boot image */
1389 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001390 fastboot_fail("incomplete bootimage");
1391 return;
1392 }
1393
Amol Jadicb524072012-08-09 16:40:18 -07001394#if DEVICE_TREE
1395 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001396 ret = copy_dtb(data);
1397
1398 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001399#else
1400 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1401 {
1402 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1403 return -1;
1404 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001405#endif
1406
1407 /* Load ramdisk & kernel */
1408 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1409 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1410
1411#if DEVICE_TREE
1412 /*
1413 * If dtb is not found look for appended DTB in the kernel.
1414 * If appended dev tree is found, update the atags with
1415 * memory address to the DTB appended location on RAM.
1416 * Else update with the atags address in the kernel header
1417 */
1418 if (!dtb_copied) {
1419 void *dtb;
Channagoud Kadabia83a6972013-04-25 15:26:04 -07001420 dtb = dev_tree_appended((void *)hdr->kernel_addr, (void *)hdr->tags_addr, hdr->kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001421 if (!dtb) {
1422 fastboot_fail("dtb not found");
1423 return;
1424 }
Amol Jadicb524072012-08-09 16:40:18 -07001425 }
1426#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001427
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001428#ifndef DEVICE_TREE
1429 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1430 {
1431 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1432 return -1;
1433 }
1434#endif
1435
Brian Swetland9c4c0752009-01-25 16:23:50 -08001436 fastboot_okay("");
1437 udc_stop();
1438
Dima Zavin77e41f32013-03-06 16:10:43 -08001439 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001440 (const char*) hdr->cmdline, board_machtype(),
1441 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001442}
1443
Dima Zavin214cc642009-01-26 11:16:21 -08001444void cmd_erase(const char *arg, void *data, unsigned sz)
1445{
1446 struct ptentry *ptn;
1447 struct ptable *ptable;
1448
1449 ptable = flash_get_ptable();
1450 if (ptable == NULL) {
1451 fastboot_fail("partition table doesn't exist");
1452 return;
1453 }
1454
1455 ptn = ptable_find(ptable, arg);
1456 if (ptn == NULL) {
1457 fastboot_fail("unknown partition name");
1458 return;
1459 }
1460
1461 if (flash_erase(ptn)) {
1462 fastboot_fail("failed to erase partition");
1463 return;
1464 }
1465 fastboot_okay("");
1466}
1467
Bikas Gurungd48bd242010-09-04 19:54:32 -07001468
1469void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1470{
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001471 BUF_DMA_ALIGN(out, 512);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001472 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001473 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001474
Kinson Chikf1a43512011-07-14 11:28:39 -07001475 index = partition_get_index(arg);
1476 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001477
Kinson Chikf1a43512011-07-14 11:28:39 -07001478 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001479 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001480 return;
1481 }
neetidb4b24d62012-01-20 12:13:09 -08001482 /* Simple inefficient version of erase. Just writing
1483 0 in first block */
1484 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1485 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001486 return;
1487 }
1488 fastboot_okay("");
1489}
1490
1491
Ajay Dudani5c761132011-04-07 20:19:04 -07001492void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001493{
1494 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001495 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001496 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001497
Greg Grisco6e754772011-06-23 12:19:39 -07001498 if (!strcmp(arg, "partition"))
1499 {
1500 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001501 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001502 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001503 return;
1504 }
1505 }
Greg Grisco6e754772011-06-23 12:19:39 -07001506 else
1507 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001508 index = partition_get_index(arg);
1509 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001510 if(ptn == 0) {
1511 fastboot_fail("partition table doesn't exist");
1512 return;
1513 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001514
Greg Grisco6e754772011-06-23 12:19:39 -07001515 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1516 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1517 fastboot_fail("image is not a boot image");
1518 return;
1519 }
1520 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001521
Kinson Chikf1a43512011-07-14 11:28:39 -07001522 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001523 if (ROUND_TO_PAGE(sz,511) > size) {
1524 fastboot_fail("size too large");
1525 return;
1526 }
1527 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1528 fastboot_fail("flash write failure");
1529 return;
1530 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001531 }
1532 fastboot_okay("");
1533 return;
1534}
1535
Ajay Dudani5c761132011-04-07 20:19:04 -07001536void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1537{
1538 unsigned int chunk;
1539 unsigned int chunk_data_sz;
1540 sparse_header_t *sparse_header;
1541 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001542 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001543 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301544 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001545 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001546
Kinson Chikf1a43512011-07-14 11:28:39 -07001547 index = partition_get_index(arg);
1548 ptn = partition_get_offset(index);
1549 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001550 fastboot_fail("partition table doesn't exist");
1551 return;
1552 }
1553
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301554 size = partition_get_size(index);
1555 if (ROUND_TO_PAGE(sz,511) > size) {
1556 fastboot_fail("size too large");
1557 return;
1558 }
1559
Ajay Dudani5c761132011-04-07 20:19:04 -07001560 /* Read and skip over sparse image header */
1561 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001562 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1563 fastboot_fail("size too large");
1564 return;
1565 }
1566
Ajay Dudani5c761132011-04-07 20:19:04 -07001567 data += sparse_header->file_hdr_sz;
1568 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1569 {
1570 /* Skip the remaining bytes in a header that is longer than
1571 * we expected.
1572 */
1573 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1574 }
1575
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001576 dprintf (SPEW, "=== Sparse Image Header ===\n");
1577 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1578 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1579 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1580 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1581 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1582 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1583 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1584 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001585
1586 /* Start processing chunks */
1587 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1588 {
1589 /* Read and skip over chunk header */
1590 chunk_header = (chunk_header_t *) data;
1591 data += sizeof(chunk_header_t);
1592
1593 dprintf (SPEW, "=== Chunk Header ===\n");
1594 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1595 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1596 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1597
1598 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1599 {
1600 /* Skip the remaining bytes in a header that is longer than
1601 * we expected.
1602 */
1603 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1604 }
1605
1606 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1607 switch (chunk_header->chunk_type)
1608 {
1609 case CHUNK_TYPE_RAW:
1610 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1611 chunk_data_sz))
1612 {
1613 fastboot_fail("Bogus chunk size for chunk type Raw");
1614 return;
1615 }
1616
Ajay Dudaniab18f022011-05-12 14:39:22 -07001617 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1618 chunk_data_sz,
1619 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001620 {
1621 fastboot_fail("flash write failure");
1622 return;
1623 }
1624 total_blocks += chunk_header->chunk_sz;
1625 data += chunk_data_sz;
1626 break;
1627
1628 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001629 total_blocks += chunk_header->chunk_sz;
1630 break;
1631
Ajay Dudani5c761132011-04-07 20:19:04 -07001632 case CHUNK_TYPE_CRC:
1633 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1634 {
1635 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1636 return;
1637 }
1638 total_blocks += chunk_header->chunk_sz;
1639 data += chunk_data_sz;
1640 break;
1641
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001642 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001643 fastboot_fail("Unknown chunk type");
1644 return;
1645 }
1646 }
1647
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001648 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1649 total_blocks, sparse_header->total_blks);
1650
1651 if(total_blocks != sparse_header->total_blks)
1652 {
1653 fastboot_fail("sparse image write failure");
1654 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001655
1656 fastboot_okay("");
1657 return;
1658}
1659
1660void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1661{
1662 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001663 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1664 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001665
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001666#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001667 int ret=0;
1668 uint32 major_version=0;
1669 uint32 minor_version=0;
1670
1671 ret = scm_svc_version(&major_version,&minor_version);
1672 if(!ret)
1673 {
1674 if(major_version >= 2)
1675 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001676 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001677 {
1678 ret = encrypt_scm((uint32 **) &data, &sz);
1679 if (ret != 0) {
1680 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1681 return;
1682 }
1683
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001684 /* Protect only for SSD */
1685 if (!strcmp(arg, "ssd")) {
1686 ret = scm_protect_keystore((uint32 *) data, sz);
1687 if (ret != 0) {
1688 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1689 return;
1690 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001691 }
1692 }
1693 else
1694 {
1695 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1696 if(ret != 0)
1697 {
1698 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1699 return;
1700 }
1701 }
1702 }
1703 else
1704 {
1705 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1706 magic_number[1] == DECRYPT_MAGIC_1)
1707 {
1708 ret = decrypt_scm((uint32 **) &data, &sz);
1709 if (ret != 0) {
1710 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1711 return;
1712 }
1713 }
1714 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1715 magic_number[1] == ENCRYPT_MAGIC_1)
1716 {
1717 ret = encrypt_scm((uint32 **) &data, &sz);
1718 if (ret != 0) {
1719 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1720 return;
1721 }
1722 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001723 }
1724 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001725 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001726 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001727 dprintf(CRITICAL,"INVALID SVC Version\n");
1728 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001729 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001730#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001731
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001732 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001733 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1734 cmd_flash_mmc_img(arg, data, sz);
1735 else
1736 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001737 return;
1738}
1739
Dima Zavin214cc642009-01-26 11:16:21 -08001740void cmd_flash(const char *arg, void *data, unsigned sz)
1741{
1742 struct ptentry *ptn;
1743 struct ptable *ptable;
1744 unsigned extra = 0;
1745
1746 ptable = flash_get_ptable();
1747 if (ptable == NULL) {
1748 fastboot_fail("partition table doesn't exist");
1749 return;
1750 }
1751
1752 ptn = ptable_find(ptable, arg);
1753 if (ptn == NULL) {
1754 fastboot_fail("unknown partition name");
1755 return;
1756 }
1757
1758 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1759 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1760 fastboot_fail("image is not a boot image");
1761 return;
1762 }
1763 }
1764
Amol Jadi5c61a952012-05-04 17:05:35 -07001765 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001766 || !strcmp(ptn->name, "userdata")
1767 || !strcmp(ptn->name, "persist")
1768 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301769 if (flash_ecc_bch_enabled())
1770 /* Spare data bytes for 8 bit ECC increased by 4 */
1771 extra = ((page_size >> 9) * 20);
1772 else
1773 extra = ((page_size >> 9) * 16);
1774 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001775 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001776
1777 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1778 if (flash_write(ptn, extra, data, sz)) {
1779 fastboot_fail("flash write failure");
1780 return;
1781 }
1782 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1783 fastboot_okay("");
1784}
1785
1786void cmd_continue(const char *arg, void *data, unsigned sz)
1787{
1788 fastboot_okay("");
1789 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001790 if (target_is_emmc_boot())
1791 {
1792 boot_linux_from_mmc();
1793 }
1794 else
1795 {
1796 boot_linux_from_flash();
1797 }
Dima Zavin214cc642009-01-26 11:16:21 -08001798}
1799
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001800void cmd_reboot(const char *arg, void *data, unsigned sz)
1801{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001802 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001803 fastboot_okay("");
1804 reboot_device(0);
1805}
1806
1807void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1808{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001809 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001810 fastboot_okay("");
1811 reboot_device(FASTBOOT_MODE);
1812}
1813
Shashank Mittal162244e2011-08-08 19:01:25 -07001814void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1815{
1816 if(!device.is_unlocked)
1817 {
1818 device.is_unlocked = 1;
1819 write_device_info(&device);
1820 }
1821 fastboot_okay("");
1822}
1823
Shashank Mittala0032282011-08-26 14:50:11 -07001824void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1825{
1826 char response[64];
1827 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1828 fastboot_info(response);
1829 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1830 fastboot_info(response);
1831 fastboot_okay("");
1832}
1833
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001834void cmd_preflash(const char *arg, void *data, unsigned sz)
1835{
1836 fastboot_okay("");
1837}
1838
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001839void splash_screen ()
1840{
1841 struct ptentry *ptn;
1842 struct ptable *ptable;
1843 struct fbcon_config *fb_display = NULL;
1844
1845 if (!target_is_emmc_boot())
1846 {
1847 ptable = flash_get_ptable();
1848 if (ptable == NULL) {
1849 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001850 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001851 }
1852
1853 ptn = ptable_find(ptable, "splash");
1854 if (ptn == NULL) {
1855 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1856 } else {
1857 fb_display = fbcon_display();
1858 if (fb_display) {
1859 if (flash_read(ptn, 0, fb_display->base,
1860 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1861 fbcon_clear();
1862 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1863 }
1864 }
1865 }
1866 }
1867}
1868
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001869/* Get the size from partiton name */
1870static void get_partition_size(const char *arg, char *response)
1871{
1872 uint64_t ptn = 0;
1873 uint64_t size;
1874 int index = INVALID_PTN;
1875
1876 index = partition_get_index(arg);
1877
1878 if (index == INVALID_PTN)
1879 {
1880 dprintf(CRITICAL, "Invalid partition index\n");
1881 return;
1882 }
1883
1884 ptn = partition_get_offset(index);
1885
1886 if(!ptn)
1887 {
1888 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1889 return;
1890 }
1891
1892 size = partition_get_size(index);
1893
1894 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1895 return;
1896}
1897
1898/*
1899 * Publish the partition type & size info
1900 * fastboot getvar will publish the required information.
1901 * fastboot getvar partition_size:<partition_name>: partition size in hex
1902 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1903 */
1904static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1905{
1906 uint8_t i;
1907
1908 for (i = 0; i < num_parts; i++) {
1909 get_partition_size(info[i].part_name, info[i].size_response);
1910
1911 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1912 {
1913 dprintf(CRITICAL, "partition size name truncated\n");
1914 return;
1915 }
1916 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1917 {
1918 dprintf(CRITICAL, "partition type name truncated\n");
1919 return;
1920 }
1921
1922 /* publish partition size & type info */
1923 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1924 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1925 }
1926}
1927
Brian Swetland9c4c0752009-01-25 16:23:50 -08001928void aboot_init(const struct app_descriptor *app)
1929{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001930 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001931 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001932 unsigned sz = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03001933 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001934
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001935 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001936 if (target_is_emmc_boot())
1937 {
1938 page_size = 2048;
1939 page_mask = page_size - 1;
1940 }
1941 else
1942 {
1943 page_size = flash_page_size();
1944 page_mask = page_size - 1;
1945 }
1946
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001947 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
1948
Shashank Mittal162244e2011-08-08 19:01:25 -07001949 if(target_use_signed_kernel())
1950 {
1951 read_device_info(&device);
1952
Shashank Mittal162244e2011-08-08 19:01:25 -07001953 }
1954
Greg Griscod6250552011-06-29 14:40:23 -07001955 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001956 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001957 surf_udc_device.serialno = sn_buf;
1958
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001959 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001960 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001961 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001962 dprintf(ALWAYS,"dload mode key sequence detected\n");
1963 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001964 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001965 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001966 }
1967 else
1968 {
1969 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001970 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001971 }
1972 boot_into_fastboot = true;
1973 }
1974 if (!boot_into_fastboot)
1975 {
1976 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
1977 boot_into_recovery = 1;
1978 if (!boot_into_recovery &&
1979 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03001980 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001981 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001982 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001983 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03001984 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001985 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001986
Ajay Dudani77421292010-10-27 19:34:06 -07001987 reboot_mode = check_reboot_mode();
1988 if (reboot_mode == RECOVERY_MODE) {
1989 boot_into_recovery = 1;
1990 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03001991 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07001992 }
1993
Pavel Nedev5d91d412013-04-29 11:34:24 +03001994 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001995 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03001996 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07001997 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03001998 if(emmc_recovery_init())
1999 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2000 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002001 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002002 if((device.is_unlocked) || (device.is_tampered))
2003 {
2004 #ifdef TZ_TAMPER_FUSE
2005 set_tamper_fuse_cmd();
2006 #endif
2007 #if USE_PCOM_SECBOOT
2008 set_tamper_flag(device.is_tampered);
2009 #endif
2010 }
Shashank Mittala0032282011-08-26 14:50:11 -07002011 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002012 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002013 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002014 else
2015 {
2016 recovery_init();
2017 #if USE_PCOM_SECBOOT
2018 if((device.is_unlocked) || (device.is_tampered))
2019 set_tamper_flag(device.is_tampered);
2020 #endif
2021 boot_linux_from_flash();
2022 }
2023 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2024 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002025 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002026
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002027 sz = target_get_max_flash_size();
2028
Shashank Mittal162244e2011-08-08 19:01:25 -07002029 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002030
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002031 if(!usb_init)
2032 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002033
2034 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002035
Shashank Mittal23b8f422010-04-16 19:27:21 -07002036 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002037 {
2038 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002039 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002040 }
2041 else
2042 {
2043 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002044 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002045 }
2046
2047 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002048 fastboot_register("reboot", cmd_reboot);
2049 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07002050 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07002051 fastboot_register("oem device-info", cmd_oem_devinfo);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002052 fastboot_register("preflash", cmd_preflash);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07002053 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08002054 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08002055 fastboot_publish("serialno", sn_buf);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002056 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2057 /* Max download size supported */
2058 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", sz);
2059 fastboot_publish("max-download-size", (const char *) max_download_size);
Kinson Chikf1a43512011-07-14 11:28:39 -07002060 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07002061 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002062 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002063}
2064
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002065uint32_t get_page_size()
2066{
2067 return page_size;
2068}
2069
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002070/*
2071 * Calculated and save hash (SHA256) for non-signed boot image.
2072 *
2073 * Hash the same data that is checked on the signed boot image.
2074 * Kernel and Ramdisk are already read to memory buffers.
2075 * Need to read the entire device-tree from mmc
2076 * since non-signed image only read the DT tags of the relevant platform.
2077 *
2078 * @param kernel_addr - kernel bufer
2079 * @param kernel_actual - kernel size in bytes
2080 * @param ramdisk_addr - ramdisk buffer
2081 * @param ramdisk_actual - ramdisk size
2082 * @param ptn - partition
2083 * @param dt_offset - device tree offset on mmc partition
2084 * @param dt_size
2085 *
2086 * @return int - 0 on success, negative value on failure.
2087 */
2088int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2089 void *ramdisk_addr, unsigned ramdisk_actual,
2090 unsigned long long ptn,
2091 unsigned dt_offset, unsigned dt_size)
2092{
2093 SHA256_CTX sha256_ctx;
2094 char digest[32]={0};
2095 char *buf = (char *)target_get_scratch_address();
2096 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2097 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2098
2099 SHA256_Init(&sha256_ctx);
2100
2101 /* Read Boot Header */
2102 if (mmc_read(ptn, buf, page_size))
2103 {
2104 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2105 return -1;
2106 }
2107 /* Read entire Device Tree */
2108 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2109 {
2110 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2111 return -1;
2112 }
2113 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2114 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2115 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2116 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2117
2118 SHA256_Final(digest, &sha256_ctx);
2119
2120 save_kernel_hash_cmd(digest);
2121 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2122
2123 return 0;
2124}
2125
Brian Swetland9c4c0752009-01-25 16:23:50 -08002126APP_START(aboot)
2127 .init = aboot_init,
2128APP_END