b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <unistd.h> |
| 6 | #include <sys/ioctl.h> |
| 7 | |
| 8 | #define CPMEM_DEVICE "/dev/cpmem" |
| 9 | #define CPLOAD_IOC_MAGIC 'Z' |
| 10 | #define CPLOAD_IOCTL_SET_CP_ADDR _IOW(CPLOAD_IOC_MAGIC, 1, int) |
| 11 | #define CPLOAD_IOCTL_GET_CP_FUSE _IOR(CPLOAD_IOC_MAGIC, 2, int) |
| 12 | |
| 13 | static int getUniqueKey(unsigned char *key, int keylen) |
| 14 | { |
| 15 | unsigned char ukey[24]; |
| 16 | int fd; |
| 17 | |
| 18 | if (keylen < 16) |
| 19 | { |
| 20 | fprintf(stderr, "Error: Key length must be at least 16 bytes.\n"); |
| 21 | return -1; |
| 22 | } |
| 23 | |
| 24 | fd = open(CPMEM_DEVICE, O_RDONLY); |
| 25 | if (fd == -1) |
| 26 | { |
| 27 | perror("Error opening device"); |
| 28 | return -1; |
| 29 | } |
| 30 | |
| 31 | memset(ukey, 0, sizeof(ukey)); |
| 32 | |
| 33 | if (ioctl(fd, CPLOAD_IOCTL_GET_CP_FUSE, ukey) == -1) |
| 34 | { |
| 35 | perror("Error in ioctl"); |
| 36 | close(fd); |
| 37 | return -1; |
| 38 | } |
| 39 | |
| 40 | close(fd); |
| 41 | |
| 42 | memcpy(key, ukey, 8); |
| 43 | memcpy(&key[8], ukey, 8); |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | int main() |
| 49 | { |
| 50 | unsigned char key[16]; |
| 51 | int ret; |
| 52 | |
| 53 | ret = getUniqueKey(key, sizeof(key)); |
| 54 | if (ret == 0) |
| 55 | { |
| 56 | printf("Unique Key: "); |
| 57 | for (int i = 0; i < sizeof(key)/2; i++) |
| 58 | { |
| 59 | printf("%02x", key[i]); |
| 60 | } |
| 61 | printf("\n"); |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | fprintf(stderr, "Failed to get unique key.\n"); |
| 66 | return EXIT_FAILURE; |
| 67 | } |
| 68 | |
| 69 | return EXIT_SUCCESS; |
| 70 | } |