Jörg Hohensohn | 6a4e4c8 | 2003-11-30 11:37:43 +0000 | [diff] [blame] | 1 | // uart_boot.cpp : Defines the entry point for the console application. |
| 2 | // |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include "scalar_types.h" // (U)INT8/16/32 |
| 8 | #include "Uart.h" // platform abstraction for UART |
| 9 | #include "client.h" // client functions |
| 10 | #include "flash.h" // flash high level functions |
| 11 | |
| 12 | // command line configuration: what shall we do? |
| 13 | struct |
| 14 | { |
| 15 | char* szPort; // COM port to use |
| 16 | bool bRecorder; // it's a recorder |
| 17 | bool bArchos; // use the Archos monitor to load, instead of UART boot |
| 18 | bool bSpindown; // spindown the harddisk |
| 19 | bool bReadID; // read manufacturer+device ID |
| 20 | char* szFlashfile; // file to be programmed |
| 21 | char* szDumpfile; // file to dump into |
| 22 | char* szExecfile; // file with the executable |
| 23 | bool bTest; // debug action |
Jörg Hohensohn | 3b6cc1c | 2004-03-11 18:02:53 +0000 | [diff] [blame] | 24 | bool bHold; // hold power (for FMs & V2s) |
Jörg Hohensohn | 6a4e4c8 | 2003-11-30 11:37:43 +0000 | [diff] [blame] | 25 | bool bBlink; // blink red LED |
| 26 | bool bNoDownload; |
| 27 | } gCmd; |
| 28 | |
| 29 | |
Jörg Hohensohn | 50d363f | 2004-11-19 22:46:19 +0000 | [diff] [blame] | 30 | |
Jörg Hohensohn | 6a4e4c8 | 2003-11-30 11:37:43 +0000 | [diff] [blame] | 31 | int ProcessCmdLine(int argc, char* argv[]) |
| 32 | { |
| 33 | argc--; // exclude our name |
| 34 | argv++; |
| 35 | |
| 36 | memset(&gCmd, 0, sizeof(gCmd)); |
| 37 | |
| 38 | if (argc == 0) |
| 39 | { |
| 40 | printf("Usage: uart_boot [-option {filename}]\n"); |
| 41 | printf(" uses activated UART boot mod, box has to be fresh started\n"); |
| 42 | printf("The order of the options does not matter, one letter is sufficient.\n"); |
| 43 | printf("Possible options are (in the order of later processing):\n"); |
| 44 | printf("-port <name of COM port to use>\n"); |
| 45 | printf("-recorder (this is a recorder/FM, default is player if not specified)\n"); |
| 46 | printf("-archos (use Archos bootloader, this one needs powerup while program waits)\n"); |
| 47 | printf("-nodownload (no MiniMon download, it's already active)\n"); |
Jörg Hohensohn | 3b6cc1c | 2004-03-11 18:02:53 +0000 | [diff] [blame] | 48 | printf("-hold (hold the power, useful for FMs and V2s, so you can release ON)\n"); |
Jörg Hohensohn | 6a4e4c8 | 2003-11-30 11:37:43 +0000 | [diff] [blame] | 49 | printf("-spindown (spindown the harddisk, else it stays on by default)\n"); |
| 50 | printf("-id (read manufacturer and device ID of flash, no checks)\n"); |
| 51 | printf("-flash <filename of binary to program into flash>\n"); |
| 52 | printf("-dump <filename to write flash content to>\n"); |
| 53 | printf("-exec <filename of executable for 0x09000000:0x09000200>\n"); |
| 54 | printf("-test (some test action currently under development, don't use!)\n"); |
| 55 | printf("-blink (blink red LED forever, meant as diagnostics)\n"); |
| 56 | printf("\n"); |
| 57 | printf("Examples:\n"); |
| 58 | printf("uart_boot -r -p COM1 -s -f flashfile.bin -d dumpfile.bin\n"); |
| 59 | printf(" recorder on COM1, spindown HD, program and dump (for e.g. offline verify)\n"); |
| 60 | printf("uart_boot -r -p COM2 -e rockbox.bin\n"); |
| 61 | printf(" recorder on COM2, load Rockbox from file and start it\n"); |
| 62 | exit (0); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | while (argc) |
| 67 | { |
| 68 | if (!strncmp("-port", *argv, 2)) |
| 69 | { |
| 70 | gCmd.szPort = *++argv; |
| 71 | if (--argc <= 0 || **argv == '-') |
| 72 | { |
| 73 | printf("No argument given for option %s, aborting.\n", argv[-1]); |
| 74 | exit (-2); |
| 75 | } |
| 76 | } |
| 77 | else if (!strncmp("-recorder", *argv, 2)) |
| 78 | { |
| 79 | gCmd.bRecorder = true; |
| 80 | } |
| 81 | else if (!strncmp("-archos", *argv, 2)) |
| 82 | { |
| 83 | gCmd.bArchos = true; |
| 84 | } |
| 85 | else if (!strncmp("-nodownload", *argv, 2)) |
| 86 | { |
| 87 | gCmd.bNoDownload = true; |
| 88 | } |
| 89 | else if (!strncmp("-spindown", *argv, 2)) |
| 90 | { |
| 91 | gCmd.bSpindown = true; |
| 92 | } |
| 93 | else if (!strncmp("-id", *argv, 2)) |
| 94 | { |
| 95 | gCmd.bReadID = true; |
| 96 | } |
| 97 | else if (!strncmp("-flash", *argv, 2)) |
| 98 | { |
| 99 | gCmd.szFlashfile = *++argv; |
| 100 | if (--argc <= 0 || **argv == '-') |
| 101 | { |
| 102 | printf("No argument given for option %s, aborting.\n", argv[-1]); |
| 103 | exit (-2); |
| 104 | } |
| 105 | } |
| 106 | else if (!strncmp("-dump", *argv, 2)) |
| 107 | { |
| 108 | gCmd.szDumpfile = *++argv; |
| 109 | if (--argc <= 0 || **argv == '-') |
| 110 | { |
| 111 | printf("No argument given for option %s, aborting.\n", argv[-1]); |
| 112 | exit (-3); |
| 113 | } |
| 114 | } |
| 115 | else if (!strncmp("-exec", *argv, 2)) |
| 116 | { |
| 117 | gCmd.szExecfile = *++argv; |
| 118 | if (--argc <= 0 || **argv == '-') |
| 119 | { |
| 120 | printf("No argument given for option %s, aborting.\n", argv[-1]); |
| 121 | exit (-4); |
| 122 | } |
| 123 | } |
| 124 | else if (!strncmp("-test", *argv, 2)) |
| 125 | { |
| 126 | gCmd.bTest = true; |
| 127 | } |
Jörg Hohensohn | 3b6cc1c | 2004-03-11 18:02:53 +0000 | [diff] [blame] | 128 | else if (!strncmp("-hold", *argv, 2)) |
| 129 | { |
| 130 | gCmd.bHold = true; |
| 131 | } |
Jörg Hohensohn | 6a4e4c8 | 2003-11-30 11:37:43 +0000 | [diff] [blame] | 132 | else if (!strncmp("-blink", *argv, 2)) |
| 133 | { |
| 134 | gCmd.bBlink = true; |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | printf("Unknown option %s, aborting. Use 'uart_boot' without options for help.\n", *argv); |
| 139 | exit(-1); |
| 140 | } |
| 141 | |
| 142 | argv++; |
| 143 | argc--; |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | int main(int argc, char* argv[]) |
| 151 | { |
| 152 | tUartHandle serial_handle; |
| 153 | UINT16 reg; |
| 154 | FILE* pFile; |
| 155 | size_t size; |
Jörg Hohensohn | 50d363f | 2004-11-19 22:46:19 +0000 | [diff] [blame] | 156 | static UINT8 abFirmware[256*1024]; // blocksize |
Jörg Hohensohn | 6a4e4c8 | 2003-11-30 11:37:43 +0000 | [diff] [blame] | 157 | memset(abFirmware, 0xFF, sizeof(abFirmware)); |
| 158 | |
| 159 | ProcessCmdLine(argc, argv); // what to do |
| 160 | |
| 161 | if (!gCmd.szPort) |
| 162 | { |
| 163 | printf("No serial port given, use 'uart_boot' without parameters for options.\n"); |
| 164 | exit(-1); |
| 165 | } |
| 166 | |
| 167 | serial_handle = UartOpen(gCmd.szPort); // opening serial port |
| 168 | if (serial_handle == NULL) |
| 169 | { |
| 170 | printf("Cannot open port %s\n", gCmd.szPort); |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | if (gCmd.bNoDownload) |
| 175 | { // just set our speed |
Jörg Hohensohn | 0044a04 | 2004-11-19 23:04:40 +0000 | [diff] [blame] | 176 | int baudrate = gCmd.bRecorder ? 115200 : 14400; |
| 177 | if (!gCmd.bRecorder && gCmd.bTest) |
| 178 | { // experimental Player speedup to 38400 baud |
| 179 | baudrate = 38400; |
| 180 | } |
| 181 | |
| 182 | if (!UartConfig(serial_handle, baudrate, eNOPARITY, eONESTOPBIT, 8)) |
Jörg Hohensohn | 6a4e4c8 | 2003-11-30 11:37:43 +0000 | [diff] [blame] | 183 | { |
| 184 | printf("Error setting up COM params\n"); |
| 185 | exit(1); |
| 186 | } |
| 187 | } |
| 188 | else |
| 189 | { // download the monitor program |
| 190 | if (gCmd.bArchos) |
| 191 | { |
| 192 | printf("Waiting for box startup to download monitor..."); |
Jörg Hohensohn | 50d363f | 2004-11-19 22:46:19 +0000 | [diff] [blame] | 193 | DownloadArchosMonitor(serial_handle, "minimon_archos.bin"); // load the monitor image |
Jörg Hohensohn | 6a4e4c8 | 2003-11-30 11:37:43 +0000 | [diff] [blame] | 194 | printf("\b\b\b done.\n"); |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | printf("Downloading monitor..."); |
| 199 | DownloadMonitor(serial_handle, gCmd.bRecorder, "minimon.bin"); // load the monitor image |
| 200 | // From now on, we can talk to the box. |
| 201 | printf("\b\b\b done.\n"); |
| 202 | |
| 203 | if (gCmd.bRecorder) |
| 204 | { // we can be faster |
| 205 | SetTargetBaudrate(serial_handle, 11059200, 115200); // set to 115200 |
| 206 | } |
Jörg Hohensohn | 0044a04 | 2004-11-19 23:04:40 +0000 | [diff] [blame] | 207 | else if (gCmd.bTest) // experimental Player speedup to 38400 baud |
| 208 | { |
| 209 | SetTargetBaudrate(serial_handle, 12000000, 38400); // set to 38400 |
Jörg Hohensohn | 50d363f | 2004-11-19 22:46:19 +0000 | [diff] [blame] | 210 | } |
Jörg Hohensohn | 6a4e4c8 | 2003-11-30 11:37:43 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Jörg Hohensohn | 3b6cc1c | 2004-03-11 18:02:53 +0000 | [diff] [blame] | 214 | |
Jörg Hohensohn | 6a4e4c8 | 2003-11-30 11:37:43 +0000 | [diff] [blame] | 215 | // do the action |
Jörg Hohensohn | 3b6cc1c | 2004-03-11 18:02:53 +0000 | [diff] [blame] | 216 | if (gCmd.bHold) |
| 217 | { |
| 218 | // hold power for FM |
| 219 | reg = ReadHalfword(serial_handle, 0x05FFFFC2); // PBDR |
| 220 | reg |= 0x0020; // set PB5 to keep power |
| 221 | WriteHalfword(serial_handle, 0x05FFFFC2, reg); |
| 222 | |
| 223 | reg = ReadHalfword(serial_handle, 0x05FFFFC6); // PBIOR |
| 224 | reg |= 0x0020; // make PB5 an output |
| 225 | WriteHalfword(serial_handle, 0x05FFFFC6, reg); |
| 226 | printf("Power hold, you can release ON button now.\n"); |
| 227 | } |
| 228 | |
Jörg Hohensohn | 6a4e4c8 | 2003-11-30 11:37:43 +0000 | [diff] [blame] | 229 | |
| 230 | if (gCmd.bSpindown) |
| 231 | { |
Jörg Hohensohn | 3b6cc1c | 2004-03-11 18:02:53 +0000 | [diff] [blame] | 232 | // power down the disk |
Jörg Hohensohn | 50d363f | 2004-11-19 22:46:19 +0000 | [diff] [blame] | 233 | if (gCmd.bRecorder) |
| 234 | { // Recorder (V1+V2) and FM have disk power control on PA5 |
| 235 | reg = ReadHalfword(serial_handle, 0x05FFFFCA); // PACR2 |
| 236 | reg &= ~0x0400; // clear bit 10: GPIO |
| 237 | WriteHalfword(serial_handle, 0x05FFFFCA, reg); |
Jörg Hohensohn | 3b6cc1c | 2004-03-11 18:02:53 +0000 | [diff] [blame] | 238 | |
Jörg Hohensohn | 50d363f | 2004-11-19 22:46:19 +0000 | [diff] [blame] | 239 | reg = ReadHalfword(serial_handle, 0x05FFFFC4); // PAIOR |
| 240 | reg |= 0x0020; // set bit 5: output |
| 241 | WriteHalfword(serial_handle, 0x05FFFFC4, reg); |
Jörg Hohensohn | 3b6cc1c | 2004-03-11 18:02:53 +0000 | [diff] [blame] | 242 | |
Jörg Hohensohn | 50d363f | 2004-11-19 22:46:19 +0000 | [diff] [blame] | 243 | reg = ReadHalfword(serial_handle, 0x05FFFFC0); // PADR |
| 244 | reg &= ~0x0020; // clear PA5 to power down |
| 245 | WriteHalfword(serial_handle, 0x05FFFFC0, reg); |
| 246 | } |
| 247 | else |
| 248 | { // new Players have disk power control on PB4 |
Jens Arnold | b7638be | 2004-11-20 01:21:53 +0000 | [diff] [blame] | 249 | reg = ReadHalfword(serial_handle, 0x05FFFFC6); // PBIOR |
Jörg Hohensohn | 50d363f | 2004-11-19 22:46:19 +0000 | [diff] [blame] | 250 | reg |= 0x0010; // set bit 4: output |
| 251 | WriteHalfword(serial_handle, 0x05FFFFC6, reg); |
| 252 | |
Jens Arnold | b7638be | 2004-11-20 01:21:53 +0000 | [diff] [blame] | 253 | reg = ReadHalfword(serial_handle, 0x05FFFFC2); // PBDR |
Jörg Hohensohn | 50d363f | 2004-11-19 22:46:19 +0000 | [diff] [blame] | 254 | reg &= ~0x0010; // clear PB4 to power down |
| 255 | WriteHalfword(serial_handle, 0x05FFFFC2, reg); |
| 256 | } |
Jörg Hohensohn | 3b6cc1c | 2004-03-11 18:02:53 +0000 | [diff] [blame] | 257 | printf("Harddisk powered down.\n"); |
Jörg Hohensohn | 6a4e4c8 | 2003-11-30 11:37:43 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | |
| 261 | if (gCmd.bReadID) |
| 262 | { |
| 263 | UINT8 bMan, bID; |
| 264 | ReadID(serial_handle, 0x02000000, &bMan, &bID); |
| 265 | printf("Manufacturer ID = 0x%02X, Device ID = 0x%02X\n", bMan, bID); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | if (gCmd.szFlashfile) |
| 270 | { |
| 271 | // flash a firmware file |
| 272 | printf("Flashing file %s...", gCmd.szFlashfile); |
| 273 | pFile = fopen(gCmd.szFlashfile, "rb"); |
| 274 | if (pFile == NULL) |
| 275 | { |
| 276 | printf("\nFlash file %s not found, exiting.\n", gCmd.szFlashfile); |
| 277 | return -2; |
| 278 | } |
| 279 | size = fread(abFirmware, 1, sizeof(abFirmware), pFile); |
| 280 | fclose (pFile); |
| 281 | |
| 282 | EraseChip(serial_handle, 0x02000000); |
| 283 | FlashByteMultiple(serial_handle, 0x02000000, size, abFirmware); |
| 284 | printf("\b\b\b done.\n"); |
| 285 | } |
| 286 | |
| 287 | |
| 288 | if (gCmd.szDumpfile) |
| 289 | { |
| 290 | // dump the flash content |
| 291 | printf("Writing flash dump into file %s...", gCmd.szDumpfile); |
| 292 | ReadByteMultiple(serial_handle, 0x02000000, sizeof(abFirmware), abFirmware); |
| 293 | pFile = fopen(gCmd.szDumpfile, "wb"); |
| 294 | if (pFile == NULL) |
| 295 | { |
| 296 | printf("\nDump file %s cannot be opened, exiting.\n", gCmd.szDumpfile); |
| 297 | return -3; |
| 298 | } |
| 299 | fwrite(abFirmware, 1, sizeof(abFirmware), pFile); |
| 300 | fclose (pFile); |
| 301 | printf("\b\b\b done.\n"); |
| 302 | } |
| 303 | |
| 304 | |
| 305 | if (gCmd.szExecfile) |
| 306 | { |
| 307 | UINT32 size; |
| 308 | |
| 309 | printf("Downloading program..."); |
| 310 | |
| 311 | // init the DRAM controller like the flash boot does |
| 312 | reg = ReadHalfword(serial_handle, 0x05FFFFCA); // PACR2 |
| 313 | reg &= 0xFFFB; // PA1 config: /RAS |
| 314 | reg |= 0x0008; |
| 315 | WriteHalfword(serial_handle, 0x05FFFFCA, reg); // PACR2 |
| 316 | reg = 0xAFFF; // CS1, CS3 config: /CASH. /CASL |
| 317 | WriteHalfword(serial_handle, 0x05FFFFEE, reg); // CASCR |
| 318 | reg = ReadHalfword(serial_handle, 0x05FFFFA0); // BCR |
| 319 | reg |= 0x8000; // DRAM enable, default bus |
| 320 | WriteHalfword(serial_handle, 0x05FFFFA0, reg); // BCR |
| 321 | reg = ReadHalfword(serial_handle, 0x05FFFFA2); // WCR1 |
| 322 | reg &= 0xFDFD; // 1-cycle CAS |
| 323 | WriteHalfword(serial_handle, 0x05FFFFA2, reg); // WCR1 |
| 324 | reg = 0x0E00; // CAS 35%, multiplexed, 10 bit row addr. |
| 325 | WriteHalfword(serial_handle, 0x05FFFFA8, reg); // DCR |
| 326 | reg = 0x5AB0; // refresh, 4 cycle waitstate |
| 327 | WriteHalfword(serial_handle, 0x05FFFFAC, reg); // RCR |
| 328 | reg = 0x9605; // refresh constant |
| 329 | WriteHalfword(serial_handle, 0x05FFFFB2, reg); // RTCOR |
| 330 | reg = 0xA518; // phi/32 |
| 331 | WriteHalfword(serial_handle, 0x05FFFFAE, reg); // RTCSR |
| 332 | |
| 333 | |
| 334 | // download Rockbox/gdb |
| 335 | pFile = fopen(gCmd.szExecfile, "rb"); |
| 336 | if (pFile == NULL) |
| 337 | { |
| 338 | printf("\nExecutable file %s cannot be opened, exiting.\n", gCmd.szExecfile); |
| 339 | return -3; |
| 340 | } |
| 341 | |
| 342 | size = fread(abFirmware, 1, sizeof(abFirmware), pFile); |
| 343 | WriteByteMultiple(serial_handle, 0x09000000, size, abFirmware); |
| 344 | fclose (pFile); |
| 345 | printf("\b\b\b done.\n"); |
| 346 | |
| 347 | // start rockbox/gdb |
| 348 | printf("Starting program..."); |
| 349 | Execute(serial_handle, 0x09000200, false); |
| 350 | printf("\b\b\b done.\n"); |
| 351 | } |
| 352 | |
| 353 | |
Jörg Hohensohn | 6a4e4c8 | 2003-11-30 11:37:43 +0000 | [diff] [blame] | 354 | if (gCmd.bBlink) |
| 355 | { |
| 356 | // blinking LED |
| 357 | UINT8 byte; |
| 358 | printf("Flashing red LED forever... (stop with Ctrl-C)\n"); |
| 359 | byte = ReadByte(serial_handle, 0x05FFFFC3); |
| 360 | while (1) |
| 361 | { |
| 362 | byte ^= 0x40; |
| 363 | WriteByte(serial_handle, 0x05FFFFC3, byte); |
| 364 | Sleep(200); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |