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" |
Maurus Cuelenaere | c33209e | 2009-02-07 13:52:03 +0000 | [diff] [blame] | 33 | #include "logf.h" |
| 34 | #include "button.h" |
| 35 | #include "string.h" |
| 36 | #include "usb.h" |
Nils Wallménius | 47608e0 | 2009-08-19 22:15:45 +0000 | [diff] [blame] | 37 | #include "file.h" |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 38 | |
Dave Chapman | 4d25bff | 2007-03-05 23:56:28 +0000 | [diff] [blame] | 39 | /* TODO: Other bootloaders need to be adjusted to set this variable to true |
Szymon Dziok | 492fafe | 2010-03-23 21:28:25 +0000 | [diff] [blame] | 40 | on a button press - currently only the ipod, H10, Vibe 500 and Sansa versions do. */ |
Jens Arnold | c41470d | 2007-09-12 22:21:03 +0000 | [diff] [blame] | 41 | #if defined(IPOD_ARCH) || defined(IRIVER_H10) || defined(IRIVER_H10_5GB) \ |
Mark Arigo | b4275d4 | 2008-05-21 03:55:17 +0000 | [diff] [blame] | 42 | || defined(SANSA_E200) || defined(SANSA_C200) || defined(GIGABEAT_F) \ |
Rafaël Carré | dee432f | 2010-03-28 04:10:40 +0000 | [diff] [blame] | 43 | || (CONFIG_CPU == AS3525) || (CONFIG_CPU == AS3525v2) || defined(COWON_D2) \ |
Mark Arigo | 6908cc5 | 2009-12-25 04:05:01 +0000 | [diff] [blame] | 44 | || defined(MROBE_100) || defined(MROBE_500) \ |
| 45 | || defined(SAMSUNG_YH925) || defined(SAMSUNG_YH920) \ |
| 46 | || defined(SAMSUNG_YH820) || defined(PHILIPS_SA9200) \ |
| 47 | || defined(PHILIPS_HDD1630) || defined(PHILIPS_HDD6330) \ |
Michael Sevakis | 863c03f | 2011-01-21 07:05:51 +0000 | [diff] [blame] | 48 | || defined(ONDA_VX747) || defined(PBELL_VIBE500) \ |
| 49 | || defined(TOSHIBA_GIGABEAT_S) |
Dave Chapman | 4d25bff | 2007-03-05 23:56:28 +0000 | [diff] [blame] | 50 | bool verbose = false; |
| 51 | #else |
| 52 | bool verbose = true; |
| 53 | #endif |
| 54 | |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 55 | int line = 0; |
| 56 | #ifdef HAVE_REMOTE_LCD |
| 57 | int remote_line = 0; |
| 58 | #endif |
| 59 | |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 60 | void reset_screen(void) |
| 61 | { |
| 62 | lcd_clear_display(); |
| 63 | line = 0; |
| 64 | #ifdef HAVE_REMOTE_LCD |
| 65 | lcd_remote_clear_display(); |
| 66 | remote_line = 0; |
| 67 | #endif |
| 68 | } |
| 69 | |
Thomas Martitz | 240923a | 2010-08-02 20:34:47 +0000 | [diff] [blame] | 70 | int printf(const char *format, ...) |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 71 | { |
Rafaël Carré | e595582 | 2011-07-02 02:49:15 +0000 | [diff] [blame] | 72 | static char printfbuf[256]; |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 73 | int len; |
| 74 | unsigned char *ptr; |
| 75 | va_list ap; |
| 76 | va_start(ap, format); |
| 77 | |
| 78 | ptr = printfbuf; |
| 79 | len = vsnprintf(ptr, sizeof(printfbuf), format, ap); |
| 80 | va_end(ap); |
| 81 | |
| 82 | lcd_puts(0, line++, ptr); |
Dave Chapman | 4d25bff | 2007-03-05 23:56:28 +0000 | [diff] [blame] | 83 | if (verbose) |
| 84 | lcd_update(); |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 85 | if(line >= LCD_HEIGHT/SYSFONT_HEIGHT) |
| 86 | line = 0; |
| 87 | #ifdef HAVE_REMOTE_LCD |
| 88 | lcd_remote_puts(0, remote_line++, ptr); |
Dave Chapman | 4d25bff | 2007-03-05 23:56:28 +0000 | [diff] [blame] | 89 | if (verbose) |
| 90 | lcd_remote_update(); |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 91 | if(remote_line >= LCD_REMOTE_HEIGHT/SYSFONT_HEIGHT) |
| 92 | remote_line = 0; |
| 93 | #endif |
Thomas Martitz | 240923a | 2010-08-02 20:34:47 +0000 | [diff] [blame] | 94 | return len; |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | char *strerror(int error) |
| 98 | { |
| 99 | switch(error) |
| 100 | { |
| 101 | case EOK: |
| 102 | return "OK"; |
| 103 | case EFILE_NOT_FOUND: |
| 104 | return "File not found"; |
| 105 | case EREAD_CHKSUM_FAILED: |
| 106 | return "Read failed (chksum)"; |
| 107 | case EREAD_MODEL_FAILED: |
| 108 | return "Read failed (model)"; |
| 109 | case EREAD_IMAGE_FAILED: |
| 110 | return "Read failed (image)"; |
| 111 | case EBAD_CHKSUM: |
| 112 | return "Bad checksum"; |
| 113 | case EFILE_TOO_BIG: |
| 114 | return "File too big"; |
Barry Wardell | 14ed3ca | 2007-03-16 14:28:00 +0000 | [diff] [blame] | 115 | case EINVALID_FORMAT: |
| 116 | return "Invalid file format"; |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 117 | default: |
| 118 | return "Unknown"; |
| 119 | } |
| 120 | } |
| 121 | |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 122 | void error(int errortype, int error, bool shutdown) |
Barry Wardell | 2370998 | 2007-03-12 22:12:20 +0000 | [diff] [blame] | 123 | { |
| 124 | switch(errortype) |
| 125 | { |
| 126 | case EATA: |
| 127 | printf("ATA error: %d", error); |
| 128 | break; |
| 129 | |
| 130 | case EDISK: |
| 131 | printf("No partition found"); |
| 132 | break; |
| 133 | |
| 134 | case EBOOTFILE: |
| 135 | printf(strerror(error)); |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | lcd_update(); |
| 140 | sleep(5*HZ); |
Rafaël Carré | 1ec8212 | 2010-06-23 05:08:36 +0000 | [diff] [blame] | 141 | if(shutdown) |
| 142 | power_off(); |
Barry Wardell | 2370998 | 2007-03-12 22:12:20 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 145 | /* Load firmware image in a format created by tools/scramble */ |
| 146 | int load_firmware(unsigned char* buf, char* firmware, int buffer_size) |
| 147 | { |
| 148 | int fd; |
| 149 | int rc; |
| 150 | int len; |
| 151 | unsigned long chksum; |
| 152 | char model[5]; |
| 153 | unsigned long sum; |
| 154 | int i; |
| 155 | char filename[MAX_PATH]; |
| 156 | |
Björn Stenberg | b69be10 | 2008-11-23 22:07:48 +0000 | [diff] [blame] | 157 | snprintf(filename,sizeof(filename), BOOTDIR "/%s",firmware); |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 158 | fd = open(filename, O_RDONLY); |
| 159 | if(fd < 0) |
| 160 | { |
| 161 | snprintf(filename,sizeof(filename),"/%s",firmware); |
| 162 | fd = open(filename, O_RDONLY); |
| 163 | if(fd < 0) |
| 164 | return EFILE_NOT_FOUND; |
| 165 | } |
| 166 | |
| 167 | len = filesize(fd) - 8; |
| 168 | |
| 169 | printf("Length: %x", len); |
| 170 | |
| 171 | if (len > buffer_size) |
| 172 | return EFILE_TOO_BIG; |
| 173 | |
| 174 | lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET); |
| 175 | |
| 176 | rc = read(fd, &chksum, 4); |
| 177 | chksum=betoh32(chksum); /* Rockbox checksums are big-endian */ |
| 178 | if(rc < 4) |
| 179 | return EREAD_CHKSUM_FAILED; |
| 180 | |
| 181 | printf("Checksum: %x", chksum); |
| 182 | |
| 183 | rc = read(fd, model, 4); |
| 184 | if(rc < 4) |
| 185 | return EREAD_MODEL_FAILED; |
| 186 | |
Rafaël Carré | e595582 | 2011-07-02 02:49:15 +0000 | [diff] [blame] | 187 | model[4] = '\0'; |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 188 | |
| 189 | printf("Model name: %s", model); |
| 190 | printf("Loading %s", firmware); |
| 191 | |
| 192 | lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET); |
| 193 | |
| 194 | rc = read(fd, buf, len); |
| 195 | if(rc < len) |
| 196 | return EREAD_IMAGE_FAILED; |
| 197 | |
| 198 | close(fd); |
| 199 | |
| 200 | sum = MODEL_NUMBER; |
| 201 | |
| 202 | for(i = 0;i < len;i++) { |
| 203 | sum += buf[i]; |
| 204 | } |
| 205 | |
| 206 | printf("Sum: %x", sum); |
| 207 | |
| 208 | if(sum != chksum) |
| 209 | return EBAD_CHKSUM; |
| 210 | |
Linus Nielsen Feltzing | 46597c9 | 2007-02-22 15:09:49 +0000 | [diff] [blame] | 211 | return EOK; |
Barry Wardell | 84b509d | 2007-01-28 18:42:11 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* Load raw binary image. */ |
| 215 | int load_raw_firmware(unsigned char* buf, char* firmware, int buffer_size) |
| 216 | { |
| 217 | int fd; |
| 218 | int rc; |
| 219 | int len; |
| 220 | char filename[MAX_PATH]; |
| 221 | |
| 222 | snprintf(filename,sizeof(filename),"%s",firmware); |
| 223 | fd = open(filename, O_RDONLY); |
| 224 | if(fd < 0) |
| 225 | { |
| 226 | return EFILE_NOT_FOUND; |
| 227 | } |
| 228 | |
| 229 | len = filesize(fd); |
| 230 | |
| 231 | if (len > buffer_size) |
| 232 | return EFILE_TOO_BIG; |
| 233 | |
| 234 | rc = read(fd, buf, len); |
| 235 | if(rc < len) |
| 236 | return EREAD_IMAGE_FAILED; |
| 237 | |
| 238 | close(fd); |
| 239 | return len; |
| 240 | } |
| 241 | |
Maurus Cuelenaere | c33209e | 2009-02-07 13:52:03 +0000 | [diff] [blame] | 242 | #ifdef ROCKBOX_HAS_LOGF /* Logf display helper for the bootloader */ |
| 243 | |
| 244 | #define LINES (LCD_HEIGHT/SYSFONT_HEIGHT) |
| 245 | #define COLUMNS ((LCD_WIDTH/SYSFONT_WIDTH) > MAX_LOGF_ENTRY ? \ |
| 246 | MAX_LOGF_ENTRY : (LCD_WIDTH/SYSFONT_WIDTH)) |
| 247 | |
| 248 | #ifdef ONDA_VX747 |
| 249 | #define LOGF_UP BUTTON_VOL_UP |
| 250 | #define LOGF_DOWN BUTTON_VOL_DOWN |
| 251 | #define LOGF_CLEAR BUTTON_MENU |
| 252 | #else |
| 253 | #warning No keymap defined for this target |
| 254 | #endif |
| 255 | |
| 256 | void display_logf(void) /* Doesn't return! */ |
| 257 | { |
| 258 | int i, index, button, user_index=0; |
| 259 | #ifdef HAVE_TOUCHSCREEN |
| 260 | int touch, prev_y=0; |
| 261 | #endif |
| 262 | char buffer[COLUMNS+1]; |
| 263 | |
| 264 | while(1) |
| 265 | { |
| 266 | index = logfindex + user_index; |
| 267 | |
| 268 | lcd_clear_display(); |
| 269 | for(i = LINES-1; i>=0; i--) |
| 270 | { |
| 271 | if(--index < 0) |
| 272 | { |
| 273 | if(logfwrap) |
| 274 | index = MAX_LOGF_LINES-1; |
| 275 | else |
| 276 | break; /* done */ |
| 277 | } |
| 278 | |
| 279 | memcpy(buffer, logfbuffer[index], COLUMNS); |
| 280 | |
| 281 | if (logfbuffer[index][MAX_LOGF_ENTRY] == LOGF_TERMINATE_CONTINUE_LINE) |
| 282 | buffer[MAX_LOGF_ENTRY-1] = '>'; |
| 283 | else if (logfbuffer[index][MAX_LOGF_ENTRY] == LOGF_TERMINATE_MULTI_LINE) |
| 284 | buffer[MAX_LOGF_ENTRY-1] = '\0'; |
| 285 | |
| 286 | buffer[COLUMNS] = '\0'; |
| 287 | |
| 288 | lcd_puts(0, i, buffer); |
| 289 | } |
| 290 | |
| 291 | button = button_get(false); |
| 292 | if(button == SYS_USB_CONNECTED) |
| 293 | usb_acknowledge(SYS_USB_CONNECTED_ACK); |
| 294 | else if(button == SYS_USB_DISCONNECTED) |
Michael Sevakis | 453550a | 2011-01-18 14:10:06 +0000 | [diff] [blame] | 295 | ; |
Maurus Cuelenaere | c33209e | 2009-02-07 13:52:03 +0000 | [diff] [blame] | 296 | else if(button & LOGF_UP) |
| 297 | user_index++; |
| 298 | else if(button & LOGF_DOWN) |
| 299 | user_index--; |
| 300 | else if(button & LOGF_CLEAR) |
| 301 | user_index = 0; |
| 302 | #ifdef HAVE_TOUCHSCREEN |
Maurus Cuelenaere | 935fad7 | 2009-02-26 21:07:27 +0000 | [diff] [blame] | 303 | else if(button & BUTTON_TOUCHSCREEN) |
Maurus Cuelenaere | c33209e | 2009-02-07 13:52:03 +0000 | [diff] [blame] | 304 | { |
| 305 | touch = button_get_data(); |
| 306 | |
| 307 | if(button & BUTTON_REL) |
| 308 | prev_y = 0; |
| 309 | |
| 310 | if(prev_y != 0) |
| 311 | user_index += (prev_y - (touch & 0xFFFF)) / SYSFONT_HEIGHT; |
| 312 | prev_y = touch & 0xFFFF; |
| 313 | } |
| 314 | #endif |
| 315 | |
| 316 | lcd_update(); |
| 317 | sleep(HZ/16); |
| 318 | } |
| 319 | } |
| 320 | #endif |