xref: /haiku/src/tools/fixup_tos_boot_checksum/fixup_tos_boot_checksum.c (revision aed95f3c6cf74b6c76a50ea214170aef2246385c)
189b87f2bSFrançois Revol #include <fcntl.h>
2f5be3a12SFrançois Revol #include <stdio.h>
3f5be3a12SFrançois Revol #include <stdint.h>
489b87f2bSFrançois Revol #include <unistd.h>
5f5be3a12SFrançois Revol 
6f5be3a12SFrançois Revol uint8_t sector[512];
7f5be3a12SFrançois Revol 
main(int argc,char ** argv)8f5be3a12SFrançois Revol int main(int argc, char **argv)
9f5be3a12SFrançois Revol {
10f5be3a12SFrançois Revol 	int fd, i;
11f5be3a12SFrançois Revol 	uint16_t sum;
12f5be3a12SFrançois Revol 	uint8_t *p = sector;
13f5be3a12SFrançois Revol 	fd = open(argv[1], O_RDWR);
14f5be3a12SFrançois Revol 	if (fd < 0) {
15f5be3a12SFrançois Revol 		return 1;
16f5be3a12SFrançois Revol 	}
17f5be3a12SFrançois Revol 	if (read(fd, sector, 512-2) < 512-2) {
18f5be3a12SFrançois Revol 		perror("read");
19f5be3a12SFrançois Revol 		return 1;
20f5be3a12SFrançois Revol 	}
21f5be3a12SFrançois Revol 	for (sum = 0, i = 0; i < (512-2)/2; i++) {
22f5be3a12SFrançois Revol 		uint16_t v;
23f5be3a12SFrançois Revol 		v = *p++ << 8;
24f5be3a12SFrançois Revol 		v += *p++;
25f5be3a12SFrançois Revol 		sum += v;
26f5be3a12SFrançois Revol 	}
27f5be3a12SFrançois Revol 	sum = 0x1234 - sum /*+ 1*/;
28f5be3a12SFrançois Revol 	//sum = 0xaa55;
29f5be3a12SFrançois Revol 	// big endian
30f5be3a12SFrançois Revol 	*p++ = (uint8_t)(sum >> 8);
31f5be3a12SFrançois Revol 	*p++ = (uint8_t)sum;
32f5be3a12SFrançois Revol 	//lseek(fd, 0LL, SEEK_SET);
33f5be3a12SFrançois Revol 	write(fd, &sector[512-2], 2);
34*aed95f3cSFrançois Revol 	close(fd);
35f5be3a12SFrançois Revol 	return 0;
36f5be3a12SFrançois Revol }
37