blob: 7f9d0d11c518c60dee5b8e23a9403a620f8852e9 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Amol Jadi2a15a272013-01-22 12:03:36 -08005 * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080038#include <kernel/thread.h>
39#include <arch/ops.h>
40
Dima Zavin214cc642009-01-26 11:16:21 -080041#include <dev/flash.h>
42#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080043#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080044#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080045#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070046#include <target.h>
47#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070048#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070049#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070050#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070051#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070052#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030053#include <sha.h>
Dima Zavin214cc642009-01-26 11:16:21 -080054
Neeti Desai17379b82012-06-04 18:42:53 -070055#if DEVICE_TREE
56#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070057#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070058#endif
59
Shashank Mittalcd98d472011-08-02 14:29:24 -070060#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080061#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080062#include "bootimg.h"
63#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070064#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070065#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070066#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070067#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080068
Shashank Mittal162244e2011-08-08 19:01:25 -070069#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070070
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070071extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070072extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070073extern void target_uninit(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070074
75void write_device_info_mmc(device_info *dev);
76void write_device_info_flash(device_info *dev);
77
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070078#define EXPAND(NAME) #NAME
79#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070080
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080081#ifdef MEMBASE
82#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
83#else
David Ng183a7422009-12-07 14:55:21 -080084#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080085#endif
86
Deepa Dinamani0e163a42013-05-24 17:08:15 -070087#ifndef MEMSIZE
88#define MEMSIZE 1024*1024
89#endif
90
91#define MAX_TAGS_SIZE 1024
92
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080093#define RECOVERY_MODE 0x77665502
94#define FASTBOOT_MODE 0x77665500
95
Kun Liang2f1601a2013-08-12 16:29:54 +080096/* make 4096 as default size to ensure EFS,EXT4's erasing */
97#define DEFAULT_ERASE_SIZE 4096
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";
Pavel Nedev5614d222013-06-17 18:01:02 +0300105static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800106
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800107static const char *baseband_apq = " androidboot.baseband=apq";
108static const char *baseband_msm = " androidboot.baseband=msm";
109static const char *baseband_csfb = " androidboot.baseband=csfb";
110static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700111static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700112static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800113static const char *baseband_dsda = " androidboot.baseband=dsda";
114static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800115static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800116
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700117static unsigned page_size = 0;
118static unsigned page_mask = 0;
119static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
120static bool boot_into_ffbm;
121
Shashank Mittalcd98d472011-08-02 14:29:24 -0700122/* Assuming unauthorized kernel image by default */
123static int auth_kernel_img = 0;
124
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700125static device_info device = {DEVICE_MAGIC, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700126
Dima Zavin42168f22009-01-30 11:52:22 -0800127struct atag_ptbl_entry
128{
129 char name[16];
130 unsigned offset;
131 unsigned size;
132 unsigned flags;
133};
134
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700135/*
136 * Partition info, required to be published
137 * for fastboot
138 */
139struct getvar_partition_info {
140 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
141 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
142 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
143 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
144 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
145};
146
147/*
148 * Right now, we are publishing the info for only
149 * three partitions
150 */
151struct getvar_partition_info part_info[] =
152{
153 { "system" , "partition-size:", "partition-type:", "", "ext4" },
154 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
155 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
156};
157
158char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700159char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800160char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700161
Greg Griscod2471ef2011-07-14 13:00:42 -0700162extern int emmc_recovery_init(void);
163
Kinson Chik0b1c8162011-08-31 16:31:57 -0700164#if NO_KEYPAD_DRIVER
165extern int fastboot_trigger(void);
166#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700167
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700168static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
169{
170 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700171#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
172 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
173 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
174 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700175#endif
176}
177
Dima Zavin42168f22009-01-30 11:52:22 -0800178static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
179{
180 struct atag_ptbl_entry atag_ptn;
181
182 memcpy(atag_ptn.name, ptn->name, 16);
183 atag_ptn.name[15] = '\0';
184 atag_ptn.offset = ptn->start;
185 atag_ptn.size = ptn->length;
186 atag_ptn.flags = ptn->flags;
187 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
188 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
189}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800190
Neeti Desaie245d492012-06-01 12:52:13 -0700191unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800192{
David Ng183a7422009-12-07 14:55:21 -0800193 int cmdline_len = 0;
194 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800195 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700196 int pause_at_bootup = 0;
Pavel Nedev5614d222013-06-17 18:01:02 +0300197 bool gpt_exists = partition_gpt_exists();
Dima Zavin42168f22009-01-30 11:52:22 -0800198
Brian Swetland9c4c0752009-01-25 16:23:50 -0800199 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800200 cmdline_len = strlen(cmdline);
201 have_cmdline = 1;
202 }
203 if (target_is_emmc_boot()) {
204 cmdline_len += strlen(emmc_cmdline);
205 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800206
207 cmdline_len += strlen(usb_sn_cmdline);
208 cmdline_len += strlen(sn_buf);
209
Pavel Nedev5614d222013-06-17 18:01:02 +0300210 if (boot_into_recovery && gpt_exists)
211 cmdline_len += strlen(secondary_gpt_enable);
212
Pavel Nedev328ac822013-04-05 15:25:11 +0300213 if (boot_into_ffbm) {
214 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700215 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800216 /* reduce kernel console messages to speed-up boot */
217 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700218 } else if (device.charger_screen_enabled &&
219 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 Nedev5614d222013-06-17 18:01:02 +0300295 if (boot_into_recovery && gpt_exists) {
296 src = secondary_gpt_enable;
297 if (have_cmdline) --dst;
298 while ((*dst++ = *src++));
299 }
300
Pavel Nedev328ac822013-04-05 15:25:11 +0300301 if (boot_into_ffbm) {
302 src = androidboot_mode;
303 if (have_cmdline) --dst;
304 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700305 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300306 if (have_cmdline) --dst;
307 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800308 src = loglevel;
309 if (have_cmdline) --dst;
310 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300311 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700312 src = battchg_pause;
313 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800314 while ((*dst++ = *src++));
315 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800316
Shashank Mittalcd98d472011-08-02 14:29:24 -0700317 if(target_use_signed_kernel() && auth_kernel_img) {
318 src = auth_kernel;
319 if (have_cmdline) --dst;
320 while ((*dst++ = *src++));
321 }
322
Ajay Dudanid04110c2011-01-17 23:55:07 -0800323 switch(target_baseband())
324 {
325 case BASEBAND_APQ:
326 src = baseband_apq;
327 if (have_cmdline) --dst;
328 while ((*dst++ = *src++));
329 break;
330
331 case BASEBAND_MSM:
332 src = baseband_msm;
333 if (have_cmdline) --dst;
334 while ((*dst++ = *src++));
335 break;
336
337 case BASEBAND_CSFB:
338 src = baseband_csfb;
339 if (have_cmdline) --dst;
340 while ((*dst++ = *src++));
341 break;
342
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800343 case BASEBAND_SVLTE2A:
344 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800345 if (have_cmdline) --dst;
346 while ((*dst++ = *src++));
347 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700348
349 case BASEBAND_MDM:
350 src = baseband_mdm;
351 if (have_cmdline) --dst;
352 while ((*dst++ = *src++));
353 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700354
355 case BASEBAND_SGLTE:
356 src = baseband_sglte;
357 if (have_cmdline) --dst;
358 while ((*dst++ = *src++));
359 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530360
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800361 case BASEBAND_SGLTE2:
362 src = baseband_sglte2;
363 if (have_cmdline) --dst;
364 while ((*dst++ = *src++));
365 break;
366
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530367 case BASEBAND_DSDA:
368 src = baseband_dsda;
369 if (have_cmdline) --dst;
370 while ((*dst++ = *src++));
371 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800372
373 case BASEBAND_DSDA2:
374 src = baseband_dsda2;
375 if (have_cmdline) --dst;
376 while ((*dst++ = *src++));
377 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800378 }
Neeti Desaie245d492012-06-01 12:52:13 -0700379 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800380 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700381 return cmdline_final;
382}
383
384unsigned *atag_core(unsigned *ptr)
385{
386 /* CORE */
387 *ptr++ = 2;
388 *ptr++ = 0x54410001;
389
390 return ptr;
391
392}
393
394unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
395 unsigned ramdisk_size)
396{
397 if (ramdisk_size) {
398 *ptr++ = 4;
399 *ptr++ = 0x54420005;
400 *ptr++ = (unsigned)ramdisk;
401 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800402 }
403
Neeti Desaie245d492012-06-01 12:52:13 -0700404 return ptr;
405}
406
407unsigned *atag_ptable(unsigned **ptr_addr)
408{
409 int i;
410 struct ptable *ptable;
411
412 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700413 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
414 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700415 *(*ptr_addr)++ = 0x4d534d70;
416 for (i = 0; i < ptable->count; ++i)
417 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
418 }
419
420 return (*ptr_addr);
421}
422
423unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
424{
425 int cmdline_length = 0;
426 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700427 char *dest;
428
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800429 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700430 n = (cmdline_length + 4) & (~3);
431
432 *ptr++ = (n / 4) + 2;
433 *ptr++ = 0x54410009;
434 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800435 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700436 ptr += (n / 4);
437
438 return ptr;
439}
440
441unsigned *atag_end(unsigned *ptr)
442{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800443 /* END */
444 *ptr++ = 0;
445 *ptr++ = 0;
446
Neeti Desaie245d492012-06-01 12:52:13 -0700447 return ptr;
448}
449
450void generate_atags(unsigned *ptr, const char *cmdline,
451 void *ramdisk, unsigned ramdisk_size)
452{
453
454 ptr = atag_core(ptr);
455 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
456 ptr = target_atag_mem(ptr);
457
458 /* Skip NAND partition ATAGS for eMMC boot */
459 if (!target_is_emmc_boot()){
460 ptr = atag_ptable(&ptr);
461 }
462
463 ptr = atag_cmdline(ptr, cmdline);
464 ptr = atag_end(ptr);
465}
466
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700467typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700468void boot_linux(void *kernel, unsigned *tags,
469 const char *cmdline, unsigned machtype,
470 void *ramdisk, unsigned ramdisk_size)
471{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800472 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800473#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700474 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800475#endif
476
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700477 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800478 uint32_t tags_phys = PA((addr_t)tags);
479
480 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700481
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800482 final_cmdline = update_cmdline((const char*)cmdline);
483
Neeti Desai17379b82012-06-04 18:42:53 -0700484#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800485 dprintf(INFO, "Updating device tree: start\n");
486
Neeti Desai17379b82012-06-04 18:42:53 -0700487 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800488 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700489 if(ret)
490 {
491 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
492 ASSERT(0);
493 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800494 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700495#else
Neeti Desaie245d492012-06-01 12:52:13 -0700496 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800497 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700498#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700499
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700500 /* Perform target specific cleanup */
501 target_uninit();
502
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800503 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
504 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800505
506 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700507
Amol Jadi4421e652011-06-16 15:00:48 -0700508 /* do any platform specific cleanup before kernel entry */
509 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700510
Brian Swetland9c4c0752009-01-25 16:23:50 -0800511 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700512
Amol Jadi504f9fe2012-08-16 13:56:48 -0700513#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800514 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700515#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700516 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700517 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800518}
519
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700520/* Function to check if the memory address range falls within the aboot
521 * boundaries.
522 * start: Start of the memory region
523 * size: Size of the memory region
524 */
525int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
526{
527 /* Check for boundary conditions. */
528 if ((start + size) < start)
529 return -1;
530
531 /* Check for memory overlap. */
532 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
533 return 0;
534 else if (start > (MEMBASE + MEMSIZE))
535 return 0;
536 else
537 return -1;
538}
539
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800540#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800541
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700542BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800543#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700544BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800545#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800546
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700547static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
548{
549 int ret;
550
551 /* Assume device is rooted at this time. */
552 device.is_tampered = 1;
553
554 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
555
556 ret = image_verify((unsigned char *)bootimg_addr,
557 (unsigned char *)(bootimg_addr + bootimg_size),
558 bootimg_size,
559 CRYPTO_AUTH_ALG_SHA256);
560
561 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
562
563 if (ret)
564 {
565 /* Authorized kernel */
566 device.is_tampered = 0;
567 }
568
569#if USE_PCOM_SECBOOT
570 set_tamper_flag(device.is_tampered);
571#endif
572
573 if(device.is_tampered)
574 {
575 write_device_info_mmc(&device);
576 #ifdef TZ_TAMPER_FUSE
577 set_tamper_fuse_cmd();
578 #endif
579 #ifdef ASSERT_ON_TAMPER
580 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
581 ASSERT(0);
582 #endif
583 }
584}
585
Shashank Mittal23b8f422010-04-16 19:27:21 -0700586int boot_linux_from_mmc(void)
587{
588 struct boot_img_hdr *hdr = (void*) buf;
589 struct boot_img_hdr *uhdr;
590 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700591 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700592 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700593 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700594
Shashank Mittalcd98d472011-08-02 14:29:24 -0700595 unsigned char *image_addr = 0;
596 unsigned kernel_actual;
597 unsigned ramdisk_actual;
598 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700599 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700600
601#if DEVICE_TREE
602 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700603 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700604 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800605 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700606#endif
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700607 if (!boot_into_recovery) {
608 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
609 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
610 if (rcode <= 0) {
611 boot_into_ffbm = false;
612 if (rcode < 0)
613 dprintf(CRITICAL,"failed to get ffbm cookie");
614 } else
615 boot_into_ffbm = true;
616 } else
617 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700618 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
619 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
620 dprintf(INFO, "Unified boot method!\n");
621 hdr = uhdr;
622 goto unified_boot;
623 }
Greg Griscod6250552011-06-29 14:40:23 -0700624 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700625 index = partition_get_index("boot");
626 ptn = partition_get_offset(index);
627 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700628 dprintf(CRITICAL, "ERROR: No boot partition found\n");
629 return -1;
630 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700631 }
632 else {
633 index = partition_get_index("recovery");
634 ptn = partition_get_offset(index);
635 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700636 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
637 return -1;
638 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700639 }
640
Greg Griscod6250552011-06-29 14:40:23 -0700641 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700642 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
643 return -1;
644 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700645
646 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700647 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700648 return -1;
649 }
650
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700651 if (hdr->page_size && (hdr->page_size != page_size)) {
652 page_size = hdr->page_size;
653 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700654 }
655
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700656 /*
657 * Update the kernel/ramdisk/tags address if the boot image header
658 * has default values, these default values come from mkbootimg when
659 * the boot image is flashed using fastboot flash:raw
660 */
661 update_ker_tags_rdisk_addr(hdr);
662
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700663 /* Get virtual addresses since the hdr saves physical addresses. */
664 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
665 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
666 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700667
668 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
669 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
670
671 /* Check if the addresses in the header are valid. */
672 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
673 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
674 {
675 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
676 return -1;
677 }
678
679#ifndef DEVICE_TREE
680 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
681 {
682 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
683 return -1;
684 }
685#endif
686
Shashank Mittalcd98d472011-08-02 14:29:24 -0700687 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300688 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
689 (int) target_use_signed_kernel(),
690 device.is_unlocked,
691 device.is_tampered);
692
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700693 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800694 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700695 offset = 0;
696
Shashank Mittalcd98d472011-08-02 14:29:24 -0700697 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700698
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800699#if DEVICE_TREE
700 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
701 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700702
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700703 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700704 {
705 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
706 return -1;
707 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800708#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530709 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700710
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700711#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700712
Amol Jadib6be5c12012-11-14 13:39:51 -0800713 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700714 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800715
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700716 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
717 {
718 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
719 return -1;
720 }
721
Shashank Mittalcd98d472011-08-02 14:29:24 -0700722 /* Read image without signature */
723 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
724 {
725 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
726 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800727 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700728
Amol Jadib6be5c12012-11-14 13:39:51 -0800729 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700730 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800731
Shashank Mittalcd98d472011-08-02 14:29:24 -0700732 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700733
734 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
735 {
736 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
737 return -1;
738 }
739
Shashank Mittalcd98d472011-08-02 14:29:24 -0700740 /* Read signature */
741 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
742 {
743 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700744 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700745 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800746
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700747 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700748
Neeti Desai465491e2012-07-31 12:53:35 -0700749 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700750 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
751 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700752
Neeti Desai465491e2012-07-31 12:53:35 -0700753 #if DEVICE_TREE
754 if(hdr->dt_size) {
755 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700756 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700757
758 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
759
Joel Kingaa335dc2013-06-03 16:11:08 -0700760 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700761 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
762 return -1;
763 }
Neeti Desai465491e2012-07-31 12:53:35 -0700764 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700765 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700766 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
767 return -1;
768 }
769
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700770 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700771 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700772 {
773 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
774 return -1;
775 }
776
Joel Kingaa335dc2013-06-03 16:11:08 -0700777 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800778 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800779 /*
780 * If appended dev tree is found, update the atags with
781 * memory address to the DTB appended location on RAM.
782 * Else update with the atags address in the kernel header
783 */
784 void *dtb;
785 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700786 hdr->kernel_size,
787 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800788 if (!dtb) {
789 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
790 return -1;
791 }
Neeti Desai465491e2012-07-31 12:53:35 -0700792 }
793 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700794 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700795 else
796 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800797 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700798
Amol Jadib6be5c12012-11-14 13:39:51 -0800799 dprintf(INFO, "Loading boot image (%d): start\n",
800 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700801 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800802
803 offset = page_size;
804
805 /* Load kernel */
806 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700807 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
808 return -1;
809 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800810 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700811
Amol Jadib6be5c12012-11-14 13:39:51 -0800812 /* Load ramdisk */
813 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700814 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800815 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700816 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
817 return -1;
818 }
819 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800820 offset += ramdisk_actual;
821
822 dprintf(INFO, "Loading boot image (%d): done\n",
823 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700824 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700825
826 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800827 offset += second_actual;
828 /* Second image loading not implemented. */
829 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700830 }
831
832 #if DEVICE_TREE
833 if(hdr->dt_size != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700834 /* Read the device tree table into buffer */
835 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
836 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
837 return -1;
838 }
839 table = (struct dt_table*) dt_buf;
840
Joel Kingaa335dc2013-06-03 16:11:08 -0700841 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700842 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
843 return -1;
844 }
845
Joel Kingaa335dc2013-06-03 16:11:08 -0700846 /* Find index of device tree within device tree table */
847 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700848 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
849 return -1;
850 }
851
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700852 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700853 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700854 {
855 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
856 return -1;
857 }
858
Joel Kingaa335dc2013-06-03 16:11:08 -0700859 if(mmc_read(ptn + offset + dt_entry.offset,
860 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700861 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
862 return -1;
863 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300864 #ifdef TZ_SAVE_KERNEL_HASH
865 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
866 hdr->ramdisk_addr, ramdisk_actual,
867 ptn, offset, hdr->dt_size);
868 #endif /* TZ_SAVE_KERNEL_HASH */
869
Channagoud Kadabi35095622013-03-01 13:53:05 -0800870 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800871 /*
872 * If appended dev tree is found, update the atags with
873 * memory address to the DTB appended location on RAM.
874 * Else update with the atags address in the kernel header
875 */
876 void *dtb;
877 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700878 kernel_actual,
879 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800880 if (!dtb) {
881 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
882 return -1;
883 }
Neeti Desai465491e2012-07-31 12:53:35 -0700884 }
885 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700886 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700887
Stanimir Varbanov69ec5462013-07-18 18:17:42 +0300888 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
889 target_load_ssd_keystore();
890
Shashank Mittal23b8f422010-04-16 19:27:21 -0700891unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700892
Dima Zavin77e41f32013-03-06 16:10:43 -0800893 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700894 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700895 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
896
897 return 0;
898}
899
Dima Zavin214cc642009-01-26 11:16:21 -0800900int boot_linux_from_flash(void)
901{
902 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800903 struct ptentry *ptn;
904 struct ptable *ptable;
905 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800906
Shashank Mittalcd98d472011-08-02 14:29:24 -0700907 unsigned char *image_addr = 0;
908 unsigned kernel_actual;
909 unsigned ramdisk_actual;
910 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800911 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700912
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700913#if DEVICE_TREE
914 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700915 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800916 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700917#endif
918
David Ng183a7422009-12-07 14:55:21 -0800919 if (target_is_emmc_boot()) {
920 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
921 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
922 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
923 return -1;
924 }
925 goto continue_boot;
926 }
927
Dima Zavin214cc642009-01-26 11:16:21 -0800928 ptable = flash_get_ptable();
929 if (ptable == NULL) {
930 dprintf(CRITICAL, "ERROR: Partition table not found\n");
931 return -1;
932 }
933
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800934 if(!boot_into_recovery)
935 {
936 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700937
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800938 if (ptn == NULL) {
939 dprintf(CRITICAL, "ERROR: No boot partition found\n");
940 return -1;
941 }
942 }
943 else
944 {
945 ptn = ptable_find(ptable, "recovery");
946 if (ptn == NULL) {
947 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
948 return -1;
949 }
Dima Zavin214cc642009-01-26 11:16:21 -0800950 }
951
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800952 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800953 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
954 return -1;
955 }
Dima Zavin214cc642009-01-26 11:16:21 -0800956
957 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700958 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800959 return -1;
960 }
961
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800962 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700963 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 -0800964 return -1;
965 }
966
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700967 /*
968 * Update the kernel/ramdisk/tags address if the boot image header
969 * has default values, these default values come from mkbootimg when
970 * the boot image is flashed using fastboot flash:raw
971 */
972 update_ker_tags_rdisk_addr(hdr);
973
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700974 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700975 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
976 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
977 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
978
979 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
980 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
981
982 /* Check if the addresses in the header are valid. */
983 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
984 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
985 {
986 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
987 return -1;
988 }
989
990#ifndef DEVICE_TREE
991 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
992 {
993 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
994 return -1;
995 }
996#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700997
Shashank Mittalcd98d472011-08-02 14:29:24 -0700998 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700999 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001000 {
1001 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001002 offset = 0;
1003
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001004#if DEVICE_TREE
1005 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1006 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001007
1008 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1009 {
1010 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1011 return -1;
1012 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001013#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001014 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001015#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001016
Amol Jadib6be5c12012-11-14 13:39:51 -08001017 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001018 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001019
Shashank Mittalcd98d472011-08-02 14:29:24 -07001020 /* Read image without signature */
1021 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1022 {
1023 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1024 return -1;
1025 }
Dima Zavin214cc642009-01-26 11:16:21 -08001026
Amol Jadib6be5c12012-11-14 13:39:51 -08001027 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001028 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001029
Shashank Mittalcd98d472011-08-02 14:29:24 -07001030 offset = imagesize_actual;
1031 /* Read signature */
1032 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1033 {
1034 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001035 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001036 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001037
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001038 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001039
1040 /* Move kernel and ramdisk to correct address */
1041 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1042 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001043#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001044 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001045 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001046 {
1047 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1048 return -1;
1049 }
1050
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001051 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1052#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001053
1054 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001055 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001056 {
1057 write_device_info_flash(&device);
1058 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301059#if USE_PCOM_SECBOOT
1060 set_tamper_flag(device.is_tampered);
1061#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001062 }
1063 else
1064 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001065 offset = page_size;
1066
Amol Jadib6be5c12012-11-14 13:39:51 -08001067 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1068 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1069 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1070
1071 dprintf(INFO, "Loading boot image (%d): start\n",
1072 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001073 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001074
1075 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001076 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1077 return -1;
1078 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001079 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001080
Amol Jadib6be5c12012-11-14 13:39:51 -08001081 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001082 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1083 return -1;
1084 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001085 offset += ramdisk_actual;
1086
1087 dprintf(INFO, "Loading boot image (%d): done\n",
1088 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001089 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001090
1091 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001092 offset += second_actual;
1093 /* Second image loading not implemented. */
1094 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001095 }
1096
1097#if DEVICE_TREE
1098 if(hdr->dt_size != 0) {
1099
1100 /* Read the device tree table into buffer */
1101 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1102 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1103 return -1;
1104 }
1105
1106 table = (struct dt_table*) dt_buf;
1107
Joel Kingaa335dc2013-06-03 16:11:08 -07001108 if (dev_tree_validate(table, hdr->page_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001109 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1110 return -1;
1111 }
1112
Joel Kingaa335dc2013-06-03 16:11:08 -07001113 /* Find index of device tree within device tree table */
1114 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001115 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1116 return -1;
1117 }
1118
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001119 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001120 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001121 {
1122 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1123 return -1;
1124 }
1125
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001126 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001127 if(flash_read(ptn, offset + dt_entry.offset,
1128 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001129 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1130 return -1;
1131 }
1132 }
1133#endif
1134
Shashank Mittalcd98d472011-08-02 14:29:24 -07001135 }
David Ng183a7422009-12-07 14:55:21 -08001136continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001137
Dima Zavin214cc642009-01-26 11:16:21 -08001138 /* TODO: create/pass atags to kernel */
1139
Ajay Dudanie28a6072011-07-01 13:59:46 -07001140 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001141 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001142 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1143
1144 return 0;
1145}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001146
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001147BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001148void write_device_info_mmc(device_info *dev)
1149{
1150 struct device_info *info = (void*) info_buf;
1151 unsigned long long ptn = 0;
1152 unsigned long long size;
1153 int index = INVALID_PTN;
1154
1155 index = partition_get_index("aboot");
1156 ptn = partition_get_offset(index);
1157 if(ptn == 0)
1158 {
1159 return;
1160 }
1161
1162 size = partition_get_size(index);
1163
1164 memcpy(info, dev, sizeof(device_info));
1165
1166 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1167 {
1168 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1169 return;
1170 }
1171}
1172
1173void read_device_info_mmc(device_info *dev)
1174{
1175 struct device_info *info = (void*) info_buf;
1176 unsigned long long ptn = 0;
1177 unsigned long long size;
1178 int index = INVALID_PTN;
1179
1180 index = partition_get_index("aboot");
1181 ptn = partition_get_offset(index);
1182 if(ptn == 0)
1183 {
1184 return;
1185 }
1186
1187 size = partition_get_size(index);
1188
1189 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1190 {
1191 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1192 return;
1193 }
1194
1195 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1196 {
1197 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1198 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001199 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001200 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001201
1202 write_device_info_mmc(info);
1203 }
1204 memcpy(dev, info, sizeof(device_info));
1205}
1206
1207void write_device_info_flash(device_info *dev)
1208{
1209 struct device_info *info = (void *) info_buf;
1210 struct ptentry *ptn;
1211 struct ptable *ptable;
1212
1213 ptable = flash_get_ptable();
1214 if (ptable == NULL)
1215 {
1216 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1217 return;
1218 }
1219
1220 ptn = ptable_find(ptable, "devinfo");
1221 if (ptn == NULL)
1222 {
1223 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1224 return;
1225 }
1226
1227 memcpy(info, dev, sizeof(device_info));
1228
1229 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1230 {
1231 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1232 return;
1233 }
1234}
1235
1236void read_device_info_flash(device_info *dev)
1237{
1238 struct device_info *info = (void*) info_buf;
1239 struct ptentry *ptn;
1240 struct ptable *ptable;
1241
1242 ptable = flash_get_ptable();
1243 if (ptable == NULL)
1244 {
1245 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1246 return;
1247 }
1248
1249 ptn = ptable_find(ptable, "devinfo");
1250 if (ptn == NULL)
1251 {
1252 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1253 return;
1254 }
1255
1256 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1257 {
1258 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1259 return;
1260 }
1261
1262 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1263 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001264 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1265 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001266 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001267 write_device_info_flash(info);
1268 }
1269 memcpy(dev, info, sizeof(device_info));
1270}
1271
1272void write_device_info(device_info *dev)
1273{
1274 if(target_is_emmc_boot())
1275 {
1276 write_device_info_mmc(dev);
1277 }
1278 else
1279 {
1280 write_device_info_flash(dev);
1281 }
1282}
1283
1284void read_device_info(device_info *dev)
1285{
1286 if(target_is_emmc_boot())
1287 {
1288 read_device_info_mmc(dev);
1289 }
1290 else
1291 {
1292 read_device_info_flash(dev);
1293 }
1294}
1295
1296void reset_device_info()
1297{
1298 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001299 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001300 write_device_info(&device);
1301}
1302
1303void set_device_root()
1304{
1305 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001306 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001307 write_device_info(&device);
1308}
1309
Amol Jadicb524072012-08-09 16:40:18 -07001310#if DEVICE_TREE
1311int copy_dtb(uint8_t *boot_image_start)
1312{
1313 uint32 dt_image_offset = 0;
1314 uint32_t n;
1315 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001316 struct dt_entry dt_entry;
Amol Jadicb524072012-08-09 16:40:18 -07001317
1318 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1319
Amol Jadicb524072012-08-09 16:40:18 -07001320 if(hdr->dt_size != 0) {
1321
1322 /* add kernel offset */
1323 dt_image_offset += page_size;
1324 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1325 dt_image_offset += n;
1326
1327 /* add ramdisk offset */
1328 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1329 dt_image_offset += n;
1330
1331 /* add second offset */
1332 if(hdr->second_size != 0) {
1333 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1334 dt_image_offset += n;
1335 }
1336
1337 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001338 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001339
Joel Kingaa335dc2013-06-03 16:11:08 -07001340 if (dev_tree_validate(table, hdr->page_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001341 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1342 return -1;
1343 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001344 /* Find index of device tree within device tree table */
1345 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001346 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1347 return -1;
1348 }
1349
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001350 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001351 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001352 {
1353 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1354 return -1;
1355 }
1356
Amol Jadicb524072012-08-09 16:40:18 -07001357 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001358 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001359 boot_image_start + dt_image_offset + dt_entry.offset,
1360 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001361 } else
1362 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001363
1364 /* Everything looks fine. Return success. */
1365 return 0;
1366}
1367#endif
1368
Brian Swetland9c4c0752009-01-25 16:23:50 -08001369void cmd_boot(const char *arg, void *data, unsigned sz)
1370{
1371 unsigned kernel_actual;
1372 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001373 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001374 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001375 int ret = 0;
1376 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001377
1378 if (sz < sizeof(hdr)) {
1379 fastboot_fail("invalid bootimage header");
1380 return;
1381 }
1382
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001383 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001384
1385 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001386 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001387
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001388 if(target_is_emmc_boot() && hdr->page_size) {
1389 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001390 page_mask = page_size - 1;
1391 }
1392
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001393 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1394 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1395
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001396 /*
1397 * Update the kernel/ramdisk/tags address if the boot image header
1398 * has default values, these default values come from mkbootimg when
1399 * the boot image is flashed using fastboot flash:raw
1400 */
1401 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001402
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001403 /* Get virtual addresses since the hdr saves physical addresses. */
1404 hdr->kernel_addr = VA(hdr->kernel_addr);
1405 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1406 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001407
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001408 /* Check if the addresses in the header are valid. */
1409 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1410 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1411 {
1412 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1413 return -1;
1414 }
1415
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001416 /* sz should have atleast raw boot image */
1417 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001418 fastboot_fail("incomplete bootimage");
1419 return;
1420 }
1421
Amol Jadicb524072012-08-09 16:40:18 -07001422#if DEVICE_TREE
1423 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001424 ret = copy_dtb(data);
1425
1426 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001427#else
1428 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1429 {
1430 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1431 return -1;
1432 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001433#endif
1434
1435 /* Load ramdisk & kernel */
1436 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1437 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1438
1439#if DEVICE_TREE
1440 /*
1441 * If dtb is not found look for appended DTB in the kernel.
1442 * If appended dev tree is found, update the atags with
1443 * memory address to the DTB appended location on RAM.
1444 * Else update with the atags address in the kernel header
1445 */
1446 if (!dtb_copied) {
1447 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001448 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1449 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001450 if (!dtb) {
1451 fastboot_fail("dtb not found");
1452 return;
1453 }
Amol Jadicb524072012-08-09 16:40:18 -07001454 }
1455#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001456
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001457#ifndef DEVICE_TREE
1458 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1459 {
1460 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1461 return -1;
1462 }
1463#endif
1464
Brian Swetland9c4c0752009-01-25 16:23:50 -08001465 fastboot_okay("");
1466 udc_stop();
1467
Dima Zavin77e41f32013-03-06 16:10:43 -08001468 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001469 (const char*) hdr->cmdline, board_machtype(),
1470 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001471}
1472
Dima Zavin214cc642009-01-26 11:16:21 -08001473void cmd_erase(const char *arg, void *data, unsigned sz)
1474{
1475 struct ptentry *ptn;
1476 struct ptable *ptable;
1477
1478 ptable = flash_get_ptable();
1479 if (ptable == NULL) {
1480 fastboot_fail("partition table doesn't exist");
1481 return;
1482 }
1483
1484 ptn = ptable_find(ptable, arg);
1485 if (ptn == NULL) {
1486 fastboot_fail("unknown partition name");
1487 return;
1488 }
1489
1490 if (flash_erase(ptn)) {
1491 fastboot_fail("failed to erase partition");
1492 return;
1493 }
1494 fastboot_okay("");
1495}
1496
Bikas Gurungd48bd242010-09-04 19:54:32 -07001497
1498void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1499{
Kun Liang2f1601a2013-08-12 16:29:54 +08001500 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001501 unsigned long long ptn = 0;
Kun Liang2f1601a2013-08-12 16:29:54 +08001502 unsigned long long size;
Kinson Chikf1a43512011-07-14 11:28:39 -07001503 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001504
Kinson Chikf1a43512011-07-14 11:28:39 -07001505 index = partition_get_index(arg);
1506 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001507
Kinson Chikf1a43512011-07-14 11:28:39 -07001508 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001509 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001510 return;
1511 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001512
1513 size = partition_get_size(index);
1514 if (size > DEFAULT_ERASE_SIZE)
1515 size = DEFAULT_ERASE_SIZE;
1516
neetidb4b24d62012-01-20 12:13:09 -08001517 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001518 0 in first several blocks */
1519 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001520 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001521 return;
1522 }
1523 fastboot_okay("");
1524}
1525
1526
Ajay Dudani5c761132011-04-07 20:19:04 -07001527void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001528{
1529 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001530 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001531 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001532
Greg Grisco6e754772011-06-23 12:19:39 -07001533 if (!strcmp(arg, "partition"))
1534 {
1535 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001536 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001537 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001538 return;
1539 }
1540 }
Greg Grisco6e754772011-06-23 12:19:39 -07001541 else
1542 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001543 index = partition_get_index(arg);
1544 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001545 if(ptn == 0) {
1546 fastboot_fail("partition table doesn't exist");
1547 return;
1548 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001549
Greg Grisco6e754772011-06-23 12:19:39 -07001550 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1551 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1552 fastboot_fail("image is not a boot image");
1553 return;
1554 }
1555 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001556
Kinson Chikf1a43512011-07-14 11:28:39 -07001557 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001558 if (ROUND_TO_PAGE(sz,511) > size) {
1559 fastboot_fail("size too large");
1560 return;
1561 }
1562 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1563 fastboot_fail("flash write failure");
1564 return;
1565 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001566 }
1567 fastboot_okay("");
1568 return;
1569}
1570
Ajay Dudani5c761132011-04-07 20:19:04 -07001571void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1572{
1573 unsigned int chunk;
1574 unsigned int chunk_data_sz;
1575 sparse_header_t *sparse_header;
1576 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001577 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001578 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301579 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001580 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001581
Kinson Chikf1a43512011-07-14 11:28:39 -07001582 index = partition_get_index(arg);
1583 ptn = partition_get_offset(index);
1584 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001585 fastboot_fail("partition table doesn't exist");
1586 return;
1587 }
1588
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301589 size = partition_get_size(index);
1590 if (ROUND_TO_PAGE(sz,511) > size) {
1591 fastboot_fail("size too large");
1592 return;
1593 }
1594
Ajay Dudani5c761132011-04-07 20:19:04 -07001595 /* Read and skip over sparse image header */
1596 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001597 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1598 fastboot_fail("size too large");
1599 return;
1600 }
1601
Ajay Dudani5c761132011-04-07 20:19:04 -07001602 data += sparse_header->file_hdr_sz;
1603 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1604 {
1605 /* Skip the remaining bytes in a header that is longer than
1606 * we expected.
1607 */
1608 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1609 }
1610
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001611 dprintf (SPEW, "=== Sparse Image Header ===\n");
1612 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1613 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1614 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1615 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1616 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1617 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1618 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1619 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001620
1621 /* Start processing chunks */
1622 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1623 {
1624 /* Read and skip over chunk header */
1625 chunk_header = (chunk_header_t *) data;
1626 data += sizeof(chunk_header_t);
1627
1628 dprintf (SPEW, "=== Chunk Header ===\n");
1629 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1630 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1631 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1632
1633 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1634 {
1635 /* Skip the remaining bytes in a header that is longer than
1636 * we expected.
1637 */
1638 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1639 }
1640
1641 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1642 switch (chunk_header->chunk_type)
1643 {
1644 case CHUNK_TYPE_RAW:
1645 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1646 chunk_data_sz))
1647 {
1648 fastboot_fail("Bogus chunk size for chunk type Raw");
1649 return;
1650 }
1651
Ajay Dudaniab18f022011-05-12 14:39:22 -07001652 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1653 chunk_data_sz,
1654 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001655 {
1656 fastboot_fail("flash write failure");
1657 return;
1658 }
1659 total_blocks += chunk_header->chunk_sz;
1660 data += chunk_data_sz;
1661 break;
1662
1663 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001664 total_blocks += chunk_header->chunk_sz;
1665 break;
1666
Ajay Dudani5c761132011-04-07 20:19:04 -07001667 case CHUNK_TYPE_CRC:
1668 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1669 {
1670 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1671 return;
1672 }
1673 total_blocks += chunk_header->chunk_sz;
1674 data += chunk_data_sz;
1675 break;
1676
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001677 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001678 fastboot_fail("Unknown chunk type");
1679 return;
1680 }
1681 }
1682
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001683 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1684 total_blocks, sparse_header->total_blks);
1685
1686 if(total_blocks != sparse_header->total_blks)
1687 {
1688 fastboot_fail("sparse image write failure");
1689 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001690
1691 fastboot_okay("");
1692 return;
1693}
1694
1695void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1696{
1697 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001698 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1699 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001700
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001701#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001702 int ret=0;
1703 uint32 major_version=0;
1704 uint32 minor_version=0;
1705
1706 ret = scm_svc_version(&major_version,&minor_version);
1707 if(!ret)
1708 {
1709 if(major_version >= 2)
1710 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001711 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001712 {
1713 ret = encrypt_scm((uint32 **) &data, &sz);
1714 if (ret != 0) {
1715 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1716 return;
1717 }
1718
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001719 /* Protect only for SSD */
1720 if (!strcmp(arg, "ssd")) {
1721 ret = scm_protect_keystore((uint32 *) data, sz);
1722 if (ret != 0) {
1723 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1724 return;
1725 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001726 }
1727 }
1728 else
1729 {
1730 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1731 if(ret != 0)
1732 {
1733 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1734 return;
1735 }
1736 }
1737 }
1738 else
1739 {
1740 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1741 magic_number[1] == DECRYPT_MAGIC_1)
1742 {
1743 ret = decrypt_scm((uint32 **) &data, &sz);
1744 if (ret != 0) {
1745 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1746 return;
1747 }
1748 }
1749 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1750 magic_number[1] == ENCRYPT_MAGIC_1)
1751 {
1752 ret = encrypt_scm((uint32 **) &data, &sz);
1753 if (ret != 0) {
1754 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1755 return;
1756 }
1757 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001758 }
1759 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001760 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001761 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001762 dprintf(CRITICAL,"INVALID SVC Version\n");
1763 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001764 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001765#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001766
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001767 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001768 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1769 cmd_flash_mmc_img(arg, data, sz);
1770 else
1771 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001772 return;
1773}
1774
Dima Zavin214cc642009-01-26 11:16:21 -08001775void cmd_flash(const char *arg, void *data, unsigned sz)
1776{
1777 struct ptentry *ptn;
1778 struct ptable *ptable;
1779 unsigned extra = 0;
1780
1781 ptable = flash_get_ptable();
1782 if (ptable == NULL) {
1783 fastboot_fail("partition table doesn't exist");
1784 return;
1785 }
1786
1787 ptn = ptable_find(ptable, arg);
1788 if (ptn == NULL) {
1789 fastboot_fail("unknown partition name");
1790 return;
1791 }
1792
1793 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1794 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1795 fastboot_fail("image is not a boot image");
1796 return;
1797 }
1798 }
1799
Amol Jadi5c61a952012-05-04 17:05:35 -07001800 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001801 || !strcmp(ptn->name, "userdata")
1802 || !strcmp(ptn->name, "persist")
1803 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301804 if (flash_ecc_bch_enabled())
1805 /* Spare data bytes for 8 bit ECC increased by 4 */
1806 extra = ((page_size >> 9) * 20);
1807 else
1808 extra = ((page_size >> 9) * 16);
1809 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001810 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001811
1812 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1813 if (flash_write(ptn, extra, data, sz)) {
1814 fastboot_fail("flash write failure");
1815 return;
1816 }
1817 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1818 fastboot_okay("");
1819}
1820
1821void cmd_continue(const char *arg, void *data, unsigned sz)
1822{
1823 fastboot_okay("");
1824 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001825 if (target_is_emmc_boot())
1826 {
1827 boot_linux_from_mmc();
1828 }
1829 else
1830 {
1831 boot_linux_from_flash();
1832 }
Dima Zavin214cc642009-01-26 11:16:21 -08001833}
1834
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001835void cmd_reboot(const char *arg, void *data, unsigned sz)
1836{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001837 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001838 fastboot_okay("");
1839 reboot_device(0);
1840}
1841
1842void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1843{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001844 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001845 fastboot_okay("");
1846 reboot_device(FASTBOOT_MODE);
1847}
1848
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001849void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
1850{
1851 dprintf(INFO, "Enabling charger screen check\n");
1852 device.charger_screen_enabled = 1;
1853 write_device_info(&device);
1854 fastboot_okay("");
1855}
1856
1857void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
1858{
1859 dprintf(INFO, "Disabling charger screen check\n");
1860 device.charger_screen_enabled = 0;
1861 write_device_info(&device);
1862 fastboot_okay("");
1863}
1864
Shashank Mittal162244e2011-08-08 19:01:25 -07001865void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1866{
1867 if(!device.is_unlocked)
1868 {
1869 device.is_unlocked = 1;
1870 write_device_info(&device);
1871 }
1872 fastboot_okay("");
1873}
1874
Shashank Mittala0032282011-08-26 14:50:11 -07001875void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1876{
1877 char response[64];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001878 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07001879 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001880 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1881 fastboot_info(response);
1882 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07001883 fastboot_info(response);
1884 fastboot_okay("");
1885}
1886
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001887void cmd_preflash(const char *arg, void *data, unsigned sz)
1888{
1889 fastboot_okay("");
1890}
1891
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001892void splash_screen ()
1893{
1894 struct ptentry *ptn;
1895 struct ptable *ptable;
1896 struct fbcon_config *fb_display = NULL;
1897
1898 if (!target_is_emmc_boot())
1899 {
1900 ptable = flash_get_ptable();
1901 if (ptable == NULL) {
1902 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001903 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001904 }
1905
1906 ptn = ptable_find(ptable, "splash");
1907 if (ptn == NULL) {
1908 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1909 } else {
1910 fb_display = fbcon_display();
1911 if (fb_display) {
1912 if (flash_read(ptn, 0, fb_display->base,
1913 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1914 fbcon_clear();
1915 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1916 }
1917 }
1918 }
1919 }
1920}
1921
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001922/* Get the size from partiton name */
1923static void get_partition_size(const char *arg, char *response)
1924{
1925 uint64_t ptn = 0;
1926 uint64_t size;
1927 int index = INVALID_PTN;
1928
1929 index = partition_get_index(arg);
1930
1931 if (index == INVALID_PTN)
1932 {
1933 dprintf(CRITICAL, "Invalid partition index\n");
1934 return;
1935 }
1936
1937 ptn = partition_get_offset(index);
1938
1939 if(!ptn)
1940 {
1941 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1942 return;
1943 }
1944
1945 size = partition_get_size(index);
1946
1947 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1948 return;
1949}
1950
1951/*
1952 * Publish the partition type & size info
1953 * fastboot getvar will publish the required information.
1954 * fastboot getvar partition_size:<partition_name>: partition size in hex
1955 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1956 */
1957static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1958{
1959 uint8_t i;
1960
1961 for (i = 0; i < num_parts; i++) {
1962 get_partition_size(info[i].part_name, info[i].size_response);
1963
1964 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1965 {
1966 dprintf(CRITICAL, "partition size name truncated\n");
1967 return;
1968 }
1969 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1970 {
1971 dprintf(CRITICAL, "partition type name truncated\n");
1972 return;
1973 }
1974
1975 /* publish partition size & type info */
1976 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1977 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1978 }
1979}
1980
Amol Jadi5edf3552013-07-23 14:15:34 -07001981/* register commands and variables for fastboot */
1982void aboot_fastboot_register_commands(void)
1983{
1984 if (target_is_emmc_boot())
1985 {
1986 fastboot_register("flash:", cmd_flash_mmc);
1987 fastboot_register("erase:", cmd_erase_mmc);
1988 }
1989 else
1990 {
1991 fastboot_register("flash:", cmd_flash);
1992 fastboot_register("erase:", cmd_erase);
1993 }
1994
1995 fastboot_register("boot", cmd_boot);
1996 fastboot_register("continue", cmd_continue);
1997 fastboot_register("reboot", cmd_reboot);
1998 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
1999 fastboot_register("oem unlock", cmd_oem_unlock);
2000 fastboot_register("oem device-info", cmd_oem_devinfo);
2001 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002002 fastboot_register("oem enable-charger-screen",
2003 cmd_oem_enable_charger_screen);
2004 fastboot_register("oem disable-charger-screen",
2005 cmd_oem_disable_charger_screen);
Amol Jadi5edf3552013-07-23 14:15:34 -07002006 /* publish variables and their values */
2007 fastboot_publish("product", TARGET(BOARD));
2008 fastboot_publish("kernel", "lk");
2009 fastboot_publish("serialno", sn_buf);
2010
2011 /*
2012 * partition info is supported only for emmc partitions
2013 * Calling this for NAND prints some error messages which
2014 * is harmless but misleading. Avoid calling this for NAND
2015 * devices.
2016 */
2017 if (target_is_emmc_boot())
2018 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2019
2020 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002021 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2022 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002023 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002024 /* Is the charger screen check enabled */
2025 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2026 device.charger_screen_enabled);
2027 fastboot_publish("charger-screen-enabled",
2028 (const char *) charger_screen_enabled);
Amol Jadi5edf3552013-07-23 14:15:34 -07002029}
2030
Brian Swetland9c4c0752009-01-25 16:23:50 -08002031void aboot_init(const struct app_descriptor *app)
2032{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002033 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002034 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002035
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002036 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002037 if (target_is_emmc_boot())
2038 {
2039 page_size = 2048;
2040 page_mask = page_size - 1;
2041 }
2042 else
2043 {
2044 page_size = flash_page_size();
2045 page_mask = page_size - 1;
2046 }
2047
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002048 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2049
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002050 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002051
Greg Griscod6250552011-06-29 14:40:23 -07002052 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002053 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002054
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002055 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002056 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002057 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002058 dprintf(ALWAYS,"dload mode key sequence detected\n");
2059 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002060 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002061 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002062 }
2063 else
2064 {
2065 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002066 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002067 }
2068 boot_into_fastboot = true;
2069 }
2070 if (!boot_into_fastboot)
2071 {
2072 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2073 boot_into_recovery = 1;
2074 if (!boot_into_recovery &&
2075 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002076 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002077 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002078 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002079 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002080 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002081 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002082
Ajay Dudani77421292010-10-27 19:34:06 -07002083 reboot_mode = check_reboot_mode();
2084 if (reboot_mode == RECOVERY_MODE) {
2085 boot_into_recovery = 1;
2086 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002087 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002088 }
2089
Pavel Nedev5d91d412013-04-29 11:34:24 +03002090 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002091 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002092 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002093 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002094 if(emmc_recovery_init())
2095 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2096 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002097 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002098 if((device.is_unlocked) || (device.is_tampered))
2099 {
2100 #ifdef TZ_TAMPER_FUSE
2101 set_tamper_fuse_cmd();
2102 #endif
2103 #if USE_PCOM_SECBOOT
2104 set_tamper_flag(device.is_tampered);
2105 #endif
2106 }
Shashank Mittala0032282011-08-26 14:50:11 -07002107 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002108 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002109 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002110 else
2111 {
2112 recovery_init();
2113 #if USE_PCOM_SECBOOT
2114 if((device.is_unlocked) || (device.is_tampered))
2115 set_tamper_flag(device.is_tampered);
2116 #endif
2117 boot_linux_from_flash();
2118 }
2119 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2120 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002121 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002122
Amol Jadi5edf3552013-07-23 14:15:34 -07002123 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002124
Amol Jadi5edf3552013-07-23 14:15:34 -07002125 /* register aboot specific fastboot commands */
2126 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002127
Amol Jadi5edf3552013-07-23 14:15:34 -07002128 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002129 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002130
2131 /* initialize and start fastboot */
2132 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002133}
2134
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002135uint32_t get_page_size()
2136{
2137 return page_size;
2138}
2139
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002140/*
2141 * Calculated and save hash (SHA256) for non-signed boot image.
2142 *
2143 * Hash the same data that is checked on the signed boot image.
2144 * Kernel and Ramdisk are already read to memory buffers.
2145 * Need to read the entire device-tree from mmc
2146 * since non-signed image only read the DT tags of the relevant platform.
2147 *
2148 * @param kernel_addr - kernel bufer
2149 * @param kernel_actual - kernel size in bytes
2150 * @param ramdisk_addr - ramdisk buffer
2151 * @param ramdisk_actual - ramdisk size
2152 * @param ptn - partition
2153 * @param dt_offset - device tree offset on mmc partition
2154 * @param dt_size
2155 *
2156 * @return int - 0 on success, negative value on failure.
2157 */
2158int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2159 void *ramdisk_addr, unsigned ramdisk_actual,
2160 unsigned long long ptn,
2161 unsigned dt_offset, unsigned dt_size)
2162{
2163 SHA256_CTX sha256_ctx;
2164 char digest[32]={0};
2165 char *buf = (char *)target_get_scratch_address();
2166 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2167 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2168
2169 SHA256_Init(&sha256_ctx);
2170
2171 /* Read Boot Header */
2172 if (mmc_read(ptn, buf, page_size))
2173 {
2174 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2175 return -1;
2176 }
2177 /* Read entire Device Tree */
2178 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2179 {
2180 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2181 return -1;
2182 }
2183 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2184 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2185 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2186 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2187
2188 SHA256_Final(digest, &sha256_ctx);
2189
2190 save_kernel_hash_cmd(digest);
2191 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2192
2193 return 0;
2194}
2195
Brian Swetland9c4c0752009-01-25 16:23:50 -08002196APP_START(aboot)
2197 .init = aboot_init,
2198APP_END