dsp_arm: Fix up some .section directives to fix crash on app targets.
[maemo-rb.git] / bootloader / rk27xx.c
blob62280e274953e3f1caa9c6bba0478dd728c46521
1 #include <stdio.h>
2 #include <system.h>
3 #include <inttypes.h>
4 #include "config.h"
5 #include "gcc_extensions.h"
6 #include "lcd.h"
7 #include "font.h"
8 #include "backlight.h"
9 #include "adc.h"
10 #include "button-target.h"
11 #include "button.h"
12 #include "common.h"
13 #include "storage.h"
14 #include "disk.h"
15 #include "panic.h"
16 #include "power.h"
17 #include "string.h"
18 #include "file.h"
19 #include "crc32-rkw.h"
20 #include "rkw-loader.h"
21 #include "version.h"
23 /* beginning of DRAM */
24 #define DRAM_ORIG 0x60000000
26 /* bootloader code runs from 0x60700000
27 * so we cannot load more code to not overwrite ourself
29 #define LOAD_SIZE 0x700000
31 extern void show_logo( void );
33 /* This function setup bare minimum
34 * and jumps to rom in order to activate
35 * hardcoded rkusb mode
37 static void enter_rkusb(void)
39 asm volatile (
40 /* turn off cache */
41 "ldr r0, =0xefff0000 \n"
42 "ldrh r1, [r0] \n"
43 "strh r1, [r0] \n"
45 /* turn off interrupts */
46 "mrs r0, cpsr \n"
47 "bic r0, r0, #0x1f \n"
48 "orr r0, r0, #0xd3 \n"
49 "msr cpsr, r0 \n"
51 /* disable iram remap */
52 "mov r0, #0x18000000 \n"
53 "add r0, r0, #0x1c000 \n"
54 "mov r1, #0 \n"
55 "str r1, [r0, #4] \n"
57 /* setup stacks in unmapped
58 * iram just as rom will do
60 "msr cpsr, #0xd2 \n"
61 "ldr r1, =0x18200274 \n"
62 "add r1, r1, #0x200 \n"
63 "mov sp, r1 \n"
64 "msr cpsr, #0xd3 \n"
65 "add r1, r1, #0x400 \n"
66 "mov sp, r1 \n"
68 /* jump to main() in rom
69 * just before dfu handler
71 "ldr r0, =0xec0 \n"
72 "bx r0 \n"
76 void main(void) NORETURN_ATTR;
77 void main(void)
79 char filename[MAX_PATH];
80 unsigned char* loadbuffer;
81 void(*kernel_entry)(void);
82 int ret;
83 enum {rb, of} boot = rb;
85 power_init();
86 system_init();
87 kernel_init();
88 enable_irq();
90 adc_init();
91 lcd_init();
92 backlight_init();
93 button_init_device();
95 font_init();
96 lcd_setfont(FONT_SYSFIXED);
98 show_logo();
100 int btn = button_read_device();
102 /* if there is some other button pressed
103 * besides POWER/PLAY we boot into OF
105 if ((btn & ~POWEROFF_BUTTON))
106 boot = of;
108 /* if we are woken up by USB insert boot into OF */
109 if (DEV_INFO & (1<<20))
110 boot = of;
112 lcd_clear_display();
114 ret = storage_init();
115 if(ret < 0)
116 error(EATA, ret, true);
118 while(!disk_init(IF_MV(0)))
119 panicf("disk_init failed!");
121 while((ret = disk_mount_all()) <= 0)
122 error(EDISK, ret, true);
124 loadbuffer = (unsigned char*)DRAM_ORIG; /* DRAM */
126 if (boot == rb)
127 snprintf(filename,sizeof(filename), BOOTDIR "/%s", BOOTFILE);
128 else if (boot == of)
129 snprintf(filename,sizeof(filename), BOOTDIR "/%s", "BASE.RKW");
131 printf("Bootloader version: %s", RBVERSION);
132 printf("Loading: %s", filename);
134 ret = load_rkw(loadbuffer, filename, LOAD_SIZE);
135 if (ret < 0)
137 printf(rkw_strerror(ret));
138 lcd_update();
139 sleep(5*HZ);
141 /* if we boot rockbox we shutdown on error
142 * if we boot OF we fall back to rkusb mode on error
144 if (boot == rb)
146 power_off();
148 else
150 /* give visual feedback what we are doing */
151 printf("Entering rockchip USB mode...");
152 lcd_update();
154 enter_rkusb();
157 else
159 /* print 'Loading OK' */
160 printf(rkw_strerror(0));
161 sleep(HZ);
164 /* jump to entrypoint */
165 kernel_entry = (void*) loadbuffer;
166 commit_discard_idcache();
168 printf("Executing");
169 kernel_entry();
171 /* this should never be reached actually */
172 printf("ERR: Failed to boot");
173 sleep(5*HZ);
175 if (boot == rb)
177 power_off();
179 else
181 /* give visual feedback what we are doing */
182 printf("Entering rockchip USB mode...");
183 lcd_update();
185 enter_rkusb();
188 /* hang */
189 while(1);