Commit | Line | Data |
---|---|---|
3c2a0909 S |
1 | #include <linux/module.h> |
2 | #include <linux/kernel.h> | |
3 | #include <linux/init.h> | |
4 | #include <linux/types.h> | |
5 | #include <linux/proc_fs.h> | |
6 | #include <linux/seq_file.h> | |
7 | #include <linux/mm.h> | |
8 | #include <linux/highmem.h> | |
9 | #include <linux/io.h> | |
10 | #include <linux/types.h> | |
11 | ||
12 | #define SECURE_LOG 0 | |
13 | #define DEBUG_LOG 1 | |
14 | ||
15 | #define DEBUG_LOG_START (0x47402000) | |
16 | #define SECURE_LOG_START (0x47502000) | |
17 | #define DEBUG_RKP_LOG_START (0x52300000) | |
18 | #define SECURE_RKP_LOG_START (0x52400000) | |
19 | ||
20 | #define DEBUG_LOG_SIZE (1<<20) | |
21 | #define DEBUG_LOG_MAGIC (0xaabbccdd) | |
22 | #define DEBUG_LOG_ENTRY_SIZE 128 | |
23 | ||
9aaf89b0 S |
24 | #define TIMA_DEBUG_LOG_SIZE 1<<18 |
25 | #define TIMA_SEC_LOG_SIZE 0x7000 | |
26 | ||
3c2a0909 S |
27 | typedef struct debug_log_entry_s |
28 | { | |
29 | uint32_t timestamp; /* timestamp at which log entry was made*/ | |
30 | uint32_t logger_id; /* id is 1 for tima, 2 for lkmauth app */ | |
31 | #define DEBUG_LOG_MSG_SIZE (DEBUG_LOG_ENTRY_SIZE - sizeof(uint32_t) - sizeof(uint32_t)) | |
32 | char log_msg[DEBUG_LOG_MSG_SIZE]; /* buffer for the entry */ | |
33 | } __attribute__ ((packed)) debug_log_entry_t; | |
34 | ||
35 | typedef struct debug_log_header_s | |
36 | { | |
37 | uint32_t magic; /* magic number */ | |
38 | uint32_t log_start_addr; /* address at which log starts */ | |
39 | uint32_t log_write_addr; /* address at which next entry is written*/ | |
40 | uint32_t num_log_entries; /* number of log entries */ | |
41 | char padding[DEBUG_LOG_ENTRY_SIZE - 4 * sizeof(uint32_t)]; | |
42 | } __attribute__ ((packed)) debug_log_header_t; | |
43 | ||
44 | #define DRIVER_DESC "A kernel module to read tima debug log" | |
45 | ||
46 | unsigned long *tima_log_addr = 0; | |
47 | unsigned long *tima_debug_log_addr = 0; | |
48 | unsigned long *tima_secure_log_addr = 0; | |
49 | unsigned long *tima_debug_rkp_log_addr = 0; | |
50 | unsigned long *tima_secure_rkp_log_addr = 0; | |
51 | ||
52 | ssize_t tima_read(struct file *filep, char __user *buf, size_t size, loff_t *offset) | |
53 | { | |
54 | /* First check is to get rid of integer overflow exploits */ | |
55 | if (size > DEBUG_LOG_SIZE || (*offset) + size > DEBUG_LOG_SIZE) { | |
56 | printk(KERN_ERR"Extra read\n"); | |
57 | return -EINVAL; | |
58 | } | |
59 | ||
60 | if (!strcmp(filep->f_path.dentry->d_iname, "tima_secure_log")) | |
61 | tima_log_addr = tima_secure_log_addr; | |
62 | else if( !strcmp(filep->f_path.dentry->d_iname, "tima_debug_log")) | |
63 | tima_log_addr = tima_debug_log_addr; | |
64 | else if(!strcmp(filep->f_path.dentry->d_iname, "tima_debug_rkp_log")) { | |
65 | if (*offset >= TIMA_DEBUG_LOG_SIZE) { | |
66 | return -EINVAL; | |
67 | } else if (*offset + size > TIMA_DEBUG_LOG_SIZE) { | |
68 | size = (TIMA_DEBUG_LOG_SIZE) - *offset; | |
69 | } | |
70 | tima_log_addr = tima_debug_rkp_log_addr; | |
71 | } | |
72 | else if(!strcmp(filep->f_path.dentry->d_iname, "tima_secure_rkp_log")) { | |
73 | if (*offset >= TIMA_SEC_LOG_SIZE) { | |
74 | return -EINVAL; | |
75 | } else if (*offset + size > TIMA_SEC_LOG_SIZE) { | |
76 | size = (TIMA_SEC_LOG_SIZE) - *offset; | |
77 | } | |
78 | tima_log_addr = tima_secure_rkp_log_addr; | |
79 | } | |
80 | ||
81 | if (copy_to_user(buf, (const char *)tima_log_addr + (*offset), size)) { | |
82 | printk(KERN_ERR"Copy to user failed\n"); | |
83 | return -1; | |
84 | } else { | |
85 | *offset += size; | |
86 | return size; | |
87 | } | |
88 | } | |
89 | ||
90 | static const struct file_operations tima_proc_fops = { | |
91 | .read = tima_read, | |
92 | }; | |
93 | ||
94 | /** | |
95 | * tima_debug_log_read_init - Initialization function for TIMA | |
96 | * | |
97 | * It creates and initializes tima proc entry with initialized read handler | |
98 | */ | |
99 | static int __init tima_debug_log_read_init(void) | |
100 | { | |
101 | if (proc_create("tima_debug_log", 0644,NULL, &tima_proc_fops) == NULL) { | |
102 | printk(KERN_ERR"tima_debug_log_read_init: Error creating proc entry\n"); | |
103 | goto error_return; | |
104 | } | |
105 | if (proc_create("tima_secure_log", 0644,NULL, &tima_proc_fops) == NULL) { | |
106 | printk(KERN_ERR"tima_secure_log_read_init: Error creating proc entry\n"); | |
107 | goto remove_debug_entry; | |
108 | } | |
109 | printk(KERN_INFO"tima_debug_log_read_init: Registering /proc/tima_debug_log Interface \n"); | |
110 | ||
111 | if (proc_create("tima_debug_rkp_log", 0644,NULL, &tima_proc_fops) == NULL) { | |
112 | printk(KERN_ERR"tima_debug_rkp_log_read_init: Error creating proc entry\n"); | |
113 | goto remove_secure_entry; | |
114 | } | |
115 | if (proc_create("tima_secure_rkp_log", 0644,NULL, &tima_proc_fops) == NULL) { | |
116 | printk(KERN_ERR"tima_secure_rkp_log_read_init: Error creating proc entry\n"); | |
117 | goto remove_debug_rkp_entry; | |
118 | } | |
119 | ||
120 | tima_debug_log_addr = (unsigned long *)phys_to_virt(DEBUG_LOG_START); | |
121 | tima_secure_log_addr = (unsigned long *)phys_to_virt(SECURE_LOG_START); | |
122 | tima_debug_rkp_log_addr = (unsigned long *)phys_to_virt(DEBUG_RKP_LOG_START); | |
123 | tima_secure_rkp_log_addr = (unsigned long *)phys_to_virt(SECURE_RKP_LOG_START); | |
124 | return 0; | |
125 | ||
126 | remove_debug_rkp_entry: | |
127 | remove_proc_entry("tima_debug_rkp_log", NULL); | |
128 | remove_secure_entry: | |
129 | remove_proc_entry("tima_secure_log", NULL); | |
130 | remove_debug_entry: | |
131 | remove_proc_entry("tima_debug_log", NULL); | |
132 | error_return: | |
133 | return -1; | |
134 | } | |
135 | ||
136 | /** | |
137 | * tima_debug_log_read_exit - Cleanup Code for TIMA | |
138 | * | |
139 | * It removes /proc/tima proc entry and does the required cleanup operations | |
140 | */ | |
141 | static void __exit tima_debug_log_read_exit(void) | |
142 | { | |
143 | remove_proc_entry("tima_debug_log", NULL); | |
144 | remove_proc_entry("tima_secure_log", NULL); | |
145 | remove_proc_entry("tima_debug_rkp_log", NULL); | |
146 | remove_proc_entry("tima_secure_rkp_log", NULL); | |
147 | printk(KERN_INFO"Deregistering /proc/tima_debug_log Interface\n"); | |
148 | } | |
149 | ||
150 | module_init(tima_debug_log_read_init); | |
151 | module_exit(tima_debug_log_read_exit); | |
152 | ||
153 | MODULE_DESCRIPTION(DRIVER_DESC); |