SYS_serialNumber.c File Reference


Detailed Description

This module provides the serial number support functions.

REVISION

Definition in file SYS_serialNumber.c.

#include "../GENERIC/generic.h"
#include "../GENERIC/eeprom.h"
#include "../MENU/tmplt.h"
#include "../fat/fat.h"
#include "sys_hardware.h"
#include "sys_assert.h"

Include dependency graph for SYS_serialNumber.c:

Include dependency graph

Go to the source code of this file.

Functions

int SYS_serialNumber (int cmd, unsigned short *strSN)
 abxxxxxcd


Function Documentation

int SYS_serialNumber int  cmd,
unsigned short *  strSN
 

abxxxxxcd

Parameters:
cmd Serial number file read/write command.
strSN Current SN string.
Returns:
-1 if file was not present at read.

Definition at line 58 of file SYS_serialNumber.c.

References FAT_cd(), FAT_fclose(), FAT_fopen(), FAT_fread(), FAT_fwrite(), GEN_ascii2hex(), GEN_eepromRead(), GEN_eepromWrite(), GEN_hex2ascii(), rb, SYS_die(), and wb.

Referenced by UI_PSF_null().

00059 {
00060     File *pSerialNumberFile;
00061     unsigned short SerialNumber[((SNLEN+1)/2)+2];
00062     unsigned short SerialNumber2[((SNLEN+1)/2)+2];
00063     unsigned short SerialNumberFilename[8];
00064     int Checksum;
00065     int ret = -1;
00066     int i;
00067     int chk;
00068     long lTmp;
00069 
00070     FAT_cd(strDISKD);   
00071     
00072     GEN_readXstr((ULONG)text_snFname, (USHORT *)SerialNumberFilename, 10);
00073 
00074     // +4 for 16-bit checksum
00075     // Serial number is stored and processed as two bytes per word.
00076     // For serial numbers to be written to eeprom, the checksum must be in 2's compliment
00077     
00078     switch (cmd)
00079     {
00080         case SN_FILEWRITE:          
00081             // Read EEPROM and create SN.txt if not already created.
00082             GEN_eepromRead(SERIAL_NUMBER_BEGIN, SNLEN+2, (int *)&SerialNumber[0]);
00083             Checksum = _SNChecksum(CHECKSUM_NORMAL, &SerialNumber[0]);
00084             
00085             if (Checksum != SerialNumber[sizeof(SerialNumber)-2])
00086             {
00087                 unsigned short DefaultSN[SNLEN];
00088                 
00089                 /*GEN_readXdata((ULONG)text_defaultSN, (USHORT *)DefaultSN, SNLEN);
00090                 for (i = 0; i < (sizeof(DefaultSN)-2); i += 2)
00091                 {
00092                     SerialNumber[i/2] = (DefaultSN[i]<<8)|DefaultSN[i+1];
00093                 }
00094                 */
00095                 // DefaultSN comes as packed.----12/19/03. MG
00096                 GEN_readXdata((ULONG)text_defaultSN, (USHORT*)&SerialNumber[0], (SNLEN+1)/2);
00097                 
00098                 SerialNumber[sizeof(SerialNumber)-2] = _SNChecksum(CHECKSUM_NORMAL, &SerialNumber[0]);
00099                 GEN_eepromWrite(SERIAL_NUMBER_BEGIN, SNLEN+2, (int *)&SerialNumber[0]);
00100             }
00101             
00102             if ((pSerialNumberFile = FAT_fopen((char *)&SerialNumberFilename[0], rb )) != NULL)
00103             {
00104                 if ((FAT_fread(pSerialNumberFile, &SerialNumber2[0], ((SNLEN+1)/2)+2 ) < ((SNLEN+1)/2)+1))
00105                 {
00106                     // File corrupted, set flag to regenerate. M.Gao 10/16/2002.
00107                     chk = 0;
00108                 }
00109                 FAT_fclose(pSerialNumberFile );
00110 
00111                 chk = 1;
00112                 for (i = 0; i < (SNLEN+1)/2; i++)
00113                 {
00114                     if (SerialNumber[i] != SerialNumber2[i])
00115                     {
00116                         chk = 0;
00117                         break;
00118                     }           
00119                 }
00120                 ret = 0;
00121             }
00122             else chk = 0;
00123             
00124             // Change checksums to ASCII.
00125             lTmp = GEN_hex2ascii(SerialNumber[sizeof(SerialNumber)-2]);
00126             SerialNumber[sizeof(SerialNumber)-2] = (lTmp>>16)&0x0ffff;
00127             SerialNumber[sizeof(SerialNumber)-1] = lTmp&0x0ffff;
00128 
00129             if (!chk)
00130             {
00131                 if ((pSerialNumberFile = FAT_fopen((char *)&SerialNumberFilename[0], wb )) == NULL)
00132                 {
00133                     break;
00134                 }
00135                 
00136                 i = FAT_fwrite(pSerialNumberFile, &SerialNumber[0], (SNLEN+1)/2+2);
00137                 if (i < (SNLEN+1)/2+2) SYS_die(SYS_SERIALNUMBER); 
00138                 ret = 0;
00139                 FAT_fclose(pSerialNumberFile );
00140             }
00141             
00142             // Copy serial number into buffer.
00143             if(strSN)
00144             {
00145                 i = 2 + (SNLEN+1)/2;
00146                 memcpy(strSN, &SerialNumber[0], i);
00147                 strSN[i] = 0;
00148             }
00149             
00150             // Update the default serial number string for "properties" menu reference.
00151             GEN_writeXdata((ULONG)text_defaultSN, (USHORT*)&SerialNumber[0], (SNLEN+1)/2);
00152             
00153             break;
00154         
00155         case SN_FILEREAD:
00156             // Read back SN and updates EEPROM if valid SN detected.
00157             if ((pSerialNumberFile = FAT_fopen((char *)&SerialNumberFilename[0], rb )) == NULL)
00158             {
00159                 break;
00160             }
00161             if ((FAT_fread(pSerialNumberFile, &SerialNumber[0], ((SNLEN+1)/2)+2 ) < ((SNLEN+1)/2)+2))
00162             {
00163                 break;
00164             }
00165             FAT_fclose(pSerialNumberFile );
00166             
00167             // Copy serial number into buffer.
00168             if(strSN)
00169             {
00170                 i = 2 + (SNLEN+1)/2;
00171                 memcpy(strSN, &SerialNumber[0], i);
00172                 strSN[i] = 0;
00173             }
00174             
00175             lTmp = (long)SerialNumber[sizeof(SerialNumber)-2]<<16;
00176             lTmp |= SerialNumber[sizeof(SerialNumber)-1];
00177             SerialNumber[sizeof(SerialNumber)-2] = GEN_ascii2hex(lTmp);
00178 
00179             if ((Checksum = _SNChecksum(CHECKSUM_COMPL, &SerialNumber[0])) == SerialNumber[sizeof(SerialNumber)-2])
00180             {
00181                 // checksum in file is the compliment, therefore, it's OK to write
00182                 // the SN to EEPROM.  But first, we need to negate the checksum and
00183                 // replace the one read from the file with the inverse.
00184                 Checksum = -1*Checksum;
00185                 SerialNumber[sizeof(SerialNumber)-2] = Checksum;
00186                 GEN_eepromWrite(SERIAL_NUMBER_BEGIN, SNLEN+2, (int *)&SerialNumber[0]);
00187                 
00188                 if ((pSerialNumberFile = FAT_fopen((char *)&SerialNumberFilename[0], wb )) == NULL)
00189                 {
00190                     break;
00191                 }
00192                 
00193                 lTmp = GEN_hex2ascii(SerialNumber[sizeof(SerialNumber)-2]);
00194                 SerialNumber[sizeof(SerialNumber)-2] = (lTmp>>16)&0x0ffff;
00195                 SerialNumber[sizeof(SerialNumber)-1] = lTmp&0x0ffff;
00196                 
00197                 i = FAT_fwrite(pSerialNumberFile, &SerialNumber[0], (SNLEN+1)/2+2);
00198                 if (i < (SNLEN+1)/2+2) SYS_die(SYS_SERIALNUMBER2);  
00199                 ret = 0;
00200                 FAT_fclose(pSerialNumberFile );
00201                 
00202                 // Update the default serial number string for "properties" menu reference.
00203                 GEN_writeXdata((ULONG)text_defaultSN, (USHORT*)&SerialNumber[0], (SNLEN+1)/2);
00204             }
00205             break;
00206     }
00207     
00208     return(ret);
00209 }

Here is the call graph for this function:


Generated on Wed Jan 19 01:13:19 2005 for neuros-firmware by  doxygen 1.3.9.1