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