--- C:/Users/MDKEND~1.ELE/AppData/Local/Temp/SDLoader.c-rev212.svn001.tmp.c Thu May 1 15:33:10 2014 +++ C:/Users/MDKEND~1.ELE/AppData/Local/Temp/SDLoader.c-rev213.svn001.tmp.c Thu May 1 17:58:42 2014 @@ -71,6 +71,7 @@ typedef struct stUPLOAD_HEADER /* local function prototype declarations */ /* =================================================================== */ +static int fnMatch(char *pat, char *str); static int fnUpdateSoftware(int iAppState, UTFILE *ptr_utFile, UPLOAD_HEADER *ptrFile_header); static unsigned short fnCRC16(unsigned short usCRC, unsigned char *ptrInput, unsigned long ulBlockSize); #if defined (_WINDOWS) || defined (_LITTLE_ENDIAN) @@ -142,25 +143,50 @@ extern void fnSD_loader(TTASKTABLE *ptrTaskTable) //utFile.ptr_utDirObject = ptr_utDirectory; utFile.ownerTask = OWN_TASK; _DISPLAY_SD_CARD_PRESENT(); // optionally display that card is ready - if (utOpenFile(NEW_SOFTWARE_FILE, &utFile, ptr_utDirectory, UTFAT_OPEN_FOR_READ) == UTFAT_PATH_IS_FILE) { // {7} - utReadFile(&utFile, (unsigned char *)&file_header, sizeof(file_header)); // read the file header - #if defined (_WINDOWS) || defined (_LITTLE_ENDIAN) - fnHeaderToLittleEndian(&file_header); - #endif - if (utFile.ulFileSize == (file_header.ulCodeLength + sizeof(UPLOAD_HEADER))) { // content length matches - if ((file_header.usMagicNumber == VALID_VERSION_MAGIC_NUMBER)) { // check test that the header version (magic number) is correct - iAppState = STATE_START_CHECKING; - iAppState = fnUpdateSoftware(iAppState, &utFile, &file_header); - return; + + ///////////////////////////////////// + + /* Find a software image file matching the required name format */ + if (utOpenDirectory("/", ptr_utDirectory) == UTFAT_SUCCESS) { + UTLISTDIRECTORY utListDirectory; + FILE_LISTING fileList = {0}; + CHAR cBuffer[MAX_UTFAT_FILE_NAME + DOS_STYLE_LIST_ENTRY_LENGTH]; + fileList.usMaxItems = 1; + fileList.ptrBuffer = cBuffer; + fileList.usBufferLength = sizeof(cBuffer); + fileList.ucStyle = DOS_TYPE_LISTING; + + utListDirectory.ptr_utDirObject = ptr_utDirectory; + utLocateDirectory("/", &utListDirectory); + while (utListDir(&utListDirectory, &fileList) != UTFAT_NO_MORE_LISTING_ITEMS_FOUND) { + cBuffer[fileList.usStringLength - 2] = '\0'; // terminate before /r/n + + if (fnMatch(NEW_SOFTWARE_FILE, &cBuffer[40])) { + fnDebugMsg(&cBuffer[40]); + fnDebugMsg(" - "); + if (utOpenFile(&cBuffer[40], &utFile, ptr_utDirectory, UTFAT_OPEN_FOR_READ) == UTFAT_PATH_IS_FILE) { // {7} + utReadFile(&utFile, (unsigned char *)&file_header, sizeof(file_header)); // read the file header + #if defined (_WINDOWS) || defined (_LITTLE_ENDIAN) + fnHeaderToLittleEndian(&file_header); + #endif + if (utFile.ulFileSize == (file_header.ulCodeLength + sizeof(UPLOAD_HEADER))) { // content length matches + if ((file_header.usMagicNumber == VALID_VERSION_MAGIC_NUMBER)) { // check test that the header version (magic number) is correct + iAppState = STATE_START_CHECKING; + iAppState = fnUpdateSoftware(iAppState, &utFile, &file_header); + return; + } + else { + _DISPLAY_INVALID_CONTENT(); // optionally display that the software file has been found but has invalid content + } + } + else { + _DISPLAY_INVALID_CONTENT(); // optionally display that the software file has been found but has invalid content + } + } } - else { - _DISPLAY_INVALID_CONTENT(); // optionally display that the software file has been found but has invalid content - } } - else { - _DISPLAY_INVALID_CONTENT(); // optionally display that the software file has been found but has invalid content - } } + else { _DISPLAY_NO_FILE(); // optionally display that the software file is not present on the SD card } @@ -179,6 +205,16 @@ extern void fnSD_loader(TTASKTABLE *ptrTaskTable) } } +static int fnMatch(char *pat, char *str) +{ + switch(*pat) { + case '\0': return !*str; + case '*': return fnMatch(pat+1, str) || *str && fnMatch(pat, str+1); + case '?': return *str && fnMatch(pat+1, str+1); + default: return *pat == *str && fnMatch(pat+1, str+1); + } +} + static void fnJumpToApplication(int iGo) // {3} { if ((iGo != 0)) { // if start required