blob: cbdc64a3d2ff058b36d43cd61a3a48aff37978bb [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
Brian Swetland9c4c0752009-01-25 16:23:50 -080046#include "bootimg.h"
47#include "fastboot.h"
48
Chandan Uddarajuda919832009-11-17 01:06:11 -080049#define DEFAULT_CMDLINE "mem=100M console=null";
Brian Swetland2defe162009-08-18 14:35:59 -070050
David Ng183a7422009-12-07 14:55:21 -080051#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080052#define RECOVERY_MODE 0x77665502
53#define FASTBOOT_MODE 0x77665500
54
David Ng183a7422009-12-07 14:55:21 -080055static const char *emmc_cmdline = " androidboot.emmc=true";
56
Brian Swetland9c4c0752009-01-25 16:23:50 -080057static struct udc_device surf_udc_device = {
58 .vendor_id = 0x18d1,
Chandan Uddarajuc53a1a12009-11-18 14:53:40 -080059 .product_id = 0xD00D,
Brian Swetland9c4c0752009-01-25 16:23:50 -080060 .version_id = 0x0100,
61 .manufacturer = "Google",
62 .product = "Android",
63};
64
Dima Zavin42168f22009-01-30 11:52:22 -080065struct atag_ptbl_entry
66{
67 char name[16];
68 unsigned offset;
69 unsigned size;
70 unsigned flags;
71};
72
Brian Swetland9c4c0752009-01-25 16:23:50 -080073void platform_uninit_timer(void);
Chandan Uddarajuc6860e12009-11-19 11:22:15 -080074unsigned* target_atag_mem(unsigned* ptr);
Chandan Uddaraju885e4db2009-12-03 22:45:26 -080075unsigned board_machtype(void);
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080076unsigned check_reboot_mode(void);
David Ng183a7422009-12-07 14:55:21 -080077int target_is_emmc_boot(void);
Chandan Uddaraju94183c02010-01-15 15:13:59 -080078void reboot_device(unsigned);
Brian Swetland9c4c0752009-01-25 16:23:50 -080079
Chandan Uddarajude85d3f2010-01-05 16:32:33 -080080static int boot_into_recovery = 0;
81
Dima Zavin42168f22009-01-30 11:52:22 -080082static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
83{
84 struct atag_ptbl_entry atag_ptn;
85
86 memcpy(atag_ptn.name, ptn->name, 16);
87 atag_ptn.name[15] = '\0';
88 atag_ptn.offset = ptn->start;
89 atag_ptn.size = ptn->length;
90 atag_ptn.flags = ptn->flags;
91 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
92 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
93}
Brian Swetland9c4c0752009-01-25 16:23:50 -080094
95void boot_linux(void *kernel, unsigned *tags,
96 const char *cmdline, unsigned machtype,
97 void *ramdisk, unsigned ramdisk_size)
98{
99 unsigned *ptr = tags;
100 void (*entry)(unsigned,unsigned,unsigned*) = kernel;
Dima Zavin42168f22009-01-30 11:52:22 -0800101 struct ptable *ptable;
David Ng183a7422009-12-07 14:55:21 -0800102 int cmdline_len = 0;
103 int have_cmdline = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800104
105 /* CORE */
106 *ptr++ = 2;
107 *ptr++ = 0x54410001;
108
109 if (ramdisk_size) {
110 *ptr++ = 4;
111 *ptr++ = 0x54420005;
Dima Zavin214cc642009-01-26 11:16:21 -0800112 *ptr++ = (unsigned)ramdisk;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800113 *ptr++ = ramdisk_size;
114 }
115
Chandan Uddarajuc6860e12009-11-19 11:22:15 -0800116 ptr = target_atag_mem(ptr);
117
David Ng183a7422009-12-07 14:55:21 -0800118 if (!target_is_emmc_boot()) {
119 /* Skip NAND partition ATAGS for eMMC boot */
120 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
121 int i;
122 *ptr++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
123 sizeof(unsigned)));
124 *ptr++ = 0x4d534d70;
125 for (i = 0; i < ptable->count; ++i)
126 ptentry_to_tag(&ptr, ptable_get(ptable, i));
127 }
Dima Zavin42168f22009-01-30 11:52:22 -0800128 }
129
Brian Swetland9c4c0752009-01-25 16:23:50 -0800130 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800131 cmdline_len = strlen(cmdline);
132 have_cmdline = 1;
133 }
134 if (target_is_emmc_boot()) {
135 cmdline_len += strlen(emmc_cmdline);
136 }
137 if (cmdline_len > 0) {
138 const char *src;
139 char *dst;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800140 unsigned n;
141 /* include terminating 0 and round up to a word multiple */
David Ng183a7422009-12-07 14:55:21 -0800142 n = (cmdline_len + 4) & (~3);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800143 *ptr++ = (n / 4) + 2;
144 *ptr++ = 0x54410009;
David Ng183a7422009-12-07 14:55:21 -0800145 dst = (char *)ptr;
146 if (have_cmdline) {
147 src = cmdline;
148 while ((*dst++ = *src++));
149 }
150 if (target_is_emmc_boot()) {
151 src = emmc_cmdline;
152 if (have_cmdline) --dst;
153 while ((*dst++ = *src++));
154 }
Brian Swetland9c4c0752009-01-25 16:23:50 -0800155 ptr += (n / 4);
156 }
157
158 /* END */
159 *ptr++ = 0;
160 *ptr++ = 0;
161
162 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d)\n",
163 kernel, ramdisk, ramdisk_size);
164 if (cmdline)
165 dprintf(INFO, "cmdline: %s\n", cmdline);
166
167 enter_critical_section();
168 platform_uninit_timer();
169 arch_disable_cache(UCACHE);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800170 arch_disable_mmu();
171
172 entry(0, machtype, tags);
173
174}
175
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800176unsigned page_size = 0;
177unsigned page_mask = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800178
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800179#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800180
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800181static unsigned char buf[4096]; //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -0800182
183int boot_linux_from_flash(void)
184{
185 struct boot_img_hdr *hdr = (void*) buf;
186 unsigned n;
187 struct ptentry *ptn;
188 struct ptable *ptable;
189 unsigned offset = 0;
190 const char *cmdline;
Shashank Mittal4f99a882010-02-01 13:58:50 -0800191 struct fbcon_config *fb_display = NULL;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800192
David Ng183a7422009-12-07 14:55:21 -0800193 if (target_is_emmc_boot()) {
194 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
195 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
196 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
197 return -1;
198 }
199 goto continue_boot;
200 }
201
Dima Zavin214cc642009-01-26 11:16:21 -0800202 ptable = flash_get_ptable();
203 if (ptable == NULL) {
204 dprintf(CRITICAL, "ERROR: Partition table not found\n");
205 return -1;
206 }
207
Shashank Mittal4f99a882010-02-01 13:58:50 -0800208#if DISPLAY_SPLASH_SCREEN
209 ptn = ptable_find(ptable, "splash");
210 if (ptn == NULL) {
211 dprintf(CRITICAL, "ERROR: No splash partition found\n");
212 } else {
213 fb_display = fbcon_display();
214 if (fb_display) {
215 if (flash_read(ptn, 0, fb_display->base,
216 (fb_display->width * fb_display->height * fb_display->bpp/8))) {
217 fbcon_clear();
218 dprintf(CRITICAL, "ERROR: Cannot read splash image\n");
219 }
220 }
221 }
222#endif
223
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800224 if(!boot_into_recovery)
225 {
226 ptn = ptable_find(ptable, "boot");
227 if (ptn == NULL) {
228 dprintf(CRITICAL, "ERROR: No boot partition found\n");
229 return -1;
230 }
231 }
232 else
233 {
234 ptn = ptable_find(ptable, "recovery");
235 if (ptn == NULL) {
236 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
237 return -1;
238 }
Dima Zavin214cc642009-01-26 11:16:21 -0800239 }
240
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800241 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -0800242 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
243 return -1;
244 }
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800245 offset += page_size;
Dima Zavin214cc642009-01-26 11:16:21 -0800246
247 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
248 dprintf(CRITICAL, "ERROR: Invaled boot image heador\n");
249 return -1;
250 }
251
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800252 if (hdr->page_size != page_size) {
253 dprintf(CRITICAL, "ERROR: Invaled boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
254 return -1;
255 }
256
257 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800258 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, n)) {
259 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
260 return -1;
261 }
262 offset += n;
263
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800264 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800265 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, n)) {
266 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
267 return -1;
268 }
269 offset += n;
270
David Ng183a7422009-12-07 14:55:21 -0800271continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -0800272 dprintf(INFO, "\nkernel @ %x (%d bytes)\n", hdr->kernel_addr,
273 hdr->kernel_size);
274 dprintf(INFO, "ramdisk @ %x (%d bytes)\n", hdr->ramdisk_addr,
275 hdr->ramdisk_size);
276
277 if(hdr->cmdline[0]) {
278 cmdline = (char*) hdr->cmdline;
279 } else {
280 cmdline = DEFAULT_CMDLINE;
281 }
282 dprintf(INFO, "cmdline = '%s'\n", cmdline);
283
284 /* TODO: create/pass atags to kernel */
285
286 dprintf(INFO, "\nBooting Linux\n");
287 boot_linux((void *)hdr->kernel_addr, (void *)TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800288 (const char *)cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -0800289 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
290
291 return 0;
292}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800293
294void cmd_boot(const char *arg, void *data, unsigned sz)
295{
296 unsigned kernel_actual;
297 unsigned ramdisk_actual;
298 static struct boot_img_hdr hdr;
299 char *ptr = ((char*) data);
300
301 if (sz < sizeof(hdr)) {
302 fastboot_fail("invalid bootimage header");
303 return;
304 }
305
306 memcpy(&hdr, data, sizeof(hdr));
307
308 /* ensure commandline is terminated */
309 hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
310
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800311 kernel_actual = ROUND_TO_PAGE(hdr.kernel_size, page_mask);
312 ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size, page_mask);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800313
314 if (2048 + kernel_actual + ramdisk_actual < sz) {
315 fastboot_fail("incomplete bootimage");
316 return;
317 }
318
319 memmove((void*) KERNEL_ADDR, ptr + 2048, hdr.kernel_size);
320 memmove((void*) RAMDISK_ADDR, ptr + 2048 + kernel_actual, hdr.ramdisk_size);
321
322 fastboot_okay("");
323 udc_stop();
324
Brian Swetland9c4c0752009-01-25 16:23:50 -0800325 boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR,
Chandan Uddaraju885e4db2009-12-03 22:45:26 -0800326 (const char*) hdr.cmdline, board_machtype(),
Brian Swetland9c4c0752009-01-25 16:23:50 -0800327 (void*) RAMDISK_ADDR, hdr.ramdisk_size);
328}
329
Dima Zavin214cc642009-01-26 11:16:21 -0800330void cmd_erase(const char *arg, void *data, unsigned sz)
331{
332 struct ptentry *ptn;
333 struct ptable *ptable;
334
335 ptable = flash_get_ptable();
336 if (ptable == NULL) {
337 fastboot_fail("partition table doesn't exist");
338 return;
339 }
340
341 ptn = ptable_find(ptable, arg);
342 if (ptn == NULL) {
343 fastboot_fail("unknown partition name");
344 return;
345 }
346
347 if (flash_erase(ptn)) {
348 fastboot_fail("failed to erase partition");
349 return;
350 }
351 fastboot_okay("");
352}
353
354void cmd_flash(const char *arg, void *data, unsigned sz)
355{
356 struct ptentry *ptn;
357 struct ptable *ptable;
358 unsigned extra = 0;
359
360 ptable = flash_get_ptable();
361 if (ptable == NULL) {
362 fastboot_fail("partition table doesn't exist");
363 return;
364 }
365
366 ptn = ptable_find(ptable, arg);
367 if (ptn == NULL) {
368 fastboot_fail("unknown partition name");
369 return;
370 }
371
372 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
373 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
374 fastboot_fail("image is not a boot image");
375 return;
376 }
377 }
378
379 if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata"))
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800380 extra = ((page_size >> 9) * 16);
Dima Zavin214cc642009-01-26 11:16:21 -0800381 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800382 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -0800383
384 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
385 if (flash_write(ptn, extra, data, sz)) {
386 fastboot_fail("flash write failure");
387 return;
388 }
389 dprintf(INFO, "partition '%s' updated\n", ptn->name);
390 fastboot_okay("");
391}
392
393void cmd_continue(const char *arg, void *data, unsigned sz)
394{
395 fastboot_okay("");
396 udc_stop();
397
398 boot_linux_from_flash();
399}
400
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800401void cmd_reboot(const char *arg, void *data, unsigned sz)
402{
403 dprintf(INFO, "rebooting the device\n");
404 fastboot_okay("");
405 reboot_device(0);
406}
407
408void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
409{
410 dprintf(INFO, "rebooting the device\n");
411 fastboot_okay("");
412 reboot_device(FASTBOOT_MODE);
413}
414
Brian Swetland9c4c0752009-01-25 16:23:50 -0800415void aboot_init(const struct app_descriptor *app)
416{
Shashank Mittal4f99a882010-02-01 13:58:50 -0800417 unsigned reboot_mode = 0;
418 unsigned disp_init = 0;
419 #if DISPLAY_SPLASH_SCREEN
420 display_init();
421 dprintf(INFO, "Diplay initialized\n");
422 disp_init = 1;
423 #endif
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800424 page_size = flash_page_size();
425 page_mask = page_size - 1;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800426 if (keys_get_state(KEY_HOME) != 0)
427 boot_into_recovery = 1;
Dima Zavinb4283602009-01-26 16:36:57 -0800428 if (keys_get_state(KEY_BACK) != 0)
429 goto fastboot;
Chandan Uddaraju26a56902010-01-13 18:26:56 -0800430 if (keys_get_state(KEY_CLEAR) != 0)
431 goto fastboot;
Dima Zavinb4283602009-01-26 16:36:57 -0800432
Chandan Uddarajude85d3f2010-01-05 16:32:33 -0800433 reboot_mode = check_reboot_mode();
434 if (reboot_mode == RECOVERY_MODE){
435 boot_into_recovery = 1;
436 }else if(reboot_mode == FASTBOOT_MODE){
437 goto fastboot;
438 }
439
Dima Zavinb4283602009-01-26 16:36:57 -0800440 boot_linux_from_flash();
441 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
442 "to fastboot mode.\n");
443
444fastboot:
Shashank Mittal4f99a882010-02-01 13:58:50 -0800445 if(!disp_init) {
446 display_init();
447 } else {
448 fbcon_clear();
449 }
Chandan Uddaraju24120502009-12-12 19:17:04 -0800450 dprintf(INFO, "Diplay initialized\n");
Brian Swetland9c4c0752009-01-25 16:23:50 -0800451 udc_init(&surf_udc_device);
452
453 fastboot_register("boot", cmd_boot);
Dima Zavin214cc642009-01-26 11:16:21 -0800454 fastboot_register("erase:", cmd_erase);
455 fastboot_register("flash:", cmd_flash);
456 fastboot_register("continue", cmd_continue);
Chandan Uddaraju94183c02010-01-15 15:13:59 -0800457 fastboot_register("reboot", cmd_reboot);
458 fastboot_register("reboot-bootloader", cmd_reboot_bootloader);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800459 fastboot_publish("product", "swordfish");
460 fastboot_publish("kernel", "lk");
461
Brian Swetland2defe162009-08-18 14:35:59 -0700462 fastboot_init((void*) SCRATCH_ADDR, 100 * 1024 * 1024);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800463 udc_start();
464}
465
466APP_START(aboot)
467 .init = aboot_init,
468APP_END
469