rpcd: iwinfo plugin fixes
[openwrt.git] / tools / firmware-utils / src / dgn3500sum.c
1 /* **************************************************************************
2
3    This program creates a modified 16bit checksum used for the Netgear
4    DGN3500 series routers. The difference between this and a standard
5    checksum is that every 0x100 bytes added 0x100 have to be subtracted
6    from the sum.
7
8    (C) 2013 Marco Antonio Mauro <marcus90 at gmail.com>
9
10    Based on previous unattributed work.
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 2 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program; if not, write to the Free Software
24    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
26  ************************************************************************* */
27
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/stat.h>
33
34 unsigned char PidDataWW[70] =
35 {
36     0x73, 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x59, 0x50, 0x35, 0x37, 0x32,
37     0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x00, 0x37,
39     0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73,
40     0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D,
41 } ;
42
43 unsigned char PidDataDE[70] =
44 {
45     0x73, 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x59, 0x50, 0x35, 0x37, 0x32,
46     0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x37,
48     0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73,
49     0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D,
50 } ;
51
52 unsigned char PidDataNA[70] =
53 {
54     0x73, 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x59, 0x50, 0x35, 0x37, 0x32,
55     0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x00, 0x37,
57     0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73,
58     0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D,
59 } ;
60
61 /* *******************************************************************
62    Reads the file into memory and returns pointer to the buffer. */
63 static char *readfile(char *filename, int *size)
64 {
65         FILE            *fp;
66         char            *buffer;
67         struct stat     info;
68
69         if (stat(filename,&info)!=0)
70                 return NULL;
71
72         if ((fp=fopen(filename,"r"))==NULL)
73                 return NULL;
74
75         buffer=NULL;
76         for (;;)
77         {
78                 if ((buffer=(char *)malloc(info.st_size+1))==NULL)
79                         break;
80
81                 if (fread(buffer,1,info.st_size,fp)!=info.st_size)
82                 {
83                         free(buffer);
84                         buffer=NULL;
85                         break;
86                 }
87
88                 buffer[info.st_size]='\0';
89                 if(size) *size = info.st_size;
90
91                 break;
92         }
93
94         (void)fclose(fp);
95
96         return buffer;
97 }
98
99
100 /* ******************************************************************* */
101 int main(int argc, char** argv)
102 {
103   unsigned long start, i;
104   char *endptr, *buffer, *p;
105   int count;  // size of file in bytes
106   unsigned short sum, sum1;
107   char sumbuf[9];
108
109   if(argc < 3) {
110     printf("ERROR: Argument missing!\n\nUsage %s filename starting offset in hex [PID code]\n\n", argv[0]);
111     return 1;
112   }
113
114
115   FILE *fp = fopen(argv[1], "a");
116   if(!fp) {
117     printf("ERROR: File not writeable!\n");
118     return 1;
119   }
120   if(argc = 4)
121   {
122     printf("%s: PID type: %s\n", argv[0], argv[3]);
123     if(strcmp(argv[3], "DE")==0)
124       fwrite(PidDataDE, sizeof(PidDataDE), sizeof(char), fp);  /* write DE pid */
125     else if(strcmp(argv[3], "NA")==0)
126       fwrite(PidDataNA, sizeof(PidDataNA), sizeof(char), fp);  /* write NA pid */
127     else /* if(strcmp(argv[3], "WW")) */
128       fwrite(PidDataWW, sizeof(PidDataWW), sizeof(char), fp);  /* write WW pid */
129   }
130   else
131     fwrite(PidDataWW, sizeof(PidDataWW), sizeof(char), fp);  /* write WW pid if unspecified */
132
133   fclose(fp);
134
135   /* Read the file to calculate the checksums */
136   buffer = readfile(argv[1], &count);
137   if(!buffer) {
138     printf("ERROR: File %s not found!\n", argv[1]);
139     return 1;
140   }
141
142   p = buffer;
143   for(i = 0; i < count; i++)
144   {
145         sum += p[i];
146   }
147
148   start = strtol(argv[2], &endptr, 16);
149   p = buffer+start;
150   sum1 = 0;
151   for(i = 0; i < count - start; i++)
152   {
153         sum1 += p[i];
154   }
155
156   sprintf(sumbuf,"%04X%04X",sum1,sum);
157   /* Append the 2 checksums to end of file */
158   fp = fopen(argv[1], "a");
159   if(!fp) {
160     printf("ERROR: File not writeable!\n");
161     return 1;
162   }
163   fwrite(sumbuf, 8, sizeof(char), fp);
164   fclose(fp);
165   free(buffer);
166   return 0;
167 }