blob: 59f43ddd21683938a01b0a07d1a2b92db1b0b1cd [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Amol Jadi2a15a272013-01-22 12:03:36 -08005 * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
36#include <dev/udc.h>
37#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070038#include <stdlib.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
43#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080044#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080045#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080046#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070047#include <target.h>
48#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070049#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070050#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070051#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070052#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070053#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030054#include <sha.h>
Dima Zavin214cc642009-01-26 11:16:21 -080055
Neeti Desai17379b82012-06-04 18:42:53 -070056#if DEVICE_TREE
57#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070058#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070059#endif
60
Shashank Mittalcd98d472011-08-02 14:29:24 -070061#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080062#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080063#include "bootimg.h"
64#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070065#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070066#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070067#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070068#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080069
Shashank Mittal162244e2011-08-08 19:01:25 -070070#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070071
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070072extern bool target_use_signed_kernel(void);
73extern void dsb();
74extern void isb();
75extern void platform_uninit(void);
76
77void write_device_info_mmc(device_info *dev);
78void write_device_info_flash(device_info *dev);
79
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070080#define EXPAND(NAME) #NAME
81#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070082
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080083#ifdef MEMBASE
84#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
85#else
David Ng183a7422009-12-07 14:55:21 -080086#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080087#endif
88
Deepa Dinamani0e163a42013-05-24 17:08:15 -070089#ifndef MEMSIZE
90#define MEMSIZE 1024*1024
91#endif
92
93#define MAX_TAGS_SIZE 1024
94
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080095#define RECOVERY_MODE 0x77665502
96#define FASTBOOT_MODE 0x77665500
97
David Ng183a7422009-12-07 14:55:21 -080098static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080099static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300100static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800101static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800102static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700103static const char *auth_kernel = " androidboot.authorized_kernel=true";
David Ng183a7422009-12-07 14:55:21 -0800104
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800105static const char *baseband_apq = " androidboot.baseband=apq";
106static const char *baseband_msm = " androidboot.baseband=msm";
107static const char *baseband_csfb = " androidboot.baseband=csfb";
108static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700109static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700110static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800111static const char *baseband_dsda = " androidboot.baseband=dsda";
112static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800113static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800114
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700115static unsigned page_size = 0;
116static unsigned page_mask = 0;
117static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
118static bool boot_into_ffbm;
119
Shashank Mittalcd98d472011-08-02 14:29:24 -0700120/* Assuming unauthorized kernel image by default */
121static int auth_kernel_img = 0;
122
Shashank Mittal162244e2011-08-08 19:01:25 -0700123static device_info device = {DEVICE_MAGIC, 0, 0};
124
Brian Swetland9c4c0752009-01-25 16:23:50 -0800125static struct udc_device surf_udc_device = {
126 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -0800127 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800128 .version_id = 0x0100,
129 .manufacturer = "Google",
130 .product = "Android",
131};
132
Dima Zavin42168f22009-01-30 11:52:22 -0800133struct atag_ptbl_entry
134{
135 char name[16];
136 unsigned offset;
137 unsigned size;
138 unsigned flags;
139};
140
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700141/*
142 * Partition info, required to be published
143 * for fastboot
144 */
145struct getvar_partition_info {
146 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
147 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
148 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
149 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
150 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
151};
152
153/*
154 * Right now, we are publishing the info for only
155 * three partitions
156 */
157struct getvar_partition_info part_info[] =
158{
159 { "system" , "partition-size:", "partition-type:", "", "ext4" },
160 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
161 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
162};
163
164char max_download_size[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800165char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700166
Greg Griscod2471ef2011-07-14 13:00:42 -0700167extern int emmc_recovery_init(void);
168
Kinson Chik0b1c8162011-08-31 16:31:57 -0700169#if NO_KEYPAD_DRIVER
170extern int fastboot_trigger(void);
171#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700172
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700173static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
174{
175 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700176#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
177 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
178 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
179 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700180#endif
181}
182
Dima Zavin42168f22009-01-30 11:52:22 -0800183static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
184{
185 struct atag_ptbl_entry atag_ptn;
186
187 memcpy(atag_ptn.name, ptn->name, 16);
188 atag_ptn.name[15] = '\0';
189 atag_ptn.offset = ptn->start;
190 atag_ptn.size = ptn->length;
191 atag_ptn.flags = ptn->flags;
192 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
193 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
194}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800195
Neeti Desaie245d492012-06-01 12:52:13 -0700196unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800197{
David Ng183a7422009-12-07 14:55:21 -0800198 int cmdline_len = 0;
199 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800200 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700201 int pause_at_bootup = 0;
Dima Zavin42168f22009-01-30 11:52:22 -0800202
Brian Swetland9c4c0752009-01-25 16:23:50 -0800203 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800204 cmdline_len = strlen(cmdline);
205 have_cmdline = 1;
206 }
207 if (target_is_emmc_boot()) {
208 cmdline_len += strlen(emmc_cmdline);
209 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800210
211 cmdline_len += strlen(usb_sn_cmdline);
212 cmdline_len += strlen(sn_buf);
213
Pavel Nedev328ac822013-04-05 15:25:11 +0300214 if (boot_into_ffbm) {
215 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700216 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800217 /* reduce kernel console messages to speed-up boot */
218 cmdline_len += strlen(loglevel);
Pavel Nedev328ac822013-04-05 15:25:11 +0300219 } else if (target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700220 pause_at_bootup = 1;
221 cmdline_len += strlen(battchg_pause);
222 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800223
Shashank Mittalcd98d472011-08-02 14:29:24 -0700224 if(target_use_signed_kernel() && auth_kernel_img) {
225 cmdline_len += strlen(auth_kernel);
226 }
227
Ajay Dudanid04110c2011-01-17 23:55:07 -0800228 /* Determine correct androidboot.baseband to use */
229 switch(target_baseband())
230 {
231 case BASEBAND_APQ:
232 cmdline_len += strlen(baseband_apq);
233 break;
234
235 case BASEBAND_MSM:
236 cmdline_len += strlen(baseband_msm);
237 break;
238
239 case BASEBAND_CSFB:
240 cmdline_len += strlen(baseband_csfb);
241 break;
242
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800243 case BASEBAND_SVLTE2A:
244 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800245 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700246
247 case BASEBAND_MDM:
248 cmdline_len += strlen(baseband_mdm);
249 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700250
251 case BASEBAND_SGLTE:
252 cmdline_len += strlen(baseband_sglte);
253 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530254
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800255 case BASEBAND_SGLTE2:
256 cmdline_len += strlen(baseband_sglte2);
257 break;
258
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530259 case BASEBAND_DSDA:
260 cmdline_len += strlen(baseband_dsda);
261 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800262
263 case BASEBAND_DSDA2:
264 cmdline_len += strlen(baseband_dsda2);
265 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800266 }
267
David Ng183a7422009-12-07 14:55:21 -0800268 if (cmdline_len > 0) {
269 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700270 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
271 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700272
Amol Jadi168b7712012-03-06 16:15:00 -0800273 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700274 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800275 if (have_cmdline) {
276 src = cmdline;
277 while ((*dst++ = *src++));
278 }
279 if (target_is_emmc_boot()) {
280 src = emmc_cmdline;
281 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700282 have_cmdline = 1;
283 while ((*dst++ = *src++));
284 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800285
286 src = usb_sn_cmdline;
287 if (have_cmdline) --dst;
288 have_cmdline = 1;
289 while ((*dst++ = *src++));
290 src = sn_buf;
291 if (have_cmdline) --dst;
292 have_cmdline = 1;
293 while ((*dst++ = *src++));
294
Pavel Nedev328ac822013-04-05 15:25:11 +0300295 if (boot_into_ffbm) {
296 src = androidboot_mode;
297 if (have_cmdline) --dst;
298 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700299 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300300 if (have_cmdline) --dst;
301 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800302 src = loglevel;
303 if (have_cmdline) --dst;
304 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300305 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700306 src = battchg_pause;
307 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800308 while ((*dst++ = *src++));
309 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800310
Shashank Mittalcd98d472011-08-02 14:29:24 -0700311 if(target_use_signed_kernel() && auth_kernel_img) {
312 src = auth_kernel;
313 if (have_cmdline) --dst;
314 while ((*dst++ = *src++));
315 }
316
Ajay Dudanid04110c2011-01-17 23:55:07 -0800317 switch(target_baseband())
318 {
319 case BASEBAND_APQ:
320 src = baseband_apq;
321 if (have_cmdline) --dst;
322 while ((*dst++ = *src++));
323 break;
324
325 case BASEBAND_MSM:
326 src = baseband_msm;
327 if (have_cmdline) --dst;
328 while ((*dst++ = *src++));
329 break;
330
331 case BASEBAND_CSFB:
332 src = baseband_csfb;
333 if (have_cmdline) --dst;
334 while ((*dst++ = *src++));
335 break;
336
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800337 case BASEBAND_SVLTE2A:
338 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800339 if (have_cmdline) --dst;
340 while ((*dst++ = *src++));
341 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700342
343 case BASEBAND_MDM:
344 src = baseband_mdm;
345 if (have_cmdline) --dst;
346 while ((*dst++ = *src++));
347 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700348
349 case BASEBAND_SGLTE:
350 src = baseband_sglte;
351 if (have_cmdline) --dst;
352 while ((*dst++ = *src++));
353 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530354
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800355 case BASEBAND_SGLTE2:
356 src = baseband_sglte2;
357 if (have_cmdline) --dst;
358 while ((*dst++ = *src++));
359 break;
360
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530361 case BASEBAND_DSDA:
362 src = baseband_dsda;
363 if (have_cmdline) --dst;
364 while ((*dst++ = *src++));
365 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800366
367 case BASEBAND_DSDA2:
368 src = baseband_dsda2;
369 if (have_cmdline) --dst;
370 while ((*dst++ = *src++));
371 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800372 }
Neeti Desaie245d492012-06-01 12:52:13 -0700373 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800374 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700375 return cmdline_final;
376}
377
378unsigned *atag_core(unsigned *ptr)
379{
380 /* CORE */
381 *ptr++ = 2;
382 *ptr++ = 0x54410001;
383
384 return ptr;
385
386}
387
388unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
389 unsigned ramdisk_size)
390{
391 if (ramdisk_size) {
392 *ptr++ = 4;
393 *ptr++ = 0x54420005;
394 *ptr++ = (unsigned)ramdisk;
395 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800396 }
397
Neeti Desaie245d492012-06-01 12:52:13 -0700398 return ptr;
399}
400
401unsigned *atag_ptable(unsigned **ptr_addr)
402{
403 int i;
404 struct ptable *ptable;
405
406 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700407 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
408 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700409 *(*ptr_addr)++ = 0x4d534d70;
410 for (i = 0; i < ptable->count; ++i)
411 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
412 }
413
414 return (*ptr_addr);
415}
416
417unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
418{
419 int cmdline_length = 0;
420 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700421 char *dest;
422
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800423 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700424 n = (cmdline_length + 4) & (~3);
425
426 *ptr++ = (n / 4) + 2;
427 *ptr++ = 0x54410009;
428 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800429 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700430 ptr += (n / 4);
431
432 return ptr;
433}
434
435unsigned *atag_end(unsigned *ptr)
436{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800437 /* END */
438 *ptr++ = 0;
439 *ptr++ = 0;
440
Neeti Desaie245d492012-06-01 12:52:13 -0700441 return ptr;
442}
443
444void generate_atags(unsigned *ptr, const char *cmdline,
445 void *ramdisk, unsigned ramdisk_size)
446{
447
448 ptr = atag_core(ptr);
449 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
450 ptr = target_atag_mem(ptr);
451
452 /* Skip NAND partition ATAGS for eMMC boot */
453 if (!target_is_emmc_boot()){
454 ptr = atag_ptable(&ptr);
455 }
456
457 ptr = atag_cmdline(ptr, cmdline);
458 ptr = atag_end(ptr);
459}
460
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700461typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700462void boot_linux(void *kernel, unsigned *tags,
463 const char *cmdline, unsigned machtype,
464 void *ramdisk, unsigned ramdisk_size)
465{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800466 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800467#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700468 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800469#endif
470
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700471 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800472 uint32_t tags_phys = PA((addr_t)tags);
473
474 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700475
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800476 final_cmdline = update_cmdline((const char*)cmdline);
477
Neeti Desai17379b82012-06-04 18:42:53 -0700478#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800479 dprintf(INFO, "Updating device tree: start\n");
480
Neeti Desai17379b82012-06-04 18:42:53 -0700481 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800482 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700483 if(ret)
484 {
485 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
486 ASSERT(0);
487 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800488 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700489#else
Neeti Desaie245d492012-06-01 12:52:13 -0700490 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800491 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700492#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700493
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800494 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
495 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800496
497 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700498
Amol Jadi4421e652011-06-16 15:00:48 -0700499 /* do any platform specific cleanup before kernel entry */
500 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700501
Brian Swetland9c4c0752009-01-25 16:23:50 -0800502 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700503
Amol Jadi504f9fe2012-08-16 13:56:48 -0700504#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800505 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700506#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700507 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700508 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800509}
510
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700511/* Function to check if the memory address range falls within the aboot
512 * boundaries.
513 * start: Start of the memory region
514 * size: Size of the memory region
515 */
516int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
517{
518 /* Check for boundary conditions. */
519 if ((start + size) < start)
520 return -1;
521
522 /* Check for memory overlap. */
523 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
524 return 0;
525 else if (start > (MEMBASE + MEMSIZE))
526 return 0;
527 else
528 return -1;
529}
530
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800531#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800532
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700533BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800534#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700535BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800536#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800537
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700538static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
539{
540 int ret;
541
542 /* Assume device is rooted at this time. */
543 device.is_tampered = 1;
544
545 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
546
547 ret = image_verify((unsigned char *)bootimg_addr,
548 (unsigned char *)(bootimg_addr + bootimg_size),
549 bootimg_size,
550 CRYPTO_AUTH_ALG_SHA256);
551
552 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
553
554 if (ret)
555 {
556 /* Authorized kernel */
557 device.is_tampered = 0;
558 }
559
560#if USE_PCOM_SECBOOT
561 set_tamper_flag(device.is_tampered);
562#endif
563
564 if(device.is_tampered)
565 {
566 write_device_info_mmc(&device);
567 #ifdef TZ_TAMPER_FUSE
568 set_tamper_fuse_cmd();
569 #endif
570 #ifdef ASSERT_ON_TAMPER
571 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
572 ASSERT(0);
573 #endif
574 }
575}
576
Shashank Mittal23b8f422010-04-16 19:27:21 -0700577int boot_linux_from_mmc(void)
578{
579 struct boot_img_hdr *hdr = (void*) buf;
580 struct boot_img_hdr *uhdr;
581 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700582 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700583 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700584 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700585
Shashank Mittalcd98d472011-08-02 14:29:24 -0700586 unsigned char *image_addr = 0;
587 unsigned kernel_actual;
588 unsigned ramdisk_actual;
589 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700590 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700591
592#if DEVICE_TREE
593 struct dt_table *table;
594 struct dt_entry *dt_entry_ptr;
595 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800596 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700597#endif
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700598 if (!boot_into_recovery) {
599 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
600 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
601 if (rcode <= 0) {
602 boot_into_ffbm = false;
603 if (rcode < 0)
604 dprintf(CRITICAL,"failed to get ffbm cookie");
605 } else
606 boot_into_ffbm = true;
607 } else
608 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700609 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
610 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
611 dprintf(INFO, "Unified boot method!\n");
612 hdr = uhdr;
613 goto unified_boot;
614 }
Greg Griscod6250552011-06-29 14:40:23 -0700615 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700616 index = partition_get_index("boot");
617 ptn = partition_get_offset(index);
618 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700619 dprintf(CRITICAL, "ERROR: No boot partition found\n");
620 return -1;
621 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700622 }
623 else {
624 index = partition_get_index("recovery");
625 ptn = partition_get_offset(index);
626 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700627 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
628 return -1;
629 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700630 }
631
Greg Griscod6250552011-06-29 14:40:23 -0700632 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700633 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
634 return -1;
635 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700636
637 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700638 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700639 return -1;
640 }
641
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700642 if (hdr->page_size && (hdr->page_size != page_size)) {
643 page_size = hdr->page_size;
644 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700645 }
646
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700647 /*
648 * Update the kernel/ramdisk/tags address if the boot image header
649 * has default values, these default values come from mkbootimg when
650 * the boot image is flashed using fastboot flash:raw
651 */
652 update_ker_tags_rdisk_addr(hdr);
653
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700654 /* Get virtual addresses since the hdr saves physical addresses. */
655 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
656 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
657 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700658
659 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
660 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
661
662 /* Check if the addresses in the header are valid. */
663 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
664 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
665 {
666 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
667 return -1;
668 }
669
670#ifndef DEVICE_TREE
671 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
672 {
673 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
674 return -1;
675 }
676#endif
677
Shashank Mittalcd98d472011-08-02 14:29:24 -0700678 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300679 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
680 (int) target_use_signed_kernel(),
681 device.is_unlocked,
682 device.is_tampered);
683
Shashank Mittala0032282011-08-26 14:50:11 -0700684 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800685 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700686 offset = 0;
687
Shashank Mittalcd98d472011-08-02 14:29:24 -0700688 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700689
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800690#if DEVICE_TREE
691 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
692 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700693
694 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
695 {
696 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
697 return -1;
698 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800699#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530700 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700701
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700702#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700703
Amol Jadib6be5c12012-11-14 13:39:51 -0800704 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700705 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800706
Shashank Mittalcd98d472011-08-02 14:29:24 -0700707 /* Read image without signature */
708 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
709 {
710 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
711 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800712 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700713
Amol Jadib6be5c12012-11-14 13:39:51 -0800714 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700715 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800716
Shashank Mittalcd98d472011-08-02 14:29:24 -0700717 offset = imagesize_actual;
718 /* Read signature */
719 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
720 {
721 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700722 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700723 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800724
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700725 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700726
Neeti Desai465491e2012-07-31 12:53:35 -0700727 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700728 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
729 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700730
Neeti Desai465491e2012-07-31 12:53:35 -0700731 #if DEVICE_TREE
732 if(hdr->dt_size) {
733 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700734 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700735
736 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
737
738 /* Restriction that the device tree entry table should be less than a page*/
739 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
740
741 /* Validate the device tree table header */
742 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
743 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
744 return -1;
745 }
746
747 /* Find index of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700748 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700749 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
750 return -1;
751 }
752
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700753 /* Validate and Read device device tree in the "tags_add */
754 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry_ptr->size))
755 {
756 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
757 return -1;
758 }
759
Neeti Desai465491e2012-07-31 12:53:35 -0700760 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800761 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800762 /*
763 * If appended dev tree is found, update the atags with
764 * memory address to the DTB appended location on RAM.
765 * Else update with the atags address in the kernel header
766 */
767 void *dtb;
768 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700769 (void *)hdr->tags_addr, hdr->kernel_size);
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
823 /* Restriction that the device tree entry table should be less than a page*/
824 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
825
826 /* Validate the device tree table header */
827 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
828 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
829 return -1;
830 }
831
832 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700833 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700834 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
835 return -1;
836 }
837
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700838 /* Validate and Read device device tree in the "tags_add */
839 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry_ptr->size))
840 {
841 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
842 return -1;
843 }
844
Neeti Desai465491e2012-07-31 12:53:35 -0700845 if(mmc_read(ptn + offset + dt_entry_ptr->offset,
846 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
847 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
848 return -1;
849 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300850 #ifdef TZ_SAVE_KERNEL_HASH
851 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
852 hdr->ramdisk_addr, ramdisk_actual,
853 ptn, offset, hdr->dt_size);
854 #endif /* TZ_SAVE_KERNEL_HASH */
855
Channagoud Kadabi35095622013-03-01 13:53:05 -0800856 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800857 /*
858 * If appended dev tree is found, update the atags with
859 * memory address to the DTB appended location on RAM.
860 * Else update with the atags address in the kernel header
861 */
862 void *dtb;
863 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700864 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800865 if (!dtb) {
866 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
867 return -1;
868 }
Neeti Desai465491e2012-07-31 12:53:35 -0700869 }
870 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700871 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700872
873unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700874
Dima Zavin77e41f32013-03-06 16:10:43 -0800875 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700876 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700877 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
878
879 return 0;
880}
881
Dima Zavin214cc642009-01-26 11:16:21 -0800882int boot_linux_from_flash(void)
883{
884 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800885 struct ptentry *ptn;
886 struct ptable *ptable;
887 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800888
Shashank Mittalcd98d472011-08-02 14:29:24 -0700889 unsigned char *image_addr = 0;
890 unsigned kernel_actual;
891 unsigned ramdisk_actual;
892 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800893 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700894
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700895#if DEVICE_TREE
896 struct dt_table *table;
897 struct dt_entry *dt_entry_ptr;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800898 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700899#endif
900
David Ng183a7422009-12-07 14:55:21 -0800901 if (target_is_emmc_boot()) {
902 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
903 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
904 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
905 return -1;
906 }
907 goto continue_boot;
908 }
909
Dima Zavin214cc642009-01-26 11:16:21 -0800910 ptable = flash_get_ptable();
911 if (ptable == NULL) {
912 dprintf(CRITICAL, "ERROR: Partition table not found\n");
913 return -1;
914 }
915
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800916 if(!boot_into_recovery)
917 {
918 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700919
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800920 if (ptn == NULL) {
921 dprintf(CRITICAL, "ERROR: No boot partition found\n");
922 return -1;
923 }
924 }
925 else
926 {
927 ptn = ptable_find(ptable, "recovery");
928 if (ptn == NULL) {
929 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
930 return -1;
931 }
Dima Zavin214cc642009-01-26 11:16:21 -0800932 }
933
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800934 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800935 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
936 return -1;
937 }
Dima Zavin214cc642009-01-26 11:16:21 -0800938
939 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700940 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800941 return -1;
942 }
943
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800944 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700945 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 -0800946 return -1;
947 }
948
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700949 /*
950 * Update the kernel/ramdisk/tags address if the boot image header
951 * has default values, these default values come from mkbootimg when
952 * the boot image is flashed using fastboot flash:raw
953 */
954 update_ker_tags_rdisk_addr(hdr);
955
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700956 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700957 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
958 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
959 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
960
961 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
962 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
963
964 /* Check if the addresses in the header are valid. */
965 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
966 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
967 {
968 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
969 return -1;
970 }
971
972#ifndef DEVICE_TREE
973 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
974 {
975 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
976 return -1;
977 }
978#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700979
Shashank Mittalcd98d472011-08-02 14:29:24 -0700980 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700981 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700982 {
983 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700984 offset = 0;
985
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800986#if DEVICE_TREE
987 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
988 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700989
990 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
991 {
992 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
993 return -1;
994 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800995#else
Shashank Mittalcd98d472011-08-02 14:29:24 -0700996 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800997#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700998
Amol Jadib6be5c12012-11-14 13:39:51 -0800999 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001000 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001001
Shashank Mittalcd98d472011-08-02 14:29:24 -07001002 /* Read image without signature */
1003 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1004 {
1005 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1006 return -1;
1007 }
Dima Zavin214cc642009-01-26 11:16:21 -08001008
Amol Jadib6be5c12012-11-14 13:39:51 -08001009 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001010 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001011
Shashank Mittalcd98d472011-08-02 14:29:24 -07001012 offset = imagesize_actual;
1013 /* Read signature */
1014 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1015 {
1016 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001017 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001018 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001019
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001020 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001021
1022 /* Move kernel and ramdisk to correct address */
1023 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1024 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001025#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001026 /* Validate and Read device device tree in the "tags_add */
1027 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry_ptr->size))
1028 {
1029 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1030 return -1;
1031 }
1032
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001033 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1034#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001035
1036 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001037 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001038 {
1039 write_device_info_flash(&device);
1040 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301041#if USE_PCOM_SECBOOT
1042 set_tamper_flag(device.is_tampered);
1043#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001044 }
1045 else
1046 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001047 offset = page_size;
1048
Amol Jadib6be5c12012-11-14 13:39:51 -08001049 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1050 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1051 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1052
1053 dprintf(INFO, "Loading boot image (%d): start\n",
1054 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001055 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001056
1057 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001058 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1059 return -1;
1060 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001061 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001062
Amol Jadib6be5c12012-11-14 13:39:51 -08001063 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001064 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1065 return -1;
1066 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001067 offset += ramdisk_actual;
1068
1069 dprintf(INFO, "Loading boot image (%d): done\n",
1070 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001071 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001072
1073 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001074 offset += second_actual;
1075 /* Second image loading not implemented. */
1076 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001077 }
1078
1079#if DEVICE_TREE
1080 if(hdr->dt_size != 0) {
1081
1082 /* Read the device tree table into buffer */
1083 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1084 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1085 return -1;
1086 }
1087
1088 table = (struct dt_table*) dt_buf;
1089
1090 /* Restriction that the device tree entry table should be less than a page*/
1091 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1092
1093 /* Validate the device tree table header */
1094 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1095 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1096 return -1;
1097 }
1098
1099 /* Calculate the offset of device tree within device tree table */
1100 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
1101 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1102 return -1;
1103 }
1104
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001105 /* Validate and Read device device tree in the "tags_add */
1106 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry_ptr->size))
1107 {
1108 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1109 return -1;
1110 }
1111
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001112 /* Read device device tree in the "tags_add */
1113 if(flash_read(ptn, offset + dt_entry_ptr->offset,
1114 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
1115 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1116 return -1;
1117 }
1118 }
1119#endif
1120
Shashank Mittalcd98d472011-08-02 14:29:24 -07001121 }
David Ng183a7422009-12-07 14:55:21 -08001122continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001123
Dima Zavin214cc642009-01-26 11:16:21 -08001124 /* TODO: create/pass atags to kernel */
1125
Ajay Dudanie28a6072011-07-01 13:59:46 -07001126 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001127 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001128 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1129
1130 return 0;
1131}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001132
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001133BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001134void write_device_info_mmc(device_info *dev)
1135{
1136 struct device_info *info = (void*) info_buf;
1137 unsigned long long ptn = 0;
1138 unsigned long long size;
1139 int index = INVALID_PTN;
1140
1141 index = partition_get_index("aboot");
1142 ptn = partition_get_offset(index);
1143 if(ptn == 0)
1144 {
1145 return;
1146 }
1147
1148 size = partition_get_size(index);
1149
1150 memcpy(info, dev, sizeof(device_info));
1151
1152 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1153 {
1154 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1155 return;
1156 }
1157}
1158
1159void read_device_info_mmc(device_info *dev)
1160{
1161 struct device_info *info = (void*) info_buf;
1162 unsigned long long ptn = 0;
1163 unsigned long long size;
1164 int index = INVALID_PTN;
1165
1166 index = partition_get_index("aboot");
1167 ptn = partition_get_offset(index);
1168 if(ptn == 0)
1169 {
1170 return;
1171 }
1172
1173 size = partition_get_size(index);
1174
1175 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1176 {
1177 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1178 return;
1179 }
1180
1181 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1182 {
1183 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1184 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001185 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001186
1187 write_device_info_mmc(info);
1188 }
1189 memcpy(dev, info, sizeof(device_info));
1190}
1191
1192void write_device_info_flash(device_info *dev)
1193{
1194 struct device_info *info = (void *) info_buf;
1195 struct ptentry *ptn;
1196 struct ptable *ptable;
1197
1198 ptable = flash_get_ptable();
1199 if (ptable == NULL)
1200 {
1201 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1202 return;
1203 }
1204
1205 ptn = ptable_find(ptable, "devinfo");
1206 if (ptn == NULL)
1207 {
1208 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1209 return;
1210 }
1211
1212 memcpy(info, dev, sizeof(device_info));
1213
1214 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1215 {
1216 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1217 return;
1218 }
1219}
1220
1221void read_device_info_flash(device_info *dev)
1222{
1223 struct device_info *info = (void*) info_buf;
1224 struct ptentry *ptn;
1225 struct ptable *ptable;
1226
1227 ptable = flash_get_ptable();
1228 if (ptable == NULL)
1229 {
1230 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1231 return;
1232 }
1233
1234 ptn = ptable_find(ptable, "devinfo");
1235 if (ptn == NULL)
1236 {
1237 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1238 return;
1239 }
1240
1241 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1242 {
1243 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1244 return;
1245 }
1246
1247 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1248 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001249 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1250 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001251 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001252 write_device_info_flash(info);
1253 }
1254 memcpy(dev, info, sizeof(device_info));
1255}
1256
1257void write_device_info(device_info *dev)
1258{
1259 if(target_is_emmc_boot())
1260 {
1261 write_device_info_mmc(dev);
1262 }
1263 else
1264 {
1265 write_device_info_flash(dev);
1266 }
1267}
1268
1269void read_device_info(device_info *dev)
1270{
1271 if(target_is_emmc_boot())
1272 {
1273 read_device_info_mmc(dev);
1274 }
1275 else
1276 {
1277 read_device_info_flash(dev);
1278 }
1279}
1280
1281void reset_device_info()
1282{
1283 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001284 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001285 write_device_info(&device);
1286}
1287
1288void set_device_root()
1289{
1290 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001291 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001292 write_device_info(&device);
1293}
1294
Amol Jadicb524072012-08-09 16:40:18 -07001295#if DEVICE_TREE
1296int copy_dtb(uint8_t *boot_image_start)
1297{
1298 uint32 dt_image_offset = 0;
1299 uint32_t n;
1300 struct dt_table *table;
1301 struct dt_entry *dt_entry_ptr;
Amol Jadicb524072012-08-09 16:40:18 -07001302
1303 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1304
Amol Jadicb524072012-08-09 16:40:18 -07001305 if(hdr->dt_size != 0) {
1306
1307 /* add kernel offset */
1308 dt_image_offset += page_size;
1309 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1310 dt_image_offset += n;
1311
1312 /* add ramdisk offset */
1313 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1314 dt_image_offset += n;
1315
1316 /* add second offset */
1317 if(hdr->second_size != 0) {
1318 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1319 dt_image_offset += n;
1320 }
1321
1322 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001323 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001324
1325 /* Restriction that the device tree entry table should be less than a page*/
1326 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1327
1328 /* Validate the device tree table header */
1329 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1330 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1331 return -1;
1332 }
1333
1334 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001335 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Amol Jadicb524072012-08-09 16:40:18 -07001336 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1337 return -1;
1338 }
1339
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001340 /* Validate and Read device device tree in the "tags_add */
1341 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry_ptr->size))
1342 {
1343 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1344 return -1;
1345 }
1346
Amol Jadicb524072012-08-09 16:40:18 -07001347 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001348 memmove((void*) hdr->tags_addr,
1349 boot_image_start + dt_image_offset + dt_entry_ptr->offset,
1350 dt_entry_ptr->size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001351 } else
1352 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001353
1354 /* Everything looks fine. Return success. */
1355 return 0;
1356}
1357#endif
1358
Brian Swetland9c4c0752009-01-25 16:23:50 -08001359void cmd_boot(const char *arg, void *data, unsigned sz)
1360{
1361 unsigned kernel_actual;
1362 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001363 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001364 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001365 int ret = 0;
1366 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001367
1368 if (sz < sizeof(hdr)) {
1369 fastboot_fail("invalid bootimage header");
1370 return;
1371 }
1372
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001373 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001374
1375 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001376 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001377
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001378 if(target_is_emmc_boot() && hdr->page_size) {
1379 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001380 page_mask = page_size - 1;
1381 }
1382
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001383 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1384 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1385
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001386 /*
1387 * Update the kernel/ramdisk/tags address if the boot image header
1388 * has default values, these default values come from mkbootimg when
1389 * the boot image is flashed using fastboot flash:raw
1390 */
1391 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001392
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001393 /* Get virtual addresses since the hdr saves physical addresses. */
1394 hdr->kernel_addr = VA(hdr->kernel_addr);
1395 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1396 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001397
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001398 /* Check if the addresses in the header are valid. */
1399 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1400 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1401 {
1402 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1403 return -1;
1404 }
1405
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001406 /* sz should have atleast raw boot image */
1407 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001408 fastboot_fail("incomplete bootimage");
1409 return;
1410 }
1411
Amol Jadicb524072012-08-09 16:40:18 -07001412#if DEVICE_TREE
1413 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001414 ret = copy_dtb(data);
1415
1416 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001417#else
1418 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1419 {
1420 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1421 return -1;
1422 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001423#endif
1424
1425 /* Load ramdisk & kernel */
1426 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1427 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1428
1429#if DEVICE_TREE
1430 /*
1431 * If dtb is not found look for appended DTB in the kernel.
1432 * If appended dev tree is found, update the atags with
1433 * memory address to the DTB appended location on RAM.
1434 * Else update with the atags address in the kernel header
1435 */
1436 if (!dtb_copied) {
1437 void *dtb;
Channagoud Kadabia83a6972013-04-25 15:26:04 -07001438 dtb = dev_tree_appended((void *)hdr->kernel_addr, (void *)hdr->tags_addr, hdr->kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001439 if (!dtb) {
1440 fastboot_fail("dtb not found");
1441 return;
1442 }
Amol Jadicb524072012-08-09 16:40:18 -07001443 }
1444#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001445
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001446#ifndef DEVICE_TREE
1447 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1448 {
1449 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1450 return -1;
1451 }
1452#endif
1453
Brian Swetland9c4c0752009-01-25 16:23:50 -08001454 fastboot_okay("");
1455 udc_stop();
1456
Dima Zavin77e41f32013-03-06 16:10:43 -08001457 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001458 (const char*) hdr->cmdline, board_machtype(),
1459 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001460}
1461
Dima Zavin214cc642009-01-26 11:16:21 -08001462void cmd_erase(const char *arg, void *data, unsigned sz)
1463{
1464 struct ptentry *ptn;
1465 struct ptable *ptable;
1466
1467 ptable = flash_get_ptable();
1468 if (ptable == NULL) {
1469 fastboot_fail("partition table doesn't exist");
1470 return;
1471 }
1472
1473 ptn = ptable_find(ptable, arg);
1474 if (ptn == NULL) {
1475 fastboot_fail("unknown partition name");
1476 return;
1477 }
1478
1479 if (flash_erase(ptn)) {
1480 fastboot_fail("failed to erase partition");
1481 return;
1482 }
1483 fastboot_okay("");
1484}
1485
Bikas Gurungd48bd242010-09-04 19:54:32 -07001486
1487void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1488{
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001489 BUF_DMA_ALIGN(out, 512);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001490 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001491 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001492
Kinson Chikf1a43512011-07-14 11:28:39 -07001493 index = partition_get_index(arg);
1494 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001495
Kinson Chikf1a43512011-07-14 11:28:39 -07001496 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001497 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001498 return;
1499 }
neetidb4b24d62012-01-20 12:13:09 -08001500 /* Simple inefficient version of erase. Just writing
1501 0 in first block */
1502 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1503 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001504 return;
1505 }
1506 fastboot_okay("");
1507}
1508
1509
Ajay Dudani5c761132011-04-07 20:19:04 -07001510void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001511{
1512 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001513 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001514 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001515
Greg Grisco6e754772011-06-23 12:19:39 -07001516 if (!strcmp(arg, "partition"))
1517 {
1518 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001519 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001520 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001521 return;
1522 }
1523 }
Greg Grisco6e754772011-06-23 12:19:39 -07001524 else
1525 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001526 index = partition_get_index(arg);
1527 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001528 if(ptn == 0) {
1529 fastboot_fail("partition table doesn't exist");
1530 return;
1531 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001532
Greg Grisco6e754772011-06-23 12:19:39 -07001533 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1534 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1535 fastboot_fail("image is not a boot image");
1536 return;
1537 }
1538 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001539
Kinson Chikf1a43512011-07-14 11:28:39 -07001540 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001541 if (ROUND_TO_PAGE(sz,511) > size) {
1542 fastboot_fail("size too large");
1543 return;
1544 }
1545 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1546 fastboot_fail("flash write failure");
1547 return;
1548 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001549 }
1550 fastboot_okay("");
1551 return;
1552}
1553
Ajay Dudani5c761132011-04-07 20:19:04 -07001554void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1555{
1556 unsigned int chunk;
1557 unsigned int chunk_data_sz;
1558 sparse_header_t *sparse_header;
1559 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001560 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001561 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301562 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001563 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001564
Kinson Chikf1a43512011-07-14 11:28:39 -07001565 index = partition_get_index(arg);
1566 ptn = partition_get_offset(index);
1567 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001568 fastboot_fail("partition table doesn't exist");
1569 return;
1570 }
1571
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301572 size = partition_get_size(index);
1573 if (ROUND_TO_PAGE(sz,511) > size) {
1574 fastboot_fail("size too large");
1575 return;
1576 }
1577
Ajay Dudani5c761132011-04-07 20:19:04 -07001578 /* Read and skip over sparse image header */
1579 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001580 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1581 fastboot_fail("size too large");
1582 return;
1583 }
1584
Ajay Dudani5c761132011-04-07 20:19:04 -07001585 data += sparse_header->file_hdr_sz;
1586 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1587 {
1588 /* Skip the remaining bytes in a header that is longer than
1589 * we expected.
1590 */
1591 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1592 }
1593
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001594 dprintf (SPEW, "=== Sparse Image Header ===\n");
1595 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1596 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1597 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1598 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1599 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1600 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1601 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1602 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001603
1604 /* Start processing chunks */
1605 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1606 {
1607 /* Read and skip over chunk header */
1608 chunk_header = (chunk_header_t *) data;
1609 data += sizeof(chunk_header_t);
1610
1611 dprintf (SPEW, "=== Chunk Header ===\n");
1612 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1613 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1614 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1615
1616 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1617 {
1618 /* Skip the remaining bytes in a header that is longer than
1619 * we expected.
1620 */
1621 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1622 }
1623
1624 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1625 switch (chunk_header->chunk_type)
1626 {
1627 case CHUNK_TYPE_RAW:
1628 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1629 chunk_data_sz))
1630 {
1631 fastboot_fail("Bogus chunk size for chunk type Raw");
1632 return;
1633 }
1634
Ajay Dudaniab18f022011-05-12 14:39:22 -07001635 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1636 chunk_data_sz,
1637 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001638 {
1639 fastboot_fail("flash write failure");
1640 return;
1641 }
1642 total_blocks += chunk_header->chunk_sz;
1643 data += chunk_data_sz;
1644 break;
1645
1646 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001647 total_blocks += chunk_header->chunk_sz;
1648 break;
1649
Ajay Dudani5c761132011-04-07 20:19:04 -07001650 case CHUNK_TYPE_CRC:
1651 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1652 {
1653 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1654 return;
1655 }
1656 total_blocks += chunk_header->chunk_sz;
1657 data += chunk_data_sz;
1658 break;
1659
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001660 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001661 fastboot_fail("Unknown chunk type");
1662 return;
1663 }
1664 }
1665
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001666 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1667 total_blocks, sparse_header->total_blks);
1668
1669 if(total_blocks != sparse_header->total_blks)
1670 {
1671 fastboot_fail("sparse image write failure");
1672 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001673
1674 fastboot_okay("");
1675 return;
1676}
1677
1678void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1679{
1680 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001681 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1682 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001683
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001684#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001685 int ret=0;
1686 uint32 major_version=0;
1687 uint32 minor_version=0;
1688
1689 ret = scm_svc_version(&major_version,&minor_version);
1690 if(!ret)
1691 {
1692 if(major_version >= 2)
1693 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001694 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001695 {
1696 ret = encrypt_scm((uint32 **) &data, &sz);
1697 if (ret != 0) {
1698 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1699 return;
1700 }
1701
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001702 /* Protect only for SSD */
1703 if (!strcmp(arg, "ssd")) {
1704 ret = scm_protect_keystore((uint32 *) data, sz);
1705 if (ret != 0) {
1706 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1707 return;
1708 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001709 }
1710 }
1711 else
1712 {
1713 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1714 if(ret != 0)
1715 {
1716 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1717 return;
1718 }
1719 }
1720 }
1721 else
1722 {
1723 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1724 magic_number[1] == DECRYPT_MAGIC_1)
1725 {
1726 ret = decrypt_scm((uint32 **) &data, &sz);
1727 if (ret != 0) {
1728 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1729 return;
1730 }
1731 }
1732 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1733 magic_number[1] == ENCRYPT_MAGIC_1)
1734 {
1735 ret = encrypt_scm((uint32 **) &data, &sz);
1736 if (ret != 0) {
1737 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1738 return;
1739 }
1740 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001741 }
1742 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001743 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001744 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001745 dprintf(CRITICAL,"INVALID SVC Version\n");
1746 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001747 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001748#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001749
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001750 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001751 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1752 cmd_flash_mmc_img(arg, data, sz);
1753 else
1754 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001755 return;
1756}
1757
Dima Zavin214cc642009-01-26 11:16:21 -08001758void cmd_flash(const char *arg, void *data, unsigned sz)
1759{
1760 struct ptentry *ptn;
1761 struct ptable *ptable;
1762 unsigned extra = 0;
1763
1764 ptable = flash_get_ptable();
1765 if (ptable == NULL) {
1766 fastboot_fail("partition table doesn't exist");
1767 return;
1768 }
1769
1770 ptn = ptable_find(ptable, arg);
1771 if (ptn == NULL) {
1772 fastboot_fail("unknown partition name");
1773 return;
1774 }
1775
1776 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1777 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1778 fastboot_fail("image is not a boot image");
1779 return;
1780 }
1781 }
1782
Amol Jadi5c61a952012-05-04 17:05:35 -07001783 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001784 || !strcmp(ptn->name, "userdata")
1785 || !strcmp(ptn->name, "persist")
1786 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301787 if (flash_ecc_bch_enabled())
1788 /* Spare data bytes for 8 bit ECC increased by 4 */
1789 extra = ((page_size >> 9) * 20);
1790 else
1791 extra = ((page_size >> 9) * 16);
1792 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001793 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001794
1795 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1796 if (flash_write(ptn, extra, data, sz)) {
1797 fastboot_fail("flash write failure");
1798 return;
1799 }
1800 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1801 fastboot_okay("");
1802}
1803
1804void cmd_continue(const char *arg, void *data, unsigned sz)
1805{
1806 fastboot_okay("");
1807 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001808 if (target_is_emmc_boot())
1809 {
1810 boot_linux_from_mmc();
1811 }
1812 else
1813 {
1814 boot_linux_from_flash();
1815 }
Dima Zavin214cc642009-01-26 11:16:21 -08001816}
1817
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001818void cmd_reboot(const char *arg, void *data, unsigned sz)
1819{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001820 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001821 fastboot_okay("");
1822 reboot_device(0);
1823}
1824
1825void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1826{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001827 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001828 fastboot_okay("");
1829 reboot_device(FASTBOOT_MODE);
1830}
1831
Shashank Mittal162244e2011-08-08 19:01:25 -07001832void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1833{
1834 if(!device.is_unlocked)
1835 {
1836 device.is_unlocked = 1;
1837 write_device_info(&device);
1838 }
1839 fastboot_okay("");
1840}
1841
Shashank Mittala0032282011-08-26 14:50:11 -07001842void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1843{
1844 char response[64];
1845 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1846 fastboot_info(response);
1847 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1848 fastboot_info(response);
1849 fastboot_okay("");
1850}
1851
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001852void cmd_preflash(const char *arg, void *data, unsigned sz)
1853{
1854 fastboot_okay("");
1855}
1856
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001857void splash_screen ()
1858{
1859 struct ptentry *ptn;
1860 struct ptable *ptable;
1861 struct fbcon_config *fb_display = NULL;
1862
1863 if (!target_is_emmc_boot())
1864 {
1865 ptable = flash_get_ptable();
1866 if (ptable == NULL) {
1867 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001868 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001869 }
1870
1871 ptn = ptable_find(ptable, "splash");
1872 if (ptn == NULL) {
1873 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1874 } else {
1875 fb_display = fbcon_display();
1876 if (fb_display) {
1877 if (flash_read(ptn, 0, fb_display->base,
1878 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1879 fbcon_clear();
1880 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1881 }
1882 }
1883 }
1884 }
1885}
1886
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001887/* Get the size from partiton name */
1888static void get_partition_size(const char *arg, char *response)
1889{
1890 uint64_t ptn = 0;
1891 uint64_t size;
1892 int index = INVALID_PTN;
1893
1894 index = partition_get_index(arg);
1895
1896 if (index == INVALID_PTN)
1897 {
1898 dprintf(CRITICAL, "Invalid partition index\n");
1899 return;
1900 }
1901
1902 ptn = partition_get_offset(index);
1903
1904 if(!ptn)
1905 {
1906 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1907 return;
1908 }
1909
1910 size = partition_get_size(index);
1911
1912 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1913 return;
1914}
1915
1916/*
1917 * Publish the partition type & size info
1918 * fastboot getvar will publish the required information.
1919 * fastboot getvar partition_size:<partition_name>: partition size in hex
1920 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1921 */
1922static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1923{
1924 uint8_t i;
1925
1926 for (i = 0; i < num_parts; i++) {
1927 get_partition_size(info[i].part_name, info[i].size_response);
1928
1929 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1930 {
1931 dprintf(CRITICAL, "partition size name truncated\n");
1932 return;
1933 }
1934 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1935 {
1936 dprintf(CRITICAL, "partition type name truncated\n");
1937 return;
1938 }
1939
1940 /* publish partition size & type info */
1941 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1942 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1943 }
1944}
1945
Brian Swetland9c4c0752009-01-25 16:23:50 -08001946void aboot_init(const struct app_descriptor *app)
1947{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001948 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001949 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001950 unsigned sz = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03001951 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001952
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001953 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001954 if (target_is_emmc_boot())
1955 {
1956 page_size = 2048;
1957 page_mask = page_size - 1;
1958 }
1959 else
1960 {
1961 page_size = flash_page_size();
1962 page_mask = page_size - 1;
1963 }
1964
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001965 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
1966
Shashank Mittal162244e2011-08-08 19:01:25 -07001967 if(target_use_signed_kernel())
1968 {
1969 read_device_info(&device);
1970
Shashank Mittal162244e2011-08-08 19:01:25 -07001971 }
1972
Greg Griscod6250552011-06-29 14:40:23 -07001973 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001974 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001975 surf_udc_device.serialno = sn_buf;
1976
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001977 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001978 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001979 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001980 dprintf(ALWAYS,"dload mode key sequence detected\n");
1981 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001982 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001983 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001984 }
1985 else
1986 {
1987 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03001988 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001989 }
1990 boot_into_fastboot = true;
1991 }
1992 if (!boot_into_fastboot)
1993 {
1994 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
1995 boot_into_recovery = 1;
1996 if (!boot_into_recovery &&
1997 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03001998 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001999 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002000 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002001 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002002 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002003 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002004
Ajay Dudani77421292010-10-27 19:34:06 -07002005 reboot_mode = check_reboot_mode();
2006 if (reboot_mode == RECOVERY_MODE) {
2007 boot_into_recovery = 1;
2008 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002009 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002010 }
2011
Pavel Nedev5d91d412013-04-29 11:34:24 +03002012 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002013 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002014 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002015 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002016 if(emmc_recovery_init())
2017 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2018 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002019 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002020 if((device.is_unlocked) || (device.is_tampered))
2021 {
2022 #ifdef TZ_TAMPER_FUSE
2023 set_tamper_fuse_cmd();
2024 #endif
2025 #if USE_PCOM_SECBOOT
2026 set_tamper_flag(device.is_tampered);
2027 #endif
2028 }
Shashank Mittala0032282011-08-26 14:50:11 -07002029 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002030 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002031 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002032 else
2033 {
2034 recovery_init();
2035 #if USE_PCOM_SECBOOT
2036 if((device.is_unlocked) || (device.is_tampered))
2037 set_tamper_flag(device.is_tampered);
2038 #endif
2039 boot_linux_from_flash();
2040 }
2041 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2042 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002043 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002044
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002045 sz = target_get_max_flash_size();
2046
Shashank Mittal162244e2011-08-08 19:01:25 -07002047 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002048
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002049 if(!usb_init)
2050 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002051
2052 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002053
Shashank Mittal23b8f422010-04-16 19:27:21 -07002054 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002055 {
2056 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002057 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002058 }
2059 else
2060 {
2061 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07002062 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002063 }
2064
2065 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002066 fastboot_register("reboot", cmd_reboot);
2067 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07002068 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07002069 fastboot_register("oem device-info", cmd_oem_devinfo);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002070 fastboot_register("preflash", cmd_preflash);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07002071 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08002072 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08002073 fastboot_publish("serialno", sn_buf);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002074 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2075 /* Max download size supported */
2076 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", sz);
2077 fastboot_publish("max-download-size", (const char *) max_download_size);
Kinson Chikf1a43512011-07-14 11:28:39 -07002078 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07002079 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002080 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002081}
2082
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002083uint32_t get_page_size()
2084{
2085 return page_size;
2086}
2087
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002088/*
2089 * Calculated and save hash (SHA256) for non-signed boot image.
2090 *
2091 * Hash the same data that is checked on the signed boot image.
2092 * Kernel and Ramdisk are already read to memory buffers.
2093 * Need to read the entire device-tree from mmc
2094 * since non-signed image only read the DT tags of the relevant platform.
2095 *
2096 * @param kernel_addr - kernel bufer
2097 * @param kernel_actual - kernel size in bytes
2098 * @param ramdisk_addr - ramdisk buffer
2099 * @param ramdisk_actual - ramdisk size
2100 * @param ptn - partition
2101 * @param dt_offset - device tree offset on mmc partition
2102 * @param dt_size
2103 *
2104 * @return int - 0 on success, negative value on failure.
2105 */
2106int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2107 void *ramdisk_addr, unsigned ramdisk_actual,
2108 unsigned long long ptn,
2109 unsigned dt_offset, unsigned dt_size)
2110{
2111 SHA256_CTX sha256_ctx;
2112 char digest[32]={0};
2113 char *buf = (char *)target_get_scratch_address();
2114 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2115 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2116
2117 SHA256_Init(&sha256_ctx);
2118
2119 /* Read Boot Header */
2120 if (mmc_read(ptn, buf, page_size))
2121 {
2122 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2123 return -1;
2124 }
2125 /* Read entire Device Tree */
2126 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2127 {
2128 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2129 return -1;
2130 }
2131 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2132 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2133 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2134 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2135
2136 SHA256_Final(digest, &sha256_ctx);
2137
2138 save_kernel_hash_cmd(digest);
2139 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2140
2141 return 0;
2142}
2143
Brian Swetland9c4c0752009-01-25 16:23:50 -08002144APP_START(aboot)
2145 .init = aboot_init,
2146APP_END