blob: ded19b88998de6a74751f06731060b4e43790daf [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"
50
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -070051#define EXPAND(NAME) #NAME
52#define TARGET(NAME) EXPAND(NAME)
Chandan Uddarajuda919832009-11-17 01:06:11 -080053#define DEFAULT_CMDLINE "mem=100M console=null";
Brian Swetland2defe162009-08-18 14:35:59 -070054
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080055#ifdef MEMBASE
56#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
57#else
David Ng183a7422009-12-07 14:55:21 -080058#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080059#endif
60
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080061#define RECOVERY_MODE 0x77665502
62#define FASTBOOT_MODE 0x77665500
63
David Ng183a7422009-12-07 14:55:21 -080064static const char *emmc_cmdline = " androidboot.emmc=true";
David Ngf773dde2010-07-26 19:55:08 -070065static const char *battchg_pause = " androidboot.battchg_pause=true";
David Ng183a7422009-12-07 14:55:21 -080066
Ajay Dudanid04110c2011-01-17 23:55:07 -080067static const char *baseband_apq = " androidboot.baseband=apq";
68static const char *baseband_msm = " androidboot.baseband=msm";
69static const char *baseband_csfb = " androidboot.baseband=csfb";
70static const char *baseband_svlte = " androidboot.baseband=svlte";
71
Brian Swetland9c4c0752009-01-25 16:23:50 -080072static struct udc_device surf_udc_device = {
73 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080074 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080075 .version_id = 0x0100,
76 .manufacturer = "Google",
77 .product = "Android",
78};
79
Dima Zavin42168f22009-01-30 11:52:22 -080080struct atag_ptbl_entry
81{
82 char name[16];
83 unsigned offset;
84 unsigned size;
85 unsigned flags;
86};
87
Brian Swetland9c4c0752009-01-25 16:23:50 -080088void platform_uninit_timer(void);
Chandan Uddarajuc6860e12009-11-19 11:22:15 -080089unsigned* target_atag_mem(unsigned* ptr);
Chandan Uddaraju885e4db2009-12-03 22:45:26 -080090unsigned board_machtype(void);
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080091unsigned check_reboot_mode(void);
David Ng3679bc52010-02-09 15:43:43 -080092void *target_get_scratch_address(void);
David Ng183a7422009-12-07 14:55:21 -080093int target_is_emmc_boot(void);
Chandan Uddaraju94183c02010-01-15 15:13:59 -080094void reboot_device(unsigned);
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -080095void target_battery_charging_enable(unsigned enable, unsigned disconnect);
Bikas Gurungd48bd242010-09-04 19:54:32 -070096unsigned int mmc_write (unsigned long long data_addr,
97 unsigned int data_len, unsigned int* in);
Shashank Mittal920798e2010-10-04 17:17:37 -070098unsigned long long mmc_ptn_offset (unsigned char * name);
99unsigned long long mmc_ptn_size (unsigned char * name);
Shashank Mittalc648e712010-10-06 18:37:42 -0700100void display_shutdown(void);
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800101
Dima Zavin42168f22009-01-30 11:52:22 -0800102static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
103{
104 struct atag_ptbl_entry atag_ptn;
105
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800106 if (ptn->type == TYPE_MODEM_PARTITION)
107 return;
Dima Zavin42168f22009-01-30 11:52:22 -0800108 memcpy(atag_ptn.name, ptn->name, 16);
109 atag_ptn.name[15] = '\0';
110 atag_ptn.offset = ptn->start;
111 atag_ptn.size = ptn->length;
112 atag_ptn.flags = ptn->flags;
113 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
114 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
115}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800116
117void boot_linux(void *kernel, unsigned *tags,
118 const char *cmdline, unsigned machtype,
119 void *ramdisk, unsigned ramdisk_size)
120{
121 unsigned *ptr = tags;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800122 unsigned pcount = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800123 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800124 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800125 int cmdline_len = 0;
126 int have_cmdline = 0;
David Ngf773dde2010-07-26 19:55:08 -0700127 int pause_at_bootup = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800128
129 /* CORE */
130 *ptr++ = 2;
131 *ptr++ = 0x54410001;
132
133 if (ramdisk_size) {
134 *ptr++ = 4;
135 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800136 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800137 *ptr++ = ramdisk_size;
138 }
139
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800140 ptr = target_atag_mem(ptr);
141
David Ng183a7422009-12-07 14:55:21 -0800142 if (!target_is_emmc_boot()) {
143 /* Skip NAND partition ATAGS for eMMC boot */
144 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
145 int i;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800146 for(i=0; i < ptable->count; i++) {
147 struct ptentry *ptn;
148 ptn = ptable_get(ptable, i);
149 if (ptn->type == TYPE_APPS_PARTITION)
150 pcount++;
151 }
152 *ptr++ = 2 + (pcount * (sizeof(struct atag_ptbl_entry) /
David Ng183a7422009-12-07 14:55:21 -0800153 sizeof(unsigned)));
154 *ptr++ = 0x4d534d70;
155 for (i = 0; i < ptable->count; ++i)
156 ptentry_to_tag(&ptr, ptable_get(ptable, i));
157 }
Dima Zavin42168f22009-01-30 11:52:22 -0800158 }
159
Brian Swetland9c4c0752009-01-25 16:23:50 -0800160 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800161 cmdline_len = strlen(cmdline);
162 have_cmdline = 1;
163 }
164 if (target_is_emmc_boot()) {
165 cmdline_len += strlen(emmc_cmdline);
166 }
David Ngf773dde2010-07-26 19:55:08 -0700167 if (target_pause_for_battery_charge()) {
168 pause_at_bootup = 1;
169 cmdline_len += strlen(battchg_pause);
170 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800171
172 /* Determine correct androidboot.baseband to use */
173 switch(target_baseband())
174 {
175 case BASEBAND_APQ:
176 cmdline_len += strlen(baseband_apq);
177 break;
178
179 case BASEBAND_MSM:
180 cmdline_len += strlen(baseband_msm);
181 break;
182
183 case BASEBAND_CSFB:
184 cmdline_len += strlen(baseband_csfb);
185 break;
186
187 case BASEBAND_SVLTE:
188 cmdline_len += strlen(baseband_svlte);
189 break;
190 }
191
David Ng183a7422009-12-07 14:55:21 -0800192 if (cmdline_len > 0) {
193 const char *src;
194 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800195 unsigned n;
196 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800197 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800198 *ptr++ = (n / 4) + 2;
199 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800200 dst = (char *)ptr;
201 if (have_cmdline) {
202 src = cmdline;
203 while ((*dst++ = *src++));
204 }
205 if (target_is_emmc_boot()) {
206 src = emmc_cmdline;
207 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700208 have_cmdline = 1;
209 while ((*dst++ = *src++));
210 }
211 if (pause_at_bootup) {
212 src = battchg_pause;
213 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800214 while ((*dst++ = *src++));
215 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800216
217 switch(target_baseband())
218 {
219 case BASEBAND_APQ:
220 src = baseband_apq;
221 if (have_cmdline) --dst;
222 while ((*dst++ = *src++));
223 break;
224
225 case BASEBAND_MSM:
226 src = baseband_msm;
227 if (have_cmdline) --dst;
228 while ((*dst++ = *src++));
229 break;
230
231 case BASEBAND_CSFB:
232 src = baseband_csfb;
233 if (have_cmdline) --dst;
234 while ((*dst++ = *src++));
235 break;
236
237 case BASEBAND_SVLTE:
238 src = baseband_svlte;
239 if (have_cmdline) --dst;
240 while ((*dst++ = *src++));
241 break;
242 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800243 ptr += (n / 4);
244 }
245
246 /* END */
247 *ptr++ = 0;
248 *ptr++ = 0;
249
250 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
251 kernel, ramdisk, ramdisk_size);
252 if (cmdline)
253 dprintf(INFO, "cmdline: %s\n", cmdline);
254
255 enter_critical_section();
256 platform_uninit_timer();
257 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800258 arch_disable_mmu();
Shashank Mittalc648e712010-10-06 18:37:42 -0700259#if DISPLAY_SPLASH_SCREEN
260 display_shutdown();
261#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800262 entry(0, machtype, tags);
263
264}
265
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800266unsigned page_size = 0;
267unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800268
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800269#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800270
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800271static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800272
Shashank Mittal23b8f422010-04-16 19:27:21 -0700273int boot_linux_from_mmc(void)
274{
275 struct boot_img_hdr *hdr = (void*) buf;
276 struct boot_img_hdr *uhdr;
277 unsigned offset = 0;
278 unsigned long long ptn = 0;
279 unsigned n = 0;
280 const char *cmdline;
281
282 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
283 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
284 dprintf(INFO, "Unified boot method!\n");
285 hdr = uhdr;
286 goto unified_boot;
287 }
Shashank Mittal85b91f62010-10-30 10:12:38 -0700288 if(!boot_into_recovery)
289 {
290 ptn = mmc_ptn_offset("boot");
291 if(ptn == 0) {
292 dprintf(CRITICAL, "ERROR: No boot partition found\n");
293 return -1;
294 }
295 }
296 else
297 {
298 ptn = mmc_ptn_offset("recovery");
299 if(ptn == 0) {
300 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
301 return -1;
302 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700303 }
304
305 if (mmc_read(ptn + offset, (unsigned int *)buf, page_size)) {
306 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
307 return -1;
308 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700309
310 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
311 dprintf(CRITICAL, "ERROR: Invaled boot image header\n");
312 return -1;
313 }
314
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700315 if (hdr->page_size && (hdr->page_size != page_size)) {
316 page_size = hdr->page_size;
317 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700318 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700319 offset += page_size;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700320
321 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
322 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
323 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
324 return -1;
325 }
326 offset += n;
327
328 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
329 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
330 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
331 return -1;
332 }
333 offset += n;
334
335unified_boot:
336 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
337 hdr->kernel_size);
338 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
339 hdr->ramdisk_size);
340
341 if(hdr->cmdline[0]) {
342 cmdline = (char*) hdr->cmdline;
343 } else {
344 cmdline = DEFAULT_CMDLINE;
345 }
346 dprintf(INFO, "cmdline = '%s'\n", cmdline);
347
348 dprintf(INFO, "\nBooting Linux\n");
349 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
350 (const char *)cmdline, board_machtype(),
351 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
352
353 return 0;
354}
355
Dima Zavin214cc642009-01-26 11:16:21 -0800356int boot_linux_from_flash(void)
357{
358 struct boot_img_hdr *hdr = (void*) buf;
359 unsigned n;
360 struct ptentry *ptn;
361 struct ptable *ptable;
362 unsigned offset = 0;
363 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800364
David Ng183a7422009-12-07 14:55:21 -0800365 if (target_is_emmc_boot()) {
366 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
367 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
368 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
369 return -1;
370 }
371 goto continue_boot;
372 }
373
Dima Zavin214cc642009-01-26 11:16:21 -0800374 ptable = flash_get_ptable();
375 if (ptable == NULL) {
376 dprintf(CRITICAL, "ERROR: Partition table not found\n");
377 return -1;
378 }
379
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800380 if(!boot_into_recovery)
381 {
382 ptn = ptable_find(ptable, "boot");
383 if (ptn == NULL) {
384 dprintf(CRITICAL, "ERROR: No boot partition found\n");
385 return -1;
386 }
387 }
388 else
389 {
390 ptn = ptable_find(ptable, "recovery");
391 if (ptn == NULL) {
392 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
393 return -1;
394 }
Dima Zavin214cc642009-01-26 11:16:21 -0800395 }
396
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800397 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800398 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
399 return -1;
400 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800401 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800402
403 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
404 dprintf(CRITICAL, "ERROR: Invaled boot image heador\n");
405 return -1;
406 }
407
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800408 if (hdr->page_size != page_size) {
409 dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
410 return -1;
411 }
412
413 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800414 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
415 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
416 return -1;
417 }
418 offset += n;
419
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800420 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800421 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
422 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
423 return -1;
424 }
425 offset += n;
426
David Ng183a7422009-12-07 14:55:21 -0800427continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800428 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
429 hdr->kernel_size);
430 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
431 hdr->ramdisk_size);
432
433 if(hdr->cmdline[0]) {
434 cmdline = (char*) hdr->cmdline;
435 } else {
436 cmdline = DEFAULT_CMDLINE;
437 }
438 dprintf(INFO, "cmdline = '%s'\n", cmdline);
439
440 /* TODO: create/pass atags to kernel */
441
442 dprintf(INFO, "\nBooting Linux\n");
443 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800444 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800445 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
446
447 return 0;
448}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800449
450void cmd_boot(const char *arg, void *data, unsigned sz)
451{
452 unsigned kernel_actual;
453 unsigned ramdisk_actual;
454 static struct boot_img_hdr hdr;
455 char *ptr = ((char*) data);
456
457 if (sz < sizeof(hdr)) {
458 fastboot_fail("invalid bootimage header");
459 return;
460 }
461
462 memcpy(&hdr, data, sizeof(hdr));
463
464 /* ensure commandline is terminated */
465 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
466
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700467 if(target_is_emmc_boot() && hdr.page_size) {
468 page_size = hdr.page_size;
469 page_mask = page_size - 1;
470 }
471
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800472 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
473 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800474
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700475 if (page_size + kernel_actual + ramdisk_actual < sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800476 fastboot_fail("incomplete bootimage");
477 return;
478 }
479
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700480 memmove((void*) KERNEL_ADDR, ptr + page_size, hdr.kernel_size);
481 memmove((void*) RAMDISK_ADDR, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800482
483 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800484 target_battery_charging_enable(0, 1);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800485 udc_stop();
486
Brian Swetland9c4c0752009-01-25 16:23:50 -0800487 boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800488 (const char*) hdr.cmdline, board_machtype(),
Brian Swetland9c4c0752009-01-25 16:23:50 -0800489 (void*) RAMDISK_ADDR, hdr.ramdisk_size);
490}
491
Dima Zavin214cc642009-01-26 11:16:21 -0800492void cmd_erase(const char *arg, void *data, unsigned sz)
493{
494 struct ptentry *ptn;
495 struct ptable *ptable;
496
497 ptable = flash_get_ptable();
498 if (ptable == NULL) {
499 fastboot_fail("partition table doesn't exist");
500 return;
501 }
502
503 ptn = ptable_find(ptable, arg);
504 if (ptn == NULL) {
505 fastboot_fail("unknown partition name");
506 return;
507 }
508
509 if (flash_erase(ptn)) {
510 fastboot_fail("failed to erase partition");
511 return;
512 }
513 fastboot_okay("");
514}
515
Bikas Gurungd48bd242010-09-04 19:54:32 -0700516
517void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
518{
519 unsigned long long ptn = 0;
520 unsigned int out[512] = {0};
521
522 ptn = mmc_ptn_offset(arg);
523 if(ptn == 0) {
524 fastboot_fail("partition table doesn't exist");
525 return;
526 }
527
528
529 /* Simple inefficient version of erase. Just writing
530 0 in first block */
531 if (mmc_write(ptn , 512, (unsigned int *)out)) {
532 fastboot_fail("failed to erase partition");
533 return;
534 }
535 fastboot_okay("");
536}
537
538
Shashank Mittal23b8f422010-04-16 19:27:21 -0700539void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
540{
541 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700542 unsigned long long size = 0;
543
Shashank Mittal23b8f422010-04-16 19:27:21 -0700544 ptn = mmc_ptn_offset(arg);
545 if(ptn == 0) {
546 fastboot_fail("partition table doesn't exist");
547 return;
548 }
549
550 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
551 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
552 fastboot_fail("image is not a boot image");
553 return;
554 }
555 }
556
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700557 size = mmc_ptn_size(arg);
558 if (ROUND_TO_PAGE(sz,511) > size) {
559 fastboot_fail("size too large");
560 return;
561 }
562
Shashank Mittal23b8f422010-04-16 19:27:21 -0700563 if (mmc_write(ptn , sz, (unsigned int *)data)) {
564 fastboot_fail("flash write failure");
565 return;
566 }
567 fastboot_okay("");
568 return;
569}
570
Dima Zavin214cc642009-01-26 11:16:21 -0800571void cmd_flash(const char *arg, void *data, unsigned sz)
572{
573 struct ptentry *ptn;
574 struct ptable *ptable;
575 unsigned extra = 0;
576
577 ptable = flash_get_ptable();
578 if (ptable == NULL) {
579 fastboot_fail("partition table doesn't exist");
580 return;
581 }
582
583 ptn = ptable_find(ptable, arg);
584 if (ptn == NULL) {
585 fastboot_fail("unknown partition name");
586 return;
587 }
588
589 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
590 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
591 fastboot_fail("image is not a boot image");
592 return;
593 }
594 }
595
Chandan Uddarajud6d45042010-02-24 21:12:45 -0800596 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")
597 || !strcmp(ptn->name, "persist"))
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800598 extra = ((page_size >> 9) * 16);
Dima Zavin214cc642009-01-26 11:16:21 -0800599 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800600 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800601
602 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
603 if (flash_write(ptn, extra, data, sz)) {
604 fastboot_fail("flash write failure");
605 return;
606 }
607 dprintf(INFO, "partition '%s' updated\n", ptn->name);
608 fastboot_okay("");
609}
610
611void cmd_continue(const char *arg, void *data, unsigned sz)
612{
613 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800614 target_battery_charging_enable(0, 1);
Dima Zavin214cc642009-01-26 11:16:21 -0800615 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700616 if (target_is_emmc_boot())
617 {
618 boot_linux_from_mmc();
619 }
620 else
621 {
622 boot_linux_from_flash();
623 }
Dima Zavin214cc642009-01-26 11:16:21 -0800624}
625
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800626void cmd_reboot(const char *arg, void *data, unsigned sz)
627{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700628 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800629 fastboot_okay("");
630 reboot_device(0);
631}
632
633void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
634{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700635 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800636 fastboot_okay("");
637 reboot_device(FASTBOOT_MODE);
638}
639
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700640void splash_screen ()
641{
642 struct ptentry *ptn;
643 struct ptable *ptable;
644 struct fbcon_config *fb_display = NULL;
645
646 if (!target_is_emmc_boot())
647 {
648 ptable = flash_get_ptable();
649 if (ptable == NULL) {
650 dprintf(CRITICAL, "ERROR: Partition table not found\n");
651 return -1;
652 }
653
654 ptn = ptable_find(ptable, "splash");
655 if (ptn == NULL) {
656 dprintf(CRITICAL, "ERROR: No splash partition found\n");
657 } else {
658 fb_display = fbcon_display();
659 if (fb_display) {
660 if (flash_read(ptn, 0, fb_display->base,
661 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
662 fbcon_clear();
663 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
664 }
665 }
666 }
667 }
668}
669
Brian Swetland9c4c0752009-01-25 16:23:50 -0800670void aboot_init(const struct app_descriptor *app)
671{
Shashank Mittal4f99a882010-02-01 13:58:50 -0800672 unsigned reboot_mode = 0;
673 unsigned disp_init = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700674 unsigned usb_init = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -0700675
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700676 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700677 if (target_is_emmc_boot())
678 {
679 page_size = 2048;
680 page_mask = page_size - 1;
681 }
682 else
683 {
684 page_size = flash_page_size();
685 page_mask = page_size - 1;
686 }
687
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700688 /* Display splash screen if enabled */
689 #if DISPLAY_SPLASH_SCREEN
690 display_init();
691 dprintf(INFO, "Diplay initialized\n");
692 disp_init = 1;
Chandan Uddaraju40b227d2010-08-03 19:25:41 -0700693 diplay_image_on_screen();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700694 #endif
695
696 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700697 if (keys_get_state(KEY_HOME) != 0)
698 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -0500699 if (keys_get_state(KEY_VOLUMEUP) != 0)
700 boot_into_recovery = 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700701 if (keys_get_state(KEY_BACK) != 0)
702 goto fastboot;
Wentao Xu153902c2010-12-20 16:20:52 -0500703 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700704 goto fastboot;
705
706 #if NO_KEYPAD_DRIVER
707 /* With no keypad implementation, check the status of USB connection. */
708 /* If USB is connected then go into fastboot mode. */
709 usb_init = 1;
710 udc_init(&surf_udc_device);
711 if (usb_cable_status())
712 goto fastboot;
713 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -0700714
Ajay Dudani77421292010-10-27 19:34:06 -0700715 reboot_mode = check_reboot_mode();
716 if (reboot_mode == RECOVERY_MODE) {
717 boot_into_recovery = 1;
718 } else if(reboot_mode == FASTBOOT_MODE) {
719 goto fastboot;
720 }
721
Shashank Mittal23b8f422010-04-16 19:27:21 -0700722 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700723 {
724 boot_linux_from_mmc();
725 }
726 else
727 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700728 recovery_init();
729 boot_linux_from_flash();
730 }
Dima Zavinb4283602009-01-26 16:36:57 -0800731 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
732 "to fastboot mode.\n");
733
734fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700735
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700736 if(!usb_init)
737 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800738
739 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700740
Shashank Mittal23b8f422010-04-16 19:27:21 -0700741 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700742 {
743 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700744 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700745 }
746 else
747 {
748 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700749 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700750 }
751
752 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800753 fastboot_register("reboot", cmd_reboot);
754 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700755 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -0800756 fastboot_publish("kernel", "lk");
757
Ajay Dudanid43419e2010-02-10 13:17:39 -0800758 fastboot_init(target_get_scratch_address(), 120 * 1024 * 1024);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800759 udc_start();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700760 target_battery_charging_enable(1, 0);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800761}
762
763APP_START(aboot)
764 .init = aboot_init,
765APP_END
766