blob: ab52899eb33367191e34d49f355d29287c6fa6c9 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Amol Jadi2a15a272013-01-22 12:03:36 -08005 * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080038#include <kernel/thread.h>
39#include <arch/ops.h>
40
Dima Zavin214cc642009-01-26 11:16:21 -080041#include <dev/flash.h>
42#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080043#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080044#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080045#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070046#include <target.h>
47#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070048#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070049#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070050#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070051#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070052#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030053#include <sha.h>
Dima Zavin214cc642009-01-26 11:16:21 -080054
Neeti Desai17379b82012-06-04 18:42:53 -070055#if DEVICE_TREE
56#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070057#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070058#endif
59
Shashank Mittalcd98d472011-08-02 14:29:24 -070060#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080061#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080062#include "bootimg.h"
63#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070064#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070065#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070066#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070067#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080068
Shashank Mittal162244e2011-08-08 19:01:25 -070069#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070070
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070071extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070072extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070073extern void target_uninit(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070074
75void write_device_info_mmc(device_info *dev);
76void write_device_info_flash(device_info *dev);
77
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070078#define EXPAND(NAME) #NAME
79#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070080
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080081#ifdef MEMBASE
82#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
83#else
David Ng183a7422009-12-07 14:55:21 -080084#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080085#endif
86
Deepa Dinamani0e163a42013-05-24 17:08:15 -070087#ifndef MEMSIZE
88#define MEMSIZE 1024*1024
89#endif
90
91#define MAX_TAGS_SIZE 1024
92
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080093#define RECOVERY_MODE 0x77665502
94#define FASTBOOT_MODE 0x77665500
95
Kun Liang2f1601a2013-08-12 16:29:54 +080096/* make 4096 as default size to ensure EFS,EXT4's erasing */
97#define DEFAULT_ERASE_SIZE 4096
Dhaval Patel223ec952013-07-18 14:49:44 -070098#define MAX_PANEL_BUF_SIZE 64
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
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800524 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
525 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800526
527 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700528
Amol Jadi4421e652011-06-16 15:00:48 -0700529 /* do any platform specific cleanup before kernel entry */
530 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700531
Brian Swetland9c4c0752009-01-25 16:23:50 -0800532 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700533
Amol Jadi504f9fe2012-08-16 13:56:48 -0700534#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800535 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700536#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700537 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700538 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800539}
540
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700541/* Function to check if the memory address range falls within the aboot
542 * boundaries.
543 * start: Start of the memory region
544 * size: Size of the memory region
545 */
546int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
547{
548 /* Check for boundary conditions. */
549 if ((start + size) < start)
550 return -1;
551
552 /* Check for memory overlap. */
553 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
554 return 0;
555 else if (start > (MEMBASE + MEMSIZE))
556 return 0;
557 else
558 return -1;
559}
560
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800561#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800562
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700563BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800564#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700565BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800566#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800567
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700568static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
569{
570 int ret;
571
572 /* Assume device is rooted at this time. */
573 device.is_tampered = 1;
574
575 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
576
577 ret = image_verify((unsigned char *)bootimg_addr,
578 (unsigned char *)(bootimg_addr + bootimg_size),
579 bootimg_size,
580 CRYPTO_AUTH_ALG_SHA256);
581
582 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
583
584 if (ret)
585 {
586 /* Authorized kernel */
587 device.is_tampered = 0;
588 }
589
590#if USE_PCOM_SECBOOT
591 set_tamper_flag(device.is_tampered);
592#endif
593
594 if(device.is_tampered)
595 {
596 write_device_info_mmc(&device);
597 #ifdef TZ_TAMPER_FUSE
598 set_tamper_fuse_cmd();
599 #endif
600 #ifdef ASSERT_ON_TAMPER
601 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
602 ASSERT(0);
603 #endif
604 }
605}
606
Shashank Mittal23b8f422010-04-16 19:27:21 -0700607int boot_linux_from_mmc(void)
608{
609 struct boot_img_hdr *hdr = (void*) buf;
610 struct boot_img_hdr *uhdr;
611 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700612 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700613 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700614 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700615
Shashank Mittalcd98d472011-08-02 14:29:24 -0700616 unsigned char *image_addr = 0;
617 unsigned kernel_actual;
618 unsigned ramdisk_actual;
619 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700620 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700621
622#if DEVICE_TREE
623 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700624 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700625 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800626 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700627#endif
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700628 if (!boot_into_recovery) {
629 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
630 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
631 if (rcode <= 0) {
632 boot_into_ffbm = false;
633 if (rcode < 0)
634 dprintf(CRITICAL,"failed to get ffbm cookie");
635 } else
636 boot_into_ffbm = true;
637 } else
638 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700639 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
640 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
641 dprintf(INFO, "Unified boot method!\n");
642 hdr = uhdr;
643 goto unified_boot;
644 }
Greg Griscod6250552011-06-29 14:40:23 -0700645 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700646 index = partition_get_index("boot");
647 ptn = partition_get_offset(index);
648 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700649 dprintf(CRITICAL, "ERROR: No boot partition found\n");
650 return -1;
651 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700652 }
653 else {
654 index = partition_get_index("recovery");
655 ptn = partition_get_offset(index);
656 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700657 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
658 return -1;
659 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700660 }
661
Greg Griscod6250552011-06-29 14:40:23 -0700662 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700663 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
664 return -1;
665 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700666
667 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700668 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700669 return -1;
670 }
671
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700672 if (hdr->page_size && (hdr->page_size != page_size)) {
673 page_size = hdr->page_size;
674 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700675 }
676
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700677 /*
678 * Update the kernel/ramdisk/tags address if the boot image header
679 * has default values, these default values come from mkbootimg when
680 * the boot image is flashed using fastboot flash:raw
681 */
682 update_ker_tags_rdisk_addr(hdr);
683
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700684 /* Get virtual addresses since the hdr saves physical addresses. */
685 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
686 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
687 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700688
689 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
690 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
691
692 /* Check if the addresses in the header are valid. */
693 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
694 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
695 {
696 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
697 return -1;
698 }
699
700#ifndef DEVICE_TREE
701 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
702 {
703 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
704 return -1;
705 }
706#endif
707
Shashank Mittalcd98d472011-08-02 14:29:24 -0700708 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300709 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
710 (int) target_use_signed_kernel(),
711 device.is_unlocked,
712 device.is_tampered);
713
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700714 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800715 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700716 offset = 0;
717
Shashank Mittalcd98d472011-08-02 14:29:24 -0700718 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700719
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800720#if DEVICE_TREE
721 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
722 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700723
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700724 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700725 {
726 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
727 return -1;
728 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800729#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530730 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700731
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700732#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700733
Amol Jadib6be5c12012-11-14 13:39:51 -0800734 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700735 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800736
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700737 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
738 {
739 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
740 return -1;
741 }
742
Shashank Mittalcd98d472011-08-02 14:29:24 -0700743 /* Read image without signature */
744 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
745 {
746 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
747 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800748 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700749
Amol Jadib6be5c12012-11-14 13:39:51 -0800750 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700751 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800752
Shashank Mittalcd98d472011-08-02 14:29:24 -0700753 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700754
755 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
756 {
757 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
758 return -1;
759 }
760
Shashank Mittalcd98d472011-08-02 14:29:24 -0700761 /* Read signature */
762 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
763 {
764 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700765 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700766 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800767
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700768 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700769
Neeti Desai465491e2012-07-31 12:53:35 -0700770 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700771 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
772 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700773
Neeti Desai465491e2012-07-31 12:53:35 -0700774 #if DEVICE_TREE
775 if(hdr->dt_size) {
776 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700777 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700778
779 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
780
Joel Kingaa335dc2013-06-03 16:11:08 -0700781 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700782 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
783 return -1;
784 }
Neeti Desai465491e2012-07-31 12:53:35 -0700785 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700786 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700787 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
788 return -1;
789 }
790
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700791 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700792 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700793 {
794 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
795 return -1;
796 }
797
Joel Kingaa335dc2013-06-03 16:11:08 -0700798 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800799 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800800 /*
801 * If appended dev tree is found, update the atags with
802 * memory address to the DTB appended location on RAM.
803 * Else update with the atags address in the kernel header
804 */
805 void *dtb;
806 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700807 hdr->kernel_size,
808 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800809 if (!dtb) {
810 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
811 return -1;
812 }
Neeti Desai465491e2012-07-31 12:53:35 -0700813 }
814 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700815 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700816 else
817 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800818 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700819
Amol Jadib6be5c12012-11-14 13:39:51 -0800820 dprintf(INFO, "Loading boot image (%d): start\n",
821 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700822 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800823
824 offset = page_size;
825
826 /* Load kernel */
827 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700828 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
829 return -1;
830 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800831 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700832
Amol Jadib6be5c12012-11-14 13:39:51 -0800833 /* Load ramdisk */
834 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700835 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800836 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700837 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
838 return -1;
839 }
840 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800841 offset += ramdisk_actual;
842
843 dprintf(INFO, "Loading boot image (%d): done\n",
844 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700845 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700846
847 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800848 offset += second_actual;
849 /* Second image loading not implemented. */
850 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700851 }
852
853 #if DEVICE_TREE
854 if(hdr->dt_size != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700855 /* Read the device tree table into buffer */
856 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
857 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
858 return -1;
859 }
860 table = (struct dt_table*) dt_buf;
861
Joel Kingaa335dc2013-06-03 16:11:08 -0700862 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700863 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
864 return -1;
865 }
866
Joel Kingaa335dc2013-06-03 16:11:08 -0700867 /* Find index of device tree within device tree table */
868 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700869 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
870 return -1;
871 }
872
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700873 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700874 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700875 {
876 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
877 return -1;
878 }
879
Joel Kingaa335dc2013-06-03 16:11:08 -0700880 if(mmc_read(ptn + offset + dt_entry.offset,
881 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700882 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
883 return -1;
884 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300885 #ifdef TZ_SAVE_KERNEL_HASH
886 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
887 hdr->ramdisk_addr, ramdisk_actual,
888 ptn, offset, hdr->dt_size);
889 #endif /* TZ_SAVE_KERNEL_HASH */
890
Channagoud Kadabi35095622013-03-01 13:53:05 -0800891 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800892 /*
893 * If appended dev tree is found, update the atags with
894 * memory address to the DTB appended location on RAM.
895 * Else update with the atags address in the kernel header
896 */
897 void *dtb;
898 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700899 kernel_actual,
900 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800901 if (!dtb) {
902 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
903 return -1;
904 }
Neeti Desai465491e2012-07-31 12:53:35 -0700905 }
906 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700907 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700908
Stanimir Varbanov69ec5462013-07-18 18:17:42 +0300909 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
910 target_load_ssd_keystore();
911
Shashank Mittal23b8f422010-04-16 19:27:21 -0700912unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700913
Dima Zavin77e41f32013-03-06 16:10:43 -0800914 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700915 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700916 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
917
918 return 0;
919}
920
Dima Zavin214cc642009-01-26 11:16:21 -0800921int boot_linux_from_flash(void)
922{
923 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800924 struct ptentry *ptn;
925 struct ptable *ptable;
926 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800927
Shashank Mittalcd98d472011-08-02 14:29:24 -0700928 unsigned char *image_addr = 0;
929 unsigned kernel_actual;
930 unsigned ramdisk_actual;
931 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800932 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700933
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700934#if DEVICE_TREE
935 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700936 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800937 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700938#endif
939
David Ng183a7422009-12-07 14:55:21 -0800940 if (target_is_emmc_boot()) {
941 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
942 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
943 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
944 return -1;
945 }
946 goto continue_boot;
947 }
948
Dima Zavin214cc642009-01-26 11:16:21 -0800949 ptable = flash_get_ptable();
950 if (ptable == NULL) {
951 dprintf(CRITICAL, "ERROR: Partition table not found\n");
952 return -1;
953 }
954
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800955 if(!boot_into_recovery)
956 {
957 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700958
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800959 if (ptn == NULL) {
960 dprintf(CRITICAL, "ERROR: No boot partition found\n");
961 return -1;
962 }
963 }
964 else
965 {
966 ptn = ptable_find(ptable, "recovery");
967 if (ptn == NULL) {
968 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
969 return -1;
970 }
Dima Zavin214cc642009-01-26 11:16:21 -0800971 }
972
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800973 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800974 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
975 return -1;
976 }
Dima Zavin214cc642009-01-26 11:16:21 -0800977
978 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700979 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800980 return -1;
981 }
982
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800983 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700984 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 -0800985 return -1;
986 }
987
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700988 /*
989 * Update the kernel/ramdisk/tags address if the boot image header
990 * has default values, these default values come from mkbootimg when
991 * the boot image is flashed using fastboot flash:raw
992 */
993 update_ker_tags_rdisk_addr(hdr);
994
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700995 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700996 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
997 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
998 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
999
1000 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1001 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1002
1003 /* Check if the addresses in the header are valid. */
1004 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1005 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1006 {
1007 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1008 return -1;
1009 }
1010
1011#ifndef DEVICE_TREE
1012 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1013 {
1014 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1015 return -1;
1016 }
1017#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001018
Shashank Mittalcd98d472011-08-02 14:29:24 -07001019 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001020 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001021 {
1022 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001023 offset = 0;
1024
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001025#if DEVICE_TREE
1026 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1027 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001028
1029 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1030 {
1031 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1032 return -1;
1033 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001034#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001035 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001036#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001037
Amol Jadib6be5c12012-11-14 13:39:51 -08001038 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001039 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001040
Shashank Mittalcd98d472011-08-02 14:29:24 -07001041 /* Read image without signature */
1042 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1043 {
1044 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1045 return -1;
1046 }
Dima Zavin214cc642009-01-26 11:16:21 -08001047
Amol Jadib6be5c12012-11-14 13:39:51 -08001048 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001049 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001050
Shashank Mittalcd98d472011-08-02 14:29:24 -07001051 offset = imagesize_actual;
1052 /* Read signature */
1053 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1054 {
1055 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001056 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001057 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001058
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001059 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001060
1061 /* Move kernel and ramdisk to correct address */
1062 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1063 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001064#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001065 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001066 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001067 {
1068 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1069 return -1;
1070 }
1071
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001072 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1073#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001074
1075 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001076 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001077 {
1078 write_device_info_flash(&device);
1079 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301080#if USE_PCOM_SECBOOT
1081 set_tamper_flag(device.is_tampered);
1082#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001083 }
1084 else
1085 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001086 offset = page_size;
1087
Amol Jadib6be5c12012-11-14 13:39:51 -08001088 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1089 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1090 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1091
1092 dprintf(INFO, "Loading boot image (%d): start\n",
1093 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001094 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001095
1096 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001097 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1098 return -1;
1099 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001100 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001101
Amol Jadib6be5c12012-11-14 13:39:51 -08001102 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001103 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1104 return -1;
1105 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001106 offset += ramdisk_actual;
1107
1108 dprintf(INFO, "Loading boot image (%d): done\n",
1109 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001110 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001111
1112 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001113 offset += second_actual;
1114 /* Second image loading not implemented. */
1115 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001116 }
1117
1118#if DEVICE_TREE
1119 if(hdr->dt_size != 0) {
1120
1121 /* Read the device tree table into buffer */
1122 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1123 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1124 return -1;
1125 }
1126
1127 table = (struct dt_table*) dt_buf;
1128
Joel Kingaa335dc2013-06-03 16:11:08 -07001129 if (dev_tree_validate(table, hdr->page_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001130 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1131 return -1;
1132 }
1133
Joel Kingaa335dc2013-06-03 16:11:08 -07001134 /* Find index of device tree within device tree table */
1135 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001136 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1137 return -1;
1138 }
1139
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001140 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001141 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001142 {
1143 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1144 return -1;
1145 }
1146
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001147 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001148 if(flash_read(ptn, offset + dt_entry.offset,
1149 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001150 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1151 return -1;
1152 }
1153 }
1154#endif
1155
Shashank Mittalcd98d472011-08-02 14:29:24 -07001156 }
David Ng183a7422009-12-07 14:55:21 -08001157continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001158
Dima Zavin214cc642009-01-26 11:16:21 -08001159 /* TODO: create/pass atags to kernel */
1160
Ajay Dudanie28a6072011-07-01 13:59:46 -07001161 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001162 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001163 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1164
1165 return 0;
1166}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001167
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001168BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001169void write_device_info_mmc(device_info *dev)
1170{
1171 struct device_info *info = (void*) info_buf;
1172 unsigned long long ptn = 0;
1173 unsigned long long size;
1174 int index = INVALID_PTN;
1175
1176 index = partition_get_index("aboot");
1177 ptn = partition_get_offset(index);
1178 if(ptn == 0)
1179 {
1180 return;
1181 }
1182
1183 size = partition_get_size(index);
1184
1185 memcpy(info, dev, sizeof(device_info));
1186
1187 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1188 {
1189 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1190 return;
1191 }
1192}
1193
1194void read_device_info_mmc(device_info *dev)
1195{
1196 struct device_info *info = (void*) info_buf;
1197 unsigned long long ptn = 0;
1198 unsigned long long size;
1199 int index = INVALID_PTN;
1200
1201 index = partition_get_index("aboot");
1202 ptn = partition_get_offset(index);
1203 if(ptn == 0)
1204 {
1205 return;
1206 }
1207
1208 size = partition_get_size(index);
1209
1210 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1211 {
1212 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1213 return;
1214 }
1215
1216 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1217 {
1218 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1219 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001220 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001221 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001222
1223 write_device_info_mmc(info);
1224 }
1225 memcpy(dev, info, sizeof(device_info));
1226}
1227
1228void write_device_info_flash(device_info *dev)
1229{
1230 struct device_info *info = (void *) info_buf;
1231 struct ptentry *ptn;
1232 struct ptable *ptable;
1233
1234 ptable = flash_get_ptable();
1235 if (ptable == NULL)
1236 {
1237 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1238 return;
1239 }
1240
1241 ptn = ptable_find(ptable, "devinfo");
1242 if (ptn == NULL)
1243 {
1244 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1245 return;
1246 }
1247
1248 memcpy(info, dev, sizeof(device_info));
1249
1250 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1251 {
1252 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1253 return;
1254 }
1255}
1256
1257void read_device_info_flash(device_info *dev)
1258{
1259 struct device_info *info = (void*) info_buf;
1260 struct ptentry *ptn;
1261 struct ptable *ptable;
1262
1263 ptable = flash_get_ptable();
1264 if (ptable == NULL)
1265 {
1266 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1267 return;
1268 }
1269
1270 ptn = ptable_find(ptable, "devinfo");
1271 if (ptn == NULL)
1272 {
1273 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1274 return;
1275 }
1276
1277 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1278 {
1279 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1280 return;
1281 }
1282
1283 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1284 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001285 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1286 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001287 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001288 write_device_info_flash(info);
1289 }
1290 memcpy(dev, info, sizeof(device_info));
1291}
1292
1293void write_device_info(device_info *dev)
1294{
1295 if(target_is_emmc_boot())
1296 {
1297 write_device_info_mmc(dev);
1298 }
1299 else
1300 {
1301 write_device_info_flash(dev);
1302 }
1303}
1304
1305void read_device_info(device_info *dev)
1306{
1307 if(target_is_emmc_boot())
1308 {
1309 read_device_info_mmc(dev);
1310 }
1311 else
1312 {
1313 read_device_info_flash(dev);
1314 }
1315}
1316
1317void reset_device_info()
1318{
1319 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001320 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001321 write_device_info(&device);
1322}
1323
1324void set_device_root()
1325{
1326 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001327 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001328 write_device_info(&device);
1329}
1330
Amol Jadicb524072012-08-09 16:40:18 -07001331#if DEVICE_TREE
1332int copy_dtb(uint8_t *boot_image_start)
1333{
1334 uint32 dt_image_offset = 0;
1335 uint32_t n;
1336 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001337 struct dt_entry dt_entry;
Amol Jadicb524072012-08-09 16:40:18 -07001338
1339 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1340
Amol Jadicb524072012-08-09 16:40:18 -07001341 if(hdr->dt_size != 0) {
1342
1343 /* add kernel offset */
1344 dt_image_offset += page_size;
1345 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1346 dt_image_offset += n;
1347
1348 /* add ramdisk offset */
1349 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1350 dt_image_offset += n;
1351
1352 /* add second offset */
1353 if(hdr->second_size != 0) {
1354 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1355 dt_image_offset += n;
1356 }
1357
1358 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001359 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001360
Joel Kingaa335dc2013-06-03 16:11:08 -07001361 if (dev_tree_validate(table, hdr->page_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001362 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1363 return -1;
1364 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001365 /* Find index of device tree within device tree table */
1366 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001367 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1368 return -1;
1369 }
1370
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001371 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001372 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001373 {
1374 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1375 return -1;
1376 }
1377
Amol Jadicb524072012-08-09 16:40:18 -07001378 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001379 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001380 boot_image_start + dt_image_offset + dt_entry.offset,
1381 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001382 } else
1383 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001384
1385 /* Everything looks fine. Return success. */
1386 return 0;
1387}
1388#endif
1389
Brian Swetland9c4c0752009-01-25 16:23:50 -08001390void cmd_boot(const char *arg, void *data, unsigned sz)
1391{
1392 unsigned kernel_actual;
1393 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001394 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001395 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001396 int ret = 0;
1397 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001398
1399 if (sz < sizeof(hdr)) {
1400 fastboot_fail("invalid bootimage header");
1401 return;
1402 }
1403
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001404 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001405
1406 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001407 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001408
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001409 if(target_is_emmc_boot() && hdr->page_size) {
1410 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001411 page_mask = page_size - 1;
1412 }
1413
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001414 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1415 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1416
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001417 /*
1418 * Update the kernel/ramdisk/tags address if the boot image header
1419 * has default values, these default values come from mkbootimg when
1420 * the boot image is flashed using fastboot flash:raw
1421 */
1422 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001423
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001424 /* Get virtual addresses since the hdr saves physical addresses. */
1425 hdr->kernel_addr = VA(hdr->kernel_addr);
1426 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1427 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001428
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001429 /* Check if the addresses in the header are valid. */
1430 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1431 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1432 {
1433 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1434 return -1;
1435 }
1436
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001437 /* sz should have atleast raw boot image */
1438 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001439 fastboot_fail("incomplete bootimage");
1440 return;
1441 }
1442
Amol Jadicb524072012-08-09 16:40:18 -07001443#if DEVICE_TREE
1444 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001445 ret = copy_dtb(data);
1446
1447 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001448#else
1449 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1450 {
1451 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1452 return -1;
1453 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001454#endif
1455
1456 /* Load ramdisk & kernel */
1457 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1458 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1459
1460#if DEVICE_TREE
1461 /*
1462 * If dtb is not found look for appended DTB in the kernel.
1463 * If appended dev tree is found, update the atags with
1464 * memory address to the DTB appended location on RAM.
1465 * Else update with the atags address in the kernel header
1466 */
1467 if (!dtb_copied) {
1468 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001469 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1470 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001471 if (!dtb) {
1472 fastboot_fail("dtb not found");
1473 return;
1474 }
Amol Jadicb524072012-08-09 16:40:18 -07001475 }
1476#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001477
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001478#ifndef DEVICE_TREE
1479 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1480 {
1481 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1482 return -1;
1483 }
1484#endif
1485
Brian Swetland9c4c0752009-01-25 16:23:50 -08001486 fastboot_okay("");
1487 udc_stop();
1488
Dima Zavin77e41f32013-03-06 16:10:43 -08001489 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001490 (const char*) hdr->cmdline, board_machtype(),
1491 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001492}
1493
Dima Zavin214cc642009-01-26 11:16:21 -08001494void cmd_erase(const char *arg, void *data, unsigned sz)
1495{
1496 struct ptentry *ptn;
1497 struct ptable *ptable;
1498
1499 ptable = flash_get_ptable();
1500 if (ptable == NULL) {
1501 fastboot_fail("partition table doesn't exist");
1502 return;
1503 }
1504
1505 ptn = ptable_find(ptable, arg);
1506 if (ptn == NULL) {
1507 fastboot_fail("unknown partition name");
1508 return;
1509 }
1510
1511 if (flash_erase(ptn)) {
1512 fastboot_fail("failed to erase partition");
1513 return;
1514 }
1515 fastboot_okay("");
1516}
1517
Bikas Gurungd48bd242010-09-04 19:54:32 -07001518
1519void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1520{
Kun Liang2f1601a2013-08-12 16:29:54 +08001521 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001522 unsigned long long ptn = 0;
Kun Liang2f1601a2013-08-12 16:29:54 +08001523 unsigned long long size;
Kinson Chikf1a43512011-07-14 11:28:39 -07001524 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001525
Kinson Chikf1a43512011-07-14 11:28:39 -07001526 index = partition_get_index(arg);
1527 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001528
Kinson Chikf1a43512011-07-14 11:28:39 -07001529 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001530 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001531 return;
1532 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001533
1534 size = partition_get_size(index);
1535 if (size > DEFAULT_ERASE_SIZE)
1536 size = DEFAULT_ERASE_SIZE;
1537
neetidb4b24d62012-01-20 12:13:09 -08001538 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001539 0 in first several blocks */
1540 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001541 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001542 return;
1543 }
1544 fastboot_okay("");
1545}
1546
1547
Ajay Dudani5c761132011-04-07 20:19:04 -07001548void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001549{
1550 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001551 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001552 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001553
Greg Grisco6e754772011-06-23 12:19:39 -07001554 if (!strcmp(arg, "partition"))
1555 {
1556 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001557 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001558 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001559 return;
1560 }
1561 }
Greg Grisco6e754772011-06-23 12:19:39 -07001562 else
1563 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001564 index = partition_get_index(arg);
1565 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001566 if(ptn == 0) {
1567 fastboot_fail("partition table doesn't exist");
1568 return;
1569 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001570
Greg Grisco6e754772011-06-23 12:19:39 -07001571 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1572 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1573 fastboot_fail("image is not a boot image");
1574 return;
1575 }
1576 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001577
Kinson Chikf1a43512011-07-14 11:28:39 -07001578 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001579 if (ROUND_TO_PAGE(sz,511) > size) {
1580 fastboot_fail("size too large");
1581 return;
1582 }
1583 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1584 fastboot_fail("flash write failure");
1585 return;
1586 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001587 }
1588 fastboot_okay("");
1589 return;
1590}
1591
Ajay Dudani5c761132011-04-07 20:19:04 -07001592void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1593{
1594 unsigned int chunk;
1595 unsigned int chunk_data_sz;
1596 sparse_header_t *sparse_header;
1597 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001598 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001599 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301600 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001601 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001602
Kinson Chikf1a43512011-07-14 11:28:39 -07001603 index = partition_get_index(arg);
1604 ptn = partition_get_offset(index);
1605 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001606 fastboot_fail("partition table doesn't exist");
1607 return;
1608 }
1609
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301610 size = partition_get_size(index);
1611 if (ROUND_TO_PAGE(sz,511) > size) {
1612 fastboot_fail("size too large");
1613 return;
1614 }
1615
Ajay Dudani5c761132011-04-07 20:19:04 -07001616 /* Read and skip over sparse image header */
1617 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001618 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1619 fastboot_fail("size too large");
1620 return;
1621 }
1622
Ajay Dudani5c761132011-04-07 20:19:04 -07001623 data += sparse_header->file_hdr_sz;
1624 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1625 {
1626 /* Skip the remaining bytes in a header that is longer than
1627 * we expected.
1628 */
1629 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1630 }
1631
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001632 dprintf (SPEW, "=== Sparse Image Header ===\n");
1633 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1634 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1635 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1636 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1637 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1638 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1639 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1640 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001641
1642 /* Start processing chunks */
1643 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1644 {
1645 /* Read and skip over chunk header */
1646 chunk_header = (chunk_header_t *) data;
1647 data += sizeof(chunk_header_t);
1648
1649 dprintf (SPEW, "=== Chunk Header ===\n");
1650 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1651 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1652 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1653
1654 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1655 {
1656 /* Skip the remaining bytes in a header that is longer than
1657 * we expected.
1658 */
1659 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1660 }
1661
1662 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1663 switch (chunk_header->chunk_type)
1664 {
1665 case CHUNK_TYPE_RAW:
1666 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1667 chunk_data_sz))
1668 {
1669 fastboot_fail("Bogus chunk size for chunk type Raw");
1670 return;
1671 }
1672
Ajay Dudaniab18f022011-05-12 14:39:22 -07001673 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1674 chunk_data_sz,
1675 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001676 {
1677 fastboot_fail("flash write failure");
1678 return;
1679 }
1680 total_blocks += chunk_header->chunk_sz;
1681 data += chunk_data_sz;
1682 break;
1683
1684 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001685 total_blocks += chunk_header->chunk_sz;
1686 break;
1687
Ajay Dudani5c761132011-04-07 20:19:04 -07001688 case CHUNK_TYPE_CRC:
1689 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1690 {
1691 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1692 return;
1693 }
1694 total_blocks += chunk_header->chunk_sz;
1695 data += chunk_data_sz;
1696 break;
1697
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001698 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001699 fastboot_fail("Unknown chunk type");
1700 return;
1701 }
1702 }
1703
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001704 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1705 total_blocks, sparse_header->total_blks);
1706
1707 if(total_blocks != sparse_header->total_blks)
1708 {
1709 fastboot_fail("sparse image write failure");
1710 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001711
1712 fastboot_okay("");
1713 return;
1714}
1715
1716void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1717{
1718 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001719 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1720 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001721
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001722#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001723 int ret=0;
1724 uint32 major_version=0;
1725 uint32 minor_version=0;
1726
1727 ret = scm_svc_version(&major_version,&minor_version);
1728 if(!ret)
1729 {
1730 if(major_version >= 2)
1731 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001732 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001733 {
1734 ret = encrypt_scm((uint32 **) &data, &sz);
1735 if (ret != 0) {
1736 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1737 return;
1738 }
1739
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001740 /* Protect only for SSD */
1741 if (!strcmp(arg, "ssd")) {
1742 ret = scm_protect_keystore((uint32 *) data, sz);
1743 if (ret != 0) {
1744 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1745 return;
1746 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001747 }
1748 }
1749 else
1750 {
1751 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1752 if(ret != 0)
1753 {
1754 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1755 return;
1756 }
1757 }
1758 }
1759 else
1760 {
1761 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1762 magic_number[1] == DECRYPT_MAGIC_1)
1763 {
1764 ret = decrypt_scm((uint32 **) &data, &sz);
1765 if (ret != 0) {
1766 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1767 return;
1768 }
1769 }
1770 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1771 magic_number[1] == ENCRYPT_MAGIC_1)
1772 {
1773 ret = encrypt_scm((uint32 **) &data, &sz);
1774 if (ret != 0) {
1775 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1776 return;
1777 }
1778 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001779 }
1780 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001781 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001782 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001783 dprintf(CRITICAL,"INVALID SVC Version\n");
1784 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001785 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001786#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001787
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001788 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001789 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1790 cmd_flash_mmc_img(arg, data, sz);
1791 else
1792 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001793 return;
1794}
1795
Dima Zavin214cc642009-01-26 11:16:21 -08001796void cmd_flash(const char *arg, void *data, unsigned sz)
1797{
1798 struct ptentry *ptn;
1799 struct ptable *ptable;
1800 unsigned extra = 0;
1801
1802 ptable = flash_get_ptable();
1803 if (ptable == NULL) {
1804 fastboot_fail("partition table doesn't exist");
1805 return;
1806 }
1807
1808 ptn = ptable_find(ptable, arg);
1809 if (ptn == NULL) {
1810 fastboot_fail("unknown partition name");
1811 return;
1812 }
1813
1814 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1815 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1816 fastboot_fail("image is not a boot image");
1817 return;
1818 }
1819 }
1820
Amol Jadi5c61a952012-05-04 17:05:35 -07001821 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001822 || !strcmp(ptn->name, "userdata")
1823 || !strcmp(ptn->name, "persist")
1824 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301825 if (flash_ecc_bch_enabled())
1826 /* Spare data bytes for 8 bit ECC increased by 4 */
1827 extra = ((page_size >> 9) * 20);
1828 else
1829 extra = ((page_size >> 9) * 16);
1830 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001831 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001832
1833 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1834 if (flash_write(ptn, extra, data, sz)) {
1835 fastboot_fail("flash write failure");
1836 return;
1837 }
1838 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1839 fastboot_okay("");
1840}
1841
1842void cmd_continue(const char *arg, void *data, unsigned sz)
1843{
1844 fastboot_okay("");
1845 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001846 if (target_is_emmc_boot())
1847 {
1848 boot_linux_from_mmc();
1849 }
1850 else
1851 {
1852 boot_linux_from_flash();
1853 }
Dima Zavin214cc642009-01-26 11:16:21 -08001854}
1855
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001856void cmd_reboot(const char *arg, void *data, unsigned sz)
1857{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001858 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001859 fastboot_okay("");
1860 reboot_device(0);
1861}
1862
1863void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1864{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001865 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001866 fastboot_okay("");
1867 reboot_device(FASTBOOT_MODE);
1868}
1869
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001870void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
1871{
1872 dprintf(INFO, "Enabling charger screen check\n");
1873 device.charger_screen_enabled = 1;
1874 write_device_info(&device);
1875 fastboot_okay("");
1876}
1877
1878void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
1879{
1880 dprintf(INFO, "Disabling charger screen check\n");
1881 device.charger_screen_enabled = 0;
1882 write_device_info(&device);
1883 fastboot_okay("");
1884}
1885
Shashank Mittal162244e2011-08-08 19:01:25 -07001886void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1887{
1888 if(!device.is_unlocked)
1889 {
1890 device.is_unlocked = 1;
1891 write_device_info(&device);
1892 }
1893 fastboot_okay("");
1894}
1895
Shashank Mittala0032282011-08-26 14:50:11 -07001896void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1897{
1898 char response[64];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001899 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07001900 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001901 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1902 fastboot_info(response);
1903 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07001904 fastboot_info(response);
1905 fastboot_okay("");
1906}
1907
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001908void cmd_preflash(const char *arg, void *data, unsigned sz)
1909{
1910 fastboot_okay("");
1911}
1912
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001913void splash_screen ()
1914{
1915 struct ptentry *ptn;
1916 struct ptable *ptable;
1917 struct fbcon_config *fb_display = NULL;
1918
1919 if (!target_is_emmc_boot())
1920 {
1921 ptable = flash_get_ptable();
1922 if (ptable == NULL) {
1923 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001924 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001925 }
1926
1927 ptn = ptable_find(ptable, "splash");
1928 if (ptn == NULL) {
1929 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1930 } else {
1931 fb_display = fbcon_display();
1932 if (fb_display) {
1933 if (flash_read(ptn, 0, fb_display->base,
1934 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1935 fbcon_clear();
1936 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1937 }
1938 }
1939 }
1940 }
1941}
1942
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001943/* Get the size from partiton name */
1944static void get_partition_size(const char *arg, char *response)
1945{
1946 uint64_t ptn = 0;
1947 uint64_t size;
1948 int index = INVALID_PTN;
1949
1950 index = partition_get_index(arg);
1951
1952 if (index == INVALID_PTN)
1953 {
1954 dprintf(CRITICAL, "Invalid partition index\n");
1955 return;
1956 }
1957
1958 ptn = partition_get_offset(index);
1959
1960 if(!ptn)
1961 {
1962 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1963 return;
1964 }
1965
1966 size = partition_get_size(index);
1967
1968 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1969 return;
1970}
1971
1972/*
1973 * Publish the partition type & size info
1974 * fastboot getvar will publish the required information.
1975 * fastboot getvar partition_size:<partition_name>: partition size in hex
1976 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1977 */
1978static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1979{
1980 uint8_t i;
1981
1982 for (i = 0; i < num_parts; i++) {
1983 get_partition_size(info[i].part_name, info[i].size_response);
1984
1985 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1986 {
1987 dprintf(CRITICAL, "partition size name truncated\n");
1988 return;
1989 }
1990 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1991 {
1992 dprintf(CRITICAL, "partition type name truncated\n");
1993 return;
1994 }
1995
1996 /* publish partition size & type info */
1997 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1998 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1999 }
2000}
2001
Amol Jadi5edf3552013-07-23 14:15:34 -07002002/* register commands and variables for fastboot */
2003void aboot_fastboot_register_commands(void)
2004{
2005 if (target_is_emmc_boot())
2006 {
2007 fastboot_register("flash:", cmd_flash_mmc);
2008 fastboot_register("erase:", cmd_erase_mmc);
2009 }
2010 else
2011 {
2012 fastboot_register("flash:", cmd_flash);
2013 fastboot_register("erase:", cmd_erase);
2014 }
2015
2016 fastboot_register("boot", cmd_boot);
2017 fastboot_register("continue", cmd_continue);
2018 fastboot_register("reboot", cmd_reboot);
2019 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2020 fastboot_register("oem unlock", cmd_oem_unlock);
2021 fastboot_register("oem device-info", cmd_oem_devinfo);
2022 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002023 fastboot_register("oem enable-charger-screen",
2024 cmd_oem_enable_charger_screen);
2025 fastboot_register("oem disable-charger-screen",
2026 cmd_oem_disable_charger_screen);
Amol Jadi5edf3552013-07-23 14:15:34 -07002027 /* publish variables and their values */
2028 fastboot_publish("product", TARGET(BOARD));
2029 fastboot_publish("kernel", "lk");
2030 fastboot_publish("serialno", sn_buf);
2031
2032 /*
2033 * partition info is supported only for emmc partitions
2034 * Calling this for NAND prints some error messages which
2035 * is harmless but misleading. Avoid calling this for NAND
2036 * devices.
2037 */
2038 if (target_is_emmc_boot())
2039 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2040
2041 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002042 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2043 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002044 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002045 /* Is the charger screen check enabled */
2046 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2047 device.charger_screen_enabled);
2048 fastboot_publish("charger-screen-enabled",
2049 (const char *) charger_screen_enabled);
Amol Jadi5edf3552013-07-23 14:15:34 -07002050}
2051
Brian Swetland9c4c0752009-01-25 16:23:50 -08002052void aboot_init(const struct app_descriptor *app)
2053{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002054 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002055 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002056
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002057 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002058 if (target_is_emmc_boot())
2059 {
2060 page_size = 2048;
2061 page_mask = page_size - 1;
2062 }
2063 else
2064 {
2065 page_size = flash_page_size();
2066 page_mask = page_size - 1;
2067 }
2068
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002069 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2070
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002071 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002072
Greg Griscod6250552011-06-29 14:40:23 -07002073 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002074 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002075
Dhaval Patel223ec952013-07-18 14:49:44 -07002076 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2077
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002078 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002079 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002080 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002081 dprintf(ALWAYS,"dload mode key sequence detected\n");
2082 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002083 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002084 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002085 }
2086 else
2087 {
2088 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002089 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002090 }
2091 boot_into_fastboot = true;
2092 }
2093 if (!boot_into_fastboot)
2094 {
2095 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2096 boot_into_recovery = 1;
2097 if (!boot_into_recovery &&
2098 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002099 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002100 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002101 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002102 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002103 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002104 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002105
Ajay Dudani77421292010-10-27 19:34:06 -07002106 reboot_mode = check_reboot_mode();
2107 if (reboot_mode == RECOVERY_MODE) {
2108 boot_into_recovery = 1;
2109 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002110 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002111 }
2112
Pavel Nedev5d91d412013-04-29 11:34:24 +03002113 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002114 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002115 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002116 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002117 if(emmc_recovery_init())
2118 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2119 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002120 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002121 if((device.is_unlocked) || (device.is_tampered))
2122 {
2123 #ifdef TZ_TAMPER_FUSE
2124 set_tamper_fuse_cmd();
2125 #endif
2126 #if USE_PCOM_SECBOOT
2127 set_tamper_flag(device.is_tampered);
2128 #endif
2129 }
Shashank Mittala0032282011-08-26 14:50:11 -07002130 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002131 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002132 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002133 else
2134 {
2135 recovery_init();
2136 #if USE_PCOM_SECBOOT
2137 if((device.is_unlocked) || (device.is_tampered))
2138 set_tamper_flag(device.is_tampered);
2139 #endif
2140 boot_linux_from_flash();
2141 }
2142 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2143 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002144 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002145
Amol Jadi5edf3552013-07-23 14:15:34 -07002146 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002147
Amol Jadi5edf3552013-07-23 14:15:34 -07002148 /* register aboot specific fastboot commands */
2149 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002150
Amol Jadi5edf3552013-07-23 14:15:34 -07002151 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002152 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002153
2154 /* initialize and start fastboot */
2155 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002156}
2157
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002158uint32_t get_page_size()
2159{
2160 return page_size;
2161}
2162
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002163/*
2164 * Calculated and save hash (SHA256) for non-signed boot image.
2165 *
2166 * Hash the same data that is checked on the signed boot image.
2167 * Kernel and Ramdisk are already read to memory buffers.
2168 * Need to read the entire device-tree from mmc
2169 * since non-signed image only read the DT tags of the relevant platform.
2170 *
2171 * @param kernel_addr - kernel bufer
2172 * @param kernel_actual - kernel size in bytes
2173 * @param ramdisk_addr - ramdisk buffer
2174 * @param ramdisk_actual - ramdisk size
2175 * @param ptn - partition
2176 * @param dt_offset - device tree offset on mmc partition
2177 * @param dt_size
2178 *
2179 * @return int - 0 on success, negative value on failure.
2180 */
2181int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2182 void *ramdisk_addr, unsigned ramdisk_actual,
2183 unsigned long long ptn,
2184 unsigned dt_offset, unsigned dt_size)
2185{
2186 SHA256_CTX sha256_ctx;
2187 char digest[32]={0};
2188 char *buf = (char *)target_get_scratch_address();
2189 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2190 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2191
2192 SHA256_Init(&sha256_ctx);
2193
2194 /* Read Boot Header */
2195 if (mmc_read(ptn, buf, page_size))
2196 {
2197 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2198 return -1;
2199 }
2200 /* Read entire Device Tree */
2201 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2202 {
2203 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2204 return -1;
2205 }
2206 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2207 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2208 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2209 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2210
2211 SHA256_Final(digest, &sha256_ctx);
2212
2213 save_kernel_hash_cmd(digest);
2214 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2215
2216 return 0;
2217}
2218
Brian Swetland9c4c0752009-01-25 16:23:50 -08002219APP_START(aboot)
2220 .init = aboot_init,
2221APP_END