blob: 2e4b8200dfa86e3ffdb1e26712497ccaeadad99f [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;
Joel Kingaa335dc2013-06-03 16:11:08 -0700598 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700599 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
Joel Kingaa335dc2013-06-03 16:11:08 -0700742 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700743 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
744 return -1;
745 }
Neeti Desai465491e2012-07-31 12:53:35 -0700746 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700747 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700748 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
749 return -1;
750 }
751
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700752 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700753 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700754 {
755 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
756 return -1;
757 }
758
Joel Kingaa335dc2013-06-03 16:11:08 -0700759 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800760 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800761 /*
762 * If appended dev tree is found, update the atags with
763 * memory address to the DTB appended location on RAM.
764 * Else update with the atags address in the kernel header
765 */
766 void *dtb;
767 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700768 hdr->kernel_size,
769 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800770 if (!dtb) {
771 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
772 return -1;
773 }
Neeti Desai465491e2012-07-31 12:53:35 -0700774 }
775 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700776 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700777 else
778 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800779 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700780
Amol Jadib6be5c12012-11-14 13:39:51 -0800781 dprintf(INFO, "Loading boot image (%d): start\n",
782 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700783 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800784
785 offset = page_size;
786
787 /* Load kernel */
788 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700789 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
790 return -1;
791 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800792 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700793
Amol Jadib6be5c12012-11-14 13:39:51 -0800794 /* Load ramdisk */
795 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700796 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800797 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700798 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
799 return -1;
800 }
801 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800802 offset += ramdisk_actual;
803
804 dprintf(INFO, "Loading boot image (%d): done\n",
805 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700806 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700807
808 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800809 offset += second_actual;
810 /* Second image loading not implemented. */
811 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700812 }
813
814 #if DEVICE_TREE
815 if(hdr->dt_size != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700816 /* Read the device tree table into buffer */
817 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
818 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
819 return -1;
820 }
821 table = (struct dt_table*) dt_buf;
822
Joel Kingaa335dc2013-06-03 16:11:08 -0700823 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700824 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
825 return -1;
826 }
827
Joel Kingaa335dc2013-06-03 16:11:08 -0700828 /* Find index of device tree within device tree table */
829 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700830 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
831 return -1;
832 }
833
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700834 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700835 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700836 {
837 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
838 return -1;
839 }
840
Joel Kingaa335dc2013-06-03 16:11:08 -0700841 if(mmc_read(ptn + offset + dt_entry.offset,
842 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700843 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
844 return -1;
845 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300846 #ifdef TZ_SAVE_KERNEL_HASH
847 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
848 hdr->ramdisk_addr, ramdisk_actual,
849 ptn, offset, hdr->dt_size);
850 #endif /* TZ_SAVE_KERNEL_HASH */
851
Channagoud Kadabi35095622013-03-01 13:53:05 -0800852 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800853 /*
854 * If appended dev tree is found, update the atags with
855 * memory address to the DTB appended location on RAM.
856 * Else update with the atags address in the kernel header
857 */
858 void *dtb;
859 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700860 kernel_actual,
861 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800862 if (!dtb) {
863 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
864 return -1;
865 }
Neeti Desai465491e2012-07-31 12:53:35 -0700866 }
867 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700868 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700869
870unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700871
Dima Zavin77e41f32013-03-06 16:10:43 -0800872 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700873 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700874 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
875
876 return 0;
877}
878
Dima Zavin214cc642009-01-26 11:16:21 -0800879int boot_linux_from_flash(void)
880{
881 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800882 struct ptentry *ptn;
883 struct ptable *ptable;
884 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800885
Shashank Mittalcd98d472011-08-02 14:29:24 -0700886 unsigned char *image_addr = 0;
887 unsigned kernel_actual;
888 unsigned ramdisk_actual;
889 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800890 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700891
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700892#if DEVICE_TREE
893 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700894 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800895 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700896#endif
897
David Ng183a7422009-12-07 14:55:21 -0800898 if (target_is_emmc_boot()) {
899 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
900 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
901 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
902 return -1;
903 }
904 goto continue_boot;
905 }
906
Dima Zavin214cc642009-01-26 11:16:21 -0800907 ptable = flash_get_ptable();
908 if (ptable == NULL) {
909 dprintf(CRITICAL, "ERROR: Partition table not found\n");
910 return -1;
911 }
912
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800913 if(!boot_into_recovery)
914 {
915 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700916
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800917 if (ptn == NULL) {
918 dprintf(CRITICAL, "ERROR: No boot partition found\n");
919 return -1;
920 }
921 }
922 else
923 {
924 ptn = ptable_find(ptable, "recovery");
925 if (ptn == NULL) {
926 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
927 return -1;
928 }
Dima Zavin214cc642009-01-26 11:16:21 -0800929 }
930
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800931 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800932 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
933 return -1;
934 }
Dima Zavin214cc642009-01-26 11:16:21 -0800935
936 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700937 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800938 return -1;
939 }
940
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800941 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700942 dprintf(CRITICAL, "ERROR: Invalid boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800943 return -1;
944 }
945
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700946 /*
947 * Update the kernel/ramdisk/tags address if the boot image header
948 * has default values, these default values come from mkbootimg when
949 * the boot image is flashed using fastboot flash:raw
950 */
951 update_ker_tags_rdisk_addr(hdr);
952
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700953 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700954 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
955 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
956 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
957
958 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
959 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
960
961 /* Check if the addresses in the header are valid. */
962 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
963 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
964 {
965 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
966 return -1;
967 }
968
969#ifndef DEVICE_TREE
970 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
971 {
972 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
973 return -1;
974 }
975#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700976
Shashank Mittalcd98d472011-08-02 14:29:24 -0700977 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700978 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700979 {
980 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700981 offset = 0;
982
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800983#if DEVICE_TREE
984 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
985 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700986
987 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
988 {
989 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
990 return -1;
991 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800992#else
Shashank Mittalcd98d472011-08-02 14:29:24 -0700993 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800994#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700995
Amol Jadib6be5c12012-11-14 13:39:51 -0800996 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700997 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800998
Shashank Mittalcd98d472011-08-02 14:29:24 -0700999 /* Read image without signature */
1000 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1001 {
1002 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1003 return -1;
1004 }
Dima Zavin214cc642009-01-26 11:16:21 -08001005
Amol Jadib6be5c12012-11-14 13:39:51 -08001006 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001007 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001008
Shashank Mittalcd98d472011-08-02 14:29:24 -07001009 offset = imagesize_actual;
1010 /* Read signature */
1011 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1012 {
1013 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001014 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001015 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001016
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001017 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001018
1019 /* Move kernel and ramdisk to correct address */
1020 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1021 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001022#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001023 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001024 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001025 {
1026 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1027 return -1;
1028 }
1029
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001030 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1031#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001032
1033 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001034 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001035 {
1036 write_device_info_flash(&device);
1037 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301038#if USE_PCOM_SECBOOT
1039 set_tamper_flag(device.is_tampered);
1040#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001041 }
1042 else
1043 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001044 offset = page_size;
1045
Amol Jadib6be5c12012-11-14 13:39:51 -08001046 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1047 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1048 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1049
1050 dprintf(INFO, "Loading boot image (%d): start\n",
1051 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001052 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001053
1054 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001055 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1056 return -1;
1057 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001058 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001059
Amol Jadib6be5c12012-11-14 13:39:51 -08001060 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001061 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1062 return -1;
1063 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001064 offset += ramdisk_actual;
1065
1066 dprintf(INFO, "Loading boot image (%d): done\n",
1067 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001068 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001069
1070 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001071 offset += second_actual;
1072 /* Second image loading not implemented. */
1073 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001074 }
1075
1076#if DEVICE_TREE
1077 if(hdr->dt_size != 0) {
1078
1079 /* Read the device tree table into buffer */
1080 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1081 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1082 return -1;
1083 }
1084
1085 table = (struct dt_table*) dt_buf;
1086
Joel Kingaa335dc2013-06-03 16:11:08 -07001087 if (dev_tree_validate(table, hdr->page_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001088 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1089 return -1;
1090 }
1091
Joel Kingaa335dc2013-06-03 16:11:08 -07001092 /* Find index of device tree within device tree table */
1093 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001094 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1095 return -1;
1096 }
1097
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001098 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001099 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001100 {
1101 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1102 return -1;
1103 }
1104
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001105 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001106 if(flash_read(ptn, offset + dt_entry.offset,
1107 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001108 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1109 return -1;
1110 }
1111 }
1112#endif
1113
Shashank Mittalcd98d472011-08-02 14:29:24 -07001114 }
David Ng183a7422009-12-07 14:55:21 -08001115continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001116
Dima Zavin214cc642009-01-26 11:16:21 -08001117 /* TODO: create/pass atags to kernel */
1118
Ajay Dudanie28a6072011-07-01 13:59:46 -07001119 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001120 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001121 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1122
1123 return 0;
1124}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001125
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001126BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001127void write_device_info_mmc(device_info *dev)
1128{
1129 struct device_info *info = (void*) info_buf;
1130 unsigned long long ptn = 0;
1131 unsigned long long size;
1132 int index = INVALID_PTN;
1133
1134 index = partition_get_index("aboot");
1135 ptn = partition_get_offset(index);
1136 if(ptn == 0)
1137 {
1138 return;
1139 }
1140
1141 size = partition_get_size(index);
1142
1143 memcpy(info, dev, sizeof(device_info));
1144
1145 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1146 {
1147 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1148 return;
1149 }
1150}
1151
1152void read_device_info_mmc(device_info *dev)
1153{
1154 struct device_info *info = (void*) info_buf;
1155 unsigned long long ptn = 0;
1156 unsigned long long size;
1157 int index = INVALID_PTN;
1158
1159 index = partition_get_index("aboot");
1160 ptn = partition_get_offset(index);
1161 if(ptn == 0)
1162 {
1163 return;
1164 }
1165
1166 size = partition_get_size(index);
1167
1168 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1169 {
1170 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1171 return;
1172 }
1173
1174 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1175 {
1176 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1177 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001178 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001179
1180 write_device_info_mmc(info);
1181 }
1182 memcpy(dev, info, sizeof(device_info));
1183}
1184
1185void write_device_info_flash(device_info *dev)
1186{
1187 struct device_info *info = (void *) info_buf;
1188 struct ptentry *ptn;
1189 struct ptable *ptable;
1190
1191 ptable = flash_get_ptable();
1192 if (ptable == NULL)
1193 {
1194 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1195 return;
1196 }
1197
1198 ptn = ptable_find(ptable, "devinfo");
1199 if (ptn == NULL)
1200 {
1201 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1202 return;
1203 }
1204
1205 memcpy(info, dev, sizeof(device_info));
1206
1207 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1208 {
1209 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1210 return;
1211 }
1212}
1213
1214void read_device_info_flash(device_info *dev)
1215{
1216 struct device_info *info = (void*) info_buf;
1217 struct ptentry *ptn;
1218 struct ptable *ptable;
1219
1220 ptable = flash_get_ptable();
1221 if (ptable == NULL)
1222 {
1223 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1224 return;
1225 }
1226
1227 ptn = ptable_find(ptable, "devinfo");
1228 if (ptn == NULL)
1229 {
1230 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1231 return;
1232 }
1233
1234 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1235 {
1236 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1237 return;
1238 }
1239
1240 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1241 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001242 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1243 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001244 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001245 write_device_info_flash(info);
1246 }
1247 memcpy(dev, info, sizeof(device_info));
1248}
1249
1250void write_device_info(device_info *dev)
1251{
1252 if(target_is_emmc_boot())
1253 {
1254 write_device_info_mmc(dev);
1255 }
1256 else
1257 {
1258 write_device_info_flash(dev);
1259 }
1260}
1261
1262void read_device_info(device_info *dev)
1263{
1264 if(target_is_emmc_boot())
1265 {
1266 read_device_info_mmc(dev);
1267 }
1268 else
1269 {
1270 read_device_info_flash(dev);
1271 }
1272}
1273
1274void reset_device_info()
1275{
1276 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001277 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001278 write_device_info(&device);
1279}
1280
1281void set_device_root()
1282{
1283 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001284 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001285 write_device_info(&device);
1286}
1287
Amol Jadicb524072012-08-09 16:40:18 -07001288#if DEVICE_TREE
1289int copy_dtb(uint8_t *boot_image_start)
1290{
1291 uint32 dt_image_offset = 0;
1292 uint32_t n;
1293 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001294 struct dt_entry dt_entry;
Amol Jadicb524072012-08-09 16:40:18 -07001295
1296 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1297
Amol Jadicb524072012-08-09 16:40:18 -07001298 if(hdr->dt_size != 0) {
1299
1300 /* add kernel offset */
1301 dt_image_offset += page_size;
1302 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1303 dt_image_offset += n;
1304
1305 /* add ramdisk offset */
1306 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1307 dt_image_offset += n;
1308
1309 /* add second offset */
1310 if(hdr->second_size != 0) {
1311 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1312 dt_image_offset += n;
1313 }
1314
1315 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001316 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001317
Joel Kingaa335dc2013-06-03 16:11:08 -07001318 if (dev_tree_validate(table, hdr->page_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001319 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1320 return -1;
1321 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001322 /* Find index of device tree within device tree table */
1323 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001324 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1325 return -1;
1326 }
1327
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001328 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001329 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001330 {
1331 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1332 return -1;
1333 }
1334
Amol Jadicb524072012-08-09 16:40:18 -07001335 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001336 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001337 boot_image_start + dt_image_offset + dt_entry.offset,
1338 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001339 } else
1340 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001341
1342 /* Everything looks fine. Return success. */
1343 return 0;
1344}
1345#endif
1346
Brian Swetland9c4c0752009-01-25 16:23:50 -08001347void cmd_boot(const char *arg, void *data, unsigned sz)
1348{
1349 unsigned kernel_actual;
1350 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001351 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001352 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001353 int ret = 0;
1354 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001355
1356 if (sz < sizeof(hdr)) {
1357 fastboot_fail("invalid bootimage header");
1358 return;
1359 }
1360
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001361 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001362
1363 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001364 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001365
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001366 if(target_is_emmc_boot() && hdr->page_size) {
1367 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001368 page_mask = page_size - 1;
1369 }
1370
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001371 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1372 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1373
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001374 /*
1375 * Update the kernel/ramdisk/tags address if the boot image header
1376 * has default values, these default values come from mkbootimg when
1377 * the boot image is flashed using fastboot flash:raw
1378 */
1379 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001380
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001381 /* Get virtual addresses since the hdr saves physical addresses. */
1382 hdr->kernel_addr = VA(hdr->kernel_addr);
1383 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1384 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001385
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001386 /* Check if the addresses in the header are valid. */
1387 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1388 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1389 {
1390 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1391 return -1;
1392 }
1393
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001394 /* sz should have atleast raw boot image */
1395 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001396 fastboot_fail("incomplete bootimage");
1397 return;
1398 }
1399
Amol Jadicb524072012-08-09 16:40:18 -07001400#if DEVICE_TREE
1401 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001402 ret = copy_dtb(data);
1403
1404 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001405#else
1406 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1407 {
1408 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1409 return -1;
1410 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001411#endif
1412
1413 /* Load ramdisk & kernel */
1414 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1415 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1416
1417#if DEVICE_TREE
1418 /*
1419 * If dtb is not found look for appended DTB in the kernel.
1420 * If appended dev tree is found, update the atags with
1421 * memory address to the DTB appended location on RAM.
1422 * Else update with the atags address in the kernel header
1423 */
1424 if (!dtb_copied) {
1425 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001426 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1427 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001428 if (!dtb) {
1429 fastboot_fail("dtb not found");
1430 return;
1431 }
Amol Jadicb524072012-08-09 16:40:18 -07001432 }
1433#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001434
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001435#ifndef DEVICE_TREE
1436 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1437 {
1438 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1439 return -1;
1440 }
1441#endif
1442
Brian Swetland9c4c0752009-01-25 16:23:50 -08001443 fastboot_okay("");
1444 udc_stop();
1445
Dima Zavin77e41f32013-03-06 16:10:43 -08001446 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001447 (const char*) hdr->cmdline, board_machtype(),
1448 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001449}
1450
Dima Zavin214cc642009-01-26 11:16:21 -08001451void cmd_erase(const char *arg, void *data, unsigned sz)
1452{
1453 struct ptentry *ptn;
1454 struct ptable *ptable;
1455
1456 ptable = flash_get_ptable();
1457 if (ptable == NULL) {
1458 fastboot_fail("partition table doesn't exist");
1459 return;
1460 }
1461
1462 ptn = ptable_find(ptable, arg);
1463 if (ptn == NULL) {
1464 fastboot_fail("unknown partition name");
1465 return;
1466 }
1467
1468 if (flash_erase(ptn)) {
1469 fastboot_fail("failed to erase partition");
1470 return;
1471 }
1472 fastboot_okay("");
1473}
1474
Bikas Gurungd48bd242010-09-04 19:54:32 -07001475
1476void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1477{
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001478 BUF_DMA_ALIGN(out, 512);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001479 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001480 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001481
Kinson Chikf1a43512011-07-14 11:28:39 -07001482 index = partition_get_index(arg);
1483 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001484
Kinson Chikf1a43512011-07-14 11:28:39 -07001485 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001486 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001487 return;
1488 }
neetidb4b24d62012-01-20 12:13:09 -08001489 /* Simple inefficient version of erase. Just writing
1490 0 in first block */
1491 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1492 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001493 return;
1494 }
1495 fastboot_okay("");
1496}
1497
1498
Ajay Dudani5c761132011-04-07 20:19:04 -07001499void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001500{
1501 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001502 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001503 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001504
Greg Grisco6e754772011-06-23 12:19:39 -07001505 if (!strcmp(arg, "partition"))
1506 {
1507 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001508 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001509 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001510 return;
1511 }
1512 }
Greg Grisco6e754772011-06-23 12:19:39 -07001513 else
1514 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001515 index = partition_get_index(arg);
1516 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001517 if(ptn == 0) {
1518 fastboot_fail("partition table doesn't exist");
1519 return;
1520 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001521
Greg Grisco6e754772011-06-23 12:19:39 -07001522 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1523 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1524 fastboot_fail("image is not a boot image");
1525 return;
1526 }
1527 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001528
Kinson Chikf1a43512011-07-14 11:28:39 -07001529 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001530 if (ROUND_TO_PAGE(sz,511) > size) {
1531 fastboot_fail("size too large");
1532 return;
1533 }
1534 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1535 fastboot_fail("flash write failure");
1536 return;
1537 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001538 }
1539 fastboot_okay("");
1540 return;
1541}
1542
Ajay Dudani5c761132011-04-07 20:19:04 -07001543void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1544{
1545 unsigned int chunk;
1546 unsigned int chunk_data_sz;
1547 sparse_header_t *sparse_header;
1548 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001549 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001550 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301551 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001552 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001553
Kinson Chikf1a43512011-07-14 11:28:39 -07001554 index = partition_get_index(arg);
1555 ptn = partition_get_offset(index);
1556 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001557 fastboot_fail("partition table doesn't exist");
1558 return;
1559 }
1560
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301561 size = partition_get_size(index);
1562 if (ROUND_TO_PAGE(sz,511) > size) {
1563 fastboot_fail("size too large");
1564 return;
1565 }
1566
Ajay Dudani5c761132011-04-07 20:19:04 -07001567 /* Read and skip over sparse image header */
1568 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001569 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1570 fastboot_fail("size too large");
1571 return;
1572 }
1573
Ajay Dudani5c761132011-04-07 20:19:04 -07001574 data += sparse_header->file_hdr_sz;
1575 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1576 {
1577 /* Skip the remaining bytes in a header that is longer than
1578 * we expected.
1579 */
1580 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1581 }
1582
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001583 dprintf (SPEW, "=== Sparse Image Header ===\n");
1584 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1585 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1586 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1587 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1588 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1589 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1590 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1591 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001592
1593 /* Start processing chunks */
1594 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1595 {
1596 /* Read and skip over chunk header */
1597 chunk_header = (chunk_header_t *) data;
1598 data += sizeof(chunk_header_t);
1599
1600 dprintf (SPEW, "=== Chunk Header ===\n");
1601 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1602 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1603 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1604
1605 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1606 {
1607 /* Skip the remaining bytes in a header that is longer than
1608 * we expected.
1609 */
1610 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1611 }
1612
1613 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1614 switch (chunk_header->chunk_type)
1615 {
1616 case CHUNK_TYPE_RAW:
1617 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1618 chunk_data_sz))
1619 {
1620 fastboot_fail("Bogus chunk size for chunk type Raw");
1621 return;
1622 }
1623
Ajay Dudaniab18f022011-05-12 14:39:22 -07001624 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1625 chunk_data_sz,
1626 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001627 {
1628 fastboot_fail("flash write failure");
1629 return;
1630 }
1631 total_blocks += chunk_header->chunk_sz;
1632 data += chunk_data_sz;
1633 break;
1634
1635 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001636 total_blocks += chunk_header->chunk_sz;
1637 break;
1638
Ajay Dudani5c761132011-04-07 20:19:04 -07001639 case CHUNK_TYPE_CRC:
1640 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1641 {
1642 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1643 return;
1644 }
1645 total_blocks += chunk_header->chunk_sz;
1646 data += chunk_data_sz;
1647 break;
1648
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001649 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001650 fastboot_fail("Unknown chunk type");
1651 return;
1652 }
1653 }
1654
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001655 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1656 total_blocks, sparse_header->total_blks);
1657
1658 if(total_blocks != sparse_header->total_blks)
1659 {
1660 fastboot_fail("sparse image write failure");
1661 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001662
1663 fastboot_okay("");
1664 return;
1665}
1666
1667void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1668{
1669 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001670 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1671 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001672
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001673#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001674 int ret=0;
1675 uint32 major_version=0;
1676 uint32 minor_version=0;
1677
1678 ret = scm_svc_version(&major_version,&minor_version);
1679 if(!ret)
1680 {
1681 if(major_version >= 2)
1682 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001683 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001684 {
1685 ret = encrypt_scm((uint32 **) &data, &sz);
1686 if (ret != 0) {
1687 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1688 return;
1689 }
1690
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001691 /* Protect only for SSD */
1692 if (!strcmp(arg, "ssd")) {
1693 ret = scm_protect_keystore((uint32 *) data, sz);
1694 if (ret != 0) {
1695 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1696 return;
1697 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001698 }
1699 }
1700 else
1701 {
1702 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1703 if(ret != 0)
1704 {
1705 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1706 return;
1707 }
1708 }
1709 }
1710 else
1711 {
1712 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1713 magic_number[1] == DECRYPT_MAGIC_1)
1714 {
1715 ret = decrypt_scm((uint32 **) &data, &sz);
1716 if (ret != 0) {
1717 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1718 return;
1719 }
1720 }
1721 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1722 magic_number[1] == ENCRYPT_MAGIC_1)
1723 {
1724 ret = encrypt_scm((uint32 **) &data, &sz);
1725 if (ret != 0) {
1726 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1727 return;
1728 }
1729 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001730 }
1731 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001732 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001733 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001734 dprintf(CRITICAL,"INVALID SVC Version\n");
1735 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001736 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001737#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001738
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001739 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001740 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1741 cmd_flash_mmc_img(arg, data, sz);
1742 else
1743 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001744 return;
1745}
1746
Dima Zavin214cc642009-01-26 11:16:21 -08001747void cmd_flash(const char *arg, void *data, unsigned sz)
1748{
1749 struct ptentry *ptn;
1750 struct ptable *ptable;
1751 unsigned extra = 0;
1752
1753 ptable = flash_get_ptable();
1754 if (ptable == NULL) {
1755 fastboot_fail("partition table doesn't exist");
1756 return;
1757 }
1758
1759 ptn = ptable_find(ptable, arg);
1760 if (ptn == NULL) {
1761 fastboot_fail("unknown partition name");
1762 return;
1763 }
1764
1765 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1766 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1767 fastboot_fail("image is not a boot image");
1768 return;
1769 }
1770 }
1771
Amol Jadi5c61a952012-05-04 17:05:35 -07001772 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001773 || !strcmp(ptn->name, "userdata")
1774 || !strcmp(ptn->name, "persist")
1775 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301776 if (flash_ecc_bch_enabled())
1777 /* Spare data bytes for 8 bit ECC increased by 4 */
1778 extra = ((page_size >> 9) * 20);
1779 else
1780 extra = ((page_size >> 9) * 16);
1781 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001782 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001783
1784 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1785 if (flash_write(ptn, extra, data, sz)) {
1786 fastboot_fail("flash write failure");
1787 return;
1788 }
1789 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1790 fastboot_okay("");
1791}
1792
1793void cmd_continue(const char *arg, void *data, unsigned sz)
1794{
1795 fastboot_okay("");
1796 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001797 if (target_is_emmc_boot())
1798 {
1799 boot_linux_from_mmc();
1800 }
1801 else
1802 {
1803 boot_linux_from_flash();
1804 }
Dima Zavin214cc642009-01-26 11:16:21 -08001805}
1806
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001807void cmd_reboot(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(0);
1812}
1813
1814void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1815{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001816 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001817 fastboot_okay("");
1818 reboot_device(FASTBOOT_MODE);
1819}
1820
Shashank Mittal162244e2011-08-08 19:01:25 -07001821void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1822{
1823 if(!device.is_unlocked)
1824 {
1825 device.is_unlocked = 1;
1826 write_device_info(&device);
1827 }
1828 fastboot_okay("");
1829}
1830
Shashank Mittala0032282011-08-26 14:50:11 -07001831void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1832{
1833 char response[64];
1834 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1835 fastboot_info(response);
1836 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1837 fastboot_info(response);
1838 fastboot_okay("");
1839}
1840
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001841void cmd_preflash(const char *arg, void *data, unsigned sz)
1842{
1843 fastboot_okay("");
1844}
1845
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001846void splash_screen ()
1847{
1848 struct ptentry *ptn;
1849 struct ptable *ptable;
1850 struct fbcon_config *fb_display = NULL;
1851
1852 if (!target_is_emmc_boot())
1853 {
1854 ptable = flash_get_ptable();
1855 if (ptable == NULL) {
1856 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001857 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001858 }
1859
1860 ptn = ptable_find(ptable, "splash");
1861 if (ptn == NULL) {
1862 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1863 } else {
1864 fb_display = fbcon_display();
1865 if (fb_display) {
1866 if (flash_read(ptn, 0, fb_display->base,
1867 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1868 fbcon_clear();
1869 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1870 }
1871 }
1872 }
1873 }
1874}
1875
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001876/* Get the size from partiton name */
1877static void get_partition_size(const char *arg, char *response)
1878{
1879 uint64_t ptn = 0;
1880 uint64_t size;
1881 int index = INVALID_PTN;
1882
1883 index = partition_get_index(arg);
1884
1885 if (index == INVALID_PTN)
1886 {
1887 dprintf(CRITICAL, "Invalid partition index\n");
1888 return;
1889 }
1890
1891 ptn = partition_get_offset(index);
1892
1893 if(!ptn)
1894 {
1895 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1896 return;
1897 }
1898
1899 size = partition_get_size(index);
1900
1901 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1902 return;
1903}
1904
1905/*
1906 * Publish the partition type & size info
1907 * fastboot getvar will publish the required information.
1908 * fastboot getvar partition_size:<partition_name>: partition size in hex
1909 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1910 */
1911static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1912{
1913 uint8_t i;
1914
1915 for (i = 0; i < num_parts; i++) {
1916 get_partition_size(info[i].part_name, info[i].size_response);
1917
1918 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1919 {
1920 dprintf(CRITICAL, "partition size name truncated\n");
1921 return;
1922 }
1923 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1924 {
1925 dprintf(CRITICAL, "partition type name truncated\n");
1926 return;
1927 }
1928
1929 /* publish partition size & type info */
1930 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1931 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1932 }
1933}
1934
Brian Swetland9c4c0752009-01-25 16:23:50 -08001935void aboot_init(const struct app_descriptor *app)
1936{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001937 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001938 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001939 unsigned sz = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03001940 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001941
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001942 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001943 if (target_is_emmc_boot())
1944 {
1945 page_size = 2048;
1946 page_mask = page_size - 1;
1947 }
1948 else
1949 {
1950 page_size = flash_page_size();
1951 page_mask = page_size - 1;
1952 }
1953
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001954 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
1955
Shashank Mittal162244e2011-08-08 19:01:25 -07001956 if(target_use_signed_kernel())
1957 {
1958 read_device_info(&device);
1959
Shashank Mittal162244e2011-08-08 19:01:25 -07001960 }
1961
Greg Griscod6250552011-06-29 14:40:23 -07001962 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001963 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001964 surf_udc_device.serialno = sn_buf;
1965
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001966 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001967 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001968 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001969 dprintf(ALWAYS,"dload mode key sequence detected\n");
1970 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001971 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001972 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001973 }
1974 else
1975 {
1976 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001977 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001978 }
1979 boot_into_fastboot = true;
1980 }
1981 if (!boot_into_fastboot)
1982 {
1983 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
1984 boot_into_recovery = 1;
1985 if (!boot_into_recovery &&
1986 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03001987 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001988 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001989 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001990 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03001991 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001992 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001993
Ajay Dudani77421292010-10-27 19:34:06 -07001994 reboot_mode = check_reboot_mode();
1995 if (reboot_mode == RECOVERY_MODE) {
1996 boot_into_recovery = 1;
1997 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03001998 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07001999 }
2000
Pavel Nedev5d91d412013-04-29 11:34:24 +03002001 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002002 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002003 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002004 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002005 if(emmc_recovery_init())
2006 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2007 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002008 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002009 if((device.is_unlocked) || (device.is_tampered))
2010 {
2011 #ifdef TZ_TAMPER_FUSE
2012 set_tamper_fuse_cmd();
2013 #endif
2014 #if USE_PCOM_SECBOOT
2015 set_tamper_flag(device.is_tampered);
2016 #endif
2017 }
Shashank Mittala0032282011-08-26 14:50:11 -07002018 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002019 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002020 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002021 else
2022 {
2023 recovery_init();
2024 #if USE_PCOM_SECBOOT
2025 if((device.is_unlocked) || (device.is_tampered))
2026 set_tamper_flag(device.is_tampered);
2027 #endif
2028 boot_linux_from_flash();
2029 }
2030 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2031 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002032 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002033
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002034 sz = target_get_max_flash_size();
2035
Shashank Mittal162244e2011-08-08 19:01:25 -07002036 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002037
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002038 if(!usb_init)
2039 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002040
2041 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002042
Shashank Mittal23b8f422010-04-16 19:27:21 -07002043 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002044 {
2045 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002046 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002047 }
2048 else
2049 {
2050 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002051 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002052 }
2053
2054 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002055 fastboot_register("reboot", cmd_reboot);
2056 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07002057 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07002058 fastboot_register("oem device-info", cmd_oem_devinfo);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002059 fastboot_register("preflash", cmd_preflash);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07002060 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08002061 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08002062 fastboot_publish("serialno", sn_buf);
Channagoud Kadabi873d8ff2013-07-01 17:31:40 -07002063 /*
2064 * fastboot publish is supported only for emmc partitions
2065 * Calling this for NAND prints some error messages which
2066 * is harmless but misleading. Avoid calling this for NAND
2067 * devices.
2068 */
2069 if (target_is_emmc_boot())
2070 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002071 /* Max download size supported */
2072 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", sz);
2073 fastboot_publish("max-download-size", (const char *) max_download_size);
Kinson Chikf1a43512011-07-14 11:28:39 -07002074 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07002075 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002076 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002077}
2078
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002079uint32_t get_page_size()
2080{
2081 return page_size;
2082}
2083
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002084/*
2085 * Calculated and save hash (SHA256) for non-signed boot image.
2086 *
2087 * Hash the same data that is checked on the signed boot image.
2088 * Kernel and Ramdisk are already read to memory buffers.
2089 * Need to read the entire device-tree from mmc
2090 * since non-signed image only read the DT tags of the relevant platform.
2091 *
2092 * @param kernel_addr - kernel bufer
2093 * @param kernel_actual - kernel size in bytes
2094 * @param ramdisk_addr - ramdisk buffer
2095 * @param ramdisk_actual - ramdisk size
2096 * @param ptn - partition
2097 * @param dt_offset - device tree offset on mmc partition
2098 * @param dt_size
2099 *
2100 * @return int - 0 on success, negative value on failure.
2101 */
2102int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2103 void *ramdisk_addr, unsigned ramdisk_actual,
2104 unsigned long long ptn,
2105 unsigned dt_offset, unsigned dt_size)
2106{
2107 SHA256_CTX sha256_ctx;
2108 char digest[32]={0};
2109 char *buf = (char *)target_get_scratch_address();
2110 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2111 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2112
2113 SHA256_Init(&sha256_ctx);
2114
2115 /* Read Boot Header */
2116 if (mmc_read(ptn, buf, page_size))
2117 {
2118 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2119 return -1;
2120 }
2121 /* Read entire Device Tree */
2122 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2123 {
2124 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2125 return -1;
2126 }
2127 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2128 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2129 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2130 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2131
2132 SHA256_Final(digest, &sha256_ctx);
2133
2134 save_kernel_hash_cmd(digest);
2135 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2136
2137 return 0;
2138}
2139
Brian Swetland9c4c0752009-01-25 16:23:50 -08002140APP_START(aboot)
2141 .init = aboot_init,
2142APP_END