blob: 68264c572d4418a99099b182236f0c5fb1355fb6 [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>
48#include <platform.h>
Dima Zavin214cc642009-01-26 11:16:21 -080049
Shashank Mittal024c0332010-02-03 11:44:00 -080050#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080051#include "bootimg.h"
52#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070053#include "sparse_format.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080054
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070055#include "scm_decrypt.h"
56
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070057#define EXPAND(NAME) #NAME
58#define TARGET(NAME) EXPAND(NAME)
Chandan Uddarajuda919832009-11-17 01:06:11 -080059#define DEFAULT_CMDLINE "mem=100M console=null";
Brian Swetland2defe162009-08-18 14:35:59 -070060
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080061#ifdef MEMBASE
62#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
63#else
David Ng183a7422009-12-07 14:55:21 -080064#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080065#endif
66
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080067#define RECOVERY_MODE 0x77665502
68#define FASTBOOT_MODE 0x77665500
69
David Ng183a7422009-12-07 14:55:21 -080070static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080071static const char *usb_sn_cmdline = " androidboot.serialno=";
David Ngf773dde2010-07-26 19:55:08 -070072static const char *battchg_pause = " androidboot.battchg_pause=true";
David Ng183a7422009-12-07 14:55:21 -080073
Ajay Dudani6cff85e2011-02-04 16:02:16 -080074static const char *baseband_apq = " androidboot.baseband=apq";
75static const char *baseband_msm = " androidboot.baseband=msm";
76static const char *baseband_csfb = " androidboot.baseband=csfb";
77static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudanid04110c2011-01-17 23:55:07 -080078
Brian Swetland9c4c0752009-01-25 16:23:50 -080079static struct udc_device surf_udc_device = {
80 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080081 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080082 .version_id = 0x0100,
83 .manufacturer = "Google",
84 .product = "Android",
85};
86
Dima Zavin42168f22009-01-30 11:52:22 -080087struct atag_ptbl_entry
88{
89 char name[16];
90 unsigned offset;
91 unsigned size;
92 unsigned flags;
93};
94
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080095char sn_buf[13];
Greg Griscod6250552011-06-29 14:40:23 -070096
97
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080098
Dima Zavin42168f22009-01-30 11:52:22 -080099static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
100{
101 struct atag_ptbl_entry atag_ptn;
102
Greg Griscod6250552011-06-29 14:40:23 -0700103 if (ptn->type == TYPE_MODEM_PARTITION) {
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800104 return;
Greg Griscod6250552011-06-29 14:40:23 -0700105 }
Dima Zavin42168f22009-01-30 11:52:22 -0800106 memcpy(atag_ptn.name, ptn->name, 16);
107 atag_ptn.name[15] = '\0';
108 atag_ptn.offset = ptn->start;
109 atag_ptn.size = ptn->length;
110 atag_ptn.flags = ptn->flags;
111 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
112 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
113}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800114
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700115void boot_linux(void *kernel, unsigned *tags,
Brian Swetland9c4c0752009-01-25 16:23:50 -0800116 const char *cmdline, unsigned machtype,
117 void *ramdisk, unsigned ramdisk_size)
118{
119 unsigned *ptr = tags;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800120 unsigned pcount = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800121 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800122 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800123 int cmdline_len = 0;
124 int have_cmdline = 0;
David Ngf773dde2010-07-26 19:55:08 -0700125 int pause_at_bootup = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800126
127 /* CORE */
128 *ptr++ = 2;
129 *ptr++ = 0x54410001;
130
131 if (ramdisk_size) {
132 *ptr++ = 4;
133 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800134 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800135 *ptr++ = ramdisk_size;
136 }
137
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800138 ptr = target_atag_mem(ptr);
139
David Ng183a7422009-12-07 14:55:21 -0800140 if (!target_is_emmc_boot()) {
141 /* Skip NAND partition ATAGS for eMMC boot */
142 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
143 int i;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800144 for(i=0; i < ptable->count; i++) {
145 struct ptentry *ptn;
146 ptn = ptable_get(ptable, i);
147 if (ptn->type == TYPE_APPS_PARTITION)
148 pcount++;
149 }
150 *ptr++ = 2 + (pcount * (sizeof(struct atag_ptbl_entry) /
David Ng183a7422009-12-07 14:55:21 -0800151 sizeof(unsigned)));
152 *ptr++ = 0x4d534d70;
153 for (i = 0; i < ptable->count; ++i)
154 ptentry_to_tag(&ptr, ptable_get(ptable, i));
155 }
Dima Zavin42168f22009-01-30 11:52:22 -0800156 }
157
Brian Swetland9c4c0752009-01-25 16:23:50 -0800158 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800159 cmdline_len = strlen(cmdline);
160 have_cmdline = 1;
161 }
162 if (target_is_emmc_boot()) {
163 cmdline_len += strlen(emmc_cmdline);
164 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800165
166 cmdline_len += strlen(usb_sn_cmdline);
167 cmdline_len += strlen(sn_buf);
168
David Ngf773dde2010-07-26 19:55:08 -0700169 if (target_pause_for_battery_charge()) {
170 pause_at_bootup = 1;
171 cmdline_len += strlen(battchg_pause);
172 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800173
174 /* Determine correct androidboot.baseband to use */
175 switch(target_baseband())
176 {
177 case BASEBAND_APQ:
178 cmdline_len += strlen(baseband_apq);
179 break;
180
181 case BASEBAND_MSM:
182 cmdline_len += strlen(baseband_msm);
183 break;
184
185 case BASEBAND_CSFB:
186 cmdline_len += strlen(baseband_csfb);
187 break;
188
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800189 case BASEBAND_SVLTE2A:
190 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800191 break;
192 }
193
David Ng183a7422009-12-07 14:55:21 -0800194 if (cmdline_len > 0) {
195 const char *src;
196 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800197 unsigned n;
198 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800199 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800200 *ptr++ = (n / 4) + 2;
201 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800202 dst = (char *)ptr;
203 if (have_cmdline) {
204 src = cmdline;
205 while ((*dst++ = *src++));
206 }
207 if (target_is_emmc_boot()) {
208 src = emmc_cmdline;
209 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700210 have_cmdline = 1;
211 while ((*dst++ = *src++));
212 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800213
214 src = usb_sn_cmdline;
215 if (have_cmdline) --dst;
216 have_cmdline = 1;
217 while ((*dst++ = *src++));
218 src = sn_buf;
219 if (have_cmdline) --dst;
220 have_cmdline = 1;
221 while ((*dst++ = *src++));
222
David Ngf773dde2010-07-26 19:55:08 -0700223 if (pause_at_bootup) {
224 src = battchg_pause;
225 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800226 while ((*dst++ = *src++));
227 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800228
229 switch(target_baseband())
230 {
231 case BASEBAND_APQ:
232 src = baseband_apq;
233 if (have_cmdline) --dst;
234 while ((*dst++ = *src++));
235 break;
236
237 case BASEBAND_MSM:
238 src = baseband_msm;
239 if (have_cmdline) --dst;
240 while ((*dst++ = *src++));
241 break;
242
243 case BASEBAND_CSFB:
244 src = baseband_csfb;
245 if (have_cmdline) --dst;
246 while ((*dst++ = *src++));
247 break;
248
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800249 case BASEBAND_SVLTE2A:
250 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800251 if (have_cmdline) --dst;
252 while ((*dst++ = *src++));
253 break;
254 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800255 ptr += (n / 4);
256 }
257
258 /* END */
259 *ptr++ = 0;
260 *ptr++ = 0;
261
262 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
263 kernel, ramdisk, ramdisk_size);
264 if (cmdline)
265 dprintf(INFO, "cmdline: %s\n", cmdline);
266
267 enter_critical_section();
268 platform_uninit_timer();
269 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800270 arch_disable_mmu();
Shashank Mittalc648e712010-10-06 18:37:42 -0700271#if DISPLAY_SPLASH_SCREEN
272 display_shutdown();
273#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800274 entry(0, machtype, tags);
275
276}
277
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800278unsigned page_size = 0;
279unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800280
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800281#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800282
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800283static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800284
Shashank Mittal23b8f422010-04-16 19:27:21 -0700285int boot_linux_from_mmc(void)
286{
287 struct boot_img_hdr *hdr = (void*) buf;
288 struct boot_img_hdr *uhdr;
289 unsigned offset = 0;
290 unsigned long long ptn = 0;
291 unsigned n = 0;
292 const char *cmdline;
293
294 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
295 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
296 dprintf(INFO, "Unified boot method!\n");
297 hdr = uhdr;
298 goto unified_boot;
299 }
Greg Griscod6250552011-06-29 14:40:23 -0700300 if (!boot_into_recovery) {
301 ptn = mmc_ptn_offset((unsigned char *) "boot");
302 if (ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700303 dprintf(CRITICAL, "ERROR: No boot partition found\n");
304 return -1;
305 }
Greg Griscod6250552011-06-29 14:40:23 -0700306 } else {
307 ptn = mmc_ptn_offset((unsigned char *) "recovery");
308 if (ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -0700309 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
310 return -1;
311 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700312 }
313
Greg Griscod6250552011-06-29 14:40:23 -0700314 if (mmc_read(ptn + offset, (unsigned int *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700315 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
316 return -1;
317 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700318
319 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700320 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -0700321 return -1;
322 }
323
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700324 if (hdr->page_size && (hdr->page_size != page_size)) {
325 page_size = hdr->page_size;
326 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700327 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700328 offset += page_size;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700329
330 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
331 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
332 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
333 return -1;
334 }
335 offset += n;
336
337 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800338 if(n != 0)
339 {
340 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
341 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
342 return -1;
343 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700344 }
345 offset += n;
346
347unified_boot:
348 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
349 hdr->kernel_size);
350 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
351 hdr->ramdisk_size);
352
353 if(hdr->cmdline[0]) {
354 cmdline = (char*) hdr->cmdline;
355 } else {
356 cmdline = DEFAULT_CMDLINE;
357 }
358 dprintf(INFO, "cmdline = '%s'\n", cmdline);
359
360 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700361 boot_linux((void *)hdr->kernel_addr, hdr->tags_addr,
Shashank Mittal23b8f422010-04-16 19:27:21 -0700362 (const char *)cmdline, board_machtype(),
363 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
364
365 return 0;
366}
367
Dima Zavin214cc642009-01-26 11:16:21 -0800368int boot_linux_from_flash(void)
369{
370 struct boot_img_hdr *hdr = (void*) buf;
371 unsigned n;
372 struct ptentry *ptn;
373 struct ptable *ptable;
374 unsigned offset = 0;
375 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800376
David Ng183a7422009-12-07 14:55:21 -0800377 if (target_is_emmc_boot()) {
378 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
379 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
380 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
381 return -1;
382 }
383 goto continue_boot;
384 }
385
Dima Zavin214cc642009-01-26 11:16:21 -0800386 ptable = flash_get_ptable();
387 if (ptable == NULL) {
388 dprintf(CRITICAL, "ERROR: Partition table not found\n");
389 return -1;
390 }
391
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800392 if(!boot_into_recovery)
393 {
394 ptn = ptable_find(ptable, "boot");
395 if (ptn == NULL) {
396 dprintf(CRITICAL, "ERROR: No boot partition found\n");
397 return -1;
398 }
399 }
400 else
401 {
402 ptn = ptable_find(ptable, "recovery");
403 if (ptn == NULL) {
404 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
405 return -1;
406 }
Dima Zavin214cc642009-01-26 11:16:21 -0800407 }
408
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800409 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800410 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
411 return -1;
412 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800413 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800414
415 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700416 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -0800417 return -1;
418 }
419
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800420 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -0700421 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 -0800422 return -1;
423 }
424
425 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800426 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
427 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
428 return -1;
429 }
430 offset += n;
431
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800432 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800433 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
434 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
435 return -1;
436 }
437 offset += n;
438
David Ng183a7422009-12-07 14:55:21 -0800439continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800440 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
441 hdr->kernel_size);
442 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
443 hdr->ramdisk_size);
444
445 if(hdr->cmdline[0]) {
446 cmdline = (char*) hdr->cmdline;
447 } else {
448 cmdline = DEFAULT_CMDLINE;
449 }
450 dprintf(INFO, "cmdline = '%s'\n", cmdline);
451
452 /* TODO: create/pass atags to kernel */
453
454 dprintf(INFO, "\nBooting Linux\n");
Ajay Dudanie28a6072011-07-01 13:59:46 -0700455 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800456 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800457 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
458
459 return 0;
460}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800461
462void cmd_boot(const char *arg, void *data, unsigned sz)
463{
464 unsigned kernel_actual;
465 unsigned ramdisk_actual;
466 static struct boot_img_hdr hdr;
467 char *ptr = ((char*) data);
468
469 if (sz < sizeof(hdr)) {
470 fastboot_fail("invalid bootimage header");
471 return;
472 }
473
474 memcpy(&hdr, data, sizeof(hdr));
475
476 /* ensure commandline is terminated */
477 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
478
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700479 if(target_is_emmc_boot() && hdr.page_size) {
480 page_size = hdr.page_size;
481 page_mask = page_size - 1;
482 }
483
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800484 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
485 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800486
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700487 if (page_size + kernel_actual + ramdisk_actual < sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800488 fastboot_fail("incomplete bootimage");
489 return;
490 }
491
Ajay Dudanie28a6072011-07-01 13:59:46 -0700492 memmove((void*) hdr.kernel_addr, ptr + page_size, hdr.kernel_size);
493 memmove((void*) hdr.ramdisk_addr, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800494
495 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800496 target_battery_charging_enable(0, 1);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800497 udc_stop();
498
Ajay Dudanie28a6072011-07-01 13:59:46 -0700499 boot_linux((void*) hdr.kernel_addr, (void*) TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800500 (const char*) hdr.cmdline, board_machtype(),
Ajay Dudanie28a6072011-07-01 13:59:46 -0700501 (void*) hdr.ramdisk_addr, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800502}
503
Dima Zavin214cc642009-01-26 11:16:21 -0800504void cmd_erase(const char *arg, void *data, unsigned sz)
505{
506 struct ptentry *ptn;
507 struct ptable *ptable;
508
509 ptable = flash_get_ptable();
510 if (ptable == NULL) {
511 fastboot_fail("partition table doesn't exist");
512 return;
513 }
514
515 ptn = ptable_find(ptable, arg);
516 if (ptn == NULL) {
517 fastboot_fail("unknown partition name");
518 return;
519 }
520
521 if (flash_erase(ptn)) {
522 fastboot_fail("failed to erase partition");
523 return;
524 }
525 fastboot_okay("");
526}
527
Bikas Gurungd48bd242010-09-04 19:54:32 -0700528
529void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
530{
531 unsigned long long ptn = 0;
532 unsigned int out[512] = {0};
533
Greg Griscod6250552011-06-29 14:40:23 -0700534 ptn = mmc_ptn_offset((unsigned char *) arg);
535 if (ptn == 0) {
Bikas Gurungd48bd242010-09-04 19:54:32 -0700536 fastboot_fail("partition table doesn't exist");
537 return;
538 }
539
540
541 /* Simple inefficient version of erase. Just writing
542 0 in first block */
543 if (mmc_write(ptn , 512, (unsigned int *)out)) {
544 fastboot_fail("failed to erase partition");
545 return;
546 }
547 fastboot_okay("");
548}
549
550
Ajay Dudani5c761132011-04-07 20:19:04 -0700551void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -0700552{
553 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700554 unsigned long long size = 0;
555
Greg Griscod6250552011-06-29 14:40:23 -0700556 ptn = mmc_ptn_offset((unsigned char *) arg);
557 if (ptn == 0) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700558 fastboot_fail("partition table doesn't exist");
559 return;
560 }
561
562 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
563 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
564 fastboot_fail("image is not a boot image");
565 return;
566 }
567 }
568
Greg Griscod6250552011-06-29 14:40:23 -0700569 size = mmc_ptn_size((unsigned char *) arg);
570 if (ROUND_TO_PAGE(sz, 511) > size) {
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700571 fastboot_fail("size too large");
572 return;
573 }
574
Greg Griscod6250552011-06-29 14:40:23 -0700575 if (mmc_write(ptn , sz, (unsigned int *) data)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -0700576 fastboot_fail("flash write failure");
577 return;
578 }
579 fastboot_okay("");
580 return;
581}
582
Greg Griscod6250552011-06-29 14:40:23 -0700583
584
Ajay Dudani5c761132011-04-07 20:19:04 -0700585void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
586{
587 unsigned int chunk;
588 unsigned int chunk_data_sz;
589 sparse_header_t *sparse_header;
590 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -0700591 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700592 unsigned long long ptn = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700593
Greg Griscod6250552011-06-29 14:40:23 -0700594 ptn = mmc_ptn_offset((unsigned char *) arg);
595 if (ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -0700596 fastboot_fail("partition table doesn't exist");
597 return;
598 }
599
600 /* Read and skip over sparse image header */
601 sparse_header = (sparse_header_t *) data;
602 data += sparse_header->file_hdr_sz;
603 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
604 {
605 /* Skip the remaining bytes in a header that is longer than
606 * we expected.
607 */
608 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
609 }
610
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700611 dprintf (SPEW, "=== Sparse Image Header ===\n");
612 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
613 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
614 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
615 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
616 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
617 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
618 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
619 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -0700620
621 /* Start processing chunks */
622 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
623 {
624 /* Read and skip over chunk header */
625 chunk_header = (chunk_header_t *) data;
626 data += sizeof(chunk_header_t);
627
628 dprintf (SPEW, "=== Chunk Header ===\n");
629 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
630 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
631 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
632
633 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
634 {
635 /* Skip the remaining bytes in a header that is longer than
636 * we expected.
637 */
638 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
639 }
640
641 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
642 switch (chunk_header->chunk_type)
643 {
644 case CHUNK_TYPE_RAW:
645 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
646 chunk_data_sz))
647 {
648 fastboot_fail("Bogus chunk size for chunk type Raw");
649 return;
650 }
651
Ajay Dudaniab18f022011-05-12 14:39:22 -0700652 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
653 chunk_data_sz,
654 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -0700655 {
656 fastboot_fail("flash write failure");
657 return;
658 }
659 total_blocks += chunk_header->chunk_sz;
660 data += chunk_data_sz;
661 break;
662
663 case CHUNK_TYPE_DONT_CARE:
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -0700664 total_blocks += chunk_header->chunk_sz;
665 break;
666
Ajay Dudani5c761132011-04-07 20:19:04 -0700667 case CHUNK_TYPE_CRC:
668 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
669 {
670 fastboot_fail("Bogus chunk size for chunk type Dont Care");
671 return;
672 }
673 total_blocks += chunk_header->chunk_sz;
674 data += chunk_data_sz;
675 break;
676
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -0700677 default:
Ajay Dudani5c761132011-04-07 20:19:04 -0700678 fastboot_fail("Unknown chunk type");
679 return;
680 }
681 }
682
Ajay Dudani0c6927b2011-05-18 11:12:16 -0700683 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
684 total_blocks, sparse_header->total_blks);
685
686 if(total_blocks != sparse_header->total_blks)
687 {
688 fastboot_fail("sparse image write failure");
689 }
Ajay Dudani5c761132011-04-07 20:19:04 -0700690
691 fastboot_okay("");
692 return;
693}
694
695void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
696{
697 sparse_header_t *sparse_header;
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700698 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
699 unsigned int *magic_number = (unsigned int *) data;
700 int ret=0;
Ajay Dudani5c761132011-04-07 20:19:04 -0700701
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700702 if (magic_number[0] == SSD_HEADER_MAGIC_0 &&
703 magic_number[1] == SSD_HEADER_MAGIC_1)
704 {
705#ifdef SSD_ENABLE
Greg Griscod6250552011-06-29 14:40:23 -0700706 ret = decrypt_img_scm((uint32 **) &data, &sz);
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700707#endif
Greg Griscod6250552011-06-29 14:40:23 -0700708 if (ret != 0) {
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -0700709 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
710 return;
711 }
712 }
713 sparse_header = (sparse_header_t *) data;
Ajay Dudani5c761132011-04-07 20:19:04 -0700714 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
715 cmd_flash_mmc_img(arg, data, sz);
716 else
717 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudani5c761132011-04-07 20:19:04 -0700718 return;
719}
720
Dima Zavin214cc642009-01-26 11:16:21 -0800721void cmd_flash(const char *arg, void *data, unsigned sz)
722{
723 struct ptentry *ptn;
724 struct ptable *ptable;
725 unsigned extra = 0;
726
727 ptable = flash_get_ptable();
728 if (ptable == NULL) {
729 fastboot_fail("partition table doesn't exist");
730 return;
731 }
732
733 ptn = ptable_find(ptable, arg);
734 if (ptn == NULL) {
735 fastboot_fail("unknown partition name");
736 return;
737 }
738
739 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
740 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
741 fastboot_fail("image is not a boot image");
742 return;
743 }
744 }
745
Chandan Uddarajud6d45042010-02-24 21:12:45 -0800746 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")
Channagoud Kadabi404a7062011-03-21 19:27:50 +0530747 || !strcmp(ptn->name, "persist")) {
748 if (flash_ecc_bch_enabled())
749 /* Spare data bytes for 8 bit ECC increased by 4 */
750 extra = ((page_size >> 9) * 20);
751 else
752 extra = ((page_size >> 9) * 16);
753 } else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800754 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800755
756 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
757 if (flash_write(ptn, extra, data, sz)) {
758 fastboot_fail("flash write failure");
759 return;
760 }
761 dprintf(INFO, "partition '%s' updated\n", ptn->name);
762 fastboot_okay("");
763}
764
765void cmd_continue(const char *arg, void *data, unsigned sz)
766{
767 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800768 target_battery_charging_enable(0, 1);
Dima Zavin214cc642009-01-26 11:16:21 -0800769 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700770 if (target_is_emmc_boot())
771 {
772 boot_linux_from_mmc();
773 }
774 else
775 {
776 boot_linux_from_flash();
777 }
Dima Zavin214cc642009-01-26 11:16:21 -0800778}
779
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800780void cmd_reboot(const char *arg, void *data, unsigned sz)
781{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700782 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800783 fastboot_okay("");
784 reboot_device(0);
785}
786
787void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
788{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700789 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800790 fastboot_okay("");
791 reboot_device(FASTBOOT_MODE);
792}
793
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700794void splash_screen ()
795{
796 struct ptentry *ptn;
797 struct ptable *ptable;
798 struct fbcon_config *fb_display = NULL;
799
800 if (!target_is_emmc_boot())
801 {
802 ptable = flash_get_ptable();
803 if (ptable == NULL) {
804 dprintf(CRITICAL, "ERROR: Partition table not found\n");
Greg Griscod6250552011-06-29 14:40:23 -0700805 return;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700806 }
807
808 ptn = ptable_find(ptable, "splash");
809 if (ptn == NULL) {
810 dprintf(CRITICAL, "ERROR: No splash partition found\n");
811 } else {
812 fb_display = fbcon_display();
813 if (fb_display) {
814 if (flash_read(ptn, 0, fb_display->base,
815 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
816 fbcon_clear();
817 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
818 }
819 }
820 }
821 }
822}
823
Brian Swetland9c4c0752009-01-25 16:23:50 -0800824void aboot_init(const struct app_descriptor *app)
825{
Shashank Mittal4f99a882010-02-01 13:58:50 -0800826 unsigned reboot_mode = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700827 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -0700828 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -0700829
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700830 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700831 if (target_is_emmc_boot())
832 {
833 page_size = 2048;
834 page_mask = page_size - 1;
835 }
836 else
837 {
838 page_size = flash_page_size();
839 page_mask = page_size - 1;
840 }
841
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700842 /* Display splash screen if enabled */
843 #if DISPLAY_SPLASH_SCREEN
844 display_init();
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700845 dprintf(SPEW, "Diplay initialized\n");
Greg Griscod6250552011-06-29 14:40:23 -0700846 display_image_on_screen();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700847 #endif
848
Greg Griscod6250552011-06-29 14:40:23 -0700849 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -0700850 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800851 surf_udc_device.serialno = sn_buf;
852
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700853 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700854 if (keys_get_state(KEY_HOME) != 0)
855 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -0500856 if (keys_get_state(KEY_VOLUMEUP) != 0)
857 boot_into_recovery = 1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700858 if(!boot_into_recovery)
859 {
860 if (keys_get_state(KEY_BACK) != 0)
861 goto fastboot;
862 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
863 goto fastboot;
864 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700865
866 #if NO_KEYPAD_DRIVER
867 /* With no keypad implementation, check the status of USB connection. */
868 /* If USB is connected then go into fastboot mode. */
869 usb_init = 1;
870 udc_init(&surf_udc_device);
871 if (usb_cable_status())
872 goto fastboot;
873 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -0700874
Ajay Dudani77421292010-10-27 19:34:06 -0700875 reboot_mode = check_reboot_mode();
876 if (reboot_mode == RECOVERY_MODE) {
877 boot_into_recovery = 1;
878 } else if(reboot_mode == FASTBOOT_MODE) {
879 goto fastboot;
880 }
881
Shashank Mittal23b8f422010-04-16 19:27:21 -0700882 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700883 {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700884 if(emmc_recovery_init())
885 dprintf(ALWAYS,"error in emmc_recovery_init\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700886 boot_linux_from_mmc();
887 }
888 else
889 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700890 recovery_init();
891 boot_linux_from_flash();
892 }
Dima Zavinb4283602009-01-26 16:36:57 -0800893 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
894 "to fastboot mode.\n");
895
896fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700897
Amol Jadi57abe4c2011-05-24 15:47:27 -0700898 target_fastboot_init();
899
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700900 if(!usb_init)
901 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800902
903 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700904
Shashank Mittal23b8f422010-04-16 19:27:21 -0700905 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700906 {
907 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700908 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700909 }
910 else
911 {
912 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700913 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700914 }
915
916 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800917 fastboot_register("reboot", cmd_reboot);
918 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700919 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -0800920 fastboot_publish("kernel", "lk");
Vivek Mehta5f1c9d42011-04-01 20:11:59 -0700921 sz = target_get_max_flash_size();
922 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800923 udc_start();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700924 target_battery_charging_enable(1, 0);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800925}
926
927APP_START(aboot)
928 .init = aboot_init,
929APP_END
930