blob: 31635eadac07ba3c53810067d55366c85cbaeaa3 [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 Dudani6cff85e2011-02-04 16:02:16 -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_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudanid04110c2011-01-17 23:55:07 -080071
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
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800187 case BASEBAND_SVLTE2A:
188 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800189 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
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800237 case BASEBAND_SVLTE2A:
238 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800239 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);
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800329 if(n != 0)
330 {
331 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
332 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
333 return -1;
334 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700335 }
336 offset += n;
337
338unified_boot:
339 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
340 hdr->kernel_size);
341 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
342 hdr->ramdisk_size);
343
344 if(hdr->cmdline[0]) {
345 cmdline = (char*) hdr->cmdline;
346 } else {
347 cmdline = DEFAULT_CMDLINE;
348 }
349 dprintf(INFO, "cmdline = '%s'\n", cmdline);
350
351 dprintf(INFO, "\nBooting Linux\n");
352 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
353 (const char *)cmdline, board_machtype(),
354 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
355
356 return 0;
357}
358
Dima Zavin214cc642009-01-26 11:16:21 -0800359int boot_linux_from_flash(void)
360{
361 struct boot_img_hdr *hdr = (void*) buf;
362 unsigned n;
363 struct ptentry *ptn;
364 struct ptable *ptable;
365 unsigned offset = 0;
366 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800367
David Ng183a7422009-12-07 14:55:21 -0800368 if (target_is_emmc_boot()) {
369 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
370 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
371 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
372 return -1;
373 }
374 goto continue_boot;
375 }
376
Dima Zavin214cc642009-01-26 11:16:21 -0800377 ptable = flash_get_ptable();
378 if (ptable == NULL) {
379 dprintf(CRITICAL, "ERROR: Partition table not found\n");
380 return -1;
381 }
382
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800383 if(!boot_into_recovery)
384 {
385 ptn = ptable_find(ptable, "boot");
386 if (ptn == NULL) {
387 dprintf(CRITICAL, "ERROR: No boot partition found\n");
388 return -1;
389 }
390 }
391 else
392 {
393 ptn = ptable_find(ptable, "recovery");
394 if (ptn == NULL) {
395 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
396 return -1;
397 }
Dima Zavin214cc642009-01-26 11:16:21 -0800398 }
399
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800400 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800401 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
402 return -1;
403 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800404 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800405
406 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
407 dprintf(CRITICAL, "ERROR: Invaled boot image heador\n");
408 return -1;
409 }
410
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800411 if (hdr->page_size != page_size) {
412 dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
413 return -1;
414 }
415
416 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800417 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
418 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
419 return -1;
420 }
421 offset += n;
422
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800423 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800424 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
425 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
426 return -1;
427 }
428 offset += n;
429
David Ng183a7422009-12-07 14:55:21 -0800430continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800431 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
432 hdr->kernel_size);
433 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
434 hdr->ramdisk_size);
435
436 if(hdr->cmdline[0]) {
437 cmdline = (char*) hdr->cmdline;
438 } else {
439 cmdline = DEFAULT_CMDLINE;
440 }
441 dprintf(INFO, "cmdline = '%s'\n", cmdline);
442
443 /* TODO: create/pass atags to kernel */
444
445 dprintf(INFO, "\nBooting Linux\n");
446 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800447 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800448 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
449
450 return 0;
451}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800452
453void cmd_boot(const char *arg, void *data, unsigned sz)
454{
455 unsigned kernel_actual;
456 unsigned ramdisk_actual;
457 static struct boot_img_hdr hdr;
458 char *ptr = ((char*) data);
459
460 if (sz < sizeof(hdr)) {
461 fastboot_fail("invalid bootimage header");
462 return;
463 }
464
465 memcpy(&hdr, data, sizeof(hdr));
466
467 /* ensure commandline is terminated */
468 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
469
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700470 if(target_is_emmc_boot() && hdr.page_size) {
471 page_size = hdr.page_size;
472 page_mask = page_size - 1;
473 }
474
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800475 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
476 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800477
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700478 if (page_size + kernel_actual + ramdisk_actual < sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800479 fastboot_fail("incomplete bootimage");
480 return;
481 }
482
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700483 memmove((void*) KERNEL_ADDR, ptr + page_size, hdr.kernel_size);
484 memmove((void*) RAMDISK_ADDR, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800485
486 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800487 target_battery_charging_enable(0, 1);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800488 udc_stop();
489
Brian Swetland9c4c0752009-01-25 16:23:50 -0800490 boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800491 (const char*) hdr.cmdline, board_machtype(),
Brian Swetland9c4c0752009-01-25 16:23:50 -0800492 (void*) RAMDISK_ADDR, hdr.ramdisk_size);
493}
494
Dima Zavin214cc642009-01-26 11:16:21 -0800495void cmd_erase(const char *arg, void *data, unsigned sz)
496{
497 struct ptentry *ptn;
498 struct ptable *ptable;
499
500 ptable = flash_get_ptable();
501 if (ptable == NULL) {
502 fastboot_fail("partition table doesn't exist");
503 return;
504 }
505
506 ptn = ptable_find(ptable, arg);
507 if (ptn == NULL) {
508 fastboot_fail("unknown partition name");
509 return;
510 }
511
512 if (flash_erase(ptn)) {
513 fastboot_fail("failed to erase partition");
514 return;
515 }
516 fastboot_okay("");
517}
518
Bikas Gurungd48bd242010-09-04 19:54:32 -0700519
520void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
521{
522 unsigned long long ptn = 0;
523 unsigned int out[512] = {0};
524
525 ptn = mmc_ptn_offset(arg);
526 if(ptn == 0) {
527 fastboot_fail("partition table doesn't exist");
528 return;
529 }
530
531
532 /* Simple inefficient version of erase. Just writing
533 0 in first block */
534 if (mmc_write(ptn , 512, (unsigned int *)out)) {
535 fastboot_fail("failed to erase partition");
536 return;
537 }
538 fastboot_okay("");
539}
540
541
Shashank Mittal23b8f422010-04-16 19:27:21 -0700542void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
543{
544 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700545 unsigned long long size = 0;
546
Shashank Mittal23b8f422010-04-16 19:27:21 -0700547 ptn = mmc_ptn_offset(arg);
548 if(ptn == 0) {
549 fastboot_fail("partition table doesn't exist");
550 return;
551 }
552
553 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
554 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
555 fastboot_fail("image is not a boot image");
556 return;
557 }
558 }
559
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700560 size = mmc_ptn_size(arg);
561 if (ROUND_TO_PAGE(sz,511) > size) {
562 fastboot_fail("size too large");
563 return;
564 }
565
Shashank Mittal23b8f422010-04-16 19:27:21 -0700566 if (mmc_write(ptn , sz, (unsigned int *)data)) {
567 fastboot_fail("flash write failure");
568 return;
569 }
570 fastboot_okay("");
571 return;
572}
573
Dima Zavin214cc642009-01-26 11:16:21 -0800574void cmd_flash(const char *arg, void *data, unsigned sz)
575{
576 struct ptentry *ptn;
577 struct ptable *ptable;
578 unsigned extra = 0;
579
580 ptable = flash_get_ptable();
581 if (ptable == NULL) {
582 fastboot_fail("partition table doesn't exist");
583 return;
584 }
585
586 ptn = ptable_find(ptable, arg);
587 if (ptn == NULL) {
588 fastboot_fail("unknown partition name");
589 return;
590 }
591
592 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
593 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
594 fastboot_fail("image is not a boot image");
595 return;
596 }
597 }
598
Chandan Uddarajud6d45042010-02-24 21:12:45 -0800599 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")
600 || !strcmp(ptn->name, "persist"))
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800601 extra = ((page_size >> 9) * 16);
Dima Zavin214cc642009-01-26 11:16:21 -0800602 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800603 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800604
605 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
606 if (flash_write(ptn, extra, data, sz)) {
607 fastboot_fail("flash write failure");
608 return;
609 }
610 dprintf(INFO, "partition '%s' updated\n", ptn->name);
611 fastboot_okay("");
612}
613
614void cmd_continue(const char *arg, void *data, unsigned sz)
615{
616 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800617 target_battery_charging_enable(0, 1);
Dima Zavin214cc642009-01-26 11:16:21 -0800618 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700619 if (target_is_emmc_boot())
620 {
621 boot_linux_from_mmc();
622 }
623 else
624 {
625 boot_linux_from_flash();
626 }
Dima Zavin214cc642009-01-26 11:16:21 -0800627}
628
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800629void cmd_reboot(const char *arg, void *data, unsigned sz)
630{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700631 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800632 fastboot_okay("");
633 reboot_device(0);
634}
635
636void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
637{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700638 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800639 fastboot_okay("");
640 reboot_device(FASTBOOT_MODE);
641}
642
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700643void splash_screen ()
644{
645 struct ptentry *ptn;
646 struct ptable *ptable;
647 struct fbcon_config *fb_display = NULL;
648
649 if (!target_is_emmc_boot())
650 {
651 ptable = flash_get_ptable();
652 if (ptable == NULL) {
653 dprintf(CRITICAL, "ERROR: Partition table not found\n");
654 return -1;
655 }
656
657 ptn = ptable_find(ptable, "splash");
658 if (ptn == NULL) {
659 dprintf(CRITICAL, "ERROR: No splash partition found\n");
660 } else {
661 fb_display = fbcon_display();
662 if (fb_display) {
663 if (flash_read(ptn, 0, fb_display->base,
664 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
665 fbcon_clear();
666 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
667 }
668 }
669 }
670 }
671}
672
Brian Swetland9c4c0752009-01-25 16:23:50 -0800673void aboot_init(const struct app_descriptor *app)
674{
Shashank Mittal4f99a882010-02-01 13:58:50 -0800675 unsigned reboot_mode = 0;
676 unsigned disp_init = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700677 unsigned usb_init = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -0700678
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700679 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700680 if (target_is_emmc_boot())
681 {
682 page_size = 2048;
683 page_mask = page_size - 1;
684 }
685 else
686 {
687 page_size = flash_page_size();
688 page_mask = page_size - 1;
689 }
690
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700691 /* Display splash screen if enabled */
692 #if DISPLAY_SPLASH_SCREEN
693 display_init();
694 dprintf(INFO, "Diplay initialized\n");
695 disp_init = 1;
Chandan Uddaraju40b227d2010-08-03 19:25:41 -0700696 diplay_image_on_screen();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700697 #endif
698
699 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700700 if (keys_get_state(KEY_HOME) != 0)
701 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -0500702 if (keys_get_state(KEY_VOLUMEUP) != 0)
703 boot_into_recovery = 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700704 if (keys_get_state(KEY_BACK) != 0)
705 goto fastboot;
Wentao Xu153902c2010-12-20 16:20:52 -0500706 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700707 goto fastboot;
708
709 #if NO_KEYPAD_DRIVER
710 /* With no keypad implementation, check the status of USB connection. */
711 /* If USB is connected then go into fastboot mode. */
712 usb_init = 1;
713 udc_init(&surf_udc_device);
714 if (usb_cable_status())
715 goto fastboot;
716 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -0700717
Ajay Dudani77421292010-10-27 19:34:06 -0700718 reboot_mode = check_reboot_mode();
719 if (reboot_mode == RECOVERY_MODE) {
720 boot_into_recovery = 1;
721 } else if(reboot_mode == FASTBOOT_MODE) {
722 goto fastboot;
723 }
724
Shashank Mittal23b8f422010-04-16 19:27:21 -0700725 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700726 {
727 boot_linux_from_mmc();
728 }
729 else
730 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700731 recovery_init();
732 boot_linux_from_flash();
733 }
Dima Zavinb4283602009-01-26 16:36:57 -0800734 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
735 "to fastboot mode.\n");
736
737fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700738
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700739 if(!usb_init)
740 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800741
742 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700743
Shashank Mittal23b8f422010-04-16 19:27:21 -0700744 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700745 {
746 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700747 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700748 }
749 else
750 {
751 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700752 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700753 }
754
755 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800756 fastboot_register("reboot", cmd_reboot);
757 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700758 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -0800759 fastboot_publish("kernel", "lk");
760
Ajay Dudanid43419e2010-02-10 13:17:39 -0800761 fastboot_init(target_get_scratch_address(), 120 * 1024 * 1024);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800762 udc_start();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700763 target_battery_charging_enable(1, 0);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800764}
765
766APP_START(aboot)
767 .init = aboot_init,
768APP_END
769