blob: fb80f3814e0d04df0a75d5fca545233264ed0935 [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
Chandan Uddarajuda919832009-11-17 01:06:11 -080050#define DEFAULT_CMDLINE "mem=100M console=null";
Brian Swetland2defe162009-08-18 14:35:59 -070051
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080052#ifdef MEMBASE
53#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
54#else
David Ng183a7422009-12-07 14:55:21 -080055#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080056#endif
57
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080058#define RECOVERY_MODE 0x77665502
59#define FASTBOOT_MODE 0x77665500
60
David Ng183a7422009-12-07 14:55:21 -080061static const char *emmc_cmdline = " androidboot.emmc=true";
62
Brian Swetland9c4c0752009-01-25 16:23:50 -080063static struct udc_device surf_udc_device = {
64 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080065 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080066 .version_id = 0x0100,
67 .manufacturer = "Google",
68 .product = "Android",
69};
70
Dima Zavin42168f22009-01-30 11:52:22 -080071struct atag_ptbl_entry
72{
73 char name[16];
74 unsigned offset;
75 unsigned size;
76 unsigned flags;
77};
78
Brian Swetland9c4c0752009-01-25 16:23:50 -080079void platform_uninit_timer(void);
Chandan Uddarajuc6860e12009-11-19 11:22:15 -080080unsigned* target_atag_mem(unsigned* ptr);
Chandan Uddaraju885e4db2009-12-03 22:45:26 -080081unsigned board_machtype(void);
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080082unsigned check_reboot_mode(void);
David Ng3679bc52010-02-09 15:43:43 -080083void *target_get_scratch_address(void);
David Ng183a7422009-12-07 14:55:21 -080084int target_is_emmc_boot(void);
Chandan Uddaraju94183c02010-01-15 15:13:59 -080085void reboot_device(unsigned);
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -080086void target_battery_charging_enable(unsigned enable, unsigned disconnect);
Brian Swetland9c4c0752009-01-25 16:23:50 -080087
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080088
Dima Zavin42168f22009-01-30 11:52:22 -080089static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
90{
91 struct atag_ptbl_entry atag_ptn;
92
Shashank Mittal8e49dec2010-03-01 15:19:04 -080093 if (ptn->type == TYPE_MODEM_PARTITION)
94 return;
Dima Zavin42168f22009-01-30 11:52:22 -080095 memcpy(atag_ptn.name, ptn->name, 16);
96 atag_ptn.name[15] = '\0';
97 atag_ptn.offset = ptn->start;
98 atag_ptn.size = ptn->length;
99 atag_ptn.flags = ptn->flags;
100 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
101 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
102}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800103
104void boot_linux(void *kernel, unsigned *tags,
105 const char *cmdline, unsigned machtype,
106 void *ramdisk, unsigned ramdisk_size)
107{
108 unsigned *ptr = tags;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800109 unsigned pcount = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800110 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800111 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800112 int cmdline_len = 0;
113 int have_cmdline = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800114
115 /* CORE */
116 *ptr++ = 2;
117 *ptr++ = 0x54410001;
118
119 if (ramdisk_size) {
120 *ptr++ = 4;
121 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800122 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800123 *ptr++ = ramdisk_size;
124 }
125
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800126 ptr = target_atag_mem(ptr);
127
David Ng183a7422009-12-07 14:55:21 -0800128 if (!target_is_emmc_boot()) {
129 /* Skip NAND partition ATAGS for eMMC boot */
130 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
131 int i;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800132 for(i=0; i < ptable->count; i++) {
133 struct ptentry *ptn;
134 ptn = ptable_get(ptable, i);
135 if (ptn->type == TYPE_APPS_PARTITION)
136 pcount++;
137 }
138 *ptr++ = 2 + (pcount * (sizeof(struct atag_ptbl_entry) /
David Ng183a7422009-12-07 14:55:21 -0800139 sizeof(unsigned)));
140 *ptr++ = 0x4d534d70;
141 for (i = 0; i < ptable->count; ++i)
142 ptentry_to_tag(&ptr, ptable_get(ptable, i));
143 }
Dima Zavin42168f22009-01-30 11:52:22 -0800144 }
145
Brian Swetland9c4c0752009-01-25 16:23:50 -0800146 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800147 cmdline_len = strlen(cmdline);
148 have_cmdline = 1;
149 }
150 if (target_is_emmc_boot()) {
151 cmdline_len += strlen(emmc_cmdline);
152 }
153 if (cmdline_len > 0) {
154 const char *src;
155 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800156 unsigned n;
157 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800158 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800159 *ptr++ = (n / 4) + 2;
160 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800161 dst = (char *)ptr;
162 if (have_cmdline) {
163 src = cmdline;
164 while ((*dst++ = *src++));
165 }
166 if (target_is_emmc_boot()) {
167 src = emmc_cmdline;
168 if (have_cmdline) --dst;
169 while ((*dst++ = *src++));
170 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800171 ptr += (n / 4);
172 }
173
174 /* END */
175 *ptr++ = 0;
176 *ptr++ = 0;
177
178 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
179 kernel, ramdisk, ramdisk_size);
180 if (cmdline)
181 dprintf(INFO, "cmdline: %s\n", cmdline);
182
183 enter_critical_section();
184 platform_uninit_timer();
185 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800186 arch_disable_mmu();
187
Shashank Mittal23b8f422010-04-16 19:27:21 -0700188 secondary_core((unsigned)kernel);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800189 entry(0, machtype, tags);
190
191}
192
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800193unsigned page_size = 0;
194unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800195
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800196#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800197
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800198static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800199
Shashank Mittal23b8f422010-04-16 19:27:21 -0700200int boot_linux_from_mmc(void)
201{
202 struct boot_img_hdr *hdr = (void*) buf;
203 struct boot_img_hdr *uhdr;
204 unsigned offset = 0;
205 unsigned long long ptn = 0;
206 unsigned n = 0;
207 const char *cmdline;
208
209 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
210 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
211 dprintf(INFO, "Unified boot method!\n");
212 hdr = uhdr;
213 goto unified_boot;
214 }
215
216 ptn = mmc_ptn_offset("boot");
217 if(ptn == 0) {
218 dprintf(CRITICAL, "ERROR: No boot partition found\n");
219 return -1;
220 }
221
222 if (mmc_read(ptn + offset, (unsigned int *)buf, page_size)) {
223 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
224 return -1;
225 }
226 offset += page_size;
227
228 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
229 dprintf(CRITICAL, "ERROR: Invaled boot image header\n");
230 return -1;
231 }
232
233 if (hdr->page_size != page_size) {
234 dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
235 return -1;
236 }
237
238 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
239 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
240 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
241 return -1;
242 }
243 offset += n;
244
245 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
246 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
247 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
248 return -1;
249 }
250 offset += n;
251
252unified_boot:
253 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
254 hdr->kernel_size);
255 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
256 hdr->ramdisk_size);
257
258 if(hdr->cmdline[0]) {
259 cmdline = (char*) hdr->cmdline;
260 } else {
261 cmdline = DEFAULT_CMDLINE;
262 }
263 dprintf(INFO, "cmdline = '%s'\n", cmdline);
264
265 dprintf(INFO, "\nBooting Linux\n");
266 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
267 (const char *)cmdline, board_machtype(),
268 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
269
270 return 0;
271}
272
Dima Zavin214cc642009-01-26 11:16:21 -0800273int boot_linux_from_flash(void)
274{
275 struct boot_img_hdr *hdr = (void*) buf;
276 unsigned n;
277 struct ptentry *ptn;
278 struct ptable *ptable;
279 unsigned offset = 0;
280 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800281
David Ng183a7422009-12-07 14:55:21 -0800282 if (target_is_emmc_boot()) {
283 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
284 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
285 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
286 return -1;
287 }
288 goto continue_boot;
289 }
290
Dima Zavin214cc642009-01-26 11:16:21 -0800291 ptable = flash_get_ptable();
292 if (ptable == NULL) {
293 dprintf(CRITICAL, "ERROR: Partition table not found\n");
294 return -1;
295 }
296
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800297 if(!boot_into_recovery)
298 {
299 ptn = ptable_find(ptable, "boot");
300 if (ptn == NULL) {
301 dprintf(CRITICAL, "ERROR: No boot partition found\n");
302 return -1;
303 }
304 }
305 else
306 {
307 ptn = ptable_find(ptable, "recovery");
308 if (ptn == NULL) {
309 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
310 return -1;
311 }
Dima Zavin214cc642009-01-26 11:16:21 -0800312 }
313
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800314 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800315 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
316 return -1;
317 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800318 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800319
320 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
321 dprintf(CRITICAL, "ERROR: Invaled boot image heador\n");
322 return -1;
323 }
324
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800325 if (hdr->page_size != page_size) {
326 dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
327 return -1;
328 }
329
330 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800331 if (flash_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
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800337 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800338 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
339 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
340 return -1;
341 }
342 offset += n;
343
David Ng183a7422009-12-07 14:55:21 -0800344continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800345 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
346 hdr->kernel_size);
347 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
348 hdr->ramdisk_size);
349
350 if(hdr->cmdline[0]) {
351 cmdline = (char*) hdr->cmdline;
352 } else {
353 cmdline = DEFAULT_CMDLINE;
354 }
355 dprintf(INFO, "cmdline = '%s'\n", cmdline);
356
357 /* TODO: create/pass atags to kernel */
358
359 dprintf(INFO, "\nBooting Linux\n");
360 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800361 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800362 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
363
364 return 0;
365}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800366
367void cmd_boot(const char *arg, void *data, unsigned sz)
368{
369 unsigned kernel_actual;
370 unsigned ramdisk_actual;
371 static struct boot_img_hdr hdr;
372 char *ptr = ((char*) data);
373
374 if (sz < sizeof(hdr)) {
375 fastboot_fail("invalid bootimage header");
376 return;
377 }
378
379 memcpy(&hdr, data, sizeof(hdr));
380
381 /* ensure commandline is terminated */
382 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
383
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800384 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
385 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800386
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700387 if (page_size + kernel_actual + ramdisk_actual < sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800388 fastboot_fail("incomplete bootimage");
389 return;
390 }
391
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700392 memmove((void*) KERNEL_ADDR, ptr + page_size, hdr.kernel_size);
393 memmove((void*) RAMDISK_ADDR, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800394
395 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800396 target_battery_charging_enable(0, 1);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800397 udc_stop();
398
Brian Swetland9c4c0752009-01-25 16:23:50 -0800399 boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800400 (const char*) hdr.cmdline, board_machtype(),
Brian Swetland9c4c0752009-01-25 16:23:50 -0800401 (void*) RAMDISK_ADDR, hdr.ramdisk_size);
402}
403
Dima Zavin214cc642009-01-26 11:16:21 -0800404void cmd_erase(const char *arg, void *data, unsigned sz)
405{
406 struct ptentry *ptn;
407 struct ptable *ptable;
408
409 ptable = flash_get_ptable();
410 if (ptable == NULL) {
411 fastboot_fail("partition table doesn't exist");
412 return;
413 }
414
415 ptn = ptable_find(ptable, arg);
416 if (ptn == NULL) {
417 fastboot_fail("unknown partition name");
418 return;
419 }
420
421 if (flash_erase(ptn)) {
422 fastboot_fail("failed to erase partition");
423 return;
424 }
425 fastboot_okay("");
426}
427
Shashank Mittal23b8f422010-04-16 19:27:21 -0700428void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
429{
430 unsigned long long ptn = 0;
431 ptn = mmc_ptn_offset(arg);
432 if(ptn == 0) {
433 fastboot_fail("partition table doesn't exist");
434 return;
435 }
436
437 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
438 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
439 fastboot_fail("image is not a boot image");
440 return;
441 }
442 }
443
444 if (mmc_write(ptn , sz, (unsigned int *)data)) {
445 fastboot_fail("flash write failure");
446 return;
447 }
448 fastboot_okay("");
449 return;
450}
451
Dima Zavin214cc642009-01-26 11:16:21 -0800452void cmd_flash(const char *arg, void *data, unsigned sz)
453{
454 struct ptentry *ptn;
455 struct ptable *ptable;
456 unsigned extra = 0;
457
458 ptable = flash_get_ptable();
459 if (ptable == NULL) {
460 fastboot_fail("partition table doesn't exist");
461 return;
462 }
463
464 ptn = ptable_find(ptable, arg);
465 if (ptn == NULL) {
466 fastboot_fail("unknown partition name");
467 return;
468 }
469
470 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
471 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
472 fastboot_fail("image is not a boot image");
473 return;
474 }
475 }
476
Chandan Uddarajud6d45042010-02-24 21:12:45 -0800477 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")
478 || !strcmp(ptn->name, "persist"))
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800479 extra = ((page_size >> 9) * 16);
Dima Zavin214cc642009-01-26 11:16:21 -0800480 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800481 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800482
483 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
484 if (flash_write(ptn, extra, data, sz)) {
485 fastboot_fail("flash write failure");
486 return;
487 }
488 dprintf(INFO, "partition '%s' updated\n", ptn->name);
489 fastboot_okay("");
490}
491
492void cmd_continue(const char *arg, void *data, unsigned sz)
493{
494 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800495 target_battery_charging_enable(0, 1);
Dima Zavin214cc642009-01-26 11:16:21 -0800496 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700497 if (target_is_emmc_boot())
498 {
499 boot_linux_from_mmc();
500 }
501 else
502 {
503 boot_linux_from_flash();
504 }
Dima Zavin214cc642009-01-26 11:16:21 -0800505}
506
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800507void cmd_reboot(const char *arg, void *data, unsigned sz)
508{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700509 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800510 fastboot_okay("");
511 reboot_device(0);
512}
513
514void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
515{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700516 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800517 fastboot_okay("");
518 reboot_device(FASTBOOT_MODE);
519}
520
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700521void splash_screen ()
522{
523 struct ptentry *ptn;
524 struct ptable *ptable;
525 struct fbcon_config *fb_display = NULL;
526
527 if (!target_is_emmc_boot())
528 {
529 ptable = flash_get_ptable();
530 if (ptable == NULL) {
531 dprintf(CRITICAL, "ERROR: Partition table not found\n");
532 return -1;
533 }
534
535 ptn = ptable_find(ptable, "splash");
536 if (ptn == NULL) {
537 dprintf(CRITICAL, "ERROR: No splash partition found\n");
538 } else {
539 fb_display = fbcon_display();
540 if (fb_display) {
541 if (flash_read(ptn, 0, fb_display->base,
542 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
543 fbcon_clear();
544 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
545 }
546 }
547 }
548 }
549}
550
Brian Swetland9c4c0752009-01-25 16:23:50 -0800551void aboot_init(const struct app_descriptor *app)
552{
Shashank Mittal4f99a882010-02-01 13:58:50 -0800553 unsigned reboot_mode = 0;
554 unsigned disp_init = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700555 unsigned usb_init = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -0700556
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700557 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700558 if (target_is_emmc_boot())
559 {
560 page_size = 2048;
561 page_mask = page_size - 1;
562 }
563 else
564 {
565 page_size = flash_page_size();
566 page_mask = page_size - 1;
567 }
568
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700569 /* Display splash screen if enabled */
570 #if DISPLAY_SPLASH_SCREEN
571 display_init();
572 dprintf(INFO, "Diplay initialized\n");
573 disp_init = 1;
574 splash_screen();
575 #endif
576
577 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700578 if (keys_get_state(KEY_HOME) != 0)
579 boot_into_recovery = 1;
580 if (keys_get_state(KEY_BACK) != 0)
581 goto fastboot;
582 if (keys_get_state(KEY_CLEAR) != 0)
583 goto fastboot;
584
585 #if NO_KEYPAD_DRIVER
586 /* With no keypad implementation, check the status of USB connection. */
587 /* If USB is connected then go into fastboot mode. */
588 usb_init = 1;
589 udc_init(&surf_udc_device);
590 if (usb_cable_status())
591 goto fastboot;
592 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -0700593
Shashank Mittal23b8f422010-04-16 19:27:21 -0700594 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700595 {
596 boot_linux_from_mmc();
597 }
598 else
599 {
600 reboot_mode = check_reboot_mode();
601 if (reboot_mode == RECOVERY_MODE) {
602 boot_into_recovery = 1;
603 } else if(reboot_mode == FASTBOOT_MODE) {
604 goto fastboot;
605 }
606 recovery_init();
607 boot_linux_from_flash();
608 }
Dima Zavinb4283602009-01-26 16:36:57 -0800609 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
610 "to fastboot mode.\n");
611
612fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700613
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700614 if(!usb_init)
615 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800616
617 fastboot_register("boot", cmd_boot);
Dima Zavin214cc642009-01-26 11:16:21 -0800618 fastboot_register("erase:", cmd_erase);
Shashank Mittal23b8f422010-04-16 19:27:21 -0700619 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700620 {
621 fastboot_register("flash:", cmd_flash_mmc);
622 }
623 else
624 {
625 fastboot_register("flash:", cmd_flash);
626 }
627
628 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800629 fastboot_register("reboot", cmd_reboot);
630 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800631 fastboot_publish("product", "swordfish");
632 fastboot_publish("kernel", "lk");
633
Ajay Dudanid43419e2010-02-10 13:17:39 -0800634 fastboot_init(target_get_scratch_address(), 120 * 1024 * 1024);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800635 udc_start();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700636 target_battery_charging_enable(1, 0);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800637}
638
639APP_START(aboot)
640 .init = aboot_init,
641APP_END
642