blob: 0322505c505f8d61b3a4ffe574ac53a7f0dfc46a [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
David Ng183a7422009-12-07 14:55:21 -080096static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080097static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +030098static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -080099static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800100static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700101static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300102static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800103
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800104static const char *baseband_apq = " androidboot.baseband=apq";
105static const char *baseband_msm = " androidboot.baseband=msm";
106static const char *baseband_csfb = " androidboot.baseband=csfb";
107static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700108static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700109static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800110static const char *baseband_dsda = " androidboot.baseband=dsda";
111static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800112static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800113
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700114static unsigned page_size = 0;
115static unsigned page_mask = 0;
116static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
117static bool boot_into_ffbm;
118
Shashank Mittalcd98d472011-08-02 14:29:24 -0700119/* Assuming unauthorized kernel image by default */
120static int auth_kernel_img = 0;
121
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700122static device_info device = {DEVICE_MAGIC, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700123
Dima Zavin42168f22009-01-30 11:52:22 -0800124struct atag_ptbl_entry
125{
126 char name[16];
127 unsigned offset;
128 unsigned size;
129 unsigned flags;
130};
131
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700132/*
133 * Partition info, required to be published
134 * for fastboot
135 */
136struct getvar_partition_info {
137 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
138 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
139 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
140 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
141 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
142};
143
144/*
145 * Right now, we are publishing the info for only
146 * three partitions
147 */
148struct getvar_partition_info part_info[] =
149{
150 { "system" , "partition-size:", "partition-type:", "", "ext4" },
151 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
152 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
153};
154
155char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700156char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800157char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700158
Greg Griscod2471ef2011-07-14 13:00:42 -0700159extern int emmc_recovery_init(void);
160
Kinson Chik0b1c8162011-08-31 16:31:57 -0700161#if NO_KEYPAD_DRIVER
162extern int fastboot_trigger(void);
163#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700164
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700165static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
166{
167 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700168#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
169 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
170 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
171 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700172#endif
173}
174
Dima Zavin42168f22009-01-30 11:52:22 -0800175static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
176{
177 struct atag_ptbl_entry atag_ptn;
178
179 memcpy(atag_ptn.name, ptn->name, 16);
180 atag_ptn.name[15] = '\0';
181 atag_ptn.offset = ptn->start;
182 atag_ptn.size = ptn->length;
183 atag_ptn.flags = ptn->flags;
184 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
185 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
186}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800187
Neeti Desaie245d492012-06-01 12:52:13 -0700188unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800189{
David Ng183a7422009-12-07 14:55:21 -0800190 int cmdline_len = 0;
191 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800192 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700193 int pause_at_bootup = 0;
Pavel Nedev5614d222013-06-17 18:01:02 +0300194 bool gpt_exists = partition_gpt_exists();
Dima Zavin42168f22009-01-30 11:52:22 -0800195
Brian Swetland9c4c0752009-01-25 16:23:50 -0800196 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800197 cmdline_len = strlen(cmdline);
198 have_cmdline = 1;
199 }
200 if (target_is_emmc_boot()) {
201 cmdline_len += strlen(emmc_cmdline);
202 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800203
204 cmdline_len += strlen(usb_sn_cmdline);
205 cmdline_len += strlen(sn_buf);
206
Pavel Nedev5614d222013-06-17 18:01:02 +0300207 if (boot_into_recovery && gpt_exists)
208 cmdline_len += strlen(secondary_gpt_enable);
209
Pavel Nedev328ac822013-04-05 15:25:11 +0300210 if (boot_into_ffbm) {
211 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700212 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800213 /* reduce kernel console messages to speed-up boot */
214 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700215 } else if (device.charger_screen_enabled &&
216 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700217 pause_at_bootup = 1;
218 cmdline_len += strlen(battchg_pause);
219 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800220
Shashank Mittalcd98d472011-08-02 14:29:24 -0700221 if(target_use_signed_kernel() && auth_kernel_img) {
222 cmdline_len += strlen(auth_kernel);
223 }
224
Ajay Dudanid04110c2011-01-17 23:55:07 -0800225 /* Determine correct androidboot.baseband to use */
226 switch(target_baseband())
227 {
228 case BASEBAND_APQ:
229 cmdline_len += strlen(baseband_apq);
230 break;
231
232 case BASEBAND_MSM:
233 cmdline_len += strlen(baseband_msm);
234 break;
235
236 case BASEBAND_CSFB:
237 cmdline_len += strlen(baseband_csfb);
238 break;
239
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800240 case BASEBAND_SVLTE2A:
241 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800242 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700243
244 case BASEBAND_MDM:
245 cmdline_len += strlen(baseband_mdm);
246 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700247
248 case BASEBAND_SGLTE:
249 cmdline_len += strlen(baseband_sglte);
250 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530251
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800252 case BASEBAND_SGLTE2:
253 cmdline_len += strlen(baseband_sglte2);
254 break;
255
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530256 case BASEBAND_DSDA:
257 cmdline_len += strlen(baseband_dsda);
258 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800259
260 case BASEBAND_DSDA2:
261 cmdline_len += strlen(baseband_dsda2);
262 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800263 }
264
David Ng183a7422009-12-07 14:55:21 -0800265 if (cmdline_len > 0) {
266 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700267 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
268 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700269
Amol Jadi168b7712012-03-06 16:15:00 -0800270 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700271 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800272 if (have_cmdline) {
273 src = cmdline;
274 while ((*dst++ = *src++));
275 }
276 if (target_is_emmc_boot()) {
277 src = emmc_cmdline;
278 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700279 have_cmdline = 1;
280 while ((*dst++ = *src++));
281 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800282
283 src = usb_sn_cmdline;
284 if (have_cmdline) --dst;
285 have_cmdline = 1;
286 while ((*dst++ = *src++));
287 src = sn_buf;
288 if (have_cmdline) --dst;
289 have_cmdline = 1;
290 while ((*dst++ = *src++));
291
Pavel Nedev5614d222013-06-17 18:01:02 +0300292 if (boot_into_recovery && gpt_exists) {
293 src = secondary_gpt_enable;
294 if (have_cmdline) --dst;
295 while ((*dst++ = *src++));
296 }
297
Pavel Nedev328ac822013-04-05 15:25:11 +0300298 if (boot_into_ffbm) {
299 src = androidboot_mode;
300 if (have_cmdline) --dst;
301 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700302 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300303 if (have_cmdline) --dst;
304 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800305 src = loglevel;
306 if (have_cmdline) --dst;
307 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300308 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700309 src = battchg_pause;
310 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800311 while ((*dst++ = *src++));
312 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800313
Shashank Mittalcd98d472011-08-02 14:29:24 -0700314 if(target_use_signed_kernel() && auth_kernel_img) {
315 src = auth_kernel;
316 if (have_cmdline) --dst;
317 while ((*dst++ = *src++));
318 }
319
Ajay Dudanid04110c2011-01-17 23:55:07 -0800320 switch(target_baseband())
321 {
322 case BASEBAND_APQ:
323 src = baseband_apq;
324 if (have_cmdline) --dst;
325 while ((*dst++ = *src++));
326 break;
327
328 case BASEBAND_MSM:
329 src = baseband_msm;
330 if (have_cmdline) --dst;
331 while ((*dst++ = *src++));
332 break;
333
334 case BASEBAND_CSFB:
335 src = baseband_csfb;
336 if (have_cmdline) --dst;
337 while ((*dst++ = *src++));
338 break;
339
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800340 case BASEBAND_SVLTE2A:
341 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800342 if (have_cmdline) --dst;
343 while ((*dst++ = *src++));
344 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700345
346 case BASEBAND_MDM:
347 src = baseband_mdm;
348 if (have_cmdline) --dst;
349 while ((*dst++ = *src++));
350 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700351
352 case BASEBAND_SGLTE:
353 src = baseband_sglte;
354 if (have_cmdline) --dst;
355 while ((*dst++ = *src++));
356 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530357
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800358 case BASEBAND_SGLTE2:
359 src = baseband_sglte2;
360 if (have_cmdline) --dst;
361 while ((*dst++ = *src++));
362 break;
363
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530364 case BASEBAND_DSDA:
365 src = baseband_dsda;
366 if (have_cmdline) --dst;
367 while ((*dst++ = *src++));
368 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800369
370 case BASEBAND_DSDA2:
371 src = baseband_dsda2;
372 if (have_cmdline) --dst;
373 while ((*dst++ = *src++));
374 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800375 }
Neeti Desaie245d492012-06-01 12:52:13 -0700376 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800377 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700378 return cmdline_final;
379}
380
381unsigned *atag_core(unsigned *ptr)
382{
383 /* CORE */
384 *ptr++ = 2;
385 *ptr++ = 0x54410001;
386
387 return ptr;
388
389}
390
391unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
392 unsigned ramdisk_size)
393{
394 if (ramdisk_size) {
395 *ptr++ = 4;
396 *ptr++ = 0x54420005;
397 *ptr++ = (unsigned)ramdisk;
398 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800399 }
400
Neeti Desaie245d492012-06-01 12:52:13 -0700401 return ptr;
402}
403
404unsigned *atag_ptable(unsigned **ptr_addr)
405{
406 int i;
407 struct ptable *ptable;
408
409 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700410 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
411 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700412 *(*ptr_addr)++ = 0x4d534d70;
413 for (i = 0; i < ptable->count; ++i)
414 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
415 }
416
417 return (*ptr_addr);
418}
419
420unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
421{
422 int cmdline_length = 0;
423 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700424 char *dest;
425
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800426 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700427 n = (cmdline_length + 4) & (~3);
428
429 *ptr++ = (n / 4) + 2;
430 *ptr++ = 0x54410009;
431 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800432 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700433 ptr += (n / 4);
434
435 return ptr;
436}
437
438unsigned *atag_end(unsigned *ptr)
439{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800440 /* END */
441 *ptr++ = 0;
442 *ptr++ = 0;
443
Neeti Desaie245d492012-06-01 12:52:13 -0700444 return ptr;
445}
446
447void generate_atags(unsigned *ptr, const char *cmdline,
448 void *ramdisk, unsigned ramdisk_size)
449{
450
451 ptr = atag_core(ptr);
452 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
453 ptr = target_atag_mem(ptr);
454
455 /* Skip NAND partition ATAGS for eMMC boot */
456 if (!target_is_emmc_boot()){
457 ptr = atag_ptable(&ptr);
458 }
459
460 ptr = atag_cmdline(ptr, cmdline);
461 ptr = atag_end(ptr);
462}
463
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700464typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700465void boot_linux(void *kernel, unsigned *tags,
466 const char *cmdline, unsigned machtype,
467 void *ramdisk, unsigned ramdisk_size)
468{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800469 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800470#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700471 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800472#endif
473
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700474 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800475 uint32_t tags_phys = PA((addr_t)tags);
476
477 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700478
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800479 final_cmdline = update_cmdline((const char*)cmdline);
480
Neeti Desai17379b82012-06-04 18:42:53 -0700481#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800482 dprintf(INFO, "Updating device tree: start\n");
483
Neeti Desai17379b82012-06-04 18:42:53 -0700484 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800485 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700486 if(ret)
487 {
488 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
489 ASSERT(0);
490 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800491 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700492#else
Neeti Desaie245d492012-06-01 12:52:13 -0700493 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800494 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700495#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700496
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700497 /* Perform target specific cleanup */
498 target_uninit();
499
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800500 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
501 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800502
503 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700504
Amol Jadi4421e652011-06-16 15:00:48 -0700505 /* do any platform specific cleanup before kernel entry */
506 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700507
Brian Swetland9c4c0752009-01-25 16:23:50 -0800508 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700509
Amol Jadi504f9fe2012-08-16 13:56:48 -0700510#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800511 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700512#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700513 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700514 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800515}
516
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700517/* Function to check if the memory address range falls within the aboot
518 * boundaries.
519 * start: Start of the memory region
520 * size: Size of the memory region
521 */
522int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
523{
524 /* Check for boundary conditions. */
525 if ((start + size) < start)
526 return -1;
527
528 /* Check for memory overlap. */
529 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
530 return 0;
531 else if (start > (MEMBASE + MEMSIZE))
532 return 0;
533 else
534 return -1;
535}
536
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800537#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800538
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700539BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800540#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700541BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800542#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800543
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700544static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
545{
546 int ret;
547
548 /* Assume device is rooted at this time. */
549 device.is_tampered = 1;
550
551 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
552
553 ret = image_verify((unsigned char *)bootimg_addr,
554 (unsigned char *)(bootimg_addr + bootimg_size),
555 bootimg_size,
556 CRYPTO_AUTH_ALG_SHA256);
557
558 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
559
560 if (ret)
561 {
562 /* Authorized kernel */
563 device.is_tampered = 0;
564 }
565
566#if USE_PCOM_SECBOOT
567 set_tamper_flag(device.is_tampered);
568#endif
569
570 if(device.is_tampered)
571 {
572 write_device_info_mmc(&device);
573 #ifdef TZ_TAMPER_FUSE
574 set_tamper_fuse_cmd();
575 #endif
576 #ifdef ASSERT_ON_TAMPER
577 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
578 ASSERT(0);
579 #endif
580 }
581}
582
Shashank Mittal23b8f422010-04-16 19:27:21 -0700583int boot_linux_from_mmc(void)
584{
585 struct boot_img_hdr *hdr = (void*) buf;
586 struct boot_img_hdr *uhdr;
587 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700588 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700589 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700590 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700591
Shashank Mittalcd98d472011-08-02 14:29:24 -0700592 unsigned char *image_addr = 0;
593 unsigned kernel_actual;
594 unsigned ramdisk_actual;
595 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700596 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700597
598#if DEVICE_TREE
599 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700600 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700601 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800602 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700603 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700604#endif
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700605 if (!boot_into_recovery) {
606 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
607 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
608 if (rcode <= 0) {
609 boot_into_ffbm = false;
610 if (rcode < 0)
611 dprintf(CRITICAL,"failed to get ffbm cookie");
612 } else
613 boot_into_ffbm = true;
614 } else
615 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700616 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
617 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
618 dprintf(INFO, "Unified boot method!\n");
619 hdr = uhdr;
620 goto unified_boot;
621 }
Greg Griscod6250552011-06-29 14:40:23 -0700622 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700623 index = partition_get_index("boot");
624 ptn = partition_get_offset(index);
625 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700626 dprintf(CRITICAL, "ERROR: No boot partition found\n");
627 return -1;
628 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700629 }
630 else {
631 index = partition_get_index("recovery");
632 ptn = partition_get_offset(index);
633 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700634 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
635 return -1;
636 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700637 }
638
Greg Griscod6250552011-06-29 14:40:23 -0700639 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700640 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
641 return -1;
642 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700643
644 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700645 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700646 return -1;
647 }
648
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700649 if (hdr->page_size && (hdr->page_size != page_size)) {
650 page_size = hdr->page_size;
651 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700652 }
653
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700654 /*
655 * Update the kernel/ramdisk/tags address if the boot image header
656 * has default values, these default values come from mkbootimg when
657 * the boot image is flashed using fastboot flash:raw
658 */
659 update_ker_tags_rdisk_addr(hdr);
660
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700661 /* Get virtual addresses since the hdr saves physical addresses. */
662 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
663 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
664 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700665
666 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
667 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
668
669 /* Check if the addresses in the header are valid. */
670 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
671 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
672 {
673 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
674 return -1;
675 }
676
677#ifndef DEVICE_TREE
678 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
679 {
680 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
681 return -1;
682 }
683#endif
684
Shashank Mittalcd98d472011-08-02 14:29:24 -0700685 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300686 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
687 (int) target_use_signed_kernel(),
688 device.is_unlocked,
689 device.is_tampered);
690
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700691 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800692 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700693 offset = 0;
694
Shashank Mittalcd98d472011-08-02 14:29:24 -0700695 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700696
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800697#if DEVICE_TREE
698 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
699 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700700
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700701 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700702 {
703 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
704 return -1;
705 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800706#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530707 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700708
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700709#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700710
Amol Jadib6be5c12012-11-14 13:39:51 -0800711 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700712 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800713
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700714 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
715 {
716 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
717 return -1;
718 }
719
Shashank Mittalcd98d472011-08-02 14:29:24 -0700720 /* Read image without signature */
721 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
722 {
723 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
724 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800725 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700726
Amol Jadib6be5c12012-11-14 13:39:51 -0800727 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700728 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800729
Shashank Mittalcd98d472011-08-02 14:29:24 -0700730 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700731
732 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
733 {
734 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
735 return -1;
736 }
737
Shashank Mittalcd98d472011-08-02 14:29:24 -0700738 /* Read signature */
739 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
740 {
741 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700742 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700743 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800744
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700745 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700746
Neeti Desai465491e2012-07-31 12:53:35 -0700747 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700748 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
749 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700750
Neeti Desai465491e2012-07-31 12:53:35 -0700751 #if DEVICE_TREE
752 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700753 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700754 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700755
Deepa Dinamani19648b42013-09-05 17:05:55 -0700756 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700757 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
758 return -1;
759 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700760
Neeti Desai465491e2012-07-31 12:53:35 -0700761 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700762 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700763 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
764 return -1;
765 }
766
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700767 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700768 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700769 {
770 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
771 return -1;
772 }
773
Joel Kingaa335dc2013-06-03 16:11:08 -0700774 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800775 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800776 /*
777 * If appended dev tree is found, update the atags with
778 * memory address to the DTB appended location on RAM.
779 * Else update with the atags address in the kernel header
780 */
781 void *dtb;
782 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700783 hdr->kernel_size,
784 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800785 if (!dtb) {
786 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
787 return -1;
788 }
Neeti Desai465491e2012-07-31 12:53:35 -0700789 }
790 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700791 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700792 else
793 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800794 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700795
Amol Jadib6be5c12012-11-14 13:39:51 -0800796 dprintf(INFO, "Loading boot image (%d): start\n",
797 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700798 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800799
800 offset = page_size;
801
802 /* Load kernel */
803 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700804 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
805 return -1;
806 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800807 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700808
Amol Jadib6be5c12012-11-14 13:39:51 -0800809 /* Load ramdisk */
810 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700811 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800812 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700813 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
814 return -1;
815 }
816 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800817 offset += ramdisk_actual;
818
819 dprintf(INFO, "Loading boot image (%d): done\n",
820 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700821 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700822
823 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800824 offset += second_actual;
825 /* Second image loading not implemented. */
826 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700827 }
828
829 #if DEVICE_TREE
830 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -0700831 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -0700832 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
833 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
834 return -1;
835 }
836 table = (struct dt_table*) dt_buf;
837
Deepa Dinamani19648b42013-09-05 17:05:55 -0700838 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700839 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
840 return -1;
841 }
842
Deepa Dinamani19648b42013-09-05 17:05:55 -0700843 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
844 if (!table)
845 return -1;
846
847 /* Read the entire device tree table into buffer */
848 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
849 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
850 return -1;
851 }
852
Joel Kingaa335dc2013-06-03 16:11:08 -0700853 /* Find index of device tree within device tree table */
854 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700855 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
856 return -1;
857 }
858
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700859 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700860 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700861 {
862 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
863 return -1;
864 }
865
Joel Kingaa335dc2013-06-03 16:11:08 -0700866 if(mmc_read(ptn + offset + dt_entry.offset,
867 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700868 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
869 return -1;
870 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300871 #ifdef TZ_SAVE_KERNEL_HASH
872 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
873 hdr->ramdisk_addr, ramdisk_actual,
874 ptn, offset, hdr->dt_size);
875 #endif /* TZ_SAVE_KERNEL_HASH */
876
Channagoud Kadabi35095622013-03-01 13:53:05 -0800877 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800878 /*
879 * If appended dev tree is found, update the atags with
880 * memory address to the DTB appended location on RAM.
881 * Else update with the atags address in the kernel header
882 */
883 void *dtb;
884 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700885 kernel_actual,
886 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800887 if (!dtb) {
888 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
889 return -1;
890 }
Neeti Desai465491e2012-07-31 12:53:35 -0700891 }
892 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700893 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700894
Stanimir Varbanov69ec5462013-07-18 18:17:42 +0300895 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
896 target_load_ssd_keystore();
897
Shashank Mittal23b8f422010-04-16 19:27:21 -0700898unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700899
Dima Zavin77e41f32013-03-06 16:10:43 -0800900 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700901 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700902 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
903
904 return 0;
905}
906
Dima Zavin214cc642009-01-26 11:16:21 -0800907int boot_linux_from_flash(void)
908{
909 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800910 struct ptentry *ptn;
911 struct ptable *ptable;
912 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800913
Shashank Mittalcd98d472011-08-02 14:29:24 -0700914 unsigned char *image_addr = 0;
915 unsigned kernel_actual;
916 unsigned ramdisk_actual;
917 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800918 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700919
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700920#if DEVICE_TREE
921 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700922 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800923 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700924 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700925#endif
926
David Ng183a7422009-12-07 14:55:21 -0800927 if (target_is_emmc_boot()) {
928 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
929 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
930 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
931 return -1;
932 }
933 goto continue_boot;
934 }
935
Dima Zavin214cc642009-01-26 11:16:21 -0800936 ptable = flash_get_ptable();
937 if (ptable == NULL) {
938 dprintf(CRITICAL, "ERROR: Partition table not found\n");
939 return -1;
940 }
941
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800942 if(!boot_into_recovery)
943 {
944 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700945
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800946 if (ptn == NULL) {
947 dprintf(CRITICAL, "ERROR: No boot partition found\n");
948 return -1;
949 }
950 }
951 else
952 {
953 ptn = ptable_find(ptable, "recovery");
954 if (ptn == NULL) {
955 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
956 return -1;
957 }
Dima Zavin214cc642009-01-26 11:16:21 -0800958 }
959
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800960 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800961 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
962 return -1;
963 }
Dima Zavin214cc642009-01-26 11:16:21 -0800964
965 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700966 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800967 return -1;
968 }
969
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800970 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700971 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 -0800972 return -1;
973 }
974
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700975 /*
976 * Update the kernel/ramdisk/tags address if the boot image header
977 * has default values, these default values come from mkbootimg when
978 * the boot image is flashed using fastboot flash:raw
979 */
980 update_ker_tags_rdisk_addr(hdr);
981
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700982 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700983 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
984 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
985 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
986
987 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
988 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
989
990 /* Check if the addresses in the header are valid. */
991 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
992 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
993 {
994 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
995 return -1;
996 }
997
998#ifndef DEVICE_TREE
999 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1000 {
1001 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1002 return -1;
1003 }
1004#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001005
Shashank Mittalcd98d472011-08-02 14:29:24 -07001006 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001007 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001008 {
1009 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001010 offset = 0;
1011
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001012#if DEVICE_TREE
1013 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1014 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001015
1016 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1017 {
1018 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1019 return -1;
1020 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001021#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001022 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001023#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001024
Amol Jadib6be5c12012-11-14 13:39:51 -08001025 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001026 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001027
Shashank Mittalcd98d472011-08-02 14:29:24 -07001028 /* Read image without signature */
1029 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1030 {
1031 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1032 return -1;
1033 }
Dima Zavin214cc642009-01-26 11:16:21 -08001034
Amol Jadib6be5c12012-11-14 13:39:51 -08001035 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001036 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001037
Shashank Mittalcd98d472011-08-02 14:29:24 -07001038 offset = imagesize_actual;
1039 /* Read signature */
1040 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1041 {
1042 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001043 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001044 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001045
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001046 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001047
1048 /* Move kernel and ramdisk to correct address */
1049 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1050 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001051#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001052 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001053 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001054 {
1055 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1056 return -1;
1057 }
1058
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001059 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1060#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001061
1062 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001063 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001064 {
1065 write_device_info_flash(&device);
1066 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301067#if USE_PCOM_SECBOOT
1068 set_tamper_flag(device.is_tampered);
1069#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001070 }
1071 else
1072 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001073 offset = page_size;
1074
Amol Jadib6be5c12012-11-14 13:39:51 -08001075 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1076 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1077 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1078
1079 dprintf(INFO, "Loading boot image (%d): start\n",
1080 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001081 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001082
1083 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001084 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1085 return -1;
1086 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001087 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001088
Amol Jadib6be5c12012-11-14 13:39:51 -08001089 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001090 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1091 return -1;
1092 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001093 offset += ramdisk_actual;
1094
1095 dprintf(INFO, "Loading boot image (%d): done\n",
1096 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001097 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001098
1099 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001100 offset += second_actual;
1101 /* Second image loading not implemented. */
1102 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001103 }
1104
1105#if DEVICE_TREE
1106 if(hdr->dt_size != 0) {
1107
1108 /* Read the device tree table into buffer */
1109 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1110 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1111 return -1;
1112 }
1113
1114 table = (struct dt_table*) dt_buf;
1115
Deepa Dinamani19648b42013-09-05 17:05:55 -07001116 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001117 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1118 return -1;
1119 }
1120
Deepa Dinamani19648b42013-09-05 17:05:55 -07001121 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1122 if (!table)
1123 return -1;
1124
1125 /* Read the entire device tree table into buffer */
1126 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1127 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1128 return -1;
1129 }
1130
1131
Joel Kingaa335dc2013-06-03 16:11:08 -07001132 /* Find index of device tree within device tree table */
1133 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001134 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1135 return -1;
1136 }
1137
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001138 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001139 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001140 {
1141 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1142 return -1;
1143 }
1144
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001145 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001146 if(flash_read(ptn, offset + dt_entry.offset,
1147 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001148 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1149 return -1;
1150 }
1151 }
1152#endif
1153
Shashank Mittalcd98d472011-08-02 14:29:24 -07001154 }
David Ng183a7422009-12-07 14:55:21 -08001155continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001156
Dima Zavin214cc642009-01-26 11:16:21 -08001157 /* TODO: create/pass atags to kernel */
1158
Ajay Dudanie28a6072011-07-01 13:59:46 -07001159 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001160 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001161 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1162
1163 return 0;
1164}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001165
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001166BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001167void write_device_info_mmc(device_info *dev)
1168{
1169 struct device_info *info = (void*) info_buf;
1170 unsigned long long ptn = 0;
1171 unsigned long long size;
1172 int index = INVALID_PTN;
1173
1174 index = partition_get_index("aboot");
1175 ptn = partition_get_offset(index);
1176 if(ptn == 0)
1177 {
1178 return;
1179 }
1180
1181 size = partition_get_size(index);
1182
1183 memcpy(info, dev, sizeof(device_info));
1184
1185 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1186 {
1187 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1188 return;
1189 }
1190}
1191
1192void read_device_info_mmc(device_info *dev)
1193{
1194 struct device_info *info = (void*) info_buf;
1195 unsigned long long ptn = 0;
1196 unsigned long long size;
1197 int index = INVALID_PTN;
1198
1199 index = partition_get_index("aboot");
1200 ptn = partition_get_offset(index);
1201 if(ptn == 0)
1202 {
1203 return;
1204 }
1205
1206 size = partition_get_size(index);
1207
1208 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1209 {
1210 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1211 return;
1212 }
1213
1214 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1215 {
1216 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1217 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001218 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001219 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001220
1221 write_device_info_mmc(info);
1222 }
1223 memcpy(dev, info, sizeof(device_info));
1224}
1225
1226void write_device_info_flash(device_info *dev)
1227{
1228 struct device_info *info = (void *) info_buf;
1229 struct ptentry *ptn;
1230 struct ptable *ptable;
1231
1232 ptable = flash_get_ptable();
1233 if (ptable == NULL)
1234 {
1235 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1236 return;
1237 }
1238
1239 ptn = ptable_find(ptable, "devinfo");
1240 if (ptn == NULL)
1241 {
1242 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1243 return;
1244 }
1245
1246 memcpy(info, dev, sizeof(device_info));
1247
1248 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1249 {
1250 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1251 return;
1252 }
1253}
1254
1255void read_device_info_flash(device_info *dev)
1256{
1257 struct device_info *info = (void*) info_buf;
1258 struct ptentry *ptn;
1259 struct ptable *ptable;
1260
1261 ptable = flash_get_ptable();
1262 if (ptable == NULL)
1263 {
1264 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1265 return;
1266 }
1267
1268 ptn = ptable_find(ptable, "devinfo");
1269 if (ptn == NULL)
1270 {
1271 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1272 return;
1273 }
1274
1275 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1276 {
1277 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1278 return;
1279 }
1280
1281 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1282 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001283 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1284 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001285 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001286 write_device_info_flash(info);
1287 }
1288 memcpy(dev, info, sizeof(device_info));
1289}
1290
1291void write_device_info(device_info *dev)
1292{
1293 if(target_is_emmc_boot())
1294 {
1295 write_device_info_mmc(dev);
1296 }
1297 else
1298 {
1299 write_device_info_flash(dev);
1300 }
1301}
1302
1303void read_device_info(device_info *dev)
1304{
1305 if(target_is_emmc_boot())
1306 {
1307 read_device_info_mmc(dev);
1308 }
1309 else
1310 {
1311 read_device_info_flash(dev);
1312 }
1313}
1314
1315void reset_device_info()
1316{
1317 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001318 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001319 write_device_info(&device);
1320}
1321
1322void set_device_root()
1323{
1324 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001325 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001326 write_device_info(&device);
1327}
1328
Amol Jadicb524072012-08-09 16:40:18 -07001329#if DEVICE_TREE
1330int copy_dtb(uint8_t *boot_image_start)
1331{
1332 uint32 dt_image_offset = 0;
1333 uint32_t n;
1334 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001335 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001336 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001337
1338 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1339
Amol Jadicb524072012-08-09 16:40:18 -07001340 if(hdr->dt_size != 0) {
1341
1342 /* add kernel offset */
1343 dt_image_offset += page_size;
1344 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1345 dt_image_offset += n;
1346
1347 /* add ramdisk offset */
1348 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1349 dt_image_offset += n;
1350
1351 /* add second offset */
1352 if(hdr->second_size != 0) {
1353 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1354 dt_image_offset += n;
1355 }
1356
1357 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001358 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001359
Deepa Dinamani19648b42013-09-05 17:05:55 -07001360 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001361 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1362 return -1;
1363 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001364 /* Find index of device tree within device tree table */
1365 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001366 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1367 return -1;
1368 }
1369
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001370 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001371 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001372 {
1373 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1374 return -1;
1375 }
1376
Amol Jadicb524072012-08-09 16:40:18 -07001377 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001378 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001379 boot_image_start + dt_image_offset + dt_entry.offset,
1380 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001381 } else
1382 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001383
1384 /* Everything looks fine. Return success. */
1385 return 0;
1386}
1387#endif
1388
Brian Swetland9c4c0752009-01-25 16:23:50 -08001389void cmd_boot(const char *arg, void *data, unsigned sz)
1390{
1391 unsigned kernel_actual;
1392 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001393 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001394 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001395 int ret = 0;
1396 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001397
1398 if (sz < sizeof(hdr)) {
1399 fastboot_fail("invalid bootimage header");
1400 return;
1401 }
1402
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001403 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001404
1405 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001406 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001407
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001408 if(target_is_emmc_boot() && hdr->page_size) {
1409 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001410 page_mask = page_size - 1;
1411 }
1412
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001413 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1414 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1415
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001416 /*
1417 * Update the kernel/ramdisk/tags address if the boot image header
1418 * has default values, these default values come from mkbootimg when
1419 * the boot image is flashed using fastboot flash:raw
1420 */
1421 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001422
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001423 /* Get virtual addresses since the hdr saves physical addresses. */
1424 hdr->kernel_addr = VA(hdr->kernel_addr);
1425 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1426 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001427
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001428 /* Check if the addresses in the header are valid. */
1429 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1430 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1431 {
1432 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1433 return -1;
1434 }
1435
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001436 /* sz should have atleast raw boot image */
1437 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001438 fastboot_fail("incomplete bootimage");
1439 return;
1440 }
1441
Amol Jadicb524072012-08-09 16:40:18 -07001442#if DEVICE_TREE
1443 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001444 ret = copy_dtb(data);
1445
1446 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001447#else
1448 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1449 {
1450 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1451 return -1;
1452 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001453#endif
1454
1455 /* Load ramdisk & kernel */
1456 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1457 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1458
1459#if DEVICE_TREE
1460 /*
1461 * If dtb is not found look for appended DTB in the kernel.
1462 * If appended dev tree is found, update the atags with
1463 * memory address to the DTB appended location on RAM.
1464 * Else update with the atags address in the kernel header
1465 */
1466 if (!dtb_copied) {
1467 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001468 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1469 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001470 if (!dtb) {
1471 fastboot_fail("dtb not found");
1472 return;
1473 }
Amol Jadicb524072012-08-09 16:40:18 -07001474 }
1475#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001476
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001477#ifndef DEVICE_TREE
1478 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1479 {
1480 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1481 return -1;
1482 }
1483#endif
1484
Brian Swetland9c4c0752009-01-25 16:23:50 -08001485 fastboot_okay("");
1486 udc_stop();
1487
Dima Zavin77e41f32013-03-06 16:10:43 -08001488 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001489 (const char*) hdr->cmdline, board_machtype(),
1490 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001491}
1492
Dima Zavin214cc642009-01-26 11:16:21 -08001493void cmd_erase(const char *arg, void *data, unsigned sz)
1494{
1495 struct ptentry *ptn;
1496 struct ptable *ptable;
1497
1498 ptable = flash_get_ptable();
1499 if (ptable == NULL) {
1500 fastboot_fail("partition table doesn't exist");
1501 return;
1502 }
1503
1504 ptn = ptable_find(ptable, arg);
1505 if (ptn == NULL) {
1506 fastboot_fail("unknown partition name");
1507 return;
1508 }
1509
1510 if (flash_erase(ptn)) {
1511 fastboot_fail("failed to erase partition");
1512 return;
1513 }
1514 fastboot_okay("");
1515}
1516
Bikas Gurungd48bd242010-09-04 19:54:32 -07001517
1518void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1519{
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001520 BUF_DMA_ALIGN(out, 512);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001521 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001522 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001523
Kinson Chikf1a43512011-07-14 11:28:39 -07001524 index = partition_get_index(arg);
1525 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001526
Kinson Chikf1a43512011-07-14 11:28:39 -07001527 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001528 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001529 return;
1530 }
neetidb4b24d62012-01-20 12:13:09 -08001531 /* Simple inefficient version of erase. Just writing
1532 0 in first block */
1533 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1534 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001535 return;
1536 }
1537 fastboot_okay("");
1538}
1539
1540
Ajay Dudani5c761132011-04-07 20:19:04 -07001541void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001542{
1543 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001544 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001545 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001546
Greg Grisco6e754772011-06-23 12:19:39 -07001547 if (!strcmp(arg, "partition"))
1548 {
1549 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001550 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001551 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001552 return;
1553 }
1554 }
Greg Grisco6e754772011-06-23 12:19:39 -07001555 else
1556 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001557 index = partition_get_index(arg);
1558 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001559 if(ptn == 0) {
1560 fastboot_fail("partition table doesn't exist");
1561 return;
1562 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001563
Greg Grisco6e754772011-06-23 12:19:39 -07001564 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1565 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1566 fastboot_fail("image is not a boot image");
1567 return;
1568 }
1569 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001570
Kinson Chikf1a43512011-07-14 11:28:39 -07001571 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001572 if (ROUND_TO_PAGE(sz,511) > size) {
1573 fastboot_fail("size too large");
1574 return;
1575 }
1576 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1577 fastboot_fail("flash write failure");
1578 return;
1579 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001580 }
1581 fastboot_okay("");
1582 return;
1583}
1584
Ajay Dudani5c761132011-04-07 20:19:04 -07001585void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1586{
1587 unsigned int chunk;
1588 unsigned int chunk_data_sz;
1589 sparse_header_t *sparse_header;
1590 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001591 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001592 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301593 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001594 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001595
Kinson Chikf1a43512011-07-14 11:28:39 -07001596 index = partition_get_index(arg);
1597 ptn = partition_get_offset(index);
1598 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001599 fastboot_fail("partition table doesn't exist");
1600 return;
1601 }
1602
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301603 size = partition_get_size(index);
1604 if (ROUND_TO_PAGE(sz,511) > size) {
1605 fastboot_fail("size too large");
1606 return;
1607 }
1608
Ajay Dudani5c761132011-04-07 20:19:04 -07001609 /* Read and skip over sparse image header */
1610 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001611 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1612 fastboot_fail("size too large");
1613 return;
1614 }
1615
Ajay Dudani5c761132011-04-07 20:19:04 -07001616 data += sparse_header->file_hdr_sz;
1617 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1618 {
1619 /* Skip the remaining bytes in a header that is longer than
1620 * we expected.
1621 */
1622 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1623 }
1624
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001625 dprintf (SPEW, "=== Sparse Image Header ===\n");
1626 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1627 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1628 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1629 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1630 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1631 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1632 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1633 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001634
1635 /* Start processing chunks */
1636 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1637 {
1638 /* Read and skip over chunk header */
1639 chunk_header = (chunk_header_t *) data;
1640 data += sizeof(chunk_header_t);
1641
1642 dprintf (SPEW, "=== Chunk Header ===\n");
1643 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1644 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1645 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1646
1647 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1648 {
1649 /* Skip the remaining bytes in a header that is longer than
1650 * we expected.
1651 */
1652 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1653 }
1654
1655 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1656 switch (chunk_header->chunk_type)
1657 {
1658 case CHUNK_TYPE_RAW:
1659 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1660 chunk_data_sz))
1661 {
1662 fastboot_fail("Bogus chunk size for chunk type Raw");
1663 return;
1664 }
1665
Ajay Dudaniab18f022011-05-12 14:39:22 -07001666 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1667 chunk_data_sz,
1668 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001669 {
1670 fastboot_fail("flash write failure");
1671 return;
1672 }
1673 total_blocks += chunk_header->chunk_sz;
1674 data += chunk_data_sz;
1675 break;
1676
1677 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001678 total_blocks += chunk_header->chunk_sz;
1679 break;
1680
Ajay Dudani5c761132011-04-07 20:19:04 -07001681 case CHUNK_TYPE_CRC:
1682 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1683 {
1684 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1685 return;
1686 }
1687 total_blocks += chunk_header->chunk_sz;
1688 data += chunk_data_sz;
1689 break;
1690
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001691 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001692 fastboot_fail("Unknown chunk type");
1693 return;
1694 }
1695 }
1696
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001697 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1698 total_blocks, sparse_header->total_blks);
1699
1700 if(total_blocks != sparse_header->total_blks)
1701 {
1702 fastboot_fail("sparse image write failure");
1703 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001704
1705 fastboot_okay("");
1706 return;
1707}
1708
1709void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1710{
1711 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001712 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1713 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001714
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001715#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001716 int ret=0;
1717 uint32 major_version=0;
1718 uint32 minor_version=0;
1719
1720 ret = scm_svc_version(&major_version,&minor_version);
1721 if(!ret)
1722 {
1723 if(major_version >= 2)
1724 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001725 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001726 {
1727 ret = encrypt_scm((uint32 **) &data, &sz);
1728 if (ret != 0) {
1729 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1730 return;
1731 }
1732
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001733 /* Protect only for SSD */
1734 if (!strcmp(arg, "ssd")) {
1735 ret = scm_protect_keystore((uint32 *) data, sz);
1736 if (ret != 0) {
1737 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1738 return;
1739 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001740 }
1741 }
1742 else
1743 {
1744 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1745 if(ret != 0)
1746 {
1747 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1748 return;
1749 }
1750 }
1751 }
1752 else
1753 {
1754 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1755 magic_number[1] == DECRYPT_MAGIC_1)
1756 {
1757 ret = decrypt_scm((uint32 **) &data, &sz);
1758 if (ret != 0) {
1759 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1760 return;
1761 }
1762 }
1763 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1764 magic_number[1] == ENCRYPT_MAGIC_1)
1765 {
1766 ret = encrypt_scm((uint32 **) &data, &sz);
1767 if (ret != 0) {
1768 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1769 return;
1770 }
1771 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001772 }
1773 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001774 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001775 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001776 dprintf(CRITICAL,"INVALID SVC Version\n");
1777 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001778 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001779#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001780
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001781 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001782 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1783 cmd_flash_mmc_img(arg, data, sz);
1784 else
1785 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001786 return;
1787}
1788
Dima Zavin214cc642009-01-26 11:16:21 -08001789void cmd_flash(const char *arg, void *data, unsigned sz)
1790{
1791 struct ptentry *ptn;
1792 struct ptable *ptable;
1793 unsigned extra = 0;
1794
1795 ptable = flash_get_ptable();
1796 if (ptable == NULL) {
1797 fastboot_fail("partition table doesn't exist");
1798 return;
1799 }
1800
1801 ptn = ptable_find(ptable, arg);
1802 if (ptn == NULL) {
1803 fastboot_fail("unknown partition name");
1804 return;
1805 }
1806
1807 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1808 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1809 fastboot_fail("image is not a boot image");
1810 return;
1811 }
1812 }
1813
Amol Jadi5c61a952012-05-04 17:05:35 -07001814 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001815 || !strcmp(ptn->name, "userdata")
1816 || !strcmp(ptn->name, "persist")
1817 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301818 if (flash_ecc_bch_enabled())
1819 /* Spare data bytes for 8 bit ECC increased by 4 */
1820 extra = ((page_size >> 9) * 20);
1821 else
1822 extra = ((page_size >> 9) * 16);
1823 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001824 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001825
1826 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1827 if (flash_write(ptn, extra, data, sz)) {
1828 fastboot_fail("flash write failure");
1829 return;
1830 }
1831 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1832 fastboot_okay("");
1833}
1834
1835void cmd_continue(const char *arg, void *data, unsigned sz)
1836{
1837 fastboot_okay("");
1838 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001839 if (target_is_emmc_boot())
1840 {
1841 boot_linux_from_mmc();
1842 }
1843 else
1844 {
1845 boot_linux_from_flash();
1846 }
Dima Zavin214cc642009-01-26 11:16:21 -08001847}
1848
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001849void cmd_reboot(const char *arg, void *data, unsigned sz)
1850{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001851 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001852 fastboot_okay("");
1853 reboot_device(0);
1854}
1855
1856void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1857{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001858 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001859 fastboot_okay("");
1860 reboot_device(FASTBOOT_MODE);
1861}
1862
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001863void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
1864{
1865 dprintf(INFO, "Enabling charger screen check\n");
1866 device.charger_screen_enabled = 1;
1867 write_device_info(&device);
1868 fastboot_okay("");
1869}
1870
1871void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
1872{
1873 dprintf(INFO, "Disabling charger screen check\n");
1874 device.charger_screen_enabled = 0;
1875 write_device_info(&device);
1876 fastboot_okay("");
1877}
1878
Shashank Mittal162244e2011-08-08 19:01:25 -07001879void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1880{
1881 if(!device.is_unlocked)
1882 {
1883 device.is_unlocked = 1;
1884 write_device_info(&device);
1885 }
1886 fastboot_okay("");
1887}
1888
Shashank Mittala0032282011-08-26 14:50:11 -07001889void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1890{
1891 char response[64];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001892 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07001893 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001894 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1895 fastboot_info(response);
1896 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07001897 fastboot_info(response);
1898 fastboot_okay("");
1899}
1900
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001901void cmd_preflash(const char *arg, void *data, unsigned sz)
1902{
1903 fastboot_okay("");
1904}
1905
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001906void splash_screen ()
1907{
1908 struct ptentry *ptn;
1909 struct ptable *ptable;
1910 struct fbcon_config *fb_display = NULL;
1911
1912 if (!target_is_emmc_boot())
1913 {
1914 ptable = flash_get_ptable();
1915 if (ptable == NULL) {
1916 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001917 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001918 }
1919
1920 ptn = ptable_find(ptable, "splash");
1921 if (ptn == NULL) {
1922 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1923 } else {
1924 fb_display = fbcon_display();
1925 if (fb_display) {
1926 if (flash_read(ptn, 0, fb_display->base,
1927 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1928 fbcon_clear();
1929 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1930 }
1931 }
1932 }
1933 }
1934}
1935
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001936/* Get the size from partiton name */
1937static void get_partition_size(const char *arg, char *response)
1938{
1939 uint64_t ptn = 0;
1940 uint64_t size;
1941 int index = INVALID_PTN;
1942
1943 index = partition_get_index(arg);
1944
1945 if (index == INVALID_PTN)
1946 {
1947 dprintf(CRITICAL, "Invalid partition index\n");
1948 return;
1949 }
1950
1951 ptn = partition_get_offset(index);
1952
1953 if(!ptn)
1954 {
1955 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1956 return;
1957 }
1958
1959 size = partition_get_size(index);
1960
1961 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1962 return;
1963}
1964
1965/*
1966 * Publish the partition type & size info
1967 * fastboot getvar will publish the required information.
1968 * fastboot getvar partition_size:<partition_name>: partition size in hex
1969 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1970 */
1971static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1972{
1973 uint8_t i;
1974
1975 for (i = 0; i < num_parts; i++) {
1976 get_partition_size(info[i].part_name, info[i].size_response);
1977
1978 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1979 {
1980 dprintf(CRITICAL, "partition size name truncated\n");
1981 return;
1982 }
1983 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1984 {
1985 dprintf(CRITICAL, "partition type name truncated\n");
1986 return;
1987 }
1988
1989 /* publish partition size & type info */
1990 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1991 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1992 }
1993}
1994
Amol Jadi5edf3552013-07-23 14:15:34 -07001995/* register commands and variables for fastboot */
1996void aboot_fastboot_register_commands(void)
1997{
1998 if (target_is_emmc_boot())
1999 {
2000 fastboot_register("flash:", cmd_flash_mmc);
2001 fastboot_register("erase:", cmd_erase_mmc);
2002 }
2003 else
2004 {
2005 fastboot_register("flash:", cmd_flash);
2006 fastboot_register("erase:", cmd_erase);
2007 }
2008
2009 fastboot_register("boot", cmd_boot);
2010 fastboot_register("continue", cmd_continue);
2011 fastboot_register("reboot", cmd_reboot);
2012 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2013 fastboot_register("oem unlock", cmd_oem_unlock);
2014 fastboot_register("oem device-info", cmd_oem_devinfo);
2015 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002016 fastboot_register("oem enable-charger-screen",
2017 cmd_oem_enable_charger_screen);
2018 fastboot_register("oem disable-charger-screen",
2019 cmd_oem_disable_charger_screen);
Amol Jadi5edf3552013-07-23 14:15:34 -07002020 /* publish variables and their values */
2021 fastboot_publish("product", TARGET(BOARD));
2022 fastboot_publish("kernel", "lk");
2023 fastboot_publish("serialno", sn_buf);
2024
2025 /*
2026 * partition info is supported only for emmc partitions
2027 * Calling this for NAND prints some error messages which
2028 * is harmless but misleading. Avoid calling this for NAND
2029 * devices.
2030 */
2031 if (target_is_emmc_boot())
2032 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2033
2034 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002035 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2036 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002037 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002038 /* Is the charger screen check enabled */
2039 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2040 device.charger_screen_enabled);
2041 fastboot_publish("charger-screen-enabled",
2042 (const char *) charger_screen_enabled);
Amol Jadi5edf3552013-07-23 14:15:34 -07002043}
2044
Brian Swetland9c4c0752009-01-25 16:23:50 -08002045void aboot_init(const struct app_descriptor *app)
2046{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002047 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002048 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002049
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002050 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002051 if (target_is_emmc_boot())
2052 {
2053 page_size = 2048;
2054 page_mask = page_size - 1;
2055 }
2056 else
2057 {
2058 page_size = flash_page_size();
2059 page_mask = page_size - 1;
2060 }
2061
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002062 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2063
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002064 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002065
Greg Griscod6250552011-06-29 14:40:23 -07002066 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002067 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002068
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002069 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002070 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002071 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002072 dprintf(ALWAYS,"dload mode key sequence detected\n");
2073 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002074 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002075 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002076 }
2077 else
2078 {
2079 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002080 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002081 }
2082 boot_into_fastboot = true;
2083 }
2084 if (!boot_into_fastboot)
2085 {
2086 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2087 boot_into_recovery = 1;
2088 if (!boot_into_recovery &&
2089 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002090 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002091 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002092 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002093 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002094 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002095 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002096
Ajay Dudani77421292010-10-27 19:34:06 -07002097 reboot_mode = check_reboot_mode();
2098 if (reboot_mode == RECOVERY_MODE) {
2099 boot_into_recovery = 1;
2100 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002101 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002102 }
2103
Pavel Nedev5d91d412013-04-29 11:34:24 +03002104 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002105 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002106 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002107 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002108 if(emmc_recovery_init())
2109 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2110 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002111 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002112 if((device.is_unlocked) || (device.is_tampered))
2113 {
2114 #ifdef TZ_TAMPER_FUSE
2115 set_tamper_fuse_cmd();
2116 #endif
2117 #if USE_PCOM_SECBOOT
2118 set_tamper_flag(device.is_tampered);
2119 #endif
2120 }
Shashank Mittala0032282011-08-26 14:50:11 -07002121 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002122 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002123 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002124 else
2125 {
2126 recovery_init();
2127 #if USE_PCOM_SECBOOT
2128 if((device.is_unlocked) || (device.is_tampered))
2129 set_tamper_flag(device.is_tampered);
2130 #endif
2131 boot_linux_from_flash();
2132 }
2133 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2134 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002135 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002136
Amol Jadi5edf3552013-07-23 14:15:34 -07002137 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002138
Amol Jadi5edf3552013-07-23 14:15:34 -07002139 /* register aboot specific fastboot commands */
2140 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002141
Amol Jadi5edf3552013-07-23 14:15:34 -07002142 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002143 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002144
2145 /* initialize and start fastboot */
2146 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002147}
2148
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002149uint32_t get_page_size()
2150{
2151 return page_size;
2152}
2153
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002154/*
2155 * Calculated and save hash (SHA256) for non-signed boot image.
2156 *
2157 * Hash the same data that is checked on the signed boot image.
2158 * Kernel and Ramdisk are already read to memory buffers.
2159 * Need to read the entire device-tree from mmc
2160 * since non-signed image only read the DT tags of the relevant platform.
2161 *
2162 * @param kernel_addr - kernel bufer
2163 * @param kernel_actual - kernel size in bytes
2164 * @param ramdisk_addr - ramdisk buffer
2165 * @param ramdisk_actual - ramdisk size
2166 * @param ptn - partition
2167 * @param dt_offset - device tree offset on mmc partition
2168 * @param dt_size
2169 *
2170 * @return int - 0 on success, negative value on failure.
2171 */
2172int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2173 void *ramdisk_addr, unsigned ramdisk_actual,
2174 unsigned long long ptn,
2175 unsigned dt_offset, unsigned dt_size)
2176{
2177 SHA256_CTX sha256_ctx;
2178 char digest[32]={0};
2179 char *buf = (char *)target_get_scratch_address();
2180 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2181 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2182
2183 SHA256_Init(&sha256_ctx);
2184
2185 /* Read Boot Header */
2186 if (mmc_read(ptn, buf, page_size))
2187 {
2188 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2189 return -1;
2190 }
2191 /* Read entire Device Tree */
2192 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2193 {
2194 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2195 return -1;
2196 }
2197 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2198 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2199 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2200 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2201
2202 SHA256_Final(digest, &sha256_ctx);
2203
2204 save_kernel_hash_cmd(digest);
2205 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2206
2207 return 0;
2208}
2209
Brian Swetland9c4c0752009-01-25 16:23:50 -08002210APP_START(aboot)
2211 .init = aboot_init,
2212APP_END