| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <fcntl.h> |
| #include <unistd.h> |
| #include <sys/ioctl.h> |
| |
| #define CPMEM_DEVICE "/dev/cpmem" |
| #define CPLOAD_IOC_MAGIC 'Z' |
| #define CPLOAD_IOCTL_SET_CP_ADDR _IOW(CPLOAD_IOC_MAGIC, 1, int) |
| #define CPLOAD_IOCTL_GET_CP_FUSE _IOR(CPLOAD_IOC_MAGIC, 2, int) |
| |
| static int getUniqueKey(unsigned char *key, int keylen) |
| { |
| unsigned char ukey[24]; |
| int fd; |
| |
| if (keylen < 16) |
| { |
| fprintf(stderr, "Error: Key length must be at least 16 bytes.\n"); |
| return -1; |
| } |
| |
| fd = open(CPMEM_DEVICE, O_RDONLY); |
| if (fd == -1) |
| { |
| perror("Error opening device"); |
| return -1; |
| } |
| |
| memset(ukey, 0, sizeof(ukey)); |
| |
| if (ioctl(fd, CPLOAD_IOCTL_GET_CP_FUSE, ukey) == -1) |
| { |
| perror("Error in ioctl"); |
| close(fd); |
| return -1; |
| } |
| |
| close(fd); |
| |
| memcpy(key, ukey, 8); |
| memcpy(&key[8], ukey, 8); |
| |
| return 0; |
| } |
| |
| int main() |
| { |
| unsigned char key[16]; |
| int ret; |
| |
| ret = getUniqueKey(key, sizeof(key)); |
| if (ret == 0) |
| { |
| printf("Unique Key: "); |
| for (int i = 0; i < sizeof(key)/2; i++) |
| { |
| printf("%02x", key[i]); |
| } |
| printf("\n"); |
| } |
| else |
| { |
| fprintf(stderr, "Failed to get unique key.\n"); |
| return EXIT_FAILURE; |
| } |
| |
| return EXIT_SUCCESS; |
| } |