#include #include #include int main(int argc, char **argv) { int fd; int ret; char buf[512]; if (argc != 2) errx(1, "usage: %s device", argv[0]); printf("opening raw device %s\n", argv[1]); fd = open(argv[1], O_RDWR); if (fd < 0) err(1, "open"); ret = read(fd, buf, sizeof(buf)); if (ret != sizeof(buf)) err(1, "read"); printf("magic code: 0x%02hhx%02hhx\n", buf[510], buf[511]); if (*(unsigned short *)&buf[510] != htons(0x55aa)) errx(1, "magic code"); if (memcmp(&buf[0x03], "OpenBSD", 7) || memcmp(&buf[0x2b], "UNIX", 4) || memcmp(&buf[0x30], "LABEL\0UFS 4.4\0", 14)) errx(1, "not an OpenBSD filesystem"); if (*(unsigned int *)&buf[0x56] != htonl(0xb402cd16) || buf[0x5a] != 0x0c || *(unsigned int *)&buf[0x5c] != htonl(0xa8037403) || buf[0x60] != 0x4e || *(unsigned short *)&buf[0x61] != htons(0x30f6)) errx(1, "bad opcode"); printf("old force_chs value: 0x%02hhx\n", buf[0x5b]); if (buf[0x5b] != 0x00) errx(1, "force_chs value seems to already be set... exit!"); printf("set it to 0x01...\n"); buf[0x5b] = 0x01; printf("writing new 512-bytes block...\n"); ret = lseek(fd, 0, SEEK_SET); if (ret == -1) err(1, "lseek"); ret = write(fd, buf, sizeof(buf)); if (ret != sizeof(buf)) err(1, "write"); printf("success!\n"); }