3eb6159800dd878f14489fb429646abb65139f4c
[kugel-rb.git] / bootloader / sansa_as3525.c
blob3eb6159800dd878f14489fb429646abb65139f4c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 by Rafaël Carré
12 * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing
13 * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
25 #include <stdio.h>
26 #include <system.h>
27 #include <inttypes.h>
28 #include "config.h"
29 #include "lcd.h"
30 #ifdef USE_ROCKBOX_USB
31 #include "usb.h"
32 #include "sysfont.h"
33 #endif /* USE_ROCKBOX_USB */
34 #include "backlight.h"
35 #include "button-target.h"
36 #include "common.h"
37 #include "storage.h"
38 #include "disk.h"
39 #include "panic.h"
40 #include "power.h"
42 int show_logo(void);
44 void main(void) __attribute__((noreturn));
45 void main(void)
47 unsigned char* loadbuffer;
48 int buffer_size;
49 void(*kernel_entry)(void);
50 int ret;
52 system_init();
53 kernel_init();
55 enable_irq();
57 lcd_init();
58 show_logo();
60 backlight_init();
62 button_init_device();
63 int btn = button_read_device();
65 #if !defined(SANSA_FUZE) && !defined(SANSA_CLIP) && !defined(SANSA_CLIPV2) \
66 && !defined(SANSA_CLIPPLUS)
67 if (button_hold())
69 verbose = true;
70 lcd_clear_display();
71 printf("Hold switch on");
72 printf("Shutting down...");
73 sleep(HZ);
74 power_off();
76 #endif
78 /* Enable bootloader messages if any button is pressed */
79 if (btn)
81 lcd_clear_display();
82 verbose = true;
85 ret = storage_init();
86 if(ret < 0)
87 error(EATA,ret);
89 #ifdef USE_ROCKBOX_USB
90 usb_init();
91 usb_start_monitoring();
92 if(usb_detect() == USB_INSERTED)
94 const char msg[] = "Bootloader USB mode";
95 reset_screen();
96 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * sizeof(msg))) / 2,
97 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
98 lcd_update();
100 while(usb_detect() == USB_INSERTED)
101 sleep(HZ);
103 reset_screen();
104 lcd_update();
106 #endif /* USE_ROCKBOX_USB */
108 if(!disk_init(IF_MV(0)))
109 panicf("disk_init failed!");
111 ret = disk_mount_all();
113 if(ret <= 0)
114 error(EDISK, ret);
116 printf("Loading firmware");
118 loadbuffer = (unsigned char*)DRAM_ORIG; /* DRAM */
119 buffer_size = (int)(loadbuffer + (DRAM_SIZE) - TTB_SIZE);
121 ret = load_firmware(loadbuffer, BOOTFILE, buffer_size);
122 if(ret < 0)
123 error(EBOOTFILE, ret);
125 if (ret == EOK)
127 kernel_entry = (void*) loadbuffer;
128 cpucache_invalidate();
129 printf("Executing");
130 kernel_entry();
131 printf("ERR: Failed to boot");
134 /* never returns */
135 while(1) ;