blob: 79061f593ad1346547c3121da7c5c1fa8c2f9a78 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Amol Jadi2a15a272013-01-22 12:03:36 -08005 * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080038#include <kernel/thread.h>
39#include <arch/ops.h>
40
Dima Zavin214cc642009-01-26 11:16:21 -080041#include <dev/flash.h>
42#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080043#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080044#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080045#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070046#include <target.h>
47#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070048#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070049#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070050#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070051#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070052#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030053#include <sha.h>
Dima Zavin214cc642009-01-26 11:16:21 -080054
Neeti Desai17379b82012-06-04 18:42:53 -070055#if DEVICE_TREE
56#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070057#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070058#endif
59
Shashank Mittalcd98d472011-08-02 14:29:24 -070060#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080061#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080062#include "bootimg.h"
63#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070064#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070065#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070066#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070067#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080068
Shashank Mittal162244e2011-08-08 19:01:25 -070069#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070070
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070071extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070072extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070073extern void target_uninit(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070074
75void write_device_info_mmc(device_info *dev);
76void write_device_info_flash(device_info *dev);
77
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070078#define EXPAND(NAME) #NAME
79#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070080
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080081#ifdef MEMBASE
82#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
83#else
David Ng183a7422009-12-07 14:55:21 -080084#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080085#endif
86
Deepa Dinamani0e163a42013-05-24 17:08:15 -070087#ifndef MEMSIZE
88#define MEMSIZE 1024*1024
89#endif
90
91#define MAX_TAGS_SIZE 1024
92
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080093#define RECOVERY_MODE 0x77665502
94#define FASTBOOT_MODE 0x77665500
95
David Ng183a7422009-12-07 14:55:21 -080096static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080097static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +030098static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -080099static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800100static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700101static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300102static const char *secondary_gpt_enable = " gpt";
David Ng183a7422009-12-07 14:55:21 -0800103
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800104static const char *baseband_apq = " androidboot.baseband=apq";
105static const char *baseband_msm = " androidboot.baseband=msm";
106static const char *baseband_csfb = " androidboot.baseband=csfb";
107static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700108static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700109static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800110static const char *baseband_dsda = " androidboot.baseband=dsda";
111static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800112static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800113
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700114static unsigned page_size = 0;
115static unsigned page_mask = 0;
116static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
117static bool boot_into_ffbm;
118
Shashank Mittalcd98d472011-08-02 14:29:24 -0700119/* Assuming unauthorized kernel image by default */
120static int auth_kernel_img = 0;
121
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700122static device_info device = {DEVICE_MAGIC, 0, 0, 0};
Shashank Mittal162244e2011-08-08 19:01:25 -0700123
Dima Zavin42168f22009-01-30 11:52:22 -0800124struct atag_ptbl_entry
125{
126 char name[16];
127 unsigned offset;
128 unsigned size;
129 unsigned flags;
130};
131
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700132/*
133 * Partition info, required to be published
134 * for fastboot
135 */
136struct getvar_partition_info {
137 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
138 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
139 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
140 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
141 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
142};
143
144/*
145 * Right now, we are publishing the info for only
146 * three partitions
147 */
148struct getvar_partition_info part_info[] =
149{
150 { "system" , "partition-size:", "partition-type:", "", "ext4" },
151 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
152 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
153};
154
155char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700156char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800157char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700158
Greg Griscod2471ef2011-07-14 13:00:42 -0700159extern int emmc_recovery_init(void);
160
Kinson Chik0b1c8162011-08-31 16:31:57 -0700161#if NO_KEYPAD_DRIVER
162extern int fastboot_trigger(void);
163#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700164
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700165static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
166{
167 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700168#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
169 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
170 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
171 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700172#endif
173}
174
Dima Zavin42168f22009-01-30 11:52:22 -0800175static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
176{
177 struct atag_ptbl_entry atag_ptn;
178
179 memcpy(atag_ptn.name, ptn->name, 16);
180 atag_ptn.name[15] = '\0';
181 atag_ptn.offset = ptn->start;
182 atag_ptn.size = ptn->length;
183 atag_ptn.flags = ptn->flags;
184 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
185 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
186}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800187
Neeti Desaie245d492012-06-01 12:52:13 -0700188unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800189{
David Ng183a7422009-12-07 14:55:21 -0800190 int cmdline_len = 0;
191 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800192 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700193 int pause_at_bootup = 0;
Pavel Nedev5614d222013-06-17 18:01:02 +0300194 bool gpt_exists = partition_gpt_exists();
Dima Zavin42168f22009-01-30 11:52:22 -0800195
Brian Swetland9c4c0752009-01-25 16:23:50 -0800196 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800197 cmdline_len = strlen(cmdline);
198 have_cmdline = 1;
199 }
200 if (target_is_emmc_boot()) {
201 cmdline_len += strlen(emmc_cmdline);
202 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800203
204 cmdline_len += strlen(usb_sn_cmdline);
205 cmdline_len += strlen(sn_buf);
206
Pavel Nedev5614d222013-06-17 18:01:02 +0300207 if (boot_into_recovery && gpt_exists)
208 cmdline_len += strlen(secondary_gpt_enable);
209
Pavel Nedev328ac822013-04-05 15:25:11 +0300210 if (boot_into_ffbm) {
211 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700212 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800213 /* reduce kernel console messages to speed-up boot */
214 cmdline_len += strlen(loglevel);
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700215 } else if (device.charger_screen_enabled &&
216 target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700217 pause_at_bootup = 1;
218 cmdline_len += strlen(battchg_pause);
219 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800220
Shashank Mittalcd98d472011-08-02 14:29:24 -0700221 if(target_use_signed_kernel() && auth_kernel_img) {
222 cmdline_len += strlen(auth_kernel);
223 }
224
Ajay Dudanid04110c2011-01-17 23:55:07 -0800225 /* Determine correct androidboot.baseband to use */
226 switch(target_baseband())
227 {
228 case BASEBAND_APQ:
229 cmdline_len += strlen(baseband_apq);
230 break;
231
232 case BASEBAND_MSM:
233 cmdline_len += strlen(baseband_msm);
234 break;
235
236 case BASEBAND_CSFB:
237 cmdline_len += strlen(baseband_csfb);
238 break;
239
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800240 case BASEBAND_SVLTE2A:
241 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800242 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700243
244 case BASEBAND_MDM:
245 cmdline_len += strlen(baseband_mdm);
246 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700247
248 case BASEBAND_SGLTE:
249 cmdline_len += strlen(baseband_sglte);
250 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530251
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800252 case BASEBAND_SGLTE2:
253 cmdline_len += strlen(baseband_sglte2);
254 break;
255
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530256 case BASEBAND_DSDA:
257 cmdline_len += strlen(baseband_dsda);
258 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800259
260 case BASEBAND_DSDA2:
261 cmdline_len += strlen(baseband_dsda2);
262 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800263 }
264
David Ng183a7422009-12-07 14:55:21 -0800265 if (cmdline_len > 0) {
266 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700267 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
268 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700269
Amol Jadi168b7712012-03-06 16:15:00 -0800270 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700271 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800272 if (have_cmdline) {
273 src = cmdline;
274 while ((*dst++ = *src++));
275 }
276 if (target_is_emmc_boot()) {
277 src = emmc_cmdline;
278 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700279 have_cmdline = 1;
280 while ((*dst++ = *src++));
281 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800282
283 src = usb_sn_cmdline;
284 if (have_cmdline) --dst;
285 have_cmdline = 1;
286 while ((*dst++ = *src++));
287 src = sn_buf;
288 if (have_cmdline) --dst;
289 have_cmdline = 1;
290 while ((*dst++ = *src++));
291
Pavel Nedev5614d222013-06-17 18:01:02 +0300292 if (boot_into_recovery && gpt_exists) {
293 src = secondary_gpt_enable;
294 if (have_cmdline) --dst;
295 while ((*dst++ = *src++));
296 }
297
Pavel Nedev328ac822013-04-05 15:25:11 +0300298 if (boot_into_ffbm) {
299 src = androidboot_mode;
300 if (have_cmdline) --dst;
301 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700302 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300303 if (have_cmdline) --dst;
304 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800305 src = loglevel;
306 if (have_cmdline) --dst;
307 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300308 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700309 src = battchg_pause;
310 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800311 while ((*dst++ = *src++));
312 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800313
Shashank Mittalcd98d472011-08-02 14:29:24 -0700314 if(target_use_signed_kernel() && auth_kernel_img) {
315 src = auth_kernel;
316 if (have_cmdline) --dst;
317 while ((*dst++ = *src++));
318 }
319
Ajay Dudanid04110c2011-01-17 23:55:07 -0800320 switch(target_baseband())
321 {
322 case BASEBAND_APQ:
323 src = baseband_apq;
324 if (have_cmdline) --dst;
325 while ((*dst++ = *src++));
326 break;
327
328 case BASEBAND_MSM:
329 src = baseband_msm;
330 if (have_cmdline) --dst;
331 while ((*dst++ = *src++));
332 break;
333
334 case BASEBAND_CSFB:
335 src = baseband_csfb;
336 if (have_cmdline) --dst;
337 while ((*dst++ = *src++));
338 break;
339
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800340 case BASEBAND_SVLTE2A:
341 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800342 if (have_cmdline) --dst;
343 while ((*dst++ = *src++));
344 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700345
346 case BASEBAND_MDM:
347 src = baseband_mdm;
348 if (have_cmdline) --dst;
349 while ((*dst++ = *src++));
350 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700351
352 case BASEBAND_SGLTE:
353 src = baseband_sglte;
354 if (have_cmdline) --dst;
355 while ((*dst++ = *src++));
356 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530357
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800358 case BASEBAND_SGLTE2:
359 src = baseband_sglte2;
360 if (have_cmdline) --dst;
361 while ((*dst++ = *src++));
362 break;
363
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530364 case BASEBAND_DSDA:
365 src = baseband_dsda;
366 if (have_cmdline) --dst;
367 while ((*dst++ = *src++));
368 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800369
370 case BASEBAND_DSDA2:
371 src = baseband_dsda2;
372 if (have_cmdline) --dst;
373 while ((*dst++ = *src++));
374 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800375 }
Neeti Desaie245d492012-06-01 12:52:13 -0700376 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800377 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700378 return cmdline_final;
379}
380
381unsigned *atag_core(unsigned *ptr)
382{
383 /* CORE */
384 *ptr++ = 2;
385 *ptr++ = 0x54410001;
386
387 return ptr;
388
389}
390
391unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
392 unsigned ramdisk_size)
393{
394 if (ramdisk_size) {
395 *ptr++ = 4;
396 *ptr++ = 0x54420005;
397 *ptr++ = (unsigned)ramdisk;
398 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800399 }
400
Neeti Desaie245d492012-06-01 12:52:13 -0700401 return ptr;
402}
403
404unsigned *atag_ptable(unsigned **ptr_addr)
405{
406 int i;
407 struct ptable *ptable;
408
409 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700410 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
411 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700412 *(*ptr_addr)++ = 0x4d534d70;
413 for (i = 0; i < ptable->count; ++i)
414 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
415 }
416
417 return (*ptr_addr);
418}
419
420unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
421{
422 int cmdline_length = 0;
423 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700424 char *dest;
425
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800426 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700427 n = (cmdline_length + 4) & (~3);
428
429 *ptr++ = (n / 4) + 2;
430 *ptr++ = 0x54410009;
431 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800432 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700433 ptr += (n / 4);
434
435 return ptr;
436}
437
438unsigned *atag_end(unsigned *ptr)
439{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800440 /* END */
441 *ptr++ = 0;
442 *ptr++ = 0;
443
Neeti Desaie245d492012-06-01 12:52:13 -0700444 return ptr;
445}
446
447void generate_atags(unsigned *ptr, const char *cmdline,
448 void *ramdisk, unsigned ramdisk_size)
449{
450
451 ptr = atag_core(ptr);
452 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
453 ptr = target_atag_mem(ptr);
454
455 /* Skip NAND partition ATAGS for eMMC boot */
456 if (!target_is_emmc_boot()){
457 ptr = atag_ptable(&ptr);
458 }
459
460 ptr = atag_cmdline(ptr, cmdline);
461 ptr = atag_end(ptr);
462}
463
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700464typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700465void boot_linux(void *kernel, unsigned *tags,
466 const char *cmdline, unsigned machtype,
467 void *ramdisk, unsigned ramdisk_size)
468{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800469 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800470#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700471 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800472#endif
473
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700474 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800475 uint32_t tags_phys = PA((addr_t)tags);
476
477 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700478
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800479 final_cmdline = update_cmdline((const char*)cmdline);
480
Neeti Desai17379b82012-06-04 18:42:53 -0700481#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800482 dprintf(INFO, "Updating device tree: start\n");
483
Neeti Desai17379b82012-06-04 18:42:53 -0700484 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800485 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700486 if(ret)
487 {
488 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
489 ASSERT(0);
490 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800491 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700492#else
Neeti Desaie245d492012-06-01 12:52:13 -0700493 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800494 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700495#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700496
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700497 /* Perform target specific cleanup */
498 target_uninit();
499
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800500 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
501 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800502
503 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700504
Amol Jadi4421e652011-06-16 15:00:48 -0700505 /* do any platform specific cleanup before kernel entry */
506 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700507
Brian Swetland9c4c0752009-01-25 16:23:50 -0800508 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700509
Amol Jadi504f9fe2012-08-16 13:56:48 -0700510#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800511 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700512#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700513 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700514 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800515}
516
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700517/* Function to check if the memory address range falls within the aboot
518 * boundaries.
519 * start: Start of the memory region
520 * size: Size of the memory region
521 */
522int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
523{
524 /* Check for boundary conditions. */
525 if ((start + size) < start)
526 return -1;
527
528 /* Check for memory overlap. */
529 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
530 return 0;
531 else if (start > (MEMBASE + MEMSIZE))
532 return 0;
533 else
534 return -1;
535}
536
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800537#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800538
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700539BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800540#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700541BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800542#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800543
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700544static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
545{
546 int ret;
547
548 /* Assume device is rooted at this time. */
549 device.is_tampered = 1;
550
551 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
552
553 ret = image_verify((unsigned char *)bootimg_addr,
554 (unsigned char *)(bootimg_addr + bootimg_size),
555 bootimg_size,
556 CRYPTO_AUTH_ALG_SHA256);
557
558 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
559
560 if (ret)
561 {
562 /* Authorized kernel */
563 device.is_tampered = 0;
564 }
565
566#if USE_PCOM_SECBOOT
567 set_tamper_flag(device.is_tampered);
568#endif
569
570 if(device.is_tampered)
571 {
572 write_device_info_mmc(&device);
573 #ifdef TZ_TAMPER_FUSE
574 set_tamper_fuse_cmd();
575 #endif
576 #ifdef ASSERT_ON_TAMPER
577 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
578 ASSERT(0);
579 #endif
580 }
581}
582
Shashank Mittal23b8f422010-04-16 19:27:21 -0700583int boot_linux_from_mmc(void)
584{
585 struct boot_img_hdr *hdr = (void*) buf;
586 struct boot_img_hdr *uhdr;
587 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700588 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700589 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700590 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700591
Shashank Mittalcd98d472011-08-02 14:29:24 -0700592 unsigned char *image_addr = 0;
593 unsigned kernel_actual;
594 unsigned ramdisk_actual;
595 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700596 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700597
598#if DEVICE_TREE
599 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700600 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -0700601 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800602 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700603#endif
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700604 if (!boot_into_recovery) {
605 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
606 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
607 if (rcode <= 0) {
608 boot_into_ffbm = false;
609 if (rcode < 0)
610 dprintf(CRITICAL,"failed to get ffbm cookie");
611 } else
612 boot_into_ffbm = true;
613 } else
614 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700615 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
616 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
617 dprintf(INFO, "Unified boot method!\n");
618 hdr = uhdr;
619 goto unified_boot;
620 }
Greg Griscod6250552011-06-29 14:40:23 -0700621 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700622 index = partition_get_index("boot");
623 ptn = partition_get_offset(index);
624 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700625 dprintf(CRITICAL, "ERROR: No boot partition found\n");
626 return -1;
627 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700628 }
629 else {
630 index = partition_get_index("recovery");
631 ptn = partition_get_offset(index);
632 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700633 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
634 return -1;
635 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700636 }
637
Greg Griscod6250552011-06-29 14:40:23 -0700638 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700639 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
640 return -1;
641 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700642
643 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700644 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700645 return -1;
646 }
647
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700648 if (hdr->page_size && (hdr->page_size != page_size)) {
649 page_size = hdr->page_size;
650 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700651 }
652
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700653 /*
654 * Update the kernel/ramdisk/tags address if the boot image header
655 * has default values, these default values come from mkbootimg when
656 * the boot image is flashed using fastboot flash:raw
657 */
658 update_ker_tags_rdisk_addr(hdr);
659
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700660 /* Get virtual addresses since the hdr saves physical addresses. */
661 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
662 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
663 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700664
665 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
666 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
667
668 /* Check if the addresses in the header are valid. */
669 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
670 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
671 {
672 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
673 return -1;
674 }
675
676#ifndef DEVICE_TREE
677 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
678 {
679 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
680 return -1;
681 }
682#endif
683
Shashank Mittalcd98d472011-08-02 14:29:24 -0700684 /* Authenticate Kernel */
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300685 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
686 (int) target_use_signed_kernel(),
687 device.is_unlocked,
688 device.is_tampered);
689
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700690 if(target_use_signed_kernel() && (!device.is_unlocked))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800691 {
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700692 offset = 0;
693
Shashank Mittalcd98d472011-08-02 14:29:24 -0700694 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700695
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800696#if DEVICE_TREE
697 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
698 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700699
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700700 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700701 {
702 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
703 return -1;
704 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800705#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530706 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Shashank Mittal162244e2011-08-08 19:01:25 -0700707
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700708#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700709
Amol Jadib6be5c12012-11-14 13:39:51 -0800710 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700711 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800712
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700713 if (check_aboot_addr_range_overlap(image_addr, imagesize_actual))
714 {
715 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
716 return -1;
717 }
718
Shashank Mittalcd98d472011-08-02 14:29:24 -0700719 /* Read image without signature */
720 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
721 {
722 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
723 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800724 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700725
Amol Jadib6be5c12012-11-14 13:39:51 -0800726 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700727 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800728
Shashank Mittalcd98d472011-08-02 14:29:24 -0700729 offset = imagesize_actual;
Deepa Dinamanid14bb4d2013-07-12 17:28:29 -0700730
731 if (check_aboot_addr_range_overlap(image_addr + offset, page_size))
732 {
733 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
734 return -1;
735 }
736
Shashank Mittalcd98d472011-08-02 14:29:24 -0700737 /* Read signature */
738 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
739 {
740 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700741 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700742 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800743
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700744 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700745
Neeti Desai465491e2012-07-31 12:53:35 -0700746 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700747 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
748 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal162244e2011-08-08 19:01:25 -0700749
Neeti Desai465491e2012-07-31 12:53:35 -0700750 #if DEVICE_TREE
751 if(hdr->dt_size) {
752 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700753 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700754
755 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
756
Joel Kingaa335dc2013-06-03 16:11:08 -0700757 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700758 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
759 return -1;
760 }
Neeti Desai465491e2012-07-31 12:53:35 -0700761 /* Find index of device tree within device tree table */
Joel Kingaa335dc2013-06-03 16:11:08 -0700762 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700763 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
764 return -1;
765 }
766
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700767 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700768 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700769 {
770 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
771 return -1;
772 }
773
Joel Kingaa335dc2013-06-03 16:11:08 -0700774 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry.offset, dt_entry.size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800775 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800776 /*
777 * If appended dev tree is found, update the atags with
778 * memory address to the DTB appended location on RAM.
779 * Else update with the atags address in the kernel header
780 */
781 void *dtb;
782 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700783 hdr->kernel_size,
784 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800785 if (!dtb) {
786 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
787 return -1;
788 }
Neeti Desai465491e2012-07-31 12:53:35 -0700789 }
790 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700791 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700792 else
793 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800794 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700795
Amol Jadib6be5c12012-11-14 13:39:51 -0800796 dprintf(INFO, "Loading boot image (%d): start\n",
797 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700798 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800799
800 offset = page_size;
801
802 /* Load kernel */
803 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700804 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
805 return -1;
806 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800807 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700808
Amol Jadib6be5c12012-11-14 13:39:51 -0800809 /* Load ramdisk */
810 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700811 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800812 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700813 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
814 return -1;
815 }
816 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800817 offset += ramdisk_actual;
818
819 dprintf(INFO, "Loading boot image (%d): done\n",
820 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700821 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700822
823 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800824 offset += second_actual;
825 /* Second image loading not implemented. */
826 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700827 }
828
829 #if DEVICE_TREE
830 if(hdr->dt_size != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700831 /* Read the device tree table into buffer */
832 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
833 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
834 return -1;
835 }
836 table = (struct dt_table*) dt_buf;
837
Joel Kingaa335dc2013-06-03 16:11:08 -0700838 if (dev_tree_validate(table, hdr->page_size) != 0) {
Neeti Desai465491e2012-07-31 12:53:35 -0700839 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
840 return -1;
841 }
842
Joel Kingaa335dc2013-06-03 16:11:08 -0700843 /* Find index of device tree within device tree table */
844 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Neeti Desai465491e2012-07-31 12:53:35 -0700845 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
846 return -1;
847 }
848
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700849 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -0700850 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700851 {
852 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
853 return -1;
854 }
855
Joel Kingaa335dc2013-06-03 16:11:08 -0700856 if(mmc_read(ptn + offset + dt_entry.offset,
857 (void *)hdr->tags_addr, dt_entry.size)) {
Neeti Desai465491e2012-07-31 12:53:35 -0700858 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
859 return -1;
860 }
Amir Samuelov57a6fa22013-06-05 16:36:43 +0300861 #ifdef TZ_SAVE_KERNEL_HASH
862 aboot_save_boot_hash_mmc(hdr->kernel_addr, kernel_actual,
863 hdr->ramdisk_addr, ramdisk_actual,
864 ptn, offset, hdr->dt_size);
865 #endif /* TZ_SAVE_KERNEL_HASH */
866
Channagoud Kadabi35095622013-03-01 13:53:05 -0800867 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800868 /*
869 * If appended dev tree is found, update the atags with
870 * memory address to the DTB appended location on RAM.
871 * Else update with the atags address in the kernel header
872 */
873 void *dtb;
874 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Dima Zavine63e5572013-05-03 12:23:06 -0700875 kernel_actual,
876 (void *)hdr->tags_addr);
Dima Zavin77e41f32013-03-06 16:10:43 -0800877 if (!dtb) {
878 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
879 return -1;
880 }
Neeti Desai465491e2012-07-31 12:53:35 -0700881 }
882 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700883 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700884
Stanimir Varbanov69ec5462013-07-18 18:17:42 +0300885 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
886 target_load_ssd_keystore();
887
Shashank Mittal23b8f422010-04-16 19:27:21 -0700888unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700889
Dima Zavin77e41f32013-03-06 16:10:43 -0800890 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700891 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700892 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
893
894 return 0;
895}
896
Dima Zavin214cc642009-01-26 11:16:21 -0800897int boot_linux_from_flash(void)
898{
899 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800900 struct ptentry *ptn;
901 struct ptable *ptable;
902 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800903
Shashank Mittalcd98d472011-08-02 14:29:24 -0700904 unsigned char *image_addr = 0;
905 unsigned kernel_actual;
906 unsigned ramdisk_actual;
907 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800908 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700909
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700910#if DEVICE_TREE
911 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -0700912 struct dt_entry dt_entry;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800913 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700914#endif
915
David Ng183a7422009-12-07 14:55:21 -0800916 if (target_is_emmc_boot()) {
917 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
918 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
919 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
920 return -1;
921 }
922 goto continue_boot;
923 }
924
Dima Zavin214cc642009-01-26 11:16:21 -0800925 ptable = flash_get_ptable();
926 if (ptable == NULL) {
927 dprintf(CRITICAL, "ERROR: Partition table not found\n");
928 return -1;
929 }
930
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800931 if(!boot_into_recovery)
932 {
933 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700934
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800935 if (ptn == NULL) {
936 dprintf(CRITICAL, "ERROR: No boot partition found\n");
937 return -1;
938 }
939 }
940 else
941 {
942 ptn = ptable_find(ptable, "recovery");
943 if (ptn == NULL) {
944 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
945 return -1;
946 }
Dima Zavin214cc642009-01-26 11:16:21 -0800947 }
948
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800949 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800950 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
951 return -1;
952 }
Dima Zavin214cc642009-01-26 11:16:21 -0800953
954 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700955 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800956 return -1;
957 }
958
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800959 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700960 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 -0800961 return -1;
962 }
963
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700964 /*
965 * Update the kernel/ramdisk/tags address if the boot image header
966 * has default values, these default values come from mkbootimg when
967 * the boot image is flashed using fastboot flash:raw
968 */
969 update_ker_tags_rdisk_addr(hdr);
970
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700971 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700972 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
973 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
974 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
975
976 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
977 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
978
979 /* Check if the addresses in the header are valid. */
980 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
981 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
982 {
983 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
984 return -1;
985 }
986
987#ifndef DEVICE_TREE
988 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
989 {
990 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
991 return -1;
992 }
993#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700994
Shashank Mittalcd98d472011-08-02 14:29:24 -0700995 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -0700996 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700997 {
998 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700999 offset = 0;
1000
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001001#if DEVICE_TREE
1002 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1003 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001004
1005 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1006 {
1007 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1008 return -1;
1009 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001010#else
Shashank Mittalcd98d472011-08-02 14:29:24 -07001011 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001012#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001013
Amol Jadib6be5c12012-11-14 13:39:51 -08001014 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001015 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001016
Shashank Mittalcd98d472011-08-02 14:29:24 -07001017 /* Read image without signature */
1018 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1019 {
1020 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1021 return -1;
1022 }
Dima Zavin214cc642009-01-26 11:16:21 -08001023
Amol Jadib6be5c12012-11-14 13:39:51 -08001024 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001025 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001026
Shashank Mittalcd98d472011-08-02 14:29:24 -07001027 offset = imagesize_actual;
1028 /* Read signature */
1029 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1030 {
1031 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001032 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001033 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001034
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001035 verify_signed_bootimg(image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001036
1037 /* Move kernel and ramdisk to correct address */
1038 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
1039 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001040#if DEVICE_TREE
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001041 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001042 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001043 {
1044 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1045 return -1;
1046 }
1047
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001048 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
1049#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001050
1051 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001052 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001053 {
1054 write_device_info_flash(&device);
1055 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301056#if USE_PCOM_SECBOOT
1057 set_tamper_flag(device.is_tampered);
1058#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001059 }
1060 else
1061 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001062 offset = page_size;
1063
Amol Jadib6be5c12012-11-14 13:39:51 -08001064 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1065 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1066 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1067
1068 dprintf(INFO, "Loading boot image (%d): start\n",
1069 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001070 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001071
1072 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001073 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1074 return -1;
1075 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001076 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001077
Amol Jadib6be5c12012-11-14 13:39:51 -08001078 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001079 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1080 return -1;
1081 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001082 offset += ramdisk_actual;
1083
1084 dprintf(INFO, "Loading boot image (%d): done\n",
1085 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001086 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001087
1088 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001089 offset += second_actual;
1090 /* Second image loading not implemented. */
1091 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001092 }
1093
1094#if DEVICE_TREE
1095 if(hdr->dt_size != 0) {
1096
1097 /* Read the device tree table into buffer */
1098 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1099 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1100 return -1;
1101 }
1102
1103 table = (struct dt_table*) dt_buf;
1104
Joel Kingaa335dc2013-06-03 16:11:08 -07001105 if (dev_tree_validate(table, hdr->page_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001106 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1107 return -1;
1108 }
1109
Joel Kingaa335dc2013-06-03 16:11:08 -07001110 /* Find index of device tree within device tree table */
1111 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001112 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1113 return -1;
1114 }
1115
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001116 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001117 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001118 {
1119 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1120 return -1;
1121 }
1122
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001123 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001124 if(flash_read(ptn, offset + dt_entry.offset,
1125 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001126 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1127 return -1;
1128 }
1129 }
1130#endif
1131
Shashank Mittalcd98d472011-08-02 14:29:24 -07001132 }
David Ng183a7422009-12-07 14:55:21 -08001133continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001134
Dima Zavin214cc642009-01-26 11:16:21 -08001135 /* TODO: create/pass atags to kernel */
1136
Ajay Dudanie28a6072011-07-01 13:59:46 -07001137 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001138 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001139 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1140
1141 return 0;
1142}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001143
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001144BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001145void write_device_info_mmc(device_info *dev)
1146{
1147 struct device_info *info = (void*) info_buf;
1148 unsigned long long ptn = 0;
1149 unsigned long long size;
1150 int index = INVALID_PTN;
1151
1152 index = partition_get_index("aboot");
1153 ptn = partition_get_offset(index);
1154 if(ptn == 0)
1155 {
1156 return;
1157 }
1158
1159 size = partition_get_size(index);
1160
1161 memcpy(info, dev, sizeof(device_info));
1162
1163 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1164 {
1165 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1166 return;
1167 }
1168}
1169
1170void read_device_info_mmc(device_info *dev)
1171{
1172 struct device_info *info = (void*) info_buf;
1173 unsigned long long ptn = 0;
1174 unsigned long long size;
1175 int index = INVALID_PTN;
1176
1177 index = partition_get_index("aboot");
1178 ptn = partition_get_offset(index);
1179 if(ptn == 0)
1180 {
1181 return;
1182 }
1183
1184 size = partition_get_size(index);
1185
1186 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1187 {
1188 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1189 return;
1190 }
1191
1192 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1193 {
1194 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1195 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001196 info->is_tampered = 0;
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001197 info->charger_screen_enabled = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001198
1199 write_device_info_mmc(info);
1200 }
1201 memcpy(dev, info, sizeof(device_info));
1202}
1203
1204void write_device_info_flash(device_info *dev)
1205{
1206 struct device_info *info = (void *) info_buf;
1207 struct ptentry *ptn;
1208 struct ptable *ptable;
1209
1210 ptable = flash_get_ptable();
1211 if (ptable == NULL)
1212 {
1213 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1214 return;
1215 }
1216
1217 ptn = ptable_find(ptable, "devinfo");
1218 if (ptn == NULL)
1219 {
1220 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1221 return;
1222 }
1223
1224 memcpy(info, dev, sizeof(device_info));
1225
1226 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1227 {
1228 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1229 return;
1230 }
1231}
1232
1233void read_device_info_flash(device_info *dev)
1234{
1235 struct device_info *info = (void*) info_buf;
1236 struct ptentry *ptn;
1237 struct ptable *ptable;
1238
1239 ptable = flash_get_ptable();
1240 if (ptable == NULL)
1241 {
1242 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1243 return;
1244 }
1245
1246 ptn = ptable_find(ptable, "devinfo");
1247 if (ptn == NULL)
1248 {
1249 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1250 return;
1251 }
1252
1253 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1254 {
1255 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1256 return;
1257 }
1258
1259 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1260 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001261 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1262 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001263 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001264 write_device_info_flash(info);
1265 }
1266 memcpy(dev, info, sizeof(device_info));
1267}
1268
1269void write_device_info(device_info *dev)
1270{
1271 if(target_is_emmc_boot())
1272 {
1273 write_device_info_mmc(dev);
1274 }
1275 else
1276 {
1277 write_device_info_flash(dev);
1278 }
1279}
1280
1281void read_device_info(device_info *dev)
1282{
1283 if(target_is_emmc_boot())
1284 {
1285 read_device_info_mmc(dev);
1286 }
1287 else
1288 {
1289 read_device_info_flash(dev);
1290 }
1291}
1292
1293void reset_device_info()
1294{
1295 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001296 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001297 write_device_info(&device);
1298}
1299
1300void set_device_root()
1301{
1302 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001303 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001304 write_device_info(&device);
1305}
1306
Amol Jadicb524072012-08-09 16:40:18 -07001307#if DEVICE_TREE
1308int copy_dtb(uint8_t *boot_image_start)
1309{
1310 uint32 dt_image_offset = 0;
1311 uint32_t n;
1312 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001313 struct dt_entry dt_entry;
Amol Jadicb524072012-08-09 16:40:18 -07001314
1315 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1316
Amol Jadicb524072012-08-09 16:40:18 -07001317 if(hdr->dt_size != 0) {
1318
1319 /* add kernel offset */
1320 dt_image_offset += page_size;
1321 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1322 dt_image_offset += n;
1323
1324 /* add ramdisk offset */
1325 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1326 dt_image_offset += n;
1327
1328 /* add second offset */
1329 if(hdr->second_size != 0) {
1330 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1331 dt_image_offset += n;
1332 }
1333
1334 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001335 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001336
Joel Kingaa335dc2013-06-03 16:11:08 -07001337 if (dev_tree_validate(table, hdr->page_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07001338 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1339 return -1;
1340 }
Joel Kingaa335dc2013-06-03 16:11:08 -07001341 /* Find index of device tree within device tree table */
1342 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07001343 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1344 return -1;
1345 }
1346
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001347 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001348 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001349 {
1350 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1351 return -1;
1352 }
1353
Amol Jadicb524072012-08-09 16:40:18 -07001354 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001355 memmove((void*) hdr->tags_addr,
Joel Kingaa335dc2013-06-03 16:11:08 -07001356 boot_image_start + dt_image_offset + dt_entry.offset,
1357 dt_entry.size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001358 } else
1359 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001360
1361 /* Everything looks fine. Return success. */
1362 return 0;
1363}
1364#endif
1365
Brian Swetland9c4c0752009-01-25 16:23:50 -08001366void cmd_boot(const char *arg, void *data, unsigned sz)
1367{
1368 unsigned kernel_actual;
1369 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001370 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001371 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001372 int ret = 0;
1373 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001374
1375 if (sz < sizeof(hdr)) {
1376 fastboot_fail("invalid bootimage header");
1377 return;
1378 }
1379
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001380 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001381
1382 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001383 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001384
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001385 if(target_is_emmc_boot() && hdr->page_size) {
1386 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001387 page_mask = page_size - 1;
1388 }
1389
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001390 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1391 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1392
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001393 /*
1394 * Update the kernel/ramdisk/tags address if the boot image header
1395 * has default values, these default values come from mkbootimg when
1396 * the boot image is flashed using fastboot flash:raw
1397 */
1398 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001399
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001400 /* Get virtual addresses since the hdr saves physical addresses. */
1401 hdr->kernel_addr = VA(hdr->kernel_addr);
1402 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1403 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001404
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001405 /* Check if the addresses in the header are valid. */
1406 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1407 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1408 {
1409 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1410 return -1;
1411 }
1412
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001413 /* sz should have atleast raw boot image */
1414 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001415 fastboot_fail("incomplete bootimage");
1416 return;
1417 }
1418
Amol Jadicb524072012-08-09 16:40:18 -07001419#if DEVICE_TREE
1420 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001421 ret = copy_dtb(data);
1422
1423 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001424#else
1425 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1426 {
1427 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1428 return -1;
1429 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001430#endif
1431
1432 /* Load ramdisk & kernel */
1433 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1434 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1435
1436#if DEVICE_TREE
1437 /*
1438 * If dtb is not found look for appended DTB in the kernel.
1439 * If appended dev tree is found, update the atags with
1440 * memory address to the DTB appended location on RAM.
1441 * Else update with the atags address in the kernel header
1442 */
1443 if (!dtb_copied) {
1444 void *dtb;
Dima Zavine63e5572013-05-03 12:23:06 -07001445 dtb = dev_tree_appended((void *)hdr->kernel_addr, hdr->kernel_size,
1446 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001447 if (!dtb) {
1448 fastboot_fail("dtb not found");
1449 return;
1450 }
Amol Jadicb524072012-08-09 16:40:18 -07001451 }
1452#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001453
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001454#ifndef DEVICE_TREE
1455 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1456 {
1457 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1458 return -1;
1459 }
1460#endif
1461
Brian Swetland9c4c0752009-01-25 16:23:50 -08001462 fastboot_okay("");
1463 udc_stop();
1464
Dima Zavin77e41f32013-03-06 16:10:43 -08001465 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001466 (const char*) hdr->cmdline, board_machtype(),
1467 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001468}
1469
Dima Zavin214cc642009-01-26 11:16:21 -08001470void cmd_erase(const char *arg, void *data, unsigned sz)
1471{
1472 struct ptentry *ptn;
1473 struct ptable *ptable;
1474
1475 ptable = flash_get_ptable();
1476 if (ptable == NULL) {
1477 fastboot_fail("partition table doesn't exist");
1478 return;
1479 }
1480
1481 ptn = ptable_find(ptable, arg);
1482 if (ptn == NULL) {
1483 fastboot_fail("unknown partition name");
1484 return;
1485 }
1486
1487 if (flash_erase(ptn)) {
1488 fastboot_fail("failed to erase partition");
1489 return;
1490 }
1491 fastboot_okay("");
1492}
1493
Bikas Gurungd48bd242010-09-04 19:54:32 -07001494
1495void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1496{
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001497 BUF_DMA_ALIGN(out, 512);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001498 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001499 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001500
Kinson Chikf1a43512011-07-14 11:28:39 -07001501 index = partition_get_index(arg);
1502 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001503
Kinson Chikf1a43512011-07-14 11:28:39 -07001504 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001505 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001506 return;
1507 }
neetidb4b24d62012-01-20 12:13:09 -08001508 /* Simple inefficient version of erase. Just writing
1509 0 in first block */
1510 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1511 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001512 return;
1513 }
1514 fastboot_okay("");
1515}
1516
1517
Ajay Dudani5c761132011-04-07 20:19:04 -07001518void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001519{
1520 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001521 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001522 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001523
Greg Grisco6e754772011-06-23 12:19:39 -07001524 if (!strcmp(arg, "partition"))
1525 {
1526 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001527 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001528 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001529 return;
1530 }
1531 }
Greg Grisco6e754772011-06-23 12:19:39 -07001532 else
1533 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001534 index = partition_get_index(arg);
1535 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001536 if(ptn == 0) {
1537 fastboot_fail("partition table doesn't exist");
1538 return;
1539 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001540
Greg Grisco6e754772011-06-23 12:19:39 -07001541 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1542 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1543 fastboot_fail("image is not a boot image");
1544 return;
1545 }
1546 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001547
Kinson Chikf1a43512011-07-14 11:28:39 -07001548 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001549 if (ROUND_TO_PAGE(sz,511) > size) {
1550 fastboot_fail("size too large");
1551 return;
1552 }
1553 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1554 fastboot_fail("flash write failure");
1555 return;
1556 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001557 }
1558 fastboot_okay("");
1559 return;
1560}
1561
Ajay Dudani5c761132011-04-07 20:19:04 -07001562void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1563{
1564 unsigned int chunk;
1565 unsigned int chunk_data_sz;
1566 sparse_header_t *sparse_header;
1567 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001568 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001569 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301570 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001571 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001572
Kinson Chikf1a43512011-07-14 11:28:39 -07001573 index = partition_get_index(arg);
1574 ptn = partition_get_offset(index);
1575 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001576 fastboot_fail("partition table doesn't exist");
1577 return;
1578 }
1579
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301580 size = partition_get_size(index);
1581 if (ROUND_TO_PAGE(sz,511) > size) {
1582 fastboot_fail("size too large");
1583 return;
1584 }
1585
Ajay Dudani5c761132011-04-07 20:19:04 -07001586 /* Read and skip over sparse image header */
1587 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001588 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1589 fastboot_fail("size too large");
1590 return;
1591 }
1592
Ajay Dudani5c761132011-04-07 20:19:04 -07001593 data += sparse_header->file_hdr_sz;
1594 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1595 {
1596 /* Skip the remaining bytes in a header that is longer than
1597 * we expected.
1598 */
1599 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1600 }
1601
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001602 dprintf (SPEW, "=== Sparse Image Header ===\n");
1603 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1604 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1605 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1606 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1607 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1608 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1609 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1610 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001611
1612 /* Start processing chunks */
1613 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1614 {
1615 /* Read and skip over chunk header */
1616 chunk_header = (chunk_header_t *) data;
1617 data += sizeof(chunk_header_t);
1618
1619 dprintf (SPEW, "=== Chunk Header ===\n");
1620 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1621 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1622 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1623
1624 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1625 {
1626 /* Skip the remaining bytes in a header that is longer than
1627 * we expected.
1628 */
1629 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1630 }
1631
1632 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1633 switch (chunk_header->chunk_type)
1634 {
1635 case CHUNK_TYPE_RAW:
1636 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1637 chunk_data_sz))
1638 {
1639 fastboot_fail("Bogus chunk size for chunk type Raw");
1640 return;
1641 }
1642
Ajay Dudaniab18f022011-05-12 14:39:22 -07001643 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1644 chunk_data_sz,
1645 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001646 {
1647 fastboot_fail("flash write failure");
1648 return;
1649 }
1650 total_blocks += chunk_header->chunk_sz;
1651 data += chunk_data_sz;
1652 break;
1653
1654 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001655 total_blocks += chunk_header->chunk_sz;
1656 break;
1657
Ajay Dudani5c761132011-04-07 20:19:04 -07001658 case CHUNK_TYPE_CRC:
1659 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1660 {
1661 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1662 return;
1663 }
1664 total_blocks += chunk_header->chunk_sz;
1665 data += chunk_data_sz;
1666 break;
1667
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001668 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001669 fastboot_fail("Unknown chunk type");
1670 return;
1671 }
1672 }
1673
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001674 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1675 total_blocks, sparse_header->total_blks);
1676
1677 if(total_blocks != sparse_header->total_blks)
1678 {
1679 fastboot_fail("sparse image write failure");
1680 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001681
1682 fastboot_okay("");
1683 return;
1684}
1685
1686void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1687{
1688 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001689 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1690 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001691
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001692#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001693 int ret=0;
1694 uint32 major_version=0;
1695 uint32 minor_version=0;
1696
1697 ret = scm_svc_version(&major_version,&minor_version);
1698 if(!ret)
1699 {
1700 if(major_version >= 2)
1701 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001702 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001703 {
1704 ret = encrypt_scm((uint32 **) &data, &sz);
1705 if (ret != 0) {
1706 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1707 return;
1708 }
1709
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001710 /* Protect only for SSD */
1711 if (!strcmp(arg, "ssd")) {
1712 ret = scm_protect_keystore((uint32 *) data, sz);
1713 if (ret != 0) {
1714 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1715 return;
1716 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001717 }
1718 }
1719 else
1720 {
1721 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1722 if(ret != 0)
1723 {
1724 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1725 return;
1726 }
1727 }
1728 }
1729 else
1730 {
1731 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1732 magic_number[1] == DECRYPT_MAGIC_1)
1733 {
1734 ret = decrypt_scm((uint32 **) &data, &sz);
1735 if (ret != 0) {
1736 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1737 return;
1738 }
1739 }
1740 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1741 magic_number[1] == ENCRYPT_MAGIC_1)
1742 {
1743 ret = encrypt_scm((uint32 **) &data, &sz);
1744 if (ret != 0) {
1745 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1746 return;
1747 }
1748 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001749 }
1750 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001751 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001752 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001753 dprintf(CRITICAL,"INVALID SVC Version\n");
1754 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001755 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001756#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001757
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001758 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001759 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1760 cmd_flash_mmc_img(arg, data, sz);
1761 else
1762 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001763 return;
1764}
1765
Dima Zavin214cc642009-01-26 11:16:21 -08001766void cmd_flash(const char *arg, void *data, unsigned sz)
1767{
1768 struct ptentry *ptn;
1769 struct ptable *ptable;
1770 unsigned extra = 0;
1771
1772 ptable = flash_get_ptable();
1773 if (ptable == NULL) {
1774 fastboot_fail("partition table doesn't exist");
1775 return;
1776 }
1777
1778 ptn = ptable_find(ptable, arg);
1779 if (ptn == NULL) {
1780 fastboot_fail("unknown partition name");
1781 return;
1782 }
1783
1784 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1785 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1786 fastboot_fail("image is not a boot image");
1787 return;
1788 }
1789 }
1790
Amol Jadi5c61a952012-05-04 17:05:35 -07001791 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001792 || !strcmp(ptn->name, "userdata")
1793 || !strcmp(ptn->name, "persist")
1794 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301795 if (flash_ecc_bch_enabled())
1796 /* Spare data bytes for 8 bit ECC increased by 4 */
1797 extra = ((page_size >> 9) * 20);
1798 else
1799 extra = ((page_size >> 9) * 16);
1800 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001801 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001802
1803 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1804 if (flash_write(ptn, extra, data, sz)) {
1805 fastboot_fail("flash write failure");
1806 return;
1807 }
1808 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1809 fastboot_okay("");
1810}
1811
1812void cmd_continue(const char *arg, void *data, unsigned sz)
1813{
1814 fastboot_okay("");
1815 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001816 if (target_is_emmc_boot())
1817 {
1818 boot_linux_from_mmc();
1819 }
1820 else
1821 {
1822 boot_linux_from_flash();
1823 }
Dima Zavin214cc642009-01-26 11:16:21 -08001824}
1825
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001826void cmd_reboot(const char *arg, void *data, unsigned sz)
1827{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001828 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001829 fastboot_okay("");
1830 reboot_device(0);
1831}
1832
1833void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1834{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001835 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001836 fastboot_okay("");
1837 reboot_device(FASTBOOT_MODE);
1838}
1839
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001840void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
1841{
1842 dprintf(INFO, "Enabling charger screen check\n");
1843 device.charger_screen_enabled = 1;
1844 write_device_info(&device);
1845 fastboot_okay("");
1846}
1847
1848void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
1849{
1850 dprintf(INFO, "Disabling charger screen check\n");
1851 device.charger_screen_enabled = 0;
1852 write_device_info(&device);
1853 fastboot_okay("");
1854}
1855
Shashank Mittal162244e2011-08-08 19:01:25 -07001856void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1857{
1858 if(!device.is_unlocked)
1859 {
1860 device.is_unlocked = 1;
1861 write_device_info(&device);
1862 }
1863 fastboot_okay("");
1864}
1865
Shashank Mittala0032282011-08-26 14:50:11 -07001866void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1867{
1868 char response[64];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001869 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07001870 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07001871 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1872 fastboot_info(response);
1873 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07001874 fastboot_info(response);
1875 fastboot_okay("");
1876}
1877
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001878void cmd_preflash(const char *arg, void *data, unsigned sz)
1879{
1880 fastboot_okay("");
1881}
1882
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05301883static struct fbimage logo_header = {0};
1884struct fbimage* splash_screen_flash();
1885
1886int splash_screen_check_header(struct fbimage *logo)
1887{
1888 if (memcmp(logo->header.magic, LOGO_IMG_MAGIC, 8))
1889 return -1;
1890 if (logo->header.width == 0 || logo->header.height == 0)
1891 return -1;
1892 return 0;
1893}
1894
1895struct fbimage* splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001896{
1897 struct ptentry *ptn;
1898 struct ptable *ptable;
1899 struct fbcon_config *fb_display = NULL;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05301900 struct fbimage *logo = &logo_header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001901
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05301902
1903 ptable = flash_get_ptable();
1904 if (ptable == NULL) {
1905 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1906 return NULL;
1907 }
1908 ptn = ptable_find(ptable, "splash");
1909 if (ptn == NULL) {
1910 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
1911 return NULL;
1912 }
1913
1914 if (flash_read(ptn, 0,(unsigned int *) logo, sizeof(logo->header))) {
1915 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1916 return NULL;
1917 }
1918
1919 if (splash_screen_check_header(logo)) {
1920 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
1921 return NULL;
1922 }
1923
1924 fb_display = fbcon_display();
1925 if (fb_display) {
1926 uint8_t *base = (uint8_t *) fb_display->base;
1927 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height) {
1928 base += LOGO_IMG_OFFSET;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001929 }
1930
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05301931 if (flash_read(ptn + sizeof(logo->header), 0,
1932 base,
1933 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
1934 fbcon_clear();
1935 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1936 return NULL;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001937 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05301938 logo->image = base;
1939 }
1940
1941 return logo;
1942}
1943
1944struct fbimage* splash_screen_mmc()
1945{
1946 int index = INVALID_PTN;
1947 unsigned long long ptn = 0;
1948 struct fbcon_config *fb_display = NULL;
1949 struct fbimage *logo = &logo_header;
1950
1951 index = partition_get_index("splash");
1952 if (index == 0) {
1953 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
1954 return NULL;
1955 }
1956
1957 ptn = partition_get_offset(index);
1958 if (ptn == 0) {
1959 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
1960 return NULL;
1961 }
1962
1963 if (mmc_read(ptn, (unsigned int *) logo, sizeof(logo->header))) {
1964 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
1965 return NULL;
1966 }
1967
1968 if (splash_screen_check_header(logo)) {
1969 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
1970 return NULL;
1971 }
1972
1973 fb_display = fbcon_display();
1974 if (fb_display) {
1975 uint8_t *base = (uint8_t *) fb_display->base;
1976 if (logo->header.width != fb_display->width || logo->header.height != fb_display->height)
1977 base += LOGO_IMG_OFFSET;
1978
1979 if (mmc_read(ptn + sizeof(logo->header),
1980 base,
1981 ((((logo->header.width * logo->header.height * fb_display->bpp/8) + 511) >> 9) << 9))) {
1982 fbcon_clear();
1983 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1984 return NULL;
1985 }
1986
1987 logo->image = base;
1988 }
1989
1990 return logo;
1991}
1992
1993
1994struct fbimage* fetch_image_from_partition()
1995{
1996 if (target_is_emmc_boot()) {
1997 return splash_screen_mmc();
1998 } else {
1999 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002000 }
2001}
2002
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002003/* Get the size from partiton name */
2004static void get_partition_size(const char *arg, char *response)
2005{
2006 uint64_t ptn = 0;
2007 uint64_t size;
2008 int index = INVALID_PTN;
2009
2010 index = partition_get_index(arg);
2011
2012 if (index == INVALID_PTN)
2013 {
2014 dprintf(CRITICAL, "Invalid partition index\n");
2015 return;
2016 }
2017
2018 ptn = partition_get_offset(index);
2019
2020 if(!ptn)
2021 {
2022 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2023 return;
2024 }
2025
2026 size = partition_get_size(index);
2027
2028 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2029 return;
2030}
2031
2032/*
2033 * Publish the partition type & size info
2034 * fastboot getvar will publish the required information.
2035 * fastboot getvar partition_size:<partition_name>: partition size in hex
2036 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2037 */
2038static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2039{
2040 uint8_t i;
2041
2042 for (i = 0; i < num_parts; i++) {
2043 get_partition_size(info[i].part_name, info[i].size_response);
2044
2045 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2046 {
2047 dprintf(CRITICAL, "partition size name truncated\n");
2048 return;
2049 }
2050 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2051 {
2052 dprintf(CRITICAL, "partition type name truncated\n");
2053 return;
2054 }
2055
2056 /* publish partition size & type info */
2057 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2058 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2059 }
2060}
2061
Amol Jadi5edf3552013-07-23 14:15:34 -07002062/* register commands and variables for fastboot */
2063void aboot_fastboot_register_commands(void)
2064{
2065 if (target_is_emmc_boot())
2066 {
2067 fastboot_register("flash:", cmd_flash_mmc);
2068 fastboot_register("erase:", cmd_erase_mmc);
2069 }
2070 else
2071 {
2072 fastboot_register("flash:", cmd_flash);
2073 fastboot_register("erase:", cmd_erase);
2074 }
2075
2076 fastboot_register("boot", cmd_boot);
2077 fastboot_register("continue", cmd_continue);
2078 fastboot_register("reboot", cmd_reboot);
2079 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
2080 fastboot_register("oem unlock", cmd_oem_unlock);
2081 fastboot_register("oem device-info", cmd_oem_devinfo);
2082 fastboot_register("preflash", cmd_preflash);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002083 fastboot_register("oem enable-charger-screen",
2084 cmd_oem_enable_charger_screen);
2085 fastboot_register("oem disable-charger-screen",
2086 cmd_oem_disable_charger_screen);
Amol Jadi5edf3552013-07-23 14:15:34 -07002087 /* publish variables and their values */
2088 fastboot_publish("product", TARGET(BOARD));
2089 fastboot_publish("kernel", "lk");
2090 fastboot_publish("serialno", sn_buf);
2091
2092 /*
2093 * partition info is supported only for emmc partitions
2094 * Calling this for NAND prints some error messages which
2095 * is harmless but misleading. Avoid calling this for NAND
2096 * devices.
2097 */
2098 if (target_is_emmc_boot())
2099 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
2100
2101 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002102 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
2103 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07002104 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002105 /* Is the charger screen check enabled */
2106 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
2107 device.charger_screen_enabled);
2108 fastboot_publish("charger-screen-enabled",
2109 (const char *) charger_screen_enabled);
Amol Jadi5edf3552013-07-23 14:15:34 -07002110}
2111
Brian Swetland9c4c0752009-01-25 16:23:50 -08002112void aboot_init(const struct app_descriptor *app)
2113{
Shashank Mittal4f99a882010-02-01 13:58:50 -08002114 unsigned reboot_mode = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03002115 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07002116
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002117 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002118 if (target_is_emmc_boot())
2119 {
2120 page_size = 2048;
2121 page_mask = page_size - 1;
2122 }
2123 else
2124 {
2125 page_size = flash_page_size();
2126 page_mask = page_size - 1;
2127 }
2128
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002129 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
2130
Ameya Thakur11cf1a62013-08-05 12:44:48 -07002131 read_device_info(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07002132
Greg Griscod6250552011-06-29 14:40:23 -07002133 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002134 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08002135
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002136 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002137 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002138 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002139 dprintf(ALWAYS,"dload mode key sequence detected\n");
2140 if (set_download_mode(EMERGENCY_DLOAD))
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002141 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002142 dprintf(CRITICAL,"dload mode not supported by target\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002143 }
2144 else
2145 {
2146 reboot_device(0);
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03002147 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
Ameya Thakur0b9c2442013-05-10 13:22:56 -07002148 }
2149 boot_into_fastboot = true;
2150 }
2151 if (!boot_into_fastboot)
2152 {
2153 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
2154 boot_into_recovery = 1;
2155 if (!boot_into_recovery &&
2156 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03002157 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07002158 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002159 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07002160 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03002161 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002162 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07002163
Ajay Dudani77421292010-10-27 19:34:06 -07002164 reboot_mode = check_reboot_mode();
2165 if (reboot_mode == RECOVERY_MODE) {
2166 boot_into_recovery = 1;
2167 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002168 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07002169 }
2170
Pavel Nedev5d91d412013-04-29 11:34:24 +03002171 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002172 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002173 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07002174 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002175 if(emmc_recovery_init())
2176 dprintf(ALWAYS,"error in emmc_recovery_init\n");
2177 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07002178 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03002179 if((device.is_unlocked) || (device.is_tampered))
2180 {
2181 #ifdef TZ_TAMPER_FUSE
2182 set_tamper_fuse_cmd();
2183 #endif
2184 #if USE_PCOM_SECBOOT
2185 set_tamper_flag(device.is_tampered);
2186 #endif
2187 }
Shashank Mittala0032282011-08-26 14:50:11 -07002188 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002189 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07002190 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03002191 else
2192 {
2193 recovery_init();
2194 #if USE_PCOM_SECBOOT
2195 if((device.is_unlocked) || (device.is_tampered))
2196 set_tamper_flag(device.is_tampered);
2197 #endif
2198 boot_linux_from_flash();
2199 }
2200 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
2201 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07002202 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07002203
Amol Jadi5edf3552013-07-23 14:15:34 -07002204 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07002205
Amol Jadi5edf3552013-07-23 14:15:34 -07002206 /* register aboot specific fastboot commands */
2207 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07002208
Amol Jadi5edf3552013-07-23 14:15:34 -07002209 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07002210 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07002211
2212 /* initialize and start fastboot */
2213 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Brian Swetland9c4c0752009-01-25 16:23:50 -08002214}
2215
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07002216uint32_t get_page_size()
2217{
2218 return page_size;
2219}
2220
Amir Samuelov57a6fa22013-06-05 16:36:43 +03002221/*
2222 * Calculated and save hash (SHA256) for non-signed boot image.
2223 *
2224 * Hash the same data that is checked on the signed boot image.
2225 * Kernel and Ramdisk are already read to memory buffers.
2226 * Need to read the entire device-tree from mmc
2227 * since non-signed image only read the DT tags of the relevant platform.
2228 *
2229 * @param kernel_addr - kernel bufer
2230 * @param kernel_actual - kernel size in bytes
2231 * @param ramdisk_addr - ramdisk buffer
2232 * @param ramdisk_actual - ramdisk size
2233 * @param ptn - partition
2234 * @param dt_offset - device tree offset on mmc partition
2235 * @param dt_size
2236 *
2237 * @return int - 0 on success, negative value on failure.
2238 */
2239int aboot_save_boot_hash_mmc(void *kernel_addr, unsigned kernel_actual,
2240 void *ramdisk_addr, unsigned ramdisk_actual,
2241 unsigned long long ptn,
2242 unsigned dt_offset, unsigned dt_size)
2243{
2244 SHA256_CTX sha256_ctx;
2245 char digest[32]={0};
2246 char *buf = (char *)target_get_scratch_address();
2247 unsigned dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
2248 unsigned imagesize_actual = page_size + kernel_actual + ramdisk_actual + dt_actual;
2249
2250 SHA256_Init(&sha256_ctx);
2251
2252 /* Read Boot Header */
2253 if (mmc_read(ptn, buf, page_size))
2254 {
2255 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2256 return -1;
2257 }
2258 /* Read entire Device Tree */
2259 if (mmc_read(ptn + dt_offset, buf+page_size, dt_actual))
2260 {
2261 dprintf(CRITICAL, "ERROR: mmc_read() fail.\n");
2262 return -1;
2263 }
2264 SHA256_Update(&sha256_ctx, buf, page_size); // Boot Header
2265 SHA256_Update(&sha256_ctx, kernel_addr, kernel_actual);
2266 SHA256_Update(&sha256_ctx, ramdisk_addr, ramdisk_actual);
2267 SHA256_Update(&sha256_ctx, buf+page_size, dt_actual); // Device Tree
2268
2269 SHA256_Final(digest, &sha256_ctx);
2270
2271 save_kernel_hash_cmd(digest);
2272 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) imagesize_actual);
2273
2274 return 0;
2275}
2276
Brian Swetland9c4c0752009-01-25 16:23:50 -08002277APP_START(aboot)
2278 .init = aboot_init,
2279APP_END