-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsawa.c
181 lines (154 loc) · 5.68 KB
/
sawa.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
////////////////////////////////////////////////////////////////////////////
// The SaWa interface with the file server
////////////////////////////////////////////////////////////////////////////
#include <linux/init.h> /* module_init, module_exit */
#include <linux/module.h> /* version info, MODULE_LICENSE, MODULE_AUTHOR, printk() */
#include <linux/fs.h> /* file stuff */
#include <linux/kernel.h> /* printk() */
#include <linux/errno.h> /* error codes */
#include <linux/module.h> /* THIS_MODULE */
#include <linux/cdev.h> /* char device stuff */
#include <linux/uaccess.h> /* copy_to_user() */
#include <linux/genhd.h>
#include <linux/blkdev.h>
#include <linux/hdreg.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/interrupt.h>
#include <linux/socket.h>
#include <linux/netdevice.h>
#include <linux/tcp.h>
#include <linux/in.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include "tcp.h"
#define SAWA_INFO 0x01
#define SAWA_READ 0x02
#define SAWA_WRITE 0x03
#define SAWA_MSG_OK 0x42
#define SAWA_MSG_ERR 0x43
#define SAWA_MSG_NOAUTH 0x44
unsigned int nb_sectors;
void dump_mem(unsigned char *addr, int size) {
int i, j=0, col=0;
char buffer[64];
// printk(KERN_INFO "%d bytes\n", size);
while (1) {
if (j >= size) return;
sprintf(buffer, "%04x ", j);
col += 6;
// printk(KERN_INFO "%04x ", j);
for (i=0; i<16; i++) {
if (j >= size) {
buffer[col++] = '\n';
buffer[col] = 0;
printk(KERN_INFO "%s", buffer);
// printk(KERN_INFO "\n");
return;
}
sprintf(&buffer[col], " %02x", addr[j]);
col += 3;
// printk(KERN_INFO " %02x", addr[j]);
j++;
}
buffer[col++] = '\n';
buffer[col] = 0;
printk(KERN_INFO "%s", buffer);
// printk(KERN_INFO "\n");
col = 0;
}
}
int sawa_send_info(void) {
struct socket *conn_socket;
unsigned int tmp = 0xFFFFFFFF;
unsigned char buffer_out[12];
unsigned char buffer_in[4];
int *int_ptr = (int*)(buffer_out);
buffer_in[0] = 0x42;
*int_ptr = 1;
buffer_out[sizeof(int)] = SAWA_INFO;
if (tcp_client_connect(&conn_socket) < 0) {
printk(KERN_INFO "SaWa: could not connect\n");
return -1;
}
tcp_client_send(conn_socket, (const char *)&buffer_out, 5, MSG_WAITALL);
printk("SaWa: %x %x %x\n", buffer_in[0], tmp, *((int*)&buffer_out));
tcp_client_receive(conn_socket, (unsigned char *)&buffer_in, MSG_DONTWAIT, 4);
nb_sectors = *(unsigned int *)buffer_in;
printk("SaWa: Number of sectors: %d\n", nb_sectors);
sock_release(conn_socket);
return 0;
}
void sawa_handle_error(int errno) {
switch(errno) {
case SAWA_MSG_ERR:
printk(KERN_INFO "SaWa: failure to read (Error)\n");
break;
case SAWA_MSG_NOAUTH:
printk(KERN_INFO "SaWa: failure to read (Unauthorized)\n");
break;
default:
printk(KERN_INFO "SaWa: failure to read (unknown response code %x)\n", errno);
}
}
int sawa_write_data(sector_t sector, unsigned long nb_sectors, unsigned char *buffer) {
struct socket *conn_socket;
unsigned int offset = sector * 512;
unsigned int payload_size = nb_sectors * 512;
unsigned char *buffer_out = (unsigned char *)kmalloc(1 + sizeof(int)*2 + payload_size, GFP_KERNEL);
int *int_ptr = (int*)(buffer_out);
int buffer_start = 1 + 2*sizeof(int);
// char response = SAWA_MSG_ERR;
if (tcp_client_connect(&conn_socket) < 0) {
printk(KERN_INFO "SaWa: could not connect\n");
return -1;
}
printk(KERN_INFO "SaWa: Write data - Offset: %d, size: %d\n", offset, payload_size);
*int_ptr = payload_size + 1 +sizeof(int);
buffer_out[sizeof(int)] = SAWA_WRITE;
int_ptr = (int*)(buffer_out + 1 + sizeof(int));
*int_ptr = offset;
memcpy(buffer_out+buffer_start, buffer, payload_size);
tcp_client_send(conn_socket, (const char *)buffer_out, buffer_start + payload_size, MSG_WAITALL);
// Trying to read the error code doesn't work yet in kernel mode
// Unclear why
// tcp_client_receive(conn_socket, (unsigned char *)&response, MSG_DONTWAIT, 1);
kfree(buffer_out);
sock_release(conn_socket);
/* if (response != SAWA_MSG_OK) {
sawa_handle_error(response);
return -1;
}
*/
printk(KERN_INFO "SaWa: Wrote %lu sectors\n", nb_sectors);
return 0;
}
int sawa_read_data(sector_t sector, unsigned long nb_sectors, unsigned char *buffer_in) {
struct socket *conn_socket;
unsigned char buffer_out[13];
unsigned int offset = sector * 512;
unsigned int payload_size = nb_sectors * 512;
int *int_ptr = (int*)(&buffer_out);
int n;
memset(buffer_out, 0xFF, 13);
if (tcp_client_connect(&conn_socket) < 0) {
printk(KERN_INFO "SaWa: could not connect\n");
return -1;
}
printk(KERN_INFO "SaWa: Read data - Offset: %d, size: %d\n", offset, payload_size);
*int_ptr = 1 +sizeof(int)*2;
buffer_out[sizeof(int)] = SAWA_READ;
int_ptr = (int*)((char*)&buffer_out + 1 + sizeof(int));
*int_ptr = offset;
int_ptr = (int*)((char*)&buffer_out + 1 + 2*sizeof(int));
*int_ptr = payload_size;
tcp_client_send(conn_socket, (const char *)&buffer_out, sizeof(int)*3 + 1, MSG_WAITALL);
n = tcp_client_receive(conn_socket, (unsigned char *)buffer_in, MSG_DONTWAIT, payload_size);
sock_release(conn_socket);
if (n > 1) {
printk(KERN_INFO "SaWa: Read %lu sectors\n", nb_sectors);
return 0;
}
sawa_handle_error(buffer_in[0]);
return -1;
}