Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id: main.c 11997 2007-01-13 09:08:18Z miipekk $ |
| 9 | * |
| 10 | * Copyright (C) 2005 by Linus Nielsen Feltzing |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 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. |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | #include "lcd.h" |
| 22 | #include "lcd-remote.h" |
| 23 | #include "font.h" |
| 24 | #include "system.h" |
| 25 | #include <stdarg.h> |
| 26 | #include <stdio.h> |
Dave Chapman | 4d25bff | 2007-03-05 23:56:28 +0000 | [diff] [blame] | 27 | #include <stdbool.h> |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 28 | #include "cpu.h" |
| 29 | #include "common.h" |
Barry Wardell | 2370998 | 2007-03-12 22:12:20 +0000 | [diff] [blame] | 30 | #include "power.h" |
| 31 | #include "kernel.h" |
Björn Stenberg | b69be10 | 2008-11-23 22:07:48 +0000 | [diff] [blame] | 32 | #include "config.h" |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 33 | |
Dave Chapman | 4d25bff | 2007-03-05 23:56:28 +0000 | [diff] [blame] | 34 | /* TODO: Other bootloaders need to be adjusted to set this variable to true |
Barry Wardell | 2370998 | 2007-03-12 22:12:20 +0000 | [diff] [blame] | 35 | on a button press - currently only the ipod, H10 and Sansa versions do. */ |
Jens Arnold | c41470d | 2007-09-12 22:21:03 +0000 | [diff] [blame] | 36 | #if defined(IPOD_ARCH) || defined(IRIVER_H10) || defined(IRIVER_H10_5GB) \ |
Mark Arigo | b4275d4 | 2008-05-21 03:55:17 +0000 | [diff] [blame] | 37 | || defined(SANSA_E200) || defined(SANSA_C200) || defined(GIGABEAT_F) \ |
Rob Purchase | 9f0fbec | 2008-11-17 21:16:00 +0000 | [diff] [blame] | 38 | || defined(PHILIPS_SA9200) || (CONFIG_CPU == AS3525) || defined(COWON_D2) |
Dave Chapman | 4d25bff | 2007-03-05 23:56:28 +0000 | [diff] [blame] | 39 | bool verbose = false; |
| 40 | #else |
| 41 | bool verbose = true; |
| 42 | #endif |
| 43 | |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 44 | int line = 0; |
| 45 | #ifdef HAVE_REMOTE_LCD |
| 46 | int remote_line = 0; |
| 47 | #endif |
| 48 | |
| 49 | char printfbuf[256]; |
| 50 | |
| 51 | void reset_screen(void) |
| 52 | { |
| 53 | lcd_clear_display(); |
| 54 | line = 0; |
| 55 | #ifdef HAVE_REMOTE_LCD |
| 56 | lcd_remote_clear_display(); |
| 57 | remote_line = 0; |
| 58 | #endif |
| 59 | } |
| 60 | |
| 61 | void printf(const char *format, ...) |
| 62 | { |
| 63 | int len; |
| 64 | unsigned char *ptr; |
| 65 | va_list ap; |
| 66 | va_start(ap, format); |
| 67 | |
| 68 | ptr = printfbuf; |
| 69 | len = vsnprintf(ptr, sizeof(printfbuf), format, ap); |
| 70 | va_end(ap); |
| 71 | |
| 72 | lcd_puts(0, line++, ptr); |
Dave Chapman | 4d25bff | 2007-03-05 23:56:28 +0000 | [diff] [blame] | 73 | if (verbose) |
| 74 | lcd_update(); |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 75 | if(line >= LCD_HEIGHT/SYSFONT_HEIGHT) |
| 76 | line = 0; |
| 77 | #ifdef HAVE_REMOTE_LCD |
| 78 | lcd_remote_puts(0, remote_line++, ptr); |
Dave Chapman | 4d25bff | 2007-03-05 23:56:28 +0000 | [diff] [blame] | 79 | if (verbose) |
| 80 | lcd_remote_update(); |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 81 | if(remote_line >= LCD_REMOTE_HEIGHT/SYSFONT_HEIGHT) |
| 82 | remote_line = 0; |
| 83 | #endif |
| 84 | } |
| 85 | |
| 86 | char *strerror(int error) |
| 87 | { |
| 88 | switch(error) |
| 89 | { |
| 90 | case EOK: |
| 91 | return "OK"; |
| 92 | case EFILE_NOT_FOUND: |
| 93 | return "File not found"; |
| 94 | case EREAD_CHKSUM_FAILED: |
| 95 | return "Read failed (chksum)"; |
| 96 | case EREAD_MODEL_FAILED: |
| 97 | return "Read failed (model)"; |
| 98 | case EREAD_IMAGE_FAILED: |
| 99 | return "Read failed (image)"; |
| 100 | case EBAD_CHKSUM: |
| 101 | return "Bad checksum"; |
| 102 | case EFILE_TOO_BIG: |
| 103 | return "File too big"; |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 104 | case EINVALID_FORMAT: |
| 105 | return "Invalid file format"; |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 106 | default: |
| 107 | return "Unknown"; |
| 108 | } |
| 109 | } |
| 110 | |
Barry Wardell | 2370998 | 2007-03-12 22:12:20 +0000 | [diff] [blame] | 111 | void error(int errortype, int error) |
| 112 | { |
| 113 | switch(errortype) |
| 114 | { |
| 115 | case EATA: |
| 116 | printf("ATA error: %d", error); |
| 117 | break; |
| 118 | |
| 119 | case EDISK: |
| 120 | printf("No partition found"); |
| 121 | break; |
| 122 | |
| 123 | case EBOOTFILE: |
| 124 | printf(strerror(error)); |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | lcd_update(); |
| 129 | sleep(5*HZ); |
| 130 | power_off(); |
| 131 | } |
| 132 | |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 133 | /* Load firmware image in a format created by tools/scramble */ |
| 134 | int load_firmware(unsigned char* buf, char* firmware, int buffer_size) |
| 135 | { |
| 136 | int fd; |
| 137 | int rc; |
| 138 | int len; |
| 139 | unsigned long chksum; |
| 140 | char model[5]; |
| 141 | unsigned long sum; |
| 142 | int i; |
| 143 | char filename[MAX_PATH]; |
| 144 | |
Björn Stenberg | b69be10 | 2008-11-23 22:07:48 +0000 | [diff] [blame] | 145 | snprintf(filename,sizeof(filename), BOOTDIR "/%s",firmware); |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 146 | fd = open(filename, O_RDONLY); |
| 147 | if(fd < 0) |
| 148 | { |
| 149 | snprintf(filename,sizeof(filename),"/%s",firmware); |
| 150 | fd = open(filename, O_RDONLY); |
| 151 | if(fd < 0) |
| 152 | return EFILE_NOT_FOUND; |
| 153 | } |
| 154 | |
| 155 | len = filesize(fd) - 8; |
| 156 | |
| 157 | printf("Length: %x", len); |
| 158 | |
| 159 | if (len > buffer_size) |
| 160 | return EFILE_TOO_BIG; |
| 161 | |
| 162 | lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET); |
| 163 | |
| 164 | rc = read(fd, &chksum, 4); |
| 165 | chksum=betoh32(chksum); /* Rockbox checksums are big-endian */ |
| 166 | if(rc < 4) |
| 167 | return EREAD_CHKSUM_FAILED; |
| 168 | |
| 169 | printf("Checksum: %x", chksum); |
| 170 | |
| 171 | rc = read(fd, model, 4); |
| 172 | if(rc < 4) |
| 173 | return EREAD_MODEL_FAILED; |
| 174 | |
| 175 | model[4] = 0; |
| 176 | |
| 177 | printf("Model name: %s", model); |
| 178 | printf("Loading %s", firmware); |
| 179 | |
| 180 | lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET); |
| 181 | |
| 182 | rc = read(fd, buf, len); |
| 183 | if(rc < len) |
| 184 | return EREAD_IMAGE_FAILED; |
| 185 | |
| 186 | close(fd); |
| 187 | |
| 188 | sum = MODEL_NUMBER; |
| 189 | |
| 190 | for(i = 0;i < len;i++) { |
| 191 | sum += buf[i]; |
| 192 | } |
| 193 | |
| 194 | printf("Sum: %x", sum); |
| 195 | |
| 196 | if(sum != chksum) |
| 197 | return EBAD_CHKSUM; |
| 198 | |
Linus Nielsen Feltzing | 46597c9 | 2007-02-22 15:09:49 +0000 | [diff] [blame] | 199 | return EOK; |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* Load raw binary image. */ |
| 203 | int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size) |
| 204 | { |
| 205 | int fd; |
| 206 | int rc; |
| 207 | int len; |
| 208 | char filename[MAX_PATH]; |
| 209 | |
| 210 | snprintf(filename,sizeof(filename),"%s",firmware); |
| 211 | fd = open(filename, O_RDONLY); |
| 212 | if(fd < 0) |
| 213 | { |
| 214 | return EFILE_NOT_FOUND; |
| 215 | } |
| 216 | |
| 217 | len = filesize(fd); |
| 218 | |
| 219 | if (len > buffer_size) |
| 220 | return EFILE_TOO_BIG; |
| 221 | |
| 222 | rc = read(fd, buf, len); |
| 223 | if(rc < len) |
| 224 | return EREAD_IMAGE_FAILED; |
| 225 | |
| 226 | close(fd); |
| 227 | return len; |
| 228 | } |
| 229 | |
| 230 | /* These functions are present in the firmware library, but we reimplement |
| 231 | them here because the originals do a lot more than we want */ |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 232 | int dbg_ports(void) |
| 233 | { |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | void mpeg_stop(void) |
| 238 | { |
| 239 | } |