Sansa AMS bootloader: enter USB mode only when needed
[kugel-rb.git] / bootloader / sansa_as3525.c
blob4219038d9ae0f0aaffd6c30467ae1f640537b90a
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 #include "usb.h"
31 #include "sysfont.h"
32 #include "backlight.h"
33 #include "button-target.h"
34 #include "common.h"
35 #include "storage.h"
36 #include "disk.h"
37 #include "panic.h"
38 #include "power.h"
40 int show_logo(void);
42 static void usb_mode(void)
44 if(usb_detect() != USB_INSERTED)
46 const char msg[] = "Plug USB cable";
47 reset_screen();
48 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * sizeof(msg))) / 2,
49 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
50 lcd_update();
52 /* wait until USB is plugged */
53 while(usb_detect() != USB_INSERTED) ;
56 const char msg[] = "Bootloader USB mode";
57 reset_screen();
58 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * sizeof(msg))) / 2,
59 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
60 lcd_update();
62 while(usb_detect() == USB_INSERTED)
63 sleep(HZ);
65 reset_screen();
66 lcd_update();
69 void main(void) __attribute__((noreturn));
70 void main(void)
72 unsigned char* loadbuffer;
73 int buffer_size;
74 void(*kernel_entry)(void);
75 int ret;
77 system_init();
78 kernel_init();
80 enable_irq();
82 lcd_init();
83 show_logo();
85 backlight_init();
87 button_init_device();
88 int btn = button_read_device();
90 #if !defined(SANSA_FUZE) && !defined(SANSA_CLIP) && !defined(SANSA_CLIPV2) \
91 && !defined(SANSA_CLIPPLUS)
92 if (button_hold())
94 verbose = true;
95 lcd_clear_display();
96 printf("Hold switch on");
97 printf("Shutting down...");
98 sleep(HZ);
99 power_off();
101 #endif
103 /* Enable bootloader messages if any button is pressed */
104 if (btn)
106 lcd_clear_display();
107 verbose = true;
110 ret = storage_init();
111 if(ret < 0)
112 error(EATA, ret, true);
114 usb_init();
115 usb_start_monitoring();
117 /* Enter USB mode if USB is plugged and SELECT button is pressed */
118 if(btn & BUTTON_SELECT && usb_detect() == USB_INSERTED)
119 usb_mode();
121 while(!disk_init(IF_MV(0)))
122 usb_mode();
124 while((ret = disk_mount_all()) <= 0)
126 error(EDISK, ret, false);
127 usb_mode();
130 printf("Loading firmware");
132 loadbuffer = (unsigned char*)DRAM_ORIG; /* DRAM */
133 buffer_size = (int)(loadbuffer + (DRAM_SIZE) - TTB_SIZE);
135 while((ret = load_firmware(loadbuffer, BOOTFILE, buffer_size)) < 0)
137 error(EBOOTFILE, ret, false);
138 usb_mode();
141 kernel_entry = (void*) loadbuffer;
142 cpucache_invalidate();
143 printf("Executing");
144 kernel_entry();
145 printf("ERR: Failed to boot");
147 /* never returns */
148 while(1) ;