| 1 | /*************************************************************************** |
| 2 | cmotfile.cxx - class for handling Motorola files. |
| 3 | |
| 4 | begin : Wed Sep 25 18:58:35 BST 2002 |
| 5 | copyright : (C) 2002 by Greg Reynolds, Jim Hawkridge |
| 6 | email : [email protected] |
| 7 | |
| 8 | 2005-11-17: modified by Thomas Fischl <[email protected]> |
| 9 | - added support for S1-Files |
| 10 | - new parse method |
| 11 | |
| 12 | ***************************************************************************/ |
| 13 | |
| 14 | /*************************************************************************** |
| 15 | * * |
| 16 | * This program is free software; you can redistribute it and/or modify * |
| 17 | * it under the terms of the GNU General Public License as published by * |
| 18 | * the Free Software Foundation; either version 2 of the License, or * |
| 19 | * (at your option) any later version. * |
| 20 | * * |
| 21 | ***************************************************************************/ |
| 22 | #include "cmotfile.h" |
| 23 | |
| 24 | CMOTFile::CMOTFile() |
| 25 | { |
| 26 | m_fp = NULL; |
| 27 | m_pHead = NULL; |
| 28 | m_pTail = NULL; |
| 29 | } |
| 30 | |
| 31 | CMOTFile::~CMOTFile() |
| 32 | { |
| 33 | |
| 34 | } |
| 35 | |
| 36 | int CMOTFile::readFile(const char* pszFilename, int nPageSize) |
| 37 | { |
| 38 | assert( pszFilename ); |
| 39 | |
| 40 | unsigned int nType; |
| 41 | unsigned int nLength; |
| 42 | |
| 43 | m_fp = fopen(pszFilename, "r"); |
| 44 | |
| 45 | if( m_fp == NULL ) { |
| 46 | printf("\nM16 Flash ERROR: couldn't open specified file."); |
| 47 | fflush(stdout); |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | if( fscanf(m_fp, "S%1u%2X", &nType, &nLength) != 2 ) { |
| 52 | printf("\nM16 Flash ERROR: badly formatted S-record file."); |
| 53 | fflush(stdout); |
| 54 | fclose(m_fp); |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | if( nType != 0 ) { |
| 59 | printf("\nM16 Flash ERROR: there must be an S0 record (a dummy will do)."); |
| 60 | fflush(stdout); |
| 61 | fclose(m_fp); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | /* if we got here then there is an S0 record which is a good start */ |
| 66 | /* advance m_fp to point to the start of the next line */ |
| 67 | fscanf(m_fp, "%*[^\n]s"); |
| 68 | fscanf(m_fp, "\n"); |
| 69 | |
| 70 | /* we should check for \r\n in case S records created on windows */ |
| 71 | |
| 72 | /* now read the file into our linked list of pages */ |
| 73 | if( processFile(nPageSize) == 0 ) |
| 74 | return 0; |
| 75 | else |
| 76 | return 1; |
| 77 | } |
| 78 | |
| 79 | /* return page which belongs to given address. NULL if page does not exist */ |
| 80 | CPage* CMOTFile::getPageToAddress(int nPageSize, long lAddress) |
| 81 | { |
| 82 | long lBaseAddress = lAddress - (lAddress % nPageSize); |
| 83 | |
| 84 | /* if no page in list */ |
| 85 | if (m_pTail == NULL) return NULL; |
| 86 | |
| 87 | if (m_pTail->getAddress() == lBaseAddress) return m_pTail; |
| 88 | |
| 89 | /* search for page with given base address */ |
| 90 | CPage* pCurrent = m_pHead; |
| 91 | do { |
| 92 | if (pCurrent->getAddress() == lBaseAddress) return pCurrent; |
| 93 | pCurrent = pCurrent->getNext(); |
| 94 | } while (pCurrent != NULL); |
| 95 | |
| 96 | /* no page found */ |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | /* convert the file contents into a linked list of pPages */ |
| 101 | int CMOTFile::processFile(int nPageSize) |
| 102 | { |
| 103 | assert( m_fp ); |
| 104 | assert( nPageSize > 0 ); |
| 105 | |
| 106 | unsigned int nType; |
| 107 | unsigned int nLength; |
| 108 | unsigned long lAddress; |
| 109 | unsigned int nVal; |
| 110 | unsigned int n; |
| 111 | |
| 112 | |
| 113 | while( true ) { |
| 114 | |
| 115 | /* read a line's header data */ |
| 116 | if( fscanf(m_fp, "S%1u%2X", &nType, &nLength) != 2 ) { |
| 117 | printf("\nM16 Flash ERROR: badly formatted input file."); |
| 118 | fflush(stdout); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | /* extract address */ |
| 123 | switch (nType) { |
| 124 | case 1: |
| 125 | // 16 bit addresses |
| 126 | if( fscanf(m_fp, "%4lX", &lAddress) != 1 ) { |
| 127 | printf("\nM16 Flash ERROR: bad address format."); |
| 128 | fflush(stdout); |
| 129 | return 0; |
| 130 | } |
| 131 | nLength-=3; |
| 132 | break; |
| 133 | case 2: |
| 134 | // 24 bit addresses |
| 135 | if( fscanf(m_fp, "%6lX", &lAddress) != 1 ) { |
| 136 | printf("\nM16 Flash ERROR: bad address format."); |
| 137 | fflush(stdout); |
| 138 | return 0; |
| 139 | } |
| 140 | nLength-=4; |
| 141 | break; |
| 142 | default: |
| 143 | // no address |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | /* process file */ |
| 148 | switch (nType) { |
| 149 | case 1: |
| 150 | case 2: |
| 151 | |
| 152 | CPage* page; |
| 153 | /* now read the data */ |
| 154 | for( n = 0; n < nLength; n++ ) { |
| 155 | |
| 156 | if( fscanf(m_fp, "%2X", &nVal) != 1 ) { |
| 157 | printf("\nM16 Flash ERROR: bad data record in file."); |
| 158 | fflush(stdout); |
| 159 | fclose(m_fp); |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | page = getPageToAddress(nPageSize, lAddress); |
| 164 | if (page == NULL) { |
| 165 | /* new page */ |
| 166 | |
| 167 | page = new CPage(nPageSize); |
| 168 | page->setAddress(lAddress - (lAddress % nPageSize)); |
| 169 | |
| 170 | if (m_pHead == NULL) m_pHead = page; |
| 171 | else m_pTail->setNext(page); |
| 172 | |
| 173 | m_pTail = page; |
| 174 | } |
| 175 | |
| 176 | // printf("adr: %6lX base: %6lX val: %2X\n", lAddress, page->getAddress(), nVal); |
| 177 | page->setByte(lAddress % nPageSize, nVal); |
| 178 | |
| 179 | lAddress++; |
| 180 | } |
| 181 | |
| 182 | break; |
| 183 | |
| 184 | case 8: |
| 185 | case 9: |
| 186 | /* S8 and S9 indicates end of file, so clean exit */ |
| 187 | fclose(m_fp); |
| 188 | return 8; |
| 189 | break; |
| 190 | case 0: |
| 191 | /* header do nothing */ |
| 192 | break; |
| 193 | default: |
| 194 | /* unknown record type */ |
| 195 | printf("\nM16 Flash ERROR: unknown record type"); |
| 196 | fflush(stdout); |
| 197 | fclose(m_fp); |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | /* loose the checksum and end of line */ |
| 202 | fscanf(m_fp, "%*[^\n]"); |
| 203 | fscanf(m_fp, "\n"); |
| 204 | } |
| 205 | } |