Better menu verbiage for test_codec
[kugel-rb.git] / flash / uart_boot / uart_boot.c
blob40c3682091bb48628e51903d32ed940ead53f843
1 // uart_boot.cpp : Defines the entry point for the console application.
2 //
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
12 // command line configuration: what shall we do?
13 struct
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
24 bool bHold; // hold power (for FMs & V2s)
25 bool bBlink; // blink red LED
26 bool bNoDownload;
27 } gCmd;
31 int ProcessCmdLine(int argc, char* argv[])
33 argc--; // exclude our name
34 argv++;
36 memset(&gCmd, 0, sizeof(gCmd));
38 if (argc == 0)
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");
48 printf("-hold (hold the power, useful for FMs and V2s, so you can release ON)\n");
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);
66 while (argc)
68 if (!strncmp("-port", *argv, 2))
70 gCmd.szPort = *++argv;
71 if (--argc <= 0 || **argv == '-')
73 printf("No argument given for option %s, aborting.\n", argv[-1]);
74 exit (-2);
77 else if (!strncmp("-recorder", *argv, 2))
79 gCmd.bRecorder = true;
81 else if (!strncmp("-archos", *argv, 2))
83 gCmd.bArchos = true;
85 else if (!strncmp("-nodownload", *argv, 2))
87 gCmd.bNoDownload = true;
89 else if (!strncmp("-spindown", *argv, 2))
91 gCmd.bSpindown = true;
93 else if (!strncmp("-id", *argv, 2))
95 gCmd.bReadID = true;
97 else if (!strncmp("-flash", *argv, 2))
99 gCmd.szFlashfile = *++argv;
100 if (--argc <= 0 || **argv == '-')
102 printf("No argument given for option %s, aborting.\n", argv[-1]);
103 exit (-2);
106 else if (!strncmp("-dump", *argv, 2))
108 gCmd.szDumpfile = *++argv;
109 if (--argc <= 0 || **argv == '-')
111 printf("No argument given for option %s, aborting.\n", argv[-1]);
112 exit (-3);
115 else if (!strncmp("-exec", *argv, 2))
117 gCmd.szExecfile = *++argv;
118 if (--argc <= 0 || **argv == '-')
120 printf("No argument given for option %s, aborting.\n", argv[-1]);
121 exit (-4);
124 else if (!strncmp("-test", *argv, 2))
126 gCmd.bTest = true;
128 else if (!strncmp("-hold", *argv, 2))
130 gCmd.bHold = true;
132 else if (!strncmp("-blink", *argv, 2))
134 gCmd.bBlink = true;
136 else
138 printf("Unknown option %s, aborting. Use 'uart_boot' without options for help.\n", *argv);
139 exit(-1);
142 argv++;
143 argc--;
146 return 0;
150 int main(int argc, char* argv[])
152 tUartHandle serial_handle;
153 UINT16 reg;
154 FILE* pFile;
155 size_t size;
156 static UINT8 abFirmware[256*1024]; // blocksize
157 memset(abFirmware, 0xFF, sizeof(abFirmware));
159 ProcessCmdLine(argc, argv); // what to do
161 if (!gCmd.szPort)
163 printf("No serial port given, use 'uart_boot' without parameters for options.\n");
164 exit(-1);
167 serial_handle = UartOpen(gCmd.szPort); // opening serial port
168 if (serial_handle == NULL)
170 printf("Cannot open port %s\n", gCmd.szPort);
171 return -1;
174 if (gCmd.bNoDownload)
175 { // just set our speed
176 int baudrate = gCmd.bRecorder ? 115200 : 14400;
177 if (!gCmd.bRecorder && gCmd.bTest)
178 { // experimental Player speedup to 38400 baud
179 baudrate = 38400;
182 if (!UartConfig(serial_handle, baudrate, eNOPARITY, eONESTOPBIT, 8))
184 printf("Error setting up COM params\n");
185 exit(1);
188 else
189 { // download the monitor program
190 if (gCmd.bArchos)
192 printf("Waiting for box startup to download monitor...");
193 DownloadArchosMonitor(serial_handle, "minimon_archos.bin"); // load the monitor image
194 printf("\b\b\b done.\n");
196 else
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");
203 if (gCmd.bRecorder)
204 { // we can be faster
205 SetTargetBaudrate(serial_handle, 11059200, 115200); // set to 115200
207 else if (gCmd.bTest) // experimental Player speedup to 38400 baud
209 SetTargetBaudrate(serial_handle, 12000000, 38400); // set to 38400
215 // do the action
216 if (gCmd.bHold)
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);
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");
230 if (gCmd.bSpindown)
232 // power down the disk
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);
239 reg = ReadHalfword(serial_handle, 0x05FFFFC4); // PAIOR
240 reg |= 0x0020; // set bit 5: output
241 WriteHalfword(serial_handle, 0x05FFFFC4, reg);
243 reg = ReadHalfword(serial_handle, 0x05FFFFC0); // PADR
244 reg &= ~0x0020; // clear PA5 to power down
245 WriteHalfword(serial_handle, 0x05FFFFC0, reg);
247 else
248 { // new Players have disk power control on PB4
249 reg = ReadHalfword(serial_handle, 0x05FFFFC6); // PBIOR
250 reg |= 0x0010; // set bit 4: output
251 WriteHalfword(serial_handle, 0x05FFFFC6, reg);
253 reg = ReadHalfword(serial_handle, 0x05FFFFC2); // PBDR
254 reg &= ~0x0010; // clear PB4 to power down
255 WriteHalfword(serial_handle, 0x05FFFFC2, reg);
257 printf("Harddisk powered down.\n");
261 if (gCmd.bReadID)
263 UINT8 bMan, bID;
264 ReadID(serial_handle, 0x02000000, &bMan, &bID);
265 printf("Manufacturer ID = 0x%02X, Device ID = 0x%02X\n", bMan, bID);
269 if (gCmd.szFlashfile)
271 // flash a firmware file
272 printf("Flashing file %s...", gCmd.szFlashfile);
273 pFile = fopen(gCmd.szFlashfile, "rb");
274 if (pFile == NULL)
276 printf("\nFlash file %s not found, exiting.\n", gCmd.szFlashfile);
277 return -2;
279 size = fread(abFirmware, 1, sizeof(abFirmware), pFile);
280 fclose (pFile);
282 EraseChip(serial_handle, 0x02000000);
283 FlashByteMultiple(serial_handle, 0x02000000, size, abFirmware);
284 printf("\b\b\b done.\n");
288 if (gCmd.szDumpfile)
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)
296 printf("\nDump file %s cannot be opened, exiting.\n", gCmd.szDumpfile);
297 return -3;
299 fwrite(abFirmware, 1, sizeof(abFirmware), pFile);
300 fclose (pFile);
301 printf("\b\b\b done.\n");
305 if (gCmd.szExecfile)
307 UINT32 size;
309 printf("Downloading program...");
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
334 // download Rockbox/gdb
335 pFile = fopen(gCmd.szExecfile, "rb");
336 if (pFile == NULL)
338 printf("\nExecutable file %s cannot be opened, exiting.\n", gCmd.szExecfile);
339 return -3;
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");
347 // start rockbox/gdb
348 printf("Starting program...");
349 Execute(serial_handle, 0x09000200, false);
350 printf("\b\b\b done.\n");
354 if (gCmd.bBlink)
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)
362 byte ^= 0x40;
363 WriteByte(serial_handle, 0x05FFFFC3, byte);
364 Sleep(200);
368 return 0;