blob: a4aab738ae625a0abc1169c0cc90a488d5869fdf [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>
38#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>
Dima Zavin214cc642009-01-26 11:16:21 -080052
Neeti Desai17379b82012-06-04 18:42:53 -070053#if DEVICE_TREE
54#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070055#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070056#endif
57
Shashank Mittalcd98d472011-08-02 14:29:24 -070058#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080059#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080060#include "bootimg.h"
61#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070062#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070063#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070064#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070065#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080066
Shashank Mittal162244e2011-08-08 19:01:25 -070067#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070068
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070069extern bool target_use_signed_kernel(void);
70extern void dsb();
71extern void isb();
72extern void platform_uninit(void);
73
74void write_device_info_mmc(device_info *dev);
75void write_device_info_flash(device_info *dev);
76
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070077#define EXPAND(NAME) #NAME
78#define TARGET(NAME) EXPAND(NAME)
Chandan Uddarajuda919832009-11-17 01:06:11 -080079#define DEFAULT_CMDLINE "mem=100M console=null";
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
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080087#define RECOVERY_MODE 0x77665502
88#define FASTBOOT_MODE 0x77665500
89
David Ng183a7422009-12-07 14:55:21 -080090static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080091static const char *usb_sn_cmdline = " androidboot.serialno=";
Ajay Dudanica3a33c2011-11-18 08:31:40 -080092static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -070093static const char *auth_kernel = " androidboot.authorized_kernel=true";
David Ng183a7422009-12-07 14:55:21 -080094
Ajay Dudani6cff85e2011-02-04 16:02:16 -080095static const char *baseband_apq = " androidboot.baseband=apq";
96static const char *baseband_msm = " androidboot.baseband=msm";
97static const char *baseband_csfb = " androidboot.baseband=csfb";
98static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -070099static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700100static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800101static const char *baseband_dsda = " androidboot.baseband=dsda";
102static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800103
Shashank Mittalcd98d472011-08-02 14:29:24 -0700104/* Assuming unauthorized kernel image by default */
105static int auth_kernel_img = 0;
106
Channagoud Kadabi35095622013-03-01 13:53:05 -0800107static uint32_t app_dev_tree = 0;
108
Shashank Mittal162244e2011-08-08 19:01:25 -0700109static device_info device = {DEVICE_MAGIC, 0, 0};
110
Brian Swetland9c4c0752009-01-25 16:23:50 -0800111static struct udc_device surf_udc_device = {
112 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -0800113 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800114 .version_id = 0x0100,
115 .manufacturer = "Google",
116 .product = "Android",
117};
118
Dima Zavin42168f22009-01-30 11:52:22 -0800119struct atag_ptbl_entry
120{
121 char name[16];
122 unsigned offset;
123 unsigned size;
124 unsigned flags;
125};
126
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800127char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700128
Greg Griscod2471ef2011-07-14 13:00:42 -0700129extern int emmc_recovery_init(void);
130
Kinson Chik0b1c8162011-08-31 16:31:57 -0700131#if NO_KEYPAD_DRIVER
132extern int fastboot_trigger(void);
133#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700134
Dima Zavin42168f22009-01-30 11:52:22 -0800135static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
136{
137 struct atag_ptbl_entry atag_ptn;
138
139 memcpy(atag_ptn.name, ptn->name, 16);
140 atag_ptn.name[15] = '\0';
141 atag_ptn.offset = ptn->start;
142 atag_ptn.size = ptn->length;
143 atag_ptn.flags = ptn->flags;
144 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
145 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
146}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800147
Neeti Desaie245d492012-06-01 12:52:13 -0700148unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800149{
David Ng183a7422009-12-07 14:55:21 -0800150 int cmdline_len = 0;
151 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800152 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700153 int pause_at_bootup = 0;
Dima Zavin42168f22009-01-30 11:52:22 -0800154
Brian Swetland9c4c0752009-01-25 16:23:50 -0800155 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800156 cmdline_len = strlen(cmdline);
157 have_cmdline = 1;
158 }
159 if (target_is_emmc_boot()) {
160 cmdline_len += strlen(emmc_cmdline);
161 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800162
163 cmdline_len += strlen(usb_sn_cmdline);
164 cmdline_len += strlen(sn_buf);
165
David Ngf773dde2010-07-26 19:55:08 -0700166 if (target_pause_for_battery_charge()) {
167 pause_at_bootup = 1;
168 cmdline_len += strlen(battchg_pause);
169 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800170
Shashank Mittalcd98d472011-08-02 14:29:24 -0700171 if(target_use_signed_kernel() && auth_kernel_img) {
172 cmdline_len += strlen(auth_kernel);
173 }
174
Ajay Dudanid04110c2011-01-17 23:55:07 -0800175 /* Determine correct androidboot.baseband to use */
176 switch(target_baseband())
177 {
178 case BASEBAND_APQ:
179 cmdline_len += strlen(baseband_apq);
180 break;
181
182 case BASEBAND_MSM:
183 cmdline_len += strlen(baseband_msm);
184 break;
185
186 case BASEBAND_CSFB:
187 cmdline_len += strlen(baseband_csfb);
188 break;
189
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800190 case BASEBAND_SVLTE2A:
191 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800192 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700193
194 case BASEBAND_MDM:
195 cmdline_len += strlen(baseband_mdm);
196 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700197
198 case BASEBAND_SGLTE:
199 cmdline_len += strlen(baseband_sglte);
200 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530201
202 case BASEBAND_DSDA:
203 cmdline_len += strlen(baseband_dsda);
204 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800205
206 case BASEBAND_DSDA2:
207 cmdline_len += strlen(baseband_dsda2);
208 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800209 }
210
David Ng183a7422009-12-07 14:55:21 -0800211 if (cmdline_len > 0) {
212 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700213 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
214 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700215
Amol Jadi168b7712012-03-06 16:15:00 -0800216 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700217 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800218 if (have_cmdline) {
219 src = cmdline;
220 while ((*dst++ = *src++));
221 }
222 if (target_is_emmc_boot()) {
223 src = emmc_cmdline;
224 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700225 have_cmdline = 1;
226 while ((*dst++ = *src++));
227 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800228
229 src = usb_sn_cmdline;
230 if (have_cmdline) --dst;
231 have_cmdline = 1;
232 while ((*dst++ = *src++));
233 src = sn_buf;
234 if (have_cmdline) --dst;
235 have_cmdline = 1;
236 while ((*dst++ = *src++));
237
David Ngf773dde2010-07-26 19:55:08 -0700238 if (pause_at_bootup) {
239 src = battchg_pause;
240 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800241 while ((*dst++ = *src++));
242 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800243
Shashank Mittalcd98d472011-08-02 14:29:24 -0700244 if(target_use_signed_kernel() && auth_kernel_img) {
245 src = auth_kernel;
246 if (have_cmdline) --dst;
247 while ((*dst++ = *src++));
248 }
249
Ajay Dudanid04110c2011-01-17 23:55:07 -0800250 switch(target_baseband())
251 {
252 case BASEBAND_APQ:
253 src = baseband_apq;
254 if (have_cmdline) --dst;
255 while ((*dst++ = *src++));
256 break;
257
258 case BASEBAND_MSM:
259 src = baseband_msm;
260 if (have_cmdline) --dst;
261 while ((*dst++ = *src++));
262 break;
263
264 case BASEBAND_CSFB:
265 src = baseband_csfb;
266 if (have_cmdline) --dst;
267 while ((*dst++ = *src++));
268 break;
269
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800270 case BASEBAND_SVLTE2A:
271 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800272 if (have_cmdline) --dst;
273 while ((*dst++ = *src++));
274 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700275
276 case BASEBAND_MDM:
277 src = baseband_mdm;
278 if (have_cmdline) --dst;
279 while ((*dst++ = *src++));
280 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700281
282 case BASEBAND_SGLTE:
283 src = baseband_sglte;
284 if (have_cmdline) --dst;
285 while ((*dst++ = *src++));
286 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530287
288 case BASEBAND_DSDA:
289 src = baseband_dsda;
290 if (have_cmdline) --dst;
291 while ((*dst++ = *src++));
292 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800293
294 case BASEBAND_DSDA2:
295 src = baseband_dsda2;
296 if (have_cmdline) --dst;
297 while ((*dst++ = *src++));
298 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800299 }
Neeti Desaie245d492012-06-01 12:52:13 -0700300 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800301
302 dprintf(INFO, "cmdline: %s\n", cmdline_final);
303
Neeti Desaie245d492012-06-01 12:52:13 -0700304 return cmdline_final;
305}
306
307unsigned *atag_core(unsigned *ptr)
308{
309 /* CORE */
310 *ptr++ = 2;
311 *ptr++ = 0x54410001;
312
313 return ptr;
314
315}
316
317unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
318 unsigned ramdisk_size)
319{
320 if (ramdisk_size) {
321 *ptr++ = 4;
322 *ptr++ = 0x54420005;
323 *ptr++ = (unsigned)ramdisk;
324 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800325 }
326
Neeti Desaie245d492012-06-01 12:52:13 -0700327 return ptr;
328}
329
330unsigned *atag_ptable(unsigned **ptr_addr)
331{
332 int i;
333 struct ptable *ptable;
334
335 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700336 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
337 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700338 *(*ptr_addr)++ = 0x4d534d70;
339 for (i = 0; i < ptable->count; ++i)
340 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
341 }
342
343 return (*ptr_addr);
344}
345
346unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
347{
348 int cmdline_length = 0;
349 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700350 char *dest;
351
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800352 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700353 n = (cmdline_length + 4) & (~3);
354
355 *ptr++ = (n / 4) + 2;
356 *ptr++ = 0x54410009;
357 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800358 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700359 ptr += (n / 4);
360
361 return ptr;
362}
363
364unsigned *atag_end(unsigned *ptr)
365{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800366 /* END */
367 *ptr++ = 0;
368 *ptr++ = 0;
369
Neeti Desaie245d492012-06-01 12:52:13 -0700370 return ptr;
371}
372
373void generate_atags(unsigned *ptr, const char *cmdline,
374 void *ramdisk, unsigned ramdisk_size)
375{
376
377 ptr = atag_core(ptr);
378 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
379 ptr = target_atag_mem(ptr);
380
381 /* Skip NAND partition ATAGS for eMMC boot */
382 if (!target_is_emmc_boot()){
383 ptr = atag_ptable(&ptr);
384 }
385
386 ptr = atag_cmdline(ptr, cmdline);
387 ptr = atag_end(ptr);
388}
389
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700390typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700391void boot_linux(void *kernel, unsigned *tags,
392 const char *cmdline, unsigned machtype,
393 void *ramdisk, unsigned ramdisk_size)
394{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800395 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800396#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700397 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800398#endif
399
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700400 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800401 uint32_t tags_phys = PA((addr_t)tags);
402
403 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700404
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800405 final_cmdline = update_cmdline((const char*)cmdline);
406
Neeti Desai17379b82012-06-04 18:42:53 -0700407#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800408 dprintf(INFO, "Updating device tree: start\n");
409
Neeti Desai17379b82012-06-04 18:42:53 -0700410 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800411 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700412 if(ret)
413 {
414 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
415 ASSERT(0);
416 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800417 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700418#else
Neeti Desaie245d492012-06-01 12:52:13 -0700419 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800420 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700421#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700422
Brian Swetland9c4c0752009-01-25 16:23:50 -0800423 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
Amol Jadib6be5c12012-11-14 13:39:51 -0800424 entry, ramdisk, ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800425
426 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700427
Amol Jadi4421e652011-06-16 15:00:48 -0700428 /* do any platform specific cleanup before kernel entry */
429 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700430
Brian Swetland9c4c0752009-01-25 16:23:50 -0800431 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700432
Amol Jadi504f9fe2012-08-16 13:56:48 -0700433#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800434 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700435#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700436
437 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800438}
439
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800440unsigned page_size = 0;
441unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800442
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800443#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800444
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800445static unsigned char buf[4096]; //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800446#if DEVICE_TREE
Neeti Desai465491e2012-07-31 12:53:35 -0700447static unsigned char dt_buf[4096];
Amol Jadib6be5c12012-11-14 13:39:51 -0800448#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800449
Shashank Mittal23b8f422010-04-16 19:27:21 -0700450int boot_linux_from_mmc(void)
451{
452 struct boot_img_hdr *hdr = (void*) buf;
453 struct boot_img_hdr *uhdr;
454 unsigned offset = 0;
455 unsigned long long ptn = 0;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700456 const char *cmdline;
Channagoud Kadabi35095622013-03-01 13:53:05 -0800457 void *tags;
Kinson Chikf1a43512011-07-14 11:28:39 -0700458 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700459
Shashank Mittalcd98d472011-08-02 14:29:24 -0700460 unsigned char *image_addr = 0;
461 unsigned kernel_actual;
462 unsigned ramdisk_actual;
463 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700464 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700465
466#if DEVICE_TREE
467 struct dt_table *table;
468 struct dt_entry *dt_entry_ptr;
469 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800470 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700471#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700472
Shashank Mittal23b8f422010-04-16 19:27:21 -0700473 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
474 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
475 dprintf(INFO, "Unified boot method!\n");
476 hdr = uhdr;
477 goto unified_boot;
478 }
Greg Griscod6250552011-06-29 14:40:23 -0700479 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700480 index = partition_get_index("boot");
481 ptn = partition_get_offset(index);
482 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700483 dprintf(CRITICAL, "ERROR: No boot partition found\n");
484 return -1;
485 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700486 }
487 else {
488 index = partition_get_index("recovery");
489 ptn = partition_get_offset(index);
490 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700491 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
492 return -1;
493 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700494 }
495
Greg Griscod6250552011-06-29 14:40:23 -0700496 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700497 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
498 return -1;
499 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700500
501 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700502 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700503 return -1;
504 }
505
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700506 if (hdr->page_size && (hdr->page_size != page_size)) {
507 page_size = hdr->page_size;
508 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700509 }
510
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700511 /* Get virtual addresses since the hdr saves physical addresses. */
512 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
513 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
514 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
515
Shashank Mittalcd98d472011-08-02 14:29:24 -0700516 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700517 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800518 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700519 image_addr = (unsigned char *)target_get_scratch_address();
520 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
521 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800522#if DEVICE_TREE
523 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
524 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
525#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530526 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800527#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700528 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700529
530 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700531 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700532
Amol Jadib6be5c12012-11-14 13:39:51 -0800533 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
534
Shashank Mittalcd98d472011-08-02 14:29:24 -0700535 /* Read image without signature */
536 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
537 {
538 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
539 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800540 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700541
Amol Jadib6be5c12012-11-14 13:39:51 -0800542 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
543
Shashank Mittalcd98d472011-08-02 14:29:24 -0700544 offset = imagesize_actual;
545 /* Read signature */
546 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
547 {
548 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
549 }
550 else
551 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800552 dprintf(INFO, "Authenticating boot image (%d): start\n", imagesize_actual);
553
Shashank Mittalcd98d472011-08-02 14:29:24 -0700554 auth_kernel_img = image_verify((unsigned char *)image_addr,
555 (unsigned char *)(image_addr + imagesize_actual),
556 imagesize_actual,
557 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700558
Amol Jadib6be5c12012-11-14 13:39:51 -0800559 dprintf(INFO, "Authenticating boot image (%d): done\n", imagesize_actual);
560
Shashank Mittal162244e2011-08-08 19:01:25 -0700561 if(auth_kernel_img)
562 {
563 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700564 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700565 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700566 }
567
Neeti Desai465491e2012-07-31 12:53:35 -0700568 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700569 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
570 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800571#if DEVICE_TREE
572 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
573#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700574
Neeti Desai465491e2012-07-31 12:53:35 -0700575 #if DEVICE_TREE
576 if(hdr->dt_size) {
577 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700578 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700579
580 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
581
582 /* Restriction that the device tree entry table should be less than a page*/
583 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
584
585 /* Validate the device tree table header */
586 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
587 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
588 return -1;
589 }
590
591 /* Find index of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700592 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700593 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
594 return -1;
595 }
596
597 /* Read device device tree in the "tags_add */
598 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800599 } else {
600 /*
601 * Look for appended device tree if DTB is not found in boot image
602 * If found load the kernel & boot up
603 */
604 app_dev_tree = dev_tree_appended((void*) hdr->kernel_addr);
605 if (!app_dev_tree) {
606 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
607 return -1;
608 }
Neeti Desai465491e2012-07-31 12:53:35 -0700609 }
610 #endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700611 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700612 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700613 {
614 write_device_info_mmc(&device);
615 #ifdef TZ_TAMPER_FUSE
616 set_tamper_fuse_cmd();
617 #endif
618 }
Channagoud Kadabibf695c62012-04-10 13:31:56 +0530619 #if USE_PCOM_SECBOOT
620 set_tamper_flag(device.is_tampered);
621 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700622 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700623 else
624 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800625 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
626 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
627 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700628
Amol Jadib6be5c12012-11-14 13:39:51 -0800629 dprintf(INFO, "Loading boot image (%d): start\n",
630 kernel_actual + ramdisk_actual);
631
632 offset = page_size;
633
634 /* Load kernel */
635 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700636 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
637 return -1;
638 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800639 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700640
Amol Jadib6be5c12012-11-14 13:39:51 -0800641 /* Load ramdisk */
642 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700643 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800644 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700645 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
646 return -1;
647 }
648 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800649 offset += ramdisk_actual;
650
651 dprintf(INFO, "Loading boot image (%d): done\n",
652 kernel_actual + ramdisk_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700653
654 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800655 offset += second_actual;
656 /* Second image loading not implemented. */
657 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700658 }
659
660 #if DEVICE_TREE
661 if(hdr->dt_size != 0) {
662
663 /* Read the device tree table into buffer */
664 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
665 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
666 return -1;
667 }
668 table = (struct dt_table*) dt_buf;
669
670 /* Restriction that the device tree entry table should be less than a page*/
671 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
672
673 /* Validate the device tree table header */
674 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
675 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
676 return -1;
677 }
678
679 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700680 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700681 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
682 return -1;
683 }
684
685 /* Read device device tree in the "tags_add */
Neeti Desai465491e2012-07-31 12:53:35 -0700686 if(mmc_read(ptn + offset + dt_entry_ptr->offset,
687 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
688 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
689 return -1;
690 }
Channagoud Kadabi35095622013-03-01 13:53:05 -0800691 } else {
692 /*
693 * Look for appended device tree if DTB is not found in boot image
694 * If found load the kernel & boot up
695 */
696 app_dev_tree = dev_tree_appended((void*) hdr->kernel_addr);
697 if (!app_dev_tree) {
698 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
699 return -1;
700 }
Neeti Desai465491e2012-07-31 12:53:35 -0700701 }
702 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700703 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700704
705unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700706
707 if(hdr->cmdline[0]) {
708 cmdline = (char*) hdr->cmdline;
709 } else {
710 cmdline = DEFAULT_CMDLINE;
711 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700712
Channagoud Kadabi35095622013-03-01 13:53:05 -0800713 /*
714 * If appended dev tree is found, update the atags with
715 * memory address to the DTB appended location on RAM.
716 * Else update with the atags address in the kernel header
717 */
718 if (app_dev_tree)
719 tags = (void *)app_dev_tree;
720 else
721 tags = (void *)hdr->tags_addr;
722
723 boot_linux((void *)hdr->kernel_addr, (unsigned *)tags,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700724 (const char *)cmdline, board_machtype(),
725 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
726
727 return 0;
728}
729
Dima Zavin214cc642009-01-26 11:16:21 -0800730int boot_linux_from_flash(void)
731{
732 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800733 struct ptentry *ptn;
734 struct ptable *ptable;
735 unsigned offset = 0;
736 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800737
Shashank Mittalcd98d472011-08-02 14:29:24 -0700738 unsigned char *image_addr = 0;
739 unsigned kernel_actual;
740 unsigned ramdisk_actual;
741 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800742 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700743
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700744#if DEVICE_TREE
745 struct dt_table *table;
746 struct dt_entry *dt_entry_ptr;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800747 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700748#endif
749
David Ng183a7422009-12-07 14:55:21 -0800750 if (target_is_emmc_boot()) {
751 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
752 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
753 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
754 return -1;
755 }
756 goto continue_boot;
757 }
758
Dima Zavin214cc642009-01-26 11:16:21 -0800759 ptable = flash_get_ptable();
760 if (ptable == NULL) {
761 dprintf(CRITICAL, "ERROR: Partition table not found\n");
762 return -1;
763 }
764
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800765 if(!boot_into_recovery)
766 {
767 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700768
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800769 if (ptn == NULL) {
770 dprintf(CRITICAL, "ERROR: No boot partition found\n");
771 return -1;
772 }
773 }
774 else
775 {
776 ptn = ptable_find(ptable, "recovery");
777 if (ptn == NULL) {
778 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
779 return -1;
780 }
Dima Zavin214cc642009-01-26 11:16:21 -0800781 }
782
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800783 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800784 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
785 return -1;
786 }
Dima Zavin214cc642009-01-26 11:16:21 -0800787
788 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700789 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800790 return -1;
791 }
792
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800793 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700794 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 -0800795 return -1;
796 }
797
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700798 /* Get virtual addresses since the hdr saves physical addresses. */
799 hdr->kernel_addr = VA(hdr->kernel_addr);
800 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
801 hdr->tags_addr = VA(hdr->tags_addr);
802
Shashank Mittalcd98d472011-08-02 14:29:24 -0700803 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700804 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700805 {
806 image_addr = (unsigned char *)target_get_scratch_address();
807 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
808 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800809#if DEVICE_TREE
810 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
811 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
812#else
Shashank Mittalcd98d472011-08-02 14:29:24 -0700813 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800814#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700815 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700816
817 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700818 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700819
Amol Jadib6be5c12012-11-14 13:39:51 -0800820 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
821
Shashank Mittalcd98d472011-08-02 14:29:24 -0700822 /* Read image without signature */
823 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
824 {
825 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
826 return -1;
827 }
Dima Zavin214cc642009-01-26 11:16:21 -0800828
Amol Jadib6be5c12012-11-14 13:39:51 -0800829 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
830
Shashank Mittalcd98d472011-08-02 14:29:24 -0700831 offset = imagesize_actual;
832 /* Read signature */
833 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
834 {
835 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
836 }
837 else
838 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800839 dprintf(INFO, "Authenticating boot image (%d): start\n", imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700840
841 /* Verify signature */
842 auth_kernel_img = image_verify((unsigned char *)image_addr,
843 (unsigned char *)(image_addr + imagesize_actual),
844 imagesize_actual,
845 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700846
Amol Jadib6be5c12012-11-14 13:39:51 -0800847 dprintf(INFO, "Authenticating boot image (%d): done\n", imagesize_actual);
848
Shashank Mittal162244e2011-08-08 19:01:25 -0700849 if(auth_kernel_img)
850 {
851 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700852 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700853 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700854 }
855
856 /* Move kernel and ramdisk to correct address */
857 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
858 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800859#if DEVICE_TREE
860 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
861#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700862
863 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700864 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700865 {
866 write_device_info_flash(&device);
867 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530868#if USE_PCOM_SECBOOT
869 set_tamper_flag(device.is_tampered);
870#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700871 }
872 else
873 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700874 offset = page_size;
875
Amol Jadib6be5c12012-11-14 13:39:51 -0800876 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
877 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
878 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
879
880 dprintf(INFO, "Loading boot image (%d): start\n",
881 kernel_actual + ramdisk_actual);
882
883 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700884 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
885 return -1;
886 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800887 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700888
Amol Jadib6be5c12012-11-14 13:39:51 -0800889 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700890 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
891 return -1;
892 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800893 offset += ramdisk_actual;
894
895 dprintf(INFO, "Loading boot image (%d): done\n",
896 kernel_actual + ramdisk_actual);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700897
898 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800899 offset += second_actual;
900 /* Second image loading not implemented. */
901 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700902 }
903
904#if DEVICE_TREE
905 if(hdr->dt_size != 0) {
906
907 /* Read the device tree table into buffer */
908 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
909 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
910 return -1;
911 }
912
913 table = (struct dt_table*) dt_buf;
914
915 /* Restriction that the device tree entry table should be less than a page*/
916 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
917
918 /* Validate the device tree table header */
919 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
920 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
921 return -1;
922 }
923
924 /* Calculate the offset of device tree within device tree table */
925 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
926 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
927 return -1;
928 }
929
930 /* Read device device tree in the "tags_add */
931 if(flash_read(ptn, offset + dt_entry_ptr->offset,
932 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
933 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
934 return -1;
935 }
936 }
937#endif
938
Shashank Mittalcd98d472011-08-02 14:29:24 -0700939 }
David Ng183a7422009-12-07 14:55:21 -0800940continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800941
942 if(hdr->cmdline[0]) {
943 cmdline = (char*) hdr->cmdline;
944 } else {
945 cmdline = DEFAULT_CMDLINE;
946 }
947 dprintf(INFO, "cmdline = '%s'\n", cmdline);
948
949 /* TODO: create/pass atags to kernel */
950
Ajay Dudanie28a6072011-07-01 13:59:46 -0700951 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800952 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800953 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
954
955 return 0;
956}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800957
Shashank Mittal162244e2011-08-08 19:01:25 -0700958unsigned char info_buf[4096];
959void write_device_info_mmc(device_info *dev)
960{
961 struct device_info *info = (void*) info_buf;
962 unsigned long long ptn = 0;
963 unsigned long long size;
964 int index = INVALID_PTN;
965
966 index = partition_get_index("aboot");
967 ptn = partition_get_offset(index);
968 if(ptn == 0)
969 {
970 return;
971 }
972
973 size = partition_get_size(index);
974
975 memcpy(info, dev, sizeof(device_info));
976
977 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
978 {
979 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
980 return;
981 }
982}
983
984void read_device_info_mmc(device_info *dev)
985{
986 struct device_info *info = (void*) info_buf;
987 unsigned long long ptn = 0;
988 unsigned long long size;
989 int index = INVALID_PTN;
990
991 index = partition_get_index("aboot");
992 ptn = partition_get_offset(index);
993 if(ptn == 0)
994 {
995 return;
996 }
997
998 size = partition_get_size(index);
999
1000 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1001 {
1002 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1003 return;
1004 }
1005
1006 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1007 {
1008 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1009 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001010 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001011
1012 write_device_info_mmc(info);
1013 }
1014 memcpy(dev, info, sizeof(device_info));
1015}
1016
1017void write_device_info_flash(device_info *dev)
1018{
1019 struct device_info *info = (void *) info_buf;
1020 struct ptentry *ptn;
1021 struct ptable *ptable;
1022
1023 ptable = flash_get_ptable();
1024 if (ptable == NULL)
1025 {
1026 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1027 return;
1028 }
1029
1030 ptn = ptable_find(ptable, "devinfo");
1031 if (ptn == NULL)
1032 {
1033 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1034 return;
1035 }
1036
1037 memcpy(info, dev, sizeof(device_info));
1038
1039 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1040 {
1041 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1042 return;
1043 }
1044}
1045
1046void read_device_info_flash(device_info *dev)
1047{
1048 struct device_info *info = (void*) info_buf;
1049 struct ptentry *ptn;
1050 struct ptable *ptable;
1051
1052 ptable = flash_get_ptable();
1053 if (ptable == NULL)
1054 {
1055 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1056 return;
1057 }
1058
1059 ptn = ptable_find(ptable, "devinfo");
1060 if (ptn == NULL)
1061 {
1062 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1063 return;
1064 }
1065
1066 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1067 {
1068 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1069 return;
1070 }
1071
1072 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1073 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001074 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1075 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001076 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001077 write_device_info_flash(info);
1078 }
1079 memcpy(dev, info, sizeof(device_info));
1080}
1081
1082void write_device_info(device_info *dev)
1083{
1084 if(target_is_emmc_boot())
1085 {
1086 write_device_info_mmc(dev);
1087 }
1088 else
1089 {
1090 write_device_info_flash(dev);
1091 }
1092}
1093
1094void read_device_info(device_info *dev)
1095{
1096 if(target_is_emmc_boot())
1097 {
1098 read_device_info_mmc(dev);
1099 }
1100 else
1101 {
1102 read_device_info_flash(dev);
1103 }
1104}
1105
1106void reset_device_info()
1107{
1108 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001109 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001110 write_device_info(&device);
1111}
1112
1113void set_device_root()
1114{
1115 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001116 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001117 write_device_info(&device);
1118}
1119
Amol Jadicb524072012-08-09 16:40:18 -07001120#if DEVICE_TREE
1121int copy_dtb(uint8_t *boot_image_start)
1122{
1123 uint32 dt_image_offset = 0;
1124 uint32_t n;
1125 struct dt_table *table;
1126 struct dt_entry *dt_entry_ptr;
Amol Jadicb524072012-08-09 16:40:18 -07001127
1128 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1129
Amol Jadicb524072012-08-09 16:40:18 -07001130 if(hdr->dt_size != 0) {
1131
1132 /* add kernel offset */
1133 dt_image_offset += page_size;
1134 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1135 dt_image_offset += n;
1136
1137 /* add ramdisk offset */
1138 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1139 dt_image_offset += n;
1140
1141 /* add second offset */
1142 if(hdr->second_size != 0) {
1143 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1144 dt_image_offset += n;
1145 }
1146
1147 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001148 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001149
1150 /* Restriction that the device tree entry table should be less than a page*/
1151 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1152
1153 /* Validate the device tree table header */
1154 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1155 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1156 return -1;
1157 }
1158
1159 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001160 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Amol Jadicb524072012-08-09 16:40:18 -07001161 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1162 return -1;
1163 }
1164
1165 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001166 memmove((void*) hdr->tags_addr,
1167 boot_image_start + dt_image_offset + dt_entry_ptr->offset,
1168 dt_entry_ptr->size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001169 } else {
1170 /*
1171 * Look for appended device tree if DTB is not found in boot image
1172 * If found load the kernel & boot up
1173 */
1174 memmove((void*) hdr->kernel_addr, boot_image_start + page_size, hdr->kernel_size);
1175 app_dev_tree = dev_tree_appended((void*) hdr->kernel_addr);
1176 if (!app_dev_tree) {
1177 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1178 return -1;
1179 }
Amol Jadicb524072012-08-09 16:40:18 -07001180 }
1181
1182 /* Everything looks fine. Return success. */
1183 return 0;
1184}
1185#endif
1186
Brian Swetland9c4c0752009-01-25 16:23:50 -08001187void cmd_boot(const char *arg, void *data, unsigned sz)
1188{
1189 unsigned kernel_actual;
1190 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001191 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001192 char *ptr = ((char*) data);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001193 void *tags;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001194
1195 if (sz < sizeof(hdr)) {
1196 fastboot_fail("invalid bootimage header");
1197 return;
1198 }
1199
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001200 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001201
1202 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001203 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001204
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001205 if(target_is_emmc_boot() && hdr->page_size) {
1206 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001207 page_mask = page_size - 1;
1208 }
1209
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001210 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1211 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1212
1213 /* Get virtual addresses since the hdr saves physical addresses. */
1214 hdr->kernel_addr = VA(hdr->kernel_addr);
1215 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1216 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001217
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001218 /* sz should have atleast raw boot image */
1219 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001220 fastboot_fail("incomplete bootimage");
1221 return;
1222 }
1223
Amol Jadicb524072012-08-09 16:40:18 -07001224#if DEVICE_TREE
1225 /* find correct dtb and copy it to right location */
1226 if(copy_dtb(data))
1227 {
1228 fastboot_fail("dtb not found");
1229 return;
1230 }
1231#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001232
1233 fastboot_okay("");
1234 udc_stop();
1235
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001236 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1237 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
Amol Jadicb524072012-08-09 16:40:18 -07001238
Channagoud Kadabi35095622013-03-01 13:53:05 -08001239 /*
1240 * If appended dev tree is found, update the atags with
1241 * memory address to the DTB appended location on RAM.
1242 * Else update with the atags address in the kernel header
1243 */
1244 if (app_dev_tree)
1245 tags = (void *)app_dev_tree;
1246 else
1247 tags = (void *)hdr->tags_addr;
1248
1249 boot_linux((void*) hdr->kernel_addr, (void*) tags,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001250 (const char*) hdr->cmdline, board_machtype(),
1251 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001252}
1253
Dima Zavin214cc642009-01-26 11:16:21 -08001254void cmd_erase(const char *arg, void *data, unsigned sz)
1255{
1256 struct ptentry *ptn;
1257 struct ptable *ptable;
1258
1259 ptable = flash_get_ptable();
1260 if (ptable == NULL) {
1261 fastboot_fail("partition table doesn't exist");
1262 return;
1263 }
1264
1265 ptn = ptable_find(ptable, arg);
1266 if (ptn == NULL) {
1267 fastboot_fail("unknown partition name");
1268 return;
1269 }
1270
1271 if (flash_erase(ptn)) {
1272 fastboot_fail("failed to erase partition");
1273 return;
1274 }
1275 fastboot_okay("");
1276}
1277
Bikas Gurungd48bd242010-09-04 19:54:32 -07001278
1279void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1280{
1281 unsigned long long ptn = 0;
neetidb4b24d62012-01-20 12:13:09 -08001282 unsigned int out[512] = {0};
Kinson Chikf1a43512011-07-14 11:28:39 -07001283 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001284
Kinson Chikf1a43512011-07-14 11:28:39 -07001285 index = partition_get_index(arg);
1286 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001287
Kinson Chikf1a43512011-07-14 11:28:39 -07001288 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001289 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001290 return;
1291 }
neetidb4b24d62012-01-20 12:13:09 -08001292 /* Simple inefficient version of erase. Just writing
1293 0 in first block */
1294 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1295 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001296 return;
1297 }
1298 fastboot_okay("");
1299}
1300
1301
Ajay Dudani5c761132011-04-07 20:19:04 -07001302void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001303{
1304 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001305 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001306 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001307
Greg Grisco6e754772011-06-23 12:19:39 -07001308 if (!strcmp(arg, "partition"))
1309 {
1310 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001311 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001312 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001313 return;
1314 }
1315 }
Greg Grisco6e754772011-06-23 12:19:39 -07001316 else
1317 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001318 index = partition_get_index(arg);
1319 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001320 if(ptn == 0) {
1321 fastboot_fail("partition table doesn't exist");
1322 return;
1323 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001324
Greg Grisco6e754772011-06-23 12:19:39 -07001325 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1326 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1327 fastboot_fail("image is not a boot image");
1328 return;
1329 }
1330 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001331
Kinson Chikf1a43512011-07-14 11:28:39 -07001332 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001333 if (ROUND_TO_PAGE(sz,511) > size) {
1334 fastboot_fail("size too large");
1335 return;
1336 }
1337 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1338 fastboot_fail("flash write failure");
1339 return;
1340 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001341 }
1342 fastboot_okay("");
1343 return;
1344}
1345
Ajay Dudani5c761132011-04-07 20:19:04 -07001346void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1347{
1348 unsigned int chunk;
1349 unsigned int chunk_data_sz;
1350 sparse_header_t *sparse_header;
1351 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001352 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001353 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301354 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001355 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001356
Kinson Chikf1a43512011-07-14 11:28:39 -07001357 index = partition_get_index(arg);
1358 ptn = partition_get_offset(index);
1359 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001360 fastboot_fail("partition table doesn't exist");
1361 return;
1362 }
1363
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301364 size = partition_get_size(index);
1365 if (ROUND_TO_PAGE(sz,511) > size) {
1366 fastboot_fail("size too large");
1367 return;
1368 }
1369
Ajay Dudani5c761132011-04-07 20:19:04 -07001370 /* Read and skip over sparse image header */
1371 sparse_header = (sparse_header_t *) data;
1372 data += sparse_header->file_hdr_sz;
1373 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1374 {
1375 /* Skip the remaining bytes in a header that is longer than
1376 * we expected.
1377 */
1378 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1379 }
1380
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001381 dprintf (SPEW, "=== Sparse Image Header ===\n");
1382 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1383 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1384 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1385 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1386 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1387 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1388 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1389 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001390
1391 /* Start processing chunks */
1392 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1393 {
1394 /* Read and skip over chunk header */
1395 chunk_header = (chunk_header_t *) data;
1396 data += sizeof(chunk_header_t);
1397
1398 dprintf (SPEW, "=== Chunk Header ===\n");
1399 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1400 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1401 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1402
1403 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1404 {
1405 /* Skip the remaining bytes in a header that is longer than
1406 * we expected.
1407 */
1408 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1409 }
1410
1411 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1412 switch (chunk_header->chunk_type)
1413 {
1414 case CHUNK_TYPE_RAW:
1415 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1416 chunk_data_sz))
1417 {
1418 fastboot_fail("Bogus chunk size for chunk type Raw");
1419 return;
1420 }
1421
Ajay Dudaniab18f022011-05-12 14:39:22 -07001422 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1423 chunk_data_sz,
1424 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001425 {
1426 fastboot_fail("flash write failure");
1427 return;
1428 }
1429 total_blocks += chunk_header->chunk_sz;
1430 data += chunk_data_sz;
1431 break;
1432
1433 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001434 total_blocks += chunk_header->chunk_sz;
1435 break;
1436
Ajay Dudani5c761132011-04-07 20:19:04 -07001437 case CHUNK_TYPE_CRC:
1438 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1439 {
1440 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1441 return;
1442 }
1443 total_blocks += chunk_header->chunk_sz;
1444 data += chunk_data_sz;
1445 break;
1446
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001447 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001448 fastboot_fail("Unknown chunk type");
1449 return;
1450 }
1451 }
1452
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001453 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1454 total_blocks, sparse_header->total_blks);
1455
1456 if(total_blocks != sparse_header->total_blks)
1457 {
1458 fastboot_fail("sparse image write failure");
1459 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001460
1461 fastboot_okay("");
1462 return;
1463}
1464
1465void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1466{
1467 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001468 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1469 unsigned int *magic_number = (unsigned int *) data;
1470 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001471
Neeti Desai127b9e02012-03-20 16:11:23 -07001472 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1473 magic_number[1] == DECRYPT_MAGIC_1)
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001474 {
1475#ifdef SSD_ENABLE
Neeti Desai127b9e02012-03-20 16:11:23 -07001476 ret = decrypt_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001477#endif
Greg Griscod6250552011-06-29 14:40:23 -07001478 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001479 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1480 return;
1481 }
1482 }
Neeti Desai127b9e02012-03-20 16:11:23 -07001483 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1484 magic_number[1] == ENCRYPT_MAGIC_1)
1485 {
1486#ifdef SSD_ENABLE
1487 ret = encrypt_scm((uint32 **) &data, &sz);
1488#endif
1489 if (ret != 0) {
1490 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1491 return;
1492 }
1493 }
1494
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001495 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001496 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1497 cmd_flash_mmc_img(arg, data, sz);
1498 else
1499 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001500 return;
1501}
1502
Dima Zavin214cc642009-01-26 11:16:21 -08001503void cmd_flash(const char *arg, void *data, unsigned sz)
1504{
1505 struct ptentry *ptn;
1506 struct ptable *ptable;
1507 unsigned extra = 0;
1508
1509 ptable = flash_get_ptable();
1510 if (ptable == NULL) {
1511 fastboot_fail("partition table doesn't exist");
1512 return;
1513 }
1514
1515 ptn = ptable_find(ptable, arg);
1516 if (ptn == NULL) {
1517 fastboot_fail("unknown partition name");
1518 return;
1519 }
1520
1521 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1522 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1523 fastboot_fail("image is not a boot image");
1524 return;
1525 }
1526 }
1527
Amol Jadi5c61a952012-05-04 17:05:35 -07001528 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001529 || !strcmp(ptn->name, "userdata")
1530 || !strcmp(ptn->name, "persist")
1531 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301532 if (flash_ecc_bch_enabled())
1533 /* Spare data bytes for 8 bit ECC increased by 4 */
1534 extra = ((page_size >> 9) * 20);
1535 else
1536 extra = ((page_size >> 9) * 16);
1537 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001538 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001539
1540 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1541 if (flash_write(ptn, extra, data, sz)) {
1542 fastboot_fail("flash write failure");
1543 return;
1544 }
1545 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1546 fastboot_okay("");
1547}
1548
1549void cmd_continue(const char *arg, void *data, unsigned sz)
1550{
1551 fastboot_okay("");
1552 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001553 if (target_is_emmc_boot())
1554 {
1555 boot_linux_from_mmc();
1556 }
1557 else
1558 {
1559 boot_linux_from_flash();
1560 }
Dima Zavin214cc642009-01-26 11:16:21 -08001561}
1562
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001563void cmd_reboot(const char *arg, void *data, unsigned sz)
1564{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001565 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001566 fastboot_okay("");
1567 reboot_device(0);
1568}
1569
1570void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1571{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001572 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001573 fastboot_okay("");
1574 reboot_device(FASTBOOT_MODE);
1575}
1576
Shashank Mittal162244e2011-08-08 19:01:25 -07001577void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1578{
1579 if(!device.is_unlocked)
1580 {
1581 device.is_unlocked = 1;
1582 write_device_info(&device);
1583 }
1584 fastboot_okay("");
1585}
1586
Shashank Mittala0032282011-08-26 14:50:11 -07001587void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1588{
1589 char response[64];
1590 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1591 fastboot_info(response);
1592 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1593 fastboot_info(response);
1594 fastboot_okay("");
1595}
1596
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001597void splash_screen ()
1598{
1599 struct ptentry *ptn;
1600 struct ptable *ptable;
1601 struct fbcon_config *fb_display = NULL;
1602
1603 if (!target_is_emmc_boot())
1604 {
1605 ptable = flash_get_ptable();
1606 if (ptable == NULL) {
1607 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001608 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001609 }
1610
1611 ptn = ptable_find(ptable, "splash");
1612 if (ptn == NULL) {
1613 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1614 } else {
1615 fb_display = fbcon_display();
1616 if (fb_display) {
1617 if (flash_read(ptn, 0, fb_display->base,
1618 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1619 fbcon_clear();
1620 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1621 }
1622 }
1623 }
1624 }
1625}
1626
Brian Swetland9c4c0752009-01-25 16:23:50 -08001627void aboot_init(const struct app_descriptor *app)
1628{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001629 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001630 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001631 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001632
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001633 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001634 if (target_is_emmc_boot())
1635 {
1636 page_size = 2048;
1637 page_mask = page_size - 1;
1638 }
1639 else
1640 {
1641 page_size = flash_page_size();
1642 page_mask = page_size - 1;
1643 }
1644
Shashank Mittal162244e2011-08-08 19:01:25 -07001645 if(target_use_signed_kernel())
1646 {
1647 read_device_info(&device);
1648
Shashank Mittal162244e2011-08-08 19:01:25 -07001649 }
1650
Greg Griscod6250552011-06-29 14:40:23 -07001651 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001652 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001653 surf_udc_device.serialno = sn_buf;
1654
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001655 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001656 if (keys_get_state(KEY_HOME) != 0)
1657 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001658 if (keys_get_state(KEY_VOLUMEUP) != 0)
1659 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001660 if(!boot_into_recovery)
1661 {
1662 if (keys_get_state(KEY_BACK) != 0)
1663 goto fastboot;
1664 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1665 goto fastboot;
1666 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001667
1668 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001669 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001670 goto fastboot;
1671 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001672
Ajay Dudani77421292010-10-27 19:34:06 -07001673 reboot_mode = check_reboot_mode();
1674 if (reboot_mode == RECOVERY_MODE) {
1675 boot_into_recovery = 1;
1676 } else if(reboot_mode == FASTBOOT_MODE) {
1677 goto fastboot;
1678 }
1679
Shashank Mittal23b8f422010-04-16 19:27:21 -07001680 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001681 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001682 if(emmc_recovery_init())
1683 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001684 if(target_use_signed_kernel())
1685 {
1686 if((device.is_unlocked) || (device.is_tampered))
1687 {
1688 #ifdef TZ_TAMPER_FUSE
1689 set_tamper_fuse_cmd();
1690 #endif
Channagoud Kadabibf695c62012-04-10 13:31:56 +05301691 #if USE_PCOM_SECBOOT
1692 set_tamper_flag(device.is_tampered);
1693 #endif
Shashank Mittala0032282011-08-26 14:50:11 -07001694 }
1695 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001696 boot_linux_from_mmc();
1697 }
1698 else
1699 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001700 recovery_init();
Channagoud Kadabie7b66702012-03-22 15:54:30 +05301701#if USE_PCOM_SECBOOT
1702 if((device.is_unlocked) || (device.is_tampered))
1703 set_tamper_flag(device.is_tampered);
1704#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001705 boot_linux_from_flash();
1706 }
Dima Zavinb4283602009-01-26 16:36:57 -08001707 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1708 "to fastboot mode.\n");
1709
1710fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001711
Shashank Mittal162244e2011-08-08 19:01:25 -07001712 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001713
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001714 if(!usb_init)
1715 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001716
1717 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001718
Shashank Mittal23b8f422010-04-16 19:27:21 -07001719 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001720 {
1721 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001722 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001723 }
1724 else
1725 {
1726 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001727 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001728 }
1729
1730 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001731 fastboot_register("reboot", cmd_reboot);
1732 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001733 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001734 fastboot_register("oem device-info", cmd_oem_devinfo);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001735 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001736 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001737 fastboot_publish("serialno", sn_buf);
Kinson Chikf1a43512011-07-14 11:28:39 -07001738 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001739 sz = target_get_max_flash_size();
1740 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001741 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001742}
1743
1744APP_START(aboot)
1745 .init = aboot_init,
1746APP_END