//------------------------------------------------------------------------------ extern "C" char *FILENAME_VersionId(void) { return "%W% %G%"; } #include // edk.h? #include static char *rcsId = "$Id$"; #ifdef _DEBUG # define new DEBUG_NEW # undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif //------------------------------------------------------------------------------ static int commPortNumber = 1; static HANDLE hPortHandle = NULL; // Handle to COM port static char s19Name[128] = "megasquirt.s19"; bool openCommPort() { char pPortName[6]; sprintf(pPortName, "com%d", commPortNumber); hPortHandle = CreateFile(pPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hPortHandle == INVALID_HANDLE_VALUE) return false; DCB dcb; if (!GetCommState(hPortHandle, &dcb)) return false; dcb.BaudRate = 9600; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; if (!SetCommState(hPortHandle, &dcb)) return false; COMMTIMEOUTS timeout; if (!GetCommTimeouts(hPortHandle, &timeout)) return false; timeout.ReadIntervalTimeout = 0; // ms between chars on read. timeout.ReadTotalTimeoutMultiplier = 0; timeout.ReadTotalTimeoutConstant = 250; timeout.WriteTotalTimeoutMultiplier = 0; timeout.WriteTotalTimeoutConstant = 0; if (!SetCommTimeouts(hPortHandle, &timeout)) return false; return true; } //------------------------------------------------------------------------------ void closeCommPort() { if (hPortHandle != NULL) { CloseHandle(hPortHandle); hPortHandle = NULL; } } //------------------------------------------------------------------------------ // readCommPort - Read char from from the COM port. Blocks if no char available. bool readCommPort(BYTE &pChar) { DWORD dwActualRead; bool bOK = 0 != ReadFile(hPortHandle, &pChar, 1, &dwActualRead, NULL); if (dwActualRead != 1) bOK = false; return bOK; } //------------------------------------------------------------------------------ bool writeCommPort(char *pBuf, DWORD dwCount) { DWORD dwBytesWritten; bool bOK = 0 != WriteFile(hPortHandle, pBuf, dwCount, &dwBytesWritten, NULL); if (dwBytesWritten != dwCount) bOK = false; return bOK; } //------------------------------------------------------------------------------ //-- -- //-- Possible boot loader messages: -- //-- -- //-- ascii_CR,ascii_LF,'Boot>' -- //-- ' (P)rogram (W)ipe (U)pgrade e(X)it' -- //-- ' Complete' -- //-- ' - waiting ...' -- //-- ' - error' -- //-- ' - what?' -- //-- ' - Reset Vector Invalid' -- //-- -- //------------------------------------------------------------------------------ #define LF "\012" #define CR "\015" //------------------------------------------------------------------------------ void send(char *msg) { writeCommPort(msg, strlen(msg)); } //------------------------------------------------------------------------------ void recv(char *response) { char inputBuffer[128]; BYTE p; int len = strlen(response); for (int i = 0, j = 0; i <= len; i++ ) { if (readCommPort(p)) { inputBuffer[j++] = p; putchar(p); } } if (j > len) j = len; inputBuffer[j] = '\0'; if (strcmp(response, inputBuffer)) { fprintf(stderr, "Download failed:\n" " Expected response \"%s\",\n" " but received this \"%s\".\n", response, inputBuffer); exit(2); } } //------------------------------------------------------------------------------ void sendFile() { FILE *s19 = fopen(s19Name, "rb"); if (s19 == NULL) { fprintf(stderr, "Download failed:\n" " Could not open file '%s'.\n", s19Name); exit(3); } char lineBuffer[128]; int lineNumber = 0; printf("\n"); while (fgets(lineBuffer, sizeof(lineBuffer), s19)) { ++lineNumber; if (lineNumber % 10 == 0) printf("Line %4d\r", lineNumber); writeCommPort(lineBuffer, strlen(lineBuffer)); } printf("File sent, %d lines.\n", lineNumber); fclose(s19); } //------------------------------------------------------------------------------ int main(int argc, char *argv[]) { printf("MS Download %d.%02d\n", MAJOR, MINOR); if (argc == 2) strcpy(s19Name, argv[1]); FILE *s19 = fopen(s19Name, "rb"); if (s19 == NULL) { fprintf(stderr, "Download failed:\n" " Could not open file '%s'.\n", s19Name); exit(3); } fclose(s19); if (!openCommPort()) { fprintf(stderr, "Download failed:\n Could not open comm port %d.", commPortNumber); exit(1); } send(CR); recv(CR LF "Boot>"); send("u"); recv("u - waiting ..."); sendFile(); recv(" Complete" CR); send("x"); closeCommPort(); exit(0); return 0; } //------------------------------------------------------------------------------