Fix buffer size used for realpath() call
[maemo-rb.git] / bootloader / sansa_as3525.c
blob140fa02a7656898ac3a4fc4e7d45ffc9a00292c4
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 "gcc_extensions.h"
30 #include "lcd.h"
31 #ifdef USE_ROCKBOX_USB
32 #include "usb.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 #ifdef USE_ROCKBOX_USB
46 static void usb_mode(void)
48 if(usb_detect() != USB_INSERTED)
50 const char msg[] = "Plug USB cable";
51 reset_screen();
52 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * sizeof(msg))) / 2,
53 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
54 lcd_update();
56 /* wait until USB is plugged */
57 while(usb_detect() != USB_INSERTED) ;
60 const char msg[] = "Bootloader USB mode";
61 reset_screen();
62 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * sizeof(msg))) / 2,
63 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
64 lcd_update();
66 while(usb_detect() == USB_INSERTED)
67 sleep(HZ);
69 reset_screen();
70 lcd_update();
72 #endif /* USE_ROCKBOX_USB */
74 void main(void) NORETURN_ATTR;
75 void main(void)
77 unsigned char* loadbuffer;
78 int buffer_size;
79 void(*kernel_entry)(void);
80 int ret;
82 system_init();
83 kernel_init();
85 enable_irq();
87 lcd_init();
88 show_logo();
90 backlight_init();
92 button_init_device();
93 int btn = button_read_device();
95 #if !defined(SANSA_FUZE) && !defined(SANSA_CLIP) && !defined(SANSA_CLIPV2) \
96 && !defined(SANSA_CLIPPLUS)
97 if (button_hold())
99 verbose = true;
100 lcd_clear_display();
101 printf("Hold switch on");
102 printf("Shutting down...");
103 sleep(HZ);
104 power_off();
106 #endif
108 /* Enable bootloader messages if any button is pressed */
109 if (btn)
111 lcd_clear_display();
112 verbose = true;
115 ret = storage_init();
116 if(ret < 0)
117 error(EATA, ret, true);
119 #ifdef USE_ROCKBOX_USB
120 usb_init();
121 usb_start_monitoring();
123 /* Enter USB mode if USB is plugged and SELECT button is pressed */
124 if(btn & BUTTON_SELECT && usb_detect() == USB_INSERTED)
125 usb_mode();
126 #endif /* USE_ROCKBOX_USB */
128 while(!disk_init(IF_MV(0)))
129 #ifdef USE_ROCKBOX_USB
130 usb_mode();
131 #else
132 panicf("disk_init failed!");
133 #endif
135 while((ret = disk_mount_all()) <= 0)
137 #ifdef USE_ROCKBOX_USB
138 error(EDISK, ret, false);
139 usb_mode();
140 #else
141 error(EDISK, ret, true);
142 #endif
145 printf("Loading firmware");
147 loadbuffer = (unsigned char*)DRAM_ORIG; /* DRAM */
148 buffer_size = (int)(loadbuffer + (DRAM_SIZE) - TTB_SIZE);
150 while((ret = load_firmware(loadbuffer, BOOTFILE, buffer_size)) < 0)
152 #ifdef USE_ROCKBOX_USB
153 error(EBOOTFILE, ret, false);
154 usb_mode();
155 #else
156 error(EBOOTFILE, ret, true);
157 #endif
160 kernel_entry = (void*) loadbuffer;
161 cpucache_invalidate();
162 printf("Executing");
163 kernel_entry();
164 printf("ERR: Failed to boot");
166 /* never returns */
167 while(1) ;