blob: f88814c9f23af2101b9bda4ce8c20d8d8e57da45 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Amol Jadi2a15a272013-01-22 12:03:36 -08005 * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
36#include <dev/udc.h>
37#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070038#include <stdlib.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
43#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080044#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080045#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080046#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070047#include <target.h>
48#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070049#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070050#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070051#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070052#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070053#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030054#include <sha.h>
Dima Zavin214cc642009-01-26 11:16:21 -080055
Neeti Desai17379b82012-06-04 18:42:53 -070056#if DEVICE_TREE
57#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070058#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070059#endif
60
Shashank Mittalcd98d472011-08-02 14:29:24 -070061#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080062#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080063#include "bootimg.h"
64#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070065#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070066#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070067#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070068#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080069
Shashank Mittal162244e2011-08-08 19:01:25 -070070#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070071
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070072extern bool target_use_signed_kernel(void);
73extern void dsb();
74extern void isb();
75extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070076extern void target_uninit(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070077
78void write_device_info_mmc(device_info *dev);
79void write_device_info_flash(device_info *dev);
80
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070081#define EXPAND(NAME) #NAME
82#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070083
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080084#ifdef MEMBASE
85#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
86#else
David Ng183a7422009-12-07 14:55:21 -080087#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080088#endif
89
Deepa Dinamani0e163a42013-05-24 17:08:15 -070090#ifndef MEMSIZE
91#define MEMSIZE 1024*1024
92#endif
93
94#define MAX_TAGS_SIZE 1024
95
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080096#define RECOVERY_MODE 0x77665502
97#define FASTBOOT_MODE 0x77665500
98
David Ng183a7422009-12-07 14:55:21 -080099static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800100static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300101static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800102static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800103static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700104static const char *auth_kernel = " androidboot.authorized_kernel=true";
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
Brian Swetland9c4c0752009-01-25 16:23:50 -0800126static struct udc_device surf_udc_device = {
127 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -0800128 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800129 .version_id = 0x0100,
130 .manufacturer = "Google",
131 .product = "Android",
132};
133
Dima Zavin42168f22009-01-30 11:52:22 -0800134struct atag_ptbl_entry
135{
136 char name[16];
137 unsigned offset;
138 unsigned size;
139 unsigned flags;
140};
141
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700142/*
143 * Partition info, required to be published
144 * for fastboot
145 */
146struct getvar_partition_info {
147 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
148 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
149 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
150 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
151 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
152};
153
154/*
155 * Right now, we are publishing the info for only
156 * three partitions
157 */
158struct getvar_partition_info part_info[] =
159{
160 { "system" , "partition-size:", "partition-type:", "", "ext4" },
161 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
162 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
163};
164
165char max_download_size[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800166char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700167
Greg Griscod2471ef2011-07-14 13:00:42 -0700168extern int emmc_recovery_init(void);
169
Kinson Chik0b1c8162011-08-31 16:31:57 -0700170#if NO_KEYPAD_DRIVER
171extern int fastboot_trigger(void);
172#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700173
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700174static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
175{
176 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700177#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
178 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
179 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
180 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700181#endif
182}
183
Dima Zavin42168f22009-01-30 11:52:22 -0800184static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
185{
186 struct atag_ptbl_entry atag_ptn;
187
188 memcpy(atag_ptn.name, ptn->name, 16);
189 atag_ptn.name[15] = '\0';
190 atag_ptn.offset = ptn->start;
191 atag_ptn.size = ptn->length;
192 atag_ptn.flags = ptn->flags;
193 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
194 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
195}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800196
Neeti Desaie245d492012-06-01 12:52:13 -0700197unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800198{
David Ng183a7422009-12-07 14:55:21 -0800199 int cmdline_len = 0;
200 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800201 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700202 int pause_at_bootup = 0;
Dima Zavin42168f22009-01-30 11:52:22 -0800203
Brian Swetland9c4c0752009-01-25 16:23:50 -0800204 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800205 cmdline_len = strlen(cmdline);
206 have_cmdline = 1;
207 }
208 if (target_is_emmc_boot()) {
209 cmdline_len += strlen(emmc_cmdline);
210 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800211
212 cmdline_len += strlen(usb_sn_cmdline);
213 cmdline_len += strlen(sn_buf);
214
Pavel Nedev328ac822013-04-05 15:25:11 +0300215 if (boot_into_ffbm) {
216 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700217 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800218 /* reduce kernel console messages to speed-up boot */
219 cmdline_len += strlen(loglevel);
Pavel Nedev328ac822013-04-05 15:25:11 +0300220 } else if (target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700221 pause_at_bootup = 1;
222 cmdline_len += strlen(battchg_pause);
223 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800224
Shashank Mittalcd98d472011-08-02 14:29:24 -0700225 if(target_use_signed_kernel() && auth_kernel_img) {
226 cmdline_len += strlen(auth_kernel);
227 }
228
Ajay Dudanid04110c2011-01-17 23:55:07 -0800229 /* Determine correct androidboot.baseband to use */
230 switch(target_baseband())
231 {
232 case BASEBAND_APQ:
233 cmdline_len += strlen(baseband_apq);
234 break;
235
236 case BASEBAND_MSM:
237 cmdline_len += strlen(baseband_msm);
238 break;
239
240 case BASEBAND_CSFB:
241 cmdline_len += strlen(baseband_csfb);
242 break;
243
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800244 case BASEBAND_SVLTE2A:
245 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800246 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700247
248 case BASEBAND_MDM:
249 cmdline_len += strlen(baseband_mdm);
250 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700251
252 case BASEBAND_SGLTE:
253 cmdline_len += strlen(baseband_sglte);
254 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530255
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800256 case BASEBAND_SGLTE2:
257 cmdline_len += strlen(baseband_sglte2);
258 break;
259
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530260 case BASEBAND_DSDA:
261 cmdline_len += strlen(baseband_dsda);
262 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800263
264 case BASEBAND_DSDA2:
265 cmdline_len += strlen(baseband_dsda2);
266 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800267 }
268
David Ng183a7422009-12-07 14:55:21 -0800269 if (cmdline_len > 0) {
270 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700271 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
272 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700273
Amol Jadi168b7712012-03-06 16:15:00 -0800274 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700275 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800276 if (have_cmdline) {
277 src = cmdline;
278 while ((*dst++ = *src++));
279 }
280 if (target_is_emmc_boot()) {
281 src = emmc_cmdline;
282 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700283 have_cmdline = 1;
284 while ((*dst++ = *src++));
285 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800286
287 src = usb_sn_cmdline;
288 if (have_cmdline) --dst;
289 have_cmdline = 1;
290 while ((*dst++ = *src++));
291 src = sn_buf;
292 if (have_cmdline) --dst;
293 have_cmdline = 1;
294 while ((*dst++ = *src++));
295
Pavel Nedev328ac822013-04-05 15:25:11 +0300296 if (boot_into_ffbm) {
297 src = androidboot_mode;
298 if (have_cmdline) --dst;
299 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700300 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300301 if (have_cmdline) --dst;
302 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800303 src = loglevel;
304 if (have_cmdline) --dst;
305 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300306 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700307 src = battchg_pause;
308 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800309 while ((*dst++ = *src++));
310 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800311
Shashank Mittalcd98d472011-08-02 14:29:24 -0700312 if(target_use_signed_kernel() && auth_kernel_img) {
313 src = auth_kernel;
314 if (have_cmdline) --dst;
315 while ((*dst++ = *src++));
316 }
317
Ajay Dudanid04110c2011-01-17 23:55:07 -0800318 switch(target_baseband())
319 {
320 case BASEBAND_APQ:
321 src = baseband_apq;
322 if (have_cmdline) --dst;
323 while ((*dst++ = *src++));
324 break;
325
326 case BASEBAND_MSM:
327 src = baseband_msm;
328 if (have_cmdline) --dst;
329 while ((*dst++ = *src++));
330 break;
331
332 case BASEBAND_CSFB:
333 src = baseband_csfb;
334 if (have_cmdline) --dst;
335 while ((*dst++ = *src++));
336 break;
337
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800338 case BASEBAND_SVLTE2A:
339 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800340 if (have_cmdline) --dst;
341 while ((*dst++ = *src++));
342 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700343
344 case BASEBAND_MDM:
345 src = baseband_mdm;
346 if (have_cmdline) --dst;
347 while ((*dst++ = *src++));
348 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700349
350 case BASEBAND_SGLTE:
351 src = baseband_sglte;
352 if (have_cmdline) --dst;
353 while ((*dst++ = *src++));
354 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530355
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800356 case BASEBAND_SGLTE2:
357 src = baseband_sglte2;
358 if (have_cmdline) --dst;
359 while ((*dst++ = *src++));
360 break;
361
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530362 case BASEBAND_DSDA:
363 src = baseband_dsda;
364 if (have_cmdline) --dst;
365 while ((*dst++ = *src++));
366 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800367
368 case BASEBAND_DSDA2:
369 src = baseband_dsda2;
370 if (have_cmdline) --dst;
371 while ((*dst++ = *src++));
372 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800373 }
Neeti Desaie245d492012-06-01 12:52:13 -0700374 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800375 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700376 return cmdline_final;
377}
378
379unsigned *atag_core(unsigned *ptr)
380{
381 /* CORE */
382 *ptr++ = 2;
383 *ptr++ = 0x54410001;
384
385 return ptr;
386
387}
388
389unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
390 unsigned ramdisk_size)
391{
392 if (ramdisk_size) {
393 *ptr++ = 4;
394 *ptr++ = 0x54420005;
395 *ptr++ = (unsigned)ramdisk;
396 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800397 }
398
Neeti Desaie245d492012-06-01 12:52:13 -0700399 return ptr;
400}
401
402unsigned *atag_ptable(unsigned **ptr_addr)
403{
404 int i;
405 struct ptable *ptable;
406
407 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700408 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
409 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700410 *(*ptr_addr)++ = 0x4d534d70;
411 for (i = 0; i < ptable->count; ++i)
412 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
413 }
414
415 return (*ptr_addr);
416}
417
418unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
419{
420 int cmdline_length = 0;
421 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700422 char *dest;
423
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800424 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700425 n = (cmdline_length + 4) & (~3);
426
427 *ptr++ = (n / 4) + 2;
428 *ptr++ = 0x54410009;
429 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800430 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700431 ptr += (n / 4);
432
433 return ptr;
434}
435
436unsigned *atag_end(unsigned *ptr)
437{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800438 /* END */
439 *ptr++ = 0;
440 *ptr++ = 0;
441
Neeti Desaie245d492012-06-01 12:52:13 -0700442 return ptr;
443}
444
445void generate_atags(unsigned *ptr, const char *cmdline,
446 void *ramdisk, unsigned ramdisk_size)
447{
448
449 ptr = atag_core(ptr);
450 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
451 ptr = target_atag_mem(ptr);
452
453 /* Skip NAND partition ATAGS for eMMC boot */
454 if (!target_is_emmc_boot()){
455 ptr = atag_ptable(&ptr);
456 }
457
458 ptr = atag_cmdline(ptr, cmdline);
459 ptr = atag_end(ptr);
460}
461
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700462typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700463void boot_linux(void *kernel, unsigned *tags,
464 const char *cmdline, unsigned machtype,
465 void *ramdisk, unsigned ramdisk_size)
466{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800467 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800468#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700469 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800470#endif
471
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700472 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800473 uint32_t tags_phys = PA((addr_t)tags);
474
475 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700476
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800477 final_cmdline = update_cmdline((const char*)cmdline);
478
Neeti Desai17379b82012-06-04 18:42:53 -0700479#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800480 dprintf(INFO, "Updating device tree: start\n");
481
Neeti Desai17379b82012-06-04 18:42:53 -0700482 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800483 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700484 if(ret)
485 {
486 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
487 ASSERT(0);
488 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800489 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700490#else
Neeti Desaie245d492012-06-01 12:52:13 -0700491 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800492 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700493#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700494
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700495 /* Perform target specific cleanup */
496 target_uninit();
497
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800498 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
499 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800500
501 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700502
Amol Jadi4421e652011-06-16 15:00:48 -0700503 /* do any platform specific cleanup before kernel entry */
504 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700505
Brian Swetland9c4c0752009-01-25 16:23:50 -0800506 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700507
Amol Jadi504f9fe2012-08-16 13:56:48 -0700508#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800509 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700510#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700511 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700512 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800513}
514
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700515/* Function to check if the memory address range falls within the aboot
516 * boundaries.
517 * start: Start of the memory region
518 * size: Size of the memory region
519 */
520int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
521{
522 /* Check for boundary conditions. */
523 if ((start + size) < start)
524 return -1;
525
526 /* Check for memory overlap. */
527 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
528 return 0;
529 else if (start > (MEMBASE + MEMSIZE))
530 return 0;
531 else
532 return -1;
533}
534
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800535#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800536
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700537BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800538#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700539BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800540#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800541
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700542static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
543{
544 int ret;
545
546 /* Assume device is rooted at this time. */
547 device.is_tampered = 1;
548
549 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
550
551 ret = image_verify((unsigned char *)bootimg_addr,
552 (unsigned char *)(bootimg_addr + bootimg_size),
553 bootimg_size,
554 CRYPTO_AUTH_ALG_SHA256);
555
556 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
557
558 if (ret)
559 {
560 /* Authorized kernel */
561 device.is_tampered = 0;
562 }
563
564#if USE_PCOM_SECBOOT
565 set_tamper_flag(device.is_tampered);
566#endif
567
568 if(device.is_tampered)
569 {
570 write_device_info_mmc(&device);
571 #ifdef TZ_TAMPER_FUSE
572 set_tamper_fuse_cmd();
573 #endif
574 #ifdef ASSERT_ON_TAMPER
575 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
576 ASSERT(0);
577 #endif
578 }
579}
580
Shashank Mittal23b8f422010-04-16 19:27:21 -0700581int boot_linux_from_mmc(void)
582{
583 struct boot_img_hdr *hdr = (void*) buf;
584 struct boot_img_hdr *uhdr;
585 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700586 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700587 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700588 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700589
Shashank Mittalcd98d472011-08-02 14:29:24 -0700590 unsigned char *image_addr = 0;
591 unsigned kernel_actual;
592 unsigned ramdisk_actual;
593 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700594 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700595
596#if DEVICE_TREE
597 struct dt_table *table;
598 struct dt_entry *dt_entry_ptr;
599 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800600 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700601#endif
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700602 if (!boot_into_recovery) {
603 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
604 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
605 if (rcode <= 0) {
606 boot_into_ffbm = false;
607 if (rcode < 0)
608 dprintf(CRITICAL,"failed to get ffbm cookie");
609 } else
610 boot_into_ffbm = true;
611 } else
612 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700613 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
614 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
615 dprintf(INFO, "Unified boot method!\n");
616 hdr = uhdr;
617 goto unified_boot;
618 }
Greg Griscod6250552011-06-29 14:40:23 -0700619 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700620 index = partition_get_index("boot");
621 ptn = partition_get_offset(index);
622 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700623 dprintf(CRITICAL, "ERROR: No boot partition found\n");
624 return -1;
625 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700626 }
627 else {
628 index = partition_get_index("recovery");
629 ptn = partition_get_offset(index);
630 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700631 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
632 return -1;
633 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700634 }
635
Greg Griscod6250552011-06-29 14:40:23 -0700636 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700637 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
638 return -1;
639 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700640
641 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700642 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700643 return -1;
644 }
645
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700646 if (hdr->page_size && (hdr->page_size != page_size)) {
647 page_size = hdr->page_size;
648 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700649 }
650
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700651 /*
652 * Update the kernel/ramdisk/tags address if the boot image header
653 * has default values, these default values come from mkbootimg when
654 * the boot image is flashed using fastboot flash:raw
655 */
656 update_ker_tags_rdisk_addr(hdr);
657
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700658 /* Get virtual addresses since the hdr saves physical addresses. */
659 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
660 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
661 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700662
663 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
664 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
665
666 /* Check if the addresses in the header are valid. */
667 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
668 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
669 {
670 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
671 return -1;
672 }
673
674#ifndef DEVICE_TREE
675 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
676 {
677 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
678 return -1;
679 }
680#endif
681
Shashank Mittalcd98d472011-08-02 14:29:24 -0700682 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300683 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
684 (int) target_use_signed_kernel(),
685 device.is_unlocked,
686 device.is_tampered);
687
Shashank Mittala0032282011-08-26 14:50:11 -0700688 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800689 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700690 offset = 0;
691
Shashank Mittalcd98d472011-08-02 14:29:24 -0700692 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700693
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800694#if DEVICE_TREE
695 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
696 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700697
698 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
699 {
700 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
701 return -1;
702 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800703#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530704 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700705
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700706#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700707
Amol Jadib6be5c12012-11-14 13:39:51 -0800708 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700709 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800710
Shashank Mittalcd98d472011-08-02 14:29:24 -0700711 /* Read image without signature */
712 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
713 {
714 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
715 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800716 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700717
Amol Jadib6be5c12012-11-14 13:39:51 -0800718 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700719 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800720
Shashank Mittalcd98d472011-08-02 14:29:24 -0700721 offset = imagesize_actual;
722 /* Read signature */
723 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
724 {
725 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700726 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700727 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800728
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700729 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700730
Neeti Desai465491e2012-07-31 12:53:35 -0700731 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700732 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
733 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700734
Neeti Desai465491e2012-07-31 12:53:35 -0700735 #if DEVICE_TREE
736 if(hdr->dt_size) {
737 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700738 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700739
740 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
741
742 /* Restriction that the device tree entry table should be less than a page*/
743 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
744
745 /* Validate the device tree table header */
746 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
747 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
748 return -1;
749 }
750
751 /* Find index of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700752 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700753 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
754 return -1;
755 }
756
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700757 /* Validate and Read device device tree in the "tags_add */
758 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry_ptr->size))
759 {
760 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
761 return -1;
762 }
763
Neeti Desai465491e2012-07-31 12:53:35 -0700764 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800765 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800766 /*
767 * If appended dev tree is found, update the atags with
768 * memory address to the DTB appended location on RAM.
769 * Else update with the atags address in the kernel header
770 */
771 void *dtb;
772 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700773 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800774 if (!dtb) {
775 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
776 return -1;
777 }
Neeti Desai465491e2012-07-31 12:53:35 -0700778 }
779 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700780 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700781 else
782 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800783 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700784
Amol Jadib6be5c12012-11-14 13:39:51 -0800785 dprintf(INFO, "Loading boot image (%d): start\n",
786 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700787 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800788
789 offset = page_size;
790
791 /* Load kernel */
792 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700793 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
794 return -1;
795 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800796 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700797
Amol Jadib6be5c12012-11-14 13:39:51 -0800798 /* Load ramdisk */
799 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700800 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800801 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700802 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
803 return -1;
804 }
805 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800806 offset += ramdisk_actual;
807
808 dprintf(INFO, "Loading boot image (%d): done\n",
809 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700810 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700811
812 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800813 offset += second_actual;
814 /* Second image loading not implemented. */
815 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700816 }
817
818 #if DEVICE_TREE
819 if(hdr->dt_size != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700820 /* Read the device tree table into buffer */
821 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
822 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
823 return -1;
824 }
825 table = (struct dt_table*) dt_buf;
826
827 /* Restriction that the device tree entry table should be less than a page*/
828 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
829
830 /* Validate the device tree table header */
831 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
832 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
833 return -1;
834 }
835
836 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700837 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700838 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
839 return -1;
840 }
841
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700842 /* Validate and Read device device tree in the "tags_add */
843 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry_ptr->size))
844 {
845 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
846 return -1;
847 }
848
Neeti Desai465491e2012-07-31 12:53:35 -0700849 if(mmc_read(ptn + offset + dt_entry_ptr->offset,
850 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
851 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
852 return -1;
853 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300854 #ifdef TZ_SAVE_KERNEL_HASH
855 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
856 hdr->ramdisk_addr, ramdisk_actual,
857 ptn, offset, hdr->dt_size);
858 #endif /* TZ_SAVE_KERNEL_HASH */
859
Channagoud Kadabi35095622013-03-01 13:53:05 -0800860 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800861 /*
862 * If appended dev tree is found, update the atags with
863 * memory address to the DTB appended location on RAM.
864 * Else update with the atags address in the kernel header
865 */
866 void *dtb;
867 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700868 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800869 if (!dtb) {
870 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
871 return -1;
872 }
Neeti Desai465491e2012-07-31 12:53:35 -0700873 }
874 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700875 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700876
877unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700878
Dima Zavin77e41f32013-03-06 16:10:43 -0800879 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700880 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700881 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
882
883 return 0;
884}
885
Dima Zavin214cc642009-01-26 11:16:21 -0800886int boot_linux_from_flash(void)
887{
888 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800889 struct ptentry *ptn;
890 struct ptable *ptable;
891 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800892
Shashank Mittalcd98d472011-08-02 14:29:24 -0700893 unsigned char *image_addr = 0;
894 unsigned kernel_actual;
895 unsigned ramdisk_actual;
896 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800897 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700898
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700899#if DEVICE_TREE
900 struct dt_table *table;
901 struct dt_entry *dt_entry_ptr;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800902 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700903#endif
904
David Ng183a7422009-12-07 14:55:21 -0800905 if (target_is_emmc_boot()) {
906 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
907 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
908 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
909 return -1;
910 }
911 goto continue_boot;
912 }
913
Dima Zavin214cc642009-01-26 11:16:21 -0800914 ptable = flash_get_ptable();
915 if (ptable == NULL) {
916 dprintf(CRITICAL, "ERROR: Partition table not found\n");
917 return -1;
918 }
919
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800920 if(!boot_into_recovery)
921 {
922 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700923
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800924 if (ptn == NULL) {
925 dprintf(CRITICAL, "ERROR: No boot partition found\n");
926 return -1;
927 }
928 }
929 else
930 {
931 ptn = ptable_find(ptable, "recovery");
932 if (ptn == NULL) {
933 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
934 return -1;
935 }
Dima Zavin214cc642009-01-26 11:16:21 -0800936 }
937
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800938 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800939 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
940 return -1;
941 }
Dima Zavin214cc642009-01-26 11:16:21 -0800942
943 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700944 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800945 return -1;
946 }
947
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800948 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700949 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 -0800950 return -1;
951 }
952
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700953 /*
954 * Update the kernel/ramdisk/tags address if the boot image header
955 * has default values, these default values come from mkbootimg when
956 * the boot image is flashed using fastboot flash:raw
957 */
958 update_ker_tags_rdisk_addr(hdr);
959
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700960 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700961 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
962 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
963 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
964
965 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
966 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
967
968 /* Check if the addresses in the header are valid. */
969 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
970 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
971 {
972 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
973 return -1;
974 }
975
976#ifndef DEVICE_TREE
977 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
978 {
979 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
980 return -1;
981 }
982#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700983
Shashank Mittalcd98d472011-08-02 14:29:24 -0700984 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700985 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700986 {
987 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700988 offset = 0;
989
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800990#if DEVICE_TREE
991 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
992 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700993
994 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
995 {
996 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
997 return -1;
998 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800999#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001000 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001001#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001002
Amol Jadib6be5c12012-11-14 13:39:51 -08001003 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001004 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001005
Shashank Mittalcd98d472011-08-02 14:29:24 -07001006 /* Read image without signature */
1007 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1008 {
1009 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1010 return -1;
1011 }
Dima Zavin214cc642009-01-26 11:16:21 -08001012
Amol Jadib6be5c12012-11-14 13:39:51 -08001013 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001014 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001015
Shashank Mittalcd98d472011-08-02 14:29:24 -07001016 offset = imagesize_actual;
1017 /* Read signature */
1018 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1019 {
1020 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001021 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001022 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001023
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001024 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001025
1026 /* Move kernel and ramdisk to correct address */
1027 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1028 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001029#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001030 /* Validate and Read device device tree in the "tags_add */
1031 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry_ptr->size))
1032 {
1033 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1034 return -1;
1035 }
1036
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001037 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1038#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001039
1040 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001041 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001042 {
1043 write_device_info_flash(&device);
1044 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301045#if USE_PCOM_SECBOOT
1046 set_tamper_flag(device.is_tampered);
1047#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001048 }
1049 else
1050 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001051 offset = page_size;
1052
Amol Jadib6be5c12012-11-14 13:39:51 -08001053 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1054 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1055 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1056
1057 dprintf(INFO, "Loading boot image (%d): start\n",
1058 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001059 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001060
1061 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001062 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1063 return -1;
1064 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001065 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001066
Amol Jadib6be5c12012-11-14 13:39:51 -08001067 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001068 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1069 return -1;
1070 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001071 offset += ramdisk_actual;
1072
1073 dprintf(INFO, "Loading boot image (%d): done\n",
1074 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001075 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001076
1077 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001078 offset += second_actual;
1079 /* Second image loading not implemented. */
1080 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001081 }
1082
1083#if DEVICE_TREE
1084 if(hdr->dt_size != 0) {
1085
1086 /* Read the device tree table into buffer */
1087 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1088 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1089 return -1;
1090 }
1091
1092 table = (struct dt_table*) dt_buf;
1093
1094 /* Restriction that the device tree entry table should be less than a page*/
1095 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1096
1097 /* Validate the device tree table header */
1098 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1099 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1100 return -1;
1101 }
1102
1103 /* Calculate the offset of device tree within device tree table */
1104 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
1105 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1106 return -1;
1107 }
1108
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001109 /* Validate and Read device device tree in the "tags_add */
1110 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry_ptr->size))
1111 {
1112 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1113 return -1;
1114 }
1115
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001116 /* Read device device tree in the "tags_add */
1117 if(flash_read(ptn, offset + dt_entry_ptr->offset,
1118 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
1119 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1120 return -1;
1121 }
1122 }
1123#endif
1124
Shashank Mittalcd98d472011-08-02 14:29:24 -07001125 }
David Ng183a7422009-12-07 14:55:21 -08001126continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001127
Dima Zavin214cc642009-01-26 11:16:21 -08001128 /* TODO: create/pass atags to kernel */
1129
Ajay Dudanie28a6072011-07-01 13:59:46 -07001130 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001131 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001132 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1133
1134 return 0;
1135}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001136
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001137BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001138void write_device_info_mmc(device_info *dev)
1139{
1140 struct device_info *info = (void*) info_buf;
1141 unsigned long long ptn = 0;
1142 unsigned long long size;
1143 int index = INVALID_PTN;
1144
1145 index = partition_get_index("aboot");
1146 ptn = partition_get_offset(index);
1147 if(ptn == 0)
1148 {
1149 return;
1150 }
1151
1152 size = partition_get_size(index);
1153
1154 memcpy(info, dev, sizeof(device_info));
1155
1156 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1157 {
1158 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1159 return;
1160 }
1161}
1162
1163void read_device_info_mmc(device_info *dev)
1164{
1165 struct device_info *info = (void*) info_buf;
1166 unsigned long long ptn = 0;
1167 unsigned long long size;
1168 int index = INVALID_PTN;
1169
1170 index = partition_get_index("aboot");
1171 ptn = partition_get_offset(index);
1172 if(ptn == 0)
1173 {
1174 return;
1175 }
1176
1177 size = partition_get_size(index);
1178
1179 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1180 {
1181 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1182 return;
1183 }
1184
1185 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1186 {
1187 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1188 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001189 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001190
1191 write_device_info_mmc(info);
1192 }
1193 memcpy(dev, info, sizeof(device_info));
1194}
1195
1196void write_device_info_flash(device_info *dev)
1197{
1198 struct device_info *info = (void *) info_buf;
1199 struct ptentry *ptn;
1200 struct ptable *ptable;
1201
1202 ptable = flash_get_ptable();
1203 if (ptable == NULL)
1204 {
1205 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1206 return;
1207 }
1208
1209 ptn = ptable_find(ptable, "devinfo");
1210 if (ptn == NULL)
1211 {
1212 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1213 return;
1214 }
1215
1216 memcpy(info, dev, sizeof(device_info));
1217
1218 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1219 {
1220 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1221 return;
1222 }
1223}
1224
1225void read_device_info_flash(device_info *dev)
1226{
1227 struct device_info *info = (void*) info_buf;
1228 struct ptentry *ptn;
1229 struct ptable *ptable;
1230
1231 ptable = flash_get_ptable();
1232 if (ptable == NULL)
1233 {
1234 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1235 return;
1236 }
1237
1238 ptn = ptable_find(ptable, "devinfo");
1239 if (ptn == NULL)
1240 {
1241 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1242 return;
1243 }
1244
1245 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1246 {
1247 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1248 return;
1249 }
1250
1251 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1252 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001253 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1254 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001255 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001256 write_device_info_flash(info);
1257 }
1258 memcpy(dev, info, sizeof(device_info));
1259}
1260
1261void write_device_info(device_info *dev)
1262{
1263 if(target_is_emmc_boot())
1264 {
1265 write_device_info_mmc(dev);
1266 }
1267 else
1268 {
1269 write_device_info_flash(dev);
1270 }
1271}
1272
1273void read_device_info(device_info *dev)
1274{
1275 if(target_is_emmc_boot())
1276 {
1277 read_device_info_mmc(dev);
1278 }
1279 else
1280 {
1281 read_device_info_flash(dev);
1282 }
1283}
1284
1285void reset_device_info()
1286{
1287 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001288 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001289 write_device_info(&device);
1290}
1291
1292void set_device_root()
1293{
1294 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001295 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001296 write_device_info(&device);
1297}
1298
Amol Jadicb524072012-08-09 16:40:18 -07001299#if DEVICE_TREE
1300int copy_dtb(uint8_t *boot_image_start)
1301{
1302 uint32 dt_image_offset = 0;
1303 uint32_t n;
1304 struct dt_table *table;
1305 struct dt_entry *dt_entry_ptr;
Amol Jadicb524072012-08-09 16:40:18 -07001306
1307 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1308
Amol Jadicb524072012-08-09 16:40:18 -07001309 if(hdr->dt_size != 0) {
1310
1311 /* add kernel offset */
1312 dt_image_offset += page_size;
1313 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1314 dt_image_offset += n;
1315
1316 /* add ramdisk offset */
1317 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1318 dt_image_offset += n;
1319
1320 /* add second offset */
1321 if(hdr->second_size != 0) {
1322 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1323 dt_image_offset += n;
1324 }
1325
1326 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001327 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001328
1329 /* Restriction that the device tree entry table should be less than a page*/
1330 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1331
1332 /* Validate the device tree table header */
1333 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1334 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1335 return -1;
1336 }
1337
1338 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001339 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Amol Jadicb524072012-08-09 16:40:18 -07001340 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1341 return -1;
1342 }
1343
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001344 /* Validate and Read device device tree in the "tags_add */
1345 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry_ptr->size))
1346 {
1347 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1348 return -1;
1349 }
1350
Amol Jadicb524072012-08-09 16:40:18 -07001351 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001352 memmove((void*) hdr->tags_addr,
1353 boot_image_start + dt_image_offset + dt_entry_ptr->offset,
1354 dt_entry_ptr->size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001355 } else
1356 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001357
1358 /* Everything looks fine. Return success. */
1359 return 0;
1360}
1361#endif
1362
Brian Swetland9c4c0752009-01-25 16:23:50 -08001363void cmd_boot(const char *arg, void *data, unsigned sz)
1364{
1365 unsigned kernel_actual;
1366 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001367 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001368 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001369 int ret = 0;
1370 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001371
1372 if (sz < sizeof(hdr)) {
1373 fastboot_fail("invalid bootimage header");
1374 return;
1375 }
1376
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001377 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001378
1379 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001380 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001381
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001382 if(target_is_emmc_boot() && hdr->page_size) {
1383 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001384 page_mask = page_size - 1;
1385 }
1386
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001387 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1388 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1389
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001390 /*
1391 * Update the kernel/ramdisk/tags address if the boot image header
1392 * has default values, these default values come from mkbootimg when
1393 * the boot image is flashed using fastboot flash:raw
1394 */
1395 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001396
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001397 /* Get virtual addresses since the hdr saves physical addresses. */
1398 hdr->kernel_addr = VA(hdr->kernel_addr);
1399 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1400 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001401
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001402 /* Check if the addresses in the header are valid. */
1403 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1404 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1405 {
1406 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1407 return -1;
1408 }
1409
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001410 /* sz should have atleast raw boot image */
1411 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001412 fastboot_fail("incomplete bootimage");
1413 return;
1414 }
1415
Amol Jadicb524072012-08-09 16:40:18 -07001416#if DEVICE_TREE
1417 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001418 ret = copy_dtb(data);
1419
1420 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001421#else
1422 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1423 {
1424 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1425 return -1;
1426 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001427#endif
1428
1429 /* Load ramdisk & kernel */
1430 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1431 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1432
1433#if DEVICE_TREE
1434 /*
1435 * If dtb is not found look for appended DTB in the kernel.
1436 * If appended dev tree is found, update the atags with
1437 * memory address to the DTB appended location on RAM.
1438 * Else update with the atags address in the kernel header
1439 */
1440 if (!dtb_copied) {
1441 void *dtb;
Channagoud Kadabia83a6972013-04-25 15:26:04 -07001442 dtb = dev_tree_appended((void *)hdr->kernel_addr, (void *)hdr->tags_addr, hdr->kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001443 if (!dtb) {
1444 fastboot_fail("dtb not found");
1445 return;
1446 }
Amol Jadicb524072012-08-09 16:40:18 -07001447 }
1448#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001449
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001450#ifndef DEVICE_TREE
1451 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1452 {
1453 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1454 return -1;
1455 }
1456#endif
1457
Brian Swetland9c4c0752009-01-25 16:23:50 -08001458 fastboot_okay("");
1459 udc_stop();
1460
Dima Zavin77e41f32013-03-06 16:10:43 -08001461 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001462 (const char*) hdr->cmdline, board_machtype(),
1463 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001464}
1465
Dima Zavin214cc642009-01-26 11:16:21 -08001466void cmd_erase(const char *arg, void *data, unsigned sz)
1467{
1468 struct ptentry *ptn;
1469 struct ptable *ptable;
1470
1471 ptable = flash_get_ptable();
1472 if (ptable == NULL) {
1473 fastboot_fail("partition table doesn't exist");
1474 return;
1475 }
1476
1477 ptn = ptable_find(ptable, arg);
1478 if (ptn == NULL) {
1479 fastboot_fail("unknown partition name");
1480 return;
1481 }
1482
1483 if (flash_erase(ptn)) {
1484 fastboot_fail("failed to erase partition");
1485 return;
1486 }
1487 fastboot_okay("");
1488}
1489
Bikas Gurungd48bd242010-09-04 19:54:32 -07001490
1491void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1492{
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001493 BUF_DMA_ALIGN(out, 512);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001494 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001495 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001496
Kinson Chikf1a43512011-07-14 11:28:39 -07001497 index = partition_get_index(arg);
1498 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001499
Kinson Chikf1a43512011-07-14 11:28:39 -07001500 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001501 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001502 return;
1503 }
neetidb4b24d62012-01-20 12:13:09 -08001504 /* Simple inefficient version of erase. Just writing
1505 0 in first block */
1506 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1507 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001508 return;
1509 }
1510 fastboot_okay("");
1511}
1512
1513
Ajay Dudani5c761132011-04-07 20:19:04 -07001514void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001515{
1516 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001517 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001518 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001519
Greg Grisco6e754772011-06-23 12:19:39 -07001520 if (!strcmp(arg, "partition"))
1521 {
1522 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001523 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001524 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001525 return;
1526 }
1527 }
Greg Grisco6e754772011-06-23 12:19:39 -07001528 else
1529 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001530 index = partition_get_index(arg);
1531 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001532 if(ptn == 0) {
1533 fastboot_fail("partition table doesn't exist");
1534 return;
1535 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001536
Greg Grisco6e754772011-06-23 12:19:39 -07001537 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1538 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1539 fastboot_fail("image is not a boot image");
1540 return;
1541 }
1542 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001543
Kinson Chikf1a43512011-07-14 11:28:39 -07001544 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001545 if (ROUND_TO_PAGE(sz,511) > size) {
1546 fastboot_fail("size too large");
1547 return;
1548 }
1549 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1550 fastboot_fail("flash write failure");
1551 return;
1552 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001553 }
1554 fastboot_okay("");
1555 return;
1556}
1557
Ajay Dudani5c761132011-04-07 20:19:04 -07001558void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1559{
1560 unsigned int chunk;
1561 unsigned int chunk_data_sz;
1562 sparse_header_t *sparse_header;
1563 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001564 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001565 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301566 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001567 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001568
Kinson Chikf1a43512011-07-14 11:28:39 -07001569 index = partition_get_index(arg);
1570 ptn = partition_get_offset(index);
1571 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001572 fastboot_fail("partition table doesn't exist");
1573 return;
1574 }
1575
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301576 size = partition_get_size(index);
1577 if (ROUND_TO_PAGE(sz,511) > size) {
1578 fastboot_fail("size too large");
1579 return;
1580 }
1581
Ajay Dudani5c761132011-04-07 20:19:04 -07001582 /* Read and skip over sparse image header */
1583 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001584 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1585 fastboot_fail("size too large");
1586 return;
1587 }
1588
Ajay Dudani5c761132011-04-07 20:19:04 -07001589 data += sparse_header->file_hdr_sz;
1590 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1591 {
1592 /* Skip the remaining bytes in a header that is longer than
1593 * we expected.
1594 */
1595 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1596 }
1597
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001598 dprintf (SPEW, "=== Sparse Image Header ===\n");
1599 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1600 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1601 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1602 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1603 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1604 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1605 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1606 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001607
1608 /* Start processing chunks */
1609 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1610 {
1611 /* Read and skip over chunk header */
1612 chunk_header = (chunk_header_t *) data;
1613 data += sizeof(chunk_header_t);
1614
1615 dprintf (SPEW, "=== Chunk Header ===\n");
1616 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1617 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1618 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1619
1620 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1621 {
1622 /* Skip the remaining bytes in a header that is longer than
1623 * we expected.
1624 */
1625 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1626 }
1627
1628 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1629 switch (chunk_header->chunk_type)
1630 {
1631 case CHUNK_TYPE_RAW:
1632 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1633 chunk_data_sz))
1634 {
1635 fastboot_fail("Bogus chunk size for chunk type Raw");
1636 return;
1637 }
1638
Ajay Dudaniab18f022011-05-12 14:39:22 -07001639 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1640 chunk_data_sz,
1641 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001642 {
1643 fastboot_fail("flash write failure");
1644 return;
1645 }
1646 total_blocks += chunk_header->chunk_sz;
1647 data += chunk_data_sz;
1648 break;
1649
1650 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001651 total_blocks += chunk_header->chunk_sz;
1652 break;
1653
Ajay Dudani5c761132011-04-07 20:19:04 -07001654 case CHUNK_TYPE_CRC:
1655 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1656 {
1657 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1658 return;
1659 }
1660 total_blocks += chunk_header->chunk_sz;
1661 data += chunk_data_sz;
1662 break;
1663
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001664 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001665 fastboot_fail("Unknown chunk type");
1666 return;
1667 }
1668 }
1669
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001670 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1671 total_blocks, sparse_header->total_blks);
1672
1673 if(total_blocks != sparse_header->total_blks)
1674 {
1675 fastboot_fail("sparse image write failure");
1676 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001677
1678 fastboot_okay("");
1679 return;
1680}
1681
1682void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1683{
1684 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001685 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1686 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001687
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001688#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001689 int ret=0;
1690 uint32 major_version=0;
1691 uint32 minor_version=0;
1692
1693 ret = scm_svc_version(&major_version,&minor_version);
1694 if(!ret)
1695 {
1696 if(major_version >= 2)
1697 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001698 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001699 {
1700 ret = encrypt_scm((uint32 **) &data, &sz);
1701 if (ret != 0) {
1702 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1703 return;
1704 }
1705
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001706 /* Protect only for SSD */
1707 if (!strcmp(arg, "ssd")) {
1708 ret = scm_protect_keystore((uint32 *) data, sz);
1709 if (ret != 0) {
1710 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1711 return;
1712 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001713 }
1714 }
1715 else
1716 {
1717 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1718 if(ret != 0)
1719 {
1720 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1721 return;
1722 }
1723 }
1724 }
1725 else
1726 {
1727 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1728 magic_number[1] == DECRYPT_MAGIC_1)
1729 {
1730 ret = decrypt_scm((uint32 **) &data, &sz);
1731 if (ret != 0) {
1732 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1733 return;
1734 }
1735 }
1736 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1737 magic_number[1] == ENCRYPT_MAGIC_1)
1738 {
1739 ret = encrypt_scm((uint32 **) &data, &sz);
1740 if (ret != 0) {
1741 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1742 return;
1743 }
1744 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001745 }
1746 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001747 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001748 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001749 dprintf(CRITICAL,"INVALID SVC Version\n");
1750 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001751 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001752#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001753
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001754 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001755 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1756 cmd_flash_mmc_img(arg, data, sz);
1757 else
1758 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001759 return;
1760}
1761
Dima Zavin214cc642009-01-26 11:16:21 -08001762void cmd_flash(const char *arg, void *data, unsigned sz)
1763{
1764 struct ptentry *ptn;
1765 struct ptable *ptable;
1766 unsigned extra = 0;
1767
1768 ptable = flash_get_ptable();
1769 if (ptable == NULL) {
1770 fastboot_fail("partition table doesn't exist");
1771 return;
1772 }
1773
1774 ptn = ptable_find(ptable, arg);
1775 if (ptn == NULL) {
1776 fastboot_fail("unknown partition name");
1777 return;
1778 }
1779
1780 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1781 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1782 fastboot_fail("image is not a boot image");
1783 return;
1784 }
1785 }
1786
Amol Jadi5c61a952012-05-04 17:05:35 -07001787 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001788 || !strcmp(ptn->name, "userdata")
1789 || !strcmp(ptn->name, "persist")
1790 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301791 if (flash_ecc_bch_enabled())
1792 /* Spare data bytes for 8 bit ECC increased by 4 */
1793 extra = ((page_size >> 9) * 20);
1794 else
1795 extra = ((page_size >> 9) * 16);
1796 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001797 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001798
1799 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1800 if (flash_write(ptn, extra, data, sz)) {
1801 fastboot_fail("flash write failure");
1802 return;
1803 }
1804 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1805 fastboot_okay("");
1806}
1807
1808void cmd_continue(const char *arg, void *data, unsigned sz)
1809{
1810 fastboot_okay("");
1811 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001812 if (target_is_emmc_boot())
1813 {
1814 boot_linux_from_mmc();
1815 }
1816 else
1817 {
1818 boot_linux_from_flash();
1819 }
Dima Zavin214cc642009-01-26 11:16:21 -08001820}
1821
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001822void cmd_reboot(const char *arg, void *data, unsigned sz)
1823{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001824 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001825 fastboot_okay("");
1826 reboot_device(0);
1827}
1828
1829void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1830{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001831 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001832 fastboot_okay("");
1833 reboot_device(FASTBOOT_MODE);
1834}
1835
Shashank Mittal162244e2011-08-08 19:01:25 -07001836void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1837{
1838 if(!device.is_unlocked)
1839 {
1840 device.is_unlocked = 1;
1841 write_device_info(&device);
1842 }
1843 fastboot_okay("");
1844}
1845
Shashank Mittala0032282011-08-26 14:50:11 -07001846void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1847{
1848 char response[64];
1849 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1850 fastboot_info(response);
1851 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1852 fastboot_info(response);
1853 fastboot_okay("");
1854}
1855
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001856void cmd_preflash(const char *arg, void *data, unsigned sz)
1857{
1858 fastboot_okay("");
1859}
1860
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001861void splash_screen ()
1862{
1863 struct ptentry *ptn;
1864 struct ptable *ptable;
1865 struct fbcon_config *fb_display = NULL;
1866
1867 if (!target_is_emmc_boot())
1868 {
1869 ptable = flash_get_ptable();
1870 if (ptable == NULL) {
1871 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001872 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001873 }
1874
1875 ptn = ptable_find(ptable, "splash");
1876 if (ptn == NULL) {
1877 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1878 } else {
1879 fb_display = fbcon_display();
1880 if (fb_display) {
1881 if (flash_read(ptn, 0, fb_display->base,
1882 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1883 fbcon_clear();
1884 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1885 }
1886 }
1887 }
1888 }
1889}
1890
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001891/* Get the size from partiton name */
1892static void get_partition_size(const char *arg, char *response)
1893{
1894 uint64_t ptn = 0;
1895 uint64_t size;
1896 int index = INVALID_PTN;
1897
1898 index = partition_get_index(arg);
1899
1900 if (index == INVALID_PTN)
1901 {
1902 dprintf(CRITICAL, "Invalid partition index\n");
1903 return;
1904 }
1905
1906 ptn = partition_get_offset(index);
1907
1908 if(!ptn)
1909 {
1910 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1911 return;
1912 }
1913
1914 size = partition_get_size(index);
1915
1916 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1917 return;
1918}
1919
1920/*
1921 * Publish the partition type & size info
1922 * fastboot getvar will publish the required information.
1923 * fastboot getvar partition_size:<partition_name>: partition size in hex
1924 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1925 */
1926static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1927{
1928 uint8_t i;
1929
1930 for (i = 0; i < num_parts; i++) {
1931 get_partition_size(info[i].part_name, info[i].size_response);
1932
1933 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1934 {
1935 dprintf(CRITICAL, "partition size name truncated\n");
1936 return;
1937 }
1938 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1939 {
1940 dprintf(CRITICAL, "partition type name truncated\n");
1941 return;
1942 }
1943
1944 /* publish partition size & type info */
1945 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1946 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1947 }
1948}
1949
Brian Swetland9c4c0752009-01-25 16:23:50 -08001950void aboot_init(const struct app_descriptor *app)
1951{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001952 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001953 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001954 unsigned sz = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03001955 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001956
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001957 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001958 if (target_is_emmc_boot())
1959 {
1960 page_size = 2048;
1961 page_mask = page_size - 1;
1962 }
1963 else
1964 {
1965 page_size = flash_page_size();
1966 page_mask = page_size - 1;
1967 }
1968
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001969 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
1970
Shashank Mittal162244e2011-08-08 19:01:25 -07001971 if(target_use_signed_kernel())
1972 {
1973 read_device_info(&device);
1974
Shashank Mittal162244e2011-08-08 19:01:25 -07001975 }
1976
Greg Griscod6250552011-06-29 14:40:23 -07001977 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001978 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001979 surf_udc_device.serialno = sn_buf;
1980
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001981 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001982 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001983 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001984 dprintf(ALWAYS,"dload mode key sequence detected\n");
1985 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001986 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001987 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001988 }
1989 else
1990 {
1991 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001992 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001993 }
1994 boot_into_fastboot = true;
1995 }
1996 if (!boot_into_fastboot)
1997 {
1998 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
1999 boot_into_recovery = 1;
2000 if (!boot_into_recovery &&
2001 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002002 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002003 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002004 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002005 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002006 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002007 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002008
Ajay Dudani77421292010-10-27 19:34:06 -07002009 reboot_mode = check_reboot_mode();
2010 if (reboot_mode == RECOVERY_MODE) {
2011 boot_into_recovery = 1;
2012 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002013 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002014 }
2015
Pavel Nedev5d91d412013-04-29 11:34:24 +03002016 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002017 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002018 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002019 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002020 if(emmc_recovery_init())
2021 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2022 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002023 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002024 if((device.is_unlocked) || (device.is_tampered))
2025 {
2026 #ifdef TZ_TAMPER_FUSE
2027 set_tamper_fuse_cmd();
2028 #endif
2029 #if USE_PCOM_SECBOOT
2030 set_tamper_flag(device.is_tampered);
2031 #endif
2032 }
Shashank Mittala0032282011-08-26 14:50:11 -07002033 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002034 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002035 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002036 else
2037 {
2038 recovery_init();
2039 #if USE_PCOM_SECBOOT
2040 if((device.is_unlocked) || (device.is_tampered))
2041 set_tamper_flag(device.is_tampered);
2042 #endif
2043 boot_linux_from_flash();
2044 }
2045 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2046 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002047 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002048
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002049 sz = target_get_max_flash_size();
2050
Shashank Mittal162244e2011-08-08 19:01:25 -07002051 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002052
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002053 if(!usb_init)
2054 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002055
2056 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002057
Shashank Mittal23b8f422010-04-16 19:27:21 -07002058 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002059 {
2060 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002061 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002062 }
2063 else
2064 {
2065 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002066 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002067 }
2068
2069 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002070 fastboot_register("reboot", cmd_reboot);
2071 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07002072 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07002073 fastboot_register("oem device-info", cmd_oem_devinfo);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002074 fastboot_register("preflash", cmd_preflash);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07002075 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08002076 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08002077 fastboot_publish("serialno", sn_buf);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002078 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2079 /* Max download size supported */
2080 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", sz);
2081 fastboot_publish("max-download-size", (const char *) max_download_size);
Kinson Chikf1a43512011-07-14 11:28:39 -07002082 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07002083 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002084 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002085}
2086
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002087uint32_t get_page_size()
2088{
2089 return page_size;
2090}
2091
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002092/*
2093 * Calculated and save hash (SHA256) for non-signed boot image.
2094 *
2095 * Hash the same data that is checked on the signed boot image.
2096 * Kernel and Ramdisk are already read to memory buffers.
2097 * Need to read the entire device-tree from mmc
2098 * since non-signed image only read the DT tags of the relevant platform.
2099 *
2100 * @param kernel_addr - kernel bufer
2101 * @param kernel_actual - kernel size in bytes
2102 * @param ramdisk_addr - ramdisk buffer
2103 * @param ramdisk_actual - ramdisk size
2104 * @param ptn - partition
2105 * @param dt_offset - device tree offset on mmc partition
2106 * @param dt_size
2107 *
2108 * @return int - 0 on success, negative value on failure.
2109 */
2110int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2111 void *ramdisk_addr, unsigned ramdisk_actual,
2112 unsigned long long ptn,
2113 unsigned dt_offset, unsigned dt_size)
2114{
2115 SHA256_CTX sha256_ctx;
2116 char digest[32]={0};
2117 char *buf = (char *)target_get_scratch_address();
2118 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2119 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2120
2121 SHA256_Init(&sha256_ctx);
2122
2123 /* Read Boot Header */
2124 if (mmc_read(ptn, buf, page_size))
2125 {
2126 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2127 return -1;
2128 }
2129 /* Read entire Device Tree */
2130 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2131 {
2132 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2133 return -1;
2134 }
2135 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2136 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2137 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2138 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2139
2140 SHA256_Final(digest, &sha256_ctx);
2141
2142 save_kernel_hash_cmd(digest);
2143 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2144
2145 return 0;
2146}
2147
Brian Swetland9c4c0752009-01-25 16:23:50 -08002148APP_START(aboot)
2149 .init = aboot_init,
2150APP_END