Make it work for files also
[kugel-rb.git] / bootloader / sansa_as3525.c
blobd8dc3f3936e19f457dd493cd50d685062bbfd170
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 #ifdef USE_ROCKBOX_USB
45 static void usb_mode(void)
47 if(usb_detect() != USB_INSERTED)
49 const char msg[] = "Plug USB cable";
50 reset_screen();
51 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * sizeof(msg))) / 2,
52 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
53 lcd_update();
55 /* wait until USB is plugged */
56 while(usb_detect() != USB_INSERTED) ;
59 const char msg[] = "Bootloader USB mode";
60 reset_screen();
61 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * sizeof(msg))) / 2,
62 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
63 lcd_update();
65 while(usb_detect() == USB_INSERTED)
66 sleep(HZ);
68 reset_screen();
69 lcd_update();
71 #endif /* USE_ROCKBOX_USB */
73 void main(void) __attribute__((noreturn));
74 void main(void)
76 unsigned char* loadbuffer;
77 int buffer_size;
78 void(*kernel_entry)(void);
79 int ret;
81 system_init();
82 kernel_init();
84 enable_irq();
86 lcd_init();
87 show_logo();
89 backlight_init();
91 button_init_device();
92 int btn = button_read_device();
94 #if !defined(SANSA_FUZE) && !defined(SANSA_CLIP) && !defined(SANSA_CLIPV2) \
95 && !defined(SANSA_CLIPPLUS)
96 if (button_hold())
98 verbose = true;
99 lcd_clear_display();
100 printf("Hold switch on");
101 printf("Shutting down...");
102 sleep(HZ);
103 power_off();
105 #endif
107 /* Enable bootloader messages if any button is pressed */
108 if (btn)
110 lcd_clear_display();
111 verbose = true;
114 ret = storage_init();
115 if(ret < 0)
116 error(EATA, ret, true);
118 #ifdef USE_ROCKBOX_USB
119 usb_init();
120 usb_start_monitoring();
122 /* Enter USB mode if USB is plugged and SELECT button is pressed */
123 if(btn & BUTTON_SELECT && usb_detect() == USB_INSERTED)
124 usb_mode();
125 #endif /* USE_ROCKBOX_USB */
127 while(!disk_init(IF_MV(0)))
128 #ifdef USE_ROCKBOX_USB
129 usb_mode();
130 #else
131 panicf("disk_init failed!");
132 #endif
134 while((ret = disk_mount_all()) <= 0)
136 #ifdef USE_ROCKBOX_USB
137 error(EDISK, ret, false);
138 usb_mode();
139 #else
140 error(EDISK, ret, true);
141 #endif
144 printf("Loading firmware");
146 loadbuffer = (unsigned char*)DRAM_ORIG; /* DRAM */
147 buffer_size = (int)(loadbuffer + (DRAM_SIZE) - TTB_SIZE);
149 while((ret = load_firmware(loadbuffer, BOOTFILE, buffer_size)) < 0)
151 #ifdef USE_ROCKBOX_USB
152 error(EBOOTFILE, ret, false);
153 usb_mode();
154 #else
155 error(EBOOTFILE, ret, true);
156 #endif
159 kernel_entry = (void*) loadbuffer;
160 cpucache_invalidate();
161 printf("Executing");
162 kernel_entry();
163 printf("ERR: Failed to boot");
165 /* never returns */
166 while(1) ;