blob: d38639bfd439eac82c17141f0621965ade8c6ed5 [file] [log] [blame]
Solomon Peachy06627932018-06-28 06:24:26 -04001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2016 by Roman Stolyarov
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "config.h"
23#include "jz4760b.h"
24#include "../kernel-internal.h"
25#include "backlight.h"
26#include "font.h"
27#include "lcd.h"
28#include "file.h"
29#include "usb.h"
30#include "system.h"
31#include "button.h"
32#include "common.h"
33#include "rb-loader.h"
34#include "loader_strerror.h"
35#include "storage.h"
36#include "file_internal.h"
37#include "disk.h"
38#include "string.h"
39#include "adc.h"
40#include "version.h"
41
42#include "xdebug.h"
43
44extern void show_logo(void);
45extern void power_off(void);
46
Solomon Peachy7e7ca0c2018-12-29 10:37:47 -050047#ifdef HAVE_BOOTLOADER_USB_MODE
Solomon Peachy06627932018-06-28 06:24:26 -040048static void show_splash(int timeout, const char *msg)
49{
50 reset_screen();
51 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2,
52 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
53 lcd_update();
54
55 sleep(timeout);
56}
57
58static void usb_mode(void)
59{
60 int button;
61
62 /* Init USB */
63 usb_init();
64 usb_start_monitoring();
65
66 /* Wait for threads to connect */
67 show_splash(HZ/2, "Waiting for USB");
68
69 while (1)
70 {
71 button = button_get_w_tmo(HZ/2);
72
73 if (button == SYS_USB_CONNECTED)
74 break; /* Hit */
75 }
76
77 if (button == SYS_USB_CONNECTED)
78 {
79 /* Got the message - wait for disconnect */
80 show_splash(0, "Bootloader USB mode");
81
82 usb_acknowledge(SYS_USB_CONNECTED_ACK);
83
84 while (1)
85 {
86 button = button_get(true);
87 if (button == SYS_USB_DISCONNECTED)
88 break;
89 }
90 }
91}
Solomon Peachy7e7ca0c2018-12-29 10:37:47 -050092#endif
Solomon Peachy06627932018-06-28 06:24:26 -040093
94static int boot_rockbox(void)
95{
96 int rc;
97 void (*kernel_entry)(void);
98
99 printf("Mounting disk...\n");
Solomon Peachy7e7ca0c2018-12-29 10:37:47 -0500100
101 while((rc = disk_mount_all()) <= 0)
Solomon Peachy06627932018-06-28 06:24:26 -0400102 {
103 verbose = true;
Solomon Peachy7e7ca0c2018-12-29 10:37:47 -0500104#ifdef HAVE_BOOTLOADER_USB_MODE
105 error(EDISK, rc, false);
106 usb_start_monitoring();
107 usb_mode();
108#else
109 error(EDISK, rc, true);
110#endif
Solomon Peachy06627932018-06-28 06:24:26 -0400111 }
112
113 printf("Loading firmware...\n");
114 rc = load_firmware((unsigned char *)CONFIG_SDRAM_START, BOOTFILE, 0x400000);
115 if(rc <= EFILE_EMPTY)
116 return rc;
117 else
118 {
119 printf("Starting Rockbox...\n");
120 adc_close(); /* Disable SADC, seems to fix the re-init Rockbox does */
121
122 disable_interrupt();
123 kernel_entry = (void*) CONFIG_SDRAM_START;
124 kernel_entry();
125
126 return 0; /* Shouldn't happen */
127 }
128}
129
Solomon Peachy7e7ca0c2018-12-29 10:37:47 -0500130#if 0
Solomon Peachy06627932018-06-28 06:24:26 -0400131static void reset_configuration(void)
132{
133 int rc;
134
135 rc = disk_mount_all();
136 if (rc <= 0)
137 {
138 verbose = true;
139 error(EDISK,rc, true);
140 }
141
142 if(rename(ROCKBOX_DIR "/config.cfg", ROCKBOX_DIR "/config.old") == 0)
143 show_splash(HZ/2, "Configuration reset successfully!");
144 else
145 show_splash(HZ/2, "Couldn't reset configuration!");
146}
Solomon Peachy7e7ca0c2018-12-29 10:37:47 -0500147#endif
Solomon Peachy06627932018-06-28 06:24:26 -0400148
149int main(void)
150{
151 int rc;
152
153 serial_puts("\n\nSPL Stage 2\n\n");
154
155 kernel_init();
156 lcd_init();
157 font_init();
158 lcd_setfont(FONT_SYSFIXED);
159 button_init();
160 backlight_init();
161
162 show_logo();
163
Solomon Peachy06627932018-06-28 06:24:26 -0400164 rc = storage_init();
165 if(rc)
166 {
167 verbose = true;
168 error(EATA, rc, true);
169 }
170
Solomon Peachy7e7ca0c2018-12-29 10:37:47 -0500171 filesystem_init();
172
173#ifdef HAVE_BOOTLOADER_USB_MODE
174 button_init_device();
175 int btn = button_read_device();
176
177 usb_init();
178
179 /* Enter USB mode if USB is plugged and PLAY button is pressed */
180 if(btn & BUTTON_PLAY) {
181 usb_start_monitoring();
182 if(usb_detect() == USB_INSERTED)
183 usb_mode();
184 }
185#endif /* HAVE_BOOTLOADER_USB_MODE */
186
Solomon Peachy06627932018-06-28 06:24:26 -0400187 /* Don't mount the disks yet, there could be file system/partition errors
188 which are fixable in USB mode */
189
190 reset_screen();
191
192 printf(MODEL_NAME" Rockbox Bootloader\n");
193 printf("Version %s\n", rbversion);
194
195 rc = boot_rockbox();
196
197 if(rc <= EFILE_EMPTY)
198 {
199 verbose = true;
200 printf("Error: %s", loader_strerror(rc));
201 }
202
203 /* Halt */
204 while (1)
205 core_idle();
206
207 return 0;
208}