blob: 06e0b9a260282d3c7ad2f1e57da1a8e9cbdc31aa [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";
David Ngf773dde2010-07-26 19:55:08 -070064static const char *battchg_pause = " androidboot.battchg_pause=true";
David Ng183a7422009-12-07 14:55:21 -080065
Brian Swetland9c4c0752009-01-25 16:23:50 -080066static struct udc_device surf_udc_device = {
67 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080068 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080069 .version_id = 0x0100,
70 .manufacturer = "Google",
71 .product = "Android",
72};
73
Dima Zavin42168f22009-01-30 11:52:22 -080074struct atag_ptbl_entry
75{
76 char name[16];
77 unsigned offset;
78 unsigned size;
79 unsigned flags;
80};
81
Brian Swetland9c4c0752009-01-25 16:23:50 -080082void platform_uninit_timer(void);
Chandan Uddarajuc6860e12009-11-19 11:22:15 -080083unsigned* target_atag_mem(unsigned* ptr);
Chandan Uddaraju885e4db2009-12-03 22:45:26 -080084unsigned board_machtype(void);
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080085unsigned check_reboot_mode(void);
David Ng3679bc52010-02-09 15:43:43 -080086void *target_get_scratch_address(void);
David Ng183a7422009-12-07 14:55:21 -080087int target_is_emmc_boot(void);
Chandan Uddaraju94183c02010-01-15 15:13:59 -080088void reboot_device(unsigned);
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -080089void target_battery_charging_enable(unsigned enable, unsigned disconnect);
Brian Swetland9c4c0752009-01-25 16:23:50 -080090
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080091
Dima Zavin42168f22009-01-30 11:52:22 -080092static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
93{
94 struct atag_ptbl_entry atag_ptn;
95
Shashank Mittal8e49dec2010-03-01 15:19:04 -080096 if (ptn->type == TYPE_MODEM_PARTITION)
97 return;
Dima Zavin42168f22009-01-30 11:52:22 -080098 memcpy(atag_ptn.name, ptn->name, 16);
99 atag_ptn.name[15] = '\0';
100 atag_ptn.offset = ptn->start;
101 atag_ptn.size = ptn->length;
102 atag_ptn.flags = ptn->flags;
103 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
104 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
105}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800106
107void boot_linux(void *kernel, unsigned *tags,
108 const char *cmdline, unsigned machtype,
109 void *ramdisk, unsigned ramdisk_size)
110{
111 unsigned *ptr = tags;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800112 unsigned pcount = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800113 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800114 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800115 int cmdline_len = 0;
116 int have_cmdline = 0;
David Ngf773dde2010-07-26 19:55:08 -0700117 int pause_at_bootup = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800118
119 /* CORE */
120 *ptr++ = 2;
121 *ptr++ = 0x54410001;
122
123 if (ramdisk_size) {
124 *ptr++ = 4;
125 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800126 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800127 *ptr++ = ramdisk_size;
128 }
129
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800130 ptr = target_atag_mem(ptr);
131
David Ng183a7422009-12-07 14:55:21 -0800132 if (!target_is_emmc_boot()) {
133 /* Skip NAND partition ATAGS for eMMC boot */
134 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
135 int i;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800136 for(i=0; i < ptable->count; i++) {
137 struct ptentry *ptn;
138 ptn = ptable_get(ptable, i);
139 if (ptn->type == TYPE_APPS_PARTITION)
140 pcount++;
141 }
142 *ptr++ = 2 + (pcount * (sizeof(struct atag_ptbl_entry) /
David Ng183a7422009-12-07 14:55:21 -0800143 sizeof(unsigned)));
144 *ptr++ = 0x4d534d70;
145 for (i = 0; i < ptable->count; ++i)
146 ptentry_to_tag(&ptr, ptable_get(ptable, i));
147 }
Dima Zavin42168f22009-01-30 11:52:22 -0800148 }
149
Brian Swetland9c4c0752009-01-25 16:23:50 -0800150 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800151 cmdline_len = strlen(cmdline);
152 have_cmdline = 1;
153 }
154 if (target_is_emmc_boot()) {
155 cmdline_len += strlen(emmc_cmdline);
156 }
David Ngf773dde2010-07-26 19:55:08 -0700157 if (target_pause_for_battery_charge()) {
158 pause_at_bootup = 1;
159 cmdline_len += strlen(battchg_pause);
160 }
David Ng183a7422009-12-07 14:55:21 -0800161 if (cmdline_len > 0) {
162 const char *src;
163 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800164 unsigned n;
165 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800166 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800167 *ptr++ = (n / 4) + 2;
168 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800169 dst = (char *)ptr;
170 if (have_cmdline) {
171 src = cmdline;
172 while ((*dst++ = *src++));
173 }
174 if (target_is_emmc_boot()) {
175 src = emmc_cmdline;
176 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700177 have_cmdline = 1;
178 while ((*dst++ = *src++));
179 }
180 if (pause_at_bootup) {
181 src = battchg_pause;
182 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800183 while ((*dst++ = *src++));
184 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800185 ptr += (n / 4);
186 }
187
188 /* END */
189 *ptr++ = 0;
190 *ptr++ = 0;
191
192 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
193 kernel, ramdisk, ramdisk_size);
194 if (cmdline)
195 dprintf(INFO, "cmdline: %s\n", cmdline);
196
197 enter_critical_section();
198 platform_uninit_timer();
199 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800200 arch_disable_mmu();
201
Shashank Mittal23b8f422010-04-16 19:27:21 -0700202 secondary_core((unsigned)kernel);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800203 entry(0, machtype, tags);
204
205}
206
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800207unsigned page_size = 0;
208unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800209
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800210#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800211
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800212static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800213
Shashank Mittal23b8f422010-04-16 19:27:21 -0700214int boot_linux_from_mmc(void)
215{
216 struct boot_img_hdr *hdr = (void*) buf;
217 struct boot_img_hdr *uhdr;
218 unsigned offset = 0;
219 unsigned long long ptn = 0;
220 unsigned n = 0;
221 const char *cmdline;
222
223 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
224 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
225 dprintf(INFO, "Unified boot method!\n");
226 hdr = uhdr;
227 goto unified_boot;
228 }
229
230 ptn = mmc_ptn_offset("boot");
231 if(ptn == 0) {
232 dprintf(CRITICAL, "ERROR: No boot partition found\n");
233 return -1;
234 }
235
236 if (mmc_read(ptn + offset, (unsigned int *)buf, page_size)) {
237 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
238 return -1;
239 }
240 offset += page_size;
241
242 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
243 dprintf(CRITICAL, "ERROR: Invaled boot image header\n");
244 return -1;
245 }
246
247 if (hdr->page_size != page_size) {
248 dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
249 return -1;
250 }
251
252 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
253 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
254 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
255 return -1;
256 }
257 offset += n;
258
259 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
260 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
261 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
262 return -1;
263 }
264 offset += n;
265
266unified_boot:
267 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
268 hdr->kernel_size);
269 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
270 hdr->ramdisk_size);
271
272 if(hdr->cmdline[0]) {
273 cmdline = (char*) hdr->cmdline;
274 } else {
275 cmdline = DEFAULT_CMDLINE;
276 }
277 dprintf(INFO, "cmdline = '%s'\n", cmdline);
278
279 dprintf(INFO, "\nBooting Linux\n");
280 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
281 (const char *)cmdline, board_machtype(),
282 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
283
284 return 0;
285}
286
Dima Zavin214cc642009-01-26 11:16:21 -0800287int boot_linux_from_flash(void)
288{
289 struct boot_img_hdr *hdr = (void*) buf;
290 unsigned n;
291 struct ptentry *ptn;
292 struct ptable *ptable;
293 unsigned offset = 0;
294 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800295
David Ng183a7422009-12-07 14:55:21 -0800296 if (target_is_emmc_boot()) {
297 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
298 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
299 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
300 return -1;
301 }
302 goto continue_boot;
303 }
304
Dima Zavin214cc642009-01-26 11:16:21 -0800305 ptable = flash_get_ptable();
306 if (ptable == NULL) {
307 dprintf(CRITICAL, "ERROR: Partition table not found\n");
308 return -1;
309 }
310
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800311 if(!boot_into_recovery)
312 {
313 ptn = ptable_find(ptable, "boot");
314 if (ptn == NULL) {
315 dprintf(CRITICAL, "ERROR: No boot partition found\n");
316 return -1;
317 }
318 }
319 else
320 {
321 ptn = ptable_find(ptable, "recovery");
322 if (ptn == NULL) {
323 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
324 return -1;
325 }
Dima Zavin214cc642009-01-26 11:16:21 -0800326 }
327
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800328 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800329 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
330 return -1;
331 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800332 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800333
334 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
335 dprintf(CRITICAL, "ERROR: Invaled boot image heador\n");
336 return -1;
337 }
338
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800339 if (hdr->page_size != page_size) {
340 dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
341 return -1;
342 }
343
344 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800345 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
346 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
347 return -1;
348 }
349 offset += n;
350
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800351 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800352 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
353 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
354 return -1;
355 }
356 offset += n;
357
David Ng183a7422009-12-07 14:55:21 -0800358continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800359 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
360 hdr->kernel_size);
361 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
362 hdr->ramdisk_size);
363
364 if(hdr->cmdline[0]) {
365 cmdline = (char*) hdr->cmdline;
366 } else {
367 cmdline = DEFAULT_CMDLINE;
368 }
369 dprintf(INFO, "cmdline = '%s'\n", cmdline);
370
371 /* TODO: create/pass atags to kernel */
372
373 dprintf(INFO, "\nBooting Linux\n");
374 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800375 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800376 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
377
378 return 0;
379}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800380
381void cmd_boot(const char *arg, void *data, unsigned sz)
382{
383 unsigned kernel_actual;
384 unsigned ramdisk_actual;
385 static struct boot_img_hdr hdr;
386 char *ptr = ((char*) data);
387
388 if (sz < sizeof(hdr)) {
389 fastboot_fail("invalid bootimage header");
390 return;
391 }
392
393 memcpy(&hdr, data, sizeof(hdr));
394
395 /* ensure commandline is terminated */
396 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
397
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800398 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
399 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800400
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700401 if (page_size + kernel_actual + ramdisk_actual < sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800402 fastboot_fail("incomplete bootimage");
403 return;
404 }
405
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700406 memmove((void*) KERNEL_ADDR, ptr + page_size, hdr.kernel_size);
407 memmove((void*) RAMDISK_ADDR, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800408
409 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800410 target_battery_charging_enable(0, 1);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800411 udc_stop();
412
Brian Swetland9c4c0752009-01-25 16:23:50 -0800413 boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800414 (const char*) hdr.cmdline, board_machtype(),
Brian Swetland9c4c0752009-01-25 16:23:50 -0800415 (void*) RAMDISK_ADDR, hdr.ramdisk_size);
416}
417
Dima Zavin214cc642009-01-26 11:16:21 -0800418void cmd_erase(const char *arg, void *data, unsigned sz)
419{
420 struct ptentry *ptn;
421 struct ptable *ptable;
422
423 ptable = flash_get_ptable();
424 if (ptable == NULL) {
425 fastboot_fail("partition table doesn't exist");
426 return;
427 }
428
429 ptn = ptable_find(ptable, arg);
430 if (ptn == NULL) {
431 fastboot_fail("unknown partition name");
432 return;
433 }
434
435 if (flash_erase(ptn)) {
436 fastboot_fail("failed to erase partition");
437 return;
438 }
439 fastboot_okay("");
440}
441
Shashank Mittal23b8f422010-04-16 19:27:21 -0700442void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
443{
444 unsigned long long ptn = 0;
445 ptn = mmc_ptn_offset(arg);
446 if(ptn == 0) {
447 fastboot_fail("partition table doesn't exist");
448 return;
449 }
450
451 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
452 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
453 fastboot_fail("image is not a boot image");
454 return;
455 }
456 }
457
458 if (mmc_write(ptn , sz, (unsigned int *)data)) {
459 fastboot_fail("flash write failure");
460 return;
461 }
462 fastboot_okay("");
463 return;
464}
465
Dima Zavin214cc642009-01-26 11:16:21 -0800466void cmd_flash(const char *arg, void *data, unsigned sz)
467{
468 struct ptentry *ptn;
469 struct ptable *ptable;
470 unsigned extra = 0;
471
472 ptable = flash_get_ptable();
473 if (ptable == NULL) {
474 fastboot_fail("partition table doesn't exist");
475 return;
476 }
477
478 ptn = ptable_find(ptable, arg);
479 if (ptn == NULL) {
480 fastboot_fail("unknown partition name");
481 return;
482 }
483
484 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
485 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
486 fastboot_fail("image is not a boot image");
487 return;
488 }
489 }
490
Chandan Uddarajud6d45042010-02-24 21:12:45 -0800491 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")
492 || !strcmp(ptn->name, "persist"))
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800493 extra = ((page_size >> 9) * 16);
Dima Zavin214cc642009-01-26 11:16:21 -0800494 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800495 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800496
497 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
498 if (flash_write(ptn, extra, data, sz)) {
499 fastboot_fail("flash write failure");
500 return;
501 }
502 dprintf(INFO, "partition '%s' updated\n", ptn->name);
503 fastboot_okay("");
504}
505
506void cmd_continue(const char *arg, void *data, unsigned sz)
507{
508 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800509 target_battery_charging_enable(0, 1);
Dima Zavin214cc642009-01-26 11:16:21 -0800510 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700511 if (target_is_emmc_boot())
512 {
513 boot_linux_from_mmc();
514 }
515 else
516 {
517 boot_linux_from_flash();
518 }
Dima Zavin214cc642009-01-26 11:16:21 -0800519}
520
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800521void cmd_reboot(const char *arg, void *data, unsigned sz)
522{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700523 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800524 fastboot_okay("");
525 reboot_device(0);
526}
527
528void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
529{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700530 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800531 fastboot_okay("");
532 reboot_device(FASTBOOT_MODE);
533}
534
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700535void splash_screen ()
536{
537 struct ptentry *ptn;
538 struct ptable *ptable;
539 struct fbcon_config *fb_display = NULL;
540
541 if (!target_is_emmc_boot())
542 {
543 ptable = flash_get_ptable();
544 if (ptable == NULL) {
545 dprintf(CRITICAL, "ERROR: Partition table not found\n");
546 return -1;
547 }
548
549 ptn = ptable_find(ptable, "splash");
550 if (ptn == NULL) {
551 dprintf(CRITICAL, "ERROR: No splash partition found\n");
552 } else {
553 fb_display = fbcon_display();
554 if (fb_display) {
555 if (flash_read(ptn, 0, fb_display->base,
556 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
557 fbcon_clear();
558 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
559 }
560 }
561 }
562 }
563}
564
Brian Swetland9c4c0752009-01-25 16:23:50 -0800565void aboot_init(const struct app_descriptor *app)
566{
Shashank Mittal4f99a882010-02-01 13:58:50 -0800567 unsigned reboot_mode = 0;
568 unsigned disp_init = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700569 unsigned usb_init = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -0700570
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700571 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700572 if (target_is_emmc_boot())
573 {
574 page_size = 2048;
575 page_mask = page_size - 1;
576 }
577 else
578 {
579 page_size = flash_page_size();
580 page_mask = page_size - 1;
581 }
582
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700583 /* Display splash screen if enabled */
584 #if DISPLAY_SPLASH_SCREEN
585 display_init();
586 dprintf(INFO, "Diplay initialized\n");
587 disp_init = 1;
588 splash_screen();
589 #endif
590
591 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700592 if (keys_get_state(KEY_HOME) != 0)
593 boot_into_recovery = 1;
594 if (keys_get_state(KEY_BACK) != 0)
595 goto fastboot;
596 if (keys_get_state(KEY_CLEAR) != 0)
597 goto fastboot;
598
599 #if NO_KEYPAD_DRIVER
600 /* With no keypad implementation, check the status of USB connection. */
601 /* If USB is connected then go into fastboot mode. */
602 usb_init = 1;
603 udc_init(&surf_udc_device);
604 if (usb_cable_status())
605 goto fastboot;
606 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -0700607
Shashank Mittal23b8f422010-04-16 19:27:21 -0700608 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700609 {
610 boot_linux_from_mmc();
611 }
612 else
613 {
614 reboot_mode = check_reboot_mode();
615 if (reboot_mode == RECOVERY_MODE) {
616 boot_into_recovery = 1;
617 } else if(reboot_mode == FASTBOOT_MODE) {
618 goto fastboot;
619 }
620 recovery_init();
621 boot_linux_from_flash();
622 }
Dima Zavinb4283602009-01-26 16:36:57 -0800623 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
624 "to fastboot mode.\n");
625
626fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700627
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700628 if(!usb_init)
629 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800630
631 fastboot_register("boot", cmd_boot);
Dima Zavin214cc642009-01-26 11:16:21 -0800632 fastboot_register("erase:", cmd_erase);
Shashank Mittal23b8f422010-04-16 19:27:21 -0700633 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700634 {
635 fastboot_register("flash:", cmd_flash_mmc);
636 }
637 else
638 {
639 fastboot_register("flash:", cmd_flash);
640 }
641
642 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800643 fastboot_register("reboot", cmd_reboot);
644 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700645 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -0800646 fastboot_publish("kernel", "lk");
647
Ajay Dudanid43419e2010-02-10 13:17:39 -0800648 fastboot_init(target_get_scratch_address(), 120 * 1024 * 1024);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800649 udc_start();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700650 target_battery_charging_enable(1, 0);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800651}
652
653APP_START(aboot)
654 .init = aboot_init,
655APP_END
656