blob: 5f5b54e682e0bbd083c44383b49f9956ae63ce29 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Kuogee Hsiehfb4b5c32013-12-20 08:34:10 -08005 * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080038#include <kernel/thread.h>
39#include <arch/ops.h>
40
Dima Zavin214cc642009-01-26 11:16:21 -080041#include <dev/flash.h>
42#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080043#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080044#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080045#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070046#include <target.h>
47#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070048#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070049#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070050#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070051#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070052#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030053#include <sha.h>
Dima Zavin214cc642009-01-26 11:16:21 -080054
Neeti Desai17379b82012-06-04 18:42:53 -070055#if DEVICE_TREE
56#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070057#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070058#endif
59
Shashank Mittalcd98d472011-08-02 14:29:24 -070060#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080061#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080062#include "bootimg.h"
63#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070064#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070065#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070066#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070067#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080068
Shashank Mittal162244e2011-08-08 19:01:25 -070069#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070070
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070071extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070072extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070073extern void target_uninit(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070074
75void write_device_info_mmc(device_info *dev);
76void write_device_info_flash(device_info *dev);
77
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070078#define EXPAND(NAME) #NAME
79#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070080
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080081#ifdef MEMBASE
82#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
83#else
David Ng183a7422009-12-07 14:55:21 -080084#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080085#endif
86
Deepa Dinamani0e163a42013-05-24 17:08:15 -070087#ifndef MEMSIZE
88#define MEMSIZE 1024*1024
89#endif
90
91#define MAX_TAGS_SIZE 1024
92
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080093#define RECOVERY_MODE 0x77665502
94#define FASTBOOT_MODE 0x77665500
95
Kun Liang2f1601a2013-08-12 16:29:54 +080096/* make 4096 as default size to ensure EFS,EXT4's erasing */
97#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsiehfb4b5c32013-12-20 08:34:10 -080098#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +080099
David Ng183a7422009-12-07 14:55:21 -0800100static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800101static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300102static const char *androidboot_mode = " androidboot.mode=";
Dhaval Patel223ec952013-07-18 14:49:44 -0700103static const char *display_cmdline = " mdss_mdp.panel=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800104static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800105static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700106static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300107static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800108
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800109static const char *baseband_apq = " androidboot.baseband=apq";
110static const char *baseband_msm = " androidboot.baseband=msm";
111static const char *baseband_csfb = " androidboot.baseband=csfb";
112static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700113static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700114static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800115static const char *baseband_dsda = " androidboot.baseband=dsda";
116static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800117static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800118
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700119static unsigned page_size = 0;
120static unsigned page_mask = 0;
121static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
122static bool boot_into_ffbm;
123
Shashank Mittalcd98d472011-08-02 14:29:24 -0700124/* Assuming unauthorized kernel image by default */
125static int auth_kernel_img = 0;
126
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700127static device_info device = {DEVICE_MAGIC, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700128
Dima Zavin42168f22009-01-30 11:52:22 -0800129struct atag_ptbl_entry
130{
131 char name[16];
132 unsigned offset;
133 unsigned size;
134 unsigned flags;
135};
136
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700137/*
138 * Partition info, required to be published
139 * for fastboot
140 */
141struct getvar_partition_info {
142 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
143 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
144 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
145 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
146 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
147};
148
149/*
150 * Right now, we are publishing the info for only
151 * three partitions
152 */
153struct getvar_partition_info part_info[] =
154{
155 { "system" , "partition-size:", "partition-type:", "", "ext4" },
156 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
157 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
158};
159
160char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700161char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800162char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700163char display_panel_buf[MAX_PANEL_BUF_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700164
Greg Griscod2471ef2011-07-14 13:00:42 -0700165extern int emmc_recovery_init(void);
166
Kinson Chik0b1c8162011-08-31 16:31:57 -0700167#if NO_KEYPAD_DRIVER
168extern int fastboot_trigger(void);
169#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700170
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700171static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
172{
173 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700174#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
175 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
176 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
177 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700178#endif
179}
180
Dima Zavin42168f22009-01-30 11:52:22 -0800181static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
182{
183 struct atag_ptbl_entry atag_ptn;
184
185 memcpy(atag_ptn.name, ptn->name, 16);
186 atag_ptn.name[15] = '\0';
187 atag_ptn.offset = ptn->start;
188 atag_ptn.size = ptn->length;
189 atag_ptn.flags = ptn->flags;
190 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
191 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
192}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800193
Neeti Desaie245d492012-06-01 12:52:13 -0700194unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800195{
David Ng183a7422009-12-07 14:55:21 -0800196 int cmdline_len = 0;
197 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800198 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700199 int pause_at_bootup = 0;
Pavel Nedev5614d222013-06-17 18:01:02 +0300200 bool gpt_exists = partition_gpt_exists();
Dima Zavin42168f22009-01-30 11:52:22 -0800201
Brian Swetland9c4c0752009-01-25 16:23:50 -0800202 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800203 cmdline_len = strlen(cmdline);
204 have_cmdline = 1;
205 }
206 if (target_is_emmc_boot()) {
207 cmdline_len += strlen(emmc_cmdline);
208 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800209
210 cmdline_len += strlen(usb_sn_cmdline);
211 cmdline_len += strlen(sn_buf);
212
Pavel Nedev5614d222013-06-17 18:01:02 +0300213 if (boot_into_recovery && gpt_exists)
214 cmdline_len += strlen(secondary_gpt_enable);
215
Pavel Nedev328ac822013-04-05 15:25:11 +0300216 if (boot_into_ffbm) {
217 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700218 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800219 /* reduce kernel console messages to speed-up boot */
220 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700221 } else if (device.charger_screen_enabled &&
222 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700223 pause_at_bootup = 1;
224 cmdline_len += strlen(battchg_pause);
225 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800226
Shashank Mittalcd98d472011-08-02 14:29:24 -0700227 if(target_use_signed_kernel() && auth_kernel_img) {
228 cmdline_len += strlen(auth_kernel);
229 }
230
Ajay Dudanid04110c2011-01-17 23:55:07 -0800231 /* Determine correct androidboot.baseband to use */
232 switch(target_baseband())
233 {
234 case BASEBAND_APQ:
235 cmdline_len += strlen(baseband_apq);
236 break;
237
238 case BASEBAND_MSM:
239 cmdline_len += strlen(baseband_msm);
240 break;
241
242 case BASEBAND_CSFB:
243 cmdline_len += strlen(baseband_csfb);
244 break;
245
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800246 case BASEBAND_SVLTE2A:
247 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800248 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700249
250 case BASEBAND_MDM:
251 cmdline_len += strlen(baseband_mdm);
252 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700253
254 case BASEBAND_SGLTE:
255 cmdline_len += strlen(baseband_sglte);
256 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530257
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800258 case BASEBAND_SGLTE2:
259 cmdline_len += strlen(baseband_sglte2);
260 break;
261
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530262 case BASEBAND_DSDA:
263 cmdline_len += strlen(baseband_dsda);
264 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800265
266 case BASEBAND_DSDA2:
267 cmdline_len += strlen(baseband_dsda2);
268 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800269 }
270
Dhaval Patel223ec952013-07-18 14:49:44 -0700271 if (target_display_panel_node(display_panel_buf, MAX_PANEL_BUF_SIZE) &&
272 strlen(display_panel_buf))
273 {
274 cmdline_len += strlen(display_cmdline);
275 cmdline_len += strlen(display_panel_buf);
276 }
277
David Ng183a7422009-12-07 14:55:21 -0800278 if (cmdline_len > 0) {
279 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700280 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
281 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700282
Amol Jadi168b7712012-03-06 16:15:00 -0800283 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700284 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800285 if (have_cmdline) {
286 src = cmdline;
287 while ((*dst++ = *src++));
288 }
289 if (target_is_emmc_boot()) {
290 src = emmc_cmdline;
291 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700292 have_cmdline = 1;
293 while ((*dst++ = *src++));
294 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800295
296 src = usb_sn_cmdline;
297 if (have_cmdline) --dst;
298 have_cmdline = 1;
299 while ((*dst++ = *src++));
300 src = sn_buf;
301 if (have_cmdline) --dst;
302 have_cmdline = 1;
303 while ((*dst++ = *src++));
304
Pavel Nedev5614d222013-06-17 18:01:02 +0300305 if (boot_into_recovery && gpt_exists) {
306 src = secondary_gpt_enable;
307 if (have_cmdline) --dst;
308 while ((*dst++ = *src++));
309 }
310
Pavel Nedev328ac822013-04-05 15:25:11 +0300311 if (boot_into_ffbm) {
312 src = androidboot_mode;
313 if (have_cmdline) --dst;
314 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700315 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300316 if (have_cmdline) --dst;
317 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800318 src = loglevel;
319 if (have_cmdline) --dst;
320 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300321 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700322 src = battchg_pause;
323 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800324 while ((*dst++ = *src++));
325 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800326
Shashank Mittalcd98d472011-08-02 14:29:24 -0700327 if(target_use_signed_kernel() && auth_kernel_img) {
328 src = auth_kernel;
329 if (have_cmdline) --dst;
330 while ((*dst++ = *src++));
331 }
332
Ajay Dudanid04110c2011-01-17 23:55:07 -0800333 switch(target_baseband())
334 {
335 case BASEBAND_APQ:
336 src = baseband_apq;
337 if (have_cmdline) --dst;
338 while ((*dst++ = *src++));
339 break;
340
341 case BASEBAND_MSM:
342 src = baseband_msm;
343 if (have_cmdline) --dst;
344 while ((*dst++ = *src++));
345 break;
346
347 case BASEBAND_CSFB:
348 src = baseband_csfb;
349 if (have_cmdline) --dst;
350 while ((*dst++ = *src++));
351 break;
352
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800353 case BASEBAND_SVLTE2A:
354 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800355 if (have_cmdline) --dst;
356 while ((*dst++ = *src++));
357 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700358
359 case BASEBAND_MDM:
360 src = baseband_mdm;
361 if (have_cmdline) --dst;
362 while ((*dst++ = *src++));
363 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700364
365 case BASEBAND_SGLTE:
366 src = baseband_sglte;
367 if (have_cmdline) --dst;
368 while ((*dst++ = *src++));
369 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530370
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800371 case BASEBAND_SGLTE2:
372 src = baseband_sglte2;
373 if (have_cmdline) --dst;
374 while ((*dst++ = *src++));
375 break;
376
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530377 case BASEBAND_DSDA:
378 src = baseband_dsda;
379 if (have_cmdline) --dst;
380 while ((*dst++ = *src++));
381 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800382
383 case BASEBAND_DSDA2:
384 src = baseband_dsda2;
385 if (have_cmdline) --dst;
386 while ((*dst++ = *src++));
387 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800388 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700389
390 if (strlen(display_panel_buf)) {
391 src = display_cmdline;
392 if (have_cmdline) --dst;
393 while ((*dst++ = *src++));
394 src = display_panel_buf;
395 if (have_cmdline) --dst;
396 while ((*dst++ = *src++));
397 }
Neeti Desaie245d492012-06-01 12:52:13 -0700398 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700399
400
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800401 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700402 return cmdline_final;
403}
404
405unsigned *atag_core(unsigned *ptr)
406{
407 /* CORE */
408 *ptr++ = 2;
409 *ptr++ = 0x54410001;
410
411 return ptr;
412
413}
414
415unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
416 unsigned ramdisk_size)
417{
418 if (ramdisk_size) {
419 *ptr++ = 4;
420 *ptr++ = 0x54420005;
421 *ptr++ = (unsigned)ramdisk;
422 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800423 }
424
Neeti Desaie245d492012-06-01 12:52:13 -0700425 return ptr;
426}
427
428unsigned *atag_ptable(unsigned **ptr_addr)
429{
430 int i;
431 struct ptable *ptable;
432
433 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700434 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
435 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700436 *(*ptr_addr)++ = 0x4d534d70;
437 for (i = 0; i < ptable->count; ++i)
438 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
439 }
440
441 return (*ptr_addr);
442}
443
444unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
445{
446 int cmdline_length = 0;
447 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700448 char *dest;
449
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800450 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700451 n = (cmdline_length + 4) & (~3);
452
453 *ptr++ = (n / 4) + 2;
454 *ptr++ = 0x54410009;
455 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800456 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700457 ptr += (n / 4);
458
459 return ptr;
460}
461
462unsigned *atag_end(unsigned *ptr)
463{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800464 /* END */
465 *ptr++ = 0;
466 *ptr++ = 0;
467
Neeti Desaie245d492012-06-01 12:52:13 -0700468 return ptr;
469}
470
471void generate_atags(unsigned *ptr, const char *cmdline,
472 void *ramdisk, unsigned ramdisk_size)
473{
474
475 ptr = atag_core(ptr);
476 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
477 ptr = target_atag_mem(ptr);
478
479 /* Skip NAND partition ATAGS for eMMC boot */
480 if (!target_is_emmc_boot()){
481 ptr = atag_ptable(&ptr);
482 }
483
484 ptr = atag_cmdline(ptr, cmdline);
485 ptr = atag_end(ptr);
486}
487
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700488typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700489void boot_linux(void *kernel, unsigned *tags,
490 const char *cmdline, unsigned machtype,
491 void *ramdisk, unsigned ramdisk_size)
492{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800493 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800494#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700495 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800496#endif
497
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700498 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800499 uint32_t tags_phys = PA((addr_t)tags);
500
501 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700502
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800503 final_cmdline = update_cmdline((const char*)cmdline);
504
Neeti Desai17379b82012-06-04 18:42:53 -0700505#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800506 dprintf(INFO, "Updating device tree: start\n");
507
Neeti Desai17379b82012-06-04 18:42:53 -0700508 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800509 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700510 if(ret)
511 {
512 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
513 ASSERT(0);
514 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800515 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700516#else
Neeti Desaie245d492012-06-01 12:52:13 -0700517 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800518 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700519#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700520
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700521 /* Perform target specific cleanup */
522 target_uninit();
523
Aravind Venkateswarandb0d5262014-02-25 16:25:30 -0800524 /* Turn off splash screen if enabled */
525#if DISPLAY_SPLASH_SCREEN
526 target_display_shutdown();
527#endif
528
529
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800530 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
531 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800532
533 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700534
Amol Jadi4421e652011-06-16 15:00:48 -0700535 /* do any platform specific cleanup before kernel entry */
536 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700537
Brian Swetland9c4c0752009-01-25 16:23:50 -0800538 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700539
Amol Jadi504f9fe2012-08-16 13:56:48 -0700540#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800541 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700542#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700543 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700544 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800545}
546
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700547/* Function to check if the memory address range falls within the aboot
548 * boundaries.
549 * start: Start of the memory region
550 * size: Size of the memory region
551 */
552int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
553{
554 /* Check for boundary conditions. */
555 if ((start + size) < start)
556 return -1;
557
558 /* Check for memory overlap. */
559 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
560 return 0;
Channagoud Kadabib4413fd2013-10-15 12:53:52 -0700561 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700562 return 0;
563 else
564 return -1;
565}
566
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800567#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800568
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700569BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800570#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700571BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800572#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800573
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700574static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
575{
576 int ret;
577
578 /* Assume device is rooted at this time. */
579 device.is_tampered = 1;
580
581 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
582
583 ret = image_verify((unsigned char *)bootimg_addr,
584 (unsigned char *)(bootimg_addr + bootimg_size),
585 bootimg_size,
586 CRYPTO_AUTH_ALG_SHA256);
587
588 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
589
590 if (ret)
591 {
592 /* Authorized kernel */
593 device.is_tampered = 0;
594 }
595
596#if USE_PCOM_SECBOOT
597 set_tamper_flag(device.is_tampered);
598#endif
599
600 if(device.is_tampered)
601 {
602 write_device_info_mmc(&device);
603 #ifdef TZ_TAMPER_FUSE
604 set_tamper_fuse_cmd();
605 #endif
606 #ifdef ASSERT_ON_TAMPER
607 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
608 ASSERT(0);
609 #endif
610 }
611}
612
Shashank Mittal23b8f422010-04-16 19:27:21 -0700613int boot_linux_from_mmc(void)
614{
615 struct boot_img_hdr *hdr = (void*) buf;
616 struct boot_img_hdr *uhdr;
617 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700618 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700619 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700620 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700621
Shashank Mittalcd98d472011-08-02 14:29:24 -0700622 unsigned char *image_addr = 0;
623 unsigned kernel_actual;
624 unsigned ramdisk_actual;
625 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700626 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700627
628#if DEVICE_TREE
629 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700630 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700631 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800632 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700633 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700634#endif
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700635 if (!boot_into_recovery) {
636 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
637 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
638 if (rcode <= 0) {
639 boot_into_ffbm = false;
640 if (rcode < 0)
641 dprintf(CRITICAL,"failed to get ffbm cookie");
642 } else
643 boot_into_ffbm = true;
644 } else
645 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700646 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
647 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
648 dprintf(INFO, "Unified boot method!\n");
649 hdr = uhdr;
650 goto unified_boot;
651 }
Greg Griscod6250552011-06-29 14:40:23 -0700652 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700653 index = partition_get_index("boot");
654 ptn = partition_get_offset(index);
655 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700656 dprintf(CRITICAL, "ERROR: No boot partition found\n");
657 return -1;
658 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700659 }
660 else {
661 index = partition_get_index("recovery");
662 ptn = partition_get_offset(index);
663 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700664 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
665 return -1;
666 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700667 }
668
Greg Griscod6250552011-06-29 14:40:23 -0700669 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700670 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
671 return -1;
672 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700673
674 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700675 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700676 return -1;
677 }
678
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700679 if (hdr->page_size && (hdr->page_size != page_size)) {
680 page_size = hdr->page_size;
681 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700682 }
683
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700684 /*
685 * Update the kernel/ramdisk/tags address if the boot image header
686 * has default values, these default values come from mkbootimg when
687 * the boot image is flashed using fastboot flash:raw
688 */
689 update_ker_tags_rdisk_addr(hdr);
690
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700691 /* Get virtual addresses since the hdr saves physical addresses. */
692 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
693 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
694 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700695
696 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
697 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
698
699 /* Check if the addresses in the header are valid. */
700 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
701 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
702 {
703 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
704 return -1;
705 }
706
707#ifndef DEVICE_TREE
708 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
709 {
710 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
711 return -1;
712 }
713#endif
714
Shashank Mittalcd98d472011-08-02 14:29:24 -0700715 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300716 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
717 (int) target_use_signed_kernel(),
718 device.is_unlocked,
719 device.is_tampered);
720
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700721 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800722 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700723 offset = 0;
724
Shashank Mittalcd98d472011-08-02 14:29:24 -0700725 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700726
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800727#if DEVICE_TREE
728 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
729 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700730
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700731 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700732 {
733 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
734 return -1;
735 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800736#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530737 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700738
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700739#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700740
Amol Jadib6be5c12012-11-14 13:39:51 -0800741 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700742 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800743
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700744 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
745 {
746 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
747 return -1;
748 }
749
Shashank Mittalcd98d472011-08-02 14:29:24 -0700750 /* Read image without signature */
751 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
752 {
753 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
754 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800755 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700756
Amol Jadib6be5c12012-11-14 13:39:51 -0800757 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700758 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800759
Shashank Mittalcd98d472011-08-02 14:29:24 -0700760 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700761
762 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
763 {
764 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
765 return -1;
766 }
767
Shashank Mittalcd98d472011-08-02 14:29:24 -0700768 /* Read signature */
769 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
770 {
771 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700772 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700773 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800774
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700775 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700776
Neeti Desai465491e2012-07-31 12:53:35 -0700777 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700778 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
779 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700780
Neeti Desai465491e2012-07-31 12:53:35 -0700781 #if DEVICE_TREE
782 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700783 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700784 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700785
Deepa Dinamani19648b42013-09-05 17:05:55 -0700786 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700787 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
788 return -1;
789 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700790
Neeti Desai465491e2012-07-31 12:53:35 -0700791 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700792 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700793 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
794 return -1;
795 }
796
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700797 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700798 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700799 {
800 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
801 return -1;
802 }
803
Joel Kingaa335dc2013-06-03 16:11:08 -0700804 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800805 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800806 /*
807 * If appended dev tree is found, update the atags with
808 * memory address to the DTB appended location on RAM.
809 * Else update with the atags address in the kernel header
810 */
811 void *dtb;
812 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700813 hdr->kernel_size,
814 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800815 if (!dtb) {
816 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
817 return -1;
818 }
Neeti Desai465491e2012-07-31 12:53:35 -0700819 }
820 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700821 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700822 else
823 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800824 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700825
Amol Jadib6be5c12012-11-14 13:39:51 -0800826 dprintf(INFO, "Loading boot image (%d): start\n",
827 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700828 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800829
830 offset = page_size;
831
832 /* Load kernel */
833 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700834 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
835 return -1;
836 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800837 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700838
Amol Jadib6be5c12012-11-14 13:39:51 -0800839 /* Load ramdisk */
840 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700841 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800842 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700843 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
844 return -1;
845 }
846 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800847 offset += ramdisk_actual;
848
849 dprintf(INFO, "Loading boot image (%d): done\n",
850 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700851 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700852
853 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800854 offset += second_actual;
855 /* Second image loading not implemented. */
856 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700857 }
858
859 #if DEVICE_TREE
860 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -0700861 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -0700862 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
863 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
864 return -1;
865 }
866 table = (struct dt_table*) dt_buf;
867
Deepa Dinamani19648b42013-09-05 17:05:55 -0700868 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700869 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
870 return -1;
871 }
872
Deepa Dinamani19648b42013-09-05 17:05:55 -0700873 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
874 if (!table)
875 return -1;
876
877 /* Read the entire device tree table into buffer */
878 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
879 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
880 return -1;
881 }
882
Joel Kingaa335dc2013-06-03 16:11:08 -0700883 /* Find index of device tree within device tree table */
884 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700885 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
886 return -1;
887 }
888
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700889 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700890 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700891 {
892 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
893 return -1;
894 }
895
Joel Kingaa335dc2013-06-03 16:11:08 -0700896 if(mmc_read(ptn + offset + dt_entry.offset,
897 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700898 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
899 return -1;
900 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300901 #ifdef TZ_SAVE_KERNEL_HASH
902 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
903 hdr->ramdisk_addr, ramdisk_actual,
904 ptn, offset, hdr->dt_size);
905 #endif /* TZ_SAVE_KERNEL_HASH */
906
Channagoud Kadabi35095622013-03-01 13:53:05 -0800907 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800908 /*
909 * If appended dev tree is found, update the atags with
910 * memory address to the DTB appended location on RAM.
911 * Else update with the atags address in the kernel header
912 */
913 void *dtb;
914 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700915 kernel_actual,
916 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800917 if (!dtb) {
918 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
919 return -1;
920 }
Neeti Desai465491e2012-07-31 12:53:35 -0700921 }
922 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700923 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700924
Stanimir Varbanov69ec5462013-07-18 18:17:42 +0300925 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
926 target_load_ssd_keystore();
927
Shashank Mittal23b8f422010-04-16 19:27:21 -0700928unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700929
Dima Zavin77e41f32013-03-06 16:10:43 -0800930 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700931 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700932 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
933
934 return 0;
935}
936
Dima Zavin214cc642009-01-26 11:16:21 -0800937int boot_linux_from_flash(void)
938{
939 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800940 struct ptentry *ptn;
941 struct ptable *ptable;
942 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800943
Shashank Mittalcd98d472011-08-02 14:29:24 -0700944 unsigned char *image_addr = 0;
945 unsigned kernel_actual;
946 unsigned ramdisk_actual;
947 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800948 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700949
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700950#if DEVICE_TREE
951 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700952 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800953 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700954 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700955#endif
956
David Ng183a7422009-12-07 14:55:21 -0800957 if (target_is_emmc_boot()) {
958 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
959 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
960 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
961 return -1;
962 }
963 goto continue_boot;
964 }
965
Dima Zavin214cc642009-01-26 11:16:21 -0800966 ptable = flash_get_ptable();
967 if (ptable == NULL) {
968 dprintf(CRITICAL, "ERROR: Partition table not found\n");
969 return -1;
970 }
971
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800972 if(!boot_into_recovery)
973 {
974 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700975
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800976 if (ptn == NULL) {
977 dprintf(CRITICAL, "ERROR: No boot partition found\n");
978 return -1;
979 }
980 }
981 else
982 {
983 ptn = ptable_find(ptable, "recovery");
984 if (ptn == NULL) {
985 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
986 return -1;
987 }
Dima Zavin214cc642009-01-26 11:16:21 -0800988 }
989
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800990 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800991 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
992 return -1;
993 }
Dima Zavin214cc642009-01-26 11:16:21 -0800994
995 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700996 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800997 return -1;
998 }
999
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001000 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001001 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 -08001002 return -1;
1003 }
1004
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001005 /*
1006 * Update the kernel/ramdisk/tags address if the boot image header
1007 * has default values, these default values come from mkbootimg when
1008 * the boot image is flashed using fastboot flash:raw
1009 */
1010 update_ker_tags_rdisk_addr(hdr);
1011
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001012 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001013 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1014 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1015 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1016
1017 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1018 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1019
1020 /* Check if the addresses in the header are valid. */
1021 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1022 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1023 {
1024 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1025 return -1;
1026 }
1027
1028#ifndef DEVICE_TREE
1029 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1030 {
1031 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1032 return -1;
1033 }
1034#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001035
Shashank Mittalcd98d472011-08-02 14:29:24 -07001036 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001037 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001038 {
1039 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001040 offset = 0;
1041
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001042#if DEVICE_TREE
1043 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1044 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001045
1046 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1047 {
1048 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1049 return -1;
1050 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001051#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001052 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001053#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001054
Amol Jadib6be5c12012-11-14 13:39:51 -08001055 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001056 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001057
Shashank Mittalcd98d472011-08-02 14:29:24 -07001058 /* Read image without signature */
1059 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1060 {
1061 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1062 return -1;
1063 }
Dima Zavin214cc642009-01-26 11:16:21 -08001064
Amol Jadib6be5c12012-11-14 13:39:51 -08001065 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001066 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001067
Shashank Mittalcd98d472011-08-02 14:29:24 -07001068 offset = imagesize_actual;
1069 /* Read signature */
1070 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1071 {
1072 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001073 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001074 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001075
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001076 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001077
1078 /* Move kernel and ramdisk to correct address */
1079 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1080 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001081#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001082 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001083 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001084 {
1085 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1086 return -1;
1087 }
1088
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001089 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1090#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001091
1092 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001093 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001094 {
1095 write_device_info_flash(&device);
1096 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301097#if USE_PCOM_SECBOOT
1098 set_tamper_flag(device.is_tampered);
1099#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001100 }
1101 else
1102 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001103 offset = page_size;
1104
Amol Jadib6be5c12012-11-14 13:39:51 -08001105 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1106 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1107 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1108
1109 dprintf(INFO, "Loading boot image (%d): start\n",
1110 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001111 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001112
1113 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001114 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1115 return -1;
1116 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001117 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001118
Amol Jadib6be5c12012-11-14 13:39:51 -08001119 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001120 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1121 return -1;
1122 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001123 offset += ramdisk_actual;
1124
1125 dprintf(INFO, "Loading boot image (%d): done\n",
1126 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001127 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001128
1129 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001130 offset += second_actual;
1131 /* Second image loading not implemented. */
1132 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001133 }
1134
1135#if DEVICE_TREE
1136 if(hdr->dt_size != 0) {
1137
1138 /* Read the device tree table into buffer */
1139 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1140 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1141 return -1;
1142 }
1143
1144 table = (struct dt_table*) dt_buf;
1145
Deepa Dinamani19648b42013-09-05 17:05:55 -07001146 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001147 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1148 return -1;
1149 }
1150
Deepa Dinamani19648b42013-09-05 17:05:55 -07001151 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1152 if (!table)
1153 return -1;
1154
1155 /* Read the entire device tree table into buffer */
1156 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1157 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1158 return -1;
1159 }
1160
1161
Joel Kingaa335dc2013-06-03 16:11:08 -07001162 /* Find index of device tree within device tree table */
1163 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001164 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1165 return -1;
1166 }
1167
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001168 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001169 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001170 {
1171 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1172 return -1;
1173 }
1174
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001175 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001176 if(flash_read(ptn, offset + dt_entry.offset,
1177 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001178 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1179 return -1;
1180 }
1181 }
1182#endif
1183
Shashank Mittalcd98d472011-08-02 14:29:24 -07001184 }
David Ng183a7422009-12-07 14:55:21 -08001185continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001186
Dima Zavin214cc642009-01-26 11:16:21 -08001187 /* TODO: create/pass atags to kernel */
1188
Ajay Dudanie28a6072011-07-01 13:59:46 -07001189 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001190 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001191 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1192
1193 return 0;
1194}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001195
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001196BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001197void write_device_info_mmc(device_info *dev)
1198{
1199 struct device_info *info = (void*) info_buf;
1200 unsigned long long ptn = 0;
1201 unsigned long long size;
1202 int index = INVALID_PTN;
1203
1204 index = partition_get_index("aboot");
1205 ptn = partition_get_offset(index);
1206 if(ptn == 0)
1207 {
1208 return;
1209 }
1210
1211 size = partition_get_size(index);
1212
1213 memcpy(info, dev, sizeof(device_info));
1214
1215 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1216 {
1217 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1218 return;
1219 }
1220}
1221
1222void read_device_info_mmc(device_info *dev)
1223{
1224 struct device_info *info = (void*) info_buf;
1225 unsigned long long ptn = 0;
1226 unsigned long long size;
1227 int index = INVALID_PTN;
1228
1229 index = partition_get_index("aboot");
1230 ptn = partition_get_offset(index);
1231 if(ptn == 0)
1232 {
1233 return;
1234 }
1235
1236 size = partition_get_size(index);
1237
1238 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1239 {
1240 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1241 return;
1242 }
1243
1244 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1245 {
1246 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1247 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001248 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001249 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001250
1251 write_device_info_mmc(info);
1252 }
1253 memcpy(dev, info, sizeof(device_info));
1254}
1255
1256void write_device_info_flash(device_info *dev)
1257{
1258 struct device_info *info = (void *) info_buf;
1259 struct ptentry *ptn;
1260 struct ptable *ptable;
1261
1262 ptable = flash_get_ptable();
1263 if (ptable == NULL)
1264 {
1265 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1266 return;
1267 }
1268
1269 ptn = ptable_find(ptable, "devinfo");
1270 if (ptn == NULL)
1271 {
1272 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1273 return;
1274 }
1275
1276 memcpy(info, dev, sizeof(device_info));
1277
1278 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1279 {
1280 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1281 return;
1282 }
1283}
1284
1285void read_device_info_flash(device_info *dev)
1286{
1287 struct device_info *info = (void*) info_buf;
1288 struct ptentry *ptn;
1289 struct ptable *ptable;
1290
1291 ptable = flash_get_ptable();
1292 if (ptable == NULL)
1293 {
1294 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1295 return;
1296 }
1297
1298 ptn = ptable_find(ptable, "devinfo");
1299 if (ptn == NULL)
1300 {
1301 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1302 return;
1303 }
1304
1305 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1306 {
1307 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1308 return;
1309 }
1310
1311 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1312 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001313 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1314 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001315 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001316 write_device_info_flash(info);
1317 }
1318 memcpy(dev, info, sizeof(device_info));
1319}
1320
1321void write_device_info(device_info *dev)
1322{
1323 if(target_is_emmc_boot())
1324 {
1325 write_device_info_mmc(dev);
1326 }
1327 else
1328 {
1329 write_device_info_flash(dev);
1330 }
1331}
1332
1333void read_device_info(device_info *dev)
1334{
1335 if(target_is_emmc_boot())
1336 {
1337 read_device_info_mmc(dev);
1338 }
1339 else
1340 {
1341 read_device_info_flash(dev);
1342 }
1343}
1344
1345void reset_device_info()
1346{
1347 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001348 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001349 write_device_info(&device);
1350}
1351
1352void set_device_root()
1353{
1354 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001355 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001356 write_device_info(&device);
1357}
1358
Amol Jadicb524072012-08-09 16:40:18 -07001359#if DEVICE_TREE
1360int copy_dtb(uint8_t *boot_image_start)
1361{
1362 uint32 dt_image_offset = 0;
1363 uint32_t n;
1364 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001365 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001366 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001367
1368 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1369
Amol Jadicb524072012-08-09 16:40:18 -07001370 if(hdr->dt_size != 0) {
1371
1372 /* add kernel offset */
1373 dt_image_offset += page_size;
1374 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1375 dt_image_offset += n;
1376
1377 /* add ramdisk offset */
1378 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1379 dt_image_offset += n;
1380
1381 /* add second offset */
1382 if(hdr->second_size != 0) {
1383 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1384 dt_image_offset += n;
1385 }
1386
1387 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001388 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001389
Deepa Dinamani19648b42013-09-05 17:05:55 -07001390 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001391 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1392 return -1;
1393 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001394 /* Find index of device tree within device tree table */
1395 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001396 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1397 return -1;
1398 }
1399
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001400 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001401 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001402 {
1403 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1404 return -1;
1405 }
1406
Amol Jadicb524072012-08-09 16:40:18 -07001407 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001408 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001409 boot_image_start + dt_image_offset + dt_entry.offset,
1410 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001411 } else
1412 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001413
1414 /* Everything looks fine. Return success. */
1415 return 0;
1416}
1417#endif
1418
Brian Swetland9c4c0752009-01-25 16:23:50 -08001419void cmd_boot(const char *arg, void *data, unsigned sz)
1420{
1421 unsigned kernel_actual;
1422 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001423 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001424 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001425 int ret = 0;
1426 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001427
1428 if (sz < sizeof(hdr)) {
1429 fastboot_fail("invalid bootimage header");
1430 return;
1431 }
1432
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001433 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001434
1435 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001436 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001437
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001438 if(target_is_emmc_boot() && hdr->page_size) {
1439 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001440 page_mask = page_size - 1;
1441 }
1442
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001443 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1444 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1445
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001446 /*
1447 * Update the kernel/ramdisk/tags address if the boot image header
1448 * has default values, these default values come from mkbootimg when
1449 * the boot image is flashed using fastboot flash:raw
1450 */
1451 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001452
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001453 /* Get virtual addresses since the hdr saves physical addresses. */
1454 hdr->kernel_addr = VA(hdr->kernel_addr);
1455 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1456 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001457
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001458 /* Check if the addresses in the header are valid. */
1459 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1460 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1461 {
1462 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabia54102a2013-12-03 11:19:20 -08001463 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001464 }
1465
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001466 /* sz should have atleast raw boot image */
1467 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001468 fastboot_fail("incomplete bootimage");
1469 return;
1470 }
1471
Amol Jadicb524072012-08-09 16:40:18 -07001472#if DEVICE_TREE
1473 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001474 ret = copy_dtb(data);
1475
1476 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001477#else
1478 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1479 {
1480 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabia54102a2013-12-03 11:19:20 -08001481 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001482 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001483#endif
1484
1485 /* Load ramdisk & kernel */
1486 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1487 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1488
1489#if DEVICE_TREE
1490 /*
1491 * If dtb is not found look for appended DTB in the kernel.
1492 * If appended dev tree is found, update the atags with
1493 * memory address to the DTB appended location on RAM.
1494 * Else update with the atags address in the kernel header
1495 */
1496 if (!dtb_copied) {
1497 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001498 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1499 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001500 if (!dtb) {
1501 fastboot_fail("dtb not found");
1502 return;
1503 }
Amol Jadicb524072012-08-09 16:40:18 -07001504 }
1505#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001506
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001507#ifndef DEVICE_TREE
1508 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1509 {
1510 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabia54102a2013-12-03 11:19:20 -08001511 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001512 }
1513#endif
1514
Brian Swetland9c4c0752009-01-25 16:23:50 -08001515 fastboot_okay("");
1516 udc_stop();
1517
Dima Zavin77e41f32013-03-06 16:10:43 -08001518 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001519 (const char*) hdr->cmdline, board_machtype(),
1520 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001521}
1522
Dima Zavin214cc642009-01-26 11:16:21 -08001523void cmd_erase(const char *arg, void *data, unsigned sz)
1524{
1525 struct ptentry *ptn;
1526 struct ptable *ptable;
1527
1528 ptable = flash_get_ptable();
1529 if (ptable == NULL) {
1530 fastboot_fail("partition table doesn't exist");
1531 return;
1532 }
1533
1534 ptn = ptable_find(ptable, arg);
1535 if (ptn == NULL) {
1536 fastboot_fail("unknown partition name");
1537 return;
1538 }
1539
1540 if (flash_erase(ptn)) {
1541 fastboot_fail("failed to erase partition");
1542 return;
1543 }
1544 fastboot_okay("");
1545}
1546
Bikas Gurungd48bd242010-09-04 19:54:32 -07001547
1548void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1549{
Kun Liang2f1601a2013-08-12 16:29:54 +08001550 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001551 unsigned long long ptn = 0;
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001552 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001553 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001554
Kinson Chikf1a43512011-07-14 11:28:39 -07001555 index = partition_get_index(arg);
1556 ptn = partition_get_offset(index);
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001557 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001558
Kinson Chikf1a43512011-07-14 11:28:39 -07001559 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001560 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001561 return;
1562 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001563
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001564#if MMC_SDHCI_SUPPORT
1565 if (mmc_erase_card(ptn, size)) {
1566 fastboot_fail("failed to erase partition\n");
1567 return;
1568 }
1569#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001570 size = partition_get_size(index);
1571 if (size > DEFAULT_ERASE_SIZE)
1572 size = DEFAULT_ERASE_SIZE;
1573
neetidb4b24d62012-01-20 12:13:09 -08001574 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001575 0 in first several blocks */
1576 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001577 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001578 return;
1579 }
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001580#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001581 fastboot_okay("");
1582}
1583
1584
Ajay Dudani5c761132011-04-07 20:19:04 -07001585void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001586{
1587 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001588 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001589 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001590
Greg Grisco6e754772011-06-23 12:19:39 -07001591 if (!strcmp(arg, "partition"))
1592 {
1593 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001594 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001595 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001596 return;
1597 }
1598 }
Greg Grisco6e754772011-06-23 12:19:39 -07001599 else
1600 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001601 index = partition_get_index(arg);
1602 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001603 if(ptn == 0) {
1604 fastboot_fail("partition table doesn't exist");
1605 return;
1606 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001607
Greg Grisco6e754772011-06-23 12:19:39 -07001608 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1609 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1610 fastboot_fail("image is not a boot image");
1611 return;
1612 }
1613 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001614
Kinson Chikf1a43512011-07-14 11:28:39 -07001615 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001616 if (ROUND_TO_PAGE(sz,511) > size) {
1617 fastboot_fail("size too large");
1618 return;
1619 }
1620 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1621 fastboot_fail("flash write failure");
1622 return;
1623 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001624 }
1625 fastboot_okay("");
1626 return;
1627}
1628
Ajay Dudani5c761132011-04-07 20:19:04 -07001629void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1630{
1631 unsigned int chunk;
1632 unsigned int chunk_data_sz;
1633 sparse_header_t *sparse_header;
1634 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001635 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001636 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301637 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001638 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001639
Kinson Chikf1a43512011-07-14 11:28:39 -07001640 index = partition_get_index(arg);
1641 ptn = partition_get_offset(index);
1642 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001643 fastboot_fail("partition table doesn't exist");
1644 return;
1645 }
1646
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301647 size = partition_get_size(index);
1648 if (ROUND_TO_PAGE(sz,511) > size) {
1649 fastboot_fail("size too large");
1650 return;
1651 }
1652
Ajay Dudani5c761132011-04-07 20:19:04 -07001653 /* Read and skip over sparse image header */
1654 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001655 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1656 fastboot_fail("size too large");
1657 return;
1658 }
1659
Ajay Dudani5c761132011-04-07 20:19:04 -07001660 data += sparse_header->file_hdr_sz;
1661 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1662 {
1663 /* Skip the remaining bytes in a header that is longer than
1664 * we expected.
1665 */
1666 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1667 }
1668
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001669 dprintf (SPEW, "=== Sparse Image Header ===\n");
1670 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1671 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1672 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1673 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1674 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1675 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1676 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1677 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001678
1679 /* Start processing chunks */
1680 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1681 {
1682 /* Read and skip over chunk header */
1683 chunk_header = (chunk_header_t *) data;
1684 data += sizeof(chunk_header_t);
1685
1686 dprintf (SPEW, "=== Chunk Header ===\n");
1687 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1688 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1689 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1690
1691 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1692 {
1693 /* Skip the remaining bytes in a header that is longer than
1694 * we expected.
1695 */
1696 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1697 }
1698
1699 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1700 switch (chunk_header->chunk_type)
1701 {
1702 case CHUNK_TYPE_RAW:
1703 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1704 chunk_data_sz))
1705 {
1706 fastboot_fail("Bogus chunk size for chunk type Raw");
1707 return;
1708 }
1709
Ajay Dudaniab18f022011-05-12 14:39:22 -07001710 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1711 chunk_data_sz,
1712 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001713 {
1714 fastboot_fail("flash write failure");
1715 return;
1716 }
1717 total_blocks += chunk_header->chunk_sz;
1718 data += chunk_data_sz;
1719 break;
1720
1721 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001722 total_blocks += chunk_header->chunk_sz;
1723 break;
1724
Ajay Dudani5c761132011-04-07 20:19:04 -07001725 case CHUNK_TYPE_CRC:
1726 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1727 {
1728 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1729 return;
1730 }
1731 total_blocks += chunk_header->chunk_sz;
1732 data += chunk_data_sz;
1733 break;
1734
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001735 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001736 fastboot_fail("Unknown chunk type");
1737 return;
1738 }
1739 }
1740
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001741 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1742 total_blocks, sparse_header->total_blks);
1743
1744 if(total_blocks != sparse_header->total_blks)
1745 {
1746 fastboot_fail("sparse image write failure");
1747 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001748
1749 fastboot_okay("");
1750 return;
1751}
1752
1753void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1754{
1755 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001756 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1757 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001758
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001759#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001760 int ret=0;
1761 uint32 major_version=0;
1762 uint32 minor_version=0;
1763
1764 ret = scm_svc_version(&major_version,&minor_version);
1765 if(!ret)
1766 {
1767 if(major_version >= 2)
1768 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001769 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001770 {
1771 ret = encrypt_scm((uint32 **) &data, &sz);
1772 if (ret != 0) {
1773 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1774 return;
1775 }
1776
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001777 /* Protect only for SSD */
1778 if (!strcmp(arg, "ssd")) {
1779 ret = scm_protect_keystore((uint32 *) data, sz);
1780 if (ret != 0) {
1781 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1782 return;
1783 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001784 }
1785 }
1786 else
1787 {
1788 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1789 if(ret != 0)
1790 {
1791 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1792 return;
1793 }
1794 }
1795 }
1796 else
1797 {
1798 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1799 magic_number[1] == DECRYPT_MAGIC_1)
1800 {
1801 ret = decrypt_scm((uint32 **) &data, &sz);
1802 if (ret != 0) {
1803 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1804 return;
1805 }
1806 }
1807 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1808 magic_number[1] == ENCRYPT_MAGIC_1)
1809 {
1810 ret = encrypt_scm((uint32 **) &data, &sz);
1811 if (ret != 0) {
1812 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1813 return;
1814 }
1815 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001816 }
1817 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001818 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001819 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001820 dprintf(CRITICAL,"INVALID SVC Version\n");
1821 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001822 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001823#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001824
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001825 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001826 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1827 cmd_flash_mmc_img(arg, data, sz);
1828 else
1829 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001830 return;
1831}
1832
Dima Zavin214cc642009-01-26 11:16:21 -08001833void cmd_flash(const char *arg, void *data, unsigned sz)
1834{
1835 struct ptentry *ptn;
1836 struct ptable *ptable;
1837 unsigned extra = 0;
1838
1839 ptable = flash_get_ptable();
1840 if (ptable == NULL) {
1841 fastboot_fail("partition table doesn't exist");
1842 return;
1843 }
1844
1845 ptn = ptable_find(ptable, arg);
1846 if (ptn == NULL) {
1847 fastboot_fail("unknown partition name");
1848 return;
1849 }
1850
1851 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1852 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1853 fastboot_fail("image is not a boot image");
1854 return;
1855 }
1856 }
1857
Amol Jadi5c61a952012-05-04 17:05:35 -07001858 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001859 || !strcmp(ptn->name, "userdata")
1860 || !strcmp(ptn->name, "persist")
1861 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301862 if (flash_ecc_bch_enabled())
1863 /* Spare data bytes for 8 bit ECC increased by 4 */
1864 extra = ((page_size >> 9) * 20);
1865 else
1866 extra = ((page_size >> 9) * 16);
1867 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001868 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001869
1870 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1871 if (flash_write(ptn, extra, data, sz)) {
1872 fastboot_fail("flash write failure");
1873 return;
1874 }
1875 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1876 fastboot_okay("");
1877}
1878
1879void cmd_continue(const char *arg, void *data, unsigned sz)
1880{
1881 fastboot_okay("");
1882 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001883 if (target_is_emmc_boot())
1884 {
1885 boot_linux_from_mmc();
1886 }
1887 else
1888 {
1889 boot_linux_from_flash();
1890 }
Dima Zavin214cc642009-01-26 11:16:21 -08001891}
1892
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001893void cmd_reboot(const char *arg, void *data, unsigned sz)
1894{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001895 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001896 fastboot_okay("");
1897 reboot_device(0);
1898}
1899
1900void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1901{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001902 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001903 fastboot_okay("");
1904 reboot_device(FASTBOOT_MODE);
1905}
1906
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001907void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
1908{
1909 dprintf(INFO, "Enabling charger screen check\n");
1910 device.charger_screen_enabled = 1;
1911 write_device_info(&device);
1912 fastboot_okay("");
1913}
1914
1915void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
1916{
1917 dprintf(INFO, "Disabling charger screen check\n");
1918 device.charger_screen_enabled = 0;
1919 write_device_info(&device);
1920 fastboot_okay("");
1921}
1922
Shashank Mittal162244e2011-08-08 19:01:25 -07001923void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1924{
1925 if(!device.is_unlocked)
1926 {
1927 device.is_unlocked = 1;
1928 write_device_info(&device);
1929 }
1930 fastboot_okay("");
1931}
1932
Shashank Mittala0032282011-08-26 14:50:11 -07001933void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1934{
1935 char response[64];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001936 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07001937 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001938 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1939 fastboot_info(response);
1940 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07001941 fastboot_info(response);
1942 fastboot_okay("");
1943}
1944
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001945void cmd_preflash(const char *arg, void *data, unsigned sz)
1946{
1947 fastboot_okay("");
1948}
1949
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05301950static struct fbimage logo_header = {0};
1951struct fbimage* splash_screen_flash();
1952
1953int splash_screen_check_header(struct fbimage *logo)
1954{
1955 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
1956 return -1;
1957 if (logo->header.width == 0 || logo->header.height == 0)
1958 return -1;
1959 return 0;
1960}
1961
1962struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001963{
1964 struct ptentry *ptn;
1965 struct ptable *ptable;
1966 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05301967 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001968
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05301969
1970 ptable = flash_get_ptable();
1971 if (ptable == NULL) {
1972 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1973 return NULL;
1974 }
1975 ptn = ptable_find(ptable, "splash");
1976 if (ptn == NULL) {
1977 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
1978 return NULL;
1979 }
1980
1981 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
1982 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1983 return NULL;
1984 }
1985
1986 if (splash_screen_check_header(logo)) {
1987 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
1988 return NULL;
1989 }
1990
1991 fb_display = fbcon_display();
1992 if (fb_display) {
1993 uint8_t *base = (uint8_t *) fb_display->base;
1994 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
1995 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001996 }
1997
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05301998 if (flash_read(ptn + sizeof(logo->header), 0,
1999 base,
2000 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2001 fbcon_clear();
2002 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2003 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002004 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302005 logo->image = base;
2006 }
2007
2008 return logo;
2009}
2010
2011struct fbimage* splash_screen_mmc()
2012{
2013 int index = INVALID_PTN;
2014 unsigned long long ptn = 0;
2015 struct fbcon_config *fb_display = NULL;
2016 struct fbimage *logo = &logo_header;
2017
2018 index = partition_get_index("splash");
2019 if (index == 0) {
2020 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2021 return NULL;
2022 }
2023
2024 ptn = partition_get_offset(index);
2025 if (ptn == 0) {
2026 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2027 return NULL;
2028 }
2029
2030 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2031 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2032 return NULL;
2033 }
2034
2035 if (splash_screen_check_header(logo)) {
2036 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2037 return NULL;
2038 }
2039
2040 fb_display = fbcon_display();
2041 if (fb_display) {
2042 uint8_t *base = (uint8_t *) fb_display->base;
2043 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2044 base += LOGO_IMG_OFFSET;
2045
2046 if (mmc_read(ptn + sizeof(logo->header),
2047 base,
2048 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2049 fbcon_clear();
2050 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2051 return NULL;
2052 }
2053
2054 logo->image = base;
2055 }
2056
2057 return logo;
2058}
2059
2060
2061struct fbimage* fetch_image_from_partition()
2062{
2063 if (target_is_emmc_boot()) {
2064 return splash_screen_mmc();
2065 } else {
2066 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002067 }
2068}
2069
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002070/* Get the size from partiton name */
2071static void get_partition_size(const char *arg, char *response)
2072{
2073 uint64_t ptn = 0;
2074 uint64_t size;
2075 int index = INVALID_PTN;
2076
2077 index = partition_get_index(arg);
2078
2079 if (index == INVALID_PTN)
2080 {
2081 dprintf(CRITICAL, "Invalid partition index\n");
2082 return;
2083 }
2084
2085 ptn = partition_get_offset(index);
2086
2087 if(!ptn)
2088 {
2089 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2090 return;
2091 }
2092
2093 size = partition_get_size(index);
2094
2095 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2096 return;
2097}
2098
2099/*
2100 * Publish the partition type & size info
2101 * fastboot getvar will publish the required information.
2102 * fastboot getvar partition_size:<partition_name>: partition size in hex
2103 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2104 */
2105static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2106{
2107 uint8_t i;
2108
2109 for (i = 0; i < num_parts; i++) {
2110 get_partition_size(info[i].part_name, info[i].size_response);
2111
2112 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2113 {
2114 dprintf(CRITICAL, "partition size name truncated\n");
2115 return;
2116 }
2117 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2118 {
2119 dprintf(CRITICAL, "partition type name truncated\n");
2120 return;
2121 }
2122
2123 /* publish partition size & type info */
2124 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2125 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2126 }
2127}
2128
Amol Jadi5edf3552013-07-23 14:15:34 -07002129/* register commands and variables for fastboot */
2130void aboot_fastboot_register_commands(void)
2131{
2132 if (target_is_emmc_boot())
2133 {
2134 fastboot_register("flash:", cmd_flash_mmc);
2135 fastboot_register("erase:", cmd_erase_mmc);
2136 }
2137 else
2138 {
2139 fastboot_register("flash:", cmd_flash);
2140 fastboot_register("erase:", cmd_erase);
2141 }
2142
2143 fastboot_register("boot", cmd_boot);
2144 fastboot_register("continue", cmd_continue);
2145 fastboot_register("reboot", cmd_reboot);
2146 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2147 fastboot_register("oem unlock", cmd_oem_unlock);
2148 fastboot_register("oem device-info", cmd_oem_devinfo);
2149 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002150 fastboot_register("oem enable-charger-screen",
2151 cmd_oem_enable_charger_screen);
2152 fastboot_register("oem disable-charger-screen",
2153 cmd_oem_disable_charger_screen);
Amol Jadi5edf3552013-07-23 14:15:34 -07002154 /* publish variables and their values */
2155 fastboot_publish("product", TARGET(BOARD));
2156 fastboot_publish("kernel", "lk");
2157 fastboot_publish("serialno", sn_buf);
2158
2159 /*
2160 * partition info is supported only for emmc partitions
2161 * Calling this for NAND prints some error messages which
2162 * is harmless but misleading. Avoid calling this for NAND
2163 * devices.
2164 */
2165 if (target_is_emmc_boot())
2166 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2167
2168 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002169 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2170 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002171 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002172 /* Is the charger screen check enabled */
2173 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2174 device.charger_screen_enabled);
2175 fastboot_publish("charger-screen-enabled",
2176 (const char *) charger_screen_enabled);
Amol Jadi5edf3552013-07-23 14:15:34 -07002177}
2178
Brian Swetland9c4c0752009-01-25 16:23:50 -08002179void aboot_init(const struct app_descriptor *app)
2180{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002181 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002182 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002183
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002184 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002185 if (target_is_emmc_boot())
2186 {
2187 page_size = 2048;
2188 page_mask = page_size - 1;
2189 }
2190 else
2191 {
2192 page_size = flash_page_size();
2193 page_mask = page_size - 1;
2194 }
2195
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002196 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2197
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002198 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002199
Aravind Venkateswarandb0d5262014-02-25 16:25:30 -08002200 /* Display splash screen if enabled */
2201#if DISPLAY_SPLASH_SCREEN
2202 dprintf(SPEW, "Display Init: Start\n");
2203 target_display_init();
2204 dprintf(SPEW, "Display Init: Done\n");
2205#endif
2206
2207
Greg Griscod6250552011-06-29 14:40:23 -07002208 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002209 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002210
Dhaval Patel223ec952013-07-18 14:49:44 -07002211 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2212
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002213 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002214 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002215 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002216 dprintf(ALWAYS,"dload mode key sequence detected\n");
2217 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002218 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002219 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002220 }
2221 else
2222 {
2223 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002224 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002225 }
2226 boot_into_fastboot = true;
2227 }
2228 if (!boot_into_fastboot)
2229 {
2230 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2231 boot_into_recovery = 1;
2232 if (!boot_into_recovery &&
2233 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002234 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002235 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002236 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002237 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002238 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002239 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002240
Ajay Dudani77421292010-10-27 19:34:06 -07002241 reboot_mode = check_reboot_mode();
2242 if (reboot_mode == RECOVERY_MODE) {
2243 boot_into_recovery = 1;
2244 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002245 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002246 }
2247
Pavel Nedev5d91d412013-04-29 11:34:24 +03002248 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002249 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002250 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002251 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002252 if(emmc_recovery_init())
2253 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2254 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002255 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002256 if((device.is_unlocked) || (device.is_tampered))
2257 {
2258 #ifdef TZ_TAMPER_FUSE
2259 set_tamper_fuse_cmd();
2260 #endif
2261 #if USE_PCOM_SECBOOT
2262 set_tamper_flag(device.is_tampered);
2263 #endif
2264 }
Shashank Mittala0032282011-08-26 14:50:11 -07002265 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002266 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002267 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002268 else
2269 {
2270 recovery_init();
2271 #if USE_PCOM_SECBOOT
2272 if((device.is_unlocked) || (device.is_tampered))
2273 set_tamper_flag(device.is_tampered);
2274 #endif
2275 boot_linux_from_flash();
2276 }
2277 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2278 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002279 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002280
Amol Jadi5edf3552013-07-23 14:15:34 -07002281 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002282
Amol Jadi5edf3552013-07-23 14:15:34 -07002283 /* register aboot specific fastboot commands */
2284 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002285
Amol Jadi5edf3552013-07-23 14:15:34 -07002286 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002287 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002288
2289 /* initialize and start fastboot */
2290 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002291}
2292
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002293uint32_t get_page_size()
2294{
2295 return page_size;
2296}
2297
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002298/*
2299 * Calculated and save hash (SHA256) for non-signed boot image.
2300 *
2301 * Hash the same data that is checked on the signed boot image.
2302 * Kernel and Ramdisk are already read to memory buffers.
2303 * Need to read the entire device-tree from mmc
2304 * since non-signed image only read the DT tags of the relevant platform.
2305 *
2306 * @param kernel_addr - kernel bufer
2307 * @param kernel_actual - kernel size in bytes
2308 * @param ramdisk_addr - ramdisk buffer
2309 * @param ramdisk_actual - ramdisk size
2310 * @param ptn - partition
2311 * @param dt_offset - device tree offset on mmc partition
2312 * @param dt_size
2313 *
2314 * @return int - 0 on success, negative value on failure.
2315 */
2316int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2317 void *ramdisk_addr, unsigned ramdisk_actual,
2318 unsigned long long ptn,
2319 unsigned dt_offset, unsigned dt_size)
2320{
2321 SHA256_CTX sha256_ctx;
2322 char digest[32]={0};
2323 char *buf = (char *)target_get_scratch_address();
2324 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2325 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2326
2327 SHA256_Init(&sha256_ctx);
2328
2329 /* Read Boot Header */
2330 if (mmc_read(ptn, buf, page_size))
2331 {
2332 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2333 return -1;
2334 }
2335 /* Read entire Device Tree */
2336 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2337 {
2338 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2339 return -1;
2340 }
2341 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2342 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2343 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2344 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2345
2346 SHA256_Final(digest, &sha256_ctx);
2347
2348 save_kernel_hash_cmd(digest);
2349 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2350
2351 return 0;
2352}
2353
Brian Swetland9c4c0752009-01-25 16:23:50 -08002354APP_START(aboot)
2355 .init = aboot_init,
2356APP_END