blob: de5e509048ed0d4173bba84cad081bc60d2f7d71 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Shashank Mittal4f99a882010-02-01 13:58:50 -08005 * Copyright (c) 2009-2010, Code Aurora Forum. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of Code Aurora nor
15 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
36#include <dev/udc.h>
37#include <string.h>
38#include <kernel/thread.h>
39#include <arch/ops.h>
40
Dima Zavin214cc642009-01-26 11:16:21 -080041#include <dev/flash.h>
42#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080043#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080044#include <dev/fbcon.h>
Dima Zavin214cc642009-01-26 11:16:21 -080045
Shashank Mittal024c0332010-02-03 11:44:00 -080046#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080047#include "bootimg.h"
48#include "fastboot.h"
49
Chandan Uddarajuda919832009-11-17 01:06:11 -080050#define DEFAULT_CMDLINE "mem=100M console=null";
Brian Swetland2defe162009-08-18 14:35:59 -070051
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080052#ifdef MEMBASE
53#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
54#else
David Ng183a7422009-12-07 14:55:21 -080055#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -080056#endif
57
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080058#define RECOVERY_MODE 0x77665502
59#define FASTBOOT_MODE 0x77665500
60
David Ng183a7422009-12-07 14:55:21 -080061static const char *emmc_cmdline = " androidboot.emmc=true";
62
Brian Swetland9c4c0752009-01-25 16:23:50 -080063static struct udc_device surf_udc_device = {
64 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080065 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080066 .version_id = 0x0100,
67 .manufacturer = "Google",
68 .product = "Android",
69};
70
Dima Zavin42168f22009-01-30 11:52:22 -080071struct atag_ptbl_entry
72{
73 char name[16];
74 unsigned offset;
75 unsigned size;
76 unsigned flags;
77};
78
Brian Swetland9c4c0752009-01-25 16:23:50 -080079void platform_uninit_timer(void);
Chandan Uddarajuc6860e12009-11-19 11:22:15 -080080unsigned* target_atag_mem(unsigned* ptr);
Chandan Uddaraju885e4db2009-12-03 22:45:26 -080081unsigned board_machtype(void);
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080082unsigned check_reboot_mode(void);
David Ng3679bc52010-02-09 15:43:43 -080083void *target_get_scratch_address(void);
David Ng183a7422009-12-07 14:55:21 -080084int target_is_emmc_boot(void);
Chandan Uddaraju94183c02010-01-15 15:13:59 -080085void reboot_device(unsigned);
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -080086void target_battery_charging_enable(unsigned enable, unsigned disconnect);
Brian Swetland9c4c0752009-01-25 16:23:50 -080087
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080088
Dima Zavin42168f22009-01-30 11:52:22 -080089static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
90{
91 struct atag_ptbl_entry atag_ptn;
92
93 memcpy(atag_ptn.name, ptn->name, 16);
94 atag_ptn.name[15] = '\0';
95 atag_ptn.offset = ptn->start;
96 atag_ptn.size = ptn->length;
97 atag_ptn.flags = ptn->flags;
98 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
99 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
100}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800101
102void boot_linux(void *kernel, unsigned *tags,
103 const char *cmdline, unsigned machtype,
104 void *ramdisk, unsigned ramdisk_size)
105{
106 unsigned *ptr = tags;
107 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800108 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800109 int cmdline_len = 0;
110 int have_cmdline = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800111
112 /* CORE */
113 *ptr++ = 2;
114 *ptr++ = 0x54410001;
115
116 if (ramdisk_size) {
117 *ptr++ = 4;
118 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800119 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800120 *ptr++ = ramdisk_size;
121 }
122
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800123 ptr = target_atag_mem(ptr);
124
David Ng183a7422009-12-07 14:55:21 -0800125 if (!target_is_emmc_boot()) {
126 /* Skip NAND partition ATAGS for eMMC boot */
127 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
128 int i;
129 *ptr++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
130 sizeof(unsigned)));
131 *ptr++ = 0x4d534d70;
132 for (i = 0; i < ptable->count; ++i)
133 ptentry_to_tag(&ptr, ptable_get(ptable, i));
134 }
Dima Zavin42168f22009-01-30 11:52:22 -0800135 }
136
Brian Swetland9c4c0752009-01-25 16:23:50 -0800137 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800138 cmdline_len = strlen(cmdline);
139 have_cmdline = 1;
140 }
141 if (target_is_emmc_boot()) {
142 cmdline_len += strlen(emmc_cmdline);
143 }
144 if (cmdline_len > 0) {
145 const char *src;
146 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800147 unsigned n;
148 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800149 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800150 *ptr++ = (n / 4) + 2;
151 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800152 dst = (char *)ptr;
153 if (have_cmdline) {
154 src = cmdline;
155 while ((*dst++ = *src++));
156 }
157 if (target_is_emmc_boot()) {
158 src = emmc_cmdline;
159 if (have_cmdline) --dst;
160 while ((*dst++ = *src++));
161 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800162 ptr += (n / 4);
163 }
164
165 /* END */
166 *ptr++ = 0;
167 *ptr++ = 0;
168
169 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
170 kernel, ramdisk, ramdisk_size);
171 if (cmdline)
172 dprintf(INFO, "cmdline: %s\n", cmdline);
173
174 enter_critical_section();
175 platform_uninit_timer();
176 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800177 arch_disable_mmu();
178
179 entry(0, machtype, tags);
180
181}
182
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800183unsigned page_size = 0;
184unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800185
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800186#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800187
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800188static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800189
190int boot_linux_from_flash(void)
191{
192 struct boot_img_hdr *hdr = (void*) buf;
193 unsigned n;
194 struct ptentry *ptn;
195 struct ptable *ptable;
196 unsigned offset = 0;
197 const char *cmdline;
Shashank Mittal4f99a882010-02-01 13:58:50 -0800198 struct fbcon_config *fb_display = NULL;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800199
David Ng183a7422009-12-07 14:55:21 -0800200 if (target_is_emmc_boot()) {
201 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
202 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
203 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
204 return -1;
205 }
206 goto continue_boot;
207 }
208
Dima Zavin214cc642009-01-26 11:16:21 -0800209 ptable = flash_get_ptable();
210 if (ptable == NULL) {
211 dprintf(CRITICAL, "ERROR: Partition table not found\n");
212 return -1;
213 }
214
Shashank Mittal4f99a882010-02-01 13:58:50 -0800215#if DISPLAY_SPLASH_SCREEN
216 ptn = ptable_find(ptable, "splash");
217 if (ptn == NULL) {
218 dprintf(CRITICAL, "ERROR: No splash partition found\n");
219 } else {
220 fb_display = fbcon_display();
221 if (fb_display) {
222 if (flash_read(ptn, 0, fb_display->base,
223 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
224 fbcon_clear();
225 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
226 }
227 }
228 }
229#endif
230
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800231 if(!boot_into_recovery)
232 {
233 ptn = ptable_find(ptable, "boot");
234 if (ptn == NULL) {
235 dprintf(CRITICAL, "ERROR: No boot partition found\n");
236 return -1;
237 }
238 }
239 else
240 {
241 ptn = ptable_find(ptable, "recovery");
242 if (ptn == NULL) {
243 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
244 return -1;
245 }
Dima Zavin214cc642009-01-26 11:16:21 -0800246 }
247
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800248 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800249 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
250 return -1;
251 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800252 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800253
254 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
255 dprintf(CRITICAL, "ERROR: Invaled boot image heador\n");
256 return -1;
257 }
258
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800259 if (hdr->page_size != page_size) {
260 dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
261 return -1;
262 }
263
264 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800265 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
266 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
267 return -1;
268 }
269 offset += n;
270
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800271 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800272 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
273 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
274 return -1;
275 }
276 offset += n;
277
David Ng183a7422009-12-07 14:55:21 -0800278continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800279 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
280 hdr->kernel_size);
281 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
282 hdr->ramdisk_size);
283
284 if(hdr->cmdline[0]) {
285 cmdline = (char*) hdr->cmdline;
286 } else {
287 cmdline = DEFAULT_CMDLINE;
288 }
289 dprintf(INFO, "cmdline = '%s'\n", cmdline);
290
291 /* TODO: create/pass atags to kernel */
292
293 dprintf(INFO, "\nBooting Linux\n");
294 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800295 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800296 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
297
298 return 0;
299}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800300
301void cmd_boot(const char *arg, void *data, unsigned sz)
302{
303 unsigned kernel_actual;
304 unsigned ramdisk_actual;
305 static struct boot_img_hdr hdr;
306 char *ptr = ((char*) data);
307
308 if (sz < sizeof(hdr)) {
309 fastboot_fail("invalid bootimage header");
310 return;
311 }
312
313 memcpy(&hdr, data, sizeof(hdr));
314
315 /* ensure commandline is terminated */
316 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
317
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800318 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
319 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800320
321 if (2048 + kernel_actual + ramdisk_actual < sz) {
322 fastboot_fail("incomplete bootimage");
323 return;
324 }
325
326 memmove((void*) KERNEL_ADDR, ptr + 2048, hdr.kernel_size);
327 memmove((void*) RAMDISK_ADDR, ptr + 2048 + kernel_actual, hdr.ramdisk_size);
328
329 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800330 target_battery_charging_enable(0, 1);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800331 udc_stop();
332
Brian Swetland9c4c0752009-01-25 16:23:50 -0800333 boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800334 (const char*) hdr.cmdline, board_machtype(),
Brian Swetland9c4c0752009-01-25 16:23:50 -0800335 (void*) RAMDISK_ADDR, hdr.ramdisk_size);
336}
337
Dima Zavin214cc642009-01-26 11:16:21 -0800338void cmd_erase(const char *arg, void *data, unsigned sz)
339{
340 struct ptentry *ptn;
341 struct ptable *ptable;
342
343 ptable = flash_get_ptable();
344 if (ptable == NULL) {
345 fastboot_fail("partition table doesn't exist");
346 return;
347 }
348
349 ptn = ptable_find(ptable, arg);
350 if (ptn == NULL) {
351 fastboot_fail("unknown partition name");
352 return;
353 }
354
355 if (flash_erase(ptn)) {
356 fastboot_fail("failed to erase partition");
357 return;
358 }
359 fastboot_okay("");
360}
361
362void cmd_flash(const char *arg, void *data, unsigned sz)
363{
364 struct ptentry *ptn;
365 struct ptable *ptable;
366 unsigned extra = 0;
367
368 ptable = flash_get_ptable();
369 if (ptable == NULL) {
370 fastboot_fail("partition table doesn't exist");
371 return;
372 }
373
374 ptn = ptable_find(ptable, arg);
375 if (ptn == NULL) {
376 fastboot_fail("unknown partition name");
377 return;
378 }
379
380 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
381 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
382 fastboot_fail("image is not a boot image");
383 return;
384 }
385 }
386
387 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata"))
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800388 extra = ((page_size >> 9) * 16);
Dima Zavin214cc642009-01-26 11:16:21 -0800389 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800390 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800391
392 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
393 if (flash_write(ptn, extra, data, sz)) {
394 fastboot_fail("flash write failure");
395 return;
396 }
397 dprintf(INFO, "partition '%s' updated\n", ptn->name);
398 fastboot_okay("");
399}
400
401void cmd_continue(const char *arg, void *data, unsigned sz)
402{
403 fastboot_okay("");
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800404 target_battery_charging_enable(0, 1);
Dima Zavin214cc642009-01-26 11:16:21 -0800405 udc_stop();
406
407 boot_linux_from_flash();
408}
409
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800410void cmd_reboot(const char *arg, void *data, unsigned sz)
411{
412 dprintf(INFO, "rebooting the device\n");
413 fastboot_okay("");
414 reboot_device(0);
415}
416
417void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
418{
419 dprintf(INFO, "rebooting the device\n");
420 fastboot_okay("");
421 reboot_device(FASTBOOT_MODE);
422}
423
Brian Swetland9c4c0752009-01-25 16:23:50 -0800424void aboot_init(const struct app_descriptor *app)
425{
Shashank Mittal4f99a882010-02-01 13:58:50 -0800426 unsigned reboot_mode = 0;
427 unsigned disp_init = 0;
428 #if DISPLAY_SPLASH_SCREEN
429 display_init();
430 dprintf(INFO, "Diplay initialized\n");
431 disp_init = 1;
432 #endif
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800433 page_size = flash_page_size();
434 page_mask = page_size - 1;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800435 if (keys_get_state(KEY_HOME) != 0)
436 boot_into_recovery = 1;
Dima Zavinb4283602009-01-26 16:36:57 -0800437 if (keys_get_state(KEY_BACK) != 0)
438 goto fastboot;
Chandan Uddaraju26a56902010-01-13 18:26:56 -0800439 if (keys_get_state(KEY_CLEAR) != 0)
440 goto fastboot;
Dima Zavinb4283602009-01-26 16:36:57 -0800441
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800442 reboot_mode = check_reboot_mode();
443 if (reboot_mode == RECOVERY_MODE){
444 boot_into_recovery = 1;
445 }else if(reboot_mode == FASTBOOT_MODE){
446 goto fastboot;
447 }
Shashank Mittal024c0332010-02-03 11:44:00 -0800448 recovery_init();
Dima Zavinb4283602009-01-26 16:36:57 -0800449 boot_linux_from_flash();
450 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
451 "to fastboot mode.\n");
452
453fastboot:
Shashank Mittal4f99a882010-02-01 13:58:50 -0800454 if(!disp_init) {
455 display_init();
456 } else {
457 fbcon_clear();
458 }
Chandan Uddaraju24120502009-12-12 19:17:04 -0800459 dprintf(INFO, "Diplay initialized\n");
Brian Swetland9c4c0752009-01-25 16:23:50 -0800460 udc_init(&surf_udc_device);
461
462 fastboot_register("boot", cmd_boot);
Dima Zavin214cc642009-01-26 11:16:21 -0800463 fastboot_register("erase:", cmd_erase);
464 fastboot_register("flash:", cmd_flash);
465 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800466 fastboot_register("reboot", cmd_reboot);
467 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800468 fastboot_publish("product", "swordfish");
469 fastboot_publish("kernel", "lk");
470
Ajay Dudanid43419e2010-02-10 13:17:39 -0800471 fastboot_init(target_get_scratch_address(), 120 * 1024 * 1024);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800472 udc_start();
Chandan Uddaraju7f5b9012010-02-06 16:37:48 -0800473 target_battery_charging_enable(1, 0);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800474}
475
476APP_START(aboot)
477 .init = aboot_init,
478APP_END
479