1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* **************************************************************************
4 This program creates a modified 16bit checksum used for the Netgear
5 DGN3500 series routers. The difference between this and a standard
6 checksum is that every 0x100 bytes added 0x100 have to be subtracted
9 (C) 2013 Marco Antonio Mauro <marcus90 at gmail.com>
11 Based on previous unattributed work.
13 ************************************************************************* */
21 unsigned char PidDataWW
[70] =
23 0x73, 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x59, 0x50, 0x35, 0x37, 0x32,
24 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x00, 0x37,
26 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73,
27 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D,
30 unsigned char PidDataDE
[70] =
32 0x73, 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x59, 0x50, 0x35, 0x37, 0x32,
33 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x37,
35 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73,
36 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D,
39 unsigned char PidDataNA
[70] =
41 0x73, 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x59, 0x50, 0x35, 0x37, 0x32,
42 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x00, 0x37,
44 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x73,
45 0x45, 0x72, 0x43, 0x6F, 0x4D, 0x6D,
48 /* *******************************************************************
49 Reads the file into memory and returns pointer to the buffer. */
50 static char *readfile(char *filename
, int *size
)
56 if (stat(filename
,&info
)!=0)
59 if ((fp
=fopen(filename
,"r"))==NULL
)
65 if ((buffer
=(char *)malloc(info
.st_size
+1))==NULL
)
68 if (fread(buffer
,1,info
.st_size
,fp
)!=info
.st_size
)
75 buffer
[info
.st_size
]='\0';
76 if(size
) *size
= info
.st_size
;
87 /* ******************************************************************* */
88 int main(int argc
, char** argv
)
90 unsigned long start
, i
;
91 char *endptr
, *buffer
, *p
;
92 int count
; // size of file in bytes
93 unsigned short sum
= 0, sum1
= 0;
94 char sumbuf
[8 + 8 + 1];
97 printf("ERROR: Argument missing!\n\nUsage %s filename starting offset in hex [PID code]\n\n", argv
[0]);
102 FILE *fp
= fopen(argv
[1], "a");
104 printf("ERROR: File not writeable!\n");
109 printf("%s: PID type: %s\n", argv
[0], argv
[3]);
110 if(strcmp(argv
[3], "DE")==0)
111 fwrite(PidDataDE
, sizeof(PidDataDE
), sizeof(char), fp
); /* write DE pid */
112 else if(strcmp(argv
[3], "NA")==0)
113 fwrite(PidDataNA
, sizeof(PidDataNA
), sizeof(char), fp
); /* write NA pid */
114 else /* if(strcmp(argv[3], "WW")) */
115 fwrite(PidDataWW
, sizeof(PidDataWW
), sizeof(char), fp
); /* write WW pid */
118 fwrite(PidDataWW
, sizeof(PidDataWW
), sizeof(char), fp
); /* write WW pid if unspecified */
122 /* Read the file to calculate the checksums */
123 buffer
= readfile(argv
[1], &count
);
125 printf("ERROR: File %s not found!\n", argv
[1]);
130 for(i
= 0; i
< count
; i
++)
135 start
= strtol(argv
[2], &endptr
, 16);
137 for(i
= 0; i
< count
- start
; i
++)
142 sprintf(sumbuf
,"%04X%04X",sum1
,sum
);
143 /* Append the 2 checksums to end of file */
144 fp
= fopen(argv
[1], "a");
146 printf("ERROR: File not writeable!\n");
149 fwrite(sumbuf
, 8, sizeof(char), fp
);