blob: 48724d071aa96ed893f41c06a979ac2f6e897c29 [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>
36#include <dev/udc.h>
37#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070038#include <stdlib.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
43#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080044#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080045#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080046#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070047#include <target.h>
48#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070049#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070050#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070051#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070052#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070053#include <boot_stats.h>
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);
72extern void dsb();
73extern void isb();
74extern void platform_uninit(void);
75
76void write_device_info_mmc(device_info *dev);
77void write_device_info_flash(device_info *dev);
78
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070079#define EXPAND(NAME) #NAME
80#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -070081
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080082#ifdef MEMBASE
83#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
84#else
David Ng183a7422009-12-07 14:55:21 -080085#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080086#endif
87
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080088#define RECOVERY_MODE 0x77665502
89#define FASTBOOT_MODE 0x77665500
90
David Ng183a7422009-12-07 14:55:21 -080091static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080092static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +030093static const char *androidboot_mode = " androidboot.mode=";
Pavel Nedev898298c2013-02-27 12:36:09 -080094static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -080095static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -070096static const char *auth_kernel = " androidboot.authorized_kernel=true";
David Ng183a7422009-12-07 14:55:21 -080097
Ajay Dudani6cff85e2011-02-04 16:02:16 -080098static const char *baseband_apq = " androidboot.baseband=apq";
99static const char *baseband_msm = " androidboot.baseband=msm";
100static const char *baseband_csfb = " androidboot.baseband=csfb";
101static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700102static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700103static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800104static const char *baseband_dsda = " androidboot.baseband=dsda";
105static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800106static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800107
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700108static unsigned page_size = 0;
109static unsigned page_mask = 0;
110static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
111static bool boot_into_ffbm;
112
Shashank Mittalcd98d472011-08-02 14:29:24 -0700113/* Assuming unauthorized kernel image by default */
114static int auth_kernel_img = 0;
115
Shashank Mittal162244e2011-08-08 19:01:25 -0700116static device_info device = {DEVICE_MAGIC, 0, 0};
117
Brian Swetland9c4c0752009-01-25 16:23:50 -0800118static struct udc_device surf_udc_device = {
119 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -0800120 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800121 .version_id = 0x0100,
122 .manufacturer = "Google",
123 .product = "Android",
124};
125
Dima Zavin42168f22009-01-30 11:52:22 -0800126struct atag_ptbl_entry
127{
128 char name[16];
129 unsigned offset;
130 unsigned size;
131 unsigned flags;
132};
133
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700134/*
135 * Partition info, required to be published
136 * for fastboot
137 */
138struct getvar_partition_info {
139 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
140 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
141 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
142 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
143 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
144};
145
146/*
147 * Right now, we are publishing the info for only
148 * three partitions
149 */
150struct getvar_partition_info part_info[] =
151{
152 { "system" , "partition-size:", "partition-type:", "", "ext4" },
153 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
154 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
155};
156
157char max_download_size[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800158char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700159
Greg Griscod2471ef2011-07-14 13:00:42 -0700160extern int emmc_recovery_init(void);
161
Kinson Chik0b1c8162011-08-31 16:31:57 -0700162#if NO_KEYPAD_DRIVER
163extern int fastboot_trigger(void);
164#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700165
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700166static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr)
167{
168 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700169#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
170 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
171 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
172 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700173#endif
174}
175
Dima Zavin42168f22009-01-30 11:52:22 -0800176static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
177{
178 struct atag_ptbl_entry atag_ptn;
179
180 memcpy(atag_ptn.name, ptn->name, 16);
181 atag_ptn.name[15] = '\0';
182 atag_ptn.offset = ptn->start;
183 atag_ptn.size = ptn->length;
184 atag_ptn.flags = ptn->flags;
185 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
186 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
187}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800188
Neeti Desaie245d492012-06-01 12:52:13 -0700189unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800190{
David Ng183a7422009-12-07 14:55:21 -0800191 int cmdline_len = 0;
192 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800193 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700194 int pause_at_bootup = 0;
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 Nedev328ac822013-04-05 15:25:11 +0300207 if (boot_into_ffbm) {
208 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700209 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800210 /* reduce kernel console messages to speed-up boot */
211 cmdline_len += strlen(loglevel);
Pavel Nedev328ac822013-04-05 15:25:11 +0300212 } else if (target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700213 pause_at_bootup = 1;
214 cmdline_len += strlen(battchg_pause);
215 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800216
Shashank Mittalcd98d472011-08-02 14:29:24 -0700217 if(target_use_signed_kernel() && auth_kernel_img) {
218 cmdline_len += strlen(auth_kernel);
219 }
220
Ajay Dudanid04110c2011-01-17 23:55:07 -0800221 /* Determine correct androidboot.baseband to use */
222 switch(target_baseband())
223 {
224 case BASEBAND_APQ:
225 cmdline_len += strlen(baseband_apq);
226 break;
227
228 case BASEBAND_MSM:
229 cmdline_len += strlen(baseband_msm);
230 break;
231
232 case BASEBAND_CSFB:
233 cmdline_len += strlen(baseband_csfb);
234 break;
235
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800236 case BASEBAND_SVLTE2A:
237 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800238 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700239
240 case BASEBAND_MDM:
241 cmdline_len += strlen(baseband_mdm);
242 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700243
244 case BASEBAND_SGLTE:
245 cmdline_len += strlen(baseband_sglte);
246 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530247
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800248 case BASEBAND_SGLTE2:
249 cmdline_len += strlen(baseband_sglte2);
250 break;
251
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530252 case BASEBAND_DSDA:
253 cmdline_len += strlen(baseband_dsda);
254 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800255
256 case BASEBAND_DSDA2:
257 cmdline_len += strlen(baseband_dsda2);
258 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800259 }
260
David Ng183a7422009-12-07 14:55:21 -0800261 if (cmdline_len > 0) {
262 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700263 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
264 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700265
Amol Jadi168b7712012-03-06 16:15:00 -0800266 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700267 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800268 if (have_cmdline) {
269 src = cmdline;
270 while ((*dst++ = *src++));
271 }
272 if (target_is_emmc_boot()) {
273 src = emmc_cmdline;
274 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700275 have_cmdline = 1;
276 while ((*dst++ = *src++));
277 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800278
279 src = usb_sn_cmdline;
280 if (have_cmdline) --dst;
281 have_cmdline = 1;
282 while ((*dst++ = *src++));
283 src = sn_buf;
284 if (have_cmdline) --dst;
285 have_cmdline = 1;
286 while ((*dst++ = *src++));
287
Pavel Nedev328ac822013-04-05 15:25:11 +0300288 if (boot_into_ffbm) {
289 src = androidboot_mode;
290 if (have_cmdline) --dst;
291 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700292 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300293 if (have_cmdline) --dst;
294 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800295 src = loglevel;
296 if (have_cmdline) --dst;
297 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300298 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700299 src = battchg_pause;
300 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800301 while ((*dst++ = *src++));
302 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800303
Shashank Mittalcd98d472011-08-02 14:29:24 -0700304 if(target_use_signed_kernel() && auth_kernel_img) {
305 src = auth_kernel;
306 if (have_cmdline) --dst;
307 while ((*dst++ = *src++));
308 }
309
Ajay Dudanid04110c2011-01-17 23:55:07 -0800310 switch(target_baseband())
311 {
312 case BASEBAND_APQ:
313 src = baseband_apq;
314 if (have_cmdline) --dst;
315 while ((*dst++ = *src++));
316 break;
317
318 case BASEBAND_MSM:
319 src = baseband_msm;
320 if (have_cmdline) --dst;
321 while ((*dst++ = *src++));
322 break;
323
324 case BASEBAND_CSFB:
325 src = baseband_csfb;
326 if (have_cmdline) --dst;
327 while ((*dst++ = *src++));
328 break;
329
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800330 case BASEBAND_SVLTE2A:
331 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800332 if (have_cmdline) --dst;
333 while ((*dst++ = *src++));
334 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700335
336 case BASEBAND_MDM:
337 src = baseband_mdm;
338 if (have_cmdline) --dst;
339 while ((*dst++ = *src++));
340 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700341
342 case BASEBAND_SGLTE:
343 src = baseband_sglte;
344 if (have_cmdline) --dst;
345 while ((*dst++ = *src++));
346 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530347
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800348 case BASEBAND_SGLTE2:
349 src = baseband_sglte2;
350 if (have_cmdline) --dst;
351 while ((*dst++ = *src++));
352 break;
353
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530354 case BASEBAND_DSDA:
355 src = baseband_dsda;
356 if (have_cmdline) --dst;
357 while ((*dst++ = *src++));
358 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800359
360 case BASEBAND_DSDA2:
361 src = baseband_dsda2;
362 if (have_cmdline) --dst;
363 while ((*dst++ = *src++));
364 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800365 }
Neeti Desaie245d492012-06-01 12:52:13 -0700366 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800367 dprintf(INFO, "cmdline: %s\n", cmdline_final);
Neeti Desaie245d492012-06-01 12:52:13 -0700368 return cmdline_final;
369}
370
371unsigned *atag_core(unsigned *ptr)
372{
373 /* CORE */
374 *ptr++ = 2;
375 *ptr++ = 0x54410001;
376
377 return ptr;
378
379}
380
381unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
382 unsigned ramdisk_size)
383{
384 if (ramdisk_size) {
385 *ptr++ = 4;
386 *ptr++ = 0x54420005;
387 *ptr++ = (unsigned)ramdisk;
388 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800389 }
390
Neeti Desaie245d492012-06-01 12:52:13 -0700391 return ptr;
392}
393
394unsigned *atag_ptable(unsigned **ptr_addr)
395{
396 int i;
397 struct ptable *ptable;
398
399 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700400 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
401 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700402 *(*ptr_addr)++ = 0x4d534d70;
403 for (i = 0; i < ptable->count; ++i)
404 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
405 }
406
407 return (*ptr_addr);
408}
409
410unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
411{
412 int cmdline_length = 0;
413 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700414 char *dest;
415
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800416 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700417 n = (cmdline_length + 4) & (~3);
418
419 *ptr++ = (n / 4) + 2;
420 *ptr++ = 0x54410009;
421 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800422 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700423 ptr += (n / 4);
424
425 return ptr;
426}
427
428unsigned *atag_end(unsigned *ptr)
429{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800430 /* END */
431 *ptr++ = 0;
432 *ptr++ = 0;
433
Neeti Desaie245d492012-06-01 12:52:13 -0700434 return ptr;
435}
436
437void generate_atags(unsigned *ptr, const char *cmdline,
438 void *ramdisk, unsigned ramdisk_size)
439{
440
441 ptr = atag_core(ptr);
442 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
443 ptr = target_atag_mem(ptr);
444
445 /* Skip NAND partition ATAGS for eMMC boot */
446 if (!target_is_emmc_boot()){
447 ptr = atag_ptable(&ptr);
448 }
449
450 ptr = atag_cmdline(ptr, cmdline);
451 ptr = atag_end(ptr);
452}
453
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700454typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700455void boot_linux(void *kernel, unsigned *tags,
456 const char *cmdline, unsigned machtype,
457 void *ramdisk, unsigned ramdisk_size)
458{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800459 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800460#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700461 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800462#endif
463
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700464 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800465 uint32_t tags_phys = PA((addr_t)tags);
466
467 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700468
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800469 final_cmdline = update_cmdline((const char*)cmdline);
470
Neeti Desai17379b82012-06-04 18:42:53 -0700471#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800472 dprintf(INFO, "Updating device tree: start\n");
473
Neeti Desai17379b82012-06-04 18:42:53 -0700474 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800475 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700476 if(ret)
477 {
478 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
479 ASSERT(0);
480 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800481 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700482#else
Neeti Desaie245d492012-06-01 12:52:13 -0700483 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800484 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700485#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700486
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800487 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
488 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800489
490 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700491
Amol Jadi4421e652011-06-16 15:00:48 -0700492 /* do any platform specific cleanup before kernel entry */
493 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700494
Brian Swetland9c4c0752009-01-25 16:23:50 -0800495 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700496
Amol Jadi504f9fe2012-08-16 13:56:48 -0700497#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800498 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700499#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700500 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700501 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800502}
503
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800504#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800505
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700506BUF_DMA_ALIGN(buf, 4096); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800507#if DEVICE_TREE
Channagoud Kadabi132ff552013-04-19 14:34:44 -0700508BUF_DMA_ALIGN(dt_buf, 4096);
Amol Jadib6be5c12012-11-14 13:39:51 -0800509#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800510
Shashank Mittal23b8f422010-04-16 19:27:21 -0700511int boot_linux_from_mmc(void)
512{
513 struct boot_img_hdr *hdr = (void*) buf;
514 struct boot_img_hdr *uhdr;
515 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700516 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700517 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700518 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700519
Shashank Mittalcd98d472011-08-02 14:29:24 -0700520 unsigned char *image_addr = 0;
521 unsigned kernel_actual;
522 unsigned ramdisk_actual;
523 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700524 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700525
526#if DEVICE_TREE
527 struct dt_table *table;
528 struct dt_entry *dt_entry_ptr;
529 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800530 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700531#endif
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700532 if (!boot_into_recovery) {
533 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
534 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
535 if (rcode <= 0) {
536 boot_into_ffbm = false;
537 if (rcode < 0)
538 dprintf(CRITICAL,"failed to get ffbm cookie");
539 } else
540 boot_into_ffbm = true;
541 } else
542 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700543 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
544 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
545 dprintf(INFO, "Unified boot method!\n");
546 hdr = uhdr;
547 goto unified_boot;
548 }
Greg Griscod6250552011-06-29 14:40:23 -0700549 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700550 index = partition_get_index("boot");
551 ptn = partition_get_offset(index);
552 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700553 dprintf(CRITICAL, "ERROR: No boot partition found\n");
554 return -1;
555 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700556 }
557 else {
558 index = partition_get_index("recovery");
559 ptn = partition_get_offset(index);
560 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700561 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
562 return -1;
563 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700564 }
565
Greg Griscod6250552011-06-29 14:40:23 -0700566 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700567 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
568 return -1;
569 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700570
571 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700572 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700573 return -1;
574 }
575
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700576 if (hdr->page_size && (hdr->page_size != page_size)) {
577 page_size = hdr->page_size;
578 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700579 }
580
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700581 /*
582 * Update the kernel/ramdisk/tags address if the boot image header
583 * has default values, these default values come from mkbootimg when
584 * the boot image is flashed using fastboot flash:raw
585 */
586 update_ker_tags_rdisk_addr(hdr);
587
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700588 /* Get virtual addresses since the hdr saves physical addresses. */
589 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
590 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
591 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Shashank Mittalcd98d472011-08-02 14:29:24 -0700592 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700593 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800594 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700595 image_addr = (unsigned char *)target_get_scratch_address();
596 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
597 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800598#if DEVICE_TREE
599 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
600 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
601#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530602 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800603#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700604 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700605
606 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700607 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700608
Amol Jadib6be5c12012-11-14 13:39:51 -0800609 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700610 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800611
Shashank Mittalcd98d472011-08-02 14:29:24 -0700612 /* Read image without signature */
613 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
614 {
615 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
616 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800617 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700618
Amol Jadib6be5c12012-11-14 13:39:51 -0800619 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700620 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800621
Shashank Mittalcd98d472011-08-02 14:29:24 -0700622 offset = imagesize_actual;
623 /* Read signature */
624 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
625 {
626 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
627 }
628 else
629 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800630 dprintf(INFO, "Authenticating boot image (%d): start\n", imagesize_actual);
631
Shashank Mittalcd98d472011-08-02 14:29:24 -0700632 auth_kernel_img = image_verify((unsigned char *)image_addr,
633 (unsigned char *)(image_addr + imagesize_actual),
634 imagesize_actual,
635 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700636
Amol Jadib6be5c12012-11-14 13:39:51 -0800637 dprintf(INFO, "Authenticating boot image (%d): done\n", imagesize_actual);
638
Shashank Mittal162244e2011-08-08 19:01:25 -0700639 if(auth_kernel_img)
640 {
641 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700642 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700643 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700644 }
645
Neeti Desai465491e2012-07-31 12:53:35 -0700646 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700647 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
648 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800649#if DEVICE_TREE
650 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
651#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700652
Neeti Desai465491e2012-07-31 12:53:35 -0700653 #if DEVICE_TREE
654 if(hdr->dt_size) {
655 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700656 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700657
658 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
659
660 /* Restriction that the device tree entry table should be less than a page*/
661 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
662
663 /* Validate the device tree table header */
664 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
665 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
666 return -1;
667 }
668
669 /* Find index of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700670 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700671 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
672 return -1;
673 }
674
675 /* Read device device tree in the "tags_add */
676 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800677 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800678 /*
679 * If appended dev tree is found, update the atags with
680 * memory address to the DTB appended location on RAM.
681 * Else update with the atags address in the kernel header
682 */
683 void *dtb;
684 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700685 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800686 if (!dtb) {
687 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
688 return -1;
689 }
Neeti Desai465491e2012-07-31 12:53:35 -0700690 }
691 #endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700692 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700693 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700694 {
695 write_device_info_mmc(&device);
696 #ifdef TZ_TAMPER_FUSE
697 set_tamper_fuse_cmd();
698 #endif
699 }
Channagoud Kadabibf695c62012-04-10 13:31:56 +0530700 #if USE_PCOM_SECBOOT
701 set_tamper_flag(device.is_tampered);
702 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700703 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700704 else
705 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800706 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
707 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
708 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700709
Amol Jadib6be5c12012-11-14 13:39:51 -0800710 dprintf(INFO, "Loading boot image (%d): start\n",
711 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700712 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800713
714 offset = page_size;
715
716 /* Load kernel */
717 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700718 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
719 return -1;
720 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800721 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700722
Amol Jadib6be5c12012-11-14 13:39:51 -0800723 /* Load ramdisk */
724 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700725 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800726 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700727 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
728 return -1;
729 }
730 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800731 offset += ramdisk_actual;
732
733 dprintf(INFO, "Loading boot image (%d): done\n",
734 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700735 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700736
737 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800738 offset += second_actual;
739 /* Second image loading not implemented. */
740 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700741 }
742
743 #if DEVICE_TREE
744 if(hdr->dt_size != 0) {
745
746 /* Read the device tree table into buffer */
747 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
748 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
749 return -1;
750 }
751 table = (struct dt_table*) dt_buf;
752
753 /* Restriction that the device tree entry table should be less than a page*/
754 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
755
756 /* Validate the device tree table header */
757 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
758 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
759 return -1;
760 }
761
762 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700763 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700764 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
765 return -1;
766 }
767
768 /* Read device device tree in the "tags_add */
Neeti Desai465491e2012-07-31 12:53:35 -0700769 if(mmc_read(ptn + offset + dt_entry_ptr->offset,
770 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
771 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
772 return -1;
773 }
Channagoud Kadabi35095622013-03-01 13:53:05 -0800774 } else {
Dima Zavin77e41f32013-03-06 16:10:43 -0800775 /*
776 * If appended dev tree is found, update the atags with
777 * memory address to the DTB appended location on RAM.
778 * Else update with the atags address in the kernel header
779 */
780 void *dtb;
781 dtb = dev_tree_appended((void*) hdr->kernel_addr,
Channagoud Kadabia83a6972013-04-25 15:26:04 -0700782 (void *)hdr->tags_addr, hdr->kernel_size);
Dima Zavin77e41f32013-03-06 16:10:43 -0800783 if (!dtb) {
784 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
785 return -1;
786 }
Neeti Desai465491e2012-07-31 12:53:35 -0700787 }
788 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700789 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700790
791unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700792
Dima Zavin77e41f32013-03-06 16:10:43 -0800793 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -0700794 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -0700795 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
796
797 return 0;
798}
799
Dima Zavin214cc642009-01-26 11:16:21 -0800800int boot_linux_from_flash(void)
801{
802 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800803 struct ptentry *ptn;
804 struct ptable *ptable;
805 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800806
Shashank Mittalcd98d472011-08-02 14:29:24 -0700807 unsigned char *image_addr = 0;
808 unsigned kernel_actual;
809 unsigned ramdisk_actual;
810 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800811 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700812
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700813#if DEVICE_TREE
814 struct dt_table *table;
815 struct dt_entry *dt_entry_ptr;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800816 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700817#endif
818
David Ng183a7422009-12-07 14:55:21 -0800819 if (target_is_emmc_boot()) {
820 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
821 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
822 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
823 return -1;
824 }
825 goto continue_boot;
826 }
827
Dima Zavin214cc642009-01-26 11:16:21 -0800828 ptable = flash_get_ptable();
829 if (ptable == NULL) {
830 dprintf(CRITICAL, "ERROR: Partition table not found\n");
831 return -1;
832 }
833
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800834 if(!boot_into_recovery)
835 {
836 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700837
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800838 if (ptn == NULL) {
839 dprintf(CRITICAL, "ERROR: No boot partition found\n");
840 return -1;
841 }
842 }
843 else
844 {
845 ptn = ptable_find(ptable, "recovery");
846 if (ptn == NULL) {
847 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
848 return -1;
849 }
Dima Zavin214cc642009-01-26 11:16:21 -0800850 }
851
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800852 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800853 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
854 return -1;
855 }
Dima Zavin214cc642009-01-26 11:16:21 -0800856
857 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700858 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800859 return -1;
860 }
861
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800862 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700863 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 -0800864 return -1;
865 }
866
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700867 /*
868 * Update the kernel/ramdisk/tags address if the boot image header
869 * has default values, these default values come from mkbootimg when
870 * the boot image is flashed using fastboot flash:raw
871 */
872 update_ker_tags_rdisk_addr(hdr);
873
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700874 /* Get virtual addresses since the hdr saves physical addresses. */
875 hdr->kernel_addr = VA(hdr->kernel_addr);
876 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
877 hdr->tags_addr = VA(hdr->tags_addr);
878
Shashank Mittalcd98d472011-08-02 14:29:24 -0700879 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700880 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700881 {
882 image_addr = (unsigned char *)target_get_scratch_address();
883 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
884 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800885#if DEVICE_TREE
886 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
887 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
888#else
Shashank Mittalcd98d472011-08-02 14:29:24 -0700889 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800890#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700891 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700892
893 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700894 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700895
Amol Jadib6be5c12012-11-14 13:39:51 -0800896 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700897 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800898
Shashank Mittalcd98d472011-08-02 14:29:24 -0700899 /* Read image without signature */
900 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
901 {
902 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
903 return -1;
904 }
Dima Zavin214cc642009-01-26 11:16:21 -0800905
Amol Jadib6be5c12012-11-14 13:39:51 -0800906 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700907 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800908
Shashank Mittalcd98d472011-08-02 14:29:24 -0700909 offset = imagesize_actual;
910 /* Read signature */
911 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
912 {
913 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
914 }
915 else
916 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800917 dprintf(INFO, "Authenticating boot image (%d): start\n", imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700918
919 /* Verify signature */
920 auth_kernel_img = image_verify((unsigned char *)image_addr,
921 (unsigned char *)(image_addr + imagesize_actual),
922 imagesize_actual,
923 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700924
Amol Jadib6be5c12012-11-14 13:39:51 -0800925 dprintf(INFO, "Authenticating boot image (%d): done\n", imagesize_actual);
926
Shashank Mittal162244e2011-08-08 19:01:25 -0700927 if(auth_kernel_img)
928 {
929 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700930 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700931 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700932 }
933
934 /* Move kernel and ramdisk to correct address */
935 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
936 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800937#if DEVICE_TREE
938 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
939#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700940
941 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700942 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700943 {
944 write_device_info_flash(&device);
945 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530946#if USE_PCOM_SECBOOT
947 set_tamper_flag(device.is_tampered);
948#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700949 }
950 else
951 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700952 offset = page_size;
953
Amol Jadib6be5c12012-11-14 13:39:51 -0800954 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
955 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
956 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
957
958 dprintf(INFO, "Loading boot image (%d): start\n",
959 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700960 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800961
962 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700963 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
964 return -1;
965 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800966 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700967
Amol Jadib6be5c12012-11-14 13:39:51 -0800968 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700969 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
970 return -1;
971 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800972 offset += ramdisk_actual;
973
974 dprintf(INFO, "Loading boot image (%d): done\n",
975 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700976 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700977
978 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800979 offset += second_actual;
980 /* Second image loading not implemented. */
981 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700982 }
983
984#if DEVICE_TREE
985 if(hdr->dt_size != 0) {
986
987 /* Read the device tree table into buffer */
988 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
989 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
990 return -1;
991 }
992
993 table = (struct dt_table*) dt_buf;
994
995 /* Restriction that the device tree entry table should be less than a page*/
996 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
997
998 /* Validate the device tree table header */
999 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1000 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1001 return -1;
1002 }
1003
1004 /* Calculate the offset of device tree within device tree table */
1005 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
1006 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1007 return -1;
1008 }
1009
1010 /* Read device device tree in the "tags_add */
1011 if(flash_read(ptn, offset + dt_entry_ptr->offset,
1012 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
1013 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1014 return -1;
1015 }
1016 }
1017#endif
1018
Shashank Mittalcd98d472011-08-02 14:29:24 -07001019 }
David Ng183a7422009-12-07 14:55:21 -08001020continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001021
Dima Zavin214cc642009-01-26 11:16:21 -08001022 /* TODO: create/pass atags to kernel */
1023
Ajay Dudanie28a6072011-07-01 13:59:46 -07001024 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001025 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001026 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1027
1028 return 0;
1029}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001030
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001031BUF_DMA_ALIGN(info_buf, 4096);
Shashank Mittal162244e2011-08-08 19:01:25 -07001032void write_device_info_mmc(device_info *dev)
1033{
1034 struct device_info *info = (void*) info_buf;
1035 unsigned long long ptn = 0;
1036 unsigned long long size;
1037 int index = INVALID_PTN;
1038
1039 index = partition_get_index("aboot");
1040 ptn = partition_get_offset(index);
1041 if(ptn == 0)
1042 {
1043 return;
1044 }
1045
1046 size = partition_get_size(index);
1047
1048 memcpy(info, dev, sizeof(device_info));
1049
1050 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
1051 {
1052 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1053 return;
1054 }
1055}
1056
1057void read_device_info_mmc(device_info *dev)
1058{
1059 struct device_info *info = (void*) info_buf;
1060 unsigned long long ptn = 0;
1061 unsigned long long size;
1062 int index = INVALID_PTN;
1063
1064 index = partition_get_index("aboot");
1065 ptn = partition_get_offset(index);
1066 if(ptn == 0)
1067 {
1068 return;
1069 }
1070
1071 size = partition_get_size(index);
1072
1073 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1074 {
1075 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1076 return;
1077 }
1078
1079 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1080 {
1081 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1082 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001083 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001084
1085 write_device_info_mmc(info);
1086 }
1087 memcpy(dev, info, sizeof(device_info));
1088}
1089
1090void write_device_info_flash(device_info *dev)
1091{
1092 struct device_info *info = (void *) info_buf;
1093 struct ptentry *ptn;
1094 struct ptable *ptable;
1095
1096 ptable = flash_get_ptable();
1097 if (ptable == NULL)
1098 {
1099 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1100 return;
1101 }
1102
1103 ptn = ptable_find(ptable, "devinfo");
1104 if (ptn == NULL)
1105 {
1106 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1107 return;
1108 }
1109
1110 memcpy(info, dev, sizeof(device_info));
1111
1112 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1113 {
1114 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1115 return;
1116 }
1117}
1118
1119void read_device_info_flash(device_info *dev)
1120{
1121 struct device_info *info = (void*) info_buf;
1122 struct ptentry *ptn;
1123 struct ptable *ptable;
1124
1125 ptable = flash_get_ptable();
1126 if (ptable == NULL)
1127 {
1128 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1129 return;
1130 }
1131
1132 ptn = ptable_find(ptable, "devinfo");
1133 if (ptn == NULL)
1134 {
1135 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1136 return;
1137 }
1138
1139 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1140 {
1141 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1142 return;
1143 }
1144
1145 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1146 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001147 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1148 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001149 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001150 write_device_info_flash(info);
1151 }
1152 memcpy(dev, info, sizeof(device_info));
1153}
1154
1155void write_device_info(device_info *dev)
1156{
1157 if(target_is_emmc_boot())
1158 {
1159 write_device_info_mmc(dev);
1160 }
1161 else
1162 {
1163 write_device_info_flash(dev);
1164 }
1165}
1166
1167void read_device_info(device_info *dev)
1168{
1169 if(target_is_emmc_boot())
1170 {
1171 read_device_info_mmc(dev);
1172 }
1173 else
1174 {
1175 read_device_info_flash(dev);
1176 }
1177}
1178
1179void reset_device_info()
1180{
1181 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001182 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001183 write_device_info(&device);
1184}
1185
1186void set_device_root()
1187{
1188 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001189 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001190 write_device_info(&device);
1191}
1192
Amol Jadicb524072012-08-09 16:40:18 -07001193#if DEVICE_TREE
1194int copy_dtb(uint8_t *boot_image_start)
1195{
1196 uint32 dt_image_offset = 0;
1197 uint32_t n;
1198 struct dt_table *table;
1199 struct dt_entry *dt_entry_ptr;
Amol Jadicb524072012-08-09 16:40:18 -07001200
1201 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1202
Amol Jadicb524072012-08-09 16:40:18 -07001203 if(hdr->dt_size != 0) {
1204
1205 /* add kernel offset */
1206 dt_image_offset += page_size;
1207 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1208 dt_image_offset += n;
1209
1210 /* add ramdisk offset */
1211 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1212 dt_image_offset += n;
1213
1214 /* add second offset */
1215 if(hdr->second_size != 0) {
1216 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1217 dt_image_offset += n;
1218 }
1219
1220 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001221 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001222
1223 /* Restriction that the device tree entry table should be less than a page*/
1224 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1225
1226 /* Validate the device tree table header */
1227 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1228 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1229 return -1;
1230 }
1231
1232 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001233 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Amol Jadicb524072012-08-09 16:40:18 -07001234 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1235 return -1;
1236 }
1237
1238 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001239 memmove((void*) hdr->tags_addr,
1240 boot_image_start + dt_image_offset + dt_entry_ptr->offset,
1241 dt_entry_ptr->size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001242 } else
1243 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07001244
1245 /* Everything looks fine. Return success. */
1246 return 0;
1247}
1248#endif
1249
Brian Swetland9c4c0752009-01-25 16:23:50 -08001250void cmd_boot(const char *arg, void *data, unsigned sz)
1251{
1252 unsigned kernel_actual;
1253 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001254 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001255 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001256 int ret = 0;
1257 uint8_t dtb_copied = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001258
1259 if (sz < sizeof(hdr)) {
1260 fastboot_fail("invalid bootimage header");
1261 return;
1262 }
1263
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001264 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001265
1266 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001267 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001268
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001269 if(target_is_emmc_boot() && hdr->page_size) {
1270 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001271 page_mask = page_size - 1;
1272 }
1273
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001274 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1275 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1276
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001277 /*
1278 * Update the kernel/ramdisk/tags address if the boot image header
1279 * has default values, these default values come from mkbootimg when
1280 * the boot image is flashed using fastboot flash:raw
1281 */
1282 update_ker_tags_rdisk_addr(hdr);
Dima Zavin3cadfff2013-03-21 14:30:48 -07001283
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001284 /* Get virtual addresses since the hdr saves physical addresses. */
1285 hdr->kernel_addr = VA(hdr->kernel_addr);
1286 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1287 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001288
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001289 /* sz should have atleast raw boot image */
1290 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001291 fastboot_fail("incomplete bootimage");
1292 return;
1293 }
1294
Amol Jadicb524072012-08-09 16:40:18 -07001295#if DEVICE_TREE
1296 /* find correct dtb and copy it to right location */
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001297 ret = copy_dtb(data);
1298
1299 dtb_copied = !ret ? 1 : 0;
1300#endif
1301
1302 /* Load ramdisk & kernel */
1303 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1304 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
1305
1306#if DEVICE_TREE
1307 /*
1308 * If dtb is not found look for appended DTB in the kernel.
1309 * If appended dev tree is found, update the atags with
1310 * memory address to the DTB appended location on RAM.
1311 * Else update with the atags address in the kernel header
1312 */
1313 if (!dtb_copied) {
1314 void *dtb;
Channagoud Kadabia83a6972013-04-25 15:26:04 -07001315 dtb = dev_tree_appended((void *)hdr->kernel_addr, (void *)hdr->tags_addr, hdr->kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07001316 if (!dtb) {
1317 fastboot_fail("dtb not found");
1318 return;
1319 }
Amol Jadicb524072012-08-09 16:40:18 -07001320 }
1321#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001322
1323 fastboot_okay("");
1324 udc_stop();
1325
Dima Zavin77e41f32013-03-06 16:10:43 -08001326 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001327 (const char*) hdr->cmdline, board_machtype(),
1328 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001329}
1330
Dima Zavin214cc642009-01-26 11:16:21 -08001331void cmd_erase(const char *arg, void *data, unsigned sz)
1332{
1333 struct ptentry *ptn;
1334 struct ptable *ptable;
1335
1336 ptable = flash_get_ptable();
1337 if (ptable == NULL) {
1338 fastboot_fail("partition table doesn't exist");
1339 return;
1340 }
1341
1342 ptn = ptable_find(ptable, arg);
1343 if (ptn == NULL) {
1344 fastboot_fail("unknown partition name");
1345 return;
1346 }
1347
1348 if (flash_erase(ptn)) {
1349 fastboot_fail("failed to erase partition");
1350 return;
1351 }
1352 fastboot_okay("");
1353}
1354
Bikas Gurungd48bd242010-09-04 19:54:32 -07001355
1356void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1357{
Channagoud Kadabi132ff552013-04-19 14:34:44 -07001358 BUF_DMA_ALIGN(out, 512);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001359 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001360 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001361
Kinson Chikf1a43512011-07-14 11:28:39 -07001362 index = partition_get_index(arg);
1363 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001364
Kinson Chikf1a43512011-07-14 11:28:39 -07001365 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001366 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001367 return;
1368 }
neetidb4b24d62012-01-20 12:13:09 -08001369 /* Simple inefficient version of erase. Just writing
1370 0 in first block */
1371 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1372 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001373 return;
1374 }
1375 fastboot_okay("");
1376}
1377
1378
Ajay Dudani5c761132011-04-07 20:19:04 -07001379void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001380{
1381 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001382 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001383 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001384
Greg Grisco6e754772011-06-23 12:19:39 -07001385 if (!strcmp(arg, "partition"))
1386 {
1387 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001388 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001389 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001390 return;
1391 }
1392 }
Greg Grisco6e754772011-06-23 12:19:39 -07001393 else
1394 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001395 index = partition_get_index(arg);
1396 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001397 if(ptn == 0) {
1398 fastboot_fail("partition table doesn't exist");
1399 return;
1400 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001401
Greg Grisco6e754772011-06-23 12:19:39 -07001402 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1403 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1404 fastboot_fail("image is not a boot image");
1405 return;
1406 }
1407 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001408
Kinson Chikf1a43512011-07-14 11:28:39 -07001409 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001410 if (ROUND_TO_PAGE(sz,511) > size) {
1411 fastboot_fail("size too large");
1412 return;
1413 }
1414 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1415 fastboot_fail("flash write failure");
1416 return;
1417 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001418 }
1419 fastboot_okay("");
1420 return;
1421}
1422
Ajay Dudani5c761132011-04-07 20:19:04 -07001423void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1424{
1425 unsigned int chunk;
1426 unsigned int chunk_data_sz;
1427 sparse_header_t *sparse_header;
1428 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001429 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001430 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301431 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001432 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001433
Kinson Chikf1a43512011-07-14 11:28:39 -07001434 index = partition_get_index(arg);
1435 ptn = partition_get_offset(index);
1436 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001437 fastboot_fail("partition table doesn't exist");
1438 return;
1439 }
1440
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301441 size = partition_get_size(index);
1442 if (ROUND_TO_PAGE(sz,511) > size) {
1443 fastboot_fail("size too large");
1444 return;
1445 }
1446
Ajay Dudani5c761132011-04-07 20:19:04 -07001447 /* Read and skip over sparse image header */
1448 sparse_header = (sparse_header_t *) data;
Ajay Dudani876b3282012-12-21 14:12:17 -08001449 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
1450 fastboot_fail("size too large");
1451 return;
1452 }
1453
Ajay Dudani5c761132011-04-07 20:19:04 -07001454 data += sparse_header->file_hdr_sz;
1455 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1456 {
1457 /* Skip the remaining bytes in a header that is longer than
1458 * we expected.
1459 */
1460 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1461 }
1462
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001463 dprintf (SPEW, "=== Sparse Image Header ===\n");
1464 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1465 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1466 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1467 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1468 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1469 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1470 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1471 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001472
1473 /* Start processing chunks */
1474 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1475 {
1476 /* Read and skip over chunk header */
1477 chunk_header = (chunk_header_t *) data;
1478 data += sizeof(chunk_header_t);
1479
1480 dprintf (SPEW, "=== Chunk Header ===\n");
1481 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1482 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1483 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1484
1485 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1486 {
1487 /* Skip the remaining bytes in a header that is longer than
1488 * we expected.
1489 */
1490 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1491 }
1492
1493 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1494 switch (chunk_header->chunk_type)
1495 {
1496 case CHUNK_TYPE_RAW:
1497 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1498 chunk_data_sz))
1499 {
1500 fastboot_fail("Bogus chunk size for chunk type Raw");
1501 return;
1502 }
1503
Ajay Dudaniab18f022011-05-12 14:39:22 -07001504 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1505 chunk_data_sz,
1506 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001507 {
1508 fastboot_fail("flash write failure");
1509 return;
1510 }
1511 total_blocks += chunk_header->chunk_sz;
1512 data += chunk_data_sz;
1513 break;
1514
1515 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001516 total_blocks += chunk_header->chunk_sz;
1517 break;
1518
Ajay Dudani5c761132011-04-07 20:19:04 -07001519 case CHUNK_TYPE_CRC:
1520 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1521 {
1522 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1523 return;
1524 }
1525 total_blocks += chunk_header->chunk_sz;
1526 data += chunk_data_sz;
1527 break;
1528
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001529 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001530 fastboot_fail("Unknown chunk type");
1531 return;
1532 }
1533 }
1534
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001535 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1536 total_blocks, sparse_header->total_blks);
1537
1538 if(total_blocks != sparse_header->total_blks)
1539 {
1540 fastboot_fail("sparse image write failure");
1541 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001542
1543 fastboot_okay("");
1544 return;
1545}
1546
1547void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1548{
1549 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001550 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1551 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001552
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001553#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001554 int ret=0;
1555 uint32 major_version=0;
1556 uint32 minor_version=0;
1557
1558 ret = scm_svc_version(&major_version,&minor_version);
1559 if(!ret)
1560 {
1561 if(major_version >= 2)
1562 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001563 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001564 {
1565 ret = encrypt_scm((uint32 **) &data, &sz);
1566 if (ret != 0) {
1567 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1568 return;
1569 }
1570
Amir Samuelovbb65ce02013-05-05 12:20:18 +03001571 /* Protect only for SSD */
1572 if (!strcmp(arg, "ssd")) {
1573 ret = scm_protect_keystore((uint32 *) data, sz);
1574 if (ret != 0) {
1575 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1576 return;
1577 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001578 }
1579 }
1580 else
1581 {
1582 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1583 if(ret != 0)
1584 {
1585 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1586 return;
1587 }
1588 }
1589 }
1590 else
1591 {
1592 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1593 magic_number[1] == DECRYPT_MAGIC_1)
1594 {
1595 ret = decrypt_scm((uint32 **) &data, &sz);
1596 if (ret != 0) {
1597 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1598 return;
1599 }
1600 }
1601 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1602 magic_number[1] == ENCRYPT_MAGIC_1)
1603 {
1604 ret = encrypt_scm((uint32 **) &data, &sz);
1605 if (ret != 0) {
1606 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1607 return;
1608 }
1609 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001610 }
1611 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001612 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001613 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001614 dprintf(CRITICAL,"INVALID SVC Version\n");
1615 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001616 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001617#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001618
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001619 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001620 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1621 cmd_flash_mmc_img(arg, data, sz);
1622 else
1623 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001624 return;
1625}
1626
Dima Zavin214cc642009-01-26 11:16:21 -08001627void cmd_flash(const char *arg, void *data, unsigned sz)
1628{
1629 struct ptentry *ptn;
1630 struct ptable *ptable;
1631 unsigned extra = 0;
1632
1633 ptable = flash_get_ptable();
1634 if (ptable == NULL) {
1635 fastboot_fail("partition table doesn't exist");
1636 return;
1637 }
1638
1639 ptn = ptable_find(ptable, arg);
1640 if (ptn == NULL) {
1641 fastboot_fail("unknown partition name");
1642 return;
1643 }
1644
1645 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1646 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1647 fastboot_fail("image is not a boot image");
1648 return;
1649 }
1650 }
1651
Amol Jadi5c61a952012-05-04 17:05:35 -07001652 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001653 || !strcmp(ptn->name, "userdata")
1654 || !strcmp(ptn->name, "persist")
1655 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301656 if (flash_ecc_bch_enabled())
1657 /* Spare data bytes for 8 bit ECC increased by 4 */
1658 extra = ((page_size >> 9) * 20);
1659 else
1660 extra = ((page_size >> 9) * 16);
1661 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001662 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001663
1664 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1665 if (flash_write(ptn, extra, data, sz)) {
1666 fastboot_fail("flash write failure");
1667 return;
1668 }
1669 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1670 fastboot_okay("");
1671}
1672
1673void cmd_continue(const char *arg, void *data, unsigned sz)
1674{
1675 fastboot_okay("");
1676 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001677 if (target_is_emmc_boot())
1678 {
1679 boot_linux_from_mmc();
1680 }
1681 else
1682 {
1683 boot_linux_from_flash();
1684 }
Dima Zavin214cc642009-01-26 11:16:21 -08001685}
1686
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001687void cmd_reboot(const char *arg, void *data, unsigned sz)
1688{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001689 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001690 fastboot_okay("");
1691 reboot_device(0);
1692}
1693
1694void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1695{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001696 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001697 fastboot_okay("");
1698 reboot_device(FASTBOOT_MODE);
1699}
1700
Shashank Mittal162244e2011-08-08 19:01:25 -07001701void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1702{
1703 if(!device.is_unlocked)
1704 {
1705 device.is_unlocked = 1;
1706 write_device_info(&device);
1707 }
1708 fastboot_okay("");
1709}
1710
Shashank Mittala0032282011-08-26 14:50:11 -07001711void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1712{
1713 char response[64];
1714 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1715 fastboot_info(response);
1716 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1717 fastboot_info(response);
1718 fastboot_okay("");
1719}
1720
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001721void cmd_preflash(const char *arg, void *data, unsigned sz)
1722{
1723 fastboot_okay("");
1724}
1725
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001726void splash_screen ()
1727{
1728 struct ptentry *ptn;
1729 struct ptable *ptable;
1730 struct fbcon_config *fb_display = NULL;
1731
1732 if (!target_is_emmc_boot())
1733 {
1734 ptable = flash_get_ptable();
1735 if (ptable == NULL) {
1736 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001737 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001738 }
1739
1740 ptn = ptable_find(ptable, "splash");
1741 if (ptn == NULL) {
1742 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1743 } else {
1744 fb_display = fbcon_display();
1745 if (fb_display) {
1746 if (flash_read(ptn, 0, fb_display->base,
1747 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1748 fbcon_clear();
1749 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1750 }
1751 }
1752 }
1753 }
1754}
1755
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001756/* Get the size from partiton name */
1757static void get_partition_size(const char *arg, char *response)
1758{
1759 uint64_t ptn = 0;
1760 uint64_t size;
1761 int index = INVALID_PTN;
1762
1763 index = partition_get_index(arg);
1764
1765 if (index == INVALID_PTN)
1766 {
1767 dprintf(CRITICAL, "Invalid partition index\n");
1768 return;
1769 }
1770
1771 ptn = partition_get_offset(index);
1772
1773 if(!ptn)
1774 {
1775 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
1776 return;
1777 }
1778
1779 size = partition_get_size(index);
1780
1781 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
1782 return;
1783}
1784
1785/*
1786 * Publish the partition type & size info
1787 * fastboot getvar will publish the required information.
1788 * fastboot getvar partition_size:<partition_name>: partition size in hex
1789 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
1790 */
1791static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
1792{
1793 uint8_t i;
1794
1795 for (i = 0; i < num_parts; i++) {
1796 get_partition_size(info[i].part_name, info[i].size_response);
1797
1798 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1799 {
1800 dprintf(CRITICAL, "partition size name truncated\n");
1801 return;
1802 }
1803 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
1804 {
1805 dprintf(CRITICAL, "partition type name truncated\n");
1806 return;
1807 }
1808
1809 /* publish partition size & type info */
1810 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
1811 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
1812 }
1813}
1814
Brian Swetland9c4c0752009-01-25 16:23:50 -08001815void aboot_init(const struct app_descriptor *app)
1816{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001817 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001818 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001819 unsigned sz = 0;
Pavel Nedev5d91d412013-04-29 11:34:24 +03001820 bool boot_into_fastboot = false;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001821
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001822 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001823 if (target_is_emmc_boot())
1824 {
1825 page_size = 2048;
1826 page_mask = page_size - 1;
1827 }
1828 else
1829 {
1830 page_size = flash_page_size();
1831 page_mask = page_size - 1;
1832 }
1833
Shashank Mittal162244e2011-08-08 19:01:25 -07001834 if(target_use_signed_kernel())
1835 {
1836 read_device_info(&device);
1837
Shashank Mittal162244e2011-08-08 19:01:25 -07001838 }
1839
Greg Griscod6250552011-06-29 14:40:23 -07001840 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001841 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001842 surf_udc_device.serialno = sn_buf;
1843
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001844 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001845 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001846 {
Ameya Thakur0b9c2442013-05-10 13:22:56 -07001847 dprintf(ALWAYS,"dload mode key sequence detected");
1848 if (set_download_mode())
1849 {
1850 dprintf(CRITICAL,"dload mode not supported by target");
1851 }
1852 else
1853 {
1854 reboot_device(0);
1855 dprintf(CRITICAL,"Failed to reboot into dload mode");
1856 }
1857 boot_into_fastboot = true;
1858 }
1859 if (!boot_into_fastboot)
1860 {
1861 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
1862 boot_into_recovery = 1;
1863 if (!boot_into_recovery &&
1864 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03001865 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001866 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001867 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001868 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03001869 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001870 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001871
Ajay Dudani77421292010-10-27 19:34:06 -07001872 reboot_mode = check_reboot_mode();
1873 if (reboot_mode == RECOVERY_MODE) {
1874 boot_into_recovery = 1;
1875 } else if(reboot_mode == FASTBOOT_MODE) {
Pavel Nedev5d91d412013-04-29 11:34:24 +03001876 boot_into_fastboot = true;
Ajay Dudani77421292010-10-27 19:34:06 -07001877 }
1878
Pavel Nedev5d91d412013-04-29 11:34:24 +03001879 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001880 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03001881 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07001882 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03001883 if(emmc_recovery_init())
1884 dprintf(ALWAYS,"error in emmc_recovery_init\n");
1885 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07001886 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03001887 if((device.is_unlocked) || (device.is_tampered))
1888 {
1889 #ifdef TZ_TAMPER_FUSE
1890 set_tamper_fuse_cmd();
1891 #endif
1892 #if USE_PCOM_SECBOOT
1893 set_tamper_flag(device.is_tampered);
1894 #endif
1895 }
Shashank Mittala0032282011-08-26 14:50:11 -07001896 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03001897 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07001898 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03001899 else
1900 {
1901 recovery_init();
1902 #if USE_PCOM_SECBOOT
1903 if((device.is_unlocked) || (device.is_tampered))
1904 set_tamper_flag(device.is_tampered);
1905 #endif
1906 boot_linux_from_flash();
1907 }
1908 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1909 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001910 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001911
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001912 sz = target_get_max_flash_size();
1913
Shashank Mittal162244e2011-08-08 19:01:25 -07001914 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001915
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001916 if(!usb_init)
1917 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001918
1919 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001920
Shashank Mittal23b8f422010-04-16 19:27:21 -07001921 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001922 {
1923 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001924 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001925 }
1926 else
1927 {
1928 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001929 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001930 }
1931
1932 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001933 fastboot_register("reboot", cmd_reboot);
1934 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001935 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001936 fastboot_register("oem device-info", cmd_oem_devinfo);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001937 fastboot_register("preflash", cmd_preflash);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001938 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001939 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001940 fastboot_publish("serialno", sn_buf);
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07001941 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
1942 /* Max download size supported */
1943 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x", sz);
1944 fastboot_publish("max-download-size", (const char *) max_download_size);
Kinson Chikf1a43512011-07-14 11:28:39 -07001945 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001946 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001947 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001948}
1949
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001950uint32_t get_page_size()
1951{
1952 return page_size;
1953}
1954
Brian Swetland9c4c0752009-01-25 16:23:50 -08001955APP_START(aboot)
1956 .init = aboot_init,
1957APP_END