blob: 82b6dd81503bd98125d79e8a8b997bf28f052c57 [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";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080065static const char *usb_sn_cmdline = " androidboot.serialno=";
David Ngf773dde2010-07-26 19:55:08 -070066static const char *battchg_pause = " androidboot.battchg_pause=true";
David Ng183a7422009-12-07 14:55:21 -080067
Ajay Dudani6cff85e2011-02-04 16:02:16 -080068static const char *baseband_apq = " androidboot.baseband=apq";
69static const char *baseband_msm = " androidboot.baseband=msm";
70static const char *baseband_csfb = " androidboot.baseband=csfb";
71static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudanid04110c2011-01-17 23:55:07 -080072
Brian Swetland9c4c0752009-01-25 16:23:50 -080073static struct udc_device surf_udc_device = {
74 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080075 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080076 .version_id = 0x0100,
77 .manufacturer = "Google",
78 .product = "Android",
79};
80
Dima Zavin42168f22009-01-30 11:52:22 -080081struct atag_ptbl_entry
82{
83 char name[16];
84 unsigned offset;
85 unsigned size;
86 unsigned flags;
87};
88
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -080089char sn_buf[13];
Brian Swetland9c4c0752009-01-25 16:23:50 -080090void platform_uninit_timer(void);
Chandan Uddarajuc6860e12009-11-19 11:22:15 -080091unsigned* target_atag_mem(unsigned* ptr);
Chandan Uddaraju885e4db2009-12-03 22:45:26 -080092unsigned board_machtype(void);
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080093unsigned check_reboot_mode(void);
David Ng3679bc52010-02-09 15:43:43 -080094void *target_get_scratch_address(void);
David Ng183a7422009-12-07 14:55:21 -080095int target_is_emmc_boot(void);
Chandan Uddaraju94183c02010-01-15 15:13:59 -080096void reboot_device(unsigned);
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -080097void target_battery_charging_enable(unsigned enable, unsigned disconnect);
Bikas Gurungd48bd242010-09-04 19:54:32 -070098unsigned int mmc_write (unsigned long long data_addr,
99 unsigned int data_len, unsigned int* in);
Shashank Mittal920798e2010-10-04 17:17:37 -0700100unsigned long long mmc_ptn_offset (unsigned char * name);
101unsigned long long mmc_ptn_size (unsigned char * name);
Shashank Mittalc648e712010-10-06 18:37:42 -0700102void display_shutdown(void);
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800103
Dima Zavin42168f22009-01-30 11:52:22 -0800104static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
105{
106 struct atag_ptbl_entry atag_ptn;
107
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800108 if (ptn->type == TYPE_MODEM_PARTITION)
109 return;
Dima Zavin42168f22009-01-30 11:52:22 -0800110 memcpy(atag_ptn.name, ptn->name, 16);
111 atag_ptn.name[15] = '\0';
112 atag_ptn.offset = ptn->start;
113 atag_ptn.size = ptn->length;
114 atag_ptn.flags = ptn->flags;
115 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
116 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
117}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800118
119void boot_linux(void *kernel, unsigned *tags,
120 const char *cmdline, unsigned machtype,
121 void *ramdisk, unsigned ramdisk_size)
122{
123 unsigned *ptr = tags;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800124 unsigned pcount = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800125 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800126 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800127 int cmdline_len = 0;
128 int have_cmdline = 0;
David Ngf773dde2010-07-26 19:55:08 -0700129 int pause_at_bootup = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800130
131 /* CORE */
132 *ptr++ = 2;
133 *ptr++ = 0x54410001;
134
135 if (ramdisk_size) {
136 *ptr++ = 4;
137 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800138 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800139 *ptr++ = ramdisk_size;
140 }
141
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800142 ptr = target_atag_mem(ptr);
143
David Ng183a7422009-12-07 14:55:21 -0800144 if (!target_is_emmc_boot()) {
145 /* Skip NAND partition ATAGS for eMMC boot */
146 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
147 int i;
Shashank Mittal8e49dec2010-03-01 15:19:04 -0800148 for(i=0; i < ptable->count; i++) {
149 struct ptentry *ptn;
150 ptn = ptable_get(ptable, i);
151 if (ptn->type == TYPE_APPS_PARTITION)
152 pcount++;
153 }
154 *ptr++ = 2 + (pcount * (sizeof(struct atag_ptbl_entry) /
David Ng183a7422009-12-07 14:55:21 -0800155 sizeof(unsigned)));
156 *ptr++ = 0x4d534d70;
157 for (i = 0; i < ptable->count; ++i)
158 ptentry_to_tag(&ptr, ptable_get(ptable, i));
159 }
Dima Zavin42168f22009-01-30 11:52:22 -0800160 }
161
Brian Swetland9c4c0752009-01-25 16:23:50 -0800162 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800163 cmdline_len = strlen(cmdline);
164 have_cmdline = 1;
165 }
166 if (target_is_emmc_boot()) {
167 cmdline_len += strlen(emmc_cmdline);
168 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800169
170 cmdline_len += strlen(usb_sn_cmdline);
171 cmdline_len += strlen(sn_buf);
172
David Ngf773dde2010-07-26 19:55:08 -0700173 if (target_pause_for_battery_charge()) {
174 pause_at_bootup = 1;
175 cmdline_len += strlen(battchg_pause);
176 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800177
178 /* Determine correct androidboot.baseband to use */
179 switch(target_baseband())
180 {
181 case BASEBAND_APQ:
182 cmdline_len += strlen(baseband_apq);
183 break;
184
185 case BASEBAND_MSM:
186 cmdline_len += strlen(baseband_msm);
187 break;
188
189 case BASEBAND_CSFB:
190 cmdline_len += strlen(baseband_csfb);
191 break;
192
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800193 case BASEBAND_SVLTE2A:
194 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800195 break;
196 }
197
David Ng183a7422009-12-07 14:55:21 -0800198 if (cmdline_len > 0) {
199 const char *src;
200 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800201 unsigned n;
202 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800203 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800204 *ptr++ = (n / 4) + 2;
205 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800206 dst = (char *)ptr;
207 if (have_cmdline) {
208 src = cmdline;
209 while ((*dst++ = *src++));
210 }
211 if (target_is_emmc_boot()) {
212 src = emmc_cmdline;
213 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700214 have_cmdline = 1;
215 while ((*dst++ = *src++));
216 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800217
218 src = usb_sn_cmdline;
219 if (have_cmdline) --dst;
220 have_cmdline = 1;
221 while ((*dst++ = *src++));
222 src = sn_buf;
223 if (have_cmdline) --dst;
224 have_cmdline = 1;
225 while ((*dst++ = *src++));
226
David Ngf773dde2010-07-26 19:55:08 -0700227 if (pause_at_bootup) {
228 src = battchg_pause;
229 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800230 while ((*dst++ = *src++));
231 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800232
233 switch(target_baseband())
234 {
235 case BASEBAND_APQ:
236 src = baseband_apq;
237 if (have_cmdline) --dst;
238 while ((*dst++ = *src++));
239 break;
240
241 case BASEBAND_MSM:
242 src = baseband_msm;
243 if (have_cmdline) --dst;
244 while ((*dst++ = *src++));
245 break;
246
247 case BASEBAND_CSFB:
248 src = baseband_csfb;
249 if (have_cmdline) --dst;
250 while ((*dst++ = *src++));
251 break;
252
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800253 case BASEBAND_SVLTE2A:
254 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800255 if (have_cmdline) --dst;
256 while ((*dst++ = *src++));
257 break;
258 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800259 ptr += (n / 4);
260 }
261
262 /* END */
263 *ptr++ = 0;
264 *ptr++ = 0;
265
266 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
267 kernel, ramdisk, ramdisk_size);
268 if (cmdline)
269 dprintf(INFO, "cmdline: %s\n", cmdline);
270
271 enter_critical_section();
272 platform_uninit_timer();
273 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800274 arch_disable_mmu();
Shashank Mittalc648e712010-10-06 18:37:42 -0700275#if DISPLAY_SPLASH_SCREEN
276 display_shutdown();
277#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800278 entry(0, machtype, tags);
279
280}
281
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800282unsigned page_size = 0;
283unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800284
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800285#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800286
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800287static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800288
Shashank Mittal23b8f422010-04-16 19:27:21 -0700289int boot_linux_from_mmc(void)
290{
291 struct boot_img_hdr *hdr = (void*) buf;
292 struct boot_img_hdr *uhdr;
293 unsigned offset = 0;
294 unsigned long long ptn = 0;
295 unsigned n = 0;
296 const char *cmdline;
297
298 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
299 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
300 dprintf(INFO, "Unified boot method!\n");
301 hdr = uhdr;
302 goto unified_boot;
303 }
Shashank Mittal85b91f62010-10-30 10:12:38 -0700304 if(!boot_into_recovery)
305 {
306 ptn = mmc_ptn_offset("boot");
307 if(ptn == 0) {
308 dprintf(CRITICAL, "ERROR: No boot partition found\n");
309 return -1;
310 }
311 }
312 else
313 {
314 ptn = mmc_ptn_offset("recovery");
315 if(ptn == 0) {
316 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
317 return -1;
318 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700319 }
320
321 if (mmc_read(ptn + offset, (unsigned int *)buf, page_size)) {
322 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
323 return -1;
324 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700325
326 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
327 dprintf(CRITICAL, "ERROR: Invaled boot image header\n");
328 return -1;
329 }
330
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700331 if (hdr->page_size && (hdr->page_size != page_size)) {
332 page_size = hdr->page_size;
333 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700334 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700335 offset += page_size;
Shashank Mittal23b8f422010-04-16 19:27:21 -0700336
337 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
338 if (mmc_read(ptn + offset, (void *)hdr->kernel_addr, n)) {
339 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
340 return -1;
341 }
342 offset += n;
343
344 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Subbaraman Narayanamurthy958fa242011-01-27 17:42:38 -0800345 if(n != 0)
346 {
347 if (mmc_read(ptn + offset, (void *)hdr->ramdisk_addr, n)) {
348 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
349 return -1;
350 }
Shashank Mittal23b8f422010-04-16 19:27:21 -0700351 }
352 offset += n;
353
354unified_boot:
355 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
356 hdr->kernel_size);
357 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
358 hdr->ramdisk_size);
359
360 if(hdr->cmdline[0]) {
361 cmdline = (char*) hdr->cmdline;
362 } else {
363 cmdline = DEFAULT_CMDLINE;
364 }
365 dprintf(INFO, "cmdline = '%s'\n", cmdline);
366
367 dprintf(INFO, "\nBooting Linux\n");
368 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
369 (const char *)cmdline, board_machtype(),
370 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
371
372 return 0;
373}
374
Dima Zavin214cc642009-01-26 11:16:21 -0800375int boot_linux_from_flash(void)
376{
377 struct boot_img_hdr *hdr = (void*) buf;
378 unsigned n;
379 struct ptentry *ptn;
380 struct ptable *ptable;
381 unsigned offset = 0;
382 const char *cmdline;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800383
David Ng183a7422009-12-07 14:55:21 -0800384 if (target_is_emmc_boot()) {
385 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
386 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
387 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
388 return -1;
389 }
390 goto continue_boot;
391 }
392
Dima Zavin214cc642009-01-26 11:16:21 -0800393 ptable = flash_get_ptable();
394 if (ptable == NULL) {
395 dprintf(CRITICAL, "ERROR: Partition table not found\n");
396 return -1;
397 }
398
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800399 if(!boot_into_recovery)
400 {
401 ptn = ptable_find(ptable, "boot");
402 if (ptn == NULL) {
403 dprintf(CRITICAL, "ERROR: No boot partition found\n");
404 return -1;
405 }
406 }
407 else
408 {
409 ptn = ptable_find(ptable, "recovery");
410 if (ptn == NULL) {
411 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
412 return -1;
413 }
Dima Zavin214cc642009-01-26 11:16:21 -0800414 }
415
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800416 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800417 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
418 return -1;
419 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800420 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800421
422 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
423 dprintf(CRITICAL, "ERROR: Invaled boot image heador\n");
424 return -1;
425 }
426
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800427 if (hdr->page_size != page_size) {
428 dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
429 return -1;
430 }
431
432 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800433 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
434 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
435 return -1;
436 }
437 offset += n;
438
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800439 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800440 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
441 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
442 return -1;
443 }
444 offset += n;
445
David Ng183a7422009-12-07 14:55:21 -0800446continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800447 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
448 hdr->kernel_size);
449 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
450 hdr->ramdisk_size);
451
452 if(hdr->cmdline[0]) {
453 cmdline = (char*) hdr->cmdline;
454 } else {
455 cmdline = DEFAULT_CMDLINE;
456 }
457 dprintf(INFO, "cmdline = '%s'\n", cmdline);
458
459 /* TODO: create/pass atags to kernel */
460
461 dprintf(INFO, "\nBooting Linux\n");
462 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800463 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800464 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
465
466 return 0;
467}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800468
469void cmd_boot(const char *arg, void *data, unsigned sz)
470{
471 unsigned kernel_actual;
472 unsigned ramdisk_actual;
473 static struct boot_img_hdr hdr;
474 char *ptr = ((char*) data);
475
476 if (sz < sizeof(hdr)) {
477 fastboot_fail("invalid bootimage header");
478 return;
479 }
480
481 memcpy(&hdr, data, sizeof(hdr));
482
483 /* ensure commandline is terminated */
484 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
485
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -0700486 if(target_is_emmc_boot() && hdr.page_size) {
487 page_size = hdr.page_size;
488 page_mask = page_size - 1;
489 }
490
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800491 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
492 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800493
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700494 if (page_size + kernel_actual + ramdisk_actual < sz) {
Brian Swetland9c4c0752009-01-25 16:23:50 -0800495 fastboot_fail("incomplete bootimage");
496 return;
497 }
498
Chandan Uddaraju1f6030f2010-03-19 13:26:38 -0700499 memmove((void*) KERNEL_ADDR, ptr + page_size, hdr.kernel_size);
500 memmove((void*) RAMDISK_ADDR, ptr + page_size + kernel_actual, hdr.ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800501
502 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800503 target_battery_charging_enable(0, 1);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800504 udc_stop();
505
Brian Swetland9c4c0752009-01-25 16:23:50 -0800506 boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800507 (const char*) hdr.cmdline, board_machtype(),
Brian Swetland9c4c0752009-01-25 16:23:50 -0800508 (void*) RAMDISK_ADDR, hdr.ramdisk_size);
509}
510
Dima Zavin214cc642009-01-26 11:16:21 -0800511void cmd_erase(const char *arg, void *data, unsigned sz)
512{
513 struct ptentry *ptn;
514 struct ptable *ptable;
515
516 ptable = flash_get_ptable();
517 if (ptable == NULL) {
518 fastboot_fail("partition table doesn't exist");
519 return;
520 }
521
522 ptn = ptable_find(ptable, arg);
523 if (ptn == NULL) {
524 fastboot_fail("unknown partition name");
525 return;
526 }
527
528 if (flash_erase(ptn)) {
529 fastboot_fail("failed to erase partition");
530 return;
531 }
532 fastboot_okay("");
533}
534
Bikas Gurungd48bd242010-09-04 19:54:32 -0700535
536void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
537{
538 unsigned long long ptn = 0;
539 unsigned int out[512] = {0};
540
541 ptn = mmc_ptn_offset(arg);
542 if(ptn == 0) {
543 fastboot_fail("partition table doesn't exist");
544 return;
545 }
546
547
548 /* Simple inefficient version of erase. Just writing
549 0 in first block */
550 if (mmc_write(ptn , 512, (unsigned int *)out)) {
551 fastboot_fail("failed to erase partition");
552 return;
553 }
554 fastboot_okay("");
555}
556
557
Shashank Mittal23b8f422010-04-16 19:27:21 -0700558void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
559{
560 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700561 unsigned long long size = 0;
562
Shashank Mittal23b8f422010-04-16 19:27:21 -0700563 ptn = mmc_ptn_offset(arg);
564 if(ptn == 0) {
565 fastboot_fail("partition table doesn't exist");
566 return;
567 }
568
569 if (!strcmp(arg, "boot") || !strcmp(arg, "recovery")) {
570 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
571 fastboot_fail("image is not a boot image");
572 return;
573 }
574 }
575
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -0700576 size = mmc_ptn_size(arg);
577 if (ROUND_TO_PAGE(sz,511) > size) {
578 fastboot_fail("size too large");
579 return;
580 }
581
Shashank Mittal23b8f422010-04-16 19:27:21 -0700582 if (mmc_write(ptn , sz, (unsigned int *)data)) {
583 fastboot_fail("flash write failure");
584 return;
585 }
586 fastboot_okay("");
587 return;
588}
589
Dima Zavin214cc642009-01-26 11:16:21 -0800590void cmd_flash(const char *arg, void *data, unsigned sz)
591{
592 struct ptentry *ptn;
593 struct ptable *ptable;
594 unsigned extra = 0;
595
596 ptable = flash_get_ptable();
597 if (ptable == NULL) {
598 fastboot_fail("partition table doesn't exist");
599 return;
600 }
601
602 ptn = ptable_find(ptable, arg);
603 if (ptn == NULL) {
604 fastboot_fail("unknown partition name");
605 return;
606 }
607
608 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
609 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
610 fastboot_fail("image is not a boot image");
611 return;
612 }
613 }
614
Chandan Uddarajud6d45042010-02-24 21:12:45 -0800615 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata")
616 || !strcmp(ptn->name, "persist"))
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800617 extra = ((page_size >> 9) * 16);
Dima Zavin214cc642009-01-26 11:16:21 -0800618 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800619 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800620
621 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
622 if (flash_write(ptn, extra, data, sz)) {
623 fastboot_fail("flash write failure");
624 return;
625 }
626 dprintf(INFO, "partition '%s' updated\n", ptn->name);
627 fastboot_okay("");
628}
629
630void cmd_continue(const char *arg, void *data, unsigned sz)
631{
632 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800633 target_battery_charging_enable(0, 1);
Dima Zavin214cc642009-01-26 11:16:21 -0800634 udc_stop();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700635 if (target_is_emmc_boot())
636 {
637 boot_linux_from_mmc();
638 }
639 else
640 {
641 boot_linux_from_flash();
642 }
Dima Zavin214cc642009-01-26 11:16:21 -0800643}
644
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800645void cmd_reboot(const char *arg, void *data, unsigned sz)
646{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700647 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800648 fastboot_okay("");
649 reboot_device(0);
650}
651
652void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
653{
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700654 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800655 fastboot_okay("");
656 reboot_device(FASTBOOT_MODE);
657}
658
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700659void splash_screen ()
660{
661 struct ptentry *ptn;
662 struct ptable *ptable;
663 struct fbcon_config *fb_display = NULL;
664
665 if (!target_is_emmc_boot())
666 {
667 ptable = flash_get_ptable();
668 if (ptable == NULL) {
669 dprintf(CRITICAL, "ERROR: Partition table not found\n");
670 return -1;
671 }
672
673 ptn = ptable_find(ptable, "splash");
674 if (ptn == NULL) {
675 dprintf(CRITICAL, "ERROR: No splash partition found\n");
676 } else {
677 fb_display = fbcon_display();
678 if (fb_display) {
679 if (flash_read(ptn, 0, fb_display->base,
680 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
681 fbcon_clear();
682 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
683 }
684 }
685 }
686 }
687}
688
Brian Swetland9c4c0752009-01-25 16:23:50 -0800689void aboot_init(const struct app_descriptor *app)
690{
Shashank Mittal4f99a882010-02-01 13:58:50 -0800691 unsigned reboot_mode = 0;
692 unsigned disp_init = 0;
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700693 unsigned usb_init = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -0700694
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700695 /* Setup page size information for nand/emmc reads */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700696 if (target_is_emmc_boot())
697 {
698 page_size = 2048;
699 page_mask = page_size - 1;
700 }
701 else
702 {
703 page_size = flash_page_size();
704 page_mask = page_size - 1;
705 }
706
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700707 /* Display splash screen if enabled */
708 #if DISPLAY_SPLASH_SCREEN
709 display_init();
710 dprintf(INFO, "Diplay initialized\n");
711 disp_init = 1;
Chandan Uddaraju40b227d2010-08-03 19:25:41 -0700712 diplay_image_on_screen();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700713 #endif
714
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800715 target_serialno(sn_buf);
716 dprintf(INFO,"sn %s\n",sn_buf);
717 surf_udc_device.serialno = sn_buf;
718
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700719 /* Check if we should do something other than booting up */
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700720 if (keys_get_state(KEY_HOME) != 0)
721 boot_into_recovery = 1;
Wentao Xu153902c2010-12-20 16:20:52 -0500722 if (keys_get_state(KEY_VOLUMEUP) != 0)
723 boot_into_recovery = 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700724 if (keys_get_state(KEY_BACK) != 0)
725 goto fastboot;
Wentao Xu153902c2010-12-20 16:20:52 -0500726 if (keys_get_state(KEY_VOLUMEDOWN) != 0)
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700727 goto fastboot;
728
729 #if NO_KEYPAD_DRIVER
730 /* With no keypad implementation, check the status of USB connection. */
731 /* If USB is connected then go into fastboot mode. */
732 usb_init = 1;
733 udc_init(&surf_udc_device);
734 if (usb_cable_status())
735 goto fastboot;
736 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -0700737
Ajay Dudani77421292010-10-27 19:34:06 -0700738 reboot_mode = check_reboot_mode();
739 if (reboot_mode == RECOVERY_MODE) {
740 boot_into_recovery = 1;
741 } else if(reboot_mode == FASTBOOT_MODE) {
742 goto fastboot;
743 }
744
Shashank Mittal23b8f422010-04-16 19:27:21 -0700745 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700746 {
747 boot_linux_from_mmc();
748 }
749 else
750 {
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700751 recovery_init();
752 boot_linux_from_flash();
753 }
Dima Zavinb4283602009-01-26 16:36:57 -0800754 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
755 "to fastboot mode.\n");
756
757fastboot:
Chandan Uddaraju2943fd62010-06-21 10:56:39 -0700758
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700759 if(!usb_init)
760 udc_init(&surf_udc_device);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800761
762 fastboot_register("boot", cmd_boot);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700763
Shashank Mittal23b8f422010-04-16 19:27:21 -0700764 if (target_is_emmc_boot())
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700765 {
766 fastboot_register("flash:", cmd_flash_mmc);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700767 fastboot_register("erase:", cmd_erase_mmc);
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700768 }
769 else
770 {
771 fastboot_register("flash:", cmd_flash);
Bikas Gurungd48bd242010-09-04 19:54:32 -0700772 fastboot_register("erase:", cmd_erase);
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700773 }
774
775 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800776 fastboot_register("reboot", cmd_reboot);
777 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700778 fastboot_publish("product", TARGET(BOARD));
Brian Swetland9c4c0752009-01-25 16:23:50 -0800779 fastboot_publish("kernel", "lk");
780
Ajay Dudanid43419e2010-02-10 13:17:39 -0800781 fastboot_init(target_get_scratch_address(), 120 * 1024 * 1024);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800782 udc_start();
Shashank Mittald8c42bf2010-06-09 15:44:28 -0700783 target_battery_charging_enable(1, 0);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800784}
785
786APP_START(aboot)
787 .init = aboot_init,
788APP_END
789