blob: 312b2782a6de74d65cf0bae6326b2465fd0ac78c [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Ajay Dudanid04110c2011-01-17 23:55:07 -08005 * Copyright (c) 2009-2011, Code Aurora Forum. 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.
14 * * Neither the name of Code Aurora nor
15 * 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>
Dima Zavin214cc642009-01-26 11:16:21 -080051
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080053#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080054#include "bootimg.h"
55#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070056#include "sparse_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070057#include "mmc.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080058
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070059#include "scm_decrypt.h"
60
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070061#define EXPAND(NAME) #NAME
62#define TARGET(NAME) EXPAND(NAME)
Chandan Uddarajuda919832009-11-17 01:06:11 -080063#define DEFAULT_CMDLINE "mem=100M console=null";
Brian Swetland2defe162009-08-18 14:35:59 -070064
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080065#ifdef MEMBASE
66#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
67#else
David Ng183a7422009-12-07 14:55:21 -080068#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080069#endif
70
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080071#define RECOVERY_MODE 0x77665502
72#define FASTBOOT_MODE 0x77665500
73
David Ng183a7422009-12-07 14:55:21 -080074static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080075static const char *usb_sn_cmdline = " androidboot.serialno=";
David Ngf773dde2010-07-26 19:55:08 -070076static const char *battchg_pause = " androidboot.battchg_pause=true";
Shashank Mittalcd98d472011-08-02 14:29:24 -070077static const char *auth_kernel = " androidboot.authorized_kernel=true";
David Ng183a7422009-12-07 14:55:21 -080078
Ajay Dudani6cff85e2011-02-04 16:02:16 -080079static const char *baseband_apq = " androidboot.baseband=apq";
80static const char *baseband_msm = " androidboot.baseband=msm";
81static const char *baseband_csfb = " androidboot.baseband=csfb";
82static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudanid04110c2011-01-17 23:55:07 -080083
Shashank Mittalcd98d472011-08-02 14:29:24 -070084/* Assuming unauthorized kernel image by default */
85static int auth_kernel_img = 0;
86
Brian Swetland9c4c0752009-01-25 16:23:50 -080087static struct udc_device surf_udc_device = {
88 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080089 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080090 .version_id = 0x0100,
91 .manufacturer = "Google",
92 .product = "Android",
93};
94
Dima Zavin42168f22009-01-30 11:52:22 -080095struct atag_ptbl_entry
96{
97 char name[16];
98 unsigned offset;
99 unsigned size;
100 unsigned flags;
101};
102
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800103char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -0700104
Greg Griscod2471ef2011-07-14 13:00:42 -0700105extern int emmc_recovery_init(void);
106
107
108
Dima Zavin42168f22009-01-30 11:52:22 -0800109static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
110{
111 struct atag_ptbl_entry atag_ptn;
112
Greg Griscod6250552011-06-29 14:40:23 -0700113 if (ptn->type == TYPE_MODEM_PARTITION) {
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800114 return;
Greg Griscod6250552011-06-29 14:40:23 -0700115 }
Dima Zavin42168f22009-01-30 11:52:22 -0800116 memcpy(atag_ptn.name, ptn->name, 16);
117 atag_ptn.name[15] = '\0';
118 atag_ptn.offset = ptn->start;
119 atag_ptn.size = ptn->length;
120 atag_ptn.flags = ptn->flags;
121 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
122 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
123}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800124
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700125void boot_linux(void *kernel, unsigned *tags,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800126 const char *cmdline, unsigned machtype,
127 void *ramdisk, unsigned ramdisk_size)
128{
129 unsigned *ptr = tags;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800130 unsigned pcount = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800131 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800132 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800133 int cmdline_len = 0;
134 int have_cmdline = 0;
David Ngf773dde2010-07-26 19:55:08 -0700135 int pause_at_bootup = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800136
137 /* CORE */
138 *ptr++ = 2;
139 *ptr++ = 0x54410001;
140
141 if (ramdisk_size) {
142 *ptr++ = 4;
143 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800144 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800145 *ptr++ = ramdisk_size;
146 }
147
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800148 ptr = target_atag_mem(ptr);
149
David Ng183a7422009-12-07 14:55:21 -0800150 if (!target_is_emmc_boot()) {
151 /* Skip NAND partition ATAGS for eMMC boot */
152 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
153 int i;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800154 for(i=0; i < ptable->count; i++) {
155 struct ptentry *ptn;
156 ptn = ptable_get(ptable, i);
157 if (ptn->type == TYPE_APPS_PARTITION)
158 pcount++;
159 }
160 *ptr++ = 2 + (pcount * (sizeof(struct atag_ptbl_entry) /
David Ng183a7422009-12-07 14:55:21 -0800161 sizeof(unsigned)));
162 *ptr++ = 0x4d534d70;
163 for (i = 0; i < ptable->count; ++i)
164 ptentry_to_tag(&ptr, ptable_get(ptable, i));
165 }
Dima Zavin42168f22009-01-30 11:52:22 -0800166 }
167
Brian Swetland9c4c0752009-01-25 16:23:50 -0800168 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800169 cmdline_len = strlen(cmdline);
170 have_cmdline = 1;
171 }
172 if (target_is_emmc_boot()) {
173 cmdline_len += strlen(emmc_cmdline);
174 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800175
176 cmdline_len += strlen(usb_sn_cmdline);
177 cmdline_len += strlen(sn_buf);
178
David Ngf773dde2010-07-26 19:55:08 -0700179 if (target_pause_for_battery_charge()) {
180 pause_at_bootup = 1;
181 cmdline_len += strlen(battchg_pause);
182 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800183
Shashank Mittalcd98d472011-08-02 14:29:24 -0700184 if(target_use_signed_kernel() && auth_kernel_img) {
185 cmdline_len += strlen(auth_kernel);
186 }
187
Ajay Dudanid04110c2011-01-17 23:55:07 -0800188 /* Determine correct androidboot.baseband to use */
189 switch(target_baseband())
190 {
191 case BASEBAND_APQ:
192 cmdline_len += strlen(baseband_apq);
193 break;
194
195 case BASEBAND_MSM:
196 cmdline_len += strlen(baseband_msm);
197 break;
198
199 case BASEBAND_CSFB:
200 cmdline_len += strlen(baseband_csfb);
201 break;
202
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800203 case BASEBAND_SVLTE2A:
204 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800205 break;
206 }
207
David Ng183a7422009-12-07 14:55:21 -0800208 if (cmdline_len > 0) {
209 const char *src;
210 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800211 unsigned n;
212 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800213 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800214 *ptr++ = (n / 4) + 2;
215 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800216 dst = (char *)ptr;
217 if (have_cmdline) {
218 src = cmdline;
219 while ((*dst++ = *src++));
220 }
221 if (target_is_emmc_boot()) {
222 src = emmc_cmdline;
223 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700224 have_cmdline = 1;
225 while ((*dst++ = *src++));
226 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800227
228 src = usb_sn_cmdline;
229 if (have_cmdline) --dst;
230 have_cmdline = 1;
231 while ((*dst++ = *src++));
232 src = sn_buf;
233 if (have_cmdline) --dst;
234 have_cmdline = 1;
235 while ((*dst++ = *src++));
236
David Ngf773dde2010-07-26 19:55:08 -0700237 if (pause_at_bootup) {
238 src = battchg_pause;
239 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800240 while ((*dst++ = *src++));
241 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800242
Shashank Mittalcd98d472011-08-02 14:29:24 -0700243 if(target_use_signed_kernel() && auth_kernel_img) {
244 src = auth_kernel;
245 if (have_cmdline) --dst;
246 while ((*dst++ = *src++));
247 }
248
Ajay Dudanid04110c2011-01-17 23:55:07 -0800249 switch(target_baseband())
250 {
251 case BASEBAND_APQ:
252 src = baseband_apq;
253 if (have_cmdline) --dst;
254 while ((*dst++ = *src++));
255 break;
256
257 case BASEBAND_MSM:
258 src = baseband_msm;
259 if (have_cmdline) --dst;
260 while ((*dst++ = *src++));
261 break;
262
263 case BASEBAND_CSFB:
264 src = baseband_csfb;
265 if (have_cmdline) --dst;
266 while ((*dst++ = *src++));
267 break;
268
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800269 case BASEBAND_SVLTE2A:
270 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800271 if (have_cmdline) --dst;
272 while ((*dst++ = *src++));
273 break;
274 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800275 ptr += (n / 4);
276 }
277
278 /* END */
279 *ptr++ = 0;
280 *ptr++ = 0;
281
282 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
283 kernel, ramdisk, ramdisk_size);
284 if (cmdline)
285 dprintf(INFO, "cmdline: %s\n", cmdline);
286
287 enter_critical_section();
Amol Jadi4421e652011-06-16 15:00:48 -0700288 /* do any platform specific cleanup before kernel entry */
289 platform_uninit();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800290 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800291 arch_disable_mmu();
Brian Swetland9c4c0752009-01-25 16:23:50 -0800292 entry(0, machtype, tags);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800293}
294
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800295unsigned page_size = 0;
296unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800297
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800298#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800299
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800300static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800301
Shashank Mittal23b8f422010-04-16 19:27:21 -0700302int boot_linux_from_mmc(void)
303{
304 struct boot_img_hdr *hdr = (void*) buf;
305 struct boot_img_hdr *uhdr;
306 unsigned offset = 0;
307 unsigned long long ptn = 0;
308 unsigned n = 0;
309 const char *cmdline;
Kinson Chikf1a43512011-07-14 11:28:39 -0700310 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700311
Shashank Mittalcd98d472011-08-02 14:29:24 -0700312 unsigned char *image_addr = 0;
313 unsigned kernel_actual;
314 unsigned ramdisk_actual;
315 unsigned imagesize_actual;
316
Shashank Mittal23b8f422010-04-16 19:27:21 -0700317 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
318 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
319 dprintf(INFO, "Unified boot method!\n");
320 hdr = uhdr;
321 goto unified_boot;
322 }
Greg Griscod6250552011-06-29 14:40:23 -0700323 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -0700324 index = partition_get_index("boot");
325 ptn = partition_get_offset(index);
326 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700327 dprintf(CRITICAL, "ERROR: No boot partition found\n");
328 return -1;
329 }
Kinson Chikf1a43512011-07-14 11:28:39 -0700330 }
331 else {
332 index = partition_get_index("recovery");
333 ptn = partition_get_offset(index);
334 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700335 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
336 return -1;
337 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700338 }
339
Greg Griscod6250552011-06-29 14:40:23 -0700340 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700341 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
342 return -1;
343 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700344
345 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700346 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700347 return -1;
348 }
349
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700350 if (hdr->page_size && (hdr->page_size != page_size)) {
351 page_size = hdr->page_size;
352 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700353 }
354
Shashank Mittalcd98d472011-08-02 14:29:24 -0700355 /* Authenticate Kernel */
356 if(target_use_signed_kernel())
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800357 {
Shashank Mittalcd98d472011-08-02 14:29:24 -0700358 image_addr = (unsigned char *)target_get_scratch_address();
359 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
360 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
361 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
362
363 offset = 0;
364 /* Read image without signature */
365 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
366 {
367 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
368 return -1;
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800369 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700370
371 offset = imagesize_actual;
372 /* Read signature */
373 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
374 {
375 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
376 }
377 else
378 {
379 auth_kernel_img = image_verify((unsigned char *)image_addr,
380 (unsigned char *)(image_addr + imagesize_actual),
381 imagesize_actual,
382 CRYPTO_AUTH_ALG_SHA256);
383 }
384
385 /* Move kernel and ramdisk to correct address */
386 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
387 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittal23b8f422010-04-16 19:27:21 -0700388 }
Shashank Mittalcd98d472011-08-02 14:29:24 -0700389 else
390 {
391 offset += page_size;
392
393 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
394 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
395 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
396 return -1;
397 }
398 offset += n;
399
400 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
401 if(n != 0)
402 {
403 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
404 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
405 return -1;
406 }
407 }
408 offset += n;
409 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700410
411unified_boot:
412 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
413 hdr->kernel_size);
414 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
415 hdr->ramdisk_size);
416
417 if(hdr->cmdline[0]) {
418 cmdline = (char*) hdr->cmdline;
419 } else {
420 cmdline = DEFAULT_CMDLINE;
421 }
422 dprintf(INFO, "cmdline = '%s'\n", cmdline);
423
424 dprintf(INFO, "\nBooting Linux\n");
Greg Griscod2471ef2011-07-14 13:00:42 -0700425 boot_linux((void *)hdr->kernel_addr, (unsigned *) hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700426 (const char *)cmdline, board_machtype(),
427 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
428
429 return 0;
430}
431
Dima Zavin214cc642009-01-26 11:16:21 -0800432int boot_linux_from_flash(void)
433{
434 struct boot_img_hdr *hdr = (void*) buf;
435 unsigned n;
436 struct ptentry *ptn;
437 struct ptable *ptable;
438 unsigned offset = 0;
439 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800440
Shashank Mittalcd98d472011-08-02 14:29:24 -0700441 unsigned char *image_addr = 0;
442 unsigned kernel_actual;
443 unsigned ramdisk_actual;
444 unsigned imagesize_actual;
445
David Ng183a7422009-12-07 14:55:21 -0800446 if (target_is_emmc_boot()) {
447 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
448 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
449 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
450 return -1;
451 }
452 goto continue_boot;
453 }
454
Dima Zavin214cc642009-01-26 11:16:21 -0800455 ptable = flash_get_ptable();
456 if (ptable == NULL) {
457 dprintf(CRITICAL, "ERROR: Partition table not found\n");
458 return -1;
459 }
460
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800461 if(!boot_into_recovery)
462 {
463 ptn = ptable_find(ptable, "boot");
464 if (ptn == NULL) {
465 dprintf(CRITICAL, "ERROR: No boot partition found\n");
466 return -1;
467 }
468 }
469 else
470 {
471 ptn = ptable_find(ptable, "recovery");
472 if (ptn == NULL) {
473 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
474 return -1;
475 }
Dima Zavin214cc642009-01-26 11:16:21 -0800476 }
477
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800478 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800479 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
480 return -1;
481 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800482 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800483
484 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700485 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800486 return -1;
487 }
488
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800489 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700490 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 -0800491 return -1;
492 }
493
Shashank Mittalcd98d472011-08-02 14:29:24 -0700494 /* Authenticate Kernel */
495 if(target_use_signed_kernel())
496 {
497 image_addr = (unsigned char *)target_get_scratch_address();
498 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
499 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
500 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Dima Zavin214cc642009-01-26 11:16:21 -0800501
Shashank Mittalcd98d472011-08-02 14:29:24 -0700502 offset = 0;
503 /* Read image without signature */
504 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
505 {
506 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
507 return -1;
508 }
Dima Zavin214cc642009-01-26 11:16:21 -0800509
Shashank Mittalcd98d472011-08-02 14:29:24 -0700510 offset = imagesize_actual;
511 /* Read signature */
512 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
513 {
514 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
515 }
516 else
517 {
518
519 /* Verify signature */
520 auth_kernel_img = image_verify((unsigned char *)image_addr,
521 (unsigned char *)(image_addr + imagesize_actual),
522 imagesize_actual,
523 CRYPTO_AUTH_ALG_SHA256);
524 }
525
526 /* Move kernel and ramdisk to correct address */
527 memmove((void*) hdr->kernel_addr, (char *)(image_addr + page_size), hdr->kernel_size);
528 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
529 }
530 else
531 {
532 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
533 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
534 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
535 return -1;
536 }
537 offset += n;
538
539 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
540 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
541 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
542 return -1;
543 }
544 offset += n;
545 }
David Ng183a7422009-12-07 14:55:21 -0800546continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800547 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
548 hdr->kernel_size);
549 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
550 hdr->ramdisk_size);
551
552 if(hdr->cmdline[0]) {
553 cmdline = (char*) hdr->cmdline;
554 } else {
555 cmdline = DEFAULT_CMDLINE;
556 }
557 dprintf(INFO, "cmdline = '%s'\n", cmdline);
558
559 /* TODO: create/pass atags to kernel */
560
561 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700562 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800563 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800564 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
565
566 return 0;
567}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800568
569void cmd_boot(const char *arg, void *data, unsigned sz)
570{
571 unsigned kernel_actual;
572 unsigned ramdisk_actual;
573 static struct boot_img_hdr hdr;
574 char *ptr = ((char*) data);
575
576 if (sz < sizeof(hdr)) {
577 fastboot_fail("invalid bootimage header");
578 return;
579 }
580
581 memcpy(&hdr, data, sizeof(hdr));
582
583 /* ensure commandline is terminated */
584 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
585
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700586 if(target_is_emmc_boot() && hdr.page_size) {
587 page_size = hdr.page_size;
588 page_mask = page_size - 1;
589 }
590
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800591 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
592 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800593
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700594 if (page_size + kernel_actual + ramdisk_actual < sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800595 fastboot_fail("incomplete bootimage");
596 return;
597 }
598
Ajay Dudanie28a6072011-07-01 13:59:46 -0700599 memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size);
600 memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800601
602 fastboot_okay("");
603 udc_stop();
604
Amol Jadie67872e2011-06-27 14:14:11 -0700605 boot_linux((void*) hdr.kernel_addr, (void*) hdr.tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800606 (const char*) hdr.cmdline, board_machtype(),
Ajay Dudanie28a6072011-07-01 13:59:46 -0700607 (void*) hdr.ramdisk_addr, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800608}
609
Dima Zavin214cc642009-01-26 11:16:21 -0800610void cmd_erase(const char *arg, void *data, unsigned sz)
611{
612 struct ptentry *ptn;
613 struct ptable *ptable;
614
615 ptable = flash_get_ptable();
616 if (ptable == NULL) {
617 fastboot_fail("partition table doesn't exist");
618 return;
619 }
620
621 ptn = ptable_find(ptable, arg);
622 if (ptn == NULL) {
623 fastboot_fail("unknown partition name");
624 return;
625 }
626
627 if (flash_erase(ptn)) {
628 fastboot_fail("failed to erase partition");
629 return;
630 }
631 fastboot_okay("");
632}
633
Bikas Gurungd48bd242010-09-04 19:54:32 -0700634
635void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
636{
637 unsigned long long ptn = 0;
638 unsigned int out[512] = {0};
Kinson Chikf1a43512011-07-14 11:28:39 -0700639 int index = INVALID_PTN;
Bikas Gurungd48bd242010-09-04 19:54:32 -0700640
Kinson Chikf1a43512011-07-14 11:28:39 -0700641 index = partition_get_index(arg);
642 ptn = partition_get_offset(index);
643 if(ptn == 0) {
Bikas Gurungd48bd242010-09-04 19:54:32 -0700644 fastboot_fail("partition table doesn't exist");
645 return;
646 }
647
Bikas Gurungd48bd242010-09-04 19:54:32 -0700648 /* Simple inefficient version of erase. Just writing
649 0 in first block */
650 if (mmc_write(ptn , 512, (unsigned int *)out)) {
651 fastboot_fail("failed to erase partition");
652 return;
653 }
654 fastboot_okay("");
655}
656
657
Ajay Dudani5c761132011-04-07 20:19:04 -0700658void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -0700659{
660 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700661 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700662 int index = INVALID_PTN;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700663
Greg Grisco6e754772011-06-23 12:19:39 -0700664 if (!strcmp(arg, "partition"))
665 {
666 dprintf(INFO, "Attempt to write partition image.\n");
667 if (mmc_write_partition(sz, (unsigned char *) data)) {
668 fastboot_fail("failed to write partition");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700669 return;
670 }
671 }
Greg Grisco6e754772011-06-23 12:19:39 -0700672 else
673 {
Kinson Chikf1a43512011-07-14 11:28:39 -0700674 index = partition_get_index(arg);
675 ptn = partition_get_offset(index);
Greg Grisco6e754772011-06-23 12:19:39 -0700676 if(ptn == 0) {
677 fastboot_fail("partition table doesn't exist");
678 return;
679 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700680
Greg Grisco6e754772011-06-23 12:19:39 -0700681 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
682 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
683 fastboot_fail("image is not a boot image");
684 return;
685 }
686 }
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700687
Kinson Chikf1a43512011-07-14 11:28:39 -0700688 size = partition_get_size(index);
Greg Grisco6e754772011-06-23 12:19:39 -0700689 if (ROUND_TO_PAGE(sz,511) > size) {
690 fastboot_fail("size too large");
691 return;
692 }
693 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
694 fastboot_fail("flash write failure");
695 return;
696 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700697 }
698 fastboot_okay("");
699 return;
700}
701
Greg Griscod6250552011-06-29 14:40:23 -0700702
703
Ajay Dudani5c761132011-04-07 20:19:04 -0700704void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
705{
706 unsigned int chunk;
707 unsigned int chunk_data_sz;
708 sparse_header_t *sparse_header;
709 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -0700710 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700711 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700712 int index = INVALID_PTN;
Ajay Dudani5c761132011-04-07 20:19:04 -0700713
Kinson Chikf1a43512011-07-14 11:28:39 -0700714 index = partition_get_index(arg);
715 ptn = partition_get_offset(index);
716 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -0700717 fastboot_fail("partition table doesn't exist");
718 return;
719 }
720
721 /* Read and skip over sparse image header */
722 sparse_header = (sparse_header_t *) data;
723 data += sparse_header->file_hdr_sz;
724 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
725 {
726 /* Skip the remaining bytes in a header that is longer than
727 * we expected.
728 */
729 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
730 }
731
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700732 dprintf (SPEW, "=== Sparse Image Header ===\n");
733 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
734 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
735 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
736 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
737 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
738 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
739 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
740 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -0700741
742 /* Start processing chunks */
743 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
744 {
745 /* Read and skip over chunk header */
746 chunk_header = (chunk_header_t *) data;
747 data += sizeof(chunk_header_t);
748
749 dprintf (SPEW, "=== Chunk Header ===\n");
750 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
751 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
752 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
753
754 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
755 {
756 /* Skip the remaining bytes in a header that is longer than
757 * we expected.
758 */
759 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
760 }
761
762 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
763 switch (chunk_header->chunk_type)
764 {
765 case CHUNK_TYPE_RAW:
766 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
767 chunk_data_sz))
768 {
769 fastboot_fail("Bogus chunk size for chunk type Raw");
770 return;
771 }
772
Ajay Dudaniab18f022011-05-12 14:39:22 -0700773 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
774 chunk_data_sz,
775 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -0700776 {
777 fastboot_fail("flash write failure");
778 return;
779 }
780 total_blocks += chunk_header->chunk_sz;
781 data += chunk_data_sz;
782 break;
783
784 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -0700785 total_blocks += chunk_header->chunk_sz;
786 break;
787
Ajay Dudani5c761132011-04-07 20:19:04 -0700788 case CHUNK_TYPE_CRC:
789 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
790 {
791 fastboot_fail("Bogus chunk size for chunk type Dont Care");
792 return;
793 }
794 total_blocks += chunk_header->chunk_sz;
795 data += chunk_data_sz;
796 break;
797
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -0700798 default:
Ajay Dudani5c761132011-04-07 20:19:04 -0700799 fastboot_fail("Unknown chunk type");
800 return;
801 }
802 }
803
Ajay Dudani0c6927b2011-05-18 11:12:16 -0700804 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
805 total_blocks, sparse_header->total_blks);
806
807 if(total_blocks != sparse_header->total_blks)
808 {
809 fastboot_fail("sparse image write failure");
810 }
Ajay Dudani5c761132011-04-07 20:19:04 -0700811
812 fastboot_okay("");
813 return;
814}
815
816void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
817{
818 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700819 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
820 unsigned int *magic_number = (unsigned int *) data;
821 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700822
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700823 if (magic_number[0] == SSD_HEADER_MAGIC_0 &&
824 magic_number[1] == SSD_HEADER_MAGIC_1)
825 {
826#ifdef SSD_ENABLE
Greg Griscod6250552011-06-29 14:40:23 -0700827 ret = decrypt_img_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700828#endif
Greg Griscod6250552011-06-29 14:40:23 -0700829 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700830 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
831 return;
832 }
833 }
834 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -0700835 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
836 cmd_flash_mmc_img(arg, data, sz);
837 else
838 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -0700839 return;
840}
841
Dima Zavin214cc642009-01-26 11:16:21 -0800842void cmd_flash(const char *arg, void *data, unsigned sz)
843{
844 struct ptentry *ptn;
845 struct ptable *ptable;
846 unsigned extra = 0;
847
848 ptable = flash_get_ptable();
849 if (ptable == NULL) {
850 fastboot_fail("partition table doesn't exist");
851 return;
852 }
853
854 ptn = ptable_find(ptable, arg);
855 if (ptn == NULL) {
856 fastboot_fail("unknown partition name");
857 return;
858 }
859
860 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
861 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
862 fastboot_fail("image is not a boot image");
863 return;
864 }
865 }
866
Chandan Uddarajud6d45042010-02-24 21:12:45 -0800867 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")
Channagoud Kadabi404a7062011-03-21 19:27:50 +0530868 || !strcmp(ptn->name, "persist")) {
869 if (flash_ecc_bch_enabled())
870 /* Spare data bytes for 8 bit ECC increased by 4 */
871 extra = ((page_size >> 9) * 20);
872 else
873 extra = ((page_size >> 9) * 16);
874 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800875 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800876
877 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
878 if (flash_write(ptn, extra, data, sz)) {
879 fastboot_fail("flash write failure");
880 return;
881 }
882 dprintf(INFO, "partition '%s' updated\n", ptn->name);
883 fastboot_okay("");
884}
885
886void cmd_continue(const char *arg, void *data, unsigned sz)
887{
888 fastboot_okay("");
889 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700890 if (target_is_emmc_boot())
891 {
892 boot_linux_from_mmc();
893 }
894 else
895 {
896 boot_linux_from_flash();
897 }
Dima Zavin214cc642009-01-26 11:16:21 -0800898}
899
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800900void cmd_reboot(const char *arg, void *data, unsigned sz)
901{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700902 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800903 fastboot_okay("");
904 reboot_device(0);
905}
906
907void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
908{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700909 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800910 fastboot_okay("");
911 reboot_device(FASTBOOT_MODE);
912}
913
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700914void splash_screen ()
915{
916 struct ptentry *ptn;
917 struct ptable *ptable;
918 struct fbcon_config *fb_display = NULL;
919
920 if (!target_is_emmc_boot())
921 {
922 ptable = flash_get_ptable();
923 if (ptable == NULL) {
924 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -0700925 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700926 }
927
928 ptn = ptable_find(ptable, "splash");
929 if (ptn == NULL) {
930 dprintf(CRITICAL, "ERROR: No splash partition found\n");
931 } else {
932 fb_display = fbcon_display();
933 if (fb_display) {
934 if (flash_read(ptn, 0, fb_display->base,
935 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
936 fbcon_clear();
937 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
938 }
939 }
940 }
941 }
942}
943
Brian Swetland9c4c0752009-01-25 16:23:50 -0800944void aboot_init(const struct app_descriptor *app)
945{
Shashank Mittal4f99a882010-02-01 13:58:50 -0800946 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700947 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -0700948 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -0700949
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700950 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700951 if (target_is_emmc_boot())
952 {
953 page_size = 2048;
954 page_mask = page_size - 1;
955 }
956 else
957 {
958 page_size = flash_page_size();
959 page_mask = page_size - 1;
960 }
961
Greg Griscod6250552011-06-29 14:40:23 -0700962 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700963 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800964 surf_udc_device.serialno = sn_buf;
965
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700966 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700967 if (keys_get_state(KEY_HOME) != 0)
968 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -0500969 if (keys_get_state(KEY_VOLUMEUP) != 0)
970 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700971 if(!boot_into_recovery)
972 {
973 if (keys_get_state(KEY_BACK) != 0)
974 goto fastboot;
975 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
976 goto fastboot;
977 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700978
979 #if NO_KEYPAD_DRIVER
980 /* With no keypad implementation, check the status of USB connection. */
981 /* If USB is connected then go into fastboot mode. */
982 usb_init = 1;
983 udc_init(&surf_udc_device);
984 if (usb_cable_status())
985 goto fastboot;
986 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -0700987
Ajay Dudani77421292010-10-27 19:34:06 -0700988 reboot_mode = check_reboot_mode();
989 if (reboot_mode == RECOVERY_MODE) {
990 boot_into_recovery = 1;
991 } else if(reboot_mode == FASTBOOT_MODE) {
992 goto fastboot;
993 }
994
Shashank Mittal23b8f422010-04-16 19:27:21 -0700995 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700996 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700997 if(emmc_recovery_init())
998 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700999 boot_linux_from_mmc();
1000 }
1001 else
1002 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001003 recovery_init();
1004 boot_linux_from_flash();
1005 }
Dima Zavinb4283602009-01-26 16:36:57 -08001006 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
1007 "to fastboot mode.\n");
1008
1009fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07001010
Amol Jadi57abe4c2011-05-24 15:47:27 -07001011 target_fastboot_init();
1012
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001013 if(!usb_init)
1014 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001015
1016 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001017
Shashank Mittal23b8f422010-04-16 19:27:21 -07001018 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001019 {
1020 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001021 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001022 }
1023 else
1024 {
1025 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -07001026 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -07001027 }
1028
1029 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -08001030 fastboot_register("reboot", cmd_reboot);
1031 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -07001032 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -08001033 fastboot_publish("kernel", "lk");
Kinson Chikf1a43512011-07-14 11:28:39 -07001034 partition_dump();
Vivek Mehta5f1c9d42011-04-01 20:11:59 -07001035 sz = target_get_max_flash_size();
1036 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001037 udc_start();
Brian Swetland9c4c0752009-01-25 16:23:50 -08001038}
1039
1040APP_START(aboot)
1041 .init = aboot_init,
1042APP_END
1043