blob: 5c1cfc030e5643e013a2ff2a72637b37f0bf7eec [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Kuogee Hsiehfb4b5c32013-12-20 08:34:10 -08005 * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabice737c82014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
43#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080044#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080045#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080046#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070047#include <target.h>
48#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070049#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070050#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070051#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070052#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070053#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030054#include <sha.h>
Dima Zavin214cc642009-01-26 11:16:21 -080055
Neeti Desai17379b82012-06-04 18:42:53 -070056#if DEVICE_TREE
57#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070058#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070059#endif
60
Shashank Mittalcd98d472011-08-02 14:29:24 -070061#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080062#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080063#include "bootimg.h"
64#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070065#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070066#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070067#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070068#include "board.h"
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
Sridhar Parasuram901bb702014-10-24 14:06:03 -070078/* fastboot command function pointer */
79typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
80
81struct fastboot_cmd_desc {
82 char * name;
83 fastboot_cmd_fn cb;
84};
85
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070086#define EXPAND(NAME) #NAME
87#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070088
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080089#ifdef MEMBASE
90#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
91#else
David Ng183a7422009-12-07 14:55:21 -080092#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080093#endif
94
Deepa Dinamani0e163a42013-05-24 17:08:15 -070095#ifndef MEMSIZE
96#define MEMSIZE 1024*1024
97#endif
98
99#define MAX_TAGS_SIZE 1024
100
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800101#define RECOVERY_MODE 0x77665502
102#define FASTBOOT_MODE 0x77665500
103
Kun Liang2f1601a2013-08-12 16:29:54 +0800104/* make 4096 as default size to ensure EFS,EXT4's erasing */
105#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsiehfb4b5c32013-12-20 08:34:10 -0800106#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +0800107
Channagoud Kadabice737c82014-06-18 17:41:01 -0700108#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
109
David Ng183a7422009-12-07 14:55:21 -0800110static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800111static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300112static const char *androidboot_mode = " androidboot.mode=";
Dhaval Patel223ec952013-07-18 14:49:44 -0700113static const char *display_cmdline = " mdss_mdp.panel=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800114static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800115static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700116static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300117static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800118
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800119static const char *baseband_apq = " androidboot.baseband=apq";
120static const char *baseband_msm = " androidboot.baseband=msm";
121static const char *baseband_csfb = " androidboot.baseband=csfb";
122static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700123static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700124static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800125static const char *baseband_dsda = " androidboot.baseband=dsda";
126static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800127static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800128
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700129static unsigned page_size = 0;
130static unsigned page_mask = 0;
131static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
132static bool boot_into_ffbm;
133
Shashank Mittalcd98d472011-08-02 14:29:24 -0700134/* Assuming unauthorized kernel image by default */
135static int auth_kernel_img = 0;
136
Unnati Gandhi15d08f92014-01-24 11:01:01 +0530137static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700138
Dima Zavin42168f22009-01-30 11:52:22 -0800139struct atag_ptbl_entry
140{
141 char name[16];
142 unsigned offset;
143 unsigned size;
144 unsigned flags;
145};
146
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700147/*
148 * Partition info, required to be published
149 * for fastboot
150 */
151struct getvar_partition_info {
152 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
153 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
154 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
155 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
156 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
157};
158
159/*
160 * Right now, we are publishing the info for only
161 * three partitions
162 */
163struct getvar_partition_info part_info[] =
164{
165 { "system" , "partition-size:", "partition-type:", "", "ext4" },
166 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
167 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
168};
169
170char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700171char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800172char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700173char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi15d08f92014-01-24 11:01:01 +0530174char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700175
Greg Griscod2471ef2011-07-14 13:00:42 -0700176extern int emmc_recovery_init(void);
177
Kinson Chik0b1c8162011-08-31 16:31:57 -0700178#if NO_KEYPAD_DRIVER
179extern int fastboot_trigger(void);
180#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700181
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700182static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
183{
184 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700185#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
186 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
187 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
188 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700189#endif
190}
191
Dima Zavin42168f22009-01-30 11:52:22 -0800192static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
193{
194 struct atag_ptbl_entry atag_ptn;
195
196 memcpy(atag_ptn.name, ptn->name, 16);
197 atag_ptn.name[15] = '\0';
198 atag_ptn.offset = ptn->start;
199 atag_ptn.size = ptn->length;
200 atag_ptn.flags = ptn->flags;
201 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
202 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
203}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800204
Neeti Desaie245d492012-06-01 12:52:13 -0700205unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800206{
David Ng183a7422009-12-07 14:55:21 -0800207 int cmdline_len = 0;
208 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800209 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700210 int pause_at_bootup = 0;
Pavel Nedev5614d222013-06-17 18:01:02 +0300211 bool gpt_exists = partition_gpt_exists();
Dima Zavin42168f22009-01-30 11:52:22 -0800212
Brian Swetland9c4c0752009-01-25 16:23:50 -0800213 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800214 cmdline_len = strlen(cmdline);
215 have_cmdline = 1;
216 }
217 if (target_is_emmc_boot()) {
218 cmdline_len += strlen(emmc_cmdline);
219 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800220
221 cmdline_len += strlen(usb_sn_cmdline);
222 cmdline_len += strlen(sn_buf);
223
Pavel Nedev5614d222013-06-17 18:01:02 +0300224 if (boot_into_recovery && gpt_exists)
225 cmdline_len += strlen(secondary_gpt_enable);
226
Pavel Nedev328ac822013-04-05 15:25:11 +0300227 if (boot_into_ffbm) {
228 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700229 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800230 /* reduce kernel console messages to speed-up boot */
231 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700232 } else if (device.charger_screen_enabled &&
233 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700234 pause_at_bootup = 1;
235 cmdline_len += strlen(battchg_pause);
236 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800237
Shashank Mittalcd98d472011-08-02 14:29:24 -0700238 if(target_use_signed_kernel() && auth_kernel_img) {
239 cmdline_len += strlen(auth_kernel);
240 }
241
Ajay Dudanid04110c2011-01-17 23:55:07 -0800242 /* Determine correct androidboot.baseband to use */
243 switch(target_baseband())
244 {
245 case BASEBAND_APQ:
246 cmdline_len += strlen(baseband_apq);
247 break;
248
249 case BASEBAND_MSM:
250 cmdline_len += strlen(baseband_msm);
251 break;
252
253 case BASEBAND_CSFB:
254 cmdline_len += strlen(baseband_csfb);
255 break;
256
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800257 case BASEBAND_SVLTE2A:
258 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800259 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700260
261 case BASEBAND_MDM:
262 cmdline_len += strlen(baseband_mdm);
263 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700264
265 case BASEBAND_SGLTE:
266 cmdline_len += strlen(baseband_sglte);
267 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530268
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800269 case BASEBAND_SGLTE2:
270 cmdline_len += strlen(baseband_sglte2);
271 break;
272
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530273 case BASEBAND_DSDA:
274 cmdline_len += strlen(baseband_dsda);
275 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800276
277 case BASEBAND_DSDA2:
278 cmdline_len += strlen(baseband_dsda2);
279 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800280 }
281
Dhaval Patel223ec952013-07-18 14:49:44 -0700282 if (target_display_panel_node(display_panel_buf, MAX_PANEL_BUF_SIZE) &&
283 strlen(display_panel_buf))
284 {
285 cmdline_len += strlen(display_cmdline);
286 cmdline_len += strlen(display_panel_buf);
287 }
288
David Ng183a7422009-12-07 14:55:21 -0800289 if (cmdline_len > 0) {
290 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700291 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
292 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700293
Amol Jadi168b7712012-03-06 16:15:00 -0800294 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700295 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800296 if (have_cmdline) {
297 src = cmdline;
298 while ((*dst++ = *src++));
299 }
300 if (target_is_emmc_boot()) {
301 src = emmc_cmdline;
302 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700303 have_cmdline = 1;
304 while ((*dst++ = *src++));
305 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800306
307 src = usb_sn_cmdline;
308 if (have_cmdline) --dst;
309 have_cmdline = 1;
310 while ((*dst++ = *src++));
311 src = sn_buf;
312 if (have_cmdline) --dst;
313 have_cmdline = 1;
314 while ((*dst++ = *src++));
315
Pavel Nedev5614d222013-06-17 18:01:02 +0300316 if (boot_into_recovery && gpt_exists) {
317 src = secondary_gpt_enable;
318 if (have_cmdline) --dst;
319 while ((*dst++ = *src++));
320 }
321
Pavel Nedev328ac822013-04-05 15:25:11 +0300322 if (boot_into_ffbm) {
323 src = androidboot_mode;
324 if (have_cmdline) --dst;
325 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700326 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300327 if (have_cmdline) --dst;
328 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800329 src = loglevel;
330 if (have_cmdline) --dst;
331 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300332 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700333 src = battchg_pause;
334 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800335 while ((*dst++ = *src++));
336 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800337
Shashank Mittalcd98d472011-08-02 14:29:24 -0700338 if(target_use_signed_kernel() && auth_kernel_img) {
339 src = auth_kernel;
340 if (have_cmdline) --dst;
341 while ((*dst++ = *src++));
342 }
343
Ajay Dudanid04110c2011-01-17 23:55:07 -0800344 switch(target_baseband())
345 {
346 case BASEBAND_APQ:
347 src = baseband_apq;
348 if (have_cmdline) --dst;
349 while ((*dst++ = *src++));
350 break;
351
352 case BASEBAND_MSM:
353 src = baseband_msm;
354 if (have_cmdline) --dst;
355 while ((*dst++ = *src++));
356 break;
357
358 case BASEBAND_CSFB:
359 src = baseband_csfb;
360 if (have_cmdline) --dst;
361 while ((*dst++ = *src++));
362 break;
363
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800364 case BASEBAND_SVLTE2A:
365 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800366 if (have_cmdline) --dst;
367 while ((*dst++ = *src++));
368 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700369
370 case BASEBAND_MDM:
371 src = baseband_mdm;
372 if (have_cmdline) --dst;
373 while ((*dst++ = *src++));
374 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700375
376 case BASEBAND_SGLTE:
377 src = baseband_sglte;
378 if (have_cmdline) --dst;
379 while ((*dst++ = *src++));
380 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530381
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800382 case BASEBAND_SGLTE2:
383 src = baseband_sglte2;
384 if (have_cmdline) --dst;
385 while ((*dst++ = *src++));
386 break;
387
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530388 case BASEBAND_DSDA:
389 src = baseband_dsda;
390 if (have_cmdline) --dst;
391 while ((*dst++ = *src++));
392 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800393
394 case BASEBAND_DSDA2:
395 src = baseband_dsda2;
396 if (have_cmdline) --dst;
397 while ((*dst++ = *src++));
398 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800399 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700400
401 if (strlen(display_panel_buf)) {
402 src = display_cmdline;
403 if (have_cmdline) --dst;
404 while ((*dst++ = *src++));
405 src = display_panel_buf;
406 if (have_cmdline) --dst;
407 while ((*dst++ = *src++));
408 }
Neeti Desaie245d492012-06-01 12:52:13 -0700409 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700410
411
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800412 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700413 return cmdline_final;
414}
415
416unsigned *atag_core(unsigned *ptr)
417{
418 /* CORE */
419 *ptr++ = 2;
420 *ptr++ = 0x54410001;
421
422 return ptr;
423
424}
425
426unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
427 unsigned ramdisk_size)
428{
429 if (ramdisk_size) {
430 *ptr++ = 4;
431 *ptr++ = 0x54420005;
432 *ptr++ = (unsigned)ramdisk;
433 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800434 }
435
Neeti Desaie245d492012-06-01 12:52:13 -0700436 return ptr;
437}
438
439unsigned *atag_ptable(unsigned **ptr_addr)
440{
441 int i;
442 struct ptable *ptable;
443
444 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700445 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
446 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700447 *(*ptr_addr)++ = 0x4d534d70;
448 for (i = 0; i < ptable->count; ++i)
449 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
450 }
451
452 return (*ptr_addr);
453}
454
455unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
456{
457 int cmdline_length = 0;
458 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700459 char *dest;
460
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800461 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700462 n = (cmdline_length + 4) & (~3);
463
464 *ptr++ = (n / 4) + 2;
465 *ptr++ = 0x54410009;
466 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800467 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700468 ptr += (n / 4);
469
470 return ptr;
471}
472
473unsigned *atag_end(unsigned *ptr)
474{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800475 /* END */
476 *ptr++ = 0;
477 *ptr++ = 0;
478
Neeti Desaie245d492012-06-01 12:52:13 -0700479 return ptr;
480}
481
482void generate_atags(unsigned *ptr, const char *cmdline,
483 void *ramdisk, unsigned ramdisk_size)
484{
485
486 ptr = atag_core(ptr);
487 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
488 ptr = target_atag_mem(ptr);
489
490 /* Skip NAND partition ATAGS for eMMC boot */
491 if (!target_is_emmc_boot()){
492 ptr = atag_ptable(&ptr);
493 }
494
495 ptr = atag_cmdline(ptr, cmdline);
496 ptr = atag_end(ptr);
497}
498
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700499typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700500void boot_linux(void *kernel, unsigned *tags,
501 const char *cmdline, unsigned machtype,
502 void *ramdisk, unsigned ramdisk_size)
503{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800504 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800505#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700506 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800507#endif
508
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700509 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800510 uint32_t tags_phys = PA((addr_t)tags);
511
512 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700513
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800514 final_cmdline = update_cmdline((const char*)cmdline);
515
Neeti Desai17379b82012-06-04 18:42:53 -0700516#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800517 dprintf(INFO, "Updating device tree: start\n");
518
Neeti Desai17379b82012-06-04 18:42:53 -0700519 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800520 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700521 if(ret)
522 {
523 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
524 ASSERT(0);
525 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800526 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700527#else
Neeti Desaie245d492012-06-01 12:52:13 -0700528 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800529 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700530#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700531
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700532 /* Perform target specific cleanup */
533 target_uninit();
534
Aravind Venkateswarandb0d5262014-02-25 16:25:30 -0800535 /* Turn off splash screen if enabled */
536#if DISPLAY_SPLASH_SCREEN
537 target_display_shutdown();
538#endif
539
540
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800541 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
542 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800543
544 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700545
Amol Jadi4421e652011-06-16 15:00:48 -0700546 /* do any platform specific cleanup before kernel entry */
547 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700548
Brian Swetland9c4c0752009-01-25 16:23:50 -0800549 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700550
Amol Jadi504f9fe2012-08-16 13:56:48 -0700551#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800552 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700553#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700554 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700555 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800556}
557
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700558/* Function to check if the memory address range falls within the aboot
559 * boundaries.
560 * start: Start of the memory region
561 * size: Size of the memory region
562 */
563int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
564{
565 /* Check for boundary conditions. */
Sundarajan Srinivasan74252482013-12-16 17:02:56 -0800566 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700567 return -1;
568
569 /* Check for memory overlap. */
570 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
571 return 0;
Channagoud Kadabib4413fd2013-10-15 12:53:52 -0700572 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700573 return 0;
574 else
575 return -1;
576}
577
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800578#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800579
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700580BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800581#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700582BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800583#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800584
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700585static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
586{
587 int ret;
588
589 /* Assume device is rooted at this time. */
590 device.is_tampered = 1;
591
592 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
593
594 ret = image_verify((unsigned char *)bootimg_addr,
595 (unsigned char *)(bootimg_addr + bootimg_size),
596 bootimg_size,
597 CRYPTO_AUTH_ALG_SHA256);
598
599 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
600
601 if (ret)
602 {
603 /* Authorized kernel */
604 device.is_tampered = 0;
605 }
606
607#if USE_PCOM_SECBOOT
608 set_tamper_flag(device.is_tampered);
609#endif
610
611 if(device.is_tampered)
612 {
613 write_device_info_mmc(&device);
614 #ifdef TZ_TAMPER_FUSE
615 set_tamper_fuse_cmd();
616 #endif
617 #ifdef ASSERT_ON_TAMPER
618 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
619 ASSERT(0);
620 #endif
621 }
622}
623
Maunik Shah78a85e52014-03-02 17:47:58 +0530624static bool check_format_bit()
625{
626 bool ret = false;
627 int index;
628 uint64_t offset;
629 struct boot_selection_info *in = NULL;
630 char *buf = NULL;
631
632 index = partition_get_index("bootselect");
633 if (index == INVALID_PTN)
634 {
635 dprintf(INFO, "Unable to locate /bootselect partition\n");
636 return ret;
637 }
638 offset = partition_get_offset(index);
639 if(!offset)
640 {
641 dprintf(INFO, "partition /bootselect doesn't exist\n");
642 return ret;
643 }
644 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
645 ASSERT(buf);
646 if (mmc_read(offset, (unsigned int *)buf, page_size))
647 {
648 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
649 free(buf);
650 return ret;
651 }
652 in = (struct boot_selection_info *) buf;
653 if ((in->signature == BOOTSELECT_SIGNATURE) &&
654 (in->version == BOOTSELECT_VERSION)) {
655 if ((in->state_info & BOOTSELECT_FORMAT) &&
656 !(in->state_info & BOOTSELECT_FACTORY))
657 ret = true;
658 } else {
659 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
660 in->signature, in->version);
661 ASSERT(0);
662 }
663 free(buf);
664 return ret;
665}
666
Shashank Mittal23b8f422010-04-16 19:27:21 -0700667int boot_linux_from_mmc(void)
668{
669 struct boot_img_hdr *hdr = (void*) buf;
670 struct boot_img_hdr *uhdr;
671 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700672 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700673 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700674 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700675
Shashank Mittalcd98d472011-08-02 14:29:24 -0700676 unsigned char *image_addr = 0;
677 unsigned kernel_actual;
678 unsigned ramdisk_actual;
679 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700680 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700681
682#if DEVICE_TREE
683 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700684 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700685 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800686 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700687 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700688#endif
Maunik Shah78a85e52014-03-02 17:47:58 +0530689 if (check_format_bit())
690 boot_into_recovery = 1;
691
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700692 if (!boot_into_recovery) {
693 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
694 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
695 if (rcode <= 0) {
696 boot_into_ffbm = false;
697 if (rcode < 0)
698 dprintf(CRITICAL,"failed to get ffbm cookie");
699 } else
700 boot_into_ffbm = true;
701 } else
702 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700703 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
704 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
705 dprintf(INFO, "Unified boot method!\n");
706 hdr = uhdr;
707 goto unified_boot;
708 }
Greg Griscod6250552011-06-29 14:40:23 -0700709 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700710 index = partition_get_index("boot");
711 ptn = partition_get_offset(index);
712 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700713 dprintf(CRITICAL, "ERROR: No boot partition found\n");
714 return -1;
715 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700716 }
717 else {
718 index = partition_get_index("recovery");
719 ptn = partition_get_offset(index);
720 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700721 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
722 return -1;
723 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700724 }
725
Greg Griscod6250552011-06-29 14:40:23 -0700726 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700727 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
728 return -1;
729 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700730
731 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700732 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700733 return -1;
734 }
735
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700736 if (hdr->page_size && (hdr->page_size != page_size)) {
737 page_size = hdr->page_size;
738 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700739 }
740
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700741 /*
742 * Update the kernel/ramdisk/tags address if the boot image header
743 * has default values, these default values come from mkbootimg when
744 * the boot image is flashed using fastboot flash:raw
745 */
746 update_ker_tags_rdisk_addr(hdr);
747
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700748 /* Get virtual addresses since the hdr saves physical addresses. */
749 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
750 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
751 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700752
753 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
754 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
755
756 /* Check if the addresses in the header are valid. */
757 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
758 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
759 {
760 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
761 return -1;
762 }
763
764#ifndef DEVICE_TREE
765 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
766 {
767 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
768 return -1;
769 }
770#endif
771
Shashank Mittalcd98d472011-08-02 14:29:24 -0700772 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300773 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
774 (int) target_use_signed_kernel(),
775 device.is_unlocked,
776 device.is_tampered);
777
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700778 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800779 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700780 offset = 0;
781
Shashank Mittalcd98d472011-08-02 14:29:24 -0700782 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700783
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800784#if DEVICE_TREE
785 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
786 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700787
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700788 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700789 {
790 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
791 return -1;
792 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800793#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530794 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700795
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700796#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700797
Amol Jadib6be5c12012-11-14 13:39:51 -0800798 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700799 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800800
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700801 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
802 {
803 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
804 return -1;
805 }
806
Shashank Mittalcd98d472011-08-02 14:29:24 -0700807 /* Read image without signature */
808 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
809 {
810 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
811 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800812 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700813
Amol Jadib6be5c12012-11-14 13:39:51 -0800814 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700815 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800816
Shashank Mittalcd98d472011-08-02 14:29:24 -0700817 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700818
819 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
820 {
821 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
822 return -1;
823 }
824
Shashank Mittalcd98d472011-08-02 14:29:24 -0700825 /* Read signature */
826 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
827 {
828 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700829 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700830 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800831
wangxl6145a502015-04-10 09:58:03 +0800832 // verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700833
Neeti Desai465491e2012-07-31 12:53:35 -0700834 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700835 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
836 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700837
Neeti Desai465491e2012-07-31 12:53:35 -0700838 #if DEVICE_TREE
839 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700840 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700841 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700842
Deepa Dinamani19648b42013-09-05 17:05:55 -0700843 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700844 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
845 return -1;
846 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700847
Neeti Desai465491e2012-07-31 12:53:35 -0700848 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700849 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700850 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
851 return -1;
852 }
853
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700854 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700855 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700856 {
857 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
858 return -1;
859 }
860
Joel Kingaa335dc2013-06-03 16:11:08 -0700861 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800862 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800863 /*
864 * If appended dev tree is found, update the atags with
865 * memory address to the DTB appended location on RAM.
866 * Else update with the atags address in the kernel header
867 */
868 void *dtb;
869 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700870 hdr->kernel_size,
871 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800872 if (!dtb) {
873 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
874 return -1;
875 }
Neeti Desai465491e2012-07-31 12:53:35 -0700876 }
877 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700878 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700879 else
880 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800881 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700882
Amol Jadib6be5c12012-11-14 13:39:51 -0800883 dprintf(INFO, "Loading boot image (%d): start\n",
884 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700885 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800886
887 offset = page_size;
888
889 /* Load kernel */
890 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700891 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
892 return -1;
893 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800894 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700895
Amol Jadib6be5c12012-11-14 13:39:51 -0800896 /* Load ramdisk */
897 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700898 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800899 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700900 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
901 return -1;
902 }
903 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800904 offset += ramdisk_actual;
905
906 dprintf(INFO, "Loading boot image (%d): done\n",
907 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700908 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700909
910 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800911 offset += second_actual;
912 /* Second image loading not implemented. */
913 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700914 }
915
916 #if DEVICE_TREE
917 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -0700918 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -0700919 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
920 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
921 return -1;
922 }
923 table = (struct dt_table*) dt_buf;
924
Deepa Dinamani19648b42013-09-05 17:05:55 -0700925 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700926 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
927 return -1;
928 }
929
Deepa Dinamani19648b42013-09-05 17:05:55 -0700930 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
931 if (!table)
932 return -1;
933
934 /* Read the entire device tree table into buffer */
935 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
936 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
937 return -1;
938 }
939
Joel Kingaa335dc2013-06-03 16:11:08 -0700940 /* Find index of device tree within device tree table */
941 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700942 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
943 return -1;
944 }
945
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700946 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700947 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700948 {
949 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
950 return -1;
951 }
952
Joel Kingaa335dc2013-06-03 16:11:08 -0700953 if(mmc_read(ptn + offset + dt_entry.offset,
954 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700955 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
956 return -1;
957 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300958 #ifdef TZ_SAVE_KERNEL_HASH
959 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
960 hdr->ramdisk_addr, ramdisk_actual,
961 ptn, offset, hdr->dt_size);
962 #endif /* TZ_SAVE_KERNEL_HASH */
963
Channagoud Kadabi35095622013-03-01 13:53:05 -0800964 } else {
Aparna Mallavarapuf5432e42014-06-03 22:15:54 +0530965
966 /* Validate the tags_addr */
967 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
968 {
969 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
970 return -1;
971 }
Dima Zavin77e41f32013-03-06 16:10:43 -0800972 /*
973 * If appended dev tree is found, update the atags with
974 * memory address to the DTB appended location on RAM.
975 * Else update with the atags address in the kernel header
976 */
977 void *dtb;
978 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700979 kernel_actual,
980 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800981 if (!dtb) {
982 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
983 return -1;
984 }
Neeti Desai465491e2012-07-31 12:53:35 -0700985 }
986 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700987 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700988
Stanimir Varbanov69ec5462013-07-18 18:17:42 +0300989 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
990 target_load_ssd_keystore();
991
Shashank Mittal23b8f422010-04-16 19:27:21 -0700992unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700993
Dima Zavin77e41f32013-03-06 16:10:43 -0800994 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700995 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700996 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
997
998 return 0;
999}
1000
Dima Zavin214cc642009-01-26 11:16:21 -08001001int boot_linux_from_flash(void)
1002{
1003 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001004 struct ptentry *ptn;
1005 struct ptable *ptable;
1006 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001007
Shashank Mittalcd98d472011-08-02 14:29:24 -07001008 unsigned char *image_addr = 0;
1009 unsigned kernel_actual;
1010 unsigned ramdisk_actual;
1011 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001012 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001013
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001014#if DEVICE_TREE
1015 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001016 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001017 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001018 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001019#endif
1020
David Ng183a7422009-12-07 14:55:21 -08001021 if (target_is_emmc_boot()) {
1022 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1023 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1024 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1025 return -1;
1026 }
1027 goto continue_boot;
1028 }
1029
Dima Zavin214cc642009-01-26 11:16:21 -08001030 ptable = flash_get_ptable();
1031 if (ptable == NULL) {
1032 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1033 return -1;
1034 }
1035
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001036 if(!boot_into_recovery)
1037 {
1038 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001039
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001040 if (ptn == NULL) {
1041 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1042 return -1;
1043 }
1044 }
1045 else
1046 {
1047 ptn = ptable_find(ptable, "recovery");
1048 if (ptn == NULL) {
1049 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1050 return -1;
1051 }
Dima Zavin214cc642009-01-26 11:16:21 -08001052 }
1053
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001054 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001055 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1056 return -1;
1057 }
Dima Zavin214cc642009-01-26 11:16:21 -08001058
1059 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001060 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001061 return -1;
1062 }
1063
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001064 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001065 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 -08001066 return -1;
1067 }
1068
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001069 /*
1070 * Update the kernel/ramdisk/tags address if the boot image header
1071 * has default values, these default values come from mkbootimg when
1072 * the boot image is flashed using fastboot flash:raw
1073 */
1074 update_ker_tags_rdisk_addr(hdr);
1075
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001076 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001077 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1078 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1079 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1080
1081 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1082 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1083
1084 /* Check if the addresses in the header are valid. */
1085 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1086 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1087 {
1088 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1089 return -1;
1090 }
1091
1092#ifndef DEVICE_TREE
1093 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1094 {
1095 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1096 return -1;
1097 }
1098#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001099
Shashank Mittalcd98d472011-08-02 14:29:24 -07001100 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001101 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001102 {
1103 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001104 offset = 0;
1105
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001106#if DEVICE_TREE
1107 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1108 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001109
1110 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1111 {
1112 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1113 return -1;
1114 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001115#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001116 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001117#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001118
Amol Jadib6be5c12012-11-14 13:39:51 -08001119 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001120 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001121
Shashank Mittalcd98d472011-08-02 14:29:24 -07001122 /* Read image without signature */
1123 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1124 {
1125 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1126 return -1;
1127 }
Dima Zavin214cc642009-01-26 11:16:21 -08001128
Amol Jadib6be5c12012-11-14 13:39:51 -08001129 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001130 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001131
Shashank Mittalcd98d472011-08-02 14:29:24 -07001132 offset = imagesize_actual;
1133 /* Read signature */
1134 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1135 {
1136 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001137 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001138 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001139
wangxl6145a502015-04-10 09:58:03 +08001140 //verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001141
1142 /* Move kernel and ramdisk to correct address */
1143 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1144 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001145#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001146 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001147 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001148 {
1149 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1150 return -1;
1151 }
1152
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001153 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1154#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001155
1156 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001157 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001158 {
1159 write_device_info_flash(&device);
1160 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301161#if USE_PCOM_SECBOOT
1162 set_tamper_flag(device.is_tampered);
1163#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001164 }
1165 else
1166 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001167 offset = page_size;
1168
Amol Jadib6be5c12012-11-14 13:39:51 -08001169 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1170 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1171 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1172
1173 dprintf(INFO, "Loading boot image (%d): start\n",
1174 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001175 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001176
1177 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001178 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1179 return -1;
1180 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001181 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001182
Amol Jadib6be5c12012-11-14 13:39:51 -08001183 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001184 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1185 return -1;
1186 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001187 offset += ramdisk_actual;
1188
1189 dprintf(INFO, "Loading boot image (%d): done\n",
1190 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001191 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001192
1193 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001194 offset += second_actual;
1195 /* Second image loading not implemented. */
1196 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001197 }
1198
1199#if DEVICE_TREE
1200 if(hdr->dt_size != 0) {
1201
1202 /* Read the device tree table into buffer */
1203 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1204 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1205 return -1;
1206 }
1207
1208 table = (struct dt_table*) dt_buf;
1209
Deepa Dinamani19648b42013-09-05 17:05:55 -07001210 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001211 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1212 return -1;
1213 }
1214
Deepa Dinamani19648b42013-09-05 17:05:55 -07001215 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1216 if (!table)
1217 return -1;
1218
1219 /* Read the entire device tree table into buffer */
1220 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1221 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1222 return -1;
1223 }
1224
1225
Joel Kingaa335dc2013-06-03 16:11:08 -07001226 /* Find index of device tree within device tree table */
1227 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001228 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1229 return -1;
1230 }
1231
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001232 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001233 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001234 {
1235 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1236 return -1;
1237 }
1238
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001239 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001240 if(flash_read(ptn, offset + dt_entry.offset,
1241 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001242 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1243 return -1;
1244 }
1245 }
1246#endif
1247
Shashank Mittalcd98d472011-08-02 14:29:24 -07001248 }
David Ng183a7422009-12-07 14:55:21 -08001249continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001250
Dima Zavin214cc642009-01-26 11:16:21 -08001251 /* TODO: create/pass atags to kernel */
1252
Ajay Dudanie28a6072011-07-01 13:59:46 -07001253 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001254 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001255 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1256
1257 return 0;
1258}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001259
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001260BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001261void write_device_info_mmc(device_info *dev)
1262{
1263 struct device_info *info = (void*) info_buf;
1264 unsigned long long ptn = 0;
1265 unsigned long long size;
1266 int index = INVALID_PTN;
1267
1268 index = partition_get_index("aboot");
1269 ptn = partition_get_offset(index);
1270 if(ptn == 0)
1271 {
1272 return;
1273 }
1274
1275 size = partition_get_size(index);
1276
1277 memcpy(info, dev, sizeof(device_info));
1278
1279 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1280 {
1281 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1282 return;
1283 }
1284}
1285
1286void read_device_info_mmc(device_info *dev)
1287{
1288 struct device_info *info = (void*) info_buf;
1289 unsigned long long ptn = 0;
1290 unsigned long long size;
1291 int index = INVALID_PTN;
1292
1293 index = partition_get_index("aboot");
1294 ptn = partition_get_offset(index);
1295 if(ptn == 0)
1296 {
1297 return;
1298 }
1299
1300 size = partition_get_size(index);
1301
1302 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1303 {
1304 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1305 return;
1306 }
1307
1308 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1309 {
1310 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1311 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001312 info->is_tampered = 0;
Liu Yuec78d6d12015-09-28 09:59:51 +08001313 info->charger_screen_enabled = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001314
1315 write_device_info_mmc(info);
1316 }
1317 memcpy(dev, info, sizeof(device_info));
1318}
1319
1320void write_device_info_flash(device_info *dev)
1321{
1322 struct device_info *info = (void *) info_buf;
1323 struct ptentry *ptn;
1324 struct ptable *ptable;
1325
1326 ptable = flash_get_ptable();
1327 if (ptable == NULL)
1328 {
1329 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1330 return;
1331 }
1332
1333 ptn = ptable_find(ptable, "devinfo");
1334 if (ptn == NULL)
1335 {
1336 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1337 return;
1338 }
1339
1340 memcpy(info, dev, sizeof(device_info));
1341
1342 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1343 {
1344 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1345 return;
1346 }
1347}
1348
1349void read_device_info_flash(device_info *dev)
1350{
1351 struct device_info *info = (void*) info_buf;
1352 struct ptentry *ptn;
1353 struct ptable *ptable;
1354
1355 ptable = flash_get_ptable();
1356 if (ptable == NULL)
1357 {
1358 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1359 return;
1360 }
1361
1362 ptn = ptable_find(ptable, "devinfo");
1363 if (ptn == NULL)
1364 {
1365 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1366 return;
1367 }
1368
1369 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1370 {
1371 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1372 return;
1373 }
1374
1375 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1376 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001377 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1378 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001379 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001380 write_device_info_flash(info);
1381 }
1382 memcpy(dev, info, sizeof(device_info));
1383}
1384
1385void write_device_info(device_info *dev)
1386{
1387 if(target_is_emmc_boot())
1388 {
1389 write_device_info_mmc(dev);
1390 }
1391 else
1392 {
1393 write_device_info_flash(dev);
1394 }
1395}
1396
1397void read_device_info(device_info *dev)
1398{
1399 if(target_is_emmc_boot())
1400 {
1401 read_device_info_mmc(dev);
1402 }
1403 else
1404 {
1405 read_device_info_flash(dev);
1406 }
1407}
1408
1409void reset_device_info()
1410{
1411 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001412 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001413 write_device_info(&device);
1414}
1415
1416void set_device_root()
1417{
1418 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001419 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001420 write_device_info(&device);
1421}
1422
Amol Jadicb524072012-08-09 16:40:18 -07001423#if DEVICE_TREE
1424int copy_dtb(uint8_t *boot_image_start)
1425{
1426 uint32 dt_image_offset = 0;
1427 uint32_t n;
1428 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001429 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001430 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001431
1432 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1433
Amol Jadicb524072012-08-09 16:40:18 -07001434 if(hdr->dt_size != 0) {
1435
1436 /* add kernel offset */
1437 dt_image_offset += page_size;
1438 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1439 dt_image_offset += n;
1440
1441 /* add ramdisk offset */
1442 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1443 dt_image_offset += n;
1444
1445 /* add second offset */
1446 if(hdr->second_size != 0) {
1447 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1448 dt_image_offset += n;
1449 }
1450
1451 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001452 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001453
Deepa Dinamani19648b42013-09-05 17:05:55 -07001454 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001455 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1456 return -1;
1457 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001458 /* Find index of device tree within device tree table */
1459 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001460 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1461 return -1;
1462 }
1463
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001464 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001465 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001466 {
1467 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1468 return -1;
1469 }
1470
Amol Jadicb524072012-08-09 16:40:18 -07001471 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001472 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001473 boot_image_start + dt_image_offset + dt_entry.offset,
1474 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001475 } else
1476 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001477
1478 /* Everything looks fine. Return success. */
1479 return 0;
1480}
1481#endif
1482
Brian Swetland9c4c0752009-01-25 16:23:50 -08001483void cmd_boot(const char *arg, void *data, unsigned sz)
1484{
1485 unsigned kernel_actual;
1486 unsigned ramdisk_actual;
Channagoud Kadabice737c82014-06-18 17:41:01 -07001487 uint32_t image_actual;
1488 uint32_t dt_actual = 0;
Channagoud Kadabi87c2dd62014-06-26 14:26:39 -07001489 uint32_t sig_actual = SIGNATURE_SIZE;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001490 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001491 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001492 int ret = 0;
1493 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001494
1495 if (sz < sizeof(hdr)) {
1496 fastboot_fail("invalid bootimage header");
1497 return;
1498 }
1499
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001500 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001501
1502 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001503 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001504
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001505 if(target_is_emmc_boot() && hdr->page_size) {
1506 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001507 page_mask = page_size - 1;
1508 }
1509
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001510 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1511 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabice737c82014-06-18 17:41:01 -07001512#if DEVICE_TREE
1513 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1514#endif
1515
1516 image_actual = ADD_OF(page_size, kernel_actual);
1517 image_actual = ADD_OF(image_actual, ramdisk_actual);
1518 image_actual = ADD_OF(image_actual, dt_actual);
1519
Channagoud Kadabi87c2dd62014-06-26 14:26:39 -07001520 if (target_use_signed_kernel() && (!device.is_unlocked))
1521 image_actual = ADD_OF(image_actual, sig_actual);
1522
Channagoud Kadabice737c82014-06-18 17:41:01 -07001523 /* sz should have atleast raw boot image */
1524 if (image_actual > sz) {
Channagoud Kadabi87c2dd62014-06-26 14:26:39 -07001525 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabice737c82014-06-18 17:41:01 -07001526 return;
1527 }
1528
1529 /* Verify the boot image
1530 * device & page_size are initialized in aboot_init
1531 */
1532 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi87c2dd62014-06-26 14:26:39 -07001533 /* Pass size excluding signature size, otherwise we would try to
1534 * access signature beyond its length
1535 */
wangxl6145a502015-04-10 09:58:03 +08001536 //verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001537
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001538 /*
1539 * Update the kernel/ramdisk/tags address if the boot image header
1540 * has default values, these default values come from mkbootimg when
1541 * the boot image is flashed using fastboot flash:raw
1542 */
1543 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001544
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001545 /* Get virtual addresses since the hdr saves physical addresses. */
1546 hdr->kernel_addr = VA(hdr->kernel_addr);
1547 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1548 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001549
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001550 /* Check if the addresses in the header are valid. */
1551 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1552 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1553 {
1554 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabia54102a2013-12-03 11:19:20 -08001555 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001556 }
1557
Amol Jadicb524072012-08-09 16:40:18 -07001558#if DEVICE_TREE
1559 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001560 ret = copy_dtb(data);
1561
1562 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001563#else
1564 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1565 {
1566 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabia54102a2013-12-03 11:19:20 -08001567 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001568 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001569#endif
1570
1571 /* Load ramdisk & kernel */
1572 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1573 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1574
1575#if DEVICE_TREE
1576 /*
1577 * If dtb is not found look for appended DTB in the kernel.
1578 * If appended dev tree is found, update the atags with
1579 * memory address to the DTB appended location on RAM.
1580 * Else update with the atags address in the kernel header
1581 */
1582 if (!dtb_copied) {
1583 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001584 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1585 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001586 if (!dtb) {
1587 fastboot_fail("dtb not found");
1588 return;
1589 }
Amol Jadicb524072012-08-09 16:40:18 -07001590 }
1591#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001592
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001593#ifndef DEVICE_TREE
1594 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1595 {
1596 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabia54102a2013-12-03 11:19:20 -08001597 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001598 }
1599#endif
1600
Brian Swetland9c4c0752009-01-25 16:23:50 -08001601 fastboot_okay("");
1602 udc_stop();
1603
Dima Zavin77e41f32013-03-06 16:10:43 -08001604 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001605 (const char*) hdr->cmdline, board_machtype(),
1606 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001607}
1608
Sridhar Parasuram901bb702014-10-24 14:06:03 -07001609void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08001610{
1611 struct ptentry *ptn;
1612 struct ptable *ptable;
1613
1614 ptable = flash_get_ptable();
1615 if (ptable == NULL) {
1616 fastboot_fail("partition table doesn't exist");
1617 return;
1618 }
1619
1620 ptn = ptable_find(ptable, arg);
1621 if (ptn == NULL) {
1622 fastboot_fail("unknown partition name");
1623 return;
1624 }
1625
1626 if (flash_erase(ptn)) {
1627 fastboot_fail("failed to erase partition");
1628 return;
1629 }
1630 fastboot_okay("");
1631}
1632
Bikas Gurungd48bd242010-09-04 19:54:32 -07001633
1634void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1635{
Kun Liang2f1601a2013-08-12 16:29:54 +08001636 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001637 unsigned long long ptn = 0;
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001638 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001639 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001640
Kinson Chikf1a43512011-07-14 11:28:39 -07001641 index = partition_get_index(arg);
1642 ptn = partition_get_offset(index);
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001643 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001644
Kinson Chikf1a43512011-07-14 11:28:39 -07001645 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001646 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001647 return;
1648 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001649
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001650#if MMC_SDHCI_SUPPORT
1651 if (mmc_erase_card(ptn, size)) {
1652 fastboot_fail("failed to erase partition\n");
1653 return;
1654 }
1655#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001656 size = partition_get_size(index);
1657 if (size > DEFAULT_ERASE_SIZE)
1658 size = DEFAULT_ERASE_SIZE;
1659
neetidb4b24d62012-01-20 12:13:09 -08001660 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001661 0 in first several blocks */
1662 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001663 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001664 return;
1665 }
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001666#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001667 fastboot_okay("");
1668}
1669
Sridhar Parasuram901bb702014-10-24 14:06:03 -07001670void cmd_erase(const char *arg, void *data, unsigned sz)
1671{
1672 if(target_is_emmc_boot())
1673 cmd_erase_mmc(arg, data, sz);
1674 else
1675 cmd_erase_nand(arg, data, sz);
1676}
Bikas Gurungd48bd242010-09-04 19:54:32 -07001677
Ajay Dudani5c761132011-04-07 20:19:04 -07001678void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001679{
1680 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001681 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001682 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001683
Greg Grisco6e754772011-06-23 12:19:39 -07001684 if (!strcmp(arg, "partition"))
1685 {
1686 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001687 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001688 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001689 return;
1690 }
1691 }
Greg Grisco6e754772011-06-23 12:19:39 -07001692 else
1693 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001694 index = partition_get_index(arg);
1695 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001696 if(ptn == 0) {
1697 fastboot_fail("partition table doesn't exist");
1698 return;
1699 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001700
Greg Grisco6e754772011-06-23 12:19:39 -07001701 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1702 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1703 fastboot_fail("image is not a boot image");
1704 return;
1705 }
1706 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001707
Kinson Chikf1a43512011-07-14 11:28:39 -07001708 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001709 if (ROUND_TO_PAGE(sz,511) > size) {
1710 fastboot_fail("size too large");
1711 return;
1712 }
1713 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1714 fastboot_fail("flash write failure");
1715 return;
1716 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001717 }
1718 fastboot_okay("");
1719 return;
1720}
1721
Ajay Dudani5c761132011-04-07 20:19:04 -07001722void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1723{
1724 unsigned int chunk;
1725 unsigned int chunk_data_sz;
1726 sparse_header_t *sparse_header;
1727 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001728 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001729 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301730 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001731 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001732
Kinson Chikf1a43512011-07-14 11:28:39 -07001733 index = partition_get_index(arg);
1734 ptn = partition_get_offset(index);
1735 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001736 fastboot_fail("partition table doesn't exist");
1737 return;
1738 }
1739
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301740 size = partition_get_size(index);
1741 if (ROUND_TO_PAGE(sz,511) > size) {
1742 fastboot_fail("size too large");
1743 return;
1744 }
1745
Ajay Dudani5c761132011-04-07 20:19:04 -07001746 /* Read and skip over sparse image header */
1747 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001748 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1749 fastboot_fail("size too large");
1750 return;
1751 }
1752
Ajay Dudani5c761132011-04-07 20:19:04 -07001753 data += sparse_header->file_hdr_sz;
1754 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1755 {
1756 /* Skip the remaining bytes in a header that is longer than
1757 * we expected.
1758 */
1759 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1760 }
1761
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001762 dprintf (SPEW, "=== Sparse Image Header ===\n");
1763 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1764 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1765 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1766 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1767 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1768 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1769 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1770 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001771
1772 /* Start processing chunks */
1773 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1774 {
1775 /* Read and skip over chunk header */
1776 chunk_header = (chunk_header_t *) data;
1777 data += sizeof(chunk_header_t);
1778
1779 dprintf (SPEW, "=== Chunk Header ===\n");
1780 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1781 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1782 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1783
1784 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1785 {
1786 /* Skip the remaining bytes in a header that is longer than
1787 * we expected.
1788 */
1789 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1790 }
1791
1792 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1793 switch (chunk_header->chunk_type)
1794 {
1795 case CHUNK_TYPE_RAW:
1796 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1797 chunk_data_sz))
1798 {
1799 fastboot_fail("Bogus chunk size for chunk type Raw");
1800 return;
1801 }
1802
Ajay Dudaniab18f022011-05-12 14:39:22 -07001803 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1804 chunk_data_sz,
1805 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001806 {
1807 fastboot_fail("flash write failure");
1808 return;
1809 }
1810 total_blocks += chunk_header->chunk_sz;
1811 data += chunk_data_sz;
1812 break;
1813
1814 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001815 total_blocks += chunk_header->chunk_sz;
1816 break;
1817
Ajay Dudani5c761132011-04-07 20:19:04 -07001818 case CHUNK_TYPE_CRC:
1819 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1820 {
1821 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1822 return;
1823 }
1824 total_blocks += chunk_header->chunk_sz;
1825 data += chunk_data_sz;
1826 break;
1827
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001828 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001829 fastboot_fail("Unknown chunk type");
1830 return;
1831 }
1832 }
1833
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001834 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1835 total_blocks, sparse_header->total_blks);
1836
1837 if(total_blocks != sparse_header->total_blks)
1838 {
1839 fastboot_fail("sparse image write failure");
1840 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001841
1842 fastboot_okay("");
1843 return;
1844}
1845
1846void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1847{
1848 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001849 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1850 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001851
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001852#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001853 int ret=0;
1854 uint32 major_version=0;
1855 uint32 minor_version=0;
1856
1857 ret = scm_svc_version(&major_version,&minor_version);
1858 if(!ret)
1859 {
1860 if(major_version >= 2)
1861 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001862 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001863 {
1864 ret = encrypt_scm((uint32 **) &data, &sz);
1865 if (ret != 0) {
1866 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1867 return;
1868 }
1869
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001870 /* Protect only for SSD */
1871 if (!strcmp(arg, "ssd")) {
1872 ret = scm_protect_keystore((uint32 *) data, sz);
1873 if (ret != 0) {
1874 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1875 return;
1876 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001877 }
1878 }
1879 else
1880 {
1881 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1882 if(ret != 0)
1883 {
1884 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1885 return;
1886 }
1887 }
1888 }
1889 else
1890 {
1891 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1892 magic_number[1] == DECRYPT_MAGIC_1)
1893 {
1894 ret = decrypt_scm((uint32 **) &data, &sz);
1895 if (ret != 0) {
1896 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1897 return;
1898 }
1899 }
1900 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1901 magic_number[1] == ENCRYPT_MAGIC_1)
1902 {
1903 ret = encrypt_scm((uint32 **) &data, &sz);
1904 if (ret != 0) {
1905 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1906 return;
1907 }
1908 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001909 }
1910 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001911 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001912 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001913 dprintf(CRITICAL,"INVALID SVC Version\n");
1914 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001915 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001916#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001917
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001918 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001919 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1920 cmd_flash_mmc_img(arg, data, sz);
1921 else
1922 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001923 return;
1924}
1925
Sridhar Parasuram901bb702014-10-24 14:06:03 -07001926void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08001927{
1928 struct ptentry *ptn;
1929 struct ptable *ptable;
1930 unsigned extra = 0;
1931
1932 ptable = flash_get_ptable();
1933 if (ptable == NULL) {
1934 fastboot_fail("partition table doesn't exist");
1935 return;
1936 }
1937
1938 ptn = ptable_find(ptable, arg);
1939 if (ptn == NULL) {
1940 fastboot_fail("unknown partition name");
1941 return;
1942 }
1943
1944 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1945 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1946 fastboot_fail("image is not a boot image");
1947 return;
1948 }
1949 }
1950
Amol Jadi5c61a952012-05-04 17:05:35 -07001951 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001952 || !strcmp(ptn->name, "userdata")
1953 || !strcmp(ptn->name, "persist")
1954 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301955 if (flash_ecc_bch_enabled())
1956 /* Spare data bytes for 8 bit ECC increased by 4 */
1957 extra = ((page_size >> 9) * 20);
1958 else
1959 extra = ((page_size >> 9) * 16);
1960 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001961 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001962
1963 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1964 if (flash_write(ptn, extra, data, sz)) {
1965 fastboot_fail("flash write failure");
1966 return;
1967 }
1968 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1969 fastboot_okay("");
1970}
1971
Sridhar Parasuram901bb702014-10-24 14:06:03 -07001972void cmd_flash(const char *arg, void *data, unsigned sz)
1973{
1974 if(target_is_emmc_boot())
1975 cmd_flash_mmc(arg, data, sz);
1976 else
1977 cmd_flash_nand(arg, data, sz);
1978}
1979
Dima Zavin214cc642009-01-26 11:16:21 -08001980void cmd_continue(const char *arg, void *data, unsigned sz)
1981{
1982 fastboot_okay("");
1983 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001984 if (target_is_emmc_boot())
1985 {
1986 boot_linux_from_mmc();
1987 }
1988 else
1989 {
1990 boot_linux_from_flash();
1991 }
Dima Zavin214cc642009-01-26 11:16:21 -08001992}
1993
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001994void cmd_reboot(const char *arg, void *data, unsigned sz)
1995{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001996 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001997 fastboot_okay("");
1998 reboot_device(0);
1999}
2000
2001void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
2002{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002003 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08002004 fastboot_okay("");
2005 reboot_device(FASTBOOT_MODE);
2006}
2007
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002008void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
2009{
2010 dprintf(INFO, "Enabling charger screen check\n");
2011 device.charger_screen_enabled = 1;
2012 write_device_info(&device);
2013 fastboot_okay("");
2014}
2015
2016void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
2017{
2018 dprintf(INFO, "Disabling charger screen check\n");
2019 device.charger_screen_enabled = 0;
2020 write_device_info(&device);
2021 fastboot_okay("");
2022}
2023
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302024void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2025{
2026 dprintf(INFO, "Selecting display panel %s\n", arg);
2027 if (arg)
2028 strlcpy(device.display_panel, arg,
2029 sizeof(device.display_panel));
2030 write_device_info(&device);
2031 fastboot_okay("");
2032}
2033
Shashank Mittal162244e2011-08-08 19:01:25 -07002034void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2035{
2036 if(!device.is_unlocked)
2037 {
2038 device.is_unlocked = 1;
2039 write_device_info(&device);
2040 }
2041 fastboot_okay("");
2042}
2043
Shashank Mittala0032282011-08-26 14:50:11 -07002044void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2045{
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302046 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002047 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002048 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002049 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2050 fastboot_info(response);
2051 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002052 fastboot_info(response);
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302053 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2054 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002055 fastboot_okay("");
2056}
2057
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002058void cmd_preflash(const char *arg, void *data, unsigned sz)
2059{
2060 fastboot_okay("");
2061}
2062
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302063static struct fbimage logo_header = {0};
2064struct fbimage* splash_screen_flash();
2065
2066int splash_screen_check_header(struct fbimage *logo)
2067{
2068 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2069 return -1;
2070 if (logo->header.width == 0 || logo->header.height == 0)
2071 return -1;
2072 return 0;
2073}
2074
2075struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002076{
2077 struct ptentry *ptn;
2078 struct ptable *ptable;
2079 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302080 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002081
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302082
2083 ptable = flash_get_ptable();
2084 if (ptable == NULL) {
2085 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2086 return NULL;
2087 }
2088 ptn = ptable_find(ptable, "splash");
2089 if (ptn == NULL) {
2090 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2091 return NULL;
2092 }
2093
2094 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2095 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2096 return NULL;
2097 }
2098
2099 if (splash_screen_check_header(logo)) {
2100 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2101 return NULL;
2102 }
2103
2104 fb_display = fbcon_display();
2105 if (fb_display) {
Vineet Bajajb42103d2014-09-09 12:29:46 +05302106 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2107 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back to default logo\n");
2108 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002109 }
Vineet Bajajb42103d2014-09-09 12:29:46 +05302110 uint8_t *base = (uint8_t *) fb_display->base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302111 if (flash_read(ptn + sizeof(logo->header), 0,
2112 base,
2113 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2114 fbcon_clear();
Vineet Bajajb42103d2014-09-09 12:29:46 +05302115 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302116 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002117 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302118 logo->image = base;
2119 }
2120
2121 return logo;
2122}
2123
2124struct fbimage* splash_screen_mmc()
2125{
2126 int index = INVALID_PTN;
2127 unsigned long long ptn = 0;
2128 struct fbcon_config *fb_display = NULL;
2129 struct fbimage *logo = &logo_header;
2130
2131 index = partition_get_index("splash");
2132 if (index == 0) {
2133 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2134 return NULL;
2135 }
2136
2137 ptn = partition_get_offset(index);
2138 if (ptn == 0) {
2139 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2140 return NULL;
2141 }
2142
2143 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2144 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2145 return NULL;
2146 }
2147
2148 if (splash_screen_check_header(logo)) {
2149 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2150 return NULL;
2151 }
2152
2153 fb_display = fbcon_display();
2154 if (fb_display) {
Vineet Bajajb42103d2014-09-09 12:29:46 +05302155 if ((logo->header.width != fb_display->width) || (logo->header.height != fb_display->height)) {
2156 dprintf(CRITICAL, "Logo config doesn't match with fb config. Fall back default logo\n");
2157 return NULL;
2158 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302159 uint8_t *base = (uint8_t *) fb_display->base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302160 if (mmc_read(ptn + sizeof(logo->header),
2161 base,
2162 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2163 fbcon_clear();
Vineet Bajajb42103d2014-09-09 12:29:46 +05302164 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302165 return NULL;
2166 }
2167
2168 logo->image = base;
2169 }
2170
2171 return logo;
2172}
2173
2174
2175struct fbimage* fetch_image_from_partition()
2176{
2177 if (target_is_emmc_boot()) {
2178 return splash_screen_mmc();
2179 } else {
2180 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002181 }
2182}
2183
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002184/* Get the size from partiton name */
2185static void get_partition_size(const char *arg, char *response)
2186{
2187 uint64_t ptn = 0;
2188 uint64_t size;
2189 int index = INVALID_PTN;
2190
2191 index = partition_get_index(arg);
2192
2193 if (index == INVALID_PTN)
2194 {
2195 dprintf(CRITICAL, "Invalid partition index\n");
2196 return;
2197 }
2198
2199 ptn = partition_get_offset(index);
2200
2201 if(!ptn)
2202 {
2203 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2204 return;
2205 }
2206
2207 size = partition_get_size(index);
2208
2209 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2210 return;
2211}
2212
2213/*
2214 * Publish the partition type & size info
2215 * fastboot getvar will publish the required information.
2216 * fastboot getvar partition_size:<partition_name>: partition size in hex
2217 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2218 */
2219static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2220{
2221 uint8_t i;
2222
2223 for (i = 0; i < num_parts; i++) {
2224 get_partition_size(info[i].part_name, info[i].size_response);
2225
2226 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2227 {
2228 dprintf(CRITICAL, "partition size name truncated\n");
2229 return;
2230 }
2231 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2232 {
2233 dprintf(CRITICAL, "partition type name truncated\n");
2234 return;
2235 }
2236
2237 /* publish partition size & type info */
2238 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2239 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2240 }
2241}
2242
Amol Jadi5edf3552013-07-23 14:15:34 -07002243/* register commands and variables for fastboot */
2244void aboot_fastboot_register_commands(void)
2245{
Sridhar Parasuram901bb702014-10-24 14:06:03 -07002246 int i;
Amol Jadi5edf3552013-07-23 14:15:34 -07002247
Sridhar Parasuram901bb702014-10-24 14:06:03 -07002248 struct fastboot_cmd_desc cmd_list[] = {
2249 /* By default the enabled list is empty. */
2250 {"", NULL},
2251 /* move commands enclosed within the below ifndef to here
2252 * if they need to be enabled in user build.
2253 */
2254#ifndef DISABLE_FASTBOOT_CMDS
2255 /* Register the following commands only for non-user builds */
2256 {"flash:", cmd_flash},
2257 {"erase:", cmd_erase},
2258 {"boot", cmd_boot},
2259 {"continue", cmd_continue},
2260 {"reboot", cmd_reboot},
2261 {"reboot-bootloader", cmd_reboot_bootloader},
2262 {"oem unlock", cmd_oem_unlock},
2263 {"oem device-info", cmd_oem_devinfo},
2264 {"preflash", cmd_preflash},
2265 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
2266 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
2267 {"oem select-display-panel", cmd_oem_select_display_panel},
2268#endif
2269 };
2270
2271 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
2272 for (i = 1; i < fastboot_cmds_count; i++)
2273 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
2274
Amol Jadi5edf3552013-07-23 14:15:34 -07002275 /* publish variables and their values */
2276 fastboot_publish("product", TARGET(BOARD));
2277 fastboot_publish("kernel", "lk");
2278 fastboot_publish("serialno", sn_buf);
2279
2280 /*
2281 * partition info is supported only for emmc partitions
2282 * Calling this for NAND prints some error messages which
2283 * is harmless but misleading. Avoid calling this for NAND
2284 * devices.
2285 */
2286 if (target_is_emmc_boot())
2287 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2288
2289 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002290 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2291 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002292 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002293 /* Is the charger screen check enabled */
2294 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2295 device.charger_screen_enabled);
2296 fastboot_publish("charger-screen-enabled",
2297 (const char *) charger_screen_enabled);
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302298 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2299 device.display_panel);
2300 fastboot_publish("display-panel",
2301 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002302}
2303
Brian Swetland9c4c0752009-01-25 16:23:50 -08002304void aboot_init(const struct app_descriptor *app)
2305{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002306 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002307 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002308
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002309 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002310 if (target_is_emmc_boot())
2311 {
2312 page_size = 2048;
2313 page_mask = page_size - 1;
2314 }
2315 else
2316 {
2317 page_size = flash_page_size();
2318 page_mask = page_size - 1;
2319 }
2320
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002321 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2322
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002323 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002324
Aravind Venkateswarandb0d5262014-02-25 16:25:30 -08002325 /* Display splash screen if enabled */
2326#if DISPLAY_SPLASH_SCREEN
2327 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302328 target_display_init(device.display_panel);
Aravind Venkateswarandb0d5262014-02-25 16:25:30 -08002329 dprintf(SPEW, "Display Init: Done\n");
2330#endif
2331
2332
Greg Griscod6250552011-06-29 14:40:23 -07002333 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002334 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002335
Dhaval Patel223ec952013-07-18 14:49:44 -07002336 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2337
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002338 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002339 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002340 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002341 dprintf(ALWAYS,"dload mode key sequence detected\n");
2342 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002343 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002344 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002345 }
2346 else
2347 {
2348 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002349 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002350 }
2351 boot_into_fastboot = true;
2352 }
2353 if (!boot_into_fastboot)
2354 {
2355 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2356 boot_into_recovery = 1;
2357 if (!boot_into_recovery &&
2358 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002359 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002360 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002361 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002362 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002363 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002364 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002365
Ajay Dudani77421292010-10-27 19:34:06 -07002366 reboot_mode = check_reboot_mode();
2367 if (reboot_mode == RECOVERY_MODE) {
2368 boot_into_recovery = 1;
2369 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002370 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002371 }
2372
Pavel Nedev5d91d412013-04-29 11:34:24 +03002373 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002374 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002375 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002376 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002377 if(emmc_recovery_init())
2378 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2379 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002380 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002381 if((device.is_unlocked) || (device.is_tampered))
2382 {
2383 #ifdef TZ_TAMPER_FUSE
2384 set_tamper_fuse_cmd();
2385 #endif
2386 #if USE_PCOM_SECBOOT
2387 set_tamper_flag(device.is_tampered);
2388 #endif
2389 }
Shashank Mittala0032282011-08-26 14:50:11 -07002390 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002391 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002392 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002393 else
2394 {
2395 recovery_init();
2396 #if USE_PCOM_SECBOOT
2397 if((device.is_unlocked) || (device.is_tampered))
2398 set_tamper_flag(device.is_tampered);
2399 #endif
2400 boot_linux_from_flash();
2401 }
2402 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2403 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002404 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002405
Amol Jadi5edf3552013-07-23 14:15:34 -07002406 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002407
Amol Jadi5edf3552013-07-23 14:15:34 -07002408 /* register aboot specific fastboot commands */
2409 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002410
Amol Jadi5edf3552013-07-23 14:15:34 -07002411 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002412 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002413
2414 /* initialize and start fastboot */
2415 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002416}
2417
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002418uint32_t get_page_size()
2419{
2420 return page_size;
2421}
2422
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002423/*
2424 * Calculated and save hash (SHA256) for non-signed boot image.
2425 *
2426 * Hash the same data that is checked on the signed boot image.
2427 * Kernel and Ramdisk are already read to memory buffers.
2428 * Need to read the entire device-tree from mmc
2429 * since non-signed image only read the DT tags of the relevant platform.
2430 *
2431 * @param kernel_addr - kernel bufer
2432 * @param kernel_actual - kernel size in bytes
2433 * @param ramdisk_addr - ramdisk buffer
2434 * @param ramdisk_actual - ramdisk size
2435 * @param ptn - partition
2436 * @param dt_offset - device tree offset on mmc partition
2437 * @param dt_size
2438 *
2439 * @return int - 0 on success, negative value on failure.
2440 */
2441int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2442 void *ramdisk_addr, unsigned ramdisk_actual,
2443 unsigned long long ptn,
2444 unsigned dt_offset, unsigned dt_size)
2445{
2446 SHA256_CTX sha256_ctx;
2447 char digest[32]={0};
2448 char *buf = (char *)target_get_scratch_address();
2449 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2450 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2451
2452 SHA256_Init(&sha256_ctx);
2453
2454 /* Read Boot Header */
2455 if (mmc_read(ptn, buf, page_size))
2456 {
2457 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2458 return -1;
2459 }
2460 /* Read entire Device Tree */
2461 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2462 {
2463 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2464 return -1;
2465 }
2466 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2467 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2468 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2469 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2470
2471 SHA256_Final(digest, &sha256_ctx);
2472
2473 save_kernel_hash_cmd(digest);
2474 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2475
2476 return 0;
2477}
2478
Brian Swetland9c4c0752009-01-25 16:23:50 -08002479APP_START(aboot)
2480 .init = aboot_init,
2481APP_END