blob: 9a45feb0248caf10db3406a32491cab608681791 [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>
Amol Jadi492d5a52013-03-15 16:12:34 -070052#include <boot_stats.h>
Dima Zavin214cc642009-01-26 11:16:21 -080053
Neeti Desai17379b82012-06-04 18:42:53 -070054#if DEVICE_TREE
55#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070056#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070057#endif
58
Shashank Mittalcd98d472011-08-02 14:29:24 -070059#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080060#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080061#include "bootimg.h"
62#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070063#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070064#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070065#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070066#include "board.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080067
Shashank Mittal162244e2011-08-08 19:01:25 -070068#include "scm.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070069
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070070extern bool target_use_signed_kernel(void);
71extern void dsb();
72extern void isb();
73extern void platform_uninit(void);
74
75void write_device_info_mmc(device_info *dev);
76void write_device_info_flash(device_info *dev);
77
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070078#define EXPAND(NAME) #NAME
79#define TARGET(NAME) EXPAND(NAME)
Chandan Uddarajuda919832009-11-17 01:06:11 -080080#define DEFAULT_CMDLINE "mem=100M console=null";
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=";
Ajay Dudanica3a33c2011-11-18 08:31:40 -080093static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -070094static const char *auth_kernel = " androidboot.authorized_kernel=true";
David Ng183a7422009-12-07 14:55:21 -080095
Ajay Dudani6cff85e2011-02-04 16:02:16 -080096static const char *baseband_apq = " androidboot.baseband=apq";
97static const char *baseband_msm = " androidboot.baseband=msm";
98static const char *baseband_csfb = " androidboot.baseband=csfb";
99static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700100static const char *baseband_mdm = " androidboot.baseband=mdm";
Amol Jadi5c61a952012-05-04 17:05:35 -0700101static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800102static const char *baseband_dsda = " androidboot.baseband=dsda";
103static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800104static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800105
Shashank Mittalcd98d472011-08-02 14:29:24 -0700106/* Assuming unauthorized kernel image by default */
107static int auth_kernel_img = 0;
108
Channagoud Kadabi35095622013-03-01 13:53:05 -0800109static uint32_t app_dev_tree = 0;
110
Shashank Mittal162244e2011-08-08 19:01:25 -0700111static device_info device = {DEVICE_MAGIC, 0, 0};
112
Brian Swetland9c4c0752009-01-25 16:23:50 -0800113static struct udc_device surf_udc_device = {
114 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -0800115 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800116 .version_id = 0x0100,
117 .manufacturer = "Google",
118 .product = "Android",
119};
120
Dima Zavin42168f22009-01-30 11:52:22 -0800121struct atag_ptbl_entry
122{
123 char name[16];
124 unsigned offset;
125 unsigned size;
126 unsigned flags;
127};
128
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800129char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700130
Greg Griscod2471ef2011-07-14 13:00:42 -0700131extern int emmc_recovery_init(void);
132
Kinson Chik0b1c8162011-08-31 16:31:57 -0700133#if NO_KEYPAD_DRIVER
134extern int fastboot_trigger(void);
135#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700136
Dima Zavin42168f22009-01-30 11:52:22 -0800137static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
138{
139 struct atag_ptbl_entry atag_ptn;
140
141 memcpy(atag_ptn.name, ptn->name, 16);
142 atag_ptn.name[15] = '\0';
143 atag_ptn.offset = ptn->start;
144 atag_ptn.size = ptn->length;
145 atag_ptn.flags = ptn->flags;
146 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
147 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
148}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800149
Neeti Desaie245d492012-06-01 12:52:13 -0700150unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800151{
David Ng183a7422009-12-07 14:55:21 -0800152 int cmdline_len = 0;
153 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800154 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700155 int pause_at_bootup = 0;
Dima Zavin42168f22009-01-30 11:52:22 -0800156
Brian Swetland9c4c0752009-01-25 16:23:50 -0800157 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800158 cmdline_len = strlen(cmdline);
159 have_cmdline = 1;
160 }
161 if (target_is_emmc_boot()) {
162 cmdline_len += strlen(emmc_cmdline);
163 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800164
165 cmdline_len += strlen(usb_sn_cmdline);
166 cmdline_len += strlen(sn_buf);
167
David Ngf773dde2010-07-26 19:55:08 -0700168 if (target_pause_for_battery_charge()) {
169 pause_at_bootup = 1;
170 cmdline_len += strlen(battchg_pause);
171 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800172
Shashank Mittalcd98d472011-08-02 14:29:24 -0700173 if(target_use_signed_kernel() && auth_kernel_img) {
174 cmdline_len += strlen(auth_kernel);
175 }
176
Ajay Dudanid04110c2011-01-17 23:55:07 -0800177 /* Determine correct androidboot.baseband to use */
178 switch(target_baseband())
179 {
180 case BASEBAND_APQ:
181 cmdline_len += strlen(baseband_apq);
182 break;
183
184 case BASEBAND_MSM:
185 cmdline_len += strlen(baseband_msm);
186 break;
187
188 case BASEBAND_CSFB:
189 cmdline_len += strlen(baseband_csfb);
190 break;
191
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800192 case BASEBAND_SVLTE2A:
193 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800194 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700195
196 case BASEBAND_MDM:
197 cmdline_len += strlen(baseband_mdm);
198 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700199
200 case BASEBAND_SGLTE:
201 cmdline_len += strlen(baseband_sglte);
202 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530203
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800204 case BASEBAND_SGLTE2:
205 cmdline_len += strlen(baseband_sglte2);
206 break;
207
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530208 case BASEBAND_DSDA:
209 cmdline_len += strlen(baseband_dsda);
210 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800211
212 case BASEBAND_DSDA2:
213 cmdline_len += strlen(baseband_dsda2);
214 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800215 }
216
David Ng183a7422009-12-07 14:55:21 -0800217 if (cmdline_len > 0) {
218 const char *src;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700219 unsigned char *dst = (unsigned char*) malloc((cmdline_len + 4) & (~3));
220 ASSERT(dst != NULL);
Neeti Desaie245d492012-06-01 12:52:13 -0700221
Amol Jadi168b7712012-03-06 16:15:00 -0800222 /* Save start ptr for debug print */
Neeti Desaie245d492012-06-01 12:52:13 -0700223 cmdline_final = dst;
David Ng183a7422009-12-07 14:55:21 -0800224 if (have_cmdline) {
225 src = cmdline;
226 while ((*dst++ = *src++));
227 }
228 if (target_is_emmc_boot()) {
229 src = emmc_cmdline;
230 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700231 have_cmdline = 1;
232 while ((*dst++ = *src++));
233 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800234
235 src = usb_sn_cmdline;
236 if (have_cmdline) --dst;
237 have_cmdline = 1;
238 while ((*dst++ = *src++));
239 src = sn_buf;
240 if (have_cmdline) --dst;
241 have_cmdline = 1;
242 while ((*dst++ = *src++));
243
David Ngf773dde2010-07-26 19:55:08 -0700244 if (pause_at_bootup) {
245 src = battchg_pause;
246 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800247 while ((*dst++ = *src++));
248 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800249
Shashank Mittalcd98d472011-08-02 14:29:24 -0700250 if(target_use_signed_kernel() && auth_kernel_img) {
251 src = auth_kernel;
252 if (have_cmdline) --dst;
253 while ((*dst++ = *src++));
254 }
255
Ajay Dudanid04110c2011-01-17 23:55:07 -0800256 switch(target_baseband())
257 {
258 case BASEBAND_APQ:
259 src = baseband_apq;
260 if (have_cmdline) --dst;
261 while ((*dst++ = *src++));
262 break;
263
264 case BASEBAND_MSM:
265 src = baseband_msm;
266 if (have_cmdline) --dst;
267 while ((*dst++ = *src++));
268 break;
269
270 case BASEBAND_CSFB:
271 src = baseband_csfb;
272 if (have_cmdline) --dst;
273 while ((*dst++ = *src++));
274 break;
275
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800276 case BASEBAND_SVLTE2A:
277 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800278 if (have_cmdline) --dst;
279 while ((*dst++ = *src++));
280 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700281
282 case BASEBAND_MDM:
283 src = baseband_mdm;
284 if (have_cmdline) --dst;
285 while ((*dst++ = *src++));
286 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700287
288 case BASEBAND_SGLTE:
289 src = baseband_sglte;
290 if (have_cmdline) --dst;
291 while ((*dst++ = *src++));
292 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530293
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800294 case BASEBAND_SGLTE2:
295 src = baseband_sglte2;
296 if (have_cmdline) --dst;
297 while ((*dst++ = *src++));
298 break;
299
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530300 case BASEBAND_DSDA:
301 src = baseband_dsda;
302 if (have_cmdline) --dst;
303 while ((*dst++ = *src++));
304 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800305
306 case BASEBAND_DSDA2:
307 src = baseband_dsda2;
308 if (have_cmdline) --dst;
309 while ((*dst++ = *src++));
310 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800311 }
Neeti Desaie245d492012-06-01 12:52:13 -0700312 }
Deepa Dinamani8b8736d2012-12-19 15:00:56 -0800313
314 dprintf(INFO, "cmdline: %s\n", cmdline_final);
315
Neeti Desaie245d492012-06-01 12:52:13 -0700316 return cmdline_final;
317}
318
319unsigned *atag_core(unsigned *ptr)
320{
321 /* CORE */
322 *ptr++ = 2;
323 *ptr++ = 0x54410001;
324
325 return ptr;
326
327}
328
329unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
330 unsigned ramdisk_size)
331{
332 if (ramdisk_size) {
333 *ptr++ = 4;
334 *ptr++ = 0x54420005;
335 *ptr++ = (unsigned)ramdisk;
336 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800337 }
338
Neeti Desaie245d492012-06-01 12:52:13 -0700339 return ptr;
340}
341
342unsigned *atag_ptable(unsigned **ptr_addr)
343{
344 int i;
345 struct ptable *ptable;
346
347 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700348 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
349 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700350 *(*ptr_addr)++ = 0x4d534d70;
351 for (i = 0; i < ptable->count; ++i)
352 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
353 }
354
355 return (*ptr_addr);
356}
357
358unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
359{
360 int cmdline_length = 0;
361 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700362 char *dest;
363
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800364 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700365 n = (cmdline_length + 4) & (~3);
366
367 *ptr++ = (n / 4) + 2;
368 *ptr++ = 0x54410009;
369 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800370 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700371 ptr += (n / 4);
372
373 return ptr;
374}
375
376unsigned *atag_end(unsigned *ptr)
377{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800378 /* END */
379 *ptr++ = 0;
380 *ptr++ = 0;
381
Neeti Desaie245d492012-06-01 12:52:13 -0700382 return ptr;
383}
384
385void generate_atags(unsigned *ptr, const char *cmdline,
386 void *ramdisk, unsigned ramdisk_size)
387{
388
389 ptr = atag_core(ptr);
390 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
391 ptr = target_atag_mem(ptr);
392
393 /* Skip NAND partition ATAGS for eMMC boot */
394 if (!target_is_emmc_boot()){
395 ptr = atag_ptable(&ptr);
396 }
397
398 ptr = atag_cmdline(ptr, cmdline);
399 ptr = atag_end(ptr);
400}
401
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700402typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700403void boot_linux(void *kernel, unsigned *tags,
404 const char *cmdline, unsigned machtype,
405 void *ramdisk, unsigned ramdisk_size)
406{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800407 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800408#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700409 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800410#endif
411
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700412 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800413 uint32_t tags_phys = PA((addr_t)tags);
414
415 ramdisk = PA(ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700416
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800417 final_cmdline = update_cmdline((const char*)cmdline);
418
Neeti Desai17379b82012-06-04 18:42:53 -0700419#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800420 dprintf(INFO, "Updating device tree: start\n");
421
Neeti Desai17379b82012-06-04 18:42:53 -0700422 /* Update the Device Tree */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800423 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700424 if(ret)
425 {
426 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
427 ASSERT(0);
428 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800429 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700430#else
Neeti Desaie245d492012-06-01 12:52:13 -0700431 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800432 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700433#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700434
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800435 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
436 entry, ramdisk, ramdisk_size, tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800437
438 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700439
Amol Jadi4421e652011-06-16 15:00:48 -0700440 /* do any platform specific cleanup before kernel entry */
441 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700442
Brian Swetland9c4c0752009-01-25 16:23:50 -0800443 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700444
Amol Jadi504f9fe2012-08-16 13:56:48 -0700445#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800446 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700447#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700448 bs_set_timestamp(BS_KERNEL_ENTRY);
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700449 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800450}
451
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800452unsigned page_size = 0;
453unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800454
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800455#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800456
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800457static unsigned char buf[4096]; //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800458#if DEVICE_TREE
Neeti Desai465491e2012-07-31 12:53:35 -0700459static unsigned char dt_buf[4096];
Amol Jadib6be5c12012-11-14 13:39:51 -0800460#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800461
Shashank Mittal23b8f422010-04-16 19:27:21 -0700462int boot_linux_from_mmc(void)
463{
464 struct boot_img_hdr *hdr = (void*) buf;
465 struct boot_img_hdr *uhdr;
466 unsigned offset = 0;
467 unsigned long long ptn = 0;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700468 const char *cmdline;
Channagoud Kadabi35095622013-03-01 13:53:05 -0800469 void *tags;
Kinson Chikf1a43512011-07-14 11:28:39 -0700470 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700471
Shashank Mittalcd98d472011-08-02 14:29:24 -0700472 unsigned char *image_addr = 0;
473 unsigned kernel_actual;
474 unsigned ramdisk_actual;
475 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700476 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -0700477
478#if DEVICE_TREE
479 struct dt_table *table;
480 struct dt_entry *dt_entry_ptr;
481 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800482 uint32_t dt_actual;
Neeti Desai465491e2012-07-31 12:53:35 -0700483#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700484
Shashank Mittal23b8f422010-04-16 19:27:21 -0700485 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
486 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
487 dprintf(INFO, "Unified boot method!\n");
488 hdr = uhdr;
489 goto unified_boot;
490 }
Greg Griscod6250552011-06-29 14:40:23 -0700491 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700492 index = partition_get_index("boot");
493 ptn = partition_get_offset(index);
494 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700495 dprintf(CRITICAL, "ERROR: No boot partition found\n");
496 return -1;
497 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700498 }
499 else {
500 index = partition_get_index("recovery");
501 ptn = partition_get_offset(index);
502 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700503 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
504 return -1;
505 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700506 }
507
Greg Griscod6250552011-06-29 14:40:23 -0700508 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700509 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
510 return -1;
511 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700512
513 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700514 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700515 return -1;
516 }
517
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700518 if (hdr->page_size && (hdr->page_size != page_size)) {
519 page_size = hdr->page_size;
520 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700521 }
522
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700523 /* Get virtual addresses since the hdr saves physical addresses. */
524 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
525 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
526 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
527
Shashank Mittalcd98d472011-08-02 14:29:24 -0700528 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700529 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800530 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700531 image_addr = (unsigned char *)target_get_scratch_address();
532 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
533 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800534#if DEVICE_TREE
535 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
536 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
537#else
Channagoud Kadabi4b276512012-08-28 15:16:30 +0530538 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800539#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700540 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700541
542 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700543 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700544
Amol Jadib6be5c12012-11-14 13:39:51 -0800545 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700546 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800547
Shashank Mittalcd98d472011-08-02 14:29:24 -0700548 /* Read image without signature */
549 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
550 {
551 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
552 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800553 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700554
Amol Jadib6be5c12012-11-14 13:39:51 -0800555 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700556 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800557
Shashank Mittalcd98d472011-08-02 14:29:24 -0700558 offset = imagesize_actual;
559 /* Read signature */
560 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
561 {
562 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
563 }
564 else
565 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800566 dprintf(INFO, "Authenticating boot image (%d): start\n", imagesize_actual);
567
Shashank Mittalcd98d472011-08-02 14:29:24 -0700568 auth_kernel_img = image_verify((unsigned char *)image_addr,
569 (unsigned char *)(image_addr + imagesize_actual),
570 imagesize_actual,
571 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700572
Amol Jadib6be5c12012-11-14 13:39:51 -0800573 dprintf(INFO, "Authenticating boot image (%d): done\n", imagesize_actual);
574
Shashank Mittal162244e2011-08-08 19:01:25 -0700575 if(auth_kernel_img)
576 {
577 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700578 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700579 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700580 }
581
Neeti Desai465491e2012-07-31 12:53:35 -0700582 /* Move kernel, ramdisk and device tree to correct address */
Shashank Mittalcd98d472011-08-02 14:29:24 -0700583 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
584 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800585#if DEVICE_TREE
586 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
587#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700588
Neeti Desai465491e2012-07-31 12:53:35 -0700589 #if DEVICE_TREE
590 if(hdr->dt_size) {
591 table = (struct dt_table*) dt_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700592 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
Neeti Desai465491e2012-07-31 12:53:35 -0700593
594 memmove((void *) dt_buf, (char *)dt_table_offset, page_size);
595
596 /* Restriction that the device tree entry table should be less than a page*/
597 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
598
599 /* Validate the device tree table header */
600 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
601 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
602 return -1;
603 }
604
605 /* Find index of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700606 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700607 dprintf(CRITICAL, "ERROR: Device Tree Blob cannot be found\n");
608 return -1;
609 }
610
611 /* Read device device tree in the "tags_add */
612 memmove((void *)hdr->tags_addr, (char *)dt_table_offset + dt_entry_ptr->offset, dt_entry_ptr->size);
Channagoud Kadabi35095622013-03-01 13:53:05 -0800613 } else {
614 /*
615 * Look for appended device tree if DTB is not found in boot image
616 * If found load the kernel & boot up
617 */
618 app_dev_tree = dev_tree_appended((void*) hdr->kernel_addr);
619 if (!app_dev_tree) {
620 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
621 return -1;
622 }
Neeti Desai465491e2012-07-31 12:53:35 -0700623 }
624 #endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700625 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700626 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700627 {
628 write_device_info_mmc(&device);
629 #ifdef TZ_TAMPER_FUSE
630 set_tamper_fuse_cmd();
631 #endif
632 }
Channagoud Kadabibf695c62012-04-10 13:31:56 +0530633 #if USE_PCOM_SECBOOT
634 set_tamper_flag(device.is_tampered);
635 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -0700636 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700637 else
638 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800639 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
640 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
641 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700642
Amol Jadib6be5c12012-11-14 13:39:51 -0800643 dprintf(INFO, "Loading boot image (%d): start\n",
644 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700645 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800646
647 offset = page_size;
648
649 /* Load kernel */
650 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700651 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
652 return -1;
653 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800654 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700655
Amol Jadib6be5c12012-11-14 13:39:51 -0800656 /* Load ramdisk */
657 if(ramdisk_actual != 0)
Shashank Mittalcd98d472011-08-02 14:29:24 -0700658 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800659 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700660 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
661 return -1;
662 }
663 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800664 offset += ramdisk_actual;
665
666 dprintf(INFO, "Loading boot image (%d): done\n",
667 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700668 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Neeti Desai465491e2012-07-31 12:53:35 -0700669
670 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800671 offset += second_actual;
672 /* Second image loading not implemented. */
673 ASSERT(0);
Neeti Desai465491e2012-07-31 12:53:35 -0700674 }
675
676 #if DEVICE_TREE
677 if(hdr->dt_size != 0) {
678
679 /* Read the device tree table into buffer */
680 if(mmc_read(ptn + offset,(unsigned int *) dt_buf, page_size)) {
681 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
682 return -1;
683 }
684 table = (struct dt_table*) dt_buf;
685
686 /* Restriction that the device tree entry table should be less than a page*/
687 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
688
689 /* Validate the device tree table header */
690 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
691 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
692 return -1;
693 }
694
695 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700696 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Neeti Desai465491e2012-07-31 12:53:35 -0700697 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
698 return -1;
699 }
700
701 /* Read device device tree in the "tags_add */
Neeti Desai465491e2012-07-31 12:53:35 -0700702 if(mmc_read(ptn + offset + dt_entry_ptr->offset,
703 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
704 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
705 return -1;
706 }
Channagoud Kadabi35095622013-03-01 13:53:05 -0800707 } else {
708 /*
709 * Look for appended device tree if DTB is not found in boot image
710 * If found load the kernel & boot up
711 */
712 app_dev_tree = dev_tree_appended((void*) hdr->kernel_addr);
713 if (!app_dev_tree) {
714 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
715 return -1;
716 }
Neeti Desai465491e2012-07-31 12:53:35 -0700717 }
718 #endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700719 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700720
721unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -0700722
723 if(hdr->cmdline[0]) {
724 cmdline = (char*) hdr->cmdline;
725 } else {
726 cmdline = DEFAULT_CMDLINE;
727 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700728
Channagoud Kadabi35095622013-03-01 13:53:05 -0800729 /*
730 * If appended dev tree is found, update the atags with
731 * memory address to the DTB appended location on RAM.
732 * Else update with the atags address in the kernel header
733 */
734 if (app_dev_tree)
735 tags = (void *)app_dev_tree;
736 else
737 tags = (void *)hdr->tags_addr;
738
739 boot_linux((void *)hdr->kernel_addr, (unsigned *)tags,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700740 (const char *)cmdline, board_machtype(),
741 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
742
743 return 0;
744}
745
Dima Zavin214cc642009-01-26 11:16:21 -0800746int boot_linux_from_flash(void)
747{
748 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -0800749 struct ptentry *ptn;
750 struct ptable *ptable;
751 unsigned offset = 0;
752 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800753
Shashank Mittalcd98d472011-08-02 14:29:24 -0700754 unsigned char *image_addr = 0;
755 unsigned kernel_actual;
756 unsigned ramdisk_actual;
757 unsigned imagesize_actual;
Amol Jadib6be5c12012-11-14 13:39:51 -0800758 unsigned second_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700759
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700760#if DEVICE_TREE
761 struct dt_table *table;
762 struct dt_entry *dt_entry_ptr;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800763 uint32_t dt_actual;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700764#endif
765
David Ng183a7422009-12-07 14:55:21 -0800766 if (target_is_emmc_boot()) {
767 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
768 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
769 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
770 return -1;
771 }
772 goto continue_boot;
773 }
774
Dima Zavin214cc642009-01-26 11:16:21 -0800775 ptable = flash_get_ptable();
776 if (ptable == NULL) {
777 dprintf(CRITICAL, "ERROR: Partition table not found\n");
778 return -1;
779 }
780
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800781 if(!boot_into_recovery)
782 {
783 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700784
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800785 if (ptn == NULL) {
786 dprintf(CRITICAL, "ERROR: No boot partition found\n");
787 return -1;
788 }
789 }
790 else
791 {
792 ptn = ptable_find(ptable, "recovery");
793 if (ptn == NULL) {
794 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
795 return -1;
796 }
Dima Zavin214cc642009-01-26 11:16:21 -0800797 }
798
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800799 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800800 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
801 return -1;
802 }
Dima Zavin214cc642009-01-26 11:16:21 -0800803
804 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700805 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800806 return -1;
807 }
808
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800809 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700810 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 -0800811 return -1;
812 }
813
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700814 /* Get virtual addresses since the hdr saves physical addresses. */
815 hdr->kernel_addr = VA(hdr->kernel_addr);
816 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
817 hdr->tags_addr = VA(hdr->tags_addr);
818
Shashank Mittalcd98d472011-08-02 14:29:24 -0700819 /* Authenticate Kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700820 if(target_use_signed_kernel() && (!device.is_unlocked) && (!device.is_tampered))
Shashank Mittalcd98d472011-08-02 14:29:24 -0700821 {
822 image_addr = (unsigned char *)target_get_scratch_address();
823 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
824 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800825#if DEVICE_TREE
826 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
827 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
828#else
Shashank Mittalcd98d472011-08-02 14:29:24 -0700829 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800830#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700831 offset = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700832
833 /* Assuming device rooted at this time */
Shashank Mittala0032282011-08-26 14:50:11 -0700834 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -0700835
Amol Jadib6be5c12012-11-14 13:39:51 -0800836 dprintf(INFO, "Loading boot image (%d): start\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700837 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800838
Shashank Mittalcd98d472011-08-02 14:29:24 -0700839 /* Read image without signature */
840 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
841 {
842 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
843 return -1;
844 }
Dima Zavin214cc642009-01-26 11:16:21 -0800845
Amol Jadib6be5c12012-11-14 13:39:51 -0800846 dprintf(INFO, "Loading boot image (%d): done\n", imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700847 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800848
Shashank Mittalcd98d472011-08-02 14:29:24 -0700849 offset = imagesize_actual;
850 /* Read signature */
851 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
852 {
853 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
854 }
855 else
856 {
Amol Jadib6be5c12012-11-14 13:39:51 -0800857 dprintf(INFO, "Authenticating boot image (%d): start\n", imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -0700858
859 /* Verify signature */
860 auth_kernel_img = image_verify((unsigned char *)image_addr,
861 (unsigned char *)(image_addr + imagesize_actual),
862 imagesize_actual,
863 CRYPTO_AUTH_ALG_SHA256);
Shashank Mittal162244e2011-08-08 19:01:25 -0700864
Amol Jadib6be5c12012-11-14 13:39:51 -0800865 dprintf(INFO, "Authenticating boot image (%d): done\n", imagesize_actual);
866
Shashank Mittal162244e2011-08-08 19:01:25 -0700867 if(auth_kernel_img)
868 {
869 /* Authorized kernel */
Shashank Mittala0032282011-08-26 14:50:11 -0700870 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700871 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700872 }
873
874 /* Move kernel and ramdisk to correct address */
875 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
876 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -0800877#if DEVICE_TREE
878 memmove((void*) hdr->tags_addr, (char *)(image_addr + page_size + kernel_actual + ramdisk_actual), hdr->dt_size);
879#endif
Shashank Mittal162244e2011-08-08 19:01:25 -0700880
881 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -0700882 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -0700883 {
884 write_device_info_flash(&device);
885 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +0530886#if USE_PCOM_SECBOOT
887 set_tamper_flag(device.is_tampered);
888#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700889 }
890 else
891 {
Shashank Mittal162244e2011-08-08 19:01:25 -0700892 offset = page_size;
893
Amol Jadib6be5c12012-11-14 13:39:51 -0800894 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
895 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
896 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
897
898 dprintf(INFO, "Loading boot image (%d): start\n",
899 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700900 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -0800901
902 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700903 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
904 return -1;
905 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800906 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -0700907
Amol Jadib6be5c12012-11-14 13:39:51 -0800908 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700909 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
910 return -1;
911 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800912 offset += ramdisk_actual;
913
914 dprintf(INFO, "Loading boot image (%d): done\n",
915 kernel_actual + ramdisk_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -0700916 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700917
918 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -0800919 offset += second_actual;
920 /* Second image loading not implemented. */
921 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700922 }
923
924#if DEVICE_TREE
925 if(hdr->dt_size != 0) {
926
927 /* Read the device tree table into buffer */
928 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
929 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
930 return -1;
931 }
932
933 table = (struct dt_table*) dt_buf;
934
935 /* Restriction that the device tree entry table should be less than a page*/
936 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
937
938 /* Validate the device tree table header */
939 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
940 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
941 return -1;
942 }
943
944 /* Calculate the offset of device tree within device tree table */
945 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
946 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
947 return -1;
948 }
949
950 /* Read device device tree in the "tags_add */
951 if(flash_read(ptn, offset + dt_entry_ptr->offset,
952 (void *)hdr->tags_addr, dt_entry_ptr->size)) {
953 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
954 return -1;
955 }
956 }
957#endif
958
Shashank Mittalcd98d472011-08-02 14:29:24 -0700959 }
David Ng183a7422009-12-07 14:55:21 -0800960continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800961
962 if(hdr->cmdline[0]) {
963 cmdline = (char*) hdr->cmdline;
964 } else {
965 cmdline = DEFAULT_CMDLINE;
966 }
967 dprintf(INFO, "cmdline = '%s'\n", cmdline);
968
969 /* TODO: create/pass atags to kernel */
970
Ajay Dudanie28a6072011-07-01 13:59:46 -0700971 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800972 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800973 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
974
975 return 0;
976}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800977
Shashank Mittal162244e2011-08-08 19:01:25 -0700978unsigned char info_buf[4096];
979void write_device_info_mmc(device_info *dev)
980{
981 struct device_info *info = (void*) info_buf;
982 unsigned long long ptn = 0;
983 unsigned long long size;
984 int index = INVALID_PTN;
985
986 index = partition_get_index("aboot");
987 ptn = partition_get_offset(index);
988 if(ptn == 0)
989 {
990 return;
991 }
992
993 size = partition_get_size(index);
994
995 memcpy(info, dev, sizeof(device_info));
996
997 if(mmc_write((ptn + size - 512), 512, (void *)info_buf))
998 {
999 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1000 return;
1001 }
1002}
1003
1004void read_device_info_mmc(device_info *dev)
1005{
1006 struct device_info *info = (void*) info_buf;
1007 unsigned long long ptn = 0;
1008 unsigned long long size;
1009 int index = INVALID_PTN;
1010
1011 index = partition_get_index("aboot");
1012 ptn = partition_get_offset(index);
1013 if(ptn == 0)
1014 {
1015 return;
1016 }
1017
1018 size = partition_get_size(index);
1019
1020 if(mmc_read((ptn + size - 512), (void *)info_buf, 512))
1021 {
1022 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
1023 return;
1024 }
1025
1026 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1027 {
1028 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1029 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001030 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001031
1032 write_device_info_mmc(info);
1033 }
1034 memcpy(dev, info, sizeof(device_info));
1035}
1036
1037void write_device_info_flash(device_info *dev)
1038{
1039 struct device_info *info = (void *) info_buf;
1040 struct ptentry *ptn;
1041 struct ptable *ptable;
1042
1043 ptable = flash_get_ptable();
1044 if (ptable == NULL)
1045 {
1046 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1047 return;
1048 }
1049
1050 ptn = ptable_find(ptable, "devinfo");
1051 if (ptn == NULL)
1052 {
1053 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1054 return;
1055 }
1056
1057 memcpy(info, dev, sizeof(device_info));
1058
1059 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1060 {
1061 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1062 return;
1063 }
1064}
1065
1066void read_device_info_flash(device_info *dev)
1067{
1068 struct device_info *info = (void*) info_buf;
1069 struct ptentry *ptn;
1070 struct ptable *ptable;
1071
1072 ptable = flash_get_ptable();
1073 if (ptable == NULL)
1074 {
1075 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1076 return;
1077 }
1078
1079 ptn = ptable_find(ptable, "devinfo");
1080 if (ptn == NULL)
1081 {
1082 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1083 return;
1084 }
1085
1086 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1087 {
1088 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1089 return;
1090 }
1091
1092 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1093 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001094 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1095 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001096 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001097 write_device_info_flash(info);
1098 }
1099 memcpy(dev, info, sizeof(device_info));
1100}
1101
1102void write_device_info(device_info *dev)
1103{
1104 if(target_is_emmc_boot())
1105 {
1106 write_device_info_mmc(dev);
1107 }
1108 else
1109 {
1110 write_device_info_flash(dev);
1111 }
1112}
1113
1114void read_device_info(device_info *dev)
1115{
1116 if(target_is_emmc_boot())
1117 {
1118 read_device_info_mmc(dev);
1119 }
1120 else
1121 {
1122 read_device_info_flash(dev);
1123 }
1124}
1125
1126void reset_device_info()
1127{
1128 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001129 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001130 write_device_info(&device);
1131}
1132
1133void set_device_root()
1134{
1135 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001136 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001137 write_device_info(&device);
1138}
1139
Amol Jadicb524072012-08-09 16:40:18 -07001140#if DEVICE_TREE
1141int copy_dtb(uint8_t *boot_image_start)
1142{
1143 uint32 dt_image_offset = 0;
1144 uint32_t n;
1145 struct dt_table *table;
1146 struct dt_entry *dt_entry_ptr;
Amol Jadicb524072012-08-09 16:40:18 -07001147
1148 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
1149
Amol Jadicb524072012-08-09 16:40:18 -07001150 if(hdr->dt_size != 0) {
1151
1152 /* add kernel offset */
1153 dt_image_offset += page_size;
1154 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1155 dt_image_offset += n;
1156
1157 /* add ramdisk offset */
1158 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1159 dt_image_offset += n;
1160
1161 /* add second offset */
1162 if(hdr->second_size != 0) {
1163 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
1164 dt_image_offset += n;
1165 }
1166
1167 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001168 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07001169
1170 /* Restriction that the device tree entry table should be less than a page*/
1171 ASSERT(((table->num_entries * sizeof(struct dt_entry))+ DEV_TREE_HEADER_SIZE) < hdr->page_size);
1172
1173 /* Validate the device tree table header */
1174 if((table->magic != DEV_TREE_MAGIC) && (table->version != DEV_TREE_VERSION)) {
1175 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1176 return -1;
1177 }
1178
1179 /* Calculate the offset of device tree within device tree table */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001180 if((dt_entry_ptr = dev_tree_get_entry_ptr(table)) == NULL){
Amol Jadicb524072012-08-09 16:40:18 -07001181 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1182 return -1;
1183 }
1184
1185 /* Read device device tree in the "tags_add */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001186 memmove((void*) hdr->tags_addr,
1187 boot_image_start + dt_image_offset + dt_entry_ptr->offset,
1188 dt_entry_ptr->size);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001189 } else {
1190 /*
1191 * Look for appended device tree if DTB is not found in boot image
1192 * If found load the kernel & boot up
1193 */
1194 memmove((void*) hdr->kernel_addr, boot_image_start + page_size, hdr->kernel_size);
1195 app_dev_tree = dev_tree_appended((void*) hdr->kernel_addr);
1196 if (!app_dev_tree) {
1197 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
1198 return -1;
1199 }
Amol Jadicb524072012-08-09 16:40:18 -07001200 }
1201
1202 /* Everything looks fine. Return success. */
1203 return 0;
1204}
1205#endif
1206
Brian Swetland9c4c0752009-01-25 16:23:50 -08001207void cmd_boot(const char *arg, void *data, unsigned sz)
1208{
1209 unsigned kernel_actual;
1210 unsigned ramdisk_actual;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001211 struct boot_img_hdr *hdr;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001212 char *ptr = ((char*) data);
Channagoud Kadabi35095622013-03-01 13:53:05 -08001213 void *tags;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001214
1215 if (sz < sizeof(hdr)) {
1216 fastboot_fail("invalid bootimage header");
1217 return;
1218 }
1219
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001220 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001221
1222 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001223 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001224
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001225 if(target_is_emmc_boot() && hdr->page_size) {
1226 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001227 page_mask = page_size - 1;
1228 }
1229
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001230 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1231 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1232
1233 /* Get virtual addresses since the hdr saves physical addresses. */
1234 hdr->kernel_addr = VA(hdr->kernel_addr);
1235 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
1236 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001237
Shashank Mittal1f0e2662011-09-01 15:06:00 -07001238 /* sz should have atleast raw boot image */
1239 if (page_size + kernel_actual + ramdisk_actual > sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -08001240 fastboot_fail("incomplete bootimage");
1241 return;
1242 }
1243
Amol Jadicb524072012-08-09 16:40:18 -07001244#if DEVICE_TREE
1245 /* find correct dtb and copy it to right location */
1246 if(copy_dtb(data))
1247 {
1248 fastboot_fail("dtb not found");
1249 return;
1250 }
1251#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08001252
1253 fastboot_okay("");
1254 udc_stop();
1255
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001256 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
1257 memmove((void*) hdr->kernel_addr, ptr + page_size, hdr->kernel_size);
Amol Jadicb524072012-08-09 16:40:18 -07001258
Channagoud Kadabi35095622013-03-01 13:53:05 -08001259 /*
1260 * If appended dev tree is found, update the atags with
1261 * memory address to the DTB appended location on RAM.
1262 * Else update with the atags address in the kernel header
1263 */
1264 if (app_dev_tree)
1265 tags = (void *)app_dev_tree;
1266 else
1267 tags = (void *)hdr->tags_addr;
1268
1269 boot_linux((void*) hdr->kernel_addr, (void*) tags,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001270 (const char*) hdr->cmdline, board_machtype(),
1271 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001272}
1273
Dima Zavin214cc642009-01-26 11:16:21 -08001274void cmd_erase(const char *arg, void *data, unsigned sz)
1275{
1276 struct ptentry *ptn;
1277 struct ptable *ptable;
1278
1279 ptable = flash_get_ptable();
1280 if (ptable == NULL) {
1281 fastboot_fail("partition table doesn't exist");
1282 return;
1283 }
1284
1285 ptn = ptable_find(ptable, arg);
1286 if (ptn == NULL) {
1287 fastboot_fail("unknown partition name");
1288 return;
1289 }
1290
1291 if (flash_erase(ptn)) {
1292 fastboot_fail("failed to erase partition");
1293 return;
1294 }
1295 fastboot_okay("");
1296}
1297
Bikas Gurungd48bd242010-09-04 19:54:32 -07001298
1299void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
1300{
1301 unsigned long long ptn = 0;
neetidb4b24d62012-01-20 12:13:09 -08001302 unsigned int out[512] = {0};
Kinson Chikf1a43512011-07-14 11:28:39 -07001303 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -07001304
Kinson Chikf1a43512011-07-14 11:28:39 -07001305 index = partition_get_index(arg);
1306 ptn = partition_get_offset(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07001307
Kinson Chikf1a43512011-07-14 11:28:39 -07001308 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07001309 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001310 return;
1311 }
neetidb4b24d62012-01-20 12:13:09 -08001312 /* Simple inefficient version of erase. Just writing
1313 0 in first block */
1314 if (mmc_write(ptn , 512, (unsigned int *)out)) {
1315 fastboot_fail("failed to erase partition");
Bikas Gurungd48bd242010-09-04 19:54:32 -07001316 return;
1317 }
1318 fastboot_okay("");
1319}
1320
1321
Ajay Dudani5c761132011-04-07 20:19:04 -07001322void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07001323{
1324 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001325 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001326 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001327
Greg Grisco6e754772011-06-23 12:19:39 -07001328 if (!strcmp(arg, "partition"))
1329 {
1330 dprintf(INFO, "Attempt to write partition image.\n");
Neeti Desai5f26aff2011-09-30 10:27:40 -07001331 if (write_partition(sz, (unsigned char *) data)) {
Greg Grisco6e754772011-06-23 12:19:39 -07001332 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001333 return;
1334 }
1335 }
Greg Grisco6e754772011-06-23 12:19:39 -07001336 else
1337 {
Kinson Chikf1a43512011-07-14 11:28:39 -07001338 index = partition_get_index(arg);
1339 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001340 if(ptn == 0) {
1341 fastboot_fail("partition table doesn't exist");
1342 return;
1343 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001344
Greg Grisco6e754772011-06-23 12:19:39 -07001345 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
1346 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1347 fastboot_fail("image is not a boot image");
1348 return;
1349 }
1350 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07001351
Kinson Chikf1a43512011-07-14 11:28:39 -07001352 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -07001353 if (ROUND_TO_PAGE(sz,511) > size) {
1354 fastboot_fail("size too large");
1355 return;
1356 }
1357 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
1358 fastboot_fail("flash write failure");
1359 return;
1360 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001361 }
1362 fastboot_okay("");
1363 return;
1364}
1365
Ajay Dudani5c761132011-04-07 20:19:04 -07001366void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
1367{
1368 unsigned int chunk;
1369 unsigned int chunk_data_sz;
1370 sparse_header_t *sparse_header;
1371 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07001372 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07001373 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301374 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001375 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -07001376
Kinson Chikf1a43512011-07-14 11:28:39 -07001377 index = partition_get_index(arg);
1378 ptn = partition_get_offset(index);
1379 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07001380 fastboot_fail("partition table doesn't exist");
1381 return;
1382 }
1383
Channagoud Kadabi65b91002011-10-11 17:34:33 +05301384 size = partition_get_size(index);
1385 if (ROUND_TO_PAGE(sz,511) > size) {
1386 fastboot_fail("size too large");
1387 return;
1388 }
1389
Ajay Dudani5c761132011-04-07 20:19:04 -07001390 /* Read and skip over sparse image header */
1391 sparse_header = (sparse_header_t *) data;
1392 data += sparse_header->file_hdr_sz;
1393 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
1394 {
1395 /* Skip the remaining bytes in a header that is longer than
1396 * we expected.
1397 */
1398 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
1399 }
1400
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001401 dprintf (SPEW, "=== Sparse Image Header ===\n");
1402 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
1403 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
1404 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
1405 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
1406 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
1407 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
1408 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
1409 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07001410
1411 /* Start processing chunks */
1412 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
1413 {
1414 /* Read and skip over chunk header */
1415 chunk_header = (chunk_header_t *) data;
1416 data += sizeof(chunk_header_t);
1417
1418 dprintf (SPEW, "=== Chunk Header ===\n");
1419 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
1420 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
1421 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
1422
1423 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
1424 {
1425 /* Skip the remaining bytes in a header that is longer than
1426 * we expected.
1427 */
1428 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
1429 }
1430
1431 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
1432 switch (chunk_header->chunk_type)
1433 {
1434 case CHUNK_TYPE_RAW:
1435 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
1436 chunk_data_sz))
1437 {
1438 fastboot_fail("Bogus chunk size for chunk type Raw");
1439 return;
1440 }
1441
Ajay Dudaniab18f022011-05-12 14:39:22 -07001442 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
1443 chunk_data_sz,
1444 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07001445 {
1446 fastboot_fail("flash write failure");
1447 return;
1448 }
1449 total_blocks += chunk_header->chunk_sz;
1450 data += chunk_data_sz;
1451 break;
1452
1453 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001454 total_blocks += chunk_header->chunk_sz;
1455 break;
1456
Ajay Dudani5c761132011-04-07 20:19:04 -07001457 case CHUNK_TYPE_CRC:
1458 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
1459 {
1460 fastboot_fail("Bogus chunk size for chunk type Dont Care");
1461 return;
1462 }
1463 total_blocks += chunk_header->chunk_sz;
1464 data += chunk_data_sz;
1465 break;
1466
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07001467 default:
Ajay Dudani5c761132011-04-07 20:19:04 -07001468 fastboot_fail("Unknown chunk type");
1469 return;
1470 }
1471 }
1472
Ajay Dudani0c6927b2011-05-18 11:12:16 -07001473 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
1474 total_blocks, sparse_header->total_blks);
1475
1476 if(total_blocks != sparse_header->total_blks)
1477 {
1478 fastboot_fail("sparse image write failure");
1479 }
Ajay Dudani5c761132011-04-07 20:19:04 -07001480
1481 fastboot_okay("");
1482 return;
1483}
1484
1485void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
1486{
1487 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001488 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
1489 unsigned int *magic_number = (unsigned int *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001490
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001491#ifdef SSD_ENABLE
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001492 int ret=0;
1493 uint32 major_version=0;
1494 uint32 minor_version=0;
1495
1496 ret = scm_svc_version(&major_version,&minor_version);
1497 if(!ret)
1498 {
1499 if(major_version >= 2)
1500 {
1501 if( !strcmp(arg,"ssd") || !strcmp(arg,"tqs") )
1502 {
1503 ret = encrypt_scm((uint32 **) &data, &sz);
1504 if (ret != 0) {
1505 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1506 return;
1507 }
1508
1509 ret = scm_protect_keystore((uint32 *) data, sz);
1510 if (ret != 0) {
1511 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
1512 return;
1513 }
1514 }
1515 else
1516 {
1517 ret = decrypt_scm_v2((uint32 **) &data, &sz);
1518 if(ret != 0)
1519 {
1520 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
1521 return;
1522 }
1523 }
1524 }
1525 else
1526 {
1527 if (magic_number[0] == DECRYPT_MAGIC_0 &&
1528 magic_number[1] == DECRYPT_MAGIC_1)
1529 {
1530 ret = decrypt_scm((uint32 **) &data, &sz);
1531 if (ret != 0) {
1532 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
1533 return;
1534 }
1535 }
1536 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
1537 magic_number[1] == ENCRYPT_MAGIC_1)
1538 {
1539 ret = encrypt_scm((uint32 **) &data, &sz);
1540 if (ret != 0) {
1541 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
1542 return;
1543 }
1544 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001545 }
1546 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001547 else
Neeti Desai127b9e02012-03-20 16:11:23 -07001548 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001549 dprintf(CRITICAL,"INVALID SVC Version\n");
1550 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07001551 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08001552#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07001553
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07001554 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -07001555 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
1556 cmd_flash_mmc_img(arg, data, sz);
1557 else
1558 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -07001559 return;
1560}
1561
Dima Zavin214cc642009-01-26 11:16:21 -08001562void cmd_flash(const char *arg, void *data, unsigned sz)
1563{
1564 struct ptentry *ptn;
1565 struct ptable *ptable;
1566 unsigned extra = 0;
1567
1568 ptable = flash_get_ptable();
1569 if (ptable == NULL) {
1570 fastboot_fail("partition table doesn't exist");
1571 return;
1572 }
1573
1574 ptn = ptable_find(ptable, arg);
1575 if (ptn == NULL) {
1576 fastboot_fail("unknown partition name");
1577 return;
1578 }
1579
1580 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
1581 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1582 fastboot_fail("image is not a boot image");
1583 return;
1584 }
1585 }
1586
Amol Jadi5c61a952012-05-04 17:05:35 -07001587 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07001588 || !strcmp(ptn->name, "userdata")
1589 || !strcmp(ptn->name, "persist")
1590 || !strcmp(ptn->name, "recoveryfs")) {
Channagoud Kadabi404a7062011-03-21 19:27:50 +05301591 if (flash_ecc_bch_enabled())
1592 /* Spare data bytes for 8 bit ECC increased by 4 */
1593 extra = ((page_size >> 9) * 20);
1594 else
1595 extra = ((page_size >> 9) * 16);
1596 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001597 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08001598
1599 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
1600 if (flash_write(ptn, extra, data, sz)) {
1601 fastboot_fail("flash write failure");
1602 return;
1603 }
1604 dprintf(INFO, "partition '%s' updated\n", ptn->name);
1605 fastboot_okay("");
1606}
1607
1608void cmd_continue(const char *arg, void *data, unsigned sz)
1609{
1610 fastboot_okay("");
1611 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001612 if (target_is_emmc_boot())
1613 {
1614 boot_linux_from_mmc();
1615 }
1616 else
1617 {
1618 boot_linux_from_flash();
1619 }
Dima Zavin214cc642009-01-26 11:16:21 -08001620}
1621
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001622void cmd_reboot(const char *arg, void *data, unsigned sz)
1623{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001624 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001625 fastboot_okay("");
1626 reboot_device(0);
1627}
1628
1629void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
1630{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001631 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001632 fastboot_okay("");
1633 reboot_device(FASTBOOT_MODE);
1634}
1635
Shashank Mittal162244e2011-08-08 19:01:25 -07001636void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
1637{
1638 if(!device.is_unlocked)
1639 {
1640 device.is_unlocked = 1;
1641 write_device_info(&device);
1642 }
1643 fastboot_okay("");
1644}
1645
Shashank Mittala0032282011-08-26 14:50:11 -07001646void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
1647{
1648 char response[64];
1649 snprintf(response, 64, "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
1650 fastboot_info(response);
1651 snprintf(response, 64, "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
1652 fastboot_info(response);
1653 fastboot_okay("");
1654}
1655
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001656void splash_screen ()
1657{
1658 struct ptentry *ptn;
1659 struct ptable *ptable;
1660 struct fbcon_config *fb_display = NULL;
1661
1662 if (!target_is_emmc_boot())
1663 {
1664 ptable = flash_get_ptable();
1665 if (ptable == NULL) {
1666 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -07001667 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001668 }
1669
1670 ptn = ptable_find(ptable, "splash");
1671 if (ptn == NULL) {
1672 dprintf(CRITICAL, "ERROR: No splash partition found\n");
1673 } else {
1674 fb_display = fbcon_display();
1675 if (fb_display) {
1676 if (flash_read(ptn, 0, fb_display->base,
1677 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
1678 fbcon_clear();
1679 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
1680 }
1681 }
1682 }
1683 }
1684}
1685
Brian Swetland9c4c0752009-01-25 16:23:50 -08001686void aboot_init(const struct app_descriptor *app)
1687{
Shashank Mittal4f99a882010-02-01 13:58:50 -08001688 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001689 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001690 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07001691
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001692 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001693 if (target_is_emmc_boot())
1694 {
1695 page_size = 2048;
1696 page_mask = page_size - 1;
1697 }
1698 else
1699 {
1700 page_size = flash_page_size();
1701 page_mask = page_size - 1;
1702 }
1703
Shashank Mittal162244e2011-08-08 19:01:25 -07001704 if(target_use_signed_kernel())
1705 {
1706 read_device_info(&device);
1707
Shashank Mittal162244e2011-08-08 19:01:25 -07001708 }
1709
Greg Griscod6250552011-06-29 14:40:23 -07001710 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07001711 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001712 surf_udc_device.serialno = sn_buf;
1713
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001714 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001715 if (keys_get_state(KEY_HOME) != 0)
1716 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -05001717 if (keys_get_state(KEY_VOLUMEUP) != 0)
1718 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001719 if(!boot_into_recovery)
1720 {
1721 if (keys_get_state(KEY_BACK) != 0)
1722 goto fastboot;
1723 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
1724 goto fastboot;
1725 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001726
1727 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07001728 if (fastboot_trigger())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001729 goto fastboot;
1730 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07001731
Ajay Dudani77421292010-10-27 19:34:06 -07001732 reboot_mode = check_reboot_mode();
1733 if (reboot_mode == RECOVERY_MODE) {
1734 boot_into_recovery = 1;
1735 } else if(reboot_mode == FASTBOOT_MODE) {
1736 goto fastboot;
1737 }
1738
Shashank Mittal23b8f422010-04-16 19:27:21 -07001739 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001740 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07001741 if(emmc_recovery_init())
1742 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittala0032282011-08-26 14:50:11 -07001743 if(target_use_signed_kernel())
1744 {
1745 if((device.is_unlocked) || (device.is_tampered))
1746 {
1747 #ifdef TZ_TAMPER_FUSE
1748 set_tamper_fuse_cmd();
1749 #endif
Channagoud Kadabibf695c62012-04-10 13:31:56 +05301750 #if USE_PCOM_SECBOOT
1751 set_tamper_flag(device.is_tampered);
1752 #endif
Shashank Mittala0032282011-08-26 14:50:11 -07001753 }
1754 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001755 boot_linux_from_mmc();
1756 }
1757 else
1758 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001759 recovery_init();
Channagoud Kadabie7b66702012-03-22 15:54:30 +05301760#if USE_PCOM_SECBOOT
1761 if((device.is_unlocked) || (device.is_tampered))
1762 set_tamper_flag(device.is_tampered);
1763#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001764 boot_linux_from_flash();
1765 }
Dima Zavinb4283602009-01-26 16:36:57 -08001766 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1767 "to fastboot mode.\n");
1768
1769fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001770
Shashank Mittal162244e2011-08-08 19:01:25 -07001771 target_fastboot_init();
Amol Jadi57abe4c2011-05-24 15:47:27 -07001772
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001773 if(!usb_init)
1774 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001775
1776 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001777
Shashank Mittal23b8f422010-04-16 19:27:21 -07001778 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001779 {
1780 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001781 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001782 }
1783 else
1784 {
1785 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001786 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001787 }
1788
1789 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001790 fastboot_register("reboot", cmd_reboot);
1791 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Shashank Mittal162244e2011-08-08 19:01:25 -07001792 fastboot_register("oem unlock", cmd_oem_unlock);
Shashank Mittala0032282011-08-26 14:50:11 -07001793 fastboot_register("oem device-info", cmd_oem_devinfo);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001794 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001795 fastboot_publish("kernel", "lk");
Trevor Bourget59b25d52012-01-13 18:43:36 -08001796 fastboot_publish("serialno", sn_buf);
Kinson Chikf1a43512011-07-14 11:28:39 -07001797 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001798 sz = target_get_max_flash_size();
1799 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001800 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001801}
1802
1803APP_START(aboot)
1804 .init = aboot_init,
1805APP_END