blob: 392bdaefacd0e5e5979d928aff49b098fa5b159e [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>
Dima Zavin214cc642009-01-26 11:16:21 -080046
Shashank Mittal024c0332010-02-03 11:44:00 -080047#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080048#include "bootimg.h"
49#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070050#include "sparse_format.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080051
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070052#define EXPAND(NAME) #NAME
53#define TARGET(NAME) EXPAND(NAME)
Chandan Uddarajuda919832009-11-17 01:06:11 -080054#define DEFAULT_CMDLINE "mem=100M console=null";
Brian Swetland2defe162009-08-18 14:35:59 -070055
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080056#ifdef MEMBASE
57#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
58#else
David Ng183a7422009-12-07 14:55:21 -080059#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080060#endif
61
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080062#define RECOVERY_MODE 0x77665502
63#define FASTBOOT_MODE 0x77665500
64
David Ng183a7422009-12-07 14:55:21 -080065static const char *emmc_cmdline = " androidboot.emmc=true";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080066static const char *usb_sn_cmdline = " androidboot.serialno=";
David Ngf773dde2010-07-26 19:55:08 -070067static const char *battchg_pause = " androidboot.battchg_pause=true";
David Ng183a7422009-12-07 14:55:21 -080068
Ajay Dudani6cff85e2011-02-04 16:02:16 -080069static const char *baseband_apq = " androidboot.baseband=apq";
70static const char *baseband_msm = " androidboot.baseband=msm";
71static const char *baseband_csfb = " androidboot.baseband=csfb";
72static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudanid04110c2011-01-17 23:55:07 -080073
Brian Swetland9c4c0752009-01-25 16:23:50 -080074static struct udc_device surf_udc_device = {
75 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080076 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080077 .version_id = 0x0100,
78 .manufacturer = "Google",
79 .product = "Android",
80};
81
Dima Zavin42168f22009-01-30 11:52:22 -080082struct atag_ptbl_entry
83{
84 char name[16];
85 unsigned offset;
86 unsigned size;
87 unsigned flags;
88};
89
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080090char sn_buf[13];
Brian Swetland9c4c0752009-01-25 16:23:50 -080091void platform_uninit_timer(void);
Chandan Uddarajuc6860e12009-11-19 11:22:15 -080092unsigned* target_atag_mem(unsigned* ptr);
Chandan Uddaraju885e4db2009-12-03 22:45:26 -080093unsigned board_machtype(void);
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080094unsigned check_reboot_mode(void);
David Ng3679bc52010-02-09 15:43:43 -080095void *target_get_scratch_address(void);
Vivek Mehta5f1c9d42011-04-01 20:11:59 -070096unsigned target_get_max_flash_size(void);
David Ng183a7422009-12-07 14:55:21 -080097int target_is_emmc_boot(void);
Chandan Uddaraju94183c02010-01-15 15:13:59 -080098void reboot_device(unsigned);
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -080099void target_battery_charging_enable(unsigned enable, unsigned disconnect);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700100unsigned int mmc_write (unsigned long long data_addr,
101 unsigned int data_len, unsigned int* in);
Shashank Mittal920798e2010-10-04 17:17:37 -0700102unsigned long long mmc_ptn_offset (unsigned char * name);
103unsigned long long mmc_ptn_size (unsigned char * name);
Shashank Mittalc648e712010-10-06 18:37:42 -0700104void display_shutdown(void);
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800105
Dima Zavin42168f22009-01-30 11:52:22 -0800106static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
107{
108 struct atag_ptbl_entry atag_ptn;
109
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800110 if (ptn->type == TYPE_MODEM_PARTITION)
111 return;
Dima Zavin42168f22009-01-30 11:52:22 -0800112 memcpy(atag_ptn.name, ptn->name, 16);
113 atag_ptn.name[15] = '\0';
114 atag_ptn.offset = ptn->start;
115 atag_ptn.size = ptn->length;
116 atag_ptn.flags = ptn->flags;
117 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
118 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
119}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800120
121void boot_linux(void *kernel, unsigned *tags,
122 const char *cmdline, unsigned machtype,
123 void *ramdisk, unsigned ramdisk_size)
124{
125 unsigned *ptr = tags;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800126 unsigned pcount = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800127 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800128 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800129 int cmdline_len = 0;
130 int have_cmdline = 0;
David Ngf773dde2010-07-26 19:55:08 -0700131 int pause_at_bootup = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800132
133 /* CORE */
134 *ptr++ = 2;
135 *ptr++ = 0x54410001;
136
137 if (ramdisk_size) {
138 *ptr++ = 4;
139 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800140 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800141 *ptr++ = ramdisk_size;
142 }
143
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800144 ptr = target_atag_mem(ptr);
145
David Ng183a7422009-12-07 14:55:21 -0800146 if (!target_is_emmc_boot()) {
147 /* Skip NAND partition ATAGS for eMMC boot */
148 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
149 int i;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800150 for(i=0; i < ptable->count; i++) {
151 struct ptentry *ptn;
152 ptn = ptable_get(ptable, i);
153 if (ptn->type == TYPE_APPS_PARTITION)
154 pcount++;
155 }
156 *ptr++ = 2 + (pcount * (sizeof(struct atag_ptbl_entry) /
David Ng183a7422009-12-07 14:55:21 -0800157 sizeof(unsigned)));
158 *ptr++ = 0x4d534d70;
159 for (i = 0; i < ptable->count; ++i)
160 ptentry_to_tag(&ptr, ptable_get(ptable, i));
161 }
Dima Zavin42168f22009-01-30 11:52:22 -0800162 }
163
Brian Swetland9c4c0752009-01-25 16:23:50 -0800164 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800165 cmdline_len = strlen(cmdline);
166 have_cmdline = 1;
167 }
168 if (target_is_emmc_boot()) {
169 cmdline_len += strlen(emmc_cmdline);
170 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800171
172 cmdline_len += strlen(usb_sn_cmdline);
173 cmdline_len += strlen(sn_buf);
174
David Ngf773dde2010-07-26 19:55:08 -0700175 if (target_pause_for_battery_charge()) {
176 pause_at_bootup = 1;
177 cmdline_len += strlen(battchg_pause);
178 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800179
180 /* Determine correct androidboot.baseband to use */
181 switch(target_baseband())
182 {
183 case BASEBAND_APQ:
184 cmdline_len += strlen(baseband_apq);
185 break;
186
187 case BASEBAND_MSM:
188 cmdline_len += strlen(baseband_msm);
189 break;
190
191 case BASEBAND_CSFB:
192 cmdline_len += strlen(baseband_csfb);
193 break;
194
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800195 case BASEBAND_SVLTE2A:
196 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800197 break;
198 }
199
David Ng183a7422009-12-07 14:55:21 -0800200 if (cmdline_len > 0) {
201 const char *src;
202 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800203 unsigned n;
204 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800205 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800206 *ptr++ = (n / 4) + 2;
207 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800208 dst = (char *)ptr;
209 if (have_cmdline) {
210 src = cmdline;
211 while ((*dst++ = *src++));
212 }
213 if (target_is_emmc_boot()) {
214 src = emmc_cmdline;
215 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700216 have_cmdline = 1;
217 while ((*dst++ = *src++));
218 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800219
220 src = usb_sn_cmdline;
221 if (have_cmdline) --dst;
222 have_cmdline = 1;
223 while ((*dst++ = *src++));
224 src = sn_buf;
225 if (have_cmdline) --dst;
226 have_cmdline = 1;
227 while ((*dst++ = *src++));
228
David Ngf773dde2010-07-26 19:55:08 -0700229 if (pause_at_bootup) {
230 src = battchg_pause;
231 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800232 while ((*dst++ = *src++));
233 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800234
235 switch(target_baseband())
236 {
237 case BASEBAND_APQ:
238 src = baseband_apq;
239 if (have_cmdline) --dst;
240 while ((*dst++ = *src++));
241 break;
242
243 case BASEBAND_MSM:
244 src = baseband_msm;
245 if (have_cmdline) --dst;
246 while ((*dst++ = *src++));
247 break;
248
249 case BASEBAND_CSFB:
250 src = baseband_csfb;
251 if (have_cmdline) --dst;
252 while ((*dst++ = *src++));
253 break;
254
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800255 case BASEBAND_SVLTE2A:
256 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800257 if (have_cmdline) --dst;
258 while ((*dst++ = *src++));
259 break;
260 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800261 ptr += (n / 4);
262 }
263
264 /* END */
265 *ptr++ = 0;
266 *ptr++ = 0;
267
268 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
269 kernel, ramdisk, ramdisk_size);
270 if (cmdline)
271 dprintf(INFO, "cmdline: %s\n", cmdline);
272
273 enter_critical_section();
274 platform_uninit_timer();
275 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800276 arch_disable_mmu();
Shashank Mittalc648e712010-10-06 18:37:42 -0700277#if DISPLAY_SPLASH_SCREEN
278 display_shutdown();
279#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800280 entry(0, machtype, tags);
281
282}
283
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800284unsigned page_size = 0;
285unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800286
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800287#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800288
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800289static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800290
Shashank Mittal23b8f422010-04-16 19:27:21 -0700291int boot_linux_from_mmc(void)
292{
293 struct boot_img_hdr *hdr = (void*) buf;
294 struct boot_img_hdr *uhdr;
295 unsigned offset = 0;
296 unsigned long long ptn = 0;
297 unsigned n = 0;
298 const char *cmdline;
299
300 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
301 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
302 dprintf(INFO, "Unified boot method!\n");
303 hdr = uhdr;
304 goto unified_boot;
305 }
Shashank Mittal85b91f62010-10-30 10:12:38 -0700306 if(!boot_into_recovery)
307 {
308 ptn = mmc_ptn_offset("boot");
309 if(ptn == 0) {
310 dprintf(CRITICAL, "ERROR: No boot partition found\n");
311 return -1;
312 }
313 }
314 else
315 {
316 ptn = mmc_ptn_offset("recovery");
317 if(ptn == 0) {
318 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
319 return -1;
320 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700321 }
322
323 if (mmc_read(ptn + offset, (unsigned int *)buf, page_size)) {
324 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
325 return -1;
326 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700327
328 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
329 dprintf(CRITICAL, "ERROR: Invaled boot image header\n");
330 return -1;
331 }
332
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700333 if (hdr->page_size && (hdr->page_size != page_size)) {
334 page_size = hdr->page_size;
335 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700336 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700337 offset += page_size;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700338
339 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
340 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
341 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
342 return -1;
343 }
344 offset += n;
345
346 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800347 if(n != 0)
348 {
349 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
350 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
351 return -1;
352 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700353 }
354 offset += n;
355
356unified_boot:
357 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
358 hdr->kernel_size);
359 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
360 hdr->ramdisk_size);
361
362 if(hdr->cmdline[0]) {
363 cmdline = (char*) hdr->cmdline;
364 } else {
365 cmdline = DEFAULT_CMDLINE;
366 }
367 dprintf(INFO, "cmdline = '%s'\n", cmdline);
368
369 dprintf(INFO, "\nBooting Linux\n");
370 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
371 (const char *)cmdline, board_machtype(),
372 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
373
374 return 0;
375}
376
Dima Zavin214cc642009-01-26 11:16:21 -0800377int boot_linux_from_flash(void)
378{
379 struct boot_img_hdr *hdr = (void*) buf;
380 unsigned n;
381 struct ptentry *ptn;
382 struct ptable *ptable;
383 unsigned offset = 0;
384 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800385
David Ng183a7422009-12-07 14:55:21 -0800386 if (target_is_emmc_boot()) {
387 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
388 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
389 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
390 return -1;
391 }
392 goto continue_boot;
393 }
394
Dima Zavin214cc642009-01-26 11:16:21 -0800395 ptable = flash_get_ptable();
396 if (ptable == NULL) {
397 dprintf(CRITICAL, "ERROR: Partition table not found\n");
398 return -1;
399 }
400
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800401 if(!boot_into_recovery)
402 {
403 ptn = ptable_find(ptable, "boot");
404 if (ptn == NULL) {
405 dprintf(CRITICAL, "ERROR: No boot partition found\n");
406 return -1;
407 }
408 }
409 else
410 {
411 ptn = ptable_find(ptable, "recovery");
412 if (ptn == NULL) {
413 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
414 return -1;
415 }
Dima Zavin214cc642009-01-26 11:16:21 -0800416 }
417
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800418 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800419 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
420 return -1;
421 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800422 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800423
424 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
425 dprintf(CRITICAL, "ERROR: Invaled boot image heador\n");
426 return -1;
427 }
428
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800429 if (hdr->page_size != page_size) {
430 dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
431 return -1;
432 }
433
434 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800435 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
436 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
437 return -1;
438 }
439 offset += n;
440
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800441 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800442 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
443 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
444 return -1;
445 }
446 offset += n;
447
David Ng183a7422009-12-07 14:55:21 -0800448continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800449 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
450 hdr->kernel_size);
451 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
452 hdr->ramdisk_size);
453
454 if(hdr->cmdline[0]) {
455 cmdline = (char*) hdr->cmdline;
456 } else {
457 cmdline = DEFAULT_CMDLINE;
458 }
459 dprintf(INFO, "cmdline = '%s'\n", cmdline);
460
461 /* TODO: create/pass atags to kernel */
462
463 dprintf(INFO, "\nBooting Linux\n");
464 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800465 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800466 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
467
468 return 0;
469}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800470
471void cmd_boot(const char *arg, void *data, unsigned sz)
472{
473 unsigned kernel_actual;
474 unsigned ramdisk_actual;
475 static struct boot_img_hdr hdr;
476 char *ptr = ((char*) data);
477
478 if (sz < sizeof(hdr)) {
479 fastboot_fail("invalid bootimage header");
480 return;
481 }
482
483 memcpy(&hdr, data, sizeof(hdr));
484
485 /* ensure commandline is terminated */
486 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
487
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700488 if(target_is_emmc_boot() && hdr.page_size) {
489 page_size = hdr.page_size;
490 page_mask = page_size - 1;
491 }
492
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800493 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
494 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800495
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700496 if (page_size + kernel_actual + ramdisk_actual < sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800497 fastboot_fail("incomplete bootimage");
498 return;
499 }
500
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700501 memmove((void*) KERNEL_ADDR, ptr + page_size, hdr.kernel_size);
502 memmove((void*) RAMDISK_ADDR, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800503
504 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800505 target_battery_charging_enable(0, 1);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800506 udc_stop();
507
Brian Swetland9c4c0752009-01-25 16:23:50 -0800508 boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800509 (const char*) hdr.cmdline, board_machtype(),
Brian Swetland9c4c0752009-01-25 16:23:50 -0800510 (void*) RAMDISK_ADDR, hdr.ramdisk_size);
511}
512
Dima Zavin214cc642009-01-26 11:16:21 -0800513void cmd_erase(const char *arg, void *data, unsigned sz)
514{
515 struct ptentry *ptn;
516 struct ptable *ptable;
517
518 ptable = flash_get_ptable();
519 if (ptable == NULL) {
520 fastboot_fail("partition table doesn't exist");
521 return;
522 }
523
524 ptn = ptable_find(ptable, arg);
525 if (ptn == NULL) {
526 fastboot_fail("unknown partition name");
527 return;
528 }
529
530 if (flash_erase(ptn)) {
531 fastboot_fail("failed to erase partition");
532 return;
533 }
534 fastboot_okay("");
535}
536
Bikas Gurungd48bd242010-09-04 19:54:32 -0700537
538void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
539{
540 unsigned long long ptn = 0;
541 unsigned int out[512] = {0};
542
543 ptn = mmc_ptn_offset(arg);
544 if(ptn == 0) {
545 fastboot_fail("partition table doesn't exist");
546 return;
547 }
548
549
550 /* Simple inefficient version of erase. Just writing
551 0 in first block */
552 if (mmc_write(ptn , 512, (unsigned int *)out)) {
553 fastboot_fail("failed to erase partition");
554 return;
555 }
556 fastboot_okay("");
557}
558
559
Ajay Dudani5c761132011-04-07 20:19:04 -0700560void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -0700561{
562 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700563 unsigned long long size = 0;
564
Shashank Mittal23b8f422010-04-16 19:27:21 -0700565 ptn = mmc_ptn_offset(arg);
566 if(ptn == 0) {
567 fastboot_fail("partition table doesn't exist");
568 return;
569 }
570
571 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
572 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
573 fastboot_fail("image is not a boot image");
574 return;
575 }
576 }
577
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700578 size = mmc_ptn_size(arg);
579 if (ROUND_TO_PAGE(sz,511) > size) {
580 fastboot_fail("size too large");
581 return;
582 }
583
Shashank Mittal23b8f422010-04-16 19:27:21 -0700584 if (mmc_write(ptn , sz, (unsigned int *)data)) {
585 fastboot_fail("flash write failure");
586 return;
587 }
588 fastboot_okay("");
589 return;
590}
591
Ajay Dudani5c761132011-04-07 20:19:04 -0700592void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
593{
594 unsigned int chunk;
595 unsigned int chunk_data_sz;
596 sparse_header_t *sparse_header;
597 chunk_header_t *chunk_header;
598 uint32_t crc32 = 0;
599 uint32_t total_blocks = 0;
600 unsigned long long ptn = 0;
601 unsigned long long size = 0;
602
603 ptn = mmc_ptn_offset(arg);
604 if(ptn == 0) {
605 fastboot_fail("partition table doesn't exist");
606 return;
607 }
608
609 /* Read and skip over sparse image header */
610 sparse_header = (sparse_header_t *) data;
611 data += sparse_header->file_hdr_sz;
612 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
613 {
614 /* Skip the remaining bytes in a header that is longer than
615 * we expected.
616 */
617 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
618 }
619
620 dprintf (INFO, "=== Sparse Image Header ===\n");
621 dprintf (INFO, "magic: 0x%x\n", sparse_header->magic);
622 dprintf (INFO, "major_version: 0x%x\n", sparse_header->major_version);
623 dprintf (INFO, "minor_version: 0x%x\n", sparse_header->minor_version);
624 dprintf (INFO, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
625 dprintf (INFO, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
626 dprintf (INFO, "blk_sz: %d\n", sparse_header->blk_sz);
627 dprintf (INFO, "total_blks: %d\n", sparse_header->total_blks);
628 dprintf (INFO, "total_chunks: %d\n", sparse_header->total_chunks);
629
630 /* Start processing chunks */
631 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
632 {
633 /* Read and skip over chunk header */
634 chunk_header = (chunk_header_t *) data;
635 data += sizeof(chunk_header_t);
636
637 dprintf (SPEW, "=== Chunk Header ===\n");
638 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
639 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
640 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
641
642 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
643 {
644 /* Skip the remaining bytes in a header that is longer than
645 * we expected.
646 */
647 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
648 }
649
650 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
651 switch (chunk_header->chunk_type)
652 {
653 case CHUNK_TYPE_RAW:
654 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
655 chunk_data_sz))
656 {
657 fastboot_fail("Bogus chunk size for chunk type Raw");
658 return;
659 }
660
661 if(mmc_write(ptn + (total_blocks*sparse_header->blk_sz),
662 chunk_data_sz,
663 (unsigned int*)data))
664 {
665 fastboot_fail("flash write failure");
666 return;
667 }
668 total_blocks += chunk_header->chunk_sz;
669 data += chunk_data_sz;
670 break;
671
672 case CHUNK_TYPE_DONT_CARE:
673 case CHUNK_TYPE_CRC:
674 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
675 {
676 fastboot_fail("Bogus chunk size for chunk type Dont Care");
677 return;
678 }
679 total_blocks += chunk_header->chunk_sz;
680 data += chunk_data_sz;
681 break;
682
683 fastboot_fail("Unknown chunk type");
684 return;
685 }
686 }
687
688 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
689 total_blocks, sparse_header->total_blks);
690
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;
698 sparse_header = (sparse_header_t *) data;
699
700 if (sparse_header->magic != SPARSE_HEADER_MAGIC)
701 cmd_flash_mmc_img(arg, data, sz);
702 else
703 cmd_flash_mmc_sparse_img(arg, data, sz);
704
705 return;
706}
707
Dima Zavin214cc642009-01-26 11:16:21 -0800708void cmd_flash(const char *arg, void *data, unsigned sz)
709{
710 struct ptentry *ptn;
711 struct ptable *ptable;
712 unsigned extra = 0;
713
714 ptable = flash_get_ptable();
715 if (ptable == NULL) {
716 fastboot_fail("partition table doesn't exist");
717 return;
718 }
719
720 ptn = ptable_find(ptable, arg);
721 if (ptn == NULL) {
722 fastboot_fail("unknown partition name");
723 return;
724 }
725
726 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
727 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
728 fastboot_fail("image is not a boot image");
729 return;
730 }
731 }
732
Chandan Uddarajud6d45042010-02-24 21:12:45 -0800733 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")
734 || !strcmp(ptn->name, "persist"))
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800735 extra = ((page_size >> 9) * 16);
Dima Zavin214cc642009-01-26 11:16:21 -0800736 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800737 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800738
739 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
740 if (flash_write(ptn, extra, data, sz)) {
741 fastboot_fail("flash write failure");
742 return;
743 }
744 dprintf(INFO, "partition '%s' updated\n", ptn->name);
745 fastboot_okay("");
746}
747
748void cmd_continue(const char *arg, void *data, unsigned sz)
749{
750 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800751 target_battery_charging_enable(0, 1);
Dima Zavin214cc642009-01-26 11:16:21 -0800752 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700753 if (target_is_emmc_boot())
754 {
755 boot_linux_from_mmc();
756 }
757 else
758 {
759 boot_linux_from_flash();
760 }
Dima Zavin214cc642009-01-26 11:16:21 -0800761}
762
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800763void cmd_reboot(const char *arg, void *data, unsigned sz)
764{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700765 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800766 fastboot_okay("");
767 reboot_device(0);
768}
769
770void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
771{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700772 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800773 fastboot_okay("");
774 reboot_device(FASTBOOT_MODE);
775}
776
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700777void splash_screen ()
778{
779 struct ptentry *ptn;
780 struct ptable *ptable;
781 struct fbcon_config *fb_display = NULL;
782
783 if (!target_is_emmc_boot())
784 {
785 ptable = flash_get_ptable();
786 if (ptable == NULL) {
787 dprintf(CRITICAL, "ERROR: Partition table not found\n");
788 return -1;
789 }
790
791 ptn = ptable_find(ptable, "splash");
792 if (ptn == NULL) {
793 dprintf(CRITICAL, "ERROR: No splash partition found\n");
794 } else {
795 fb_display = fbcon_display();
796 if (fb_display) {
797 if (flash_read(ptn, 0, fb_display->base,
798 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
799 fbcon_clear();
800 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
801 }
802 }
803 }
804 }
805}
806
Brian Swetland9c4c0752009-01-25 16:23:50 -0800807void aboot_init(const struct app_descriptor *app)
808{
Shashank Mittal4f99a882010-02-01 13:58:50 -0800809 unsigned reboot_mode = 0;
810 unsigned disp_init = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700811 unsigned usb_init = 0;
Vivek Mehta5f1c9d42011-04-01 20:11:59 -0700812 unsigned sz = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -0700813
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700814 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700815 if (target_is_emmc_boot())
816 {
817 page_size = 2048;
818 page_mask = page_size - 1;
819 }
820 else
821 {
822 page_size = flash_page_size();
823 page_mask = page_size - 1;
824 }
825
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700826 /* Display splash screen if enabled */
827 #if DISPLAY_SPLASH_SCREEN
828 display_init();
829 dprintf(INFO, "Diplay initialized\n");
830 disp_init = 1;
Chandan Uddaraju40b227d2010-08-03 19:25:41 -0700831 diplay_image_on_screen();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700832 #endif
833
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800834 target_serialno(sn_buf);
835 dprintf(INFO,"sn %s\n",sn_buf);
836 surf_udc_device.serialno = sn_buf;
837
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700838 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700839 if (keys_get_state(KEY_HOME) != 0)
840 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -0500841 if (keys_get_state(KEY_VOLUMEUP) != 0)
842 boot_into_recovery = 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700843 if (keys_get_state(KEY_BACK) != 0)
844 goto fastboot;
Wentao Xu153902c2010-12-20 16:20:52 -0500845 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700846 goto fastboot;
847
848 #if NO_KEYPAD_DRIVER
849 /* With no keypad implementation, check the status of USB connection. */
850 /* If USB is connected then go into fastboot mode. */
851 usb_init = 1;
852 udc_init(&surf_udc_device);
853 if (usb_cable_status())
854 goto fastboot;
855 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -0700856
Ajay Dudani77421292010-10-27 19:34:06 -0700857 reboot_mode = check_reboot_mode();
858 if (reboot_mode == RECOVERY_MODE) {
859 boot_into_recovery = 1;
860 } else if(reboot_mode == FASTBOOT_MODE) {
861 goto fastboot;
862 }
863
Shashank Mittal23b8f422010-04-16 19:27:21 -0700864 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700865 {
866 boot_linux_from_mmc();
867 }
868 else
869 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700870 recovery_init();
871 boot_linux_from_flash();
872 }
Dima Zavinb4283602009-01-26 16:36:57 -0800873 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
874 "to fastboot mode.\n");
875
876fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700877
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700878 if(!usb_init)
879 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800880
881 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700882
Shashank Mittal23b8f422010-04-16 19:27:21 -0700883 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700884 {
885 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700886 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700887 }
888 else
889 {
890 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700891 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700892 }
893
894 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800895 fastboot_register("reboot", cmd_reboot);
896 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700897 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -0800898 fastboot_publish("kernel", "lk");
Vivek Mehta5f1c9d42011-04-01 20:11:59 -0700899 sz = target_get_max_flash_size();
900 fastboot_init(target_get_scratch_address(), sz);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800901 udc_start();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700902 target_battery_charging_enable(1, 0);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800903}
904
905APP_START(aboot)
906 .init = aboot_init,
907APP_END
908