blob: 41c05eff8f0d093dca1f0f12f2c447268163c822 [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
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070078#define EXPAND(NAME) #NAME
79#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070080
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080081#ifdef MEMBASE
82#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
83#else
David Ng183a7422009-12-07 14:55:21 -080084#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080085#endif
86
Deepa Dinamani0e163a42013-05-24 17:08:15 -070087#ifndef MEMSIZE
88#define MEMSIZE 1024*1024
89#endif
90
91#define MAX_TAGS_SIZE 1024
92
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080093#define RECOVERY_MODE 0x77665502
94#define FASTBOOT_MODE 0x77665500
95
Kun Liang2f1601a2013-08-12 16:29:54 +080096/* make 4096 as default size to ensure EFS,EXT4's erasing */
97#define DEFAULT_ERASE_SIZE 4096
Kuogee Hsiehfb4b5c32013-12-20 08:34:10 -080098#define MAX_PANEL_BUF_SIZE 128
Kun Liang2f1601a2013-08-12 16:29:54 +080099
Channagoud Kadabice737c82014-06-18 17:41:01 -0700100#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
101
David Ng183a7422009-12-07 14:55:21 -0800102static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800103static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300104static const char *androidboot_mode = " androidboot.mode=";
Dhaval Patel223ec952013-07-18 14:49:44 -0700105static const char *display_cmdline = " mdss_mdp.panel=";
Pavel Nedev898298c2013-02-27 12:36:09 -0800106static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800107static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700108static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300109static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800110
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800111static const char *baseband_apq = " androidboot.baseband=apq";
112static const char *baseband_msm = " androidboot.baseband=msm";
113static const char *baseband_csfb = " androidboot.baseband=csfb";
114static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700115static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700116static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800117static const char *baseband_dsda = " androidboot.baseband=dsda";
118static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800119static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800120
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700121static unsigned page_size = 0;
122static unsigned page_mask = 0;
123static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
124static bool boot_into_ffbm;
125
Shashank Mittalcd98d472011-08-02 14:29:24 -0700126/* Assuming unauthorized kernel image by default */
127static int auth_kernel_img = 0;
128
Unnati Gandhi15d08f92014-01-24 11:01:01 +0530129static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700130
Dima Zavin42168f22009-01-30 11:52:22 -0800131struct atag_ptbl_entry
132{
133 char name[16];
134 unsigned offset;
135 unsigned size;
136 unsigned flags;
137};
138
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700139/*
140 * Partition info, required to be published
141 * for fastboot
142 */
143struct getvar_partition_info {
144 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
145 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
146 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
147 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
148 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
149};
150
151/*
152 * Right now, we are publishing the info for only
153 * three partitions
154 */
155struct getvar_partition_info part_info[] =
156{
157 { "system" , "partition-size:", "partition-type:", "", "ext4" },
158 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
159 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
160};
161
162char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700163char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800164char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700165char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi15d08f92014-01-24 11:01:01 +0530166char panel_display_mode[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700167
Greg Griscod2471ef2011-07-14 13:00:42 -0700168extern int emmc_recovery_init(void);
169
Kinson Chik0b1c8162011-08-31 16:31:57 -0700170#if NO_KEYPAD_DRIVER
171extern int fastboot_trigger(void);
172#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700173
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700174static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
175{
176 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700177#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
178 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
179 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
180 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700181#endif
182}
183
Dima Zavin42168f22009-01-30 11:52:22 -0800184static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
185{
186 struct atag_ptbl_entry atag_ptn;
187
188 memcpy(atag_ptn.name, ptn->name, 16);
189 atag_ptn.name[15] = '\0';
190 atag_ptn.offset = ptn->start;
191 atag_ptn.size = ptn->length;
192 atag_ptn.flags = ptn->flags;
193 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
194 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
195}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800196
Neeti Desaie245d492012-06-01 12:52:13 -0700197unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800198{
David Ng183a7422009-12-07 14:55:21 -0800199 int cmdline_len = 0;
200 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800201 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700202 int pause_at_bootup = 0;
Pavel Nedev5614d222013-06-17 18:01:02 +0300203 bool gpt_exists = partition_gpt_exists();
Dima Zavin42168f22009-01-30 11:52:22 -0800204
Brian Swetland9c4c0752009-01-25 16:23:50 -0800205 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800206 cmdline_len = strlen(cmdline);
207 have_cmdline = 1;
208 }
209 if (target_is_emmc_boot()) {
210 cmdline_len += strlen(emmc_cmdline);
211 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800212
213 cmdline_len += strlen(usb_sn_cmdline);
214 cmdline_len += strlen(sn_buf);
215
Pavel Nedev5614d222013-06-17 18:01:02 +0300216 if (boot_into_recovery && gpt_exists)
217 cmdline_len += strlen(secondary_gpt_enable);
218
Pavel Nedev328ac822013-04-05 15:25:11 +0300219 if (boot_into_ffbm) {
220 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700221 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800222 /* reduce kernel console messages to speed-up boot */
223 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700224 } else if (device.charger_screen_enabled &&
225 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700226 pause_at_bootup = 1;
227 cmdline_len += strlen(battchg_pause);
228 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800229
Shashank Mittalcd98d472011-08-02 14:29:24 -0700230 if(target_use_signed_kernel() && auth_kernel_img) {
231 cmdline_len += strlen(auth_kernel);
232 }
233
Ajay Dudanid04110c2011-01-17 23:55:07 -0800234 /* Determine correct androidboot.baseband to use */
235 switch(target_baseband())
236 {
237 case BASEBAND_APQ:
238 cmdline_len += strlen(baseband_apq);
239 break;
240
241 case BASEBAND_MSM:
242 cmdline_len += strlen(baseband_msm);
243 break;
244
245 case BASEBAND_CSFB:
246 cmdline_len += strlen(baseband_csfb);
247 break;
248
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800249 case BASEBAND_SVLTE2A:
250 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800251 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700252
253 case BASEBAND_MDM:
254 cmdline_len += strlen(baseband_mdm);
255 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700256
257 case BASEBAND_SGLTE:
258 cmdline_len += strlen(baseband_sglte);
259 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530260
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800261 case BASEBAND_SGLTE2:
262 cmdline_len += strlen(baseband_sglte2);
263 break;
264
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530265 case BASEBAND_DSDA:
266 cmdline_len += strlen(baseband_dsda);
267 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800268
269 case BASEBAND_DSDA2:
270 cmdline_len += strlen(baseband_dsda2);
271 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800272 }
273
Dhaval Patel223ec952013-07-18 14:49:44 -0700274 if (target_display_panel_node(display_panel_buf, MAX_PANEL_BUF_SIZE) &&
275 strlen(display_panel_buf))
276 {
277 cmdline_len += strlen(display_cmdline);
278 cmdline_len += strlen(display_panel_buf);
279 }
280
David Ng183a7422009-12-07 14:55:21 -0800281 if (cmdline_len > 0) {
282 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700283 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
284 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700285
Amol Jadi168b7712012-03-06 16:15:00 -0800286 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700287 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800288 if (have_cmdline) {
289 src = cmdline;
290 while ((*dst++ = *src++));
291 }
292 if (target_is_emmc_boot()) {
293 src = emmc_cmdline;
294 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700295 have_cmdline = 1;
296 while ((*dst++ = *src++));
297 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800298
299 src = usb_sn_cmdline;
300 if (have_cmdline) --dst;
301 have_cmdline = 1;
302 while ((*dst++ = *src++));
303 src = sn_buf;
304 if (have_cmdline) --dst;
305 have_cmdline = 1;
306 while ((*dst++ = *src++));
307
Pavel Nedev5614d222013-06-17 18:01:02 +0300308 if (boot_into_recovery && gpt_exists) {
309 src = secondary_gpt_enable;
310 if (have_cmdline) --dst;
311 while ((*dst++ = *src++));
312 }
313
Pavel Nedev328ac822013-04-05 15:25:11 +0300314 if (boot_into_ffbm) {
315 src = androidboot_mode;
316 if (have_cmdline) --dst;
317 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700318 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300319 if (have_cmdline) --dst;
320 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800321 src = loglevel;
322 if (have_cmdline) --dst;
323 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300324 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700325 src = battchg_pause;
326 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800327 while ((*dst++ = *src++));
328 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800329
Shashank Mittalcd98d472011-08-02 14:29:24 -0700330 if(target_use_signed_kernel() && auth_kernel_img) {
331 src = auth_kernel;
332 if (have_cmdline) --dst;
333 while ((*dst++ = *src++));
334 }
335
Ajay Dudanid04110c2011-01-17 23:55:07 -0800336 switch(target_baseband())
337 {
338 case BASEBAND_APQ:
339 src = baseband_apq;
340 if (have_cmdline) --dst;
341 while ((*dst++ = *src++));
342 break;
343
344 case BASEBAND_MSM:
345 src = baseband_msm;
346 if (have_cmdline) --dst;
347 while ((*dst++ = *src++));
348 break;
349
350 case BASEBAND_CSFB:
351 src = baseband_csfb;
352 if (have_cmdline) --dst;
353 while ((*dst++ = *src++));
354 break;
355
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800356 case BASEBAND_SVLTE2A:
357 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800358 if (have_cmdline) --dst;
359 while ((*dst++ = *src++));
360 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700361
362 case BASEBAND_MDM:
363 src = baseband_mdm;
364 if (have_cmdline) --dst;
365 while ((*dst++ = *src++));
366 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700367
368 case BASEBAND_SGLTE:
369 src = baseband_sglte;
370 if (have_cmdline) --dst;
371 while ((*dst++ = *src++));
372 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530373
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800374 case BASEBAND_SGLTE2:
375 src = baseband_sglte2;
376 if (have_cmdline) --dst;
377 while ((*dst++ = *src++));
378 break;
379
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530380 case BASEBAND_DSDA:
381 src = baseband_dsda;
382 if (have_cmdline) --dst;
383 while ((*dst++ = *src++));
384 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800385
386 case BASEBAND_DSDA2:
387 src = baseband_dsda2;
388 if (have_cmdline) --dst;
389 while ((*dst++ = *src++));
390 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800391 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700392
393 if (strlen(display_panel_buf)) {
394 src = display_cmdline;
395 if (have_cmdline) --dst;
396 while ((*dst++ = *src++));
397 src = display_panel_buf;
398 if (have_cmdline) --dst;
399 while ((*dst++ = *src++));
400 }
Neeti Desaie245d492012-06-01 12:52:13 -0700401 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700402
403
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800404 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700405 return cmdline_final;
406}
407
408unsigned *atag_core(unsigned *ptr)
409{
410 /* CORE */
411 *ptr++ = 2;
412 *ptr++ = 0x54410001;
413
414 return ptr;
415
416}
417
418unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
419 unsigned ramdisk_size)
420{
421 if (ramdisk_size) {
422 *ptr++ = 4;
423 *ptr++ = 0x54420005;
424 *ptr++ = (unsigned)ramdisk;
425 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800426 }
427
Neeti Desaie245d492012-06-01 12:52:13 -0700428 return ptr;
429}
430
431unsigned *atag_ptable(unsigned **ptr_addr)
432{
433 int i;
434 struct ptable *ptable;
435
436 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700437 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
438 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700439 *(*ptr_addr)++ = 0x4d534d70;
440 for (i = 0; i < ptable->count; ++i)
441 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
442 }
443
444 return (*ptr_addr);
445}
446
447unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
448{
449 int cmdline_length = 0;
450 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700451 char *dest;
452
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800453 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700454 n = (cmdline_length + 4) & (~3);
455
456 *ptr++ = (n / 4) + 2;
457 *ptr++ = 0x54410009;
458 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800459 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700460 ptr += (n / 4);
461
462 return ptr;
463}
464
465unsigned *atag_end(unsigned *ptr)
466{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800467 /* END */
468 *ptr++ = 0;
469 *ptr++ = 0;
470
Neeti Desaie245d492012-06-01 12:52:13 -0700471 return ptr;
472}
473
474void generate_atags(unsigned *ptr, const char *cmdline,
475 void *ramdisk, unsigned ramdisk_size)
476{
477
478 ptr = atag_core(ptr);
479 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
480 ptr = target_atag_mem(ptr);
481
482 /* Skip NAND partition ATAGS for eMMC boot */
483 if (!target_is_emmc_boot()){
484 ptr = atag_ptable(&ptr);
485 }
486
487 ptr = atag_cmdline(ptr, cmdline);
488 ptr = atag_end(ptr);
489}
490
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700491typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700492void boot_linux(void *kernel, unsigned *tags,
493 const char *cmdline, unsigned machtype,
494 void *ramdisk, unsigned ramdisk_size)
495{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800496 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800497#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700498 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800499#endif
500
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700501 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800502 uint32_t tags_phys = PA((addr_t)tags);
503
504 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700505
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800506 final_cmdline = update_cmdline((const char*)cmdline);
507
Neeti Desai17379b82012-06-04 18:42:53 -0700508#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800509 dprintf(INFO, "Updating device tree: start\n");
510
Neeti Desai17379b82012-06-04 18:42:53 -0700511 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800512 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700513 if(ret)
514 {
515 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
516 ASSERT(0);
517 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800518 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700519#else
Neeti Desaie245d492012-06-01 12:52:13 -0700520 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800521 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700522#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700523
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700524 /* Perform target specific cleanup */
525 target_uninit();
526
Aravind Venkateswarandb0d5262014-02-25 16:25:30 -0800527 /* Turn off splash screen if enabled */
528#if DISPLAY_SPLASH_SCREEN
529 target_display_shutdown();
530#endif
531
532
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800533 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
534 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800535
536 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700537
Amol Jadi4421e652011-06-16 15:00:48 -0700538 /* do any platform specific cleanup before kernel entry */
539 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700540
Brian Swetland9c4c0752009-01-25 16:23:50 -0800541 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700542
Amol Jadi504f9fe2012-08-16 13:56:48 -0700543#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800544 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700545#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700546 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700547 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800548}
549
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700550/* Function to check if the memory address range falls within the aboot
551 * boundaries.
552 * start: Start of the memory region
553 * size: Size of the memory region
554 */
555int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
556{
557 /* Check for boundary conditions. */
Sundarajan Srinivasan74252482013-12-16 17:02:56 -0800558 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700559 return -1;
560
561 /* Check for memory overlap. */
562 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
563 return 0;
Channagoud Kadabib4413fd2013-10-15 12:53:52 -0700564 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700565 return 0;
566 else
567 return -1;
568}
569
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800570#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800571
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700572BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800573#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700574BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800575#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800576
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700577static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
578{
579 int ret;
580
581 /* Assume device is rooted at this time. */
582 device.is_tampered = 1;
583
584 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
585
586 ret = image_verify((unsigned char *)bootimg_addr,
587 (unsigned char *)(bootimg_addr + bootimg_size),
588 bootimg_size,
589 CRYPTO_AUTH_ALG_SHA256);
590
591 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
592
593 if (ret)
594 {
595 /* Authorized kernel */
596 device.is_tampered = 0;
597 }
598
599#if USE_PCOM_SECBOOT
600 set_tamper_flag(device.is_tampered);
601#endif
602
603 if(device.is_tampered)
604 {
605 write_device_info_mmc(&device);
606 #ifdef TZ_TAMPER_FUSE
607 set_tamper_fuse_cmd();
608 #endif
609 #ifdef ASSERT_ON_TAMPER
610 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
611 ASSERT(0);
612 #endif
613 }
614}
615
Maunik Shah78a85e52014-03-02 17:47:58 +0530616static bool check_format_bit()
617{
618 bool ret = false;
619 int index;
620 uint64_t offset;
621 struct boot_selection_info *in = NULL;
622 char *buf = NULL;
623
624 index = partition_get_index("bootselect");
625 if (index == INVALID_PTN)
626 {
627 dprintf(INFO, "Unable to locate /bootselect partition\n");
628 return ret;
629 }
630 offset = partition_get_offset(index);
631 if(!offset)
632 {
633 dprintf(INFO, "partition /bootselect doesn't exist\n");
634 return ret;
635 }
636 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
637 ASSERT(buf);
638 if (mmc_read(offset, (unsigned int *)buf, page_size))
639 {
640 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
641 free(buf);
642 return ret;
643 }
644 in = (struct boot_selection_info *) buf;
645 if ((in->signature == BOOTSELECT_SIGNATURE) &&
646 (in->version == BOOTSELECT_VERSION)) {
647 if ((in->state_info & BOOTSELECT_FORMAT) &&
648 !(in->state_info & BOOTSELECT_FACTORY))
649 ret = true;
650 } else {
651 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
652 in->signature, in->version);
653 ASSERT(0);
654 }
655 free(buf);
656 return ret;
657}
658
Shashank Mittal23b8f422010-04-16 19:27:21 -0700659int boot_linux_from_mmc(void)
660{
661 struct boot_img_hdr *hdr = (void*) buf;
662 struct boot_img_hdr *uhdr;
663 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700664 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700665 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700666 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700667
Shashank Mittalcd98d472011-08-02 14:29:24 -0700668 unsigned char *image_addr = 0;
669 unsigned kernel_actual;
670 unsigned ramdisk_actual;
671 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700672 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700673
674#if DEVICE_TREE
675 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700676 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700677 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800678 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -0700679 uint32_t dt_hdr_size;
Neeti Desai465491e2012-07-31 12:53:35 -0700680#endif
Maunik Shah78a85e52014-03-02 17:47:58 +0530681 if (check_format_bit())
682 boot_into_recovery = 1;
683
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700684 if (!boot_into_recovery) {
685 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
686 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
687 if (rcode <= 0) {
688 boot_into_ffbm = false;
689 if (rcode < 0)
690 dprintf(CRITICAL,"failed to get ffbm cookie");
691 } else
692 boot_into_ffbm = true;
693 } else
694 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700695 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
696 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
697 dprintf(INFO, "Unified boot method!\n");
698 hdr = uhdr;
699 goto unified_boot;
700 }
Greg Griscod6250552011-06-29 14:40:23 -0700701 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700702 index = partition_get_index("boot");
703 ptn = partition_get_offset(index);
704 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700705 dprintf(CRITICAL, "ERROR: No boot partition found\n");
706 return -1;
707 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700708 }
709 else {
710 index = partition_get_index("recovery");
711 ptn = partition_get_offset(index);
712 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700713 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
714 return -1;
715 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700716 }
717
Greg Griscod6250552011-06-29 14:40:23 -0700718 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700719 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
720 return -1;
721 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700722
723 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700724 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700725 return -1;
726 }
727
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700728 if (hdr->page_size && (hdr->page_size != page_size)) {
729 page_size = hdr->page_size;
730 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700731 }
732
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700733 /*
734 * Update the kernel/ramdisk/tags address if the boot image header
735 * has default values, these default values come from mkbootimg when
736 * the boot image is flashed using fastboot flash:raw
737 */
738 update_ker_tags_rdisk_addr(hdr);
739
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700740 /* Get virtual addresses since the hdr saves physical addresses. */
741 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
742 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
743 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700744
745 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
746 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
747
748 /* Check if the addresses in the header are valid. */
749 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
750 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
751 {
752 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
753 return -1;
754 }
755
756#ifndef DEVICE_TREE
757 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
758 {
759 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
760 return -1;
761 }
762#endif
763
Shashank Mittalcd98d472011-08-02 14:29:24 -0700764 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300765 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
766 (int) target_use_signed_kernel(),
767 device.is_unlocked,
768 device.is_tampered);
769
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700770 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800771 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700772 offset = 0;
773
Shashank Mittalcd98d472011-08-02 14:29:24 -0700774 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700775
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800776#if DEVICE_TREE
777 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
778 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700779
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700780 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700781 {
782 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
783 return -1;
784 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800785#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530786 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700787
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700788#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700789
Amol Jadib6be5c12012-11-14 13:39:51 -0800790 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700791 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800792
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700793 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
794 {
795 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
796 return -1;
797 }
798
Shashank Mittalcd98d472011-08-02 14:29:24 -0700799 /* Read image without signature */
800 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
801 {
802 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
803 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800804 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700805
Amol Jadib6be5c12012-11-14 13:39:51 -0800806 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700807 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800808
Shashank Mittalcd98d472011-08-02 14:29:24 -0700809 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700810
811 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
812 {
813 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
814 return -1;
815 }
816
Shashank Mittalcd98d472011-08-02 14:29:24 -0700817 /* Read signature */
818 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
819 {
820 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700821 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700822 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800823
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700824 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700825
Neeti Desai465491e2012-07-31 12:53:35 -0700826 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700827 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
828 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700829
Neeti Desai465491e2012-07-31 12:53:35 -0700830 #if DEVICE_TREE
831 if(hdr->dt_size) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700832 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Deepa Dinamani19648b42013-09-05 17:05:55 -0700833 table = (struct dt_table*) dt_table_offset;
Neeti Desai465491e2012-07-31 12:53:35 -0700834
Deepa Dinamani19648b42013-09-05 17:05:55 -0700835 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700836 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
837 return -1;
838 }
Deepa Dinamani19648b42013-09-05 17:05:55 -0700839
Neeti Desai465491e2012-07-31 12:53:35 -0700840 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700841 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700842 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
843 return -1;
844 }
845
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700846 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700847 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700848 {
849 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
850 return -1;
851 }
852
Joel Kingaa335dc2013-06-03 16:11:08 -0700853 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800854 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800855 /*
856 * If appended dev tree is found, update the atags with
857 * memory address to the DTB appended location on RAM.
858 * Else update with the atags address in the kernel header
859 */
860 void *dtb;
861 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700862 hdr->kernel_size,
863 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800864 if (!dtb) {
865 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
866 return -1;
867 }
Neeti Desai465491e2012-07-31 12:53:35 -0700868 }
869 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700870 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700871 else
872 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800873 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700874
Amol Jadib6be5c12012-11-14 13:39:51 -0800875 dprintf(INFO, "Loading boot image (%d): start\n",
876 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700877 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800878
879 offset = page_size;
880
881 /* Load kernel */
882 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700883 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
884 return -1;
885 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800886 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700887
Amol Jadib6be5c12012-11-14 13:39:51 -0800888 /* Load ramdisk */
889 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700890 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800891 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700892 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
893 return -1;
894 }
895 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800896 offset += ramdisk_actual;
897
898 dprintf(INFO, "Loading boot image (%d): done\n",
899 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700900 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700901
902 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800903 offset += second_actual;
904 /* Second image loading not implemented. */
905 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700906 }
907
908 #if DEVICE_TREE
909 if(hdr->dt_size != 0) {
Deepa Dinamani19648b42013-09-05 17:05:55 -0700910 /* Read the first page of device tree table into buffer */
Neeti Desai465491e2012-07-31 12:53:35 -0700911 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
912 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
913 return -1;
914 }
915 table = (struct dt_table*) dt_buf;
916
Deepa Dinamani19648b42013-09-05 17:05:55 -0700917 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700918 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
919 return -1;
920 }
921
Deepa Dinamani19648b42013-09-05 17:05:55 -0700922 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
923 if (!table)
924 return -1;
925
926 /* Read the entire device tree table into buffer */
927 if(mmc_read(ptn + offset,(unsigned int *) table, dt_hdr_size)) {
928 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
929 return -1;
930 }
931
Joel Kingaa335dc2013-06-03 16:11:08 -0700932 /* Find index of device tree within device tree table */
933 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700934 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
935 return -1;
936 }
937
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700938 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700939 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700940 {
941 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
942 return -1;
943 }
944
Joel Kingaa335dc2013-06-03 16:11:08 -0700945 if(mmc_read(ptn + offset + dt_entry.offset,
946 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700947 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
948 return -1;
949 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300950 #ifdef TZ_SAVE_KERNEL_HASH
951 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
952 hdr->ramdisk_addr, ramdisk_actual,
953 ptn, offset, hdr->dt_size);
954 #endif /* TZ_SAVE_KERNEL_HASH */
955
Channagoud Kadabi35095622013-03-01 13:53:05 -0800956 } else {
Aparna Mallavarapuf5432e42014-06-03 22:15:54 +0530957
958 /* Validate the tags_addr */
959 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
960 {
961 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
962 return -1;
963 }
Dima Zavin77e41f32013-03-06 16:10:43 -0800964 /*
965 * If appended dev tree is found, update the atags with
966 * memory address to the DTB appended location on RAM.
967 * Else update with the atags address in the kernel header
968 */
969 void *dtb;
970 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700971 kernel_actual,
972 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800973 if (!dtb) {
974 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
975 return -1;
976 }
Neeti Desai465491e2012-07-31 12:53:35 -0700977 }
978 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700979 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700980
Stanimir Varbanov69ec5462013-07-18 18:17:42 +0300981 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
982 target_load_ssd_keystore();
983
Shashank Mittal23b8f422010-04-16 19:27:21 -0700984unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700985
Dima Zavin77e41f32013-03-06 16:10:43 -0800986 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700987 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700988 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
989
990 return 0;
991}
992
Dima Zavin214cc642009-01-26 11:16:21 -0800993int boot_linux_from_flash(void)
994{
995 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800996 struct ptentry *ptn;
997 struct ptable *ptable;
998 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800999
Shashank Mittalcd98d472011-08-02 14:29:24 -07001000 unsigned char *image_addr = 0;
1001 unsigned kernel_actual;
1002 unsigned ramdisk_actual;
1003 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -08001004 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001005
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001006#if DEVICE_TREE
1007 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001008 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001009 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001010 uint32_t dt_hdr_size;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001011#endif
1012
David Ng183a7422009-12-07 14:55:21 -08001013 if (target_is_emmc_boot()) {
1014 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1015 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1016 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1017 return -1;
1018 }
1019 goto continue_boot;
1020 }
1021
Dima Zavin214cc642009-01-26 11:16:21 -08001022 ptable = flash_get_ptable();
1023 if (ptable == NULL) {
1024 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1025 return -1;
1026 }
1027
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001028 if(!boot_into_recovery)
1029 {
1030 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001031
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001032 if (ptn == NULL) {
1033 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1034 return -1;
1035 }
1036 }
1037 else
1038 {
1039 ptn = ptable_find(ptable, "recovery");
1040 if (ptn == NULL) {
1041 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1042 return -1;
1043 }
Dima Zavin214cc642009-01-26 11:16:21 -08001044 }
1045
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001046 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001047 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1048 return -1;
1049 }
Dima Zavin214cc642009-01-26 11:16:21 -08001050
1051 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001052 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001053 return -1;
1054 }
1055
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001056 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001057 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 -08001058 return -1;
1059 }
1060
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001061 /*
1062 * Update the kernel/ramdisk/tags address if the boot image header
1063 * has default values, these default values come from mkbootimg when
1064 * the boot image is flashed using fastboot flash:raw
1065 */
1066 update_ker_tags_rdisk_addr(hdr);
1067
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001068 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001069 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1070 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1071 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1072
1073 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1074 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1075
1076 /* Check if the addresses in the header are valid. */
1077 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1078 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1079 {
1080 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1081 return -1;
1082 }
1083
1084#ifndef DEVICE_TREE
1085 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1086 {
1087 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1088 return -1;
1089 }
1090#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001091
Shashank Mittalcd98d472011-08-02 14:29:24 -07001092 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001093 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001094 {
1095 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001096 offset = 0;
1097
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001098#if DEVICE_TREE
1099 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1100 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001101
1102 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1103 {
1104 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1105 return -1;
1106 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001107#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001108 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001109#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001110
Amol Jadib6be5c12012-11-14 13:39:51 -08001111 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001112 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001113
Shashank Mittalcd98d472011-08-02 14:29:24 -07001114 /* Read image without signature */
1115 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1116 {
1117 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1118 return -1;
1119 }
Dima Zavin214cc642009-01-26 11:16:21 -08001120
Amol Jadib6be5c12012-11-14 13:39:51 -08001121 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001122 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001123
Shashank Mittalcd98d472011-08-02 14:29:24 -07001124 offset = imagesize_actual;
1125 /* Read signature */
1126 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1127 {
1128 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001129 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001130 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001131
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001132 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001133
1134 /* Move kernel and ramdisk to correct address */
1135 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1136 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001137#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001138 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001139 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001140 {
1141 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1142 return -1;
1143 }
1144
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001145 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1146#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001147
1148 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001149 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001150 {
1151 write_device_info_flash(&device);
1152 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301153#if USE_PCOM_SECBOOT
1154 set_tamper_flag(device.is_tampered);
1155#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001156 }
1157 else
1158 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001159 offset = page_size;
1160
Amol Jadib6be5c12012-11-14 13:39:51 -08001161 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1162 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1163 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1164
1165 dprintf(INFO, "Loading boot image (%d): start\n",
1166 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001167 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001168
1169 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001170 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1171 return -1;
1172 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001173 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001174
Amol Jadib6be5c12012-11-14 13:39:51 -08001175 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001176 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1177 return -1;
1178 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001179 offset += ramdisk_actual;
1180
1181 dprintf(INFO, "Loading boot image (%d): done\n",
1182 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001183 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001184
1185 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001186 offset += second_actual;
1187 /* Second image loading not implemented. */
1188 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001189 }
1190
1191#if DEVICE_TREE
1192 if(hdr->dt_size != 0) {
1193
1194 /* Read the device tree table into buffer */
1195 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1196 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1197 return -1;
1198 }
1199
1200 table = (struct dt_table*) dt_buf;
1201
Deepa Dinamani19648b42013-09-05 17:05:55 -07001202 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001203 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1204 return -1;
1205 }
1206
Deepa Dinamani19648b42013-09-05 17:05:55 -07001207 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1208 if (!table)
1209 return -1;
1210
1211 /* Read the entire device tree table into buffer */
1212 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1213 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1214 return -1;
1215 }
1216
1217
Joel Kingaa335dc2013-06-03 16:11:08 -07001218 /* Find index of device tree within device tree table */
1219 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001220 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1221 return -1;
1222 }
1223
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001224 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001225 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001226 {
1227 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1228 return -1;
1229 }
1230
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001231 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001232 if(flash_read(ptn, offset + dt_entry.offset,
1233 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001234 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1235 return -1;
1236 }
1237 }
1238#endif
1239
Shashank Mittalcd98d472011-08-02 14:29:24 -07001240 }
David Ng183a7422009-12-07 14:55:21 -08001241continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001242
Dima Zavin214cc642009-01-26 11:16:21 -08001243 /* TODO: create/pass atags to kernel */
1244
Ajay Dudanie28a6072011-07-01 13:59:46 -07001245 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001246 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001247 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1248
1249 return 0;
1250}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001251
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001252BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001253void write_device_info_mmc(device_info *dev)
1254{
1255 struct device_info *info = (void*) info_buf;
1256 unsigned long long ptn = 0;
1257 unsigned long long size;
1258 int index = INVALID_PTN;
1259
1260 index = partition_get_index("aboot");
1261 ptn = partition_get_offset(index);
1262 if(ptn == 0)
1263 {
1264 return;
1265 }
1266
1267 size = partition_get_size(index);
1268
1269 memcpy(info, dev, sizeof(device_info));
1270
1271 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1272 {
1273 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1274 return;
1275 }
1276}
1277
1278void read_device_info_mmc(device_info *dev)
1279{
1280 struct device_info *info = (void*) info_buf;
1281 unsigned long long ptn = 0;
1282 unsigned long long size;
1283 int index = INVALID_PTN;
1284
1285 index = partition_get_index("aboot");
1286 ptn = partition_get_offset(index);
1287 if(ptn == 0)
1288 {
1289 return;
1290 }
1291
1292 size = partition_get_size(index);
1293
1294 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1295 {
1296 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1297 return;
1298 }
1299
1300 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1301 {
1302 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1303 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001304 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001305 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001306
1307 write_device_info_mmc(info);
1308 }
1309 memcpy(dev, info, sizeof(device_info));
1310}
1311
1312void write_device_info_flash(device_info *dev)
1313{
1314 struct device_info *info = (void *) info_buf;
1315 struct ptentry *ptn;
1316 struct ptable *ptable;
1317
1318 ptable = flash_get_ptable();
1319 if (ptable == NULL)
1320 {
1321 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1322 return;
1323 }
1324
1325 ptn = ptable_find(ptable, "devinfo");
1326 if (ptn == NULL)
1327 {
1328 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1329 return;
1330 }
1331
1332 memcpy(info, dev, sizeof(device_info));
1333
1334 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1335 {
1336 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1337 return;
1338 }
1339}
1340
1341void read_device_info_flash(device_info *dev)
1342{
1343 struct device_info *info = (void*) info_buf;
1344 struct ptentry *ptn;
1345 struct ptable *ptable;
1346
1347 ptable = flash_get_ptable();
1348 if (ptable == NULL)
1349 {
1350 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1351 return;
1352 }
1353
1354 ptn = ptable_find(ptable, "devinfo");
1355 if (ptn == NULL)
1356 {
1357 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1358 return;
1359 }
1360
1361 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1362 {
1363 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1364 return;
1365 }
1366
1367 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1368 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001369 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1370 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001371 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001372 write_device_info_flash(info);
1373 }
1374 memcpy(dev, info, sizeof(device_info));
1375}
1376
1377void write_device_info(device_info *dev)
1378{
1379 if(target_is_emmc_boot())
1380 {
1381 write_device_info_mmc(dev);
1382 }
1383 else
1384 {
1385 write_device_info_flash(dev);
1386 }
1387}
1388
1389void read_device_info(device_info *dev)
1390{
1391 if(target_is_emmc_boot())
1392 {
1393 read_device_info_mmc(dev);
1394 }
1395 else
1396 {
1397 read_device_info_flash(dev);
1398 }
1399}
1400
1401void reset_device_info()
1402{
1403 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001404 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001405 write_device_info(&device);
1406}
1407
1408void set_device_root()
1409{
1410 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001411 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001412 write_device_info(&device);
1413}
1414
Amol Jadicb524072012-08-09 16:40:18 -07001415#if DEVICE_TREE
1416int copy_dtb(uint8_t *boot_image_start)
1417{
1418 uint32 dt_image_offset = 0;
1419 uint32_t n;
1420 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001421 struct dt_entry dt_entry;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001422 uint32_t dt_hdr_size;
Amol Jadicb524072012-08-09 16:40:18 -07001423
1424 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1425
Amol Jadicb524072012-08-09 16:40:18 -07001426 if(hdr->dt_size != 0) {
1427
1428 /* add kernel offset */
1429 dt_image_offset += page_size;
1430 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1431 dt_image_offset += n;
1432
1433 /* add ramdisk offset */
1434 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1435 dt_image_offset += n;
1436
1437 /* add second offset */
1438 if(hdr->second_size != 0) {
1439 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1440 dt_image_offset += n;
1441 }
1442
1443 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001444 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001445
Deepa Dinamani19648b42013-09-05 17:05:55 -07001446 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001447 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1448 return -1;
1449 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001450 /* Find index of device tree within device tree table */
1451 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001452 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1453 return -1;
1454 }
1455
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001456 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001457 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001458 {
1459 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1460 return -1;
1461 }
1462
Amol Jadicb524072012-08-09 16:40:18 -07001463 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001464 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001465 boot_image_start + dt_image_offset + dt_entry.offset,
1466 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001467 } else
1468 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001469
1470 /* Everything looks fine. Return success. */
1471 return 0;
1472}
1473#endif
1474
Brian Swetland9c4c0752009-01-25 16:23:50 -08001475void cmd_boot(const char *arg, void *data, unsigned sz)
1476{
1477 unsigned kernel_actual;
1478 unsigned ramdisk_actual;
Channagoud Kadabice737c82014-06-18 17:41:01 -07001479 uint32_t image_actual;
1480 uint32_t dt_actual = 0;
Channagoud Kadabi87c2dd62014-06-26 14:26:39 -07001481 uint32_t sig_actual = SIGNATURE_SIZE;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001482 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001483 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001484 int ret = 0;
1485 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001486
1487 if (sz < sizeof(hdr)) {
1488 fastboot_fail("invalid bootimage header");
1489 return;
1490 }
1491
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001492 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001493
1494 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001495 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001496
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001497 if(target_is_emmc_boot() && hdr->page_size) {
1498 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001499 page_mask = page_size - 1;
1500 }
1501
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001502 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1503 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabice737c82014-06-18 17:41:01 -07001504#if DEVICE_TREE
1505 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1506#endif
1507
1508 image_actual = ADD_OF(page_size, kernel_actual);
1509 image_actual = ADD_OF(image_actual, ramdisk_actual);
1510 image_actual = ADD_OF(image_actual, dt_actual);
1511
Channagoud Kadabi87c2dd62014-06-26 14:26:39 -07001512 if (target_use_signed_kernel() && (!device.is_unlocked))
1513 image_actual = ADD_OF(image_actual, sig_actual);
1514
Channagoud Kadabice737c82014-06-18 17:41:01 -07001515 /* sz should have atleast raw boot image */
1516 if (image_actual > sz) {
Channagoud Kadabi87c2dd62014-06-26 14:26:39 -07001517 fastboot_fail("bootimage: incomplete or not signed");
Channagoud Kadabice737c82014-06-18 17:41:01 -07001518 return;
1519 }
1520
1521 /* Verify the boot image
1522 * device & page_size are initialized in aboot_init
1523 */
1524 if (target_use_signed_kernel() && (!device.is_unlocked))
Channagoud Kadabi87c2dd62014-06-26 14:26:39 -07001525 /* Pass size excluding signature size, otherwise we would try to
1526 * access signature beyond its length
1527 */
1528 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001529
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001530 /*
1531 * Update the kernel/ramdisk/tags address if the boot image header
1532 * has default values, these default values come from mkbootimg when
1533 * the boot image is flashed using fastboot flash:raw
1534 */
1535 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001536
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001537 /* Get virtual addresses since the hdr saves physical addresses. */
1538 hdr->kernel_addr = VA(hdr->kernel_addr);
1539 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1540 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001541
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001542 /* Check if the addresses in the header are valid. */
1543 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1544 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1545 {
1546 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabia54102a2013-12-03 11:19:20 -08001547 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001548 }
1549
Amol Jadicb524072012-08-09 16:40:18 -07001550#if DEVICE_TREE
1551 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001552 ret = copy_dtb(data);
1553
1554 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001555#else
1556 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1557 {
1558 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabia54102a2013-12-03 11:19:20 -08001559 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001560 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001561#endif
1562
1563 /* Load ramdisk & kernel */
1564 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1565 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1566
1567#if DEVICE_TREE
1568 /*
1569 * If dtb is not found look for appended DTB in the kernel.
1570 * If appended dev tree is found, update the atags with
1571 * memory address to the DTB appended location on RAM.
1572 * Else update with the atags address in the kernel header
1573 */
1574 if (!dtb_copied) {
1575 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001576 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1577 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001578 if (!dtb) {
1579 fastboot_fail("dtb not found");
1580 return;
1581 }
Amol Jadicb524072012-08-09 16:40:18 -07001582 }
1583#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001584
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001585#ifndef DEVICE_TREE
1586 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1587 {
1588 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabia54102a2013-12-03 11:19:20 -08001589 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001590 }
1591#endif
1592
Brian Swetland9c4c0752009-01-25 16:23:50 -08001593 fastboot_okay("");
1594 udc_stop();
1595
Dima Zavin77e41f32013-03-06 16:10:43 -08001596 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001597 (const char*) hdr->cmdline, board_machtype(),
1598 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001599}
1600
Dima Zavin214cc642009-01-26 11:16:21 -08001601void cmd_erase(const char *arg, void *data, unsigned sz)
1602{
1603 struct ptentry *ptn;
1604 struct ptable *ptable;
1605
1606 ptable = flash_get_ptable();
1607 if (ptable == NULL) {
1608 fastboot_fail("partition table doesn't exist");
1609 return;
1610 }
1611
1612 ptn = ptable_find(ptable, arg);
1613 if (ptn == NULL) {
1614 fastboot_fail("unknown partition name");
1615 return;
1616 }
1617
1618 if (flash_erase(ptn)) {
1619 fastboot_fail("failed to erase partition");
1620 return;
1621 }
1622 fastboot_okay("");
1623}
1624
Bikas Gurungd48bd242010-09-04 19:54:32 -07001625
1626void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1627{
Kun Liang2f1601a2013-08-12 16:29:54 +08001628 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001629 unsigned long long ptn = 0;
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001630 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001631 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001632
Kinson Chikf1a43512011-07-14 11:28:39 -07001633 index = partition_get_index(arg);
1634 ptn = partition_get_offset(index);
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001635 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001636
Kinson Chikf1a43512011-07-14 11:28:39 -07001637 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001638 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001639 return;
1640 }
Kun Liang2f1601a2013-08-12 16:29:54 +08001641
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001642#if MMC_SDHCI_SUPPORT
1643 if (mmc_erase_card(ptn, size)) {
1644 fastboot_fail("failed to erase partition\n");
1645 return;
1646 }
1647#else
Kun Liang2f1601a2013-08-12 16:29:54 +08001648 size = partition_get_size(index);
1649 if (size > DEFAULT_ERASE_SIZE)
1650 size = DEFAULT_ERASE_SIZE;
1651
neetidb4b24d62012-01-20 12:13:09 -08001652 /* Simple inefficient version of erase. Just writing
Kun Liang2f1601a2013-08-12 16:29:54 +08001653 0 in first several blocks */
1654 if (mmc_write(ptn , size, (unsigned int *)out)) {
neetidb4b24d62012-01-20 12:13:09 -08001655 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001656 return;
1657 }
Oliver Wang0e0dfa72013-10-22 18:40:13 +08001658#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07001659 fastboot_okay("");
1660}
1661
1662
Ajay Dudani5c761132011-04-07 20:19:04 -07001663void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001664{
1665 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001666 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001667 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001668
Greg Grisco6e754772011-06-23 12:19:39 -07001669 if (!strcmp(arg, "partition"))
1670 {
1671 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001672 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001673 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001674 return;
1675 }
1676 }
Greg Grisco6e754772011-06-23 12:19:39 -07001677 else
1678 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001679 index = partition_get_index(arg);
1680 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001681 if(ptn == 0) {
1682 fastboot_fail("partition table doesn't exist");
1683 return;
1684 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001685
Greg Grisco6e754772011-06-23 12:19:39 -07001686 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1687 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1688 fastboot_fail("image is not a boot image");
1689 return;
1690 }
1691 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001692
Kinson Chikf1a43512011-07-14 11:28:39 -07001693 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001694 if (ROUND_TO_PAGE(sz,511) > size) {
1695 fastboot_fail("size too large");
1696 return;
1697 }
1698 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1699 fastboot_fail("flash write failure");
1700 return;
1701 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001702 }
1703 fastboot_okay("");
1704 return;
1705}
1706
Ajay Dudani5c761132011-04-07 20:19:04 -07001707void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1708{
1709 unsigned int chunk;
1710 unsigned int chunk_data_sz;
1711 sparse_header_t *sparse_header;
1712 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001713 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001714 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301715 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001716 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001717
Kinson Chikf1a43512011-07-14 11:28:39 -07001718 index = partition_get_index(arg);
1719 ptn = partition_get_offset(index);
1720 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001721 fastboot_fail("partition table doesn't exist");
1722 return;
1723 }
1724
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301725 size = partition_get_size(index);
1726 if (ROUND_TO_PAGE(sz,511) > size) {
1727 fastboot_fail("size too large");
1728 return;
1729 }
1730
Ajay Dudani5c761132011-04-07 20:19:04 -07001731 /* Read and skip over sparse image header */
1732 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001733 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1734 fastboot_fail("size too large");
1735 return;
1736 }
1737
Ajay Dudani5c761132011-04-07 20:19:04 -07001738 data += sparse_header->file_hdr_sz;
1739 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1740 {
1741 /* Skip the remaining bytes in a header that is longer than
1742 * we expected.
1743 */
1744 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1745 }
1746
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001747 dprintf (SPEW, "=== Sparse Image Header ===\n");
1748 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1749 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1750 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1751 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1752 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1753 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1754 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1755 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001756
1757 /* Start processing chunks */
1758 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1759 {
1760 /* Read and skip over chunk header */
1761 chunk_header = (chunk_header_t *) data;
1762 data += sizeof(chunk_header_t);
1763
1764 dprintf (SPEW, "=== Chunk Header ===\n");
1765 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1766 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1767 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1768
1769 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1770 {
1771 /* Skip the remaining bytes in a header that is longer than
1772 * we expected.
1773 */
1774 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1775 }
1776
1777 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1778 switch (chunk_header->chunk_type)
1779 {
1780 case CHUNK_TYPE_RAW:
1781 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1782 chunk_data_sz))
1783 {
1784 fastboot_fail("Bogus chunk size for chunk type Raw");
1785 return;
1786 }
1787
Ajay Dudaniab18f022011-05-12 14:39:22 -07001788 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1789 chunk_data_sz,
1790 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001791 {
1792 fastboot_fail("flash write failure");
1793 return;
1794 }
1795 total_blocks += chunk_header->chunk_sz;
1796 data += chunk_data_sz;
1797 break;
1798
1799 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001800 total_blocks += chunk_header->chunk_sz;
1801 break;
1802
Ajay Dudani5c761132011-04-07 20:19:04 -07001803 case CHUNK_TYPE_CRC:
1804 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1805 {
1806 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1807 return;
1808 }
1809 total_blocks += chunk_header->chunk_sz;
1810 data += chunk_data_sz;
1811 break;
1812
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001813 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001814 fastboot_fail("Unknown chunk type");
1815 return;
1816 }
1817 }
1818
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001819 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1820 total_blocks, sparse_header->total_blks);
1821
1822 if(total_blocks != sparse_header->total_blks)
1823 {
1824 fastboot_fail("sparse image write failure");
1825 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001826
1827 fastboot_okay("");
1828 return;
1829}
1830
1831void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1832{
1833 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001834 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1835 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001836
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001837#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001838 int ret=0;
1839 uint32 major_version=0;
1840 uint32 minor_version=0;
1841
1842 ret = scm_svc_version(&major_version,&minor_version);
1843 if(!ret)
1844 {
1845 if(major_version >= 2)
1846 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001847 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001848 {
1849 ret = encrypt_scm((uint32 **) &data, &sz);
1850 if (ret != 0) {
1851 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1852 return;
1853 }
1854
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001855 /* Protect only for SSD */
1856 if (!strcmp(arg, "ssd")) {
1857 ret = scm_protect_keystore((uint32 *) data, sz);
1858 if (ret != 0) {
1859 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1860 return;
1861 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001862 }
1863 }
1864 else
1865 {
1866 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1867 if(ret != 0)
1868 {
1869 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1870 return;
1871 }
1872 }
1873 }
1874 else
1875 {
1876 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1877 magic_number[1] == DECRYPT_MAGIC_1)
1878 {
1879 ret = decrypt_scm((uint32 **) &data, &sz);
1880 if (ret != 0) {
1881 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1882 return;
1883 }
1884 }
1885 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1886 magic_number[1] == ENCRYPT_MAGIC_1)
1887 {
1888 ret = encrypt_scm((uint32 **) &data, &sz);
1889 if (ret != 0) {
1890 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1891 return;
1892 }
1893 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001894 }
1895 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001896 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001897 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001898 dprintf(CRITICAL,"INVALID SVC Version\n");
1899 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001900 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001901#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001902
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001903 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001904 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1905 cmd_flash_mmc_img(arg, data, sz);
1906 else
1907 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001908 return;
1909}
1910
Dima Zavin214cc642009-01-26 11:16:21 -08001911void cmd_flash(const char *arg, void *data, unsigned sz)
1912{
1913 struct ptentry *ptn;
1914 struct ptable *ptable;
1915 unsigned extra = 0;
1916
1917 ptable = flash_get_ptable();
1918 if (ptable == NULL) {
1919 fastboot_fail("partition table doesn't exist");
1920 return;
1921 }
1922
1923 ptn = ptable_find(ptable, arg);
1924 if (ptn == NULL) {
1925 fastboot_fail("unknown partition name");
1926 return;
1927 }
1928
1929 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1930 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1931 fastboot_fail("image is not a boot image");
1932 return;
1933 }
1934 }
1935
Amol Jadi5c61a952012-05-04 17:05:35 -07001936 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001937 || !strcmp(ptn->name, "userdata")
1938 || !strcmp(ptn->name, "persist")
1939 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301940 if (flash_ecc_bch_enabled())
1941 /* Spare data bytes for 8 bit ECC increased by 4 */
1942 extra = ((page_size >> 9) * 20);
1943 else
1944 extra = ((page_size >> 9) * 16);
1945 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001946 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001947
1948 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1949 if (flash_write(ptn, extra, data, sz)) {
1950 fastboot_fail("flash write failure");
1951 return;
1952 }
1953 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1954 fastboot_okay("");
1955}
1956
1957void cmd_continue(const char *arg, void *data, unsigned sz)
1958{
1959 fastboot_okay("");
1960 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001961 if (target_is_emmc_boot())
1962 {
1963 boot_linux_from_mmc();
1964 }
1965 else
1966 {
1967 boot_linux_from_flash();
1968 }
Dima Zavin214cc642009-01-26 11:16:21 -08001969}
1970
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001971void cmd_reboot(const char *arg, void *data, unsigned sz)
1972{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001973 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001974 fastboot_okay("");
1975 reboot_device(0);
1976}
1977
1978void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1979{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001980 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001981 fastboot_okay("");
1982 reboot_device(FASTBOOT_MODE);
1983}
1984
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001985void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
1986{
1987 dprintf(INFO, "Enabling charger screen check\n");
1988 device.charger_screen_enabled = 1;
1989 write_device_info(&device);
1990 fastboot_okay("");
1991}
1992
1993void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
1994{
1995 dprintf(INFO, "Disabling charger screen check\n");
1996 device.charger_screen_enabled = 0;
1997 write_device_info(&device);
1998 fastboot_okay("");
1999}
2000
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302001void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
2002{
2003 dprintf(INFO, "Selecting display panel %s\n", arg);
2004 if (arg)
2005 strlcpy(device.display_panel, arg,
2006 sizeof(device.display_panel));
2007 write_device_info(&device);
2008 fastboot_okay("");
2009}
2010
Shashank Mittal162244e2011-08-08 19:01:25 -07002011void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
2012{
2013 if(!device.is_unlocked)
2014 {
2015 device.is_unlocked = 1;
2016 write_device_info(&device);
2017 }
2018 fastboot_okay("");
2019}
2020
Shashank Mittala0032282011-08-26 14:50:11 -07002021void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
2022{
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302023 char response[128];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002024 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002025 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002026 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
2027 fastboot_info(response);
2028 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07002029 fastboot_info(response);
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302030 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
2031 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07002032 fastboot_okay("");
2033}
2034
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002035void cmd_preflash(const char *arg, void *data, unsigned sz)
2036{
2037 fastboot_okay("");
2038}
2039
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302040static struct fbimage logo_header = {0};
2041struct fbimage* splash_screen_flash();
2042
2043int splash_screen_check_header(struct fbimage *logo)
2044{
2045 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
2046 return -1;
2047 if (logo->header.width == 0 || logo->header.height == 0)
2048 return -1;
2049 return 0;
2050}
2051
2052struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002053{
2054 struct ptentry *ptn;
2055 struct ptable *ptable;
2056 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302057 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002058
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302059
2060 ptable = flash_get_ptable();
2061 if (ptable == NULL) {
2062 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2063 return NULL;
2064 }
2065 ptn = ptable_find(ptable, "splash");
2066 if (ptn == NULL) {
2067 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
2068 return NULL;
2069 }
2070
2071 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
2072 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2073 return NULL;
2074 }
2075
2076 if (splash_screen_check_header(logo)) {
2077 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
2078 return NULL;
2079 }
2080
2081 fb_display = fbcon_display();
2082 if (fb_display) {
2083 uint8_t *base = (uint8_t *) fb_display->base;
2084 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
2085 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002086 }
2087
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302088 if (flash_read(ptn + sizeof(logo->header), 0,
2089 base,
2090 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2091 fbcon_clear();
2092 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2093 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002094 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05302095 logo->image = base;
2096 }
2097
2098 return logo;
2099}
2100
2101struct fbimage* splash_screen_mmc()
2102{
2103 int index = INVALID_PTN;
2104 unsigned long long ptn = 0;
2105 struct fbcon_config *fb_display = NULL;
2106 struct fbimage *logo = &logo_header;
2107
2108 index = partition_get_index("splash");
2109 if (index == 0) {
2110 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
2111 return NULL;
2112 }
2113
2114 ptn = partition_get_offset(index);
2115 if (ptn == 0) {
2116 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
2117 return NULL;
2118 }
2119
2120 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
2121 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
2122 return NULL;
2123 }
2124
2125 if (splash_screen_check_header(logo)) {
2126 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
2127 return NULL;
2128 }
2129
2130 fb_display = fbcon_display();
2131 if (fb_display) {
2132 uint8_t *base = (uint8_t *) fb_display->base;
2133 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
2134 base += LOGO_IMG_OFFSET;
2135
2136 if (mmc_read(ptn + sizeof(logo->header),
2137 base,
2138 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
2139 fbcon_clear();
2140 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
2141 return NULL;
2142 }
2143
2144 logo->image = base;
2145 }
2146
2147 return logo;
2148}
2149
2150
2151struct fbimage* fetch_image_from_partition()
2152{
2153 if (target_is_emmc_boot()) {
2154 return splash_screen_mmc();
2155 } else {
2156 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002157 }
2158}
2159
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002160/* Get the size from partiton name */
2161static void get_partition_size(const char *arg, char *response)
2162{
2163 uint64_t ptn = 0;
2164 uint64_t size;
2165 int index = INVALID_PTN;
2166
2167 index = partition_get_index(arg);
2168
2169 if (index == INVALID_PTN)
2170 {
2171 dprintf(CRITICAL, "Invalid partition index\n");
2172 return;
2173 }
2174
2175 ptn = partition_get_offset(index);
2176
2177 if(!ptn)
2178 {
2179 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2180 return;
2181 }
2182
2183 size = partition_get_size(index);
2184
2185 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2186 return;
2187}
2188
2189/*
2190 * Publish the partition type & size info
2191 * fastboot getvar will publish the required information.
2192 * fastboot getvar partition_size:<partition_name>: partition size in hex
2193 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2194 */
2195static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2196{
2197 uint8_t i;
2198
2199 for (i = 0; i < num_parts; i++) {
2200 get_partition_size(info[i].part_name, info[i].size_response);
2201
2202 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2203 {
2204 dprintf(CRITICAL, "partition size name truncated\n");
2205 return;
2206 }
2207 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2208 {
2209 dprintf(CRITICAL, "partition type name truncated\n");
2210 return;
2211 }
2212
2213 /* publish partition size & type info */
2214 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2215 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2216 }
2217}
2218
Amol Jadi5edf3552013-07-23 14:15:34 -07002219/* register commands and variables for fastboot */
2220void aboot_fastboot_register_commands(void)
2221{
2222 if (target_is_emmc_boot())
2223 {
2224 fastboot_register("flash:", cmd_flash_mmc);
2225 fastboot_register("erase:", cmd_erase_mmc);
2226 }
2227 else
2228 {
2229 fastboot_register("flash:", cmd_flash);
2230 fastboot_register("erase:", cmd_erase);
2231 }
2232
2233 fastboot_register("boot", cmd_boot);
2234 fastboot_register("continue", cmd_continue);
2235 fastboot_register("reboot", cmd_reboot);
2236 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2237 fastboot_register("oem unlock", cmd_oem_unlock);
2238 fastboot_register("oem device-info", cmd_oem_devinfo);
2239 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002240 fastboot_register("oem enable-charger-screen",
2241 cmd_oem_enable_charger_screen);
2242 fastboot_register("oem disable-charger-screen",
2243 cmd_oem_disable_charger_screen);
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302244 fastboot_register("oem select-display-panel",
2245 cmd_oem_select_display_panel);
Amol Jadi5edf3552013-07-23 14:15:34 -07002246 /* publish variables and their values */
2247 fastboot_publish("product", TARGET(BOARD));
2248 fastboot_publish("kernel", "lk");
2249 fastboot_publish("serialno", sn_buf);
2250
2251 /*
2252 * partition info is supported only for emmc partitions
2253 * Calling this for NAND prints some error messages which
2254 * is harmless but misleading. Avoid calling this for NAND
2255 * devices.
2256 */
2257 if (target_is_emmc_boot())
2258 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2259
2260 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002261 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2262 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002263 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002264 /* Is the charger screen check enabled */
2265 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2266 device.charger_screen_enabled);
2267 fastboot_publish("charger-screen-enabled",
2268 (const char *) charger_screen_enabled);
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302269 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
2270 device.display_panel);
2271 fastboot_publish("display-panel",
2272 (const char *) panel_display_mode);
Amol Jadi5edf3552013-07-23 14:15:34 -07002273}
2274
Brian Swetland9c4c0752009-01-25 16:23:50 -08002275void aboot_init(const struct app_descriptor *app)
2276{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002277 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002278 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002279
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002280 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002281 if (target_is_emmc_boot())
2282 {
2283 page_size = 2048;
2284 page_mask = page_size - 1;
2285 }
2286 else
2287 {
2288 page_size = flash_page_size();
2289 page_mask = page_size - 1;
2290 }
2291
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002292 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2293
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002294 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002295
Aravind Venkateswarandb0d5262014-02-25 16:25:30 -08002296 /* Display splash screen if enabled */
2297#if DISPLAY_SPLASH_SCREEN
2298 dprintf(SPEW, "Display Init: Start\n");
Unnati Gandhi15d08f92014-01-24 11:01:01 +05302299 target_display_init(device.display_panel);
Aravind Venkateswarandb0d5262014-02-25 16:25:30 -08002300 dprintf(SPEW, "Display Init: Done\n");
2301#endif
2302
2303
Greg Griscod6250552011-06-29 14:40:23 -07002304 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002305 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002306
Dhaval Patel223ec952013-07-18 14:49:44 -07002307 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
2308
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002309 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002310 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002311 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002312 dprintf(ALWAYS,"dload mode key sequence detected\n");
2313 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002314 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002315 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002316 }
2317 else
2318 {
2319 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002320 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002321 }
2322 boot_into_fastboot = true;
2323 }
2324 if (!boot_into_fastboot)
2325 {
2326 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2327 boot_into_recovery = 1;
2328 if (!boot_into_recovery &&
2329 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002330 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002331 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002332 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002333 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002334 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002335 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002336
Ajay Dudani77421292010-10-27 19:34:06 -07002337 reboot_mode = check_reboot_mode();
2338 if (reboot_mode == RECOVERY_MODE) {
2339 boot_into_recovery = 1;
2340 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002341 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002342 }
2343
Pavel Nedev5d91d412013-04-29 11:34:24 +03002344 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002345 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002346 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002347 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002348 if(emmc_recovery_init())
2349 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2350 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002351 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002352 if((device.is_unlocked) || (device.is_tampered))
2353 {
2354 #ifdef TZ_TAMPER_FUSE
2355 set_tamper_fuse_cmd();
2356 #endif
2357 #if USE_PCOM_SECBOOT
2358 set_tamper_flag(device.is_tampered);
2359 #endif
2360 }
Shashank Mittala0032282011-08-26 14:50:11 -07002361 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002362 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002363 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002364 else
2365 {
2366 recovery_init();
2367 #if USE_PCOM_SECBOOT
2368 if((device.is_unlocked) || (device.is_tampered))
2369 set_tamper_flag(device.is_tampered);
2370 #endif
2371 boot_linux_from_flash();
2372 }
2373 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2374 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002375 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002376
Amol Jadi5edf3552013-07-23 14:15:34 -07002377 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002378
Amol Jadi5edf3552013-07-23 14:15:34 -07002379 /* register aboot specific fastboot commands */
2380 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002381
Amol Jadi5edf3552013-07-23 14:15:34 -07002382 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002383 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002384
2385 /* initialize and start fastboot */
2386 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002387}
2388
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002389uint32_t get_page_size()
2390{
2391 return page_size;
2392}
2393
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002394/*
2395 * Calculated and save hash (SHA256) for non-signed boot image.
2396 *
2397 * Hash the same data that is checked on the signed boot image.
2398 * Kernel and Ramdisk are already read to memory buffers.
2399 * Need to read the entire device-tree from mmc
2400 * since non-signed image only read the DT tags of the relevant platform.
2401 *
2402 * @param kernel_addr - kernel bufer
2403 * @param kernel_actual - kernel size in bytes
2404 * @param ramdisk_addr - ramdisk buffer
2405 * @param ramdisk_actual - ramdisk size
2406 * @param ptn - partition
2407 * @param dt_offset - device tree offset on mmc partition
2408 * @param dt_size
2409 *
2410 * @return int - 0 on success, negative value on failure.
2411 */
2412int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2413 void *ramdisk_addr, unsigned ramdisk_actual,
2414 unsigned long long ptn,
2415 unsigned dt_offset, unsigned dt_size)
2416{
2417 SHA256_CTX sha256_ctx;
2418 char digest[32]={0};
2419 char *buf = (char *)target_get_scratch_address();
2420 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2421 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2422
2423 SHA256_Init(&sha256_ctx);
2424
2425 /* Read Boot Header */
2426 if (mmc_read(ptn, buf, page_size))
2427 {
2428 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2429 return -1;
2430 }
2431 /* Read entire Device Tree */
2432 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2433 {
2434 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2435 return -1;
2436 }
2437 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2438 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2439 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2440 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2441
2442 SHA256_Final(digest, &sha256_ctx);
2443
2444 save_kernel_hash_cmd(digest);
2445 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2446
2447 return 0;
2448}
2449
Brian Swetland9c4c0752009-01-25 16:23:50 -08002450APP_START(aboot)
2451 .init = aboot_init,
2452APP_END