blob: ccc88ada140867aef4176eed9f157d8f2dbc33ed [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Shashank Mittal4f99a882010-02-01 13:58:50 -08005 * Copyright (c) 2009-2010, 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>
Dima Zavin214cc642009-01-26 11:16:21 -080045
Shashank Mittal024c0332010-02-03 11:44:00 -080046#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080047#include "bootimg.h"
48#include "fastboot.h"
49
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070050#define EXPAND(NAME) #NAME
51#define TARGET(NAME) EXPAND(NAME)
Chandan Uddarajuda919832009-11-17 01:06:11 -080052#define DEFAULT_CMDLINE "mem=100M console=null";
Brian Swetland2defe162009-08-18 14:35:59 -070053
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080054#ifdef MEMBASE
55#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
56#else
David Ng183a7422009-12-07 14:55:21 -080057#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080058#endif
59
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080060#define RECOVERY_MODE 0x77665502
61#define FASTBOOT_MODE 0x77665500
62
David Ng183a7422009-12-07 14:55:21 -080063static const char *emmc_cmdline = " androidboot.emmc=true";
64
Brian Swetland9c4c0752009-01-25 16:23:50 -080065static struct udc_device surf_udc_device = {
66 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080067 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080068 .version_id = 0x0100,
69 .manufacturer = "Google",
70 .product = "Android",
71};
72
Dima Zavin42168f22009-01-30 11:52:22 -080073struct atag_ptbl_entry
74{
75 char name[16];
76 unsigned offset;
77 unsigned size;
78 unsigned flags;
79};
80
Brian Swetland9c4c0752009-01-25 16:23:50 -080081void platform_uninit_timer(void);
Chandan Uddarajuc6860e12009-11-19 11:22:15 -080082unsigned* target_atag_mem(unsigned* ptr);
Chandan Uddaraju885e4db2009-12-03 22:45:26 -080083unsigned board_machtype(void);
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080084unsigned check_reboot_mode(void);
David Ng3679bc52010-02-09 15:43:43 -080085void *target_get_scratch_address(void);
David Ng183a7422009-12-07 14:55:21 -080086int target_is_emmc_boot(void);
Chandan Uddaraju94183c02010-01-15 15:13:59 -080087void reboot_device(unsigned);
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -080088void target_battery_charging_enable(unsigned enable, unsigned disconnect);
Brian Swetland9c4c0752009-01-25 16:23:50 -080089
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080090
Dima Zavin42168f22009-01-30 11:52:22 -080091static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
92{
93 struct atag_ptbl_entry atag_ptn;
94
Shashank Mittal8e49dec2010-03-01 15:19:04 -080095 if (ptn->type == TYPE_MODEM_PARTITION)
96 return;
Dima Zavin42168f22009-01-30 11:52:22 -080097 memcpy(atag_ptn.name, ptn->name, 16);
98 atag_ptn.name[15] = '\0';
99 atag_ptn.offset = ptn->start;
100 atag_ptn.size = ptn->length;
101 atag_ptn.flags = ptn->flags;
102 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
103 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
104}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800105
106void boot_linux(void *kernel, unsigned *tags,
107 const char *cmdline, unsigned machtype,
108 void *ramdisk, unsigned ramdisk_size)
109{
110 unsigned *ptr = tags;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800111 unsigned pcount = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800112 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800113 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800114 int cmdline_len = 0;
115 int have_cmdline = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800116
117 /* CORE */
118 *ptr++ = 2;
119 *ptr++ = 0x54410001;
120
121 if (ramdisk_size) {
122 *ptr++ = 4;
123 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800124 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800125 *ptr++ = ramdisk_size;
126 }
127
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800128 ptr = target_atag_mem(ptr);
129
David Ng183a7422009-12-07 14:55:21 -0800130 if (!target_is_emmc_boot()) {
131 /* Skip NAND partition ATAGS for eMMC boot */
132 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
133 int i;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800134 for(i=0; i < ptable->count; i++) {
135 struct ptentry *ptn;
136 ptn = ptable_get(ptable, i);
137 if (ptn->type == TYPE_APPS_PARTITION)
138 pcount++;
139 }
140 *ptr++ = 2 + (pcount * (sizeof(struct atag_ptbl_entry) /
David Ng183a7422009-12-07 14:55:21 -0800141 sizeof(unsigned)));
142 *ptr++ = 0x4d534d70;
143 for (i = 0; i < ptable->count; ++i)
144 ptentry_to_tag(&ptr, ptable_get(ptable, i));
145 }
Dima Zavin42168f22009-01-30 11:52:22 -0800146 }
147
Brian Swetland9c4c0752009-01-25 16:23:50 -0800148 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800149 cmdline_len = strlen(cmdline);
150 have_cmdline = 1;
151 }
152 if (target_is_emmc_boot()) {
153 cmdline_len += strlen(emmc_cmdline);
154 }
155 if (cmdline_len > 0) {
156 const char *src;
157 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800158 unsigned n;
159 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800160 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800161 *ptr++ = (n / 4) + 2;
162 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800163 dst = (char *)ptr;
164 if (have_cmdline) {
165 src = cmdline;
166 while ((*dst++ = *src++));
167 }
168 if (target_is_emmc_boot()) {
169 src = emmc_cmdline;
170 if (have_cmdline) --dst;
171 while ((*dst++ = *src++));
172 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800173 ptr += (n / 4);
174 }
175
176 /* END */
177 *ptr++ = 0;
178 *ptr++ = 0;
179
180 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
181 kernel, ramdisk, ramdisk_size);
182 if (cmdline)
183 dprintf(INFO, "cmdline: %s\n", cmdline);
184
185 enter_critical_section();
186 platform_uninit_timer();
187 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800188 arch_disable_mmu();
189
Shashank Mittal23b8f422010-04-16 19:27:21 -0700190 secondary_core((unsigned)kernel);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800191 entry(0, machtype, tags);
192
193}
194
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800195unsigned page_size = 0;
196unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800197
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800198#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800199
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800200static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800201
Shashank Mittal23b8f422010-04-16 19:27:21 -0700202int boot_linux_from_mmc(void)
203{
204 struct boot_img_hdr *hdr = (void*) buf;
205 struct boot_img_hdr *uhdr;
206 unsigned offset = 0;
207 unsigned long long ptn = 0;
208 unsigned n = 0;
209 const char *cmdline;
210
211 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
212 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
213 dprintf(INFO, "Unified boot method!\n");
214 hdr = uhdr;
215 goto unified_boot;
216 }
217
218 ptn = mmc_ptn_offset("boot");
219 if(ptn == 0) {
220 dprintf(CRITICAL, "ERROR: No boot partition found\n");
221 return -1;
222 }
223
224 if (mmc_read(ptn + offset, (unsigned int *)buf, page_size)) {
225 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
226 return -1;
227 }
228 offset += page_size;
229
230 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
231 dprintf(CRITICAL, "ERROR: Invaled boot image header\n");
232 return -1;
233 }
234
235 if (hdr->page_size != page_size) {
236 dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
237 return -1;
238 }
239
240 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
241 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
242 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
243 return -1;
244 }
245 offset += n;
246
247 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
248 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
249 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
250 return -1;
251 }
252 offset += n;
253
254unified_boot:
255 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
256 hdr->kernel_size);
257 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
258 hdr->ramdisk_size);
259
260 if(hdr->cmdline[0]) {
261 cmdline = (char*) hdr->cmdline;
262 } else {
263 cmdline = DEFAULT_CMDLINE;
264 }
265 dprintf(INFO, "cmdline = '%s'\n", cmdline);
266
267 dprintf(INFO, "\nBooting Linux\n");
268 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
269 (const char *)cmdline, board_machtype(),
270 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
271
272 return 0;
273}
274
Dima Zavin214cc642009-01-26 11:16:21 -0800275int boot_linux_from_flash(void)
276{
277 struct boot_img_hdr *hdr = (void*) buf;
278 unsigned n;
279 struct ptentry *ptn;
280 struct ptable *ptable;
281 unsigned offset = 0;
282 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800283
David Ng183a7422009-12-07 14:55:21 -0800284 if (target_is_emmc_boot()) {
285 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
286 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
287 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
288 return -1;
289 }
290 goto continue_boot;
291 }
292
Dima Zavin214cc642009-01-26 11:16:21 -0800293 ptable = flash_get_ptable();
294 if (ptable == NULL) {
295 dprintf(CRITICAL, "ERROR: Partition table not found\n");
296 return -1;
297 }
298
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800299 if(!boot_into_recovery)
300 {
301 ptn = ptable_find(ptable, "boot");
302 if (ptn == NULL) {
303 dprintf(CRITICAL, "ERROR: No boot partition found\n");
304 return -1;
305 }
306 }
307 else
308 {
309 ptn = ptable_find(ptable, "recovery");
310 if (ptn == NULL) {
311 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
312 return -1;
313 }
Dima Zavin214cc642009-01-26 11:16:21 -0800314 }
315
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800316 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800317 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
318 return -1;
319 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800320 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800321
322 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
323 dprintf(CRITICAL, "ERROR: Invaled boot image heador\n");
324 return -1;
325 }
326
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800327 if (hdr->page_size != page_size) {
328 dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
329 return -1;
330 }
331
332 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800333 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
334 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
335 return -1;
336 }
337 offset += n;
338
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800339 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800340 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
341 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
342 return -1;
343 }
344 offset += n;
345
David Ng183a7422009-12-07 14:55:21 -0800346continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800347 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
348 hdr->kernel_size);
349 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
350 hdr->ramdisk_size);
351
352 if(hdr->cmdline[0]) {
353 cmdline = (char*) hdr->cmdline;
354 } else {
355 cmdline = DEFAULT_CMDLINE;
356 }
357 dprintf(INFO, "cmdline = '%s'\n", cmdline);
358
359 /* TODO: create/pass atags to kernel */
360
361 dprintf(INFO, "\nBooting Linux\n");
362 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800363 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800364 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
365
366 return 0;
367}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800368
369void cmd_boot(const char *arg, void *data, unsigned sz)
370{
371 unsigned kernel_actual;
372 unsigned ramdisk_actual;
373 static struct boot_img_hdr hdr;
374 char *ptr = ((char*) data);
375
376 if (sz < sizeof(hdr)) {
377 fastboot_fail("invalid bootimage header");
378 return;
379 }
380
381 memcpy(&hdr, data, sizeof(hdr));
382
383 /* ensure commandline is terminated */
384 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
385
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800386 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
387 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800388
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700389 if (page_size + kernel_actual + ramdisk_actual < sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800390 fastboot_fail("incomplete bootimage");
391 return;
392 }
393
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700394 memmove((void*) KERNEL_ADDR, ptr + page_size, hdr.kernel_size);
395 memmove((void*) RAMDISK_ADDR, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800396
397 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800398 target_battery_charging_enable(0, 1);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800399 udc_stop();
400
Brian Swetland9c4c0752009-01-25 16:23:50 -0800401 boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800402 (const char*) hdr.cmdline, board_machtype(),
Brian Swetland9c4c0752009-01-25 16:23:50 -0800403 (void*) RAMDISK_ADDR, hdr.ramdisk_size);
404}
405
Dima Zavin214cc642009-01-26 11:16:21 -0800406void cmd_erase(const char *arg, void *data, unsigned sz)
407{
408 struct ptentry *ptn;
409 struct ptable *ptable;
410
411 ptable = flash_get_ptable();
412 if (ptable == NULL) {
413 fastboot_fail("partition table doesn't exist");
414 return;
415 }
416
417 ptn = ptable_find(ptable, arg);
418 if (ptn == NULL) {
419 fastboot_fail("unknown partition name");
420 return;
421 }
422
423 if (flash_erase(ptn)) {
424 fastboot_fail("failed to erase partition");
425 return;
426 }
427 fastboot_okay("");
428}
429
Shashank Mittal23b8f422010-04-16 19:27:21 -0700430void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
431{
432 unsigned long long ptn = 0;
433 ptn = mmc_ptn_offset(arg);
434 if(ptn == 0) {
435 fastboot_fail("partition table doesn't exist");
436 return;
437 }
438
439 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
440 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
441 fastboot_fail("image is not a boot image");
442 return;
443 }
444 }
445
446 if (mmc_write(ptn , sz, (unsigned int *)data)) {
447 fastboot_fail("flash write failure");
448 return;
449 }
450 fastboot_okay("");
451 return;
452}
453
Dima Zavin214cc642009-01-26 11:16:21 -0800454void cmd_flash(const char *arg, void *data, unsigned sz)
455{
456 struct ptentry *ptn;
457 struct ptable *ptable;
458 unsigned extra = 0;
459
460 ptable = flash_get_ptable();
461 if (ptable == NULL) {
462 fastboot_fail("partition table doesn't exist");
463 return;
464 }
465
466 ptn = ptable_find(ptable, arg);
467 if (ptn == NULL) {
468 fastboot_fail("unknown partition name");
469 return;
470 }
471
472 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
473 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
474 fastboot_fail("image is not a boot image");
475 return;
476 }
477 }
478
Chandan Uddarajud6d45042010-02-24 21:12:45 -0800479 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")
480 || !strcmp(ptn->name, "persist"))
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800481 extra = ((page_size >> 9) * 16);
Dima Zavin214cc642009-01-26 11:16:21 -0800482 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800483 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800484
485 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
486 if (flash_write(ptn, extra, data, sz)) {
487 fastboot_fail("flash write failure");
488 return;
489 }
490 dprintf(INFO, "partition '%s' updated\n", ptn->name);
491 fastboot_okay("");
492}
493
494void cmd_continue(const char *arg, void *data, unsigned sz)
495{
496 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800497 target_battery_charging_enable(0, 1);
Dima Zavin214cc642009-01-26 11:16:21 -0800498 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700499 if (target_is_emmc_boot())
500 {
501 boot_linux_from_mmc();
502 }
503 else
504 {
505 boot_linux_from_flash();
506 }
Dima Zavin214cc642009-01-26 11:16:21 -0800507}
508
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800509void cmd_reboot(const char *arg, void *data, unsigned sz)
510{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700511 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800512 fastboot_okay("");
513 reboot_device(0);
514}
515
516void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
517{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700518 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800519 fastboot_okay("");
520 reboot_device(FASTBOOT_MODE);
521}
522
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700523void splash_screen ()
524{
525 struct ptentry *ptn;
526 struct ptable *ptable;
527 struct fbcon_config *fb_display = NULL;
528
529 if (!target_is_emmc_boot())
530 {
531 ptable = flash_get_ptable();
532 if (ptable == NULL) {
533 dprintf(CRITICAL, "ERROR: Partition table not found\n");
534 return -1;
535 }
536
537 ptn = ptable_find(ptable, "splash");
538 if (ptn == NULL) {
539 dprintf(CRITICAL, "ERROR: No splash partition found\n");
540 } else {
541 fb_display = fbcon_display();
542 if (fb_display) {
543 if (flash_read(ptn, 0, fb_display->base,
544 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
545 fbcon_clear();
546 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
547 }
548 }
549 }
550 }
551}
552
Brian Swetland9c4c0752009-01-25 16:23:50 -0800553void aboot_init(const struct app_descriptor *app)
554{
Shashank Mittal4f99a882010-02-01 13:58:50 -0800555 unsigned reboot_mode = 0;
556 unsigned disp_init = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700557 unsigned usb_init = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -0700558
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700559 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700560 if (target_is_emmc_boot())
561 {
562 page_size = 2048;
563 page_mask = page_size - 1;
564 }
565 else
566 {
567 page_size = flash_page_size();
568 page_mask = page_size - 1;
569 }
570
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700571 /* Display splash screen if enabled */
572 #if DISPLAY_SPLASH_SCREEN
573 display_init();
574 dprintf(INFO, "Diplay initialized\n");
575 disp_init = 1;
576 splash_screen();
577 #endif
578
579 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700580 if (keys_get_state(KEY_HOME) != 0)
581 boot_into_recovery = 1;
582 if (keys_get_state(KEY_BACK) != 0)
583 goto fastboot;
584 if (keys_get_state(KEY_CLEAR) != 0)
585 goto fastboot;
586
587 #if NO_KEYPAD_DRIVER
588 /* With no keypad implementation, check the status of USB connection. */
589 /* If USB is connected then go into fastboot mode. */
590 usb_init = 1;
591 udc_init(&surf_udc_device);
592 if (usb_cable_status())
593 goto fastboot;
594 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -0700595
Shashank Mittal23b8f422010-04-16 19:27:21 -0700596 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700597 {
598 boot_linux_from_mmc();
599 }
600 else
601 {
602 reboot_mode = check_reboot_mode();
603 if (reboot_mode == RECOVERY_MODE) {
604 boot_into_recovery = 1;
605 } else if(reboot_mode == FASTBOOT_MODE) {
606 goto fastboot;
607 }
608 recovery_init();
609 boot_linux_from_flash();
610 }
Dima Zavinb4283602009-01-26 16:36:57 -0800611 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
612 "to fastboot mode.\n");
613
614fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700615
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700616 if(!usb_init)
617 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800618
619 fastboot_register("boot", cmd_boot);
Dima Zavin214cc642009-01-26 11:16:21 -0800620 fastboot_register("erase:", cmd_erase);
Shashank Mittal23b8f422010-04-16 19:27:21 -0700621 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700622 {
623 fastboot_register("flash:", cmd_flash_mmc);
624 }
625 else
626 {
627 fastboot_register("flash:", cmd_flash);
628 }
629
630 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800631 fastboot_register("reboot", cmd_reboot);
632 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700633 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -0800634 fastboot_publish("kernel", "lk");
635
Ajay Dudanid43419e2010-02-10 13:17:39 -0800636 fastboot_init(target_get_scratch_address(), 120 * 1024 * 1024);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800637 udc_start();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700638 target_battery_charging_enable(1, 0);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800639}
640
641APP_START(aboot)
642 .init = aboot_init,
643APP_END
644