blob: 3ca41d989cec40b6d6f3344bbd4ab15ecb7a5067 [file] [log] [blame]
Will Robertson590501c2007-09-21 15:51:53 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 by Greg White
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19#include "config.h"
20
21#include <stdlib.h>
22#include <stdio.h>
23#include "inttypes.h"
24#include "string.h"
25#include "cpu.h"
26#include "system.h"
27#include "lcd.h"
28#include "kernel.h"
29#include "thread.h"
30#include "ata.h"
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +000031#include "dir.h"
Will Robertson590501c2007-09-21 15:51:53 +000032#include "fat.h"
33#include "disk.h"
34#include "font.h"
35#include "adc.h"
36#include "backlight.h"
37#include "backlight-target.h"
38#include "button.h"
39#include "panic.h"
40#include "power.h"
41#include "file.h"
42#include "common.h"
43#include "rbunicode.h"
44#include "usb.h"
45#include "mmu-imx31.h"
46#include "lcd-target.h"
47#include "avic-imx31.h"
48#include <stdarg.h>
49
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000050#define TAR_CHUNK 512
51#define TAR_HEADER_SIZE 157
52
Will Robertson590501c2007-09-21 15:51:53 +000053char version[] = APPSVERSION;
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +000054char basedir[] = "/Content/0b00/00/"; /* Where files sent via MTP are stored */
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +000055int (*kernel_entry)(void);
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000056char *tarbuf = (char *)0x00000040;
Michael Sevakisa07c0342008-02-08 02:20:05 +000057extern void reference_system_c(void);
58
59/* Dummy stub that creates C references for C functions only used by
60 assembly - never called */
61void reference_files(void)
62{
63 reference_system_c();
64}
Will Robertson590501c2007-09-21 15:51:53 +000065
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000066void untar(int tar_fd)
67{
68 char header[TAR_HEADER_SIZE];
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000069 char *ptr;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000070 char path[102];
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000071 int fd, i, size = 0;
72 int ret;
73
74 ret = read(tar_fd, tarbuf, filesize(tar_fd));
75 if (ret < 0) {
76 printf("couldn't read tar file (%d)", ret);
77 return;
78 }
79 ptr = tarbuf;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000080
81 while (1)
82 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000083 memcpy(header, ptr, TAR_HEADER_SIZE);
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000084
85 if (*header == '\0') /* Check for EOF */
86 break;
87
88 /* Parse the size field */
89 size = 0;
90 for (i = 124 ; i < 124 + 11 ; i++) {
91 size = (8 * size) + header[i] - '0';
92 }
93
94 /* Skip rest of header */
Nicolas Pennequin90a3d582008-04-16 00:38:06 +000095 ptr += TAR_CHUNK;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +000096
97 /* Make the path absolute */
98 strcpy(path, "/");
99 strcat(path, header);
100
101 if (header[156] == '0') /* file */
102 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000103 int wc;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000104
105 fd = creat(path);
106 if (fd < 0)
107 {
108 printf("failed to create file (%d)", fd);
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000109 }
110 else
111 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000112 wc = write(fd, ptr, size);
113 if (wc < 0)
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000114 {
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000115 printf("write failed (%d)", wc);
116 break;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000117 }
118 close(fd);
119 }
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000120 ptr += (size + TAR_CHUNK-1) & (~(TAR_CHUNK-1));
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000121 }
122 else if (header[156] == '5') /* directory */
123 {
124 int ret;
125
126 /* Remove the trailing slash */
127 if (path[strlen(path) - 1] == '/')
128 path[strlen(path) - 1] = '\0';
129
130 /* Create the dir */
131 ret = mkdir(path);
132 if (ret < 0 && ret != -4)
133 {
134 printf("failed to create dir (%d)", ret);
135 }
136 }
137 }
138}
139
Will Robertson590501c2007-09-21 15:51:53 +0000140void main(void)
141{
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000142 char buf[MAX_PATH];
143 char tarstring[6];
Nicolas Pennequin620e6b42008-04-15 23:17:03 +0000144 char model[5];
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000145
Will Robertson590501c2007-09-21 15:51:53 +0000146 lcd_clear_display();
147 printf("Hello world!");
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000148 printf("Gigabeat S Rockbox Bootloader v.00000006");
Michael Sevakisa07c0342008-02-08 02:20:05 +0000149 system_init();
Will Robertson590501c2007-09-21 15:51:53 +0000150 kernel_init();
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000151 printf("kernel init done");
Will Robertson590501c2007-09-21 15:51:53 +0000152 int rc;
153
Michael Sevakisec05b662008-03-31 06:05:16 +0000154 enable_interrupt(IRQ_FIQ_STATUS);
Michael Sevakisa07c0342008-02-08 02:20:05 +0000155
Will Robertson590501c2007-09-21 15:51:53 +0000156 rc = ata_init();
157 if(rc)
158 {
159 reset_screen();
160 error(EATA, rc);
161 }
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000162 printf("ata init done");
Will Robertson590501c2007-09-21 15:51:53 +0000163
164 disk_init();
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000165 printf("disk init done");
Will Robertson590501c2007-09-21 15:51:53 +0000166
167 rc = disk_mount_all();
168 if (rc<=0)
169 {
170 error(EDISK,rc);
171 }
172
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000173 /* Look for a tar file */
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000174 struct dirent_uncached* entry;
175 DIR_UNCACHED* dir;
176 int fd;
177 dir = opendir_uncached(basedir);
178 while ((entry = readdir_uncached(dir)))
179 {
180 if (*entry->d_name != '.')
181 {
182 snprintf(buf, sizeof(buf), "%s%s", basedir, entry->d_name);
183 fd = open(buf, O_RDONLY);
184 if (fd >= 0)
185 {
Nicolas Pennequin620e6b42008-04-15 23:17:03 +0000186 /* Check whether the file is a rockbox binary. */
187 lseek(fd, 4, SEEK_SET);
188 rc = read(fd, model, 4);
189 if (rc == 4)
190 {
191 model[4] = 0;
192 if (strcmp(model, "gigs") == 0)
193 {
194 printf("Found rockbox binary. Moving...");
195 close(fd);
196 remove("/.rockbox/rockbox.gigabeat");
197 int ret = rename(buf, "/.rockbox/rockbox.gigabeat");
198 printf("returned %d", ret);
Nicolas Pennequin90a3d582008-04-16 00:38:06 +0000199 sleep(HZ);
Nicolas Pennequin620e6b42008-04-15 23:17:03 +0000200 break;
201 }
202 }
203
204 /* Check whether the file is a tar file. */
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000205 lseek(fd, 257, SEEK_SET);
206 rc = read(fd, tarstring, 5);
207 if (rc == 5)
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000208 {
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000209 tarstring[5] = 0;
210 if (strcmp(tarstring, "ustar") == 0)
211 {
212 printf("Found tar file. Unarchiving...");
213 lseek(fd, 0, SEEK_SET);
214 untar(fd);
215 close(fd);
216 printf("Removing tar file");
217 remove(buf);
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000218 break;
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000219 }
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000220 }
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000221 close(fd);
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000222 }
223 }
224 }
Will Robertson590501c2007-09-21 15:51:53 +0000225
Will Robertson32f61092007-12-23 12:19:40 +0000226 unsigned char *loadbuffer = (unsigned char *)0x0;
Michael Sevakisa07c0342008-02-08 02:20:05 +0000227 int buffer_size = 31*1024*1024;
Will Robertson590501c2007-09-21 15:51:53 +0000228
Nicolas Pennequine2f5f212008-02-17 23:17:08 +0000229 rc = load_firmware(loadbuffer, "/.rockbox/rockbox.gigabeat", buffer_size);
Will Robertson590501c2007-09-21 15:51:53 +0000230 if(rc < 0)
Michael Sevakisa07c0342008-02-08 02:20:05 +0000231 error((int)buf, rc);
232
233 system_prepare_fw_start();
Will Robertson590501c2007-09-21 15:51:53 +0000234
235 if (rc == EOK)
236 {
237 kernel_entry = (void*) loadbuffer;
Michael Sevakis1f021af2008-02-05 04:43:19 +0000238 invalidate_icache();
Will Robertson590501c2007-09-21 15:51:53 +0000239 rc = kernel_entry();
240 }
Nicolas Pennequinc2ca8c72007-11-27 15:40:29 +0000241
242 while (1);
Will Robertson590501c2007-09-21 15:51:53 +0000243}
244