FS#11347 by me: *dir LUA functions: luadir module
[kugel-rb.git] / bootloader / sansa_as3525.c
blobe63bb2c7495ef51c109cdd75517d6ab9198e749f
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 "usb_core.h"
33 #include "sysfont.h"
34 #endif /* USE_ROCKBOX_USB */
35 #include "backlight.h"
36 #include "button-target.h"
37 #include "common.h"
38 #include "storage.h"
39 #include "disk.h"
40 #include "panic.h"
41 #include "power.h"
43 int show_logo(void);
45 void main(void) __attribute__((noreturn));
46 void main(void)
48 unsigned char* loadbuffer;
49 int buffer_size;
50 void(*kernel_entry)(void);
51 int ret;
53 system_init();
54 kernel_init();
56 enable_irq();
58 lcd_init();
59 show_logo();
61 backlight_init();
63 button_init_device();
64 int btn = button_read_device();
66 #if !defined(SANSA_FUZE) && !defined(SANSA_CLIP) && !defined(SANSA_CLIPV2) \
67 && !defined(SANSA_CLIPPLUS)
68 if (button_hold())
70 verbose = true;
71 lcd_clear_display();
72 printf("Hold switch on");
73 printf("Shutting down...");
74 sleep(HZ);
75 power_off();
77 #endif
79 /* Enable bootloader messages if any button is pressed */
80 if (btn)
82 lcd_clear_display();
83 verbose = true;
86 ret = storage_init();
87 if(ret < 0)
88 error(EATA,ret);
90 #ifdef USE_ROCKBOX_USB
91 usb_init();
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 usb_core_enable_driver(USB_DRIVER_MASS_STORAGE, true);
101 usb_enable(true);
103 while(usb_detect() == USB_INSERTED)
104 sleep(HZ);
106 usb_enable(false);
108 reset_screen();
109 lcd_update();
111 #endif /* USE_ROCKBOX_USB */
113 if(!disk_init(IF_MV(0)))
114 panicf("disk_init failed!");
116 ret = disk_mount_all();
118 if(ret <= 0)
119 error(EDISK, ret);
121 printf("Loading firmware");
123 loadbuffer = (unsigned char*)DRAM_ORIG; /* DRAM */
124 buffer_size = (int)(loadbuffer + (DRAM_SIZE) - TTB_SIZE);
126 ret = load_firmware(loadbuffer, BOOTFILE, buffer_size);
127 if(ret < 0)
128 error(EBOOTFILE, ret);
130 disable_irq(); /* disable irq until we have copied the new vectors */
132 if (ret == EOK)
134 kernel_entry = (void*) loadbuffer;
135 printf("Executing");
136 kernel_entry();
137 printf("ERR: Failed to boot");
140 /* never returns */
141 while(1) ;