Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 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 Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 31 | #include "dir.h" |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 32 | #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 Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame^] | 50 | #define TAR_CHUNK 512 |
| 51 | #define TAR_HEADER_SIZE 157 |
| 52 | |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 53 | char version[] = APPSVERSION; |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 54 | char basedir[] = "/Content/0b00/00/"; /* Where files sent via MTP are stored */ |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 55 | int (*kernel_entry)(void); |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 56 | extern void reference_system_c(void); |
| 57 | |
| 58 | /* Dummy stub that creates C references for C functions only used by |
| 59 | assembly - never called */ |
| 60 | void reference_files(void) |
| 61 | { |
| 62 | reference_system_c(); |
| 63 | } |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 64 | |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame^] | 65 | void untar(int tar_fd) |
| 66 | { |
| 67 | char header[TAR_HEADER_SIZE]; |
| 68 | char copybuf[TAR_CHUNK]; |
| 69 | char path[102]; |
| 70 | int fd, i, size = 0, pos = 0; |
| 71 | |
| 72 | while (1) |
| 73 | { |
| 74 | read(tar_fd, header, TAR_HEADER_SIZE); |
| 75 | |
| 76 | if (*header == '\0') /* Check for EOF */ |
| 77 | break; |
| 78 | |
| 79 | /* Parse the size field */ |
| 80 | size = 0; |
| 81 | for (i = 124 ; i < 124 + 11 ; i++) { |
| 82 | size = (8 * size) + header[i] - '0'; |
| 83 | } |
| 84 | |
| 85 | /* Skip rest of header */ |
| 86 | pos = lseek(tar_fd, TAR_CHUNK - TAR_HEADER_SIZE, SEEK_CUR); |
| 87 | |
| 88 | /* Make the path absolute */ |
| 89 | strcpy(path, "/"); |
| 90 | strcat(path, header); |
| 91 | |
| 92 | if (header[156] == '0') /* file */ |
| 93 | { |
| 94 | int rc, wc, total = 0; |
| 95 | |
| 96 | fd = creat(path); |
| 97 | if (fd < 0) |
| 98 | { |
| 99 | printf("failed to create file (%d)", fd); |
| 100 | /* Skip the file */ |
| 101 | lseek(tar_fd, (size + 511) & (~511), SEEK_CUR); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | /* Copy the file over 512 bytes at a time */ |
| 106 | while (total < size) |
| 107 | { |
| 108 | rc = read(tar_fd, copybuf, TAR_CHUNK); |
| 109 | pos += rc; |
| 110 | |
| 111 | wc = write(fd, copybuf, MIN(rc, size - total)); |
| 112 | if (wc < 0) |
| 113 | { |
| 114 | printf("write failed (%d)", wc); |
| 115 | break; |
| 116 | } |
| 117 | total += wc; |
| 118 | } |
| 119 | close(fd); |
| 120 | } |
| 121 | } |
| 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 Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 140 | void main(void) |
| 141 | { |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame^] | 142 | char buf[MAX_PATH]; |
| 143 | char tarstring[6]; |
| 144 | |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 145 | lcd_clear_display(); |
| 146 | printf("Hello world!"); |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame^] | 147 | printf("Gigabeat S Rockbox Bootloader v.00000004"); |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 148 | system_init(); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 149 | kernel_init(); |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 150 | printf("kernel init done"); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 151 | int rc; |
| 152 | |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 153 | set_interrupt_status(IRQ_FIQ_ENABLED, IRQ_FIQ_STATUS); |
| 154 | |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 155 | rc = ata_init(); |
| 156 | if(rc) |
| 157 | { |
| 158 | reset_screen(); |
| 159 | error(EATA, rc); |
| 160 | } |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 161 | printf("ata init done"); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 162 | |
| 163 | disk_init(); |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 164 | printf("disk init done"); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 165 | |
| 166 | rc = disk_mount_all(); |
| 167 | if (rc<=0) |
| 168 | { |
| 169 | error(EDISK,rc); |
| 170 | } |
| 171 | |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame^] | 172 | /* Look for a tar file */ |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 173 | struct dirent_uncached* entry; |
| 174 | DIR_UNCACHED* dir; |
| 175 | int fd; |
| 176 | dir = opendir_uncached(basedir); |
| 177 | while ((entry = readdir_uncached(dir))) |
| 178 | { |
| 179 | if (*entry->d_name != '.') |
| 180 | { |
| 181 | snprintf(buf, sizeof(buf), "%s%s", basedir, entry->d_name); |
| 182 | fd = open(buf, O_RDONLY); |
| 183 | if (fd >= 0) |
| 184 | { |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame^] | 185 | lseek(fd, 257, SEEK_SET); |
| 186 | rc = read(fd, tarstring, 5); |
| 187 | if (rc == 5) |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 188 | { |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame^] | 189 | tarstring[5] = 0; |
| 190 | if (strcmp(tarstring, "ustar") == 0) |
| 191 | { |
| 192 | printf("Found tar file. Unarchiving..."); |
| 193 | lseek(fd, 0, SEEK_SET); |
| 194 | untar(fd); |
| 195 | close(fd); |
| 196 | printf("Removing tar file"); |
| 197 | remove(buf); |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 198 | break; |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame^] | 199 | } |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 200 | } |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame^] | 201 | close(fd); |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | } |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 205 | |
Will Robertson | 32f6109 | 2007-12-23 12:19:40 +0000 | [diff] [blame] | 206 | unsigned char *loadbuffer = (unsigned char *)0x0; |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 207 | int buffer_size = 31*1024*1024; |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 208 | |
Nicolas Pennequin | e2f5f21 | 2008-02-17 23:17:08 +0000 | [diff] [blame^] | 209 | rc = load_firmware(loadbuffer, "/.rockbox/rockbox.gigabeat", buffer_size); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 210 | if(rc < 0) |
Michael Sevakis | a07c034 | 2008-02-08 02:20:05 +0000 | [diff] [blame] | 211 | error((int)buf, rc); |
| 212 | |
| 213 | system_prepare_fw_start(); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 214 | |
| 215 | if (rc == EOK) |
| 216 | { |
| 217 | kernel_entry = (void*) loadbuffer; |
Michael Sevakis | 1f021af | 2008-02-05 04:43:19 +0000 | [diff] [blame] | 218 | invalidate_icache(); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 219 | rc = kernel_entry(); |
| 220 | } |
Nicolas Pennequin | c2ca8c7 | 2007-11-27 15:40:29 +0000 | [diff] [blame] | 221 | |
| 222 | while (1); |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 223 | } |
| 224 | |